1 // SALOME VTKViewer : build VTK viewer into Salome desktop
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
29 #include "VISU_MergeFilter.hxx"
31 #include <vtkCellData.h>
32 #include <vtkObjectFactory.h>
33 #include <vtkPointData.h>
34 #include <vtkPolyData.h>
35 #include <vtkRectilinearGrid.h>
36 #include <vtkStructuredGrid.h>
37 #include <vtkStructuredPoints.h>
38 #include <vtkUnstructuredGrid.h>
46 TFieldNode(const char* name, vtkDataSet* ptr=0)
48 int length = static_cast<int>(strlen(name));
50 this->Name = new char[length+1];
51 strcpy(this->Name, name);
70 TFieldNode(const TFieldNode&) {}
71 void operator=(const TFieldNode&) {}
85 TFieldNode* node = this->First;
95 void Add(const char* name, vtkDataSet* ptr)
97 TFieldNode* newNode = new TFieldNode(name, ptr);
99 this->First = newNode;
100 this->Last = newNode;
102 this->Last->Next = newNode;
103 this->Last = newNode;
107 friend class TFieldListIterator;
114 class TFieldListIterator
117 TFieldListIterator(TFieldList* list)
124 this->Position = this->List->First;
128 if (this->Position) {
129 this->Position = this->Position->Next;
134 return this->Position ? 0 : 1;
138 return this->Position;
142 TFieldNode* Position;
148 //------------------------------------------------------------------------------
149 vtkStandardNewMacro(VISU_MergeFilter);
151 //------------------------------------------------------------------------------
153 // Create object with no input or output.
154 VISU_MergeFilter::VISU_MergeFilter()
156 this->FieldList = new VISU::TFieldList;
159 VISU_MergeFilter::~VISU_MergeFilter()
161 delete this->FieldList;
164 void VISU_MergeFilter::SetScalars(vtkDataSet *input)
166 this->vtkProcessObject::SetNthInput(1, input);
168 vtkDataSet *VISU_MergeFilter::GetScalars()
170 if (this->NumberOfInputs < 2)
174 return (vtkDataSet *)(this->Inputs[1]);
177 void VISU_MergeFilter::SetVectors(vtkDataSet *input)
179 this->vtkProcessObject::SetNthInput(2, input);
181 vtkDataSet *VISU_MergeFilter::GetVectors()
183 if (this->NumberOfInputs < 3)
187 return (vtkDataSet *)(this->Inputs[2]);
190 void VISU_MergeFilter::SetNormals(vtkDataSet *input)
192 this->vtkProcessObject::SetNthInput(3, input);
194 vtkDataSet *VISU_MergeFilter::GetNormals()
196 if (this->NumberOfInputs < 4)
200 return (vtkDataSet *)(this->Inputs[3]);
203 void VISU_MergeFilter::SetTCoords(vtkDataSet *input)
205 this->vtkProcessObject::SetNthInput(4, input);
207 vtkDataSet *VISU_MergeFilter::GetTCoords()
209 if (this->NumberOfInputs < 5)
213 return (vtkDataSet *)(this->Inputs[4]);
216 void VISU_MergeFilter::SetTensors(vtkDataSet *input)
218 this->vtkProcessObject::SetNthInput(5, input);
220 vtkDataSet *VISU_MergeFilter::GetTensors()
222 if (this->NumberOfInputs < 6)
226 return (vtkDataSet *)(this->Inputs[5]);
229 void VISU_MergeFilter::AddField(const char* name, vtkDataSet* input)
231 this->FieldList->Add(name, input);
234 void VISU_MergeFilter::Execute()
236 vtkIdType numPts, numScalars=0, numVectors=0, numNormals=0, numTCoords=0;
237 vtkIdType numTensors=0;
238 vtkIdType numCells, numCellScalars=0, numCellVectors=0, numCellNormals=0;
239 vtkIdType numCellTCoords=0, numCellTensors=0;
241 vtkDataArray *scalars = NULL;
242 vtkDataArray *vectors = NULL;
243 vtkDataArray *normals = NULL;
244 vtkDataArray *tcoords = NULL;
245 vtkDataArray *tensors = NULL;
247 vtkDataArray *cellScalars = NULL;
248 vtkDataArray *cellVectors = NULL;
249 vtkDataArray *cellNormals = NULL;
250 vtkDataArray *cellTCoords = NULL;
251 vtkDataArray *cellTensors = NULL;
252 vtkDataSet *output = this->GetOutput();
253 vtkPointData *outputPD = output->GetPointData();
254 vtkCellData *outputCD = output->GetCellData();
256 vtkDebugMacro(<<"Merging data!");
258 // geometry needs to be copied
259 output->CopyStructure(this->GetInput());
260 if ( (numPts = this->GetInput()->GetNumberOfPoints()) < 1 )
262 vtkWarningMacro(<<"Nothing to merge!");
264 numCells = this->GetInput()->GetNumberOfCells();
266 if ( this->GetScalars() )
268 pd = this->GetScalars()->GetPointData();
269 scalars = pd->GetScalars();
270 if ( scalars != NULL )
272 numScalars = scalars->GetNumberOfTuples();
274 cd = this->GetScalars()->GetCellData();
275 cellScalars = cd->GetScalars();
276 if ( cellScalars != NULL )
278 numCellScalars = cellScalars->GetNumberOfTuples();
282 if ( this->GetVectors() )
284 pd = this->GetVectors()->GetPointData();
285 vectors = pd->GetVectors();
286 if ( vectors != NULL )
288 numVectors= vectors->GetNumberOfTuples();
290 cd = this->GetVectors()->GetCellData();
291 cellVectors = cd->GetVectors();
292 if ( cellVectors != NULL )
294 numCellVectors = cellVectors->GetNumberOfTuples();
298 if ( this->GetNormals() )
300 pd = this->GetNormals()->GetPointData();
301 normals = pd->GetNormals();
302 if ( normals != NULL )
304 numNormals= normals->GetNumberOfTuples();
306 cd = this->GetNormals()->GetCellData();
307 cellNormals = cd->GetNormals();
308 if ( cellNormals != NULL )
310 numCellNormals = cellNormals->GetNumberOfTuples();
314 if ( this->GetTCoords() )
316 pd = this->GetTCoords()->GetPointData();
317 tcoords = pd->GetTCoords();
318 if ( tcoords != NULL )
320 numTCoords= tcoords->GetNumberOfTuples();
322 cd = this->GetTCoords()->GetCellData();
323 cellTCoords = cd->GetTCoords();
324 if ( cellTCoords != NULL )
326 numCellTCoords = cellTCoords->GetNumberOfTuples();
330 if ( this->GetTensors() )
332 pd = this->GetTensors()->GetPointData();
333 tensors = pd->GetTensors();
334 if ( tensors != NULL )
336 numTensors = tensors->GetNumberOfTuples();
338 cd = this->GetTensors()->GetCellData();
339 cellTensors = cd->GetTensors();
340 if ( cellTensors != NULL )
342 numCellTensors = cellTensors->GetNumberOfTuples();
346 // merge data only if it is consistent
347 if ( numPts == numScalars )
349 outputPD->SetScalars(scalars);
351 if ( numCells == numCellScalars )
353 outputCD->SetScalars(cellScalars);
356 if ( numPts == numVectors )
358 outputPD->SetVectors(vectors);
360 if ( numCells == numCellVectors )
362 outputCD->SetVectors(cellVectors);
365 if ( numPts == numNormals )
367 outputPD->SetNormals(normals);
369 if ( numCells == numCellNormals )
371 outputCD->SetNormals(cellNormals);
374 if ( numPts == numTCoords )
376 outputPD->SetTCoords(tcoords);
378 if ( numCells == numCellTCoords )
380 outputCD->SetTCoords(cellTCoords);
383 if ( numPts == numTensors )
385 outputPD->SetTensors(tensors);
387 if ( numCells == numCellTensors )
389 outputCD->SetTensors(cellTensors);
392 VISU::TFieldListIterator it(this->FieldList);
396 for(it.Begin(); !it.End() ; it.Next())
398 pd = it.Get()->Ptr->GetPointData();
399 cd = it.Get()->Ptr->GetCellData();
400 name = it.Get()->GetName();
401 if ( (da=pd->GetArray(name)) )
403 num = da->GetNumberOfTuples();
406 outputPD->AddArray(da);
409 if ( (da=cd->GetArray(name)) )
411 num = da->GetNumberOfTuples();
412 if (num == numCells) // To fix a VTK bug
414 outputCD->AddArray(da);