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