Merge the input centerlines into a tree of two-point lines, numbered from the inlet, with a temporary branch and bifurcation classification of each point, and smooth each branch.
433{
434 vtkNew<vtkCleanPolyData> cleaner;
436 cleaner->PointMergingOn();
437 cleaner->Update();
438 vtkPolyData* centerlines = cleaner->GetOutput();
439
442 {
443 vtkErrorMacro(<<"Centerlines have no cells");
444 return false;
445 }
446
447 if (!IsOnePiece(centerlines))
448 {
449 vtkErrorMacro(<<"Centerlines consist of more than one piece");
450 return false;
451 }
452
453 const vtkIdType numberOfPoints = centerlines->GetNumberOfPoints();
454 vtkDataArray* inputRadius = centerlines->GetPointData()->GetArray(this->
RadiusArrayName);
455
456 vtkNew<vtkPoints> points;
457 vtkNew<vtkCellArray> lines;
458
459 vtkNew<vtkDoubleArray> radius;
461 radius->SetNumberOfValues(numberOfPoints);
462 radius->Fill(0.0);
463
464 vtkNew<vtkIntArray> globalNodeId;
466 globalNodeId->SetNumberOfValues(numberOfPoints);
467 globalNodeId->Fill(0);
468
469 vtkNew<vtkIntArray> centerlineId;
472 centerlineId->SetNumberOfTuples(numberOfPoints);
473 centerlineId->Fill(0);
474
475
476
477
478 std::vector<vtkIdType> newPointIds(numberOfPoints,-1);
479 newPointIds[0] = points->InsertNextPoint(centerlines->GetPoint(0));
480 radius->SetValue(0,inputRadius->GetTuple1(0));
481
482 vtkNew<vtkIdList> cellPointIds;
484 {
485 centerlines->GetCellPoints(c,cellPointIds);
486 if (cellPointIds->GetNumberOfIds() == 0 || newPointIds[cellPointIds->GetId(0)] < 0)
487 {
488 vtkErrorMacro(<<"Centerlines must all start from the same inlet point");
489 return false;
490 }
491 for (vtkIdType p=0; p<cellPointIds->GetNumberOfIds(); p++)
492 {
493 vtkIdType id = cellPointIds->GetId(p);
494 if (newPointIds[id] < 0)
495 {
496 vtkIdType newId = points->InsertNextPoint(centerlines->GetPoint(id));
497 newPointIds[id] = newId;
498 vtkIdType line[2] = {newPointIds[cellPointIds->GetId(p-1)], newId};
499 lines->InsertNextCell(2,line);
500 radius->SetValue(newId,inputRadius->GetTuple1(id));
501 globalNodeId->SetValue(newId,newId);
502 }
503 centerlineId->SetComponent(newPointIds[id],c,1);
504 }
505 }
506
507 vtkNew<vtkPolyData> polyData;
508 polyData->SetPoints(points);
509 polyData->SetLines(lines);
510
511 if (polyData->GetNumberOfPoints() != numberOfPoints)
512 {
513 vtkErrorMacro(<<"Centerlines have points that belong to no centerline");
514 return false;
515 }
516
517 if (polyData->GetNumberOfPoints() != polyData->GetNumberOfCells() + 1)
518 {
519 vtkErrorMacro(<<"Centerlines do not form a tree");
520 return false;
521 }
522
523
524
525 vtkNew<vtkIntArray> bifurcationIds;
526 bifurcationIds->SetName(BifurcationIdTmpArrayName);
527 bifurcationIds->SetNumberOfValues(numberOfPoints);
528 bifurcationIds->Fill(Undefined);
529 vtkNew<vtkIntArray> branchIds;
530 branchIds->SetName(BranchIdTmpArrayName);
531 branchIds->SetNumberOfValues(numberOfPoints);
532 branchIds->Fill(Undefined);
533
534 polyData->GetPointData()->AddArray(bifurcationIds);
535 polyData->GetPointData()->AddArray(branchIds);
536 polyData->GetPointData()->AddArray(radius);
537 polyData->GetPointData()->AddArray(globalNodeId);
538 polyData->GetPointData()->AddArray(centerlineId);
539
540 std::vector<std::vector<vtkIdType>> pointCells = GetPointCells(polyData);
541
542 int numberOfBranches = 0;
543 for (vtkIdType p=0; p<numberOfPoints; p++)
544 {
545 branchIds->SetValue(p,numberOfBranches);
546 if (pointCells[p].size() == 1 && p != 0)
547 {
548 numberOfBranches++;
549 }
550 else if (pointCells[p].size() > 2)
551 {
552 bifurcationIds->SetValue(p,Bifurcation);
553 numberOfBranches++;
554 }
555 }
556
557 vtkNew<vtkPointLocator> locator;
558 BuildPointLocator(polyData,locator);
559 vtkNew<vtkIdList> closePoints;
560 double point[3];
561
562
563
564 for (vtkIdType i=0; i<numberOfPoints; i++)
565 {
566 if (pointCells[i].size() <= 2)
567 {
568 continue;
569 }
570 int upstreamBranchId = branchIds->GetValue(i);
571 std::set<int> downstreamBranchIds;
572 for (vtkIdType cellId : pointCells[i])
573 {
574 polyData->GetCellPoints(cellId,cellPointIds);
575 for (vtkIdType k=0; k<cellPointIds->GetNumberOfIds(); k++)
576 {
577 int id = branchIds->GetValue(cellPointIds->GetId(k));
578 if (id != upstreamBranchId)
579 {
580 downstreamBranchIds.insert(id);
581 }
582 }
583 }
584 polyData->GetPoint(i,point);
585 locator->FindPointsWithinRadius(radius->GetValue(i),point,closePoints);
586 for (vtkIdType k=0; k<closePoints->GetNumberOfIds(); k++)
587 {
588 vtkIdType closePointId = closePoints->GetId(k);
589 if (downstreamBranchIds.count(branchIds->GetValue(closePointId)))
590 {
591 bifurcationIds->SetValue(closePointId,BifurcationDownstream);
592 }
593 }
594 }
595
596
597
598
599 std::vector<bool> nearCap(numberOfPoints,false);
600 for (vtkIdType i=0; i<numberOfPoints; i++)
601 {
602 if (pointCells[i].size() != 1)
603 {
604 continue;
605 }
606 polyData->GetPoint(i,point);
607 locator->FindPointsWithinRadius(2.0 * radius->GetValue(i),point,closePoints);
608 for (vtkIdType k=0; k<closePoints->GetNumberOfIds(); k++)
609 {
610 vtkIdType closePointId = closePoints->GetId(k);
611 if (branchIds->GetValue(closePointId) == branchIds->GetValue(i))
612 {
613 nearCap[closePointId] = true;
614 }
615 }
616 }
617
618
619
620
621 const int numberOfIterations = 200;
622 const double capRelaxation = 1.0;
623 const double relaxation = 0.01;
624 double point0[3], point1[3], point2[3], displacement[3], tangent[3];
625 for (int iteration=0; iteration<numberOfIterations; iteration++)
626 {
627 for (vtkIdType k=1; k<numberOfPoints-1; k++)
628 {
629 int branchId = branchIds->GetValue(k);
630 if (branchIds->GetValue(k-1) != branchId || branchIds->GetValue(k+1) != branchId)
631 {
632 continue;
633 }
634 points->GetPoint(k-1,point0);
635 points->GetPoint(k,point1);
636 points->GetPoint(k+1,point2);
637 for (int l=0; l<3; l++)
638 {
639 displacement[l] = 0.5 * (point0[l] + point2[l]) - point1[l];
640 }
641 if (nearCap[k])
642 {
643 double distance01 = sqrt(vtkMath::Distance2BetweenPoints(point0,point1));
644 double distance12 = sqrt(vtkMath::Distance2BetweenPoints(point1,point2));
645 for (int l=0; l<3; l++)
646 {
647 tangent[l] = (point1[l] - point0[l]) / distance01 + (point2[l] - point1[l]) / distance12;
648 }
649 vtkMath::Normalize(tangent);
650 double tangentialDisplacement = vtkMath::Dot(displacement,tangent);
651 for (int l=0; l<3; l++)
652 {
653 point1[l] += capRelaxation * (displacement[l] - tangentialDisplacement * tangent[l]);
654 }
655 }
656 else
657 {
658 for (int l=0; l<3; l++)
659 {
660 point1[l] += relaxation * displacement[l];
661 }
662 }
663 points->SetPoint(k,point1);
664 }
665 }
666
668 return true;
669}