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