Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / VTKViewer / VTKViewer_ExtractUnstructuredGrid.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  VISU CONVERTOR :
23 // File:    VISU_ExtractUnstructuredGrid.cxx
24 // Author:  Alexey PETROV
25 // Module : VISU
26 //
27 #include "VTKViewer_ExtractUnstructuredGrid.h"
28 #include "VTKViewer_CellLocationsArray.h"
29
30 #include <vtkUnsignedCharArray.h>
31 #include <vtkUnstructuredGrid.h>
32 #include <vtkObjectFactory.h>
33 #include <vtkCellArray.h>
34 #include <vtkIdList.h>
35 #include <vtkCell.h>
36 #include <vtkInformation.h>
37 #include <vtkInformationVector.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 {
85   if ( myCellIds.empty() && myCellTypes.empty() )
86     return theOutId;
87
88   if ( myOut2InId.empty() || theOutId > (int)myOut2InId.size() )
89     return -1;
90 #if defined __GNUC_2__
91   return myOut2InId[theOutId];
92 #else
93   return myOut2InId.at(theOutId);
94 #endif
95 }
96
97 vtkIdType VTKViewer_ExtractUnstructuredGrid::GetOutputId(int theInId) const{
98   if(myCellIds.empty() && myCellTypes.empty()) return theInId;
99   TMapId::const_iterator anIter = myIn2OutId.find(theInId);
100   if(anIter == myIn2OutId.end()) return -1;
101   return anIter->second;
102 }
103
104
105 inline void InsertCell(vtkUnstructuredGrid *theInput,
106                        vtkCellArray *theConnectivity, 
107                        vtkUnsignedCharArray* theCellTypesArray,
108                        vtkIdType theCellId, 
109                        vtkIdList *theIdList,
110                        bool theStoreMapping,
111                        vtkIdType theOutId, 
112                        VTKViewer_ExtractUnstructuredGrid::TVectorId& theOut2InId,
113                        VTKViewer_ExtractUnstructuredGrid::TMapId& theIn2OutId)
114 {
115   vtkCell *aCell = theInput->GetCell(theCellId);
116   vtkIdList *aPntIds = aCell->GetPointIds();
117   vtkIdType aNbIds = aPntIds->GetNumberOfIds();
118   theIdList->SetNumberOfIds(aNbIds);
119   for(vtkIdType i = 0; i < aNbIds; i++){
120     theIdList->SetId(i,aPntIds->GetId(i));
121   }
122   theConnectivity->InsertNextCell(theIdList);
123
124   vtkIdType aCellType = aCell->GetCellType();
125   theCellTypesArray->InsertNextValue(aCellType);
126   if(theStoreMapping){
127     theOut2InId.push_back(theCellId);
128     theIn2OutId[theCellId] = theOutId;
129   }
130 }
131
132 inline void InsertPointCell(vtkCellArray *theConnectivity, 
133                             vtkUnsignedCharArray* theCellTypesArray,
134                             vtkIdType theCellId, 
135                             vtkIdList *theIdList,
136                             bool theStoreMapping,
137                             vtkIdType theOutId, 
138                             VTKViewer_ExtractUnstructuredGrid::TVectorId& theOut2InId,
139                             VTKViewer_ExtractUnstructuredGrid::TMapId& theIn2OutId)
140 {
141   theIdList->SetId(0,theCellId);
142   theConnectivity->InsertNextCell(theIdList);
143   theCellTypesArray->InsertNextValue(VTK_VERTEX);
144   if(theStoreMapping){
145     theOut2InId.push_back(theCellId);
146     theIn2OutId[theCellId] = theOutId;
147   }
148 }
149
150
151 // int VTKViewer_ExtractUnstructuredGrid::RequestData(
152 //   vtkInformation *vtkNotUsed(request),
153 //   vtkInformationVector **inputVector,
154 //   vtkInformationVector *outputVector)
155 void VTKViewer_ExtractUnstructuredGrid::Execute()
156 {
157   /*
158   not ported yet to the new executive-based pipeline architecture.
159
160   // get the info objects
161   vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
162   vtkInformation *outInfo = outputVector->GetInformationObject(0);
163
164   // get the input and ouptut
165   vtkUnstructuredGrid *anInput = vtkUnstructuredGrid::SafeDownCast(
166     inInfo->Get(vtkDataObject::DATA_OBJECT()));
167   vtkUnstructuredGrid *anOutput = vtkUnstructuredGrid::SafeDownCast(
168     outInfo->Get(vtkDataObject::DATA_OBJECT()));
169   */
170   vtkUnstructuredGrid *anInput = this->GetInput();
171   vtkUnstructuredGrid *anOutput = this->GetOutput();
172   
173   myOut2InId.clear();  myIn2OutId.clear();
174
175 /*  if(MYDEBUG){
176     MESSAGE("Execute - anInput->GetNumberOfCells() = "<<anInput->GetNumberOfCells());
177     MESSAGE("Execute - myCellTypes.size() = "<<myCellTypes.size());
178     MESSAGE("Execute - myCellIds.size() = "<<myCellIds.size());
179     MESSAGE("Execute - myExtractionMode = "<<myExtractionMode);
180     MESSAGE("Execute - myChangeMode = "<<myChangeMode);
181   }*/
182   if(myExtractionMode == eCells){
183     if(myChangeMode == ePassAll || myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving){
184       if(vtkIdType aNbElems = anInput->GetNumberOfCells()){
185         if(myStoreMapping) myOut2InId.reserve(aNbElems);
186         anOutput->ShallowCopy(anInput);
187         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
188           if(myStoreMapping){
189             myOut2InId.push_back(aCellId);
190             myIn2OutId[aCellId] = anOutId;
191           }
192         }
193       }
194     }else{
195       vtkIdList *anIdList = vtkIdList::New();
196       vtkCellArray *aConnectivity = vtkCellArray::New();
197       vtkIdType aNbElems = anInput->GetNumberOfCells();
198       aConnectivity->Allocate(2*aNbElems,0);
199       vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
200       aCellTypesArray->SetNumberOfComponents(1);
201       aCellTypesArray->Allocate(aNbElems*aCellTypesArray->GetNumberOfComponents());
202       if(!myCellIds.empty() && myCellTypes.empty()){
203         if(myStoreMapping) myOut2InId.reserve(myCellIds.size());
204         if(myChangeMode == eAdding){
205           for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
206             if(myCellIds.find(aCellId) != myCellIds.end()){
207               InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
208                          myStoreMapping,anOutId,myOut2InId,myIn2OutId);
209             }
210           }
211         }else{
212           for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
213             if(myCellIds.find(aCellId) == myCellIds.end()){
214               InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
215                          myStoreMapping,anOutId,myOut2InId,myIn2OutId);
216             }
217           }
218         }
219       }else if(myCellIds.empty() && !myCellTypes.empty()){
220         if(myChangeMode == eAdding){
221           for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
222             vtkIdType aType = anInput->GetCellType(aCellId);
223             if(myCellTypes.find(aType) != myCellTypes.end()){
224               InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
225                          myStoreMapping,anOutId,myOut2InId,myIn2OutId);
226             }
227           }
228         }else{
229           for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
230             vtkIdType aType = anInput->GetCellType(aCellId);
231             if(myCellTypes.find(aType) == myCellTypes.end()){
232               InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
233                          myStoreMapping,anOutId,myOut2InId,myIn2OutId);
234             }
235           }
236         }
237       }else if(!myCellIds.empty() && !myCellTypes.empty()){
238         if(myChangeMode == eAdding){
239           for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
240             vtkIdType aType = anInput->GetCellType(aCellId);
241             if(myCellTypes.find(aType) != myCellTypes.end()){
242               if(myCellIds.find(aCellId) != myCellIds.end()){
243                 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
244                            myStoreMapping,anOutId,myOut2InId,myIn2OutId);
245               }
246             }
247           }
248         }else{
249           for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
250             vtkIdType aType = anInput->GetCellType(aCellId);
251             if(myCellTypes.find(aType) == myCellTypes.end()){
252               if(myCellIds.find(aCellId) == myCellIds.end()){
253                 InsertCell(anInput,aConnectivity,aCellTypesArray,aCellId,anIdList,
254                            myStoreMapping,anOutId,myOut2InId,myIn2OutId);
255               }
256             }
257           }
258         }
259       }
260       if((aNbElems = aConnectivity->GetNumberOfCells())){
261         VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
262         aCellLocationsArray->SetNumberOfComponents(1);
263         aCellLocationsArray->SetNumberOfTuples(aNbElems);
264         aConnectivity->InitTraversal();
265         for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
266           aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
267         }
268         anOutput->SetCells(aCellTypesArray,aCellLocationsArray,aConnectivity);
269         anOutput->SetPoints(anInput->GetPoints());
270         aCellLocationsArray->Delete();
271       }
272       aCellTypesArray->Delete();
273       aConnectivity->Delete();
274       anIdList->Delete();
275     }
276   }else{
277     vtkIdList *anIdList = vtkIdList::New();
278     anIdList->SetNumberOfIds(1);
279     vtkCellArray *aConnectivity = vtkCellArray::New();
280     vtkIdType aNbElems = anInput->GetNumberOfPoints();
281     aConnectivity->Allocate(2*aNbElems,0);
282     vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
283     aCellTypesArray->SetNumberOfComponents(1);
284     aCellTypesArray->Allocate(aNbElems*aCellTypesArray->GetNumberOfComponents());
285     if(myChangeMode == ePassAll || myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving){
286       if(myStoreMapping) myOut2InId.reserve(aNbElems);
287       for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
288         InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
289                         myStoreMapping,anOutId,myOut2InId,myIn2OutId);
290       }
291     }else if(!myCellIds.empty() && myCellTypes.empty()){
292       if(myStoreMapping) myOut2InId.reserve(myCellIds.size());
293       if(myChangeMode == eAdding){
294         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
295           if(myCellIds.find(aCellId) != myCellIds.end()){
296             InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
297                             myStoreMapping,anOutId,myOut2InId,myIn2OutId);
298           }
299         }
300       }else{
301         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
302           if(myCellIds.find(aCellId) == myCellIds.end()){
303             InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
304                             myStoreMapping,anOutId,myOut2InId,myIn2OutId);
305           }
306         }
307       }
308     }else if(myCellIds.empty() && !myCellTypes.empty()){
309       if(myChangeMode == eAdding){
310         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
311           vtkIdType aType = anInput->GetCellType(aCellId);
312           if(myCellTypes.find(aType) != myCellTypes.end()){
313             InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
314                             myStoreMapping,anOutId,myOut2InId,myIn2OutId);
315           }
316         }
317       }else{
318         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
319           vtkIdType aType = anInput->GetCellType(aCellId);
320           if(myCellTypes.find(aType) == myCellTypes.end()){
321             InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
322                             myStoreMapping,anOutId,myOut2InId,myIn2OutId);
323           }
324         }
325       }
326     }else if(!myCellIds.empty() && !myCellTypes.empty()){
327       if(myChangeMode == eAdding){
328         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
329           vtkIdType aType = anInput->GetCellType(aCellId);
330           if(myCellTypes.find(aType) != myCellTypes.end()){
331             if(myCellIds.find(aCellId) != myCellIds.end()){
332               InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
333                               myStoreMapping,anOutId,myOut2InId,myIn2OutId);
334             }
335           }
336         }
337       }else{
338         for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
339           vtkIdType aType = anInput->GetCellType(aCellId);
340           if(myCellTypes.find(aType) == myCellTypes.end()){
341             if(myCellIds.find(aCellId) == myCellIds.end()){
342               InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
343                               myStoreMapping,anOutId,myOut2InId,myIn2OutId);
344             }
345           }
346         }
347       }
348     }
349     if((aNbElems = aConnectivity->GetNumberOfCells())){
350       VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
351       aCellLocationsArray->SetNumberOfComponents(1);
352       aCellLocationsArray->SetNumberOfTuples(aNbElems);
353       aConnectivity->InitTraversal();
354       for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
355         aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
356       }
357       anOutput->SetCells(aCellTypesArray,aCellLocationsArray,aConnectivity);
358       anOutput->SetPoints(anInput->GetPoints());
359       aCellLocationsArray->Delete();
360     }
361     aCellTypesArray->Delete();
362     aConnectivity->Delete();
363     anIdList->Delete();
364   }
365 /*  if(MYDEBUG){
366     MESSAGE("Execute - anOutput->GetNumberOfCells() = "<<anOutput->GetNumberOfCells());
367     if(myStoreMapping){
368       MESSAGE("Execute - myOut2InId.size() = "<<myOut2InId.size());
369       MESSAGE("Execute - myIn2OutId.size() = "<<myIn2OutId.size());
370     }
371   }*/
372 //  return 1;
373 }