213{
214
215 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
216 vtkInformation *outInfo = outputVector->GetInformationObject(0);
217
218 vtkPolyData *input = vtkPolyData::SafeDownCast(
219 inInfo->Get(vtkDataObject::DATA_OBJECT()));
220 vtkPolyData *output = vtkPolyData::SafeDownCast(
221 outInfo->Get(vtkDataObject::DATA_OBJECT()));
222
223
224 if ( input->GetNumberOfPoints() < 6 )
225 {
226 vtkErrorMacro(<< "Error: too few input points.");
227 return 1;
228 }
229 input->BuildLinks();
230
231
232 vtkSmartPointer<vtkPoints> newPoints = vtkSmartPointer<vtkPoints>::New();
233 newPoints->DeepCopy(input->GetPoints());
234 vtkSmartPointer<vtkCellArray> newPolys = vtkSmartPointer<vtkCellArray>::New();
235 newPolys->DeepCopy(input->GetPolys());
236
237
239 vtkSmartPointer<vtkIntArray> cellEntityIdsArray;
240 if (markCells)
241 {
242 cellEntityIdsArray = vtkSmartPointer<vtkIntArray>::New();
244 if (input->GetCellData()->GetArray(this->CellEntityIdsArrayName))
245 {
246 cellEntityIdsArray->DeepCopy(input->GetCellData()->GetArray(this->CellEntityIdsArrayName));
247 }
248 else
249 {
250 cellEntityIdsArray->SetNumberOfTuples(newPolys->GetNumberOfCells());
252 }
253 }
254
255
256 info("Finding boundaries of input surface...");
257
258
259
260 vtkNew<vtkPolyData> boundariesData;
261 vtkNew<vtkIdList> boundaryLabels;
264 vtkPolyData* boundaries = boundariesData;
265
266 int numberOfBoundaries = boundaries->GetNumberOfCells();
267 std::cout << "Found " << numberOfBoundaries << " boundaries." << std::endl;
268
269
270 info("Computing boundary barycenters...");
271 vtkSmartPointer<vtkPoints> barycenters = vtkSmartPointer<vtkPoints>::New();
272 barycenters->SetNumberOfPoints(numberOfBoundaries);
273 for (int i=0; i<numberOfBoundaries; i++)
274 {
275 double barycenter[3];
277 barycenters->SetPoint(i, barycenter);
278 }
279
280
281 info("Pairing boundaries...");
283 || (!this->BoundaryIds && numberOfBoundaries % 2) )
284 {
285 vtkErrorMacro(<< "Error: the number of boundaries must be even.");
286 }
287
288
289 vtkNew<vtkIdList> boundaryIndicesToUse;
290 vtkIdList* boundaryIndexFilter = this->
BoundaryIds;
292 {
293 for (vtkIdType boundaryIndex=0; boundaryIndex<numberOfBoundaries; boundaryIndex++)
294 {
295 if (this->
BoundaryIds->IsId(boundaryLabels->GetId(boundaryIndex)) != -1)
296 {
297 boundaryIndicesToUse->InsertNextId(boundaryIndex);
298 }
299 }
300 boundaryIndexFilter = boundaryIndicesToUse;
301 }
303
304
305 for (size_t pairingCount=0; pairingCount<boundaryPairs.size(); ++pairingCount)
306 {
307 vtkIdType i0 = boundaryPairs[pairingCount].first;
308 vtkIdType i1 = boundaryPairs[pairingCount].second;
309
310
311 info("Finding boundary point ids...");
312 vtkIdTypeArray * boundaryPointIdsArray = vtkIdTypeArray::SafeDownCast(boundaries->GetPointData()->GetScalars());
313
314
315 vtkPolyLine* boundary0 = vtkPolyLine::SafeDownCast(boundaries->GetCell(i0));
316 vtkIdType numberOfBoundaryPoints0 = boundary0->GetNumberOfPoints();
317 if (numberOfBoundaryPoints0 < 8)
318 {
319 vtkErrorMacro(<< "Error: Assuming at least 8 boundary points!");
320 }
321 vtkSmartPointer<vtkIdList> boundaryPointIds0 = vtkSmartPointer<vtkIdList>::New();
322 boundaryPointIds0->SetNumberOfIds(numberOfBoundaryPoints0);
323 for (int j=0; j<numberOfBoundaryPoints0; j++)
324 {
325
326 boundaryPointIds0->SetId(j, boundaryPointIdsArray->GetValue(boundary0->GetPointId(j)));
327 }
328
329
330 vtkPolyLine* boundary1 = vtkPolyLine::SafeDownCast(boundaries->GetCell(i1));
331 vtkIdType numberOfBoundaryPoints1 = boundary1->GetNumberOfPoints();
332 if (numberOfBoundaryPoints1 < 8)
333 {
334 vtkErrorMacro(<< "Error: Assuming at least 8 boundary points!");
335 }
336 vtkSmartPointer<vtkIdList> boundaryPointIds1 = vtkSmartPointer<vtkIdList>::New();
337 boundaryPointIds1->SetNumberOfIds(numberOfBoundaryPoints1);
338 for (int j=0; j<numberOfBoundaryPoints1; j++)
339 {
340
341 boundaryPointIds1->SetId(j, boundaryPointIdsArray->GetValue(boundary1->GetPointId(j)));
342 }
343
344
345 info("Finding closest vertices...");
347 vtkIdType i0start = starts.first;
348 vtkIdType i1start = starts.second;
349
350
351
352 int i0forward = (i0start + 1) % numberOfBoundaryPoints0;
353 int i1forward = (i1start + 1) % numberOfBoundaryPoints1;
354 double cross[2][3];
355 double barycenter[3];
356 double startingPoint[3];
357 double pointForward[3];
358
359 barycenters->GetPoint(i0, barycenter);
360 input->GetPoint(boundaryPointIds0->GetId(i0start), startingPoint);
361 input->GetPoint(boundaryPointIds0->GetId(i0forward), pointForward);
362 cross_diff(startingPoint, pointForward, barycenter, cross[0]);
363
364 barycenters->GetPoint(i1, barycenter);
365 input->GetPoint(boundaryPointIds1->GetId(i1start), startingPoint);
366 input->GetPoint(boundaryPointIds1->GetId(i1forward), pointForward);
367 cross_diff(startingPoint, pointForward, barycenter, cross[1]);
368
369 int dir0 = +1;
370 bool backwards = vtkMath::Dot(cross[0], cross[1]) < 0.0;
371 int dir1 = backwards ? dir0: -dir0;
372
373
374 double nudgeFactor = 0.01;
375 i0forward = (i0start + dir0 + numberOfBoundaryPoints0) % numberOfBoundaryPoints0;
376 input->GetPoint(boundaryPointIds0->GetId(i0start), startingPoint);
377 input->GetPoint(boundaryPointIds0->GetId(i0forward), pointForward);
378 double nudgeVector0[3] = { nudgeFactor*(pointForward[0] - startingPoint[0]),
379 nudgeFactor*(pointForward[1] - startingPoint[1]),
380 nudgeFactor*(pointForward[2] - startingPoint[2]) };
381 i1forward = (i1start - dir1 + numberOfBoundaryPoints1) % numberOfBoundaryPoints1;
382 input->GetPoint(boundaryPointIds1->GetId(i1start), startingPoint);
383 input->GetPoint(boundaryPointIds1->GetId(i1forward), pointForward);
384 double nudgeVector1[3] = { nudgeFactor*(pointForward[0] - startingPoint[0]),
385 nudgeFactor*(pointForward[1] - startingPoint[1]),
386 nudgeFactor*(pointForward[2] - startingPoint[2]) };
387
388
389 info("Setting up n-sided polygon...");
390
391 int npts = numberOfBoundaryPoints0 + numberOfBoundaryPoints1 + 2;
392 vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New();
393 vtkPoints * polygonPoints = polygon->GetPoints();
394 vtkIdList * polygonPointIds = polygon->GetPointIds();
395 polygonPoints->SetNumberOfPoints(npts);
396 polygonPointIds->SetNumberOfIds(npts);
397 vector<vtkIdType> polygonPointIdToGlobalPointId(npts);
398
399
400 int kk = 0;
401 for (int k=0; k<=numberOfBoundaryPoints0; k++)
402 {
403 vtkIdType bp0id = (i0start + dir0*k + numberOfBoundaryPoints0) % numberOfBoundaryPoints0;
404 vtkIdType pid = boundaryPointIds0->GetId(bp0id);
405 double point[3];
406 input->GetPoint(pid, point);
407 if (k == 0)
408 {
409 point[0] += nudgeVector0[0];
410 point[1] += nudgeVector0[1];
411 point[2] += nudgeVector0[2];
412 }
413 polygonPointIdToGlobalPointId[kk] = pid;
414 polygonPointIds->SetId(kk,kk);
415 polygonPoints->InsertPoint(kk, point);
416 kk++;
417 }
418
419
420 for (int k=0; k<=numberOfBoundaryPoints1; k++)
421 {
422 vtkIdType bp1id = (i1start + dir1*k + numberOfBoundaryPoints1) % numberOfBoundaryPoints1;
423 vtkIdType pid = boundaryPointIds1->GetId(bp1id);
424 double point[3];
425 input->GetPoint(pid, point);
426 if (k == numberOfBoundaryPoints1)
427 {
428 point[0] += nudgeVector1[0];
429 point[1] += nudgeVector1[1];
430 point[2] += nudgeVector1[2];
431 }
432 polygonPointIdToGlobalPointId[kk] = pid;
433 polygonPointIds->SetId(kk,kk);
434 polygonPoints->InsertPoint(kk, point);
435 kk++;
436 }
437
438
439 info("Triangulating...");
440 vtkSmartPointer<vtkIdList> outTris = vtkSmartPointer<vtkIdList>::New();
441 int triRes = polygon->NonDegenerateTriangulate(outTris);
442 if (triRes == 0)
443 {
444 vtkErrorMacro(<< "Error: Numerical degeneracy when triangulating endcaps surface! Code: " << triRes);
445 }
446
447
448 info("Appending datasets...");
449 size_t nt = outTris->GetNumberOfIds()/3;
450 for (size_t it=0; it<nt; ++it)
451 {
452
453 vtkIdType triangleIds[3] = { polygonPointIdToGlobalPointId[outTris->GetId(3*it+0)],
454 polygonPointIdToGlobalPointId[outTris->GetId(3*it+1)],
455 polygonPointIdToGlobalPointId[outTris->GetId(3*it+2)] };
456
457
458 newPolys->InsertNextCell(3, triangleIds);
459
460 if (markCells)
461 {
462
463
464 vtkIdType boundaryId = useBoundaryLabels ? boundaryLabels->GetId(i0) : i0;
465 vtkIdType partnerBoundaryId = useBoundaryLabels ? boundaryLabels->GetId(i1) : i1;
466 cellEntityIdsArray->InsertNextValue(static_cast<int>(
468 }
469 }
470 }
471
472
473 output->SetPoints(newPoints);
474 output->SetPolys(newPolys);
475 output->GetPointData()->PassData(input->GetPointData());
476 if (markCells)
477 {
478 output->GetCellData()->AddArray(cellEntityIdsArray);
479 }
480 info("Done!");
481
482 return 1;
483}
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])
vtkIdType PairCapCellEntityId(vtkIdType positionalId, vtkIdType boundaryId, vtkIdType partnerBoundaryId, bool useBoundaryLabels)
pair< vtkIdType, vtkIdType > IdPair
vector< IdPair > build_closest_pairs(vtkSmartPointer< vtkPoints > barycenters, vtkIdList *boundaryIds)
IdPair find_closest_points(vtkPolyData *points, vtkSmartPointer< vtkIdList > pointIds0, vtkSmartPointer< vtkIdList > pointIds1)