Salome HOME
Initial version
[modules/gui.git] / src / VTKViewer / VTKViewer_ExtractUnstructuredGrid.cxx
1 //  VISU CONVERTOR :
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 // File:    VISU_ExtractUnstructuredGrid.cxx
24 // Author:  Alexey PETROV
25 // Module : VISU
26
27
28 #include "VTKViewer_ExtractUnstructuredGrid.h"
29 //#include "utilities.h"
30
31 #include <vtkUnsignedCharArray.h>
32 #include <vtkUnstructuredGrid.h>
33 #include <vtkObjectFactory.h>
34 #include <vtkCellArray.h>
35 #include <vtkIntArray.h>
36 #include <vtkIdList.h>
37 #include <vtkCell.h>
38
39 using namespace std;
40
41 #ifdef _DEBUG_
42 static int MYDEBUG = 0;
43 #else
44 static int MYDEBUG = 0;
45 #endif
46
47 #if defined __GNUC__
48   #if __GNUC__ == 2
49     #define __GNUC_2__
50   #endif
51 #endif
52
53 vtkStandardNewMacro(VTKViewer_ExtractUnstructuredGrid);
54
55
56 VTKViewer_ExtractUnstructuredGrid::VTKViewer_ExtractUnstructuredGrid():
57   myExtractionMode(eCells), myChangeMode(ePassAll)
58 {}
59
60
61 VTKViewer_ExtractUnstructuredGrid::~VTKViewer_ExtractUnstructuredGrid(){}
62
63
64 void VTKViewer_ExtractUnstructuredGrid::RegisterCell(vtkIdType theCellId){
65 //  if(0 && MYDEBUG) MESSAGE("RegisterCell - theCellId = "<<theCellId);
66   myCellIds.insert(theCellId);
67   Modified();
68 }
69
70
71 void VTKViewer_ExtractUnstructuredGrid::RegisterCellsWithType(vtkIdType theCellType){
72 //  if(0 && MYDEBUG) MESSAGE("RegisterCellsWithType - theCellType = "<<theCellType);
73   myCellTypes.insert(theCellType);
74   Modified();
75 }
76
77
78 void VTKViewer_ExtractUnstructuredGrid::SetStoreMapping(int theStoreMapping){
79   myStoreMapping = theStoreMapping != 0;
80   this->Modified();
81 }
82
83 vtkIdType VTKViewer_ExtractUnstructuredGrid::GetInputId(int theOutId) const{
84   if(myCellIds.empty() && myCellTypes.empty()) return theOutId;
85   if(myOut2InId.empty() || theOutId > myOut2InId.size()) return -1;
86 #if defined __GNUC_2__
87   return myOut2InId[theOutId];
88 #else
89   return myOut2InId.at(theOutId);
90 #endif
91 }
92
93 vtkIdType VTKViewer_ExtractUnstructuredGrid::GetOutputId(int theInId) const{
94   if(myCellIds.empty() && myCellTypes.empty()) return theInId;
95   TMapId::const_iterator anIter = myIn2OutId.find(theInId);
96   if(anIter == myIn2OutId.end()) return -1;
97   return anIter->second;
98 }
99
100
101 inline void InsertCell(vtkUnstructuredGrid *theInput,
102                        vtkCellArray *theConnectivity, 
103                        vtkUnsignedCharArray* theCellTypesArray,
104                        vtkIdType theCellId, 
105                        vtkIdList *theIdList,
106                        bool theStoreMapping,
107                        vtkIdType theOutId, 
108                        VTKViewer_ExtractUnstructuredGrid::TVectorId& theOut2InId,
109                        VTKViewer_ExtractUnstructuredGrid::TMapId& theIn2OutId)
110 {
111   vtkCell *aCell = theInput->GetCell(theCellId);
112   vtkIdList *aPntIds = aCell->GetPointIds();
113   vtkIdType aNbIds = aPntIds->GetNumberOfIds();
114   theIdList->SetNumberOfIds(aNbIds);
115   for(vtkIdType i = 0; i < aNbIds; i++){
116     theIdList->SetId(i,aPntIds->GetId(i));
117   }
118   theConnectivity->InsertNextCell(theIdList);
119
120   vtkIdType aCellType = aCell->GetCellType();
121   theCellTypesArray->InsertNextValue(aCellType);
122   if(theStoreMapping){
123     theOut2InId.push_back(theCellId);
124     theIn2OutId[theCellId] = theOutId;
125   }
126 }
127
128 inline void InsertPointCell(vtkCellArray *theConnectivity, 
129                             vtkUnsignedCharArray* theCellTypesArray,
130                             vtkIdType theCellId, 
131                             vtkIdList *theIdList,
132                             bool theStoreMapping,
133                             vtkIdType theOutId, 
134                             VTKViewer_ExtractUnstructuredGrid::TVectorId& theOut2InId,
135                             VTKViewer_ExtractUnstructuredGrid::TMapId& theIn2OutId)
136 {
137   theIdList->SetId(0,theCellId);
138   theConnectivity->InsertNextCell(theIdList);
139   theCellTypesArray->InsertNextValue(VTK_VERTEX);
140   if(theStoreMapping){
141     theOut2InId.push_back(theCellId);
142     theIn2OutId[theCellId] = theOutId;
143   }
144 }
145
146 void VTKViewer_ExtractUnstructuredGrid::Execute(){
147   vtkUnstructuredGrid *anInput = this->GetInput();
148   vtkUnstructuredGrid *anOutput = this->GetOutput();
149   myOut2InId.clear();  myIn2OutId.clear();
150
151 /*  if(MYDEBUG){
152     MESSAGE("Execute - anInput->GetNumberOfCells() = "<<anInput->GetNumberOfCells());
153     MESSAGE("Execute - myCellTypes.size() = "<<myCellTypes.size());
154     MESSAGE("Execute - myCellIds.size() = "<<myCellIds.size());
155     MESSAGE("Execute - myExtractionMode = "<<myExtractionMode);
156     MESSAGE("Execute - myChangeMode = "<<myChangeMode);
157   }*/
158   if(myExtractionMode == eCells){
159     if(myChangeMode == ePassAll || myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving){
160       if(vtkIdType aNbElems = anInput->GetNumberOfCells()){
161         if(myStoreMapping) myOut2InId.reserve(aNbElems);
162         anOutput->ShallowCopy(anInput);
163         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
164           if(myStoreMapping){
165             myOut2InId.push_back(aCellId);
166             myIn2OutId[aCellId] = anOutId;
167           }
168         }
169       }
170     }else{
171       vtkIdList *anIdList = vtkIdList::New();
172       vtkCellArray *aConnectivity = vtkCellArray::New();
173       vtkIdType aNbElems = anInput->GetNumberOfCells();
174       aConnectivity->Allocate(2*aNbElems,0);
175       vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
176       aCellTypesArray->SetNumberOfComponents(1);
177       aCellTypesArray->Allocate(aNbElems*aCellTypesArray->GetNumberOfComponents());
178       if(!myCellIds.empty() && myCellTypes.empty()){
179         if(myStoreMapping) myOut2InId.reserve(myCellIds.size());
180         if(myChangeMode == eAdding){
181           for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
182             if(myCellIds.find(aCellId) != myCellIds.end()){
183               InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
184                          myStoreMapping,anOutId,myOut2InId,myIn2OutId);
185             }
186           }
187         }else{
188           for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
189             if(myCellIds.find(aCellId) == myCellIds.end()){
190               InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
191                          myStoreMapping,anOutId,myOut2InId,myIn2OutId);
192             }
193           }
194         }
195       }else if(myCellIds.empty() && !myCellTypes.empty()){
196         if(myChangeMode == eAdding){
197           for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
198             vtkIdType aType = anInput->GetCellType(aCellId);
199             if(myCellTypes.find(aType) != myCellTypes.end()){
200               InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
201                          myStoreMapping,anOutId,myOut2InId,myIn2OutId);
202             }
203           }
204         }else{
205           for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
206             vtkIdType aType = anInput->GetCellType(aCellId);
207             if(myCellTypes.find(aType) == myCellTypes.end()){
208               InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
209                          myStoreMapping,anOutId,myOut2InId,myIn2OutId);
210             }
211           }
212         }
213       }else if(!myCellIds.empty() && !myCellTypes.empty()){
214         if(myChangeMode == eAdding){
215           for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
216             vtkIdType aType = anInput->GetCellType(aCellId);
217             if(myCellTypes.find(aType) != myCellTypes.end()){
218               if(myCellIds.find(aCellId) != myCellIds.end()){
219                 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
220                            myStoreMapping,anOutId,myOut2InId,myIn2OutId);
221               }
222             }
223           }
224         }else{
225           for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
226             vtkIdType aType = anInput->GetCellType(aCellId);
227             if(myCellTypes.find(aType) == myCellTypes.end()){
228               if(myCellIds.find(aCellId) == myCellIds.end()){
229                 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
230                            myStoreMapping,anOutId,myOut2InId,myIn2OutId);
231               }
232             }
233           }
234         }
235       }
236       if((aNbElems = aConnectivity->GetNumberOfCells())){
237         vtkIntArray* aCellLocationsArray = vtkIntArray::New();
238         aCellLocationsArray->SetNumberOfComponents(1);
239         aCellLocationsArray->SetNumberOfTuples(aNbElems);
240         aConnectivity->InitTraversal();
241         for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
242           aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
243         }
244         anOutput->SetCells(aCellTypesArray,aCellLocationsArray,aConnectivity);
245         anOutput->SetPoints(anInput->GetPoints());
246         aCellLocationsArray->Delete();
247       }
248       aCellTypesArray->Delete();
249       aConnectivity->Delete();
250       anIdList->Delete();
251     }
252   }else{
253     vtkIdList *anIdList = vtkIdList::New();
254     anIdList->SetNumberOfIds(1);
255     vtkCellArray *aConnectivity = vtkCellArray::New();
256     vtkIdType aNbElems = anInput->GetNumberOfPoints();
257     aConnectivity->Allocate(2*aNbElems,0);
258     vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
259     aCellTypesArray->SetNumberOfComponents(1);
260     aCellTypesArray->Allocate(aNbElems*aCellTypesArray->GetNumberOfComponents());
261     if(myChangeMode == ePassAll || myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving){
262       if(myStoreMapping) myOut2InId.reserve(aNbElems);
263       for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
264         InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
265                         myStoreMapping,anOutId,myOut2InId,myIn2OutId);
266       }
267     }else if(!myCellIds.empty() && myCellTypes.empty()){
268       if(myStoreMapping) myOut2InId.reserve(myCellIds.size());
269       if(myChangeMode == eAdding){
270         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
271           if(myCellIds.find(aCellId) != myCellIds.end()){
272             InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
273                             myStoreMapping,anOutId,myOut2InId,myIn2OutId);
274           }
275         }
276       }else{
277         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
278           if(myCellIds.find(aCellId) == myCellIds.end()){
279             InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
280                             myStoreMapping,anOutId,myOut2InId,myIn2OutId);
281           }
282         }
283       }
284     }else if(myCellIds.empty() && !myCellTypes.empty()){
285       if(myChangeMode == eAdding){
286         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
287           vtkIdType aType = anInput->GetCellType(aCellId);
288           if(myCellTypes.find(aType) != myCellTypes.end()){
289             InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
290                             myStoreMapping,anOutId,myOut2InId,myIn2OutId);
291           }
292         }
293       }else{
294         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
295           vtkIdType aType = anInput->GetCellType(aCellId);
296           if(myCellTypes.find(aType) == myCellTypes.end()){
297             InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
298                             myStoreMapping,anOutId,myOut2InId,myIn2OutId);
299           }
300         }
301       }
302     }else if(!myCellIds.empty() && !myCellTypes.empty()){
303       if(myChangeMode == eAdding){
304         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
305           vtkIdType aType = anInput->GetCellType(aCellId);
306           if(myCellTypes.find(aType) != myCellTypes.end()){
307             if(myCellIds.find(aCellId) != myCellIds.end()){
308               InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
309                               myStoreMapping,anOutId,myOut2InId,myIn2OutId);
310             }
311           }
312         }
313       }else{
314         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
315           vtkIdType aType = anInput->GetCellType(aCellId);
316           if(myCellTypes.find(aType) == myCellTypes.end()){
317             if(myCellIds.find(aCellId) == myCellIds.end()){
318               InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
319                               myStoreMapping,anOutId,myOut2InId,myIn2OutId);
320             }
321           }
322         }
323       }
324     }
325     if((aNbElems = aConnectivity->GetNumberOfCells())){
326       vtkIntArray* aCellLocationsArray = vtkIntArray::New();
327       aCellLocationsArray->SetNumberOfComponents(1);
328       aCellLocationsArray->SetNumberOfTuples(aNbElems);
329       aConnectivity->InitTraversal();
330       for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
331         aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
332       }
333       anOutput->SetCells(aCellTypesArray,aCellLocationsArray,aConnectivity);
334       anOutput->SetPoints(anInput->GetPoints());
335       aCellLocationsArray->Delete();
336     }
337     aCellTypesArray->Delete();
338     aConnectivity->Delete();
339     anIdList->Delete();
340   }
341 /*  if(MYDEBUG){
342     MESSAGE("Execute - anOutput->GetNumberOfCells() = "<<anOutput->GetNumberOfCells());
343     if(myStoreMapping){
344       MESSAGE("Execute - myOut2InId.size() = "<<myOut2InId.size());
345       MESSAGE("Execute - myIn2OutId.size() = "<<myIn2OutId.size());
346     }
347   }*/
348 }