VMTK
Loading...
Searching...
No Matches
vtkvmtkUnstructuredGridTetraFilter.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkUnstructuredGridTetraFilter.h,v $
5Language: C++
6Date: $Date: 2006/07/17 09:53:14 $
7Version: $Revision: 1.1 $
8
9 Copyright (c) Luca Antiga, David Steinman. All rights reserved.
10 See LICENSE file for details.
11
12 Portions of this code are covered under the VTK copyright.
13 See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm
14 for details.
15
16 This software is distributed WITHOUT ANY WARRANTY; without even
17 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 PURPOSE. See the above copyright notices for more information.
19
20=========================================================================*/
21
23
24#include "vtkCellData.h"
25#include "vtkCellLinks.h"
26#include "vtkCellType.h"
27#include "vtkGenericCell.h"
28#include "vtkIdList.h"
29#include "vtkInformation.h"
30#include "vtkInformationVector.h"
31#include "vtkObjectFactory.h"
32#include "vtkOrderedTriangulator.h"
33#include "vtkPointData.h"
34#include "vtkUnstructuredGrid.h"
35#include "vtkUnsignedCharArray.h"
36
38
40{
41 this->Triangulator = vtkOrderedTriangulator::New();
42 this->Triangulator->PreSortedOff();
43 this->Triangulator->UseTemplatesOn();
44 this->TetrahedraOnly = 0;
45}
46
52
54 vtkInformation *vtkNotUsed(request),
55 vtkInformationVector **inputVector,
56 vtkInformationVector *outputVector)
57{
58 // get the info objects
59 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
60 vtkInformation *outInfo = outputVector->GetInformationObject(0);
61
62 // get the input and output
63 vtkUnstructuredGrid *input = vtkUnstructuredGrid::SafeDownCast(
64 inInfo->Get(vtkDataObject::DATA_OBJECT()));
65 vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
66 outInfo->Get(vtkDataObject::DATA_OBJECT()));
67
68 this->Execute(input, output);
69
70 vtkDebugMacro(<<"Produced " << this->GetOutput()->GetNumberOfCells() << " cells");
71
72 return 1;
73}
74
75int vtkvmtkUnstructuredGridTetraFilter::TriangulateQuad(vtkUnstructuredGrid* output, vtkIdList *quadPtIds, vtkIdList *ptIds)
76{
77 vtkIdList* neighborCellIds = vtkIdList::New();
78
79 ptIds->Initialize();
80 ptIds->InsertId(0,quadPtIds->GetId(0));
81 ptIds->InsertId(1,quadPtIds->GetId(1));
82 ptIds->InsertId(2,quadPtIds->GetId(2));
83
84 output->GetCellNeighbors(-1,ptIds,neighborCellIds);
85
86 if (neighborCellIds->GetNumberOfIds() > 0)
87 {
88 ptIds->InsertId(3,quadPtIds->GetId(0));
89 ptIds->InsertId(4,quadPtIds->GetId(2));
90 ptIds->InsertId(5,quadPtIds->GetId(3));
91 }
92 else
93 {
94 ptIds->InsertId(0,quadPtIds->GetId(0));
95 ptIds->InsertId(1,quadPtIds->GetId(1));
96 ptIds->InsertId(2,quadPtIds->GetId(3));
97 ptIds->InsertId(3,quadPtIds->GetId(1));
98 ptIds->InsertId(4,quadPtIds->GetId(2));
99 ptIds->InsertId(5,quadPtIds->GetId(3));
100 }
101
102 neighborCellIds->Delete();
103
104 return 1;
105}
106
107
108// 3D cells use the ordered triangulator. The ordered triangulator is used
109// to create templates on the fly. Once the templates are created then they
110// are used to produce the final triangulation.
111//
112void vtkvmtkUnstructuredGridTetraFilter::Execute(vtkUnstructuredGrid *dataSetInput,
113 vtkUnstructuredGrid *output)
114{
115 vtkPointSet *input = static_cast<vtkPointSet*>(dataSetInput); //has to be
116 vtkIdType numCells = input->GetNumberOfCells();
117 vtkGenericCell *cell;
118 vtkIdType newCellId, j;
119 int k;
120 vtkCellData *inCD=input->GetCellData();
121 vtkCellData *outCD=output->GetCellData();
122 vtkPoints *cellPts;
123 vtkIdList *cellPtIds;
124 vtkIdType ptId, numTets, ncells;
125 int numPts, type;
126 int numSimplices, dim;
127 vtkIdType pts[4];
128 double x[3];
129
130 if (numCells == 0)
131 {
132 return;
133 }
134
135 //avoid doing cell simplification if all cells are already simplices
136 vtkUnsignedCharArray* cellTypes = dataSetInput->GetCellTypesArray();
137 if (cellTypes)
138 {
139 int allsimplices = 1;
140 for (vtkIdType cellId = 0; cellId < cellTypes->GetSize() && allsimplices; cellId++)
141 {
142 switch (cellTypes->GetValue(cellId))
143 {
144 case VTK_TETRA:
145 break;
146 case VTK_VERTEX:
147 case VTK_LINE:
148 case VTK_TRIANGLE:
149 if (this->TetrahedraOnly)
150 {
151 allsimplices = 0;
152 }
153 break;
154 default:
155 allsimplices = 0;
156 break;
157 }
158 }
159 if (allsimplices)
160 {
161 output->ShallowCopy(input);
162 return;
163 }
164 }
165
166 cell = vtkGenericCell::New();
167 cellPts = vtkPoints::New();
168 cellPtIds = vtkIdList::New();
169
170 // Create an array of points
171 vtkCellData *tempCD = vtkCellData::New();
172 tempCD->ShallowCopy(inCD);
173 tempCD->SetActiveGlobalIds(NULL);
174
175 outCD->CopyAllocate(tempCD, input->GetNumberOfCells()*5);
176 output->Allocate(input->GetNumberOfCells()*5);
177
178 // Points are passed through
179 output->SetPoints(input->GetPoints());
180 output->GetPointData()->PassData(input->GetPointData());
181
182 for (vtkIdType cellId=0; cellId < numCells; cellId++)
183 {
184 input->GetCell(cellId, cell);
185 dim = cell->GetCellDimension();
186
187 if ( dim != 3 ) //use ordered triangulation
188 {
189 continue;
190 }
191
192 numPts = cell->GetNumberOfPoints();
193 double *p, *pPtr=cell->GetParametricCoords();
194 this->Triangulator->InitTriangulation(0.0,1.0, 0.0,1.0, 0.0,1.0, numPts);
195 for (p=pPtr, j=0; j<numPts; j++, p+=3)
196 {
197 ptId = cell->PointIds->GetId(j);
198 cell->Points->GetPoint(j, x);
199 this->Triangulator->InsertPoint(ptId, x, p, 0);
200 }//for all cell points
201 if ( cell->IsPrimaryCell() ) //use templates if topology is fixed
202 {
203 int numEdges=cell->GetNumberOfEdges();
204 this->Triangulator->TemplateTriangulate(cell->GetCellType(),
205 numPts,numEdges);
206 }
207 else //use ordered triangulator
208 {
209 this->Triangulator->Triangulate();
210 }
211
212 ncells = output->GetNumberOfCells();
213 numTets = this->Triangulator->AddTetras(0,output);
214
215 for (j=0; j < numTets; j++)
216 {
217 outCD->CopyData(tempCD, cellId, ncells+j);
218 }
219 }
220
221 if (!this->TetrahedraOnly) //2D or lower dimension
222 {
223 output->BuildLinks();
224
225 for (vtkIdType cellId=0; cellId < numCells; cellId++)
226 {
227 input->GetCell(cellId, cell);
228 dim = cell->GetCellDimension();
229
230 if (dim != 2)
231 {
232 continue;
233 }
234
235 dim++;
236
237 if (cell->GetCellType() == VTK_QUAD)
238 {
239 this->TriangulateQuad(output,cell->GetPointIds(),cellPtIds);
240 }
241 else
242 {
243 cell->Triangulate(0, cellPtIds, cellPts);
244 }
245
246 numPts = cellPtIds->GetNumberOfIds();
247
248 numSimplices = numPts / dim;
249 type = 0;
250 switch (dim)
251 {
252 case 1:
253 type = VTK_VERTEX; break;
254 case 2:
255 type = VTK_LINE; break;
256 case 3:
257 type = VTK_TRIANGLE; break;
258 }
259
260 for ( j=0; j < numSimplices; j++ )
261 {
262 for (k=0; k<dim; k++)
263 {
264 pts[k] = cellPtIds->GetId(dim*j+k);
265 }
266 newCellId = output->InsertNextCell(type, dim, pts);
267 outCD->CopyData(tempCD, cellId, newCellId);
268 }
269 }
270
271 //links->Delete();
272 }
273
274 output->Squeeze();
275
276 tempCD->Delete();
277
278 cellPts->Delete();
279 cellPtIds->Delete();
280 cell->Delete();
281}
282
283void vtkvmtkUnstructuredGridTetraFilter::PrintSelf(std::ostream& os, vtkIndent indent)
284{
285 this->Superclass::PrintSelf(os,indent);
286 os << indent << "TetrahedraOnly: " << (this->TetrahedraOnly ? "On":"Off") << "\n";
287}
288
Convert the elements of a mesh to linear tetrahedra.
int TriangulateQuad(vtkUnstructuredGrid *output, vtkIdList *quadPtIds, vtkIdList *ptIds)
void Execute(vtkUnstructuredGrid *, vtkUnstructuredGrid *)
void PrintSelf(std::ostream &os, vtkIndent indent) override
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
vtkStandardNewMacro(vtkvmtkUnstructuredGridTetraFilter)