1 /*=========================================================================
3 Program: Visualization Toolkit
4 Module: vtkStaticDataSetSurfaceFilter.cxx
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
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.
14 =========================================================================*/
15 #include "vtkStaticDataSetSurfaceFilter.h"
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>
25 vtkStandardNewMacro(vtkStaticDataSetSurfaceFilter);
27 //-----------------------------------------------------------------------------
28 int vtkStaticDataSetSurfaceFilter::UnstructuredGridExecute(vtkDataSet* input, vtkPolyData* output)
30 vtkUnstructuredGrid* inputUG = vtkUnstructuredGrid::SafeDownCast(input);
33 // Rely on superclass for any input which is not a vtkUnstructuredGrid
34 return this->Superclass::UnstructuredGridExecute(input, output);
37 // Check if cache is still valid
38 if (this->InputMeshTime == inputUG->GetMeshMTime() && this->FilterMTime == this->GetMTime())
40 if (vtksys::SystemTools::HasEnv("VTK_DEBUG_STATIC_MESH"))
42 vtkWarningMacro("Using static mesh cache");
46 output->ShallowCopy(this->Cache.Get());
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)
58 "OriginalPointIds or OriginalCellIds are missing, cannot use static mesh cache");
59 return this->Superclass::UnstructuredGridExecute(input, output);
62 // Recover input point and cell data
63 vtkPointData* inPD = input->GetPointData();
64 vtkCellData* inCD = input->GetCellData();
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++)
72 pointIds->SetId(i, origPointArray->GetTuple1(i));
75 // Remove array that have disappeared from input
76 for (int iArr = outPD->GetNumberOfArrays() - 1; iArr >= 0; iArr--)
78 vtkAbstractArray* inArr = inPD->GetAbstractArray(outPD->GetArrayName(iArr));
81 outPD->RemoveArray(iArr);
85 // Update or create arrays present in input
86 for (int iArr = 0; iArr < inPD->GetNumberOfArrays(); iArr++)
88 vtkAbstractArray* outArr = outPD->GetAbstractArray(inPD->GetArrayName(iArr));
91 inPD->GetAbstractArray(iArr)->GetTuples(pointIds.Get(), outArr);
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);
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++)
113 cellIds->SetId(i, origCellArray->GetTuple1(i));
116 // Remove array that have disappeared from input
117 for (int iArr = outCD->GetNumberOfArrays() - 1; iArr >= 0; iArr--)
119 vtkAbstractArray* inArr = inCD->GetAbstractArray(outCD->GetArrayName(iArr));
122 outCD->RemoveArray(iArr);
126 for (int iArr = 0; iArr < inCD->GetNumberOfArrays(); iArr++)
128 vtkAbstractArray* outArr = outCD->GetAbstractArray(inCD->GetArrayName(iArr));
131 inCD->GetAbstractArray(iArr)->GetTuples(cellIds.Get(), outArr);
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);
147 // Update output field data
148 output->GetFieldData()->ShallowCopy(input->GetFieldData());
153 // Cache is not valid, Execute supercall algorithm
154 if (vtksys::SystemTools::HasEnv("VTK_DEBUG_STATIC_MESH"))
156 vtkWarningMacro("Building static mesh cache");
159 int ret = this->Superclass::UnstructuredGridExecute(input, output);
161 // Update the cache with superclass output
162 this->Cache->ShallowCopy(output);
163 this->InputMeshTime = inputUG->GetMeshMTime();
164 this->FilterMTime = this->GetMTime();
169 //----------------------------------------------------------------------------
170 void vtkStaticDataSetSurfaceFilter::PrintSelf(ostream& os, vtkIndent indent)
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;