257 vtkInformation *vtkNotUsed(request),
258 vtkInformationVector **inputVector,
259 vtkInformationVector *outputVector)
261 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
262 vtkInformation *outInfo = outputVector->GetInformationObject(0);
264 vtkPolyData *input = vtkPolyData::SafeDownCast(
265 inInfo->Get(vtkDataObject::DATA_OBJECT()));
266 vtkPolyData *output = vtkPolyData::SafeDownCast(
267 outInfo->Get(vtkDataObject::DATA_OBJECT()));
271 vtkErrorMacro(<<
"Centerlines not set");
280 for (
const char* arrayName : arrayNames)
284 vtkErrorMacro(<<
"An array name is not specified");
289 if (!this->
Centerlines->GetPointData()->GetArray(this->RadiusArrayName))
291 vtkErrorMacro(<<
"Centerlines have no point data array named " << this->
RadiusArrayName);
295 if (input->GetNumberOfPolys() == 0)
297 vtkErrorMacro(<<
"Input surface has no polygons");
302 ProgressStep step(
this,
"Computing surface normals",PROGRESS_NORMALS,PROGRESS_CLEAN);
305 vtkNew<vtkPolyData> surface;
306 surface->ShallowCopy(input);
307 surface->GetPointData()->SetActiveNormals(
nullptr);
309 vtkNew<vtkPolyDataNormals> surfaceNormals;
310 surfaceNormals->SetInputData(surface);
311 surfaceNormals->SplittingOff();
312 surfaceNormals->AutoOrientNormalsOn();
313 surfaceNormals->ComputePointNormalsOn();
314 surfaceNormals->ConsistencyOn();
315 surfaceNormals->Update();
316 this->
Surface->DeepCopy(surfaceNormals->GetOutput());
324 ProgressStep step(
this,
"Merging and smoothing centerlines",PROGRESS_CLEAN,PROGRESS_LABEL_PRELIMINARY);
344 vtkNew<vtkIntArray> centerlineBifurcation;
346 centerlineBifurcation->SetNumberOfValues(this->
Centerlines->GetNumberOfPoints());
347 centerlineBifurcation->Fill(Bifurcation);
348 this->
Centerlines->GetPointData()->AddArray(centerlineBifurcation);
350 vtkNew<vtkIntArray> sectionBifurcation;
352 output->GetCellData()->AddArray(sectionBifurcation);
353 vtkNew<vtkIntArray> sectionGlobalNodeId;
355 output->GetCellData()->AddArray(sectionGlobalNodeId);
358 ProgressStep step(
this,
"Labeling surface",PROGRESS_LABEL_PRELIMINARY,PROGRESS_SECTIONS);
361 if (!this->
LabelSurface(BranchIdTmpArrayName,BifurcationIdTmpArrayName))
371 std::vector<std::vector<vtkIdType>> pointCells = GetPointCells(this->
Centerlines);
372 this->Internals->NumberOfPointCells.resize(pointCells.size());
373 for (
size_t i=0; i<pointCells.size(); i++)
375 this->Internals->NumberOfPointCells[i] =
static_cast<vtkIdType
>(pointCells[i].size());
377 this->Internals->SectionBifurcation = sectionBifurcation;
378 this->Internals->SectionGlobalNodeId = sectionGlobalNodeId;
379 this->Internals->CenterlineBifurcation = centerlineBifurcation;
383 ProgressStep step(
this,
"Computing centerline sections",PROGRESS_SECTIONS,PROGRESS_GROUP);
387 this->Internals->NumberOfPointCells.clear();
388 this->Internals->SectionBifurcation =
nullptr;
389 this->Internals->SectionGlobalNodeId =
nullptr;
390 this->Internals->CenterlineBifurcation =
nullptr;
391 this->Internals->CenterlineNormal =
nullptr;
399 ProgressStep step(
this,
"Grouping branches and bifurcations",PROGRESS_GROUP,PROGRESS_LABEL);
416 ProgressStep step(
this,
"Labeling surface",PROGRESS_LABEL,1.0);
424 this->
Centerlines->GetPointData()->RemoveArray(BranchIdTmpArrayName);
425 this->
Centerlines->GetPointData()->RemoveArray(BifurcationIdTmpArrayName);
426 this->
Surface->GetPointData()->RemoveArray(BranchIdTmpArrayName);
428 this->UpdateProgress(1.0);
434 vtkNew<vtkCleanPolyData> cleaner;
436 cleaner->PointMergingOn();
438 vtkPolyData* centerlines = cleaner->GetOutput();
443 vtkErrorMacro(<<
"Centerlines have no cells");
447 if (!IsOnePiece(centerlines))
449 vtkErrorMacro(<<
"Centerlines consist of more than one piece");
453 const vtkIdType numberOfPoints = centerlines->GetNumberOfPoints();
454 vtkDataArray* inputRadius = centerlines->GetPointData()->GetArray(this->
RadiusArrayName);
456 vtkNew<vtkPoints> points;
457 vtkNew<vtkCellArray> lines;
459 vtkNew<vtkDoubleArray> radius;
461 radius->SetNumberOfValues(numberOfPoints);
464 vtkNew<vtkIntArray> globalNodeId;
466 globalNodeId->SetNumberOfValues(numberOfPoints);
467 globalNodeId->Fill(0);
469 vtkNew<vtkIntArray> centerlineId;
472 centerlineId->SetNumberOfTuples(numberOfPoints);
473 centerlineId->Fill(0);
478 std::vector<vtkIdType> newPointIds(numberOfPoints,-1);
479 newPointIds[0] = points->InsertNextPoint(centerlines->GetPoint(0));
480 radius->SetValue(0,inputRadius->GetTuple1(0));
482 vtkNew<vtkIdList> cellPointIds;
485 centerlines->GetCellPoints(c,cellPointIds);
486 if (cellPointIds->GetNumberOfIds() == 0 || newPointIds[cellPointIds->GetId(0)] < 0)
488 vtkErrorMacro(<<
"Centerlines must all start from the same inlet point");
491 for (vtkIdType p=0; p<cellPointIds->GetNumberOfIds(); p++)
493 vtkIdType
id = cellPointIds->GetId(p);
494 if (newPointIds[
id] < 0)
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);
503 centerlineId->SetComponent(newPointIds[
id],c,1);
507 vtkNew<vtkPolyData> polyData;
508 polyData->SetPoints(points);
509 polyData->SetLines(lines);
511 if (polyData->GetNumberOfPoints() != numberOfPoints)
513 vtkErrorMacro(<<
"Centerlines have points that belong to no centerline");
517 if (polyData->GetNumberOfPoints() != polyData->GetNumberOfCells() + 1)
519 vtkErrorMacro(<<
"Centerlines do not form a tree");
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);
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);
540 std::vector<std::vector<vtkIdType>> pointCells = GetPointCells(polyData);
542 int numberOfBranches = 0;
543 for (vtkIdType p=0; p<numberOfPoints; p++)
545 branchIds->SetValue(p,numberOfBranches);
546 if (pointCells[p].size() == 1 && p != 0)
550 else if (pointCells[p].size() > 2)
552 bifurcationIds->SetValue(p,Bifurcation);
557 vtkNew<vtkPointLocator> locator;
558 BuildPointLocator(polyData,locator);
559 vtkNew<vtkIdList> closePoints;
564 for (vtkIdType i=0; i<numberOfPoints; i++)
566 if (pointCells[i].size() <= 2)
570 int upstreamBranchId = branchIds->GetValue(i);
571 std::set<int> downstreamBranchIds;
572 for (vtkIdType cellId : pointCells[i])
574 polyData->GetCellPoints(cellId,cellPointIds);
575 for (vtkIdType k=0; k<cellPointIds->GetNumberOfIds(); k++)
577 int id = branchIds->GetValue(cellPointIds->GetId(k));
578 if (
id != upstreamBranchId)
580 downstreamBranchIds.insert(
id);
584 polyData->GetPoint(i,point);
585 locator->FindPointsWithinRadius(radius->GetValue(i),point,closePoints);
586 for (vtkIdType k=0; k<closePoints->GetNumberOfIds(); k++)
588 vtkIdType closePointId = closePoints->GetId(k);
589 if (downstreamBranchIds.count(branchIds->GetValue(closePointId)))
591 bifurcationIds->SetValue(closePointId,BifurcationDownstream);
599 std::vector<bool> nearCap(numberOfPoints,
false);
600 for (vtkIdType i=0; i<numberOfPoints; i++)
602 if (pointCells[i].size() != 1)
606 polyData->GetPoint(i,point);
607 locator->FindPointsWithinRadius(2.0 * radius->GetValue(i),point,closePoints);
608 for (vtkIdType k=0; k<closePoints->GetNumberOfIds(); k++)
610 vtkIdType closePointId = closePoints->GetId(k);
611 if (branchIds->GetValue(closePointId) == branchIds->GetValue(i))
613 nearCap[closePointId] =
true;
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++)
627 for (vtkIdType k=1; k<numberOfPoints-1; k++)
629 int branchId = branchIds->GetValue(k);
630 if (branchIds->GetValue(k-1) != branchId || branchIds->GetValue(k+1) != branchId)
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++)
639 displacement[l] = 0.5 * (point0[l] + point2[l]) - point1[l];
643 double distance01 = sqrt(vtkMath::Distance2BetweenPoints(point0,point1));
644 double distance12 = sqrt(vtkMath::Distance2BetweenPoints(point1,point2));
645 for (
int l=0; l<3; l++)
647 tangent[l] = (point1[l] - point0[l]) / distance01 + (point2[l] - point1[l]) / distance12;
649 vtkMath::Normalize(tangent);
650 double tangentialDisplacement = vtkMath::Dot(displacement,tangent);
651 for (
int l=0; l<3; l++)
653 point1[l] += capRelaxation * (displacement[l] - tangentialDisplacement * tangent[l]);
658 for (
int l=0; l<3; l++)
660 point1[l] += relaxation * displacement[l];
663 points->SetPoint(k,point1);
673 const vtkIdType numberOfPoints = this->
Centerlines->GetNumberOfPoints();
674 vtkNew<vtkDoubleArray> tangents;
676 tangents->SetNumberOfComponents(3);
677 tangents->SetNumberOfTuples(numberOfPoints);
679 vtkDataArray* surfaceNormals = this->
Surface->GetPointData()->GetNormals();
680 vtkNew<vtkPointLocator> locator;
681 BuildPointLocator(this->
Surface,locator);
683 std::vector<std::vector<vtkIdType>> pointCells = GetPointCells(this->
Centerlines);
684 vtkNew<vtkIdList> cellPointIds;
685 double point[3], point0[3], point1[3], direction[3];
686 for (vtkIdType p=0; p<numberOfPoints; p++)
688 double tangent[3] = {0.0, 0.0, 0.0};
689 if (pointCells[p].size() == 1)
694 surfaceNormals->GetTuple(locator->FindClosestPoint(point),tangent);
697 vtkMath::MultiplyScalar(tangent,-1.0);
702 for (vtkIdType cellId : pointCells[p])
704 this->
Centerlines->GetCellPoints(cellId,cellPointIds);
705 this->
Centerlines->GetPoint(cellPointIds->GetId(0),point0);
706 this->
Centerlines->GetPoint(cellPointIds->GetId(1),point1);
707 vtkMath::Subtract(point1,point0,direction);
708 vtkMath::Normalize(direction);
709 vtkMath::Add(tangent,direction,tangent);
711 vtkMath::Normalize(tangent);
713 tangents->SetTuple(p,tangent);
716 this->
Centerlines->GetPointData()->AddArray(tangents);
722 const vtkIdType numberOfPoints = centerlines->GetNumberOfPoints();
723 std::vector<std::vector<vtkIdType>> pointCells = GetPointCells(centerlines);
725 vtkDataArray* radius = centerlines->GetPointData()->GetArray(this->
RadiusArrayName);
727 vtkDataArray* bifurcationIds = centerlines->GetPointData()->GetArray(BifurcationIdTmpArrayName);
728 vtkDataArray* branchIds = centerlines->GetPointData()->GetArray(BranchIdTmpArrayName);
733 std::vector<vtkIdType> newPointIds(numberOfPoints,-1);
734 std::vector<vtkIdType> newCapNeighborIds(numberOfPoints,-1);
735 vtkIdType numberOfNewPoints = 0;
736 for (vtkIdType i=0; i<numberOfPoints; i++)
738 if (pointCells[i].size() != 1)
740 newPointIds[i] = numberOfNewPoints++;
744 newPointIds[i] = numberOfNewPoints++;
745 newCapNeighborIds[i] = numberOfNewPoints++;
749 newCapNeighborIds[i] = numberOfNewPoints++;
750 newPointIds[i] = numberOfNewPoints++;
754 vtkNew<vtkPoints> points;
755 points->SetNumberOfPoints(numberOfNewPoints);
757 vtkNew<vtkDoubleArray> newRadius;
759 newRadius->SetNumberOfValues(numberOfNewPoints);
760 vtkNew<vtkDoubleArray> newTangents;
762 newTangents->SetNumberOfComponents(3);
763 newTangents->SetNumberOfTuples(numberOfNewPoints);
764 vtkNew<vtkIntArray> newBifurcationIds;
765 newBifurcationIds->SetName(BifurcationIdTmpArrayName);
766 newBifurcationIds->SetNumberOfValues(numberOfNewPoints);
767 vtkNew<vtkIntArray> newBranchIds;
768 newBranchIds->SetName(BranchIdTmpArrayName);
769 newBranchIds->SetNumberOfValues(numberOfNewPoints);
770 vtkNew<vtkIntArray> newGlobalNodeId;
772 newGlobalNodeId->SetNumberOfValues(numberOfNewPoints);
773 vtkNew<vtkIntArray> newCenterlineId;
776 newCenterlineId->SetNumberOfTuples(numberOfNewPoints);
778 vtkNew<vtkIdList> cellPointIds;
779 double point[3], neighborPoint[3], tangent[3], neighborTangent[3];
780 for (vtkIdType i=0; i<numberOfPoints; i++)
782 centerlines->GetPoint(i,point);
783 tangents->GetTuple(i,tangent);
785 std::vector<vtkIdType> ids = {newPointIds[i]};
786 if (newCapNeighborIds[i] >= 0)
788 ids.push_back(newCapNeighborIds[i]);
790 for (vtkIdType
id : ids)
792 newBifurcationIds->SetValue(
id,
static_cast<int>(bifurcationIds->GetTuple1(i)));
793 newBranchIds->SetValue(
id,
static_cast<int>(branchIds->GetTuple1(i)));
794 newCenterlineId->SetTuple(
id,i,centerlineId);
795 newGlobalNodeId->SetValue(
id,
static_cast<int>(
id));
798 points->SetPoint(newPointIds[i],point);
799 newRadius->SetValue(newPointIds[i],radius->GetTuple1(i));
800 newTangents->SetTuple(newPointIds[i],tangent);
802 if (newCapNeighborIds[i] < 0)
807 centerlines->GetCellPoints(pointCells[i][0],cellPointIds);
808 vtkIdType neighborId = cellPointIds->GetId(0) == i ? cellPointIds->GetId(1) : cellPointIds->GetId(0);
809 centerlines->GetPoint(neighborId,neighborPoint);
810 tangents->GetTuple(neighborId,neighborTangent);
811 double newPoint[3], newTangent[3];
812 for (
int j=0; j<3; j++)
814 newPoint[j] = 0.5 * (point[j] + neighborPoint[j]);
815 newTangent[j] = 0.5 * (tangent[j] + neighborTangent[j]);
817 vtkMath::Normalize(newTangent);
818 points->SetPoint(newCapNeighborIds[i],newPoint);
819 newTangents->SetTuple(newCapNeighborIds[i],newTangent);
820 newRadius->SetValue(newCapNeighborIds[i],0.5 * (radius->GetTuple1(i) + radius->GetTuple1(neighborId)));
825 vtkNew<vtkCellArray> lines;
826 if (newCapNeighborIds[0] >= 0)
828 vtkIdType line[2] = {newPointIds[0], newCapNeighborIds[0]};
829 lines->InsertNextCell(2,line);
831 for (vtkIdType cellId=0; cellId<centerlines->GetNumberOfCells(); cellId++)
833 centerlines->GetCellPoints(cellId,cellPointIds);
834 vtkIdType pointId0 = cellPointIds->GetId(0);
835 vtkIdType pointId1 = cellPointIds->GetId(1);
836 if (pointId1 == 0 || (pointId0 != 0 && newCapNeighborIds[pointId0] >= 0))
838 vtkErrorMacro(<<
"Centerline lines are not oriented from the inlet");
841 vtkIdType newPointId0 = pointId0 == 0 && newCapNeighborIds[0] >= 0 ? newCapNeighborIds[0] : newPointIds[pointId0];
842 if (newCapNeighborIds[pointId1] >= 0)
844 vtkIdType line0[2] = {newPointId0, newCapNeighborIds[pointId1]};
845 vtkIdType line1[2] = {newCapNeighborIds[pointId1], newPointIds[pointId1]};
846 lines->InsertNextCell(2,line0);
847 lines->InsertNextCell(2,line1);
851 vtkIdType line[2] = {newPointId0, newPointIds[pointId1]};
852 lines->InsertNextCell(2,line);
856 vtkNew<vtkPolyData> refined;
857 refined->SetPoints(points);
858 refined->SetLines(lines);
859 refined->GetPointData()->AddArray(newBifurcationIds);
860 refined->GetPointData()->AddArray(newBranchIds);
861 refined->GetPointData()->AddArray(newRadius);
862 refined->GetPointData()->AddArray(newCenterlineId);
863 refined->GetPointData()->AddArray(newGlobalNodeId);
864 refined->GetPointData()->AddArray(newTangents);
872 vtkDataArray* centerlineLabels = this->
Centerlines->GetPointData()->GetArray(labelArrayName);
873 vtkDataArray* centerlineOtherLabels = this->
Centerlines->GetPointData()->GetArray(otherArrayName);
874 if (!centerlineLabels || !centerlineOtherLabels)
876 vtkErrorMacro(<<
"Centerlines have no point data array named " << (centerlineLabels ? otherArrayName : labelArrayName));
880 const vtkIdType numberOfSurfacePoints = this->
Surface->GetNumberOfPoints();
881 vtkNew<vtkIntArray> surfaceLabels;
882 surfaceLabels->SetName(labelArrayName);
883 surfaceLabels->SetNumberOfValues(numberOfSurfacePoints);
884 surfaceLabels->Fill(-1);
885 std::vector<double> labelDistances(numberOfSurfacePoints,-1.0);
887 vtkNew<vtkPointLocator> locator;
888 BuildPointLocator(this->
Surface,locator);
891 vtkDataArray* surfaceNormals = this->
Surface->GetPointData()->GetNormals();
893 vtkNew<vtkIdList> surfacePointIds;
894 double centerlinePoint[3], surfacePoint[3], normal[3], offset[3];
895 for (vtkIdType i=0; i<this->
Centerlines->GetNumberOfPoints(); i++)
897 if (centerlineOtherLabels->GetTuple1(i) != -1)
901 int label =
static_cast<int>(centerlineLabels->GetTuple1(i));
904 locator->FindPointsWithinRadius(10.0 * radius->GetTuple1(i),centerlinePoint,surfacePointIds);
905 for (vtkIdType j=0; j<surfacePointIds->GetNumberOfIds(); j++)
907 vtkIdType surfacePointId = surfacePointIds->GetId(j);
908 this->
Surface->GetPoint(surfacePointId,surfacePoint);
909 surfaceNormals->GetTuple(surfacePointId,normal);
910 vtkMath::Subtract(surfacePoint,centerlinePoint,offset);
911 double distance = vtkMath::Norm(offset);
914 if (vtkMath::Dot(offset,normal) < -1.0e-2)
918 if (surfaceLabels->GetValue(surfacePointId) > -1 && distance > labelDistances[surfacePointId])
922 surfaceLabels->SetValue(surfacePointId,label);
923 labelDistances[surfacePointId] = distance;
927 this->
Surface->GetPointData()->AddArray(surfaceLabels);
934 const vtkIdType numberOfPoints = centerlines->GetNumberOfPoints();
936 vtkIntArray* bifurcation = vtkIntArray::SafeDownCast(centerlines->GetPointData()->GetArray(this->CenterlineSectionBifurcationArrayName));
937 vtkDataArray* bifurcationIds = centerlines->GetPointData()->GetArray(BifurcationIdTmpArrayName);
939 vtkNew<vtkIntArray> remove;
940 remove->SetName(RemoveArrayName);
941 remove->SetNumberOfValues(numberOfPoints);
943 centerlines->GetPointData()->AddArray(remove);
947 for (vtkIdType i=0; i<numberOfPoints; i++)
949 if (bifurcationIds->GetTuple1(i) == Bifurcation)
951 bifurcation->SetValue(i,Bifurcation);
955 vtkNew<vtkPolyData> bifurcations;
956 vtkNew<vtkPolyData> branches;
962 if (bifurcations->GetNumberOfCells() > 0)
964 vtkNew<vtkConnectivityFilter> connectivity;
965 connectivity->SetInputData(bifurcations);
966 connectivity->SetExtractionModeToAllRegions();
967 connectivity->ColorRegionsOn();
968 connectivity->Update();
970 vtkNew<vtkThreshold> threshold;
971 threshold->SetInputConnection(connectivity->GetOutputPort());
972 threshold->SetInputArrayToProcess(0,0,0,vtkDataObject::FIELD_ASSOCIATION_CELLS,RegionIdArrayName);
973 threshold->SetThresholdFunction(vtkThreshold::THRESHOLD_BETWEEN);
975 vtkNew<vtkIdList> cellPointIds;
976 for (vtkIdType regionId=0; regionId<connectivity->GetNumberOfExtractedRegions(); regionId++)
978 threshold->SetLowerThreshold(regionId);
979 threshold->SetUpperThreshold(regionId);
981 vtkUnstructuredGrid* region = threshold->GetOutput();
982 vtkDataArray* regionBranchIds = region->GetPointData()->GetArray(BranchIdTmpArrayName);
985 std::set<int> branchIdsInRegion;
986 for (vtkIdType j=0; j<region->GetNumberOfPoints(); j++)
988 branchIdsInRegion.insert(
static_cast<int>(regionBranchIds->GetTuple1(j)));
990 if (branchIdsInRegion.size() != 1)
995 std::vector<int> numberOfRegionPointCells(region->GetNumberOfPoints(),0);
996 for (vtkIdType cellId=0; cellId<region->GetNumberOfCells(); cellId++)
998 region->GetCellPoints(cellId,cellPointIds);
999 for (vtkIdType k=0; k<cellPointIds->GetNumberOfIds(); k++)
1001 numberOfRegionPointCells[cellPointIds->GetId(k)]++;
1004 for (vtkIdType j=0; j<region->GetNumberOfPoints(); j++)
1006 vtkIdType pointId =
static_cast<vtkIdType
>(regionGlobalNodeIds->GetTuple1(j));
1007 bifurcation->SetValue(pointId,Branch);
1008 if (numberOfRegionPointCells[j] != 1)
1010 remove->SetValue(pointId,1);
1018 std::vector<std::vector<vtkIdType>> pointCells = GetPointCells(centerlines);
1019 vtkNew<vtkIdList> cellPointIds;
1020 for (vtkIdType i=0; i<numberOfPoints; i++)
1022 if (bifurcation->GetValue(i) != Branch)
1026 bool isolated =
true;
1027 for (vtkIdType cellId : pointCells[i])
1029 centerlines->GetCellPoints(cellId,cellPointIds);
1030 for (vtkIdType k=0; k<cellPointIds->GetNumberOfIds(); k++)
1032 vtkIdType
id = cellPointIds->GetId(k);
1033 if (
id != i && bifurcation->GetValue(
id) == Branch)
1041 bifurcation->SetValue(i,Bifurcation);
1046 bifurcation->SetValue(0,Branch);
1047 bifurcation->SetValue(1,Branch);
1048 for (vtkIdType i=1; i<numberOfPoints; i++)
1050 if (pointCells[i].size() == 1)
1052 bifurcation->SetValue(i-1,Branch);
1053 bifurcation->SetValue(i,Branch);
1058 vtkNew<vtkPointDataToCellData> pointToCellData;
1059 pointToCellData->SetInputData(centerlines);
1060 pointToCellData->PassPointDataOn();
1062 vtkNew<vtkThreshold> threshold;
1063 threshold->SetInputConnection(pointToCellData->GetOutputPort());
1064 threshold->SetInputArrayToProcess(0,0,0,vtkDataObject::FIELD_ASSOCIATION_CELLS,RemoveArrayName);
1065 threshold->SetThresholdFunction(vtkThreshold::THRESHOLD_BETWEEN);
1066 threshold->SetLowerThreshold(0.0);
1067 threshold->SetUpperThreshold(0.0);
1069 threshold->Update();
1070 vtkNew<vtkPolyData> kept;
1071 LinesToPolyData(threshold->GetOutput(),kept);
1076 vtkNew<vtkCellArray> lines;
1077 lines->DeepCopy(kept->GetLines());
1078 vtkNew<vtkIntArray> globalNodeIds;
1080 globalNodeIds->SetNumberOfValues(kept->GetNumberOfPoints());
1081 if (kept->GetNumberOfPoints() > 0)
1083 globalNodeIds->SetValue(0,0);
1085 for (vtkIdType i=1; i<kept->GetNumberOfPoints(); i++)
1087 if (oldGlobalNodeIds->GetTuple1(i-1) + 1 != oldGlobalNodeIds->GetTuple1(i))
1089 vtkIdType line[2] = {i-1, i};
1090 lines->InsertNextCell(2,line);
1092 globalNodeIds->SetValue(i,
static_cast<int>(i));
1095 vtkNew<vtkPolyData> cleaned;
1096 cleaned->SetPoints(kept->GetPoints());
1097 cleaned->SetLines(lines);
1098 cleaned->GetPointData()->ShallowCopy(kept->GetPointData());
1099 cleaned->GetPointData()->RemoveArray(RemoveArrayName);
1100 cleaned->GetPointData()->AddArray(globalNodeIds);
1102 if (cleaned->GetNumberOfPoints() != cleaned->GetNumberOfCells() + 1)
1104 vtkErrorMacro(<<
"Centerlines do not form a tree after removing bifurcation regions within branches");
1116 vtkNew<vtkIntArray> cellBifurcation;
1118 cellBifurcation->SetNumberOfValues(this->
Centerlines->GetNumberOfCells());
1119 vtkNew<vtkIdList> cellPointIds;
1120 for (vtkIdType cellId=0; cellId<this->
Centerlines->GetNumberOfCells(); cellId++)
1122 this->
Centerlines->GetCellPoints(cellId,cellPointIds);
1124 for (vtkIdType k=0; k<cellPointIds->GetNumberOfIds(); k++)
1126 if (pointBifurcation->GetTuple1(cellPointIds->GetId(k)) == Bifurcation)
1128 value = Bifurcation;
1131 cellBifurcation->SetValue(cellId,value);
1133 this->
Centerlines->GetCellData()->AddArray(cellBifurcation);
1135 vtkNew<vtkThreshold> threshold;
1137 threshold->SetThresholdFunction(vtkThreshold::THRESHOLD_BETWEEN);
1140 for (
int value : {Branch, Bifurcation})
1142 threshold->SetLowerThreshold(value);
1143 threshold->SetUpperThreshold(value);
1144 threshold->Update();
1145 LinesToPolyData(threshold->GetOutput(),value == Branch ? branches : bifurcations);
1151 vtkNew<vtkConnectivityFilter> connectivity;
1152 connectivity->SetInputData(lines);
1153 connectivity->SetExtractionModeToAllRegions();
1154 connectivity->ColorRegionsOn();
1155 connectivity->Update();
1157 vtkNew<vtkPolyData> labeled;
1158 labeled->DeepCopy(connectivity->GetOutput());
1159 vtkDataArray* regionIds = labeled->GetPointData()->GetArray(RegionIdArrayName);
1161 const vtkIdType numberOfPoints = labeled->GetNumberOfPoints();
1165 vtkNew<vtkDoubleArray> path;
1167 path->SetNumberOfValues(numberOfPoints);
1170 std::vector<std::vector<std::pair<double,vtkIdType>>> regionPoints(connectivity->GetNumberOfExtractedRegions());
1171 for (vtkIdType i=0; i<numberOfPoints; i++)
1173 regionPoints[
static_cast<size_t>(regionIds->GetTuple1(i))].push_back({globalNodeIds->GetTuple1(i), i});
1175 double point[3], previousPoint[3];
1176 for (std::vector<std::pair<double,vtkIdType>>& points : regionPoints)
1178 std::sort(points.begin(),points.end());
1179 double distance = 0.0;
1180 for (
size_t k=0; k<points.size(); k++)
1182 labeled->GetPoint(points[k].second,point);
1185 distance += sqrt(vtkMath::Distance2BetweenPoints(point,previousPoint));
1187 path->SetValue(points[k].second,distance);
1188 previousPoint[0] = point[0];
1189 previousPoint[1] = point[1];
1190 previousPoint[2] = point[2];
1193 labeled->GetPointData()->AddArray(path);
1195 RenameRegionIds(labeled->GetPointData(),labelArrayName);
1196 RenameRegionIds(labeled->GetCellData(),labelArrayName);
1198 vtkNew<vtkIdTypeArray> otherPointIds;
1199 otherPointIds->SetName(otherArrayName);
1200 otherPointIds->SetNumberOfValues(numberOfPoints);
1201 otherPointIds->Fill(-1);
1202 labeled->GetPointData()->AddArray(otherPointIds);
1203 vtkNew<vtkIdTypeArray> otherCellIds;
1204 otherCellIds->SetName(otherArrayName);
1205 otherCellIds->SetNumberOfValues(labeled->GetNumberOfCells());
1206 otherCellIds->Fill(-1);
1207 labeled->GetCellData()->AddArray(otherCellIds);
1209 lines->DeepCopy(labeled);
1214 vtkNew<vtkPolyData> bifurcations;
1215 vtkNew<vtkPolyData> branches;
1218 vtkNew<vtkAppendFilter> append;
1219 append->MergePointsOn();
1220 if (bifurcations->GetNumberOfCells() > 0)
1223 append->AddInputData(bifurcations);
1225 if (branches->GetNumberOfCells() > 0)
1228 append->AddInputData(branches);
1230 if (append->GetNumberOfInputConnections(0) == 0)
1232 vtkErrorMacro(<<
"Centerlines have no lines left");
1237 vtkNew<vtkPolyData> merged;
1238 LinesToPolyData(append->GetOutput(),merged);
1240 if (!IsOnePiece(merged))
1242 vtkErrorMacro(<<
"Centerlines consist of more than one piece after grouping");
1247 const vtkIdType numberOfPoints = merged->GetNumberOfPoints();
1249 std::vector<vtkIdType> mergedPointIds(numberOfPoints,-1);
1250 for (vtkIdType i=0; i<numberOfPoints; i++)
1252 vtkIdType
id =
static_cast<vtkIdType
>(globalNodeIds->GetTuple1(i));
1253 if (id < 0 || id >= numberOfPoints || mergedPointIds[
id] >= 0)
1255 vtkErrorMacro(<<
"Centerline point ids are not unique after grouping");
1258 mergedPointIds[id] = i;
1261 vtkNew<vtkPoints> points;
1262 points->SetNumberOfPoints(numberOfPoints);
1263 vtkNew<vtkPolyData> ordered;
1264 ordered->GetPointData()->CopyAllocate(merged->GetPointData(),numberOfPoints);
1265 for (vtkIdType
id=0;
id<numberOfPoints;
id++)
1267 points->SetPoint(
id,merged->GetPoint(mergedPointIds[
id]));
1268 ordered->GetPointData()->CopyData(merged->GetPointData(),mergedPointIds[
id],
id);
1271 vtkNew<vtkCellArray> lines;
1272 vtkNew<vtkIdList> cellPointIds;
1273 for (vtkIdType cellId=0; cellId<merged->GetNumberOfCells(); cellId++)
1275 merged->GetCellPoints(cellId,cellPointIds);
1276 lines->InsertNextCell(cellPointIds->GetNumberOfIds());
1277 for (vtkIdType k=0; k<cellPointIds->GetNumberOfIds(); k++)
1279 lines->InsertCellPoint(
static_cast<vtkIdType
>(globalNodeIds->GetTuple1(cellPointIds->GetId(k))));
1283 ordered->SetPoints(points);
1284 ordered->SetLines(lines);