VMTK
Loading...
Searching...
No Matches
vtkvmtkSimplifyVoronoiDiagram.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkSimplifyVoronoiDiagram.cxx,v $
5Language: C++
6Date: $Date: 2006/04/06 16:46:43 $
7Version: $Revision: 1.4 $
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 "vtkCellArray.h"
24#include "vtkCellTypes.h"
25#include "vtkCellLinks.h"
26#include "vtkPolyData.h"
27#include "vtkPointData.h"
28#include "vtkInformation.h"
29#include "vtkInformationVector.h"
30#include "vtkObjectFactory.h"
31
32
34
43
45{
46 if (this->UnremovablePointIds)
47 {
48 this->UnremovablePointIds->Delete();
49 this->UnremovablePointIds = NULL;
50 }
51
52 if (this->UnremovableCellIds)
53 {
54 this->UnremovableCellIds->Delete();
55 this->UnremovableCellIds = NULL;
56 }
57}
58
59vtkIdType vtkvmtkSimplifyVoronoiDiagram::IsBoundaryEdge(vtkCellLinks* links, vtkIdType* edge)
60{
61 vtkIdType j, k;
62 vtkIdType cellId, ncells0, *cells0, ncells1, *cells1;
63
64 ncells0 = links->GetNcells(edge[0]);
65 cells0 = links->GetCells(edge[0]);
66 ncells1 = links->GetNcells(edge[1]);
67 cells1 = links->GetCells(edge[1]);
68
69 cellId = -1;
70
71 for (j=0; j<ncells0; j++)
72 {
73 for (k=0; k<ncells1; k++)
74 {
75 if (cells0[j]==cells1[k])
76 {
77 if (cellId==-1)
78 {
79 cellId = cells0[j];
80 }
81 else if (cells0[j]!=cellId)
82 {
83 return 0;
84 }
85 }
86 }
87 }
88
89 if (cellId==-1)
90 return -1;
91
92 return 1;
93}
94
96 vtkInformation* vtkNotUsed(request),
97 vtkInformationVector** inputVector,
98 vtkInformationVector* outputVector)
99{
100 vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
101 vtkInformation* outInfo = outputVector->GetInformationObject(0);
102
103 vtkPolyData* input = vtkPolyData::SafeDownCast(
104 inInfo->Get(vtkDataObject::DATA_OBJECT()));
105 vtkPolyData* output = vtkPolyData::SafeDownCast(
106 outInfo->Get(vtkDataObject::DATA_OBJECT()));
107
108 bool anyRemoved, removeCell, considerPoint;
109 bool* isUnremovable;
110 vtkIdType i, j, id;
111 vtkIdType n;
112 vtkIdType npts, ncells;
113 const vtkIdType *pts;
114 vtkPolyData* poly = vtkPolyData::New();
115 poly->SetPoints(input->GetPoints());
116 npts = 0;
117 pts = NULL;
118 vtkIdType edge[2];
119 vtkCellArray* currentPolys;
120 vtkCellLinks* currentLinks;
121 vtkIdType newCellId;
122 vtkCellArray* inputPolys = input->GetPolys();
123
124 currentPolys = vtkCellArray::New();
125 currentLinks = vtkCellLinks::New();
126
127 n = 0;
129 {
130 n = input->GetNumberOfPoints();
131 }
133 {
134 n = inputPolys->GetNumberOfCells();
135 }
136
137 isUnremovable = new bool[n];
138
139 bool anyUnremovable = false;
140
141 for (i=0; i<n; i++)
142 {
143 isUnremovable[i] = false;
144 }
145
147 {
148 if (this->UnremovablePointIds)
149 {
150 for (i=0; i<this->UnremovablePointIds->GetNumberOfIds(); i++)
151 {
152 id = this->UnremovablePointIds->GetId(i);
153 if ((id >= 0) && (id < n))
154 {
155 anyUnremovable = true;
156 isUnremovable[id] = true;
157 }
158 else
159 {
160 vtkErrorMacro(<< "Out of range id found among UnremovablePointIds!");
161 }
162 }
163 }
164 }
166 {
167 if (this->UnremovableCellIds)
168 {
169 for (i=0; i<this->UnremovableCellIds->GetNumberOfIds(); i++)
170 {
171 id = this->UnremovableCellIds->GetId(i);
172 if ((id >= 0) && (id < n))
173 {
174 anyUnremovable = true;
175 isUnremovable[id] = true;
176 }
177 else if (id<0)
178 {
179 vtkErrorMacro(<< "Out of range id found among UnremovableCellIds!");
180 }
181 }
182 }
183 }
184
185 currentPolys->DeepCopy(inputPolys);
186
187 currentLinks->Allocate(input->GetNumberOfPoints());
188
189 // Reworking cell links classes for performance and consistent API
190 // See https://github.com/Kitware/VTK/commit/88efc809a25130c2ab83dd89a80cea458e3bb56a
191 // #pragma message "vtkvmtkSimplifyVoronoiDiagram::RequestData not functional. Must be updated based on Kitware/VTK@88efc809a"
192 // if (true)
193 // {
194 // vtkErrorMacro(<< "vtkvmtkSimplifyVoronoiDiagram::RequestData is not functional when built against VTK >= 9");
195 // return 0;
196 // }
197 poly->SetPolys(currentPolys);
198
199#if ((VTK_MAJOR_VERSION >= 9 && VTK_MINOR_VERSION >= 0 && VTK_BUILD_VERSION >= 20221108) || (VTK_MAJOR_VERSION >= 9 && VTK_MINOR_VERSION >= 2 && VTK_BUILD_VERSION >= 0))
200 currentLinks->SetDataSet(poly);
201 currentLinks->BuildLinks();
202#else
203 currentLinks->BuildLinks(poly);
204#endif
205
206 anyRemoved = true;
207 while (anyRemoved)
208 {
209 anyRemoved = false;
210 vtkCellArray* newPolys = vtkCellArray::New();
211 vtkIdList* newCell = vtkIdList::New();
212 currentPolys->InitTraversal();
213 for (i=0; i<currentPolys->GetNumberOfCells(); i++)
214 {
215 currentPolys->GetNextCell(npts,pts);
216
217 if (npts==0)
218 {
219 continue;
220 }
221
222 newCell->Initialize();
224 {
225 for (j=0; j<npts; j++)
226 {
227 considerPoint = false;
228 ncells = currentLinks->GetNcells(pts[j]);
229
230 if (ncells==1)
231 {
232 considerPoint = true;
233 }
234
235 if (considerPoint)
236 {
237 if (isUnremovable[pts[j]])
238 {
239 newCell->InsertNextId(pts[j]);
240 }
241 else
242 {
243 anyRemoved = true;
244 }
245 }
246 else
247 {
248 newCell->InsertNextId(pts[j]);
249 }
250 }
251 }
253 {
254 removeCell = false;
255 if (!isUnremovable[i])
256 {
257 for (j=0; j<npts; j++)
258 {
259 edge[0] = pts[j];
260 edge[1] = pts[(j+1)%npts];
261
262 if (this->IsBoundaryEdge(currentLinks,edge)>0)
263 {
264 removeCell = true;
265 break;
266 }
267 }
268 }
269
270 if (removeCell)
271 {
272 anyRemoved = true;
273 }
274 else
275 {
276 for (j=0; j<npts; j++)
277 {
278 newCell->InsertNextId(pts[j]);
279 }
280 }
281 }
282
283 if (newCell->GetNumberOfIds() > 2)
284 {
285 newCellId = newPolys->InsertNextCell(newCell);
286 }
287 }
288
289 currentPolys->DeepCopy(newPolys);
290 currentLinks->Delete();
291 currentLinks = vtkCellLinks::New();
292 currentLinks->Allocate(input->GetNumberOfPoints());
293
294 // Reworking cell links classes for performance and consistent API
295 // See https://github.com/Kitware/VTK/commit/88efc809a25130c2ab83dd89a80cea458e3bb56a
296 // #pragma message "vtkvmtkSimplifyVoronoiDiagram::RequestData not functional. Must be updated based on Kitware/VTK@88efc809a"
297 // vtkErrorMacro(<< "!");
298 poly->SetPolys(currentPolys);
299#if ((VTK_MAJOR_VERSION >= 9 && VTK_MINOR_VERSION >= 0 && VTK_BUILD_VERSION >= 20221108) || (VTK_MAJOR_VERSION >= 9 && VTK_MINOR_VERSION >= 2 && VTK_BUILD_VERSION >= 0))
300 currentLinks->SetDataSet(poly);
301 currentLinks->BuildLinks();
302#else
303 currentLinks->BuildLinks(poly);
304#endif
305
306 newPolys->Delete();
307 newCell->Delete();
308 if (this->OnePassOnly)
309 {
310 break;
311 }
312 }
313
314 if (anyUnremovable && !this->IncludeUnremovable)
315 {
316 vtkCellArray* newPolys = vtkCellArray::New();
317 vtkIdList* newCell = vtkIdList::New();
318 currentPolys->InitTraversal();
319 for (i=0; i<currentPolys->GetNumberOfCells(); i++)
320 {
321 currentPolys->GetNextCell(npts,pts);
322 newCell->Initialize();
323 if (npts==0)
324 {
325 newPolys->InsertNextCell(npts,pts);
326 continue;
327 }
328 if (!isUnremovable[i])
329 {
330 for (j=0; j<npts; j++)
331 {
332 newCell->InsertNextId(pts[j]);
333 }
334 }
335 if (newCell->GetNumberOfIds() > 2)
336 {
337 newPolys->InsertNextCell(newCell);
338 }
339 }
340 currentPolys->DeepCopy(newPolys);
341 newPolys->Delete();
342 newCell->Delete();
343 }
344
345 // simply passes points and point data (eventually vtkCleanPolyData)
346 // will have less cells but same points
347 output->SetPoints(input->GetPoints());
348 output->GetPointData()->PassData(input->GetPointData());
349
350 // WARNING: cell data are thrown away in the current version
351 output->SetPolys(currentPolys);
352
353 delete[] isUnremovable;
354
355 currentLinks->Delete();
356 currentPolys->Delete();
357 poly->Delete();
358
359 return 1;
360}
361
362void vtkvmtkSimplifyVoronoiDiagram::PrintSelf(std::ostream& os, vtkIndent indent)
363{
364 this->Superclass::PrintSelf(os,indent);
365}
Remove non essential Voronoi polygon points (little spikes).
void PrintSelf(std::ostream &os, vtkIndent indent) override
vtkIdType IsBoundaryEdge(vtkCellLinks *links, vtkIdType *edge)
int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
vtkStandardNewMacro(vtkvmtkSimplifyVoronoiDiagram)
#define VTK_VMTK_REMOVE_BOUNDARY_CELLS
#define VTK_VMTK_REMOVE_BOUNDARY_POINTS