VMTK
Loading...
Searching...
No Matches
vtkvmtkPolyDataBoundaryExtractor.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkPolyDataBoundaryExtractor.cxx,v $
5Language: C++
6Date: $Date: 2006/07/27 08:27:26 $
7Version: $Revision: 1.6 $
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 "vtkPolyData.h"
25#include "vtkPointData.h"
26#include "vtkPoints.h"
27#include "vtkIdList.h"
28#include "vtkGenericCell.h"
29#include "vtkIdTypeArray.h"
30#include "vtkInformation.h"
31#include "vtkInformationVector.h"
32#include "vtkObjectFactory.h"
33
34
36
40
42 vtkInformation *vtkNotUsed(request),
43 vtkInformationVector **inputVector,
44 vtkInformationVector *outputVector)
45{
46 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
47 vtkInformation *outInfo = outputVector->GetInformationObject(0);
48
49 vtkPolyData *input = vtkPolyData::SafeDownCast(
50 inInfo->Get(vtkDataObject::DATA_OBJECT()));
51 vtkPolyData *output = vtkPolyData::SafeDownCast(
52 outInfo->Get(vtkDataObject::DATA_OBJECT()));
53
54 vtkIdList *boundary, *boundaryIds, *cellEdgeNeighbors, *newCell;
55 vtkIdType i, j, currentId, id, id0, id1;
56 vtkCell* cell;
57 bool foundAny, foundNeighbor, done;
58 vtkPoints* newPoints;
59 vtkCellArray* newLines;
60 vtkIdTypeArray* newScalars;
61
62 // The topology queries below need cell links, but they must not be built on the input: a
63 // filter does not own its input, and since VTK 9.4 the links hold a reference back to the
64 // dataset they were built for. That reference cycle is normally reclaimed by VTK's garbage
65 // collector, but when it is created while another filter's Update() is executing - this
66 // extractor runs inside vtkvmtkPolyDataSurfaceRemeshing, among others - the collector never
67 // reclaims it, and the caller's surface is leaked together with the links, its cell map and
68 // its cell instances. The links are built instead on a shallow structural copy, which shares
69 // the points and cell arrays (so every point and cell id below means the same thing) but
70 // keeps the acceleration structures, and their reference cycle, to itself.
71 vtkPolyData* working = vtkPolyData::New();
72 working->CopyStructure(input);
73 working->BuildCells();
74 working->BuildLinks();
75
76 // Allocate
77 boundary = vtkIdList::New();
78 boundaryIds = vtkIdList::New();
79 cellEdgeNeighbors = vtkIdList::New();
80 newCell = vtkIdList::New();
81 newPoints = vtkPoints::New();
82 newLines = vtkCellArray::New();
83 newScalars = vtkIdTypeArray::New();
84
85 // Execute
86 for (i=0; i<working->GetNumberOfCells(); i++)
87 {
88 cell = working->GetCell(i);
89 for (j=0; j<3; j++)
90 {
91 cellEdgeNeighbors->Initialize();
92 auto edge = cell->GetEdge(j);
93 if (!edge) continue;
94 auto pointIds = edge->GetPointIds();
95 id0 = pointIds->GetId(0);
96 id1 = pointIds->GetId(1);
97 working->GetCellEdgeNeighbors(i,id0,id1,cellEdgeNeighbors);
98 if (cellEdgeNeighbors->GetNumberOfIds()==0)
99 {
100 boundaryIds->InsertUniqueId(id0);
101 boundaryIds->InsertUniqueId(id1);
102 }
103 }
104 }
105
106 if (boundaryIds->GetNumberOfIds() == 0)
107 {
108 output->SetPoints(newPoints);
109 output->SetLines(newLines);
110 output->GetPointData()->SetScalars(newScalars);
111
112 newPoints->Delete();
113 newLines->Delete();
114 newScalars->Delete();
115
116 boundary->Delete();
117 boundaryIds->Delete();
118 cellEdgeNeighbors->Delete();
119 newCell->Delete();
120
121 // Dropping the links first breaks their reference cycle with the working copy by plain
122 // reference counting, so its destruction cannot depend on the garbage collector either.
123 working->Initialize();
124 working->Delete();
125
126 return 1;
127 }
128
129 foundAny = false;
130 foundNeighbor = false;
131 done = false;
132 currentId = -1;
133 int loopCount = 0;
134 bool isBoundaryEdge;
135
136 while (!done)
137 {
138 foundAny = false;
139 foundNeighbor = false;
140
141 for (i=0; i<boundaryIds->GetNumberOfIds(); i++)
142 {
143 id = boundaryIds->GetId(i);
144 if (id!=-1)
145 {
146 foundAny = true;
147 isBoundaryEdge = false;
148 if (currentId!=-1 && working->IsEdge(currentId,id))
149 {
150 cellEdgeNeighbors->Initialize();
151 working->GetCellEdgeNeighbors(-1,currentId,id,cellEdgeNeighbors);
152 if (cellEdgeNeighbors->GetNumberOfIds() == 1)
153 {
154 isBoundaryEdge = true;
155 }
156 }
157 if ((currentId==-1) || isBoundaryEdge)
158 {
159 foundNeighbor = true;
160 }
161
162 if (foundNeighbor)
163 {
164 currentId = id;
165 boundary->InsertNextId(currentId);
166 boundaryIds->SetId(i,-1);
167 break;
168 }
169 }
170 }
171
172 if ( (((!foundNeighbor)&&(foundAny))||(!foundAny)) && (boundary->GetNumberOfIds() > 2))
173 {
174 // Each point of the boundary is listed once. The polyline is a ring, and the caller closes
175 // it, which is what every caller in vmtk does: it takes the number of points of the cell
176 // and steps round with (j+1) modulo that. Repeating the first point at the end to mark the
177 // ring as closed would put a duplicate in the middle of that arithmetic, which biases the
178 // barycenter, the mean radius and the normal computed from these points, and leaves a
179 // degenerate triangle in the fan of a cap and in the first layer of a flow extension.
180 newCell->Initialize();
181 newCell->SetNumberOfIds(boundary->GetNumberOfIds());
182 for (j=0; j<boundary->GetNumberOfIds(); j++)
183 {
184 id = boundary->GetId(j);
185 newCell->SetId(j,newPoints->InsertNextPoint(working->GetPoint(id)));
186 newScalars->InsertNextValue(id);
187 }
188
189 newLines->InsertNextCell(newCell);
190
191 currentId = -1;
192 boundary->Initialize();
193 }
194
195 if (!foundAny)
196 {
197 done = true;
198 }
199
200 loopCount++;
201 if (loopCount > 2*boundaryIds->GetNumberOfIds())
202 {
203 int missing = 0;
204 for (int n=0; n<boundaryIds->GetNumberOfIds(); n++)
205 {
206 if (boundaryIds->GetId(n) != -1)
207 {
208 missing++;
209 }
210 }
211 vtkErrorMacro(<<"Can't find adjacent point. Bailing out.");
212 done = true;
213 }
214 }
215
216 output->SetPoints(newPoints);
217 output->SetLines(newLines);
218 output->GetPointData()->SetScalars(newScalars);
219
220 // Destroy
221 newPoints->Delete();
222 newLines->Delete();
223 newScalars->Delete();
224
225 boundary->Delete();
226 boundaryIds->Delete();
227 cellEdgeNeighbors->Delete();
228 newCell->Delete();
229
230 // Dropping the links first breaks their reference cycle with the working copy by plain
231 // reference counting, so its destruction cannot depend on the garbage collector either.
232 working->Initialize();
233 working->Delete();
234
235 return 1;
236}
237
238void vtkvmtkPolyDataBoundaryExtractor::PrintSelf(std::ostream& os, vtkIndent indent)
239{
240 this->Superclass::PrintSelf(os,indent);
241}
Extract boundary edges as poly lines.
void PrintSelf(std::ostream &os, vtkIndent indent) override
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
vtkStandardNewMacro(vtkvmtkPolyDataBoundaryExtractor)