Salome HOME
Fix initial state of checkboxes in MEDReader
[modules/paravis.git] / src / Plugins / StaticMesh / plugin / StaticMeshModule / vtkStaticDataSetSurfaceFilter.cxx
1 /*=========================================================================
2
3   Program:   Visualization Toolkit
4   Module:    vtkStaticDataSetSurfaceFilter.cxx
5
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13
14 =========================================================================*/
15 #include "vtkStaticDataSetSurfaceFilter.h"
16
17 #include <vtkCellData.h>
18 #include <vtkInformation.h>
19 #include <vtkInformationVector.h>
20 #include <vtkObjectFactory.h>
21 #include <vtkPointData.h>
22 #include <vtkUnstructuredGrid.h>
23 #include <vtksys/SystemTools.hxx>
24
25 vtkStandardNewMacro(vtkStaticDataSetSurfaceFilter);
26
27 //-----------------------------------------------------------------------------
28 int vtkStaticDataSetSurfaceFilter::UnstructuredGridExecute(vtkDataSet* input, vtkPolyData* output)
29 {
30   vtkUnstructuredGrid* inputUG = vtkUnstructuredGrid::SafeDownCast(input);
31   if (!inputUG)
32   {
33     // Rely on superclass for any input which is not a vtkUnstructuredGrid
34     return this->Superclass::UnstructuredGridExecute(input, output);
35   }
36
37   // Check if cache is still valid
38   if (this->InputMeshTime == inputUG->GetMeshMTime() && this->FilterMTime == this->GetMTime())
39   {
40     if (vtksys::SystemTools::HasEnv("VTK_DEBUG_STATIC_MESH"))
41     {
42       vtkWarningMacro("Using static mesh cache");
43     }
44
45     // Use cache as base
46     output->ShallowCopy(this->Cache.Get());
47
48     // Recover original ids
49     vtkPointData* outPD = output->GetPointData();
50     vtkCellData* outCD = output->GetCellData();
51     vtkIdTypeArray* origPointArray =
52       vtkIdTypeArray::SafeDownCast(outPD->GetArray(this->GetOriginalPointIdsName()));
53     vtkIdTypeArray* origCellArray =
54       vtkIdTypeArray::SafeDownCast(outCD->GetArray(this->GetOriginalCellIdsName()));
55     if (!origPointArray || !origCellArray)
56     {
57       vtkErrorMacro(
58         "OriginalPointIds or OriginalCellIds are missing, cannot use static mesh cache");
59       return this->Superclass::UnstructuredGridExecute(input, output);
60     }
61
62     // Recover input point and cell data
63     vtkPointData* inPD = input->GetPointData();
64     vtkCellData* inCD = input->GetCellData();
65
66     // Update output point data
67     vtkNew<vtkIdList> pointIds;
68     const vtkIdType origPANbTuples = origPointArray->GetNumberOfTuples();
69     pointIds->SetNumberOfIds(origPANbTuples);
70     for (vtkIdType i = 0; i < origPANbTuples; i++)
71     {
72       pointIds->SetId(i, origPointArray->GetTuple1(i));
73     }
74
75     // Remove array that have disappeared from input
76     for (int iArr = outPD->GetNumberOfArrays() - 1; iArr >= 0; iArr--)
77     {
78       vtkAbstractArray* inArr = inPD->GetAbstractArray(outPD->GetArrayName(iArr));
79       if (!inArr)
80       {
81         outPD->RemoveArray(iArr);
82       }
83     }
84
85     // Update or create arrays present in input
86     for (int iArr = 0; iArr < inPD->GetNumberOfArrays(); iArr++)
87     {
88       vtkAbstractArray* outArr = outPD->GetAbstractArray(inPD->GetArrayName(iArr));
89       if (outArr)
90       {
91         inPD->GetAbstractArray(iArr)->GetTuples(pointIds.Get(), outArr);
92       }
93       else
94       {
95         // New array in input, create it in output
96         vtkAbstractArray* inArr = inPD->GetAbstractArray(iArr);
97         outArr = inArr->NewInstance();
98         outArr->SetName(inArr->GetName());
99         outArr->SetNumberOfComponents(inArr->GetNumberOfComponents());
100         outArr->SetNumberOfTuples(output->GetNumberOfPoints());
101         inArr->GetTuples(pointIds.Get(), outArr);
102         outPD->AddArray(outArr);
103         outArr->Delete();
104       }
105     }
106
107     // Update output cell data
108     vtkNew<vtkIdList> cellIds;
109     const vtkIdType origCANbTuples = origCellArray->GetNumberOfTuples();
110     cellIds->SetNumberOfIds(origCANbTuples);
111     for (vtkIdType i = 0; i < origCANbTuples; i++)
112     {
113       cellIds->SetId(i, origCellArray->GetTuple1(i));
114     }
115
116     // Remove array that have disappeared from input
117     for (int iArr = outCD->GetNumberOfArrays() - 1; iArr >= 0; iArr--)
118     {
119       vtkAbstractArray* inArr = inCD->GetAbstractArray(outCD->GetArrayName(iArr));
120       if (!inArr)
121       {
122         outCD->RemoveArray(iArr);
123       }
124     }
125
126     for (int iArr = 0; iArr < inCD->GetNumberOfArrays(); iArr++)
127     {
128       vtkAbstractArray* outArr = outCD->GetAbstractArray(inCD->GetArrayName(iArr));
129       if (outArr)
130       {
131         inCD->GetAbstractArray(iArr)->GetTuples(cellIds.Get(), outArr);
132       }
133       else
134       {
135         // New array in input, create it in output
136         vtkAbstractArray* inArr = inCD->GetAbstractArray(iArr);
137         outArr = inArr->NewInstance();
138         outArr->SetName(inArr->GetName());
139         outArr->SetNumberOfComponents(inArr->GetNumberOfComponents());
140         outArr->SetNumberOfTuples(output->GetNumberOfCells());
141         inArr->GetTuples(cellIds.Get(), outArr);
142         outCD->AddArray(outArr);
143         outArr->Delete();
144       }
145     }
146
147     // Update output field data
148     output->GetFieldData()->ShallowCopy(input->GetFieldData());
149     return 1;
150   }
151   else
152   {
153     // Cache is not valid, Execute supercall algorithm
154     if (vtksys::SystemTools::HasEnv("VTK_DEBUG_STATIC_MESH"))
155     {
156       vtkWarningMacro("Building static mesh cache");
157     }
158
159     int ret = this->Superclass::UnstructuredGridExecute(input, output);
160
161     // Update the cache with superclass output
162     this->Cache->ShallowCopy(output);
163     this->InputMeshTime = inputUG->GetMeshMTime();
164     this->FilterMTime = this->GetMTime();
165     return ret;
166   }
167 }
168
169 //----------------------------------------------------------------------------
170 void vtkStaticDataSetSurfaceFilter::PrintSelf(ostream& os, vtkIndent indent)
171 {
172   this->Superclass::PrintSelf(os, indent);
173   os << indent << "Cache: " << this->Cache << endl;
174   os << indent << "Input Mesh Time: " << this->InputMeshTime << endl;
175   os << indent << "Filter mTime: " << this->FilterMTime << endl;
176 }