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