79{
80 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
81 vtkInformation *outInfo = outputVector->GetInformationObject(0);
82
83 vtkPolyData *input = vtkPolyData::SafeDownCast(
84 inInfo->Get(vtkDataObject::DATA_OBJECT()));
85 vtkPolyData *output = vtkPolyData::SafeDownCast(
86 outInfo->Get(vtkDataObject::DATA_OBJECT()));
87
88 if ( input->GetNumberOfPoints() < 1 )
89 {
90 return 1;
91 }
92
93 input->BuildLinks();
94
95 vtkPoints* newPoints = vtkPoints::New();
96 newPoints->DeepCopy(input->GetPoints());
97
98 vtkCellArray* newPolys = vtkCellArray::New();
99 newPolys->DeepCopy(input->GetPolys());
100
101 vtkIdTypeArray* cellEntityIdsArray = NULL;
103 if (markCells)
104 {
105 cellEntityIdsArray = vtkIdTypeArray::New();
107 if (input->GetCellData()->GetArray(this->CellEntityIdsArrayName))
108 {
109 cellEntityIdsArray->DeepCopy(input->GetCellData()->GetArray(this->CellEntityIdsArrayName));
110 }
111 else
112 {
113 cellEntityIdsArray->SetNumberOfTuples(newPolys->GetNumberOfCells());
115 }
116 }
117
118
119
120
121 vtkNew<vtkPolyData> boundaries;
122 vtkNew<vtkIdList> boundaryLabels;
125 int numberOfBoundaries = boundaries->GetNumberOfCells();
126
128 || (!this->BoundaryIds && numberOfBoundaries % 2) )
129 {
130 vtkErrorMacro(<< "Error: the number of boundaries must be even.");
131 newPoints->Delete();
132 newPolys->Delete();
133 if (markCells)
134 {
135 cellEntityIdsArray->Delete();
136 }
137 }
138
139 vtkPoints* barycenters = vtkPoints::New();
140 barycenters->SetNumberOfPoints(numberOfBoundaries);
141
142 vtkIdList* boundaryPairings = vtkIdList::New();
143 boundaryPairings->SetNumberOfIds(numberOfBoundaries);
144 vtkIdList* visitedBoundaries = vtkIdList::New();
145 visitedBoundaries->SetNumberOfIds(numberOfBoundaries);
146
147 double barycenter[3];
148 for (int i=0; i<numberOfBoundaries; i++)
149 {
150 vtkCell* boundary = boundaries->GetCell(i);
152 barycenters->SetPoint(i,barycenter);
153 boundaryPairings->SetId(i,-1);
154 visitedBoundaries->SetId(i,-1);
155 }
156
157 double currentBarycenter[3];
158 double distance2, minDistance2 = 0.0;
159 vtkIdType closestBoundaryId;
160 for (int i=0; i<numberOfBoundaries; i++)
161 {
163 {
164 continue;
165 }
166 if (boundaryPairings->GetId(i) != -1 || boundaryPairings->IsId(i) != -1)
167 {
168 continue;
169 }
170 barycenters->GetPoint(i,barycenter);
171 closestBoundaryId = -1;
172 for (int j=i+1; j<numberOfBoundaries; j++)
173 {
175 {
176 continue;
177 }
178 if (boundaryPairings->GetId(j) != -1 || boundaryPairings->IsId(j) != -1)
179 {
180 continue;
181 }
182 barycenters->GetPoint(j,currentBarycenter);
183 distance2 = vtkMath::Distance2BetweenPoints(barycenter,currentBarycenter);
184 if (closestBoundaryId == -1 || distance2 < minDistance2)
185 {
186 minDistance2 = distance2;
187 closestBoundaryId = j;
188 }
189 }
190 boundaryPairings->SetId(i,closestBoundaryId);
191 boundaryPairings->SetId(closestBoundaryId,i);
192 }
193
194 for (int i=0; i<numberOfBoundaries; i++)
195 {
197 {
198 continue;
199 }
200
201 if (boundaryPairings->GetId(i) == -1)
202 {
203 continue;
204 }
205
206 if (visitedBoundaries->GetId(i) != -1)
207 {
208 continue;
209 }
210
211 visitedBoundaries->SetId(i,i);
212 visitedBoundaries->SetId(boundaryPairings->GetId(i),i);
213
214 vtkIdTypeArray* boundaryPointIdsArray = vtkIdTypeArray::New();
215 boundaryPointIdsArray->DeepCopy(boundaries->GetPointData()->GetScalars());
216
217 vtkPolyLine* boundary0 = vtkPolyLine::SafeDownCast(boundaries->GetCell(i));
218 vtkIdType numberOfBoundaryPoints0 = boundary0->GetNumberOfPoints();
219 vtkIdList* boundaryPointIds0 = vtkIdList::New();
220 boundaryPointIds0->SetNumberOfIds(numberOfBoundaryPoints0);
221 for (int j=0; j<numberOfBoundaryPoints0; j++)
222 {
223 boundaryPointIds0->SetId(j,boundaryPointIdsArray->GetValue(boundary0->GetPointId(j)));
224 }
225
226 vtkPolyLine* boundary1 = vtkPolyLine::SafeDownCast(boundaries->GetCell(boundaryPairings->GetId(i)));
227 vtkIdType numberOfBoundaryPoints1 = boundary1->GetNumberOfPoints();
228 vtkIdList* boundaryPointIds1 = vtkIdList::New();
229 boundaryPointIds1->SetNumberOfIds(numberOfBoundaryPoints1);
230 for (int j=0; j<numberOfBoundaryPoints1; j++)
231 {
232 boundaryPointIds1->SetId(j,boundaryPointIdsArray->GetValue(boundary1->GetPointId(j)));
233 }
234
235 double startingPoint[3], currentPoint[3];
236 input->GetPoint(boundaryPointIds0->GetId(0),startingPoint);
237
238 vtkIdType offset = -1;
239 for (int j=0; j<numberOfBoundaryPoints1; j++)
240 {
241 input->GetPoint(boundaryPointIds1->GetId(j),currentPoint);
242 distance2 = vtkMath::Distance2BetweenPoints(startingPoint,currentPoint);
243 if (offset == -1 || distance2 < minDistance2)
244 {
245 offset = j;
246 minDistance2 = distance2;
247 }
248 }
249
250 double vectorToStart[3], vectorToForward[3], cross0[3], cross1[3];
251 double pointForward[3];
252
253 barycenters->GetPoint(i,barycenter);
254 input->GetPoint(boundaryPointIds0->GetId(0),startingPoint);
255 input->GetPoint(boundaryPointIds0->GetId(numberOfBoundaryPoints0/8),pointForward);
256
257 vectorToStart[0] = startingPoint[0] - barycenter[0];
258 vectorToStart[1] = startingPoint[1] - barycenter[1];
259 vectorToStart[2] = startingPoint[2] - barycenter[2];
260
261 vectorToForward[0] = pointForward[0] - barycenter[0];
262 vectorToForward[1] = pointForward[1] - barycenter[1];
263 vectorToForward[2] = pointForward[2] - barycenter[2];
264
265 vtkMath::Cross(vectorToStart,vectorToForward,cross0);
266
267 barycenters->GetPoint(boundaryPairings->GetId(i),barycenter);
268 input->GetPoint(boundaryPointIds1->GetId(0),startingPoint);
269 input->GetPoint(boundaryPointIds1->GetId(numberOfBoundaryPoints1/8),pointForward);
270
271 vectorToStart[0] = startingPoint[0] - barycenter[0];
272 vectorToStart[1] = startingPoint[1] - barycenter[1];
273 vectorToStart[2] = startingPoint[2] - barycenter[2];
274
275 vectorToForward[0] = pointForward[0] - barycenter[0];
276 vectorToForward[1] = pointForward[1] - barycenter[1];
277 vectorToForward[2] = pointForward[2] - barycenter[2];
278
279 vtkMath::Cross(vectorToStart,vectorToForward,cross1);
280
281 bool backward = false;
282 if (vtkMath::Dot(cross0,cross1) < 0.0)
283 {
284 backward = true;
285 }
286
287 double point0[3], nextPoint0[3];
288 double point1[3], nextPoint1[3];
289 vtkIdType pointId0, nextPointId0;
290 vtkIdType pointId1, nextPointId1;
291 int j = 0;
292 int k = 0;
293 while (true)
294 {
295 if (j == numberOfBoundaryPoints0 && k == numberOfBoundaryPoints1)
296 {
297 break;
298 }
299
300 pointId0 = j % numberOfBoundaryPoints0;
301 nextPointId0 = (pointId0 + 1) % numberOfBoundaryPoints0;
302 pointId1 = (k + offset) % numberOfBoundaryPoints1;
303 nextPointId1 = (pointId1 + 1) % numberOfBoundaryPoints1;
304 if (backward)
305 {
306 pointId1 = (numberOfBoundaryPoints1 - k + offset + numberOfBoundaryPoints1) % numberOfBoundaryPoints1;
307 nextPointId1 = (pointId1 - 1 + numberOfBoundaryPoints1) % numberOfBoundaryPoints1;
308 }
309
310 input->GetPoint(boundaryPointIds0->GetId(pointId0),point0);
311 input->GetPoint(boundaryPointIds0->GetId(nextPointId0),nextPoint0);
312 input->GetPoint(boundaryPointIds1->GetId(pointId1),point1);
313 input->GetPoint(boundaryPointIds1->GetId(nextPointId1),nextPoint1);
314
315 newPolys->InsertNextCell(3);
316 newPolys->InsertCellPoint(boundaryPointIds0->GetId(pointId0));
317 newPolys->InsertCellPoint(boundaryPointIds1->GetId(pointId1));
318
319 bool next1 = false;
320 if (vtkMath::Distance2BetweenPoints(point0,nextPoint1) < vtkMath::Distance2BetweenPoints(point1,nextPoint0))
321 {
322 next1 = true;
323 }
324 if (k == numberOfBoundaryPoints1)
325 {
326 next1 = false;
327 }
328 else if (j == numberOfBoundaryPoints0)
329 {
330 next1 = true;
331 }
332
333 if (next1)
334 {
335 newPolys->InsertCellPoint(boundaryPointIds1->GetId(nextPointId1));
336 k++;
337 }
338 else
339 {
340 newPolys->InsertCellPoint(boundaryPointIds0->GetId(nextPointId0));
341 j++;
342 }
343
344 if (markCells)
345 {
346
347
348 vtkIdType boundaryId = useBoundaryLabels ? boundaryLabels->GetId(i) : i;
349 vtkIdType partnerBoundaryId = useBoundaryLabels ? boundaryLabels->GetId(boundaryPairings->GetId(i)) : boundaryPairings->GetId(i);
350 cellEntityIdsArray->InsertNextValue(
352 }
353 }
354
355 boundaryPointIdsArray->Delete();
356 boundaryPointIds0->Delete();
357 boundaryPointIds1->Delete();
358 }
359
360 output->SetPoints(newPoints);
361 output->SetPolys(newPolys);
362
363 output->GetPointData()->PassData(input->GetPointData());
364
365 if (markCells)
366 {
367 output->GetCellData()->AddArray(cellEntityIdsArray);
368 cellEntityIdsArray->Delete();
369 }
370
371 newPoints->Delete();
372 newPolys->Delete();
373 barycenters->Delete();
374 boundaryPairings->Delete();
375 visitedBoundaries->Delete();
376
377 return 1;
378}
vtkIdType PairCapCellEntityId(vtkIdType positionalId, vtkIdType boundaryId, vtkIdType partnerBoundaryId, bool useBoundaryLabels)
static bool GetOrExtractBoundaries(vtkPolyData *surface, const char *boundaryLabelsArrayName, const char *boundaryPointOrderArrayName, vtkPolyData *boundaries, vtkIdList *boundaryLabels, vtkObject *warningSource=nullptr)
static void ComputeBoundaryBarycenter(vtkPoints *points, double barycenter[3])