Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[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 "VTKViewer_CellLocationsArray.h"
30
31 #include <vtkUnsignedCharArray.h>
32 #include <vtkUnstructuredGrid.h>
33 #include <vtkObjectFactory.h>
34 #include <vtkCellArray.h>
35 #include <vtkIdList.h>
36 #include <vtkCell.h>
37
38 using namespace std;
39
40 #ifdef _DEBUG_
41 static int MYDEBUG = 0;
42 #else
43 static int MYDEBUG = 0;
44 #endif
45
46 #if defined __GNUC__
47   #if __GNUC__ == 2
48     #define __GNUC_2__
49   #endif
50 #endif
51
52 vtkStandardNewMacro(VTKViewer_ExtractUnstructuredGrid);
53
54
55 VTKViewer_ExtractUnstructuredGrid::VTKViewer_ExtractUnstructuredGrid():
56   myExtractionMode(eCells), myChangeMode(ePassAll)
57 {}
58
59
60 VTKViewer_ExtractUnstructuredGrid::~VTKViewer_ExtractUnstructuredGrid(){}
61
62
63 void VTKViewer_ExtractUnstructuredGrid::RegisterCell(vtkIdType theCellId){
64 //  if(0 && MYDEBUG) MESSAGE("RegisterCell - theCellId = "<<theCellId);
65   myCellIds.insert(theCellId);
66   Modified();
67 }
68
69
70 void VTKViewer_ExtractUnstructuredGrid::RegisterCellsWithType(vtkIdType theCellType){
71 //  if(0 && MYDEBUG) MESSAGE("RegisterCellsWithType - theCellType = "<<theCellType);
72   myCellTypes.insert(theCellType);
73   Modified();
74 }
75
76
77 void VTKViewer_ExtractUnstructuredGrid::SetStoreMapping(int theStoreMapping){
78   myStoreMapping = theStoreMapping != 0;
79   this->Modified();
80 }
81
82 vtkIdType VTKViewer_ExtractUnstructuredGrid::GetInputId(int theOutId) const{
83   if(myCellIds.empty() && myCellTypes.empty()) return theOutId;
84   if(myOut2InId.empty() || theOutId > myOut2InId.size()) return -1;
85 #if defined __GNUC_2__
86   return myOut2InId[theOutId];
87 #else
88   return myOut2InId.at(theOutId);
89 #endif
90 }
91
92 vtkIdType VTKViewer_ExtractUnstructuredGrid::GetOutputId(int theInId) const{
93   if(myCellIds.empty() && myCellTypes.empty()) return theInId;
94   TMapId::const_iterator anIter = myIn2OutId.find(theInId);
95   if(anIter == myIn2OutId.end()) return -1;
96   return anIter->second;
97 }
98
99
100 inline void InsertCell(vtkUnstructuredGrid *theInput,
101                        vtkCellArray *theConnectivity, 
102                        vtkUnsignedCharArray* theCellTypesArray,
103                        vtkIdType theCellId, 
104                        vtkIdList *theIdList,
105                        bool theStoreMapping,
106                        vtkIdType theOutId, 
107                        VTKViewer_ExtractUnstructuredGrid::TVectorId& theOut2InId,
108                        VTKViewer_ExtractUnstructuredGrid::TMapId& theIn2OutId)
109 {
110   vtkCell *aCell = theInput->GetCell(theCellId);
111   vtkIdList *aPntIds = aCell->GetPointIds();
112   vtkIdType aNbIds = aPntIds->GetNumberOfIds();
113   theIdList->SetNumberOfIds(aNbIds);
114   for(vtkIdType i = 0; i < aNbIds; i++){
115     theIdList->SetId(i,aPntIds->GetId(i));
116   }
117   theConnectivity->InsertNextCell(theIdList);
118
119   vtkIdType aCellType = aCell->GetCellType();
120   theCellTypesArray->InsertNextValue(aCellType);
121   if(theStoreMapping){
122     theOut2InId.push_back(theCellId);
123     theIn2OutId[theCellId] = theOutId;
124   }
125 }
126
127 inline void InsertPointCell(vtkCellArray *theConnectivity, 
128                             vtkUnsignedCharArray* theCellTypesArray,
129                             vtkIdType theCellId, 
130                             vtkIdList *theIdList,
131                             bool theStoreMapping,
132                             vtkIdType theOutId, 
133                             VTKViewer_ExtractUnstructuredGrid::TVectorId& theOut2InId,
134                             VTKViewer_ExtractUnstructuredGrid::TMapId& theIn2OutId)
135 {
136   theIdList->SetId(0,theCellId);
137   theConnectivity->InsertNextCell(theIdList);
138   theCellTypesArray->InsertNextValue(VTK_VERTEX);
139   if(theStoreMapping){
140     theOut2InId.push_back(theCellId);
141     theIn2OutId[theCellId] = theOutId;
142   }
143 }
144
145 void VTKViewer_ExtractUnstructuredGrid::Execute(){
146   vtkUnstructuredGrid *anInput = this->GetInput();
147   vtkUnstructuredGrid *anOutput = this->GetOutput();
148   myOut2InId.clear();  myIn2OutId.clear();
149
150 /*  if(MYDEBUG){
151     MESSAGE("Execute - anInput->GetNumberOfCells() = "<<anInput->GetNumberOfCells());
152     MESSAGE("Execute - myCellTypes.size() = "<<myCellTypes.size());
153     MESSAGE("Execute - myCellIds.size() = "<<myCellIds.size());
154     MESSAGE("Execute - myExtractionMode = "<<myExtractionMode);
155     MESSAGE("Execute - myChangeMode = "<<myChangeMode);
156   }*/
157   if(myExtractionMode == eCells){
158     if(myChangeMode == ePassAll || myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving){
159       if(vtkIdType aNbElems = anInput->GetNumberOfCells()){
160         if(myStoreMapping) myOut2InId.reserve(aNbElems);
161         anOutput->ShallowCopy(anInput);
162         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
163           if(myStoreMapping){
164             myOut2InId.push_back(aCellId);
165             myIn2OutId[aCellId] = anOutId;
166           }
167         }
168       }
169     }else{
170       vtkIdList *anIdList = vtkIdList::New();
171       vtkCellArray *aConnectivity = vtkCellArray::New();
172       vtkIdType aNbElems = anInput->GetNumberOfCells();
173       aConnectivity->Allocate(2*aNbElems,0);
174       vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
175       aCellTypesArray->SetNumberOfComponents(1);
176       aCellTypesArray->Allocate(aNbElems*aCellTypesArray->GetNumberOfComponents());
177       if(!myCellIds.empty() && myCellTypes.empty()){
178         if(myStoreMapping) myOut2InId.reserve(myCellIds.size());
179         if(myChangeMode == eAdding){
180           for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
181             if(myCellIds.find(aCellId) != myCellIds.end()){
182               InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
183                          myStoreMapping,anOutId,myOut2InId,myIn2OutId);
184             }
185           }
186         }else{
187           for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
188             if(myCellIds.find(aCellId) == myCellIds.end()){
189               InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
190                          myStoreMapping,anOutId,myOut2InId,myIn2OutId);
191             }
192           }
193         }
194       }else if(myCellIds.empty() && !myCellTypes.empty()){
195         if(myChangeMode == eAdding){
196           for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
197             vtkIdType aType = anInput->GetCellType(aCellId);
198             if(myCellTypes.find(aType) != myCellTypes.end()){
199               InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
200                          myStoreMapping,anOutId,myOut2InId,myIn2OutId);
201             }
202           }
203         }else{
204           for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
205             vtkIdType aType = anInput->GetCellType(aCellId);
206             if(myCellTypes.find(aType) == myCellTypes.end()){
207               InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
208                          myStoreMapping,anOutId,myOut2InId,myIn2OutId);
209             }
210           }
211         }
212       }else if(!myCellIds.empty() && !myCellTypes.empty()){
213         if(myChangeMode == eAdding){
214           for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
215             vtkIdType aType = anInput->GetCellType(aCellId);
216             if(myCellTypes.find(aType) != myCellTypes.end()){
217               if(myCellIds.find(aCellId) != myCellIds.end()){
218                 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
219                            myStoreMapping,anOutId,myOut2InId,myIn2OutId);
220               }
221             }
222           }
223         }else{
224           for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
225             vtkIdType aType = anInput->GetCellType(aCellId);
226             if(myCellTypes.find(aType) == myCellTypes.end()){
227               if(myCellIds.find(aCellId) == myCellIds.end()){
228                 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
229                            myStoreMapping,anOutId,myOut2InId,myIn2OutId);
230               }
231             }
232           }
233         }
234       }
235       if((aNbElems = aConnectivity->GetNumberOfCells())){
236         VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
237         aCellLocationsArray->SetNumberOfComponents(1);
238         aCellLocationsArray->SetNumberOfTuples(aNbElems);
239         aConnectivity->InitTraversal();
240         for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
241           aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
242         }
243         anOutput->SetCells(aCellTypesArray,aCellLocationsArray,aConnectivity);
244         anOutput->SetPoints(anInput->GetPoints());
245         aCellLocationsArray->Delete();
246       }
247       aCellTypesArray->Delete();
248       aConnectivity->Delete();
249       anIdList->Delete();
250     }
251   }else{
252     vtkIdList *anIdList = vtkIdList::New();
253     anIdList->SetNumberOfIds(1);
254     vtkCellArray *aConnectivity = vtkCellArray::New();
255     vtkIdType aNbElems = anInput->GetNumberOfPoints();
256     aConnectivity->Allocate(2*aNbElems,0);
257     vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
258     aCellTypesArray->SetNumberOfComponents(1);
259     aCellTypesArray->Allocate(aNbElems*aCellTypesArray->GetNumberOfComponents());
260     if(myChangeMode == ePassAll || myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving){
261       if(myStoreMapping) myOut2InId.reserve(aNbElems);
262       for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
263         InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
264                         myStoreMapping,anOutId,myOut2InId,myIn2OutId);
265       }
266     }else if(!myCellIds.empty() && myCellTypes.empty()){
267       if(myStoreMapping) myOut2InId.reserve(myCellIds.size());
268       if(myChangeMode == eAdding){
269         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
270           if(myCellIds.find(aCellId) != myCellIds.end()){
271             InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
272                             myStoreMapping,anOutId,myOut2InId,myIn2OutId);
273           }
274         }
275       }else{
276         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
277           if(myCellIds.find(aCellId) == myCellIds.end()){
278             InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
279                             myStoreMapping,anOutId,myOut2InId,myIn2OutId);
280           }
281         }
282       }
283     }else if(myCellIds.empty() && !myCellTypes.empty()){
284       if(myChangeMode == eAdding){
285         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
286           vtkIdType aType = anInput->GetCellType(aCellId);
287           if(myCellTypes.find(aType) != myCellTypes.end()){
288             InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
289                             myStoreMapping,anOutId,myOut2InId,myIn2OutId);
290           }
291         }
292       }else{
293         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
294           vtkIdType aType = anInput->GetCellType(aCellId);
295           if(myCellTypes.find(aType) == myCellTypes.end()){
296             InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
297                             myStoreMapping,anOutId,myOut2InId,myIn2OutId);
298           }
299         }
300       }
301     }else if(!myCellIds.empty() && !myCellTypes.empty()){
302       if(myChangeMode == eAdding){
303         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
304           vtkIdType aType = anInput->GetCellType(aCellId);
305           if(myCellTypes.find(aType) != myCellTypes.end()){
306             if(myCellIds.find(aCellId) != myCellIds.end()){
307               InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
308                               myStoreMapping,anOutId,myOut2InId,myIn2OutId);
309             }
310           }
311         }
312       }else{
313         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
314           vtkIdType aType = anInput->GetCellType(aCellId);
315           if(myCellTypes.find(aType) == myCellTypes.end()){
316             if(myCellIds.find(aCellId) == myCellIds.end()){
317               InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
318                               myStoreMapping,anOutId,myOut2InId,myIn2OutId);
319             }
320           }
321         }
322       }
323     }
324     if((aNbElems = aConnectivity->GetNumberOfCells())){
325       VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
326       aCellLocationsArray->SetNumberOfComponents(1);
327       aCellLocationsArray->SetNumberOfTuples(aNbElems);
328       aConnectivity->InitTraversal();
329       for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
330         aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
331       }
332       anOutput->SetCells(aCellTypesArray,aCellLocationsArray,aConnectivity);
333       anOutput->SetPoints(anInput->GetPoints());
334       aCellLocationsArray->Delete();
335     }
336     aCellTypesArray->Delete();
337     aConnectivity->Delete();
338     anIdList->Delete();
339   }
340 /*  if(MYDEBUG){
341     MESSAGE("Execute - anOutput->GetNumberOfCells() = "<<anOutput->GetNumberOfCells());
342     if(myStoreMapping){
343       MESSAGE("Execute - myOut2InId.size() = "<<myOut2InId.size());
344       MESSAGE("Execute - myIn2OutId.size() = "<<myIn2OutId.size());
345     }
346   }*/
347 }