VMTK
Loading...
Searching...
No Matches
vtkvmtkVoronoiDiagram3D.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkVoronoiDiagram3D.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 "vtkUnstructuredGrid.h"
24#include "vtkTetra.h"
25#include "vtkDoubleArray.h"
26#include "vtkIntArray.h"
27#include "vtkPointData.h"
28#include "vtkPolyData.h"
29#include "vtkObjectFactory.h"
30#include "vtkInformation.h"
31#include "vtkInformationVector.h"
32#include "vtkvmtkConstants.h"
33
34
36
38{
39 this->BuildLines = 0;
40 this->PoleIds = vtkIdList::New();
41 this->RadiusArrayName = NULL;
42}
43
45{
46 this->PoleIds->Delete();
47
48 if (this->RadiusArrayName)
49 {
50 delete[] this->RadiusArrayName;
51 this->RadiusArrayName = NULL;
52 }
53}
54
56{
57 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkUnstructuredGrid");
58 return 1;
59}
60
61void vtkvmtkVoronoiDiagram3D::ExtractUniqueEdges(vtkUnstructuredGrid* input, vtkCellArray* edgeArray) // could be a class
62{
63 int isVisited;
64 vtkIdType i, j, k;
65 vtkIdType edgePts[2];
66 vtkIdType npts;
67 const vtkIdType *pts;
68 vtkIntArray* visited;
69 vtkIdList* pointCells;
70 vtkIdList* insertedLoopPoints;
71
72 visited = vtkIntArray::New();
73 visited->SetNumberOfTuples(input->GetNumberOfPoints());
74 visited->FillComponent(0,0);
75
76 pointCells = vtkIdList::New();
77 insertedLoopPoints = vtkIdList::New();
78
79 for (i=0; i<input->GetNumberOfPoints(); i++)
80 {
81 visited->SetValue(i,1);
82 edgePts[0] = i;
83 pointCells->Initialize();
84 insertedLoopPoints->Initialize();
85 input->GetPointCells(i,pointCells);
86 for (j=0; j<pointCells->GetNumberOfIds(); j++)
87 {
88 input->GetCellPoints(pointCells->GetId(j),npts,pts);
89 for (k=0; k<npts; k++)
90 {
91 isVisited = visited->GetValue(pts[k]);
92 if (!isVisited)
93 if (insertedLoopPoints->IsId(pts[k])==-1)
94 {
95 edgePts[1] = pts[k];
96 insertedLoopPoints->InsertNextId(pts[k]);
97 edgeArray->InsertNextCell(2,edgePts);
98 }
99 }
100 }
101 }
102
103 visited->Delete();
104 pointCells->Delete();
105 insertedLoopPoints->Delete();
106
107}
108
109void vtkvmtkVoronoiDiagram3D::BuildVoronoiPolys(vtkUnstructuredGrid* input, vtkCellArray* voronoiPolys)
110{
111 bool boundaryTetra;
112 vtkIdType npts;
113 const vtkIdType *pts;
114 pts = NULL;
115 vtkIdType neighborTetraId;
116 vtkIdType i, k, h;
117 vtkCellArray* edgeArray;
118 vtkIdList* polyIds;
119 vtkIdList* neighborCells;
120 vtkIdList* neighborTrianglePointIds;
121 vtkIdList* neighborNeighborCells;
122 vtkIdList* linePointIds;
123 vtkTetra* neighborTetra;
124
125 edgeArray = vtkCellArray::New();
126 polyIds = vtkIdList::New();
127 neighborCells = vtkIdList::New();
128 neighborTrianglePointIds = vtkIdList::New();
129 neighborNeighborCells = vtkIdList::New();
130 linePointIds = vtkIdList::New();
131
132 this->ExtractUniqueEdges(input,edgeArray);
133
134 edgeArray->InitTraversal();
135 for (i=0; i<edgeArray->GetNumberOfCells(); i++)
136 {
137 edgeArray->GetNextCell(npts,pts);
138 linePointIds->Initialize();
139 linePointIds->InsertNextId(pts[0]);
140 linePointIds->InsertNextId(pts[1]);
141
142 boundaryTetra = false;
143 neighborCells->Initialize();
144 neighborTrianglePointIds->Initialize();
145 polyIds->Initialize();
146 input->GetCellNeighbors(-1,linePointIds,neighborCells);
147
148 neighborTrianglePointIds->InsertNextId(linePointIds->GetId(0));
149 neighborTrianglePointIds->InsertNextId(linePointIds->GetId(1));
150
151 neighborTetraId = neighborCells->GetId(0);
152 neighborTetra = vtkTetra::SafeDownCast(input->GetCell(neighborTetraId));
153 polyIds->InsertNextId(neighborTetraId);
154 for (k=0; k<neighborCells->GetNumberOfIds(); k++)
155 {
156 neighborNeighborCells->Initialize();
157 for (h=0; h<neighborTetra->GetNumberOfPoints(); h++)
158 {
159 if (neighborTrianglePointIds->IsId(neighborTetra->GetPointId(h))==-1)
160 {
161 neighborTrianglePointIds->InsertId(2,neighborTetra->GetPointId(h));
162 input->GetCellNeighbors(neighborTetraId,neighborTrianglePointIds,neighborNeighborCells);
163 if (neighborNeighborCells->GetNumberOfIds()==0)
164 {
165 boundaryTetra = true;
166 break;
167 }
168 else if (neighborNeighborCells->GetNumberOfIds()==1)
169 {
170 neighborTetraId = neighborNeighborCells->GetId(0);
171 neighborTetra = vtkTetra::SafeDownCast(input->GetCell(neighborTetraId));
172 polyIds->InsertUniqueId(neighborTetraId);
173 break;
174 }
175 }
176 }
177 if (boundaryTetra)
178 break;
179 }
180 if (!boundaryTetra)
181 voronoiPolys->InsertNextCell(polyIds);
182 }
183
184 edgeArray->Delete();
185 polyIds->Delete();
186 neighborCells->Delete();
187 neighborTrianglePointIds->Delete();
188 neighborNeighborCells->Delete();
189 linePointIds->Delete();
190}
191
193 vtkInformation *vtkNotUsed(request),
194 vtkInformationVector **inputVector,
195 vtkInformationVector *outputVector)
196{
197 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
198 vtkInformation *outInfo = outputVector->GetInformationObject(0);
199
200 vtkUnstructuredGrid* input = vtkUnstructuredGrid::SafeDownCast(
201 inInfo->Get(vtkDataObject::DATA_OBJECT()));
202 vtkPolyData *output = vtkPolyData::SafeDownCast(
203 outInfo->Get(vtkDataObject::DATA_OBJECT()));
204
205 // Declare
206 double p0[3], p1[3], p2[3], p3[3];
207 double tetraCenter[3], tetraRadius;
208 double currentRadius;
209 vtkIdType i, j, id, poleId;
210 vtkPoints* newPoints;
211 vtkCellArray* newPolys;
212 vtkCellArray* newLines;
213 vtkDoubleArray* newScalars;
214 vtkDoubleArray* thicknessScalars;
215 vtkIdList* cellIds;
216 vtkTetra* tetra;
217
218 poleId = -1;
219
220 // Allocate
221 newPoints = vtkPoints::New();
222 newPoints->SetNumberOfPoints(input->GetNumberOfCells());
223 newScalars = vtkDoubleArray::New();
224 newScalars->SetNumberOfTuples(input->GetNumberOfCells());
225 newPolys = vtkCellArray::New();
226 newLines = vtkCellArray::New();
227 thicknessScalars = vtkDoubleArray::New();
228 thicknessScalars->SetNumberOfTuples(input->GetNumberOfPoints());
229 thicknessScalars->FillComponent(0,0.0);
230
231 cellIds = vtkIdList::New();
232
233 this->PoleIds->SetNumberOfIds(input->GetNumberOfPoints());
234
235 // Execute
236
237 for (i=0; i<input->GetNumberOfPoints(); i++)
238 {
239 this->PoleIds->SetId(i,-1);
240 }
241
242 for (i=0; i<input->GetNumberOfCells(); i++)
243 {
244 tetra = (vtkTetra*) input->GetCell(i);
245 tetra->GetPoints()->GetPoint(0,p0);
246 tetra->GetPoints()->GetPoint(1,p1);
247 tetra->GetPoints()->GetPoint(2,p2);
248 tetra->GetPoints()->GetPoint(3,p3);
249 tetraRadius = sqrt(vtkTetra::Circumsphere(p0,p1,p2,p3,tetraCenter));
250
251 newPoints->SetPoint(i,tetraCenter);
252 newScalars->SetValue(i,(double)tetraRadius);
253 }
254
255 // compute poles
256 input->BuildLinks();
257 for (i=0; i<input->GetNumberOfPoints(); i++)
258 {
259 cellIds->Initialize();
260 input->GetPointCells(i,cellIds);
261 currentRadius = thicknessScalars->GetValue(i);
262 for (j=0; j<cellIds->GetNumberOfIds(); j++)
263 {
264 id = cellIds->GetId(j);
265 tetraRadius = newScalars->GetValue(id);
266 if (tetraRadius - currentRadius > VTK_VMTK_DOUBLE_TOL)
267 {
268 poleId = id;
269 currentRadius = tetraRadius;
270 }
271 }
272 this->PoleIds->SetId(i,poleId);
273 thicknessScalars->SetValue(i,currentRadius);
274 }
275
276 this->BuildVoronoiPolys(input,newPolys);
277
278 if (this->BuildLines)
279 {
280 this->BuildVoronoiLines();
281 }
282
283 if (this->RadiusArrayName)
284 {
285 newScalars->SetName(this->RadiusArrayName);
286 }
287 else
288 {
289 newScalars->SetName("VoronoiRadius");
290 }
291
292 output->SetPoints(newPoints);
293 output->GetPointData()->AddArray(newScalars);
294 output->GetPointData()->SetActiveScalars(newScalars->GetName());
295 output->SetPolys(newPolys);
296
297 if (this->BuildLines)
298 {
299 output->SetLines(newLines);
300 }
301
302 // Destroy
303 newPoints->Delete();
304 newPolys->Delete();
305 newLines->Delete();
306 newScalars->Delete();
307 thicknessScalars->Delete();
308 cellIds->Delete();
309
310 return 1;
311}
312
313void vtkvmtkVoronoiDiagram3D::PrintSelf(std::ostream& os, vtkIndent indent)
314{
315 this->Superclass::PrintSelf(os,indent);
316}
Compute the Voronoi diagram from a Delaunay tessellation or an internal Delaunay tessellation.
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
int FillInputPortInformation(int, vtkInformation *info) override
void ExtractUniqueEdges(vtkUnstructuredGrid *input, vtkCellArray *edgeArray)
void PrintSelf(std::ostream &os, vtkIndent indent) override
void BuildVoronoiPolys(vtkUnstructuredGrid *input, vtkCellArray *voronoiPolys)
#define VTK_VMTK_DOUBLE_TOL
vtkStandardNewMacro(vtkvmtkVoronoiDiagram3D)