Salome HOME
Merge from OCC_development_generic_2006
[modules/gui.git] / src / VTKViewer / VTKViewer_AppendFilter.cxx
1 //  SALOME OBJECT : kernel of SALOME component
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 //
24 //  File   : VTKViewer_GeometryFilter.cxx
25 //  Author : 
26 //  Module : SALOME
27 //  $Header$
28
29 #include "VTKViewer_AppendFilter.h"
30
31 #include "VTKViewer_ConvexTool.h"
32
33 #include <vtkSmartPointer.h>
34 #include <vtkCellArray.h>
35 #include <vtkCellData.h>
36 #include <vtkGenericCell.h>
37 #include <vtkHexahedron.h>
38 #include <vtkMergePoints.h>
39 #include <vtkObjectFactory.h>
40 #include <vtkPointData.h>
41 #include <vtkPolyData.h>
42 #include <vtkPyramid.h>
43 #include <vtkStructuredGrid.h>
44 #include <vtkTetra.h>
45 #include <vtkUnsignedCharArray.h>
46 #include <vtkUnstructuredGrid.h>
47 #include <vtkVoxel.h>
48 #include <vtkWedge.h>
49 #include <vtkDataSetCollection.h>
50
51 #include <vector>
52 #include <map>
53 using namespace std;
54
55
56 #ifdef _DEBUG_
57 //static int MYDEBUG = 0;
58 //static int MYDEBUGWITHFILES = 0;
59 #else
60 //static int MYDEBUG = 0;
61 //static int MYDEBUGWITHFILES = 0;
62 #endif
63
64 #if defined __GNUC__
65   #if __GNUC__ == 2
66     #define __GNUC_2__
67   #endif
68 #endif
69
70 vtkCxxRevisionMacro(VTKViewer_AppendFilter, "$Revision$");
71 vtkStandardNewMacro(VTKViewer_AppendFilter);
72
73 VTKViewer_AppendFilter
74 ::VTKViewer_AppendFilter() 
75 {
76   myDoMappingFlag=false;
77 }
78
79 VTKViewer_AppendFilter
80 ::~VTKViewer_AppendFilter()
81 {}
82
83 void
84 VTKViewer_AppendFilter
85 ::SetDoMappingFlag(const bool theFlag)
86 {
87   myDoMappingFlag=theFlag;
88 }
89
90 bool 
91 VTKViewer_AppendFilter
92 ::DoMappingFlag() const
93 {
94   return myDoMappingFlag;
95 }
96
97 void
98 VTKViewer_AppendFilter
99 ::SetPoints(vtkPoints* thePoints)
100 {
101   myPoints = thePoints;
102 }
103
104 vtkPoints*
105 VTKViewer_AppendFilter
106 ::GetPoints()
107 {
108   return myPoints.GetPointer();
109 }
110
111 void
112 VTKViewer_AppendFilter
113 ::Execute()
114 {
115   if (myPoints.GetPointer()) {
116     MakeOutput();
117   }
118   else {
119     vtkAppendFilter::Execute();
120   }
121   if (myDoMappingFlag){
122     DoMapping();
123   }
124 }
125
126 void 
127 VTKViewer_AppendFilter
128 ::Reset()
129 {
130   myNodeIds.clear();
131   myCellIds.clear();
132   myNodeRanges.clear();
133   myCellRanges.clear();
134   myNodeMapObjIDVtkID.clear();
135   myCellMapObjIDVtkID.clear();
136 }
137 //==================================================================
138 // function: DoMapping
139 // purpose :
140 //==================================================================
141 void 
142 VTKViewer_AppendFilter
143 ::DoMapping()
144 {
145   int i, j, i1, i2, iNodeCnt, iCellCnt; 
146   IteratorOfDataMapOfIntegerInteger aMapIt;
147   vtkIdType aNbPnts, aNbCells, aId;
148   vtkDataSet *pDS;
149   //
150   Reset();
151   //
152   iNodeCnt=0;
153   iCellCnt=0;
154   for (i=0; i<NumberOfInputs; ++i) {
155     pDS=(vtkDataSet *)Inputs[i];
156     //
157     // Nodes
158     if (!myPoints.GetPointer()) {
159       aNbPnts=pDS->GetNumberOfPoints();
160       i1=myNodeIds.size();
161       i2=i1+aNbPnts-1;
162       myNodeRanges.push_back(i1);
163       myNodeRanges.push_back(i2);
164       //
165       for(j=0; j<aNbPnts; ++j) {
166         aId=(vtkIdType)j;
167         myNodeIds.push_back(aId);
168         //
169         aMapIt=myNodeMapObjIDVtkID.find(aId);
170         if (aMapIt==myNodeMapObjIDVtkID.end()) {
171           // if not found
172           myNodeMapObjIDVtkID[aId]=iNodeCnt;
173         }
174         ++iNodeCnt;
175       }
176     }
177     //
178     // Cells
179     aNbCells=pDS->GetNumberOfCells();
180     i1=myCellIds.size();
181     i2=i1+aNbCells-1;
182     myCellRanges.push_back(i1);
183     myCellRanges.push_back(i2);
184     for(j=0; j<aNbCells; ++j) {
185       aId=(vtkIdType)j;
186       myCellIds.push_back(aId);
187       //
188       aMapIt=myCellMapObjIDVtkID.find(aId);
189       if (aMapIt==myCellMapObjIDVtkID.end()) {
190         // if not found
191         myCellMapObjIDVtkID[aId]=iCellCnt;
192       }
193       ++iCellCnt;
194     }
195   }
196 }
197
198 //---------------------------------------------------------------
199 vtkIdType
200 VTKViewer_AppendFilter
201 ::GetPointOutputID(vtkIdType theInputID)
202 {
203   if (myPoints.GetPointer()) {
204     return theInputID;
205   }
206   //
207   int aVtkID=-1;
208   IteratorOfDataMapOfIntegerInteger aMapIt;
209   //
210   aMapIt=myNodeMapObjIDVtkID.find(theInputID);
211   if (aMapIt!=myNodeMapObjIDVtkID.end()) {
212     // found
213     PairOfDataMapOfIntegerInteger& aPair=(*aMapIt);
214     aVtkID=aPair.second;
215   }
216   return aVtkID;
217 }
218
219
220 //---------------------------------------------------------------
221 vtkIdType 
222 VTKViewer_AppendFilter
223 ::GetCellOutputID(vtkIdType theInputID)
224 {
225   int aVtkID=-1;
226   IteratorOfDataMapOfIntegerInteger aMapIt;
227   //
228   aMapIt=myCellMapObjIDVtkID.find(theInputID);
229   if (aMapIt!=myCellMapObjIDVtkID.end()) {
230     // found
231     PairOfDataMapOfIntegerInteger& aPair=(*aMapIt);
232     aVtkID=aPair.second;
233   }
234   return aVtkID;
235 }
236
237
238 //---------------------------------------------------------------
239 vtkIdType 
240 VTKViewer_AppendFilter
241 ::GetPointInputID(vtkIdType theOutputID, 
242                   vtkIdType& theInputDataSetID)
243 {
244   if (myPoints.GetPointer()) {
245     theInputDataSetID=0;
246     return theOutputID;
247   }
248   //
249   int aNb, aNbRanges, aRetID, i, i1, i2, j;
250   //
251   aRetID=-1;
252   theInputDataSetID=-1;
253   //
254   aNb=myNodeIds.size();
255   if (theOutputID<0 ||  theOutputID>=aNb) {
256     return aRetID;
257   }
258   //
259   aRetID=(int)myNodeIds[theOutputID];
260   //
261   aNbRanges=myNodeRanges.size()/2;
262   for (i=0; i<aNbRanges; ++i) {
263     j=2*i;
264     i1=myNodeRanges[j];
265     i2=myNodeRanges[j+1];
266     if (theOutputID>=i1 && theOutputID<=i2) {
267       theInputDataSetID=i;
268     }
269   }
270   //
271   return aRetID;
272 }
273
274
275 //---------------------------------------------------------------
276 vtkIdType 
277 VTKViewer_AppendFilter
278 ::GetCellInputID(vtkIdType theOutputID, 
279                  vtkIdType& theInputDataSetID)
280 {
281   int aNb, aNbRanges, aRetID, i, i1, i2, j;
282   //
283   aRetID=-1;
284   theInputDataSetID=-1;
285   //
286   aNb=myCellIds.size();
287   if (theOutputID<0 ||  theOutputID>=aNb) {
288     return aRetID;
289   }
290   //
291   aRetID=(int)myCellIds[theOutputID];
292   //
293   aNbRanges=myCellRanges.size()/2;
294   for (i=0; i<aNbRanges; ++i) {
295     j=2*i;
296     i1=myCellRanges[j];
297     i2=myCellRanges[j+1];
298     if (theOutputID>=i1 && theOutputID<=i2) {
299       theInputDataSetID=i;
300     }
301   }
302   //
303   return aRetID;
304 }
305
306
307 //---------------------------------------------------------------
308 void 
309 VTKViewer_AppendFilter
310 ::MakeOutput()
311 {
312   int idx;
313   vtkIdType numPts, numCells, newCellId, cellId;
314   vtkCellData *cd;
315   vtkIdList *ptIds;
316   vtkDataSet *ds;
317   vtkUnstructuredGrid *output = this->GetOutput();
318   //
319   numPts = myPoints->GetNumberOfPoints();
320   if (numPts < 1) {
321     return;
322   }
323   //
324   numCells = 0;
325   for (idx = 0; idx < this->NumberOfInputs; ++idx) {
326     ds = (vtkDataSet *)(this->Inputs[idx]);
327     if (ds != NULL)  {
328       if ( ds->GetNumberOfPoints() <= 0 && ds->GetNumberOfCells() <= 0 )  {
329         continue; //no input, just skip
330       }
331       numCells += ds->GetNumberOfCells();
332     }//if non-empty dataset
333   }//for all inputs
334   if (numCells < 1) {
335     return;
336   }
337   //
338   // Now can allocate memory
339   output->Allocate(numCells); 
340   ptIds = vtkIdList::New(); 
341   ptIds->Allocate(VTK_CELL_SIZE);
342   //
343   // Append each input dataset together
344   //
345   // 1.points
346   output->SetPoints(myPoints.GetPointer());
347   // 2.cells
348   for (idx = 0; idx < this->NumberOfInputs; ++idx) {
349     ds = (vtkDataSet *)(this->Inputs[idx]);
350     if (ds != NULL) {
351       numCells = ds->GetNumberOfCells(); 
352       cd = ds->GetCellData();
353       // copy cell and cell data
354       for (cellId=0; cellId<numCells; cellId++)  {
355         ds->GetCellPoints(cellId, ptIds);
356         newCellId = output->InsertNextCell(ds->GetCellType(cellId), ptIds);
357       }
358     }
359   }
360   //
361   ptIds->Delete();
362 }
363