Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : VTKViewer_GeometryFilter.cxx
25 //  Author : 
26 //  Module : SALOME
27 //  $Header$
28
29 #include "VTKViewer_AppendFilter.h"
30
31 #include <vtkCell.h>
32 #include <vtkCellData.h>
33 #include <vtkDataSetAttributes.h>
34 #include <vtkDataSetCollection.h>
35 #include <vtkObjectFactory.h>
36 #include <vtkPointData.h>
37 #include <vtkUnstructuredGrid.h>
38
39 #include <vtkPoints.h>
40
41 #if defined __GNUC__
42   #if __GNUC__ == 2
43     #define __GNUC_2__
44   #endif
45 #endif
46
47 vtkCxxRevisionMacro(VTKViewer_AppendFilter, "$Revision$");
48 vtkStandardNewMacro(VTKViewer_AppendFilter);
49
50 VTKViewer_AppendFilter
51 ::VTKViewer_AppendFilter() 
52 {
53   myDoMappingFlag = false;
54 }
55
56 VTKViewer_AppendFilter
57 ::~VTKViewer_AppendFilter()
58 {}
59
60 void
61 VTKViewer_AppendFilter
62 ::SetDoMappingFlag(const bool theFlag)
63 {
64   if(myDoMappingFlag == theFlag)
65     return;
66
67   myDoMappingFlag = theFlag;
68
69   this->Modified();
70 }
71
72 bool 
73 VTKViewer_AppendFilter
74 ::DoMappingFlag() const
75 {
76   return myDoMappingFlag;
77 }
78
79 void
80 VTKViewer_AppendFilter
81 ::SetPoints(vtkPoints* thePoints)
82 {
83   if(GetPoints() == thePoints)
84     return;
85
86   myPoints = thePoints;
87
88   this->Modified();
89 }
90
91 vtkPoints*
92 VTKViewer_AppendFilter
93 ::GetPoints()
94 {
95   return myPoints.GetPointer();
96 }
97
98 void
99 VTKViewer_AppendFilter
100 ::Execute()
101 {
102   if(myPoints.GetPointer())
103     MakeOutput();
104   else
105     Superclass::Execute();
106
107   if(myDoMappingFlag)
108     DoMapping();
109 }
110
111
112 void 
113 VTKViewer_AppendFilter
114 ::DoMapping()
115 {
116   myNodeRanges.clear();
117   myCellRanges.clear();
118
119   vtkIdType aPntStartId = 0;
120   vtkIdType aCellStartId = 0;
121
122   for(vtkIdType aDataSetId = 0; aDataSetId < this->NumberOfInputs; ++aDataSetId){
123     vtkDataSet* aDataSet = (vtkDataSet *)(this->Inputs[aDataSetId]);
124     // Do mapping of the nodes
125     if(!myPoints.GetPointer()){
126       vtkIdType aNbPnts = aDataSet->GetNumberOfPoints();
127       myNodeRanges.push_back(aPntStartId + aNbPnts);
128       aPntStartId += aNbPnts;
129     }
130     // Do mapping of the cells
131     vtkIdType aNbCells = aDataSet->GetNumberOfCells();
132     myCellRanges.push_back(aCellStartId + aNbCells);
133     aCellStartId += aNbCells;
134   }
135 }
136
137 namespace
138 {
139   inline
140   vtkIdType
141   GetOutputID(vtkIdType theInputID,
142               vtkIdType theInputDataSetID,
143               const VTKViewer_AppendFilter::TVectorIds& theRanges)
144   {
145     theInputID = theInputDataSetID = -1;
146
147     vtkIdType aNbInputs = theRanges.size();
148     if(theInputDataSetID < 0 || theInputDataSetID >= aNbInputs)
149       return -1;
150     
151     vtkIdType aStartId = theRanges[theInputDataSetID];
152     return aStartId + theInputID;
153   }
154 }
155
156 vtkIdType
157 VTKViewer_AppendFilter
158 ::GetPointOutputID(vtkIdType theInputID,
159                    vtkIdType theInputDataSetID)
160 {
161   if(myPoints.GetPointer())
162     return theInputID;
163
164   return GetOutputID(theInputID,theInputDataSetID,myNodeRanges);
165 }
166
167
168 vtkIdType 
169 VTKViewer_AppendFilter
170 ::GetCellOutputID(vtkIdType theInputID,
171                    vtkIdType theInputDataSetID)
172 {
173   if(myPoints.GetPointer())
174     return theInputID;
175
176   return GetOutputID(theInputID,theInputDataSetID,myCellRanges);
177 }
178
179
180 namespace
181 {
182   void
183   GetInputID(vtkIdType theOutputID,
184              vtkIdType& theInputID,
185              vtkIdType& theStartID,
186              vtkIdType& theInputDataSetID,
187              const VTKViewer_AppendFilter::TVectorIds& theRanges)
188   {
189     theInputID = theStartID = theInputDataSetID = -1;
190
191     if(theRanges.empty())
192       return;
193
194     const vtkIdType& aRangeEnd = theRanges.back();
195     if(theOutputID < 0 ||  theOutputID >= aRangeEnd)
196       return;
197
198     vtkIdType aStartId = 0;
199     vtkIdType aNbInputs = theRanges.size();
200     for(vtkIdType aDataSetId = 0; aDataSetId < aNbInputs; ++aDataSetId){
201       vtkIdType aRange = theRanges[aDataSetId];
202       if(aRange > theOutputID){
203         theInputID = theOutputID - aStartId;
204         theInputDataSetID = aDataSetId;
205         theStartID = aStartId;
206         break;
207       }
208       aStartId = aRange;
209     }
210   }
211 }
212
213 void 
214 VTKViewer_AppendFilter
215 ::GetPointInputID(vtkIdType theOutputID,
216                   vtkIdType& theInputID,
217                   vtkIdType& theStartID,
218                   vtkIdType& theInputDataSetID)
219 {
220   if(myPoints.GetPointer()) {
221     theStartID = theInputDataSetID = 0;
222     theInputID = theOutputID;
223     return;
224   }
225
226   ::GetInputID(theOutputID,
227                theInputID,
228                theStartID,
229                theInputDataSetID,
230                myNodeRanges);
231 }
232
233
234 void
235 VTKViewer_AppendFilter
236 ::GetCellInputID(vtkIdType theOutputID,
237                  vtkIdType& theInputID,
238                  vtkIdType& theStartID,
239                  vtkIdType& theInputDataSetID)
240 {
241   ::GetInputID(theOutputID,
242                theInputID,
243                theStartID,
244                theInputDataSetID,
245                myCellRanges);
246 }
247
248
249 void 
250 VTKViewer_AppendFilter
251 ::MakeOutput()
252 {
253   int idx;
254   vtkIdType numPts, numCells, newCellId, cellId;
255   vtkCellData *cd;
256   vtkIdList *ptIds;
257   vtkDataSet *ds;
258   vtkUnstructuredGrid *output = this->GetOutput();
259   //
260   numPts = myPoints->GetNumberOfPoints();
261   if (numPts < 1) {
262     return;
263   }
264   //
265   numCells = 0;
266   for (idx = 0; idx < this->NumberOfInputs; ++idx) {
267     ds = (vtkDataSet *)(this->Inputs[idx]);
268     if (ds != NULL)  {
269       if ( ds->GetNumberOfPoints() <= 0 && ds->GetNumberOfCells() <= 0 )  {
270         continue; //no input, just skip
271       }
272       numCells += ds->GetNumberOfCells();
273     }//if non-empty dataset
274   }//for all inputs
275   if (numCells < 1) {
276     return;
277   }
278   //
279   // Now can allocate memory
280   output->Allocate(numCells); 
281   ptIds = vtkIdList::New(); 
282   ptIds->Allocate(VTK_CELL_SIZE);
283   //
284   // Append each input dataset together
285   //
286   // 1.points
287   output->SetPoints(myPoints.GetPointer());
288   // 2.cells
289   for (idx = 0; idx < this->NumberOfInputs; ++idx) {
290     ds = (vtkDataSet *)(this->Inputs[idx]);
291     if (ds != NULL) {
292       numCells = ds->GetNumberOfCells(); 
293       cd = ds->GetCellData();
294       // copy cell and cell data
295       for (cellId=0; cellId<numCells; cellId++)  {
296         ds->GetCellPoints(cellId, ptIds);
297         newCellId = output->InsertNextCell(ds->GetCellType(cellId), ptIds);
298       }
299     }
300   }
301   //
302   ptIds->Delete();
303 }
304