VMTK
Loading...
Searching...
No Matches
vtkvmtkInternalTetrahedraExtractor.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkInternalTetrahedraExtractor.cxx,v $
5Language: C++
6Date: $Date: 2006/04/06 16:46:43 $
7Version: $Revision: 1.5 $
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#include "vtkPointData.h"
24#include "vtkCellArray.h"
25#include "vtkUnstructuredGrid.h"
26#include "vtkIntArray.h"
27#include "vtkTetra.h"
28#include "vtkTriangle.h"
29#include "vtkMath.h"
30#include "vtkvmtkConstants.h"
31#include "vtkInformation.h"
32#include "vtkInformationVector.h"
33#include "vtkObjectFactory.h"
34
35
37
48
50{
51 if (this->CapCenterIds)
52 {
53 this->CapCenterIds->Delete();
54 this->CapCenterIds = NULL;
55 }
57 {
58 delete [] this->OutwardNormalsArrayName;
59 this->OutwardNormalsArrayName = NULL;
60 }
61 if (this->Surface)
62 {
63 this->Surface->Delete();
64 this->Surface = NULL;
65 }
66}
67
69 vtkInformation *request,
70 vtkInformationVector **inputVector,
71 vtkInformationVector *outputVector)
72{
73 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
74 vtkInformation *outInfo = outputVector->GetInformationObject(0);
75
76 vtkUnstructuredGrid *input = vtkUnstructuredGrid::SafeDownCast(
77 inInfo->Get(vtkDataObject::DATA_OBJECT()));
78 vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
79 outInfo->Get(vtkDataObject::DATA_OBJECT()));
80
81 // Declare
82 double circumcenter[3];
83 double p0[3], p1[3], p2[3], p3[3];
84 double v0[3], v1[3], v2[3], v3[3], n0[3], n1[3], n2[3], n3[3];
85 double dot0, dot1, dot2, dot3;
86 bool boundaryTetra;
87 bool allDotPositive, allDotMinusOnePositive;
88 vtkIdType i, j;
89 vtkCellArray* newTetras;
90 vtkIdList* newCellTypes;
91 vtkIntArray* keepCell;
92 vtkDataArray* outwardPointNormals;
93 vtkTetra* tetra;
94
95 if (!this->OutwardNormalsArrayName)
96 {
97 vtkErrorMacro(<< "No normals array name specified!");
98 return 1;
99 }
100
101 outwardPointNormals = input->GetPointData()->GetArray(this->OutwardNormalsArrayName);
102
103 if (!outwardPointNormals)
104 {
105 vtkErrorMacro(<< "Array with name specified does not exist!");
106 return 1;
107 }
108
109 if (outwardPointNormals->GetNumberOfComponents()!=3)
110 {
111 vtkErrorMacro(<< "Normals have NumberOfComponents != 3!");
112 return 1;
113 }
114
115 if (outwardPointNormals->GetNumberOfTuples()!=input->GetNumberOfPoints())
116 {
117 vtkErrorMacro(<< "Number of normals does not match input number of points !");
118 return 1;
119 }
120
121 if ((this->UseCaps)&&(!this->CapCenterIds))
122 {
123 vtkErrorMacro(<< "UseCapsOn but no CapCenterIds specified !");
124 return 1;
125 }
126
127 if (this->RemoveSubresolutionTetrahedra && !this->Surface)
128 {
129 vtkErrorMacro(<< "RemoveSubresolutionTetrahedraOn but no Surface specified !");
130 return 1;
131 }
132
133 if (this->RemoveSubresolutionTetrahedra && this->Surface->GetNumberOfPoints() != input->GetNumberOfPoints())
134 {
135 vtkErrorMacro(<< "NumberOfPoints of Surface is different than that of input Delaunay tessellation, the Delaunay tessellation doesn't look like it has been produced from the Surface !");
136 return 1;
137 }
138
139 // Allocate
140 newTetras = vtkCellArray::New();
141 newCellTypes = vtkIdList::New();
142 keepCell = vtkIntArray::New();
143 keepCell->SetNumberOfTuples(input->GetNumberOfCells());
144
145 // Execute
146
147 //skeleton: dual of inner delaunay tets (Attali, Sk0)(not necessarily internal) or inner voronoi elements (Sk2)(not necessarily homotpic).
148 //actual choice: Sk2.
149
150 double tolerance = this->Tolerance;
151
152 for (i=0; i<input->GetNumberOfCells(); i++)
153 {
154 tetra = vtkTetra::SafeDownCast(input->GetCell(i));
155
156 if (!tetra)
157 {
158 continue;
159 }
160
161 boundaryTetra = false;
162 if (this->UseCaps)
163 {
164 for (j=0; j<tetra->GetNumberOfPoints(); j++)
165 {
166 if (this->CapCenterIds->IsId(tetra->GetPointId(j))!=-1)
167 {
168 boundaryTetra = true;
169 }
170 }
171 }
172
173 tetra->GetPoints()->GetPoint(0,p0);
174 tetra->GetPoints()->GetPoint(1,p1);
175 tetra->GetPoints()->GetPoint(2,p2);
176 tetra->GetPoints()->GetPoint(3,p3);
177 vtkTetra::Circumsphere(p0,p1,p2,p3,circumcenter);
178
179 for (j=0; j<3; j++)
180 {
181 v0[j] = p0[j] - circumcenter[j];
182 v1[j] = p1[j] - circumcenter[j];
183 v2[j] = p2[j] - circumcenter[j];
184 v3[j] = p3[j] - circumcenter[j];
185 }
186
187 outwardPointNormals->GetTuple(tetra->GetPointId(0),n0);
188 outwardPointNormals->GetTuple(tetra->GetPointId(1),n1);
189 outwardPointNormals->GetTuple(tetra->GetPointId(2),n2);
190 outwardPointNormals->GetTuple(tetra->GetPointId(3),n3);
191
192 dot0 = vtkMath::Dot(v0,n0);
193 dot1 = vtkMath::Dot(v1,n1);
194 dot2 = vtkMath::Dot(v2,n2);
195 dot3 = vtkMath::Dot(v3,n3);
196
197 allDotPositive = false;
198 allDotMinusOnePositive = false;
199
200 if ((dot0>tolerance)&&(dot1>tolerance)&&(dot2>tolerance)&&(dot3>tolerance))
201 {
202 allDotPositive = true;
203 }
204 else if (((dot0>tolerance)&&(dot1>tolerance)&&(dot2>tolerance))||
205 ((dot0>tolerance)&&(dot1>tolerance)&&(dot3>tolerance))||
206 ((dot0>tolerance)&&(dot2>tolerance)&&(dot3>tolerance))||
207 ((dot1>tolerance)&&(dot2>tolerance)&&(dot3>tolerance)))
208 {
209 allDotMinusOnePositive = true;
210 }
211
212 keepCell->SetValue(i,0);
213
214 if (allDotPositive)
215 {
216 keepCell->SetValue(i,1);
217 }
218 else if (boundaryTetra)
219 {
220 if (allDotMinusOnePositive)
221 {
222 keepCell->SetValue(i,1);
223 }
224 }
225 }
226
228 {
229 double pt0[3], pt1[3], pt2[3];
230 this->Surface->BuildLinks();
231
232 vtkIdList* cellNeighbors = vtkIdList::New();
233 for (i=0; i<input->GetNumberOfCells(); i++)
234 {
235 if (keepCell->GetValue(i) == 0)
236 {
237 continue;
238 }
239
240 tetra = vtkTetra::SafeDownCast(input->GetCell(i));
241 if (!tetra)
242 {
243 continue;
244 }
245 bool onNewBoundary = false;
246 for (j=0; j<tetra->GetNumberOfFaces(); j++)
247 {
248 cellNeighbors->Initialize();
249 input->GetCellNeighbors(i,tetra->GetFace(j)->GetPointIds(),cellNeighbors);
250 if (cellNeighbors->GetNumberOfIds() == 0 || keepCell->GetValue(cellNeighbors->GetId(0)) == 0)
251 {
252 onNewBoundary = true;
253 }
254 }
255 if (!onNewBoundary)
256 {
257 continue;
258 }
259
260 tetra->GetPoints()->GetPoint(0,p0);
261 tetra->GetPoints()->GetPoint(1,p1);
262 tetra->GetPoints()->GetPoint(2,p2);
263 tetra->GetPoints()->GetPoint(3,p3);
264 double circumradius = vtkTetra::Circumsphere(p0,p1,p2,p3,circumcenter);
265
266 for (j=0; j<tetra->GetNumberOfPoints(); j++)
267 {
268 vtkIdType ncells;
269 vtkIdType* cells;
270 this->Surface->GetPointCells(j,ncells,cells);
271 double minEdgeLength = VTK_VMTK_LARGE_DOUBLE;
272 int k;
273 for (k=0; k<ncells; k++)
274 {
275 vtkTriangle* triangle = vtkTriangle::SafeDownCast(this->Surface->GetCell(cells[k]));
276 if (!triangle)
277 {
278 continue;
279 }
280 triangle->GetPoints()->GetPoint(0,pt0);
281 triangle->GetPoints()->GetPoint(1,pt1);
282 triangle->GetPoints()->GetPoint(2,pt2);
283 double triangleArea = triangle->TriangleArea(pt0,pt1,pt2);
284 double edgeLength = sqrt(2.0 * triangleArea);
285 if (edgeLength < minEdgeLength)
286 {
287 minEdgeLength = edgeLength;
288 }
289 }
290 // TODO: set factor as a member variable.
291 if (circumradius < this->SubresolutionFactor * minEdgeLength)
292 {
293 keepCell->SetValue(i,0);
294 }
295 }
296 }
297 cellNeighbors->Delete();
298 }
299
300 for (i=0; i<input->GetNumberOfCells(); i++)
301 {
302 if (keepCell->GetValue(i))
303 {
304 newCellTypes->InsertNextId(VTK_TETRA);
305 newTetras->InsertNextCell(input->GetCell(i));
306 }
307 }
308
309 int* newCellTypesInt = new int[newCellTypes->GetNumberOfIds()];
310 for (i=0; i<newCellTypes->GetNumberOfIds(); i++)
311 {
312 newCellTypesInt[i] = newCellTypes->GetId(i);
313 }
314
315 output->SetPoints(input->GetPoints());
316 output->GetPointData()->PassData(input->GetPointData());
317 output->SetCells(newCellTypesInt,newTetras);
318
319 // Destroy
320 newTetras->Delete();
321 newCellTypes->Delete();
322 keepCell->Delete();
323
324 delete[] newCellTypesInt;
325
326 return 1;
327}
328
329void vtkvmtkInternalTetrahedraExtractor::PrintSelf(std::ostream& os, vtkIndent indent)
330{
331 this->Superclass::PrintSelf(os,indent);
332}
Extract internal tetrahedra from a Delaunay tessellation of a surface.
void PrintSelf(std::ostream &os, vtkIndent indent) override
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
#define VTK_VMTK_DOUBLE_TOL
#define VTK_VMTK_LARGE_DOUBLE
vtkStandardNewMacro(vtkvmtkInternalTetrahedraExtractor)