23#include "vtkPolyData.h"
24#include "vtkPolyLine.h"
25#include "vtkPolygon.h"
26#include "vtkTriangle.h"
27#include "vtkPointData.h"
28#include "vtkCellData.h"
29#include "vtkCellArray.h"
30#include "vtkDoubleArray.h"
31#include "vtkIntArray.h"
35#include "vtkStripper.h"
36#include "vtkPolyDataConnectivityFilter.h"
38#include "vtkCleanPolyData.h"
39#include "vtkAppendPolyData.h"
42#include "vtkInformation.h"
43#include "vtkInformationVector.h"
44#include "vtkObjectFactory.h"
45#include "vtkVersion.h"
47#include "vtkGenericCell.h"
49#include "vtkIdTypeArray.h"
50#include "vtkSmartPointer.h"
174 vtkInformation *vtkNotUsed(request),
175 vtkInformationVector **inputVector,
176 vtkInformationVector *outputVector)
178 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
179 vtkInformation *outInfo = outputVector->GetInformationObject(0);
181 vtkPolyData *input = vtkPolyData::SafeDownCast(
182 inInfo->Get(vtkDataObject::DATA_OBJECT()));
183 vtkPolyData *output = vtkPolyData::SafeDownCast(
184 outInfo->Get(vtkDataObject::DATA_OBJECT()));
188 vtkErrorMacro(<<
"GroupIdsArrayName not specified");
192 vtkDataArray* groupIdsArray = input->GetPointData()->GetArray(this->
GroupIdsArrayName);
196 vtkErrorMacro(<<
"GroupIdsArray with name specified does not exist");
202 vtkErrorMacro(<<
"Centerlines not set");
208 vtkErrorMacro(<<
"CenterlineRadiusArrayName not specified");
214 if (!centerlineRadiusArray)
216 vtkErrorMacro(<<
"CenterlineRadiusArray with name specified does not exist");
222 vtkErrorMacro(<<
"CenterlineGroupIdsArrayName not specified");
228 if (!centerlineGroupIdsArray)
230 vtkErrorMacro(<<
"CenterlineGroupIdsArray with name specified does not exist");
236 vtkErrorMacro(<<
"CenterlineIdsArrayName not specified");
242 if (!centerlineIdsArray)
244 vtkErrorMacro(<<
"CenterlineIdsArray with name specified does not exist");
250 vtkErrorMacro(<<
"CenterlineTractIdsArrayName not specified");
256 if (!centerlineTractIdsArray)
258 vtkErrorMacro(<<
"CenterlineTractIdsArray with name specified does not exist");
264 vtkErrorMacro(<<
"BlankingArrayName not specified");
272 vtkErrorMacro(<<
"BlankingArray with name specified does not exist");
278 vtkErrorMacro(<<
"BranchSectionGroupIdsArrayName not specified");
284 vtkErrorMacro(<<
"BranchSectionAreaArrayName not specified");
290 vtkErrorMacro(<<
"BranchSectionMinSizeArrayName not specified");
296 vtkErrorMacro(<<
"BranchSectionMaxSizeArrayName not specified");
302 vtkErrorMacro(<<
"BranchSectionShapeArrayName not specified");
308 vtkErrorMacro(<<
"BranchSectionClosedArrayName not specified");
314 vtkErrorMacro(<<
"BranchSectionDistanceSpheresArrayName not specified");
318 vtkPoints* outputPoints = vtkPoints::New();
319 vtkCellArray* outputPolys = vtkCellArray::New();
321 output->SetPoints(outputPoints);
322 output->SetPolys(outputPolys);
324 vtkIntArray* branchSectionGroupIdsArray = vtkIntArray::New();
327 vtkDoubleArray* branchSectionAreaArray = vtkDoubleArray::New();
330 vtkDoubleArray* branchSectionShapeArray = vtkDoubleArray::New();
333 vtkDoubleArray* branchSectionMinSizeArray = vtkDoubleArray::New();
336 vtkDoubleArray* branchSectionMaxSizeArray = vtkDoubleArray::New();
339 vtkIntArray* branchSectionClosedArray = vtkIntArray::New();
342 vtkIntArray* branchSectionDistanceSpheresArray = vtkIntArray::New();
345 output->GetCellData()->AddArray(branchSectionGroupIdsArray);
346 output->GetCellData()->AddArray(branchSectionAreaArray);
347 output->GetCellData()->AddArray(branchSectionMinSizeArray);
348 output->GetCellData()->AddArray(branchSectionMaxSizeArray);
349 output->GetCellData()->AddArray(branchSectionShapeArray);
350 output->GetCellData()->AddArray(branchSectionClosedArray);
351 output->GetCellData()->AddArray(branchSectionDistanceSpheresArray);
353 vtkIdList* nonBlankedGroupIds = vtkIdList::New();
356 for (i=0; i<nonBlankedGroupIds->GetNumberOfIds(); i++)
358 vtkIdType groupId = nonBlankedGroupIds->GetId(i);
363 nonBlankedGroupIds->Delete();
365 outputPoints->Delete();
366 outputPolys->Delete();
368 branchSectionGroupIdsArray->Delete();
369 branchSectionAreaArray->Delete();
370 branchSectionMinSizeArray->Delete();
371 branchSectionMaxSizeArray->Delete();
372 branchSectionShapeArray->Delete();
373 branchSectionClosedArray->Delete();
374 branchSectionDistanceSpheresArray->Delete();
381 vtkPoints* branchSectionPoints = output->GetPoints();
382 vtkCellArray* branchSectionPolys = output->GetPolys();
384 vtkIntArray* branchSectionGroupIdsArray = vtkIntArray::SafeDownCast(output->GetCellData()->GetArray(this->BranchSectionGroupIdsArrayName));
385 vtkDoubleArray* branchSectionAreaArray = vtkDoubleArray::SafeDownCast(output->GetCellData()->GetArray(this->BranchSectionAreaArrayName));
386 vtkDoubleArray* branchSectionMinSizeArray = vtkDoubleArray::SafeDownCast(output->GetCellData()->GetArray(this->BranchSectionMinSizeArrayName));
387 vtkDoubleArray* branchSectionMaxSizeArray = vtkDoubleArray::SafeDownCast(output->GetCellData()->GetArray(this->BranchSectionMaxSizeArrayName));
388 vtkDoubleArray* branchSectionShapeArray = vtkDoubleArray::SafeDownCast(output->GetCellData()->GetArray(this->BranchSectionShapeArrayName));
389 vtkIntArray* branchSectionClosedArray = vtkIntArray::SafeDownCast(output->GetCellData()->GetArray(this->BranchSectionClosedArrayName));
390 vtkIntArray* branchSectionDistanceSpheresArray = vtkIntArray::SafeDownCast(output->GetCellData()->GetArray(this->BranchSectionDistanceSpheresArrayName));
396 double averagePoint[3];
397 averagePoint[0] = averagePoint[1] = averagePoint[2] = 0.0;
399 double averageTangent[3];
400 averageTangent[0] = averageTangent[1] = averageTangent[2] = 0.0;
402 double weightSum = 0.0;
405 bool anyPoint =
false;
407 vtkIdList* groupCellIds = vtkIdList::New();
409 for (j=0; j<groupCellIds->GetNumberOfIds(); j++)
411 vtkIdType centerlineCellId = groupCellIds->GetId(j);
412 vtkPoints* centerlineCellPoints = this->
Centerlines->GetCell(centerlineCellId)->GetPoints();
414 vtkIdType firstSubId = 0;
415 double firstPCoord = 0.0;
416 bool reverseTouchingDirection =
false;
419 firstSubId = centerlineCellPoints->GetNumberOfPoints()-2;
421 reverseTouchingDirection =
true;
424 vtkIdType touchingSubId = -1;
425 double touchingPCoord = 0.0;
428 if (touchingSubId == -1)
434 double touchingPoint[3];
437 double touchingPoint0[3], touchingPoint1[3];
438 centerlineCellPoints->GetPoint(touchingSubId,touchingPoint0);
439 centerlineCellPoints->GetPoint(touchingSubId+1,touchingPoint1);
441 double touchingPointTangent[3];
442 touchingPointTangent[0] = touchingPoint1[0] - touchingPoint0[0];
443 touchingPointTangent[1] = touchingPoint1[1] - touchingPoint0[1];
444 touchingPointTangent[2] = touchingPoint1[2] - touchingPoint0[2];
446 vtkMath::Normalize(touchingPointTangent);
447 double touchingPointRadius = 0.0;
450 averagePoint[0] += touchingPointRadius * touchingPointRadius * touchingPoint[0];
451 averagePoint[1] += touchingPointRadius * touchingPointRadius * touchingPoint[1];
452 averagePoint[2] += touchingPointRadius * touchingPointRadius * touchingPoint[2];
454 averageTangent[0] += touchingPointRadius * touchingPointRadius * touchingPointTangent[0];
455 averageTangent[1] += touchingPointRadius * touchingPointRadius * touchingPointTangent[1];
456 averageTangent[2] += touchingPointRadius * touchingPointRadius * touchingPointTangent[2];
458 weightSum += touchingPointRadius * touchingPointRadius;
466 averagePoint[0] /= weightSum;
467 averagePoint[1] /= weightSum;
468 averagePoint[2] /= weightSum;
470 averageTangent[0] /= weightSum;
471 averageTangent[1] /= weightSum;
472 averageTangent[2] /= weightSum;
474 vtkMath::Normalize(averageTangent);
478 vtkPolyData* cylinder = vtkPolyData::New();
481 vtkPolyData* section = vtkPolyData::New();
485 section->BuildCells();
486 if (section->GetNumberOfCells() == 0)
488 groupCellIds->Delete();
493 vtkPoints* sectionCellPoints = section->GetCell(0)->GetPoints();
494 int numberOfSectionCellPoints = sectionCellPoints->GetNumberOfPoints();
495 branchSectionPolys->InsertNextCell(numberOfSectionCellPoints);
497 for (k=0; k<numberOfSectionCellPoints; k++)
499 vtkIdType branchPointId = branchSectionPoints->InsertNextPoint(sectionCellPoints->GetPoint(k));
500 branchSectionPolys->InsertCellPoint(branchPointId);
507 branchSectionGroupIdsArray->InsertNextValue(groupId);
508 branchSectionAreaArray->InsertNextValue(area);
509 branchSectionMinSizeArray->InsertNextValue(sizeRange[0]);
510 branchSectionMaxSizeArray->InsertNextValue(sizeRange[1]);
511 branchSectionShapeArray->InsertNextValue(shape);
512 branchSectionClosedArray->InsertNextValue(closed);
513 branchSectionDistanceSpheresArray->InsertNextValue(totalNumberOfSpheres);
515 groupCellIds->Delete();
523const char* SourceCellIdsArrayName =
"vtkvmtkPolyDataBranchSectionsSourceCellIds";
532void CopyIdsFromNearestEdgePoint(vtkPolyData* surface, vtkDataArray* surfaceIdsArray, vtkPolyData* section)
534 vtkIdTypeArray* sourceCellIds = vtkIdTypeArray::SafeDownCast(section->GetCellData()->GetArray(SourceCellIdsArrayName));
536 const vtkIdType numberOfSectionPoints = section->GetNumberOfPoints();
537 vtkSmartPointer<vtkDataArray> sectionIdsArray = vtkSmartPointer<vtkDataArray>::Take(surfaceIdsArray->NewInstance());
538 sectionIdsArray->SetName(surfaceIdsArray->GetName());
539 sectionIdsArray->SetNumberOfComponents(surfaceIdsArray->GetNumberOfComponents());
540 sectionIdsArray->SetNumberOfTuples(numberOfSectionPoints);
541 sectionIdsArray->Fill(0.0);
543 vtkNew<vtkGenericCell> sourceCell;
544 vtkNew<vtkIdList> pointCellIds;
545 double point[3], edgePoint0[3], edgePoint1[3], closestPoint[3];
546 for (vtkIdType i=0; sourceCellIds != NULL && i<numberOfSectionPoints; i++)
548 section->GetPointCells(i,pointCellIds);
549 if (pointCellIds->GetNumberOfIds() == 0)
553 section->GetPoint(i,point);
554 surface->GetCell(sourceCellIds->GetValue(pointCellIds->GetId(0)),sourceCell);
555 double minDistance2 = VTK_DOUBLE_MAX;
556 vtkIdType nearestSurfacePointId = -1;
557 for (
int e=0; e<sourceCell->GetNumberOfEdges(); e++)
559 vtkCell* edge = sourceCell->GetEdge(e);
560 vtkIdType edgePointId0 = edge->GetPointId(0);
561 vtkIdType edgePointId1 = edge->GetPointId(1);
562 surface->GetPoint(edgePointId0,edgePoint0);
563 surface->GetPoint(edgePointId1,edgePoint1);
565 double distance2 = vtkLine::DistanceToLine(point,edgePoint0,edgePoint1,t,closestPoint);
566 if (distance2 < minDistance2)
568 minDistance2 = distance2;
569 nearestSurfacePointId = vtkMath::Distance2BetweenPoints(point,edgePoint0) <= vtkMath::Distance2BetweenPoints(point,edgePoint1) ? edgePointId0 : edgePointId1;
572 if (nearestSurfacePointId >= 0)
574 sectionIdsArray->SetTuple(i,nearestSurfacePointId,surfaceIdsArray);
578 section->GetPointData()->Initialize();
579 section->GetPointData()->AddArray(sectionIdsArray);
587 section->Initialize();
590 vtkNew<vtkPlane> plane;
591 plane->SetOrigin(origin);
592 plane->SetNormal(normal);
594 vtkDataArray* surfaceIdsArray = idsArrayName != NULL ? cylinder->GetPointData()->GetArray(idsArrayName) : NULL;
596 vtkNew<vtkCutter> cutter;
597 vtkNew<vtkPolyData> cutInput;
598 if (surfaceIdsArray != NULL)
603 cutInput->CopyStructure(cylinder);
604 vtkNew<vtkIdTypeArray> sourceCellIds;
605 sourceCellIds->SetName(SourceCellIdsArrayName);
606 sourceCellIds->SetNumberOfValues(cutInput->GetNumberOfCells());
607 for (vtkIdType i=0; i<cutInput->GetNumberOfCells(); i++)
609 sourceCellIds->SetValue(i,i);
611 cutInput->GetCellData()->AddArray(sourceCellIds);
612 cutter->SetInputData(cutInput);
616 cutter->SetInputData(cylinder);
618 cutter->SetCutFunction(plane);
621 cutter->GenerateCutScalarsOn();
622 cutter->SetValue(0,0.0);
625 vtkNew<vtkCleanPolyData> cleaner;
626 if (surfaceIdsArray != NULL)
628 cleaner->SetInputConnection(cutter->GetOutputPort());
634 vtkNew<vtkPolyData> cutGeometry;
635 cutGeometry->CopyStructure(cutter->GetOutput());
636 cleaner->SetInputData(cutGeometry);
640 if (cleaner->GetOutput()->GetNumberOfPoints() == 0)
645 vtkNew<vtkPolyDataConnectivityFilter> connectivityFilter;
646 connectivityFilter->SetInputConnection(cleaner->GetOutputPort());
647 connectivityFilter->SetExtractionModeToClosestPointRegion();
648 connectivityFilter->SetClosestPoint(origin);
649 connectivityFilter->Update();
651 section->DeepCopy(connectivityFilter->GetOutput());
655 if (section->GetNumberOfCells() == 0)
660 section->BuildCells();
661 section->BuildLinks();
663 if (surfaceIdsArray != NULL)
665 CopyIdsFromNearestEdgePoint(cylinder,surfaceIdsArray,section);
670 int numberOfLinePoints = section->GetNumberOfPoints();
674 const vtkIdType *pts;
677 int numberOfSingleCellPoints = 0;
678 vtkIdType firstPointId = -1;
680 for (
int i=0; i<numberOfLinePoints; i++)
682 section->GetPointCells(i,ncells,cells);
685 ++numberOfSingleCellPoints;
690 if (numberOfSingleCellPoints == 0)
692 firstPointId = section->GetCell(0)->GetPointId(0);
695 vtkNew<vtkIdList> polygonPointIds;
696 polygonPointIds->InsertNextId(firstPointId);
699 vtkIdType pointId = firstPointId;
701 vtkIdType cellId = -1;
704 section->GetPointCells(pointId,ncells,cells);
707 if (pointId == firstPointId)
717 else if (ncells == 2)
719 if (cells[0] == cellId)
729 section->GetCellPoints(cellId,npts,pts);
731 if (pts[0] == pointId)
740 if (pointId == firstPointId)
747 polygonPointIds->InsertNextId(pointId);
758 vtkNew<vtkCellArray> sectionLines;
759 section->SetLines(sectionLines);
761 vtkNew<vtkCellArray> sectionPolys;
762 sectionPolys->InsertNextCell(polygonPointIds);
763 section->SetPolys(sectionPolys);
766 section->GetCellData()->Initialize();
768 section->DeleteCells();
769 section->DeleteLinks();
774 branchSection->BuildCells();
776 if (branchSection->GetNumberOfCells() == 0)
781 vtkPolygon* sectionPolygon = vtkPolygon::SafeDownCast(branchSection->GetCell(0));
783 vtkIdList* trianglePointIds = vtkIdList::New();
785 sectionPolygon->EarCutTriangulation(trianglePointIds);
787 int numberOfTriangles = trianglePointIds->GetNumberOfIds() / 3;
789 double polygonArea = 0.0;
791 for (
int i=0; i<numberOfTriangles; i++)
793 vtkIdType pointId0 = trianglePointIds->GetId(3*i);
794 vtkIdType pointId1 = trianglePointIds->GetId(3*i+1);
795 vtkIdType pointId2 = trianglePointIds->GetId(3*i+2);
797 double point0[3], point1[3], point2[3];
799 sectionPolygon->GetPoints()->GetPoint(pointId0,point0);
800 sectionPolygon->GetPoints()->GetPoint(pointId1,point1);
801 sectionPolygon->GetPoints()->GetPoint(pointId2,point2);
803 double triangleArea = vtkTriangle::TriangleArea(point0,point1,point2);
805 polygonArea += triangleArea;
808 trianglePointIds->Delete();
813#ifdef VMTK_ONE_SIDED_SECTION_SHAPE
816 branchSection->BuildCells();
818 if (branchSection->GetNumberOfCells() == 0)
820 sizeRange[0] = sizeRange[1] = 0.0;
824 vtkPolygon* sectionPolygon = vtkPolygon::SafeDownCast(branchSection->GetCell(0));
826 int numberOfSectionPolygonPoints = sectionPolygon->GetNumberOfPoints();
829 double maxDistance = 0.0;
831 for (
int i=0; i<numberOfSectionPolygonPoints; i++)
834 sectionPolygon->GetPoints()->GetPoint(i,point);
835 double distance = sqrt(vtkMath::Distance2BetweenPoints(point,center));
837 if (distance > maxDistance)
839 maxDistance = distance;
842 if (distance < minDistance)
844 minDistance = distance;
848 sizeRange[0] = minDistance;
849 sizeRange[1] = maxDistance;
851 double sectionShape = minDistance / maxDistance;
858 branchSection->BuildCells();
860 if (branchSection->GetNumberOfCells() == 0)
862 sizeRange[0] = sizeRange[1] = 0.0;
866 vtkPolygon* sectionPolygon = vtkPolygon::SafeDownCast(branchSection->GetCell(0));
868 int numberOfSectionPolygonPoints = sectionPolygon->GetNumberOfPoints();
871 double maxDistance = 0.0;
873 vtkIdType minDistanceId = -1;
874 vtkIdType maxDistanceId = -1;
877 for (
int i=0; i<numberOfSectionPolygonPoints; i++)
879 sectionPolygon->GetPoints()->GetPoint(i,point);
880 double distance = sqrt(vtkMath::Distance2BetweenPoints(point,center));
882 if (distance > maxDistance)
884 maxDistance = distance;
888 if (distance < minDistance)
890 minDistance = distance;
895 if (minDistance == -1 || maxDistance == -1)
905 double planeNormal[3];
906 double radialVector0[3];
907 double radialVector1[3];
910 planeNormal[0] = 0.0;
911 planeNormal[1] = 0.0;
912 planeNormal[2] = 0.0;
914 for (
int i=0; i<numberOfSectionPolygonPoints; i++)
916 sectionPolygon->GetPoints()->GetPoint(i,point0);
917 sectionPolygon->GetPoints()->GetPoint((i+numberOfSectionPolygonPoints/4)%numberOfSectionPolygonPoints,point1);
919 radialVector0[0] = point0[0] - center[0];
920 radialVector0[1] = point0[1] - center[1];
921 radialVector0[2] = point0[2] - center[2];
923 radialVector1[0] = point1[0] - center[0];
924 radialVector1[1] = point1[1] - center[1];
925 radialVector1[2] = point1[2] - center[2];
927 vtkMath::Cross(point0,point1,cross);
929 planeNormal[0] += cross[0];
930 planeNormal[1] += cross[1];
931 planeNormal[2] += cross[2];
934 vtkMath::Normalize(planeNormal);
936 double minDistancePoint[3];
937 sectionPolygon->GetPoints()->GetPoint(minDistanceId,minDistancePoint);
939 double maxDistancePoint[3];
940 sectionPolygon->GetPoints()->GetPoint(maxDistanceId,maxDistancePoint);
942 double minDistanceNormal[3];
943 double maxDistanceNormal[3];
945 minDistanceNormal[0] = minDistancePoint[0] - center[0];
946 minDistanceNormal[1] = minDistancePoint[1] - center[1];
947 minDistanceNormal[2] = minDistancePoint[2] - center[2];
949 vtkMath::Normalize(minDistanceNormal);
951 maxDistanceNormal[0] = maxDistancePoint[0] - center[0];
952 maxDistanceNormal[1] = maxDistancePoint[1] - center[1];
953 maxDistanceNormal[2] = maxDistancePoint[2] - center[2];
955 vtkMath::Normalize(maxDistanceNormal);
957 double minDistanceOppositePoint[3];
958 double maxDistanceOppositePoint[3];
960 minDistanceOppositePoint[0] = center[0] - 2.0 * maxDistance * minDistanceNormal[0];
961 minDistanceOppositePoint[1] = center[1] - 2.0 * maxDistance * minDistanceNormal[1];
962 minDistanceOppositePoint[2] = center[2] - 2.0 * maxDistance * minDistanceNormal[2];
964 maxDistanceOppositePoint[0] = center[0] - 2.0 * maxDistance * maxDistanceNormal[0];
965 maxDistanceOppositePoint[1] = center[1] - 2.0 * maxDistance * maxDistanceNormal[1];
966 maxDistanceOppositePoint[2] = center[2] - 2.0 * maxDistance * maxDistanceNormal[2];
968 double intersectionPoint[3];
969 double maxIntersectionDistance = 0.0;
973 for (
int i=0; i<numberOfSectionPolygonPoints; i++)
975 sectionPolygon->GetPoints()->GetPoint(i,point0);
976 sectionPolygon->GetPoints()->GetPoint((i+1)%numberOfSectionPolygonPoints,point1);
978 intersection = vtkLine::Intersection(minDistanceOppositePoint,center,point0,point1,u,v);
980 if (intersection == 0)
985 intersectionPoint[0] = (1.0 - u) * minDistanceOppositePoint[0] + u * center[0];
986 intersectionPoint[1] = (1.0 - u) * minDistanceOppositePoint[1] + u * center[1];
987 intersectionPoint[2] = (1.0 - u) * minDistanceOppositePoint[2] + u * center[2];
989 double intersectionDistance = sqrt(vtkMath::Distance2BetweenPoints(intersectionPoint,center));
991 if (intersectionDistance > maxIntersectionDistance)
993 maxIntersectionDistance = intersectionDistance;
997 minDistance += maxIntersectionDistance;
999 maxIntersectionDistance = 0.0;
1001 for (
int i=0; i<numberOfSectionPolygonPoints; i++)
1003 sectionPolygon->GetPoints()->GetPoint(i,point0);
1004 sectionPolygon->GetPoints()->GetPoint((i+1)%numberOfSectionPolygonPoints,point1);
1006 intersection = vtkLine::Intersection(maxDistanceOppositePoint,center,point0,point1,u,v);
1008 if (intersection == 0)
1013 intersectionPoint[0] = (1.0 - u) * maxDistanceOppositePoint[0] + u * center[0];
1014 intersectionPoint[1] = (1.0 - u) * maxDistanceOppositePoint[1] + u * center[1];
1015 intersectionPoint[2] = (1.0 - u) * maxDistanceOppositePoint[2] + u * center[2];
1017 double intersectionDistance = sqrt(vtkMath::Distance2BetweenPoints(intersectionPoint,center));
1019 if (intersectionDistance > maxIntersectionDistance)
1021 maxIntersectionDistance = intersectionDistance;
1025 maxDistance += maxIntersectionDistance;
1027 sizeRange[0] = minDistance;
1028 sizeRange[1] = maxDistance;
1030 double sectionShape = minDistance / maxDistance;
1032 return sectionShape;
1038 section->BuildCells();
1040 if (section->GetNumberOfCells() == 0)
1054 std::set<std::array<double,3>> crossingPoints;
1055 if (centerlines->GetPoints())
1057 std::vector<double> signedDistances(centerlines->GetNumberOfPoints());
1059 for (vtkIdType i=0; i<centerlines->GetNumberOfPoints(); i++)
1061 centerlines->GetPoint(i,point);
1062 signedDistances[i] =
static_cast<float>(normal[0] * (point[0] - origin[0]) + normal[1] * (point[1] - origin[1]) + normal[2] * (point[2] - origin[2]));
1064 const bool floatPoints = centerlines->GetPoints()->GetDataType() == VTK_FLOAT;
1065 vtkNew<vtkIdList> cellPointIds;
1066 double point0[3], point1[3];
1067 for (vtkIdType cellId=0; cellId<centerlines->GetNumberOfCells(); cellId++)
1069 int cellType = centerlines->GetCellType(cellId);
1070 if (cellType != VTK_LINE && cellType != VTK_POLY_LINE)
1074 centerlines->GetCellPoints(cellId,cellPointIds);
1075 for (vtkIdType k=0; k+1<cellPointIds->GetNumberOfIds(); k++)
1077 vtkIdType pointId0 = cellPointIds->GetId(k);
1078 vtkIdType pointId1 = cellPointIds->GetId(k+1);
1079 double distance0 = signedDistances[pointId0];
1080 double distance1 = signedDistances[pointId1];
1081 if ((distance0 >= 0.0) == (distance1 >= 0.0))
1086 if (distance0 >= 0.0)
1088 std::swap(pointId0,pointId1);
1089 std::swap(distance0,distance1);
1091 centerlines->GetPoint(pointId0,point0);
1092 centerlines->GetPoint(pointId1,point1);
1093 const double t = (0.0 - distance0) / (distance1 - distance0);
1094 std::array<double,3> crossing;
1095 for (
int j=0; j<3; j++)
1097 crossing[j] = point0[j] + t * (point1[j] - point0[j]);
1100 crossing[j] =
static_cast<float>(crossing[j]);
1103 crossingPoints.insert(crossing);
1107 if (crossingPoints.empty())
1118 double planeNormal[3] = {normal[0], normal[1], normal[2]};
1119 vtkMath::Normalize(planeNormal);
1120 double axis0[3], axis1[3];
1121 vtkMath::Perpendiculars(planeNormal,axis0,axis1,0.0);
1123 vtkPoints* sectionPoints = section->GetCell(0)->GetPoints();
1124 const int numberOfPolygonPoints =
static_cast<int>(sectionPoints->GetNumberOfPoints());
1125 if (numberOfPolygonPoints < 3)
1129 std::vector<double> polygon(3*numberOfPolygonPoints);
1130 double polygonBounds[6] = {VTK_DOUBLE_MAX, VTK_DOUBLE_MIN, VTK_DOUBLE_MAX, VTK_DOUBLE_MIN, 0.0, 0.0};
1131 double point[3], planePoint[3];
1132 for (
int i=0; i<numberOfPolygonPoints; i++)
1134 sectionPoints->GetPoint(i,point);
1135 vtkMath::Subtract(point,origin,point);
1136 double* polygonPoint = &polygon[3*i];
1137 polygonPoint[0] = vtkMath::Dot(point,axis0);
1138 polygonPoint[1] = vtkMath::Dot(point,axis1);
1139 polygonPoint[2] = 0.0;
1140 polygonBounds[0] = vtkMath::Min(polygonBounds[0],polygonPoint[0]);
1141 polygonBounds[1] = vtkMath::Max(polygonBounds[1],polygonPoint[0]);
1142 polygonBounds[2] = vtkMath::Min(polygonBounds[2],polygonPoint[1]);
1143 polygonBounds[3] = vtkMath::Max(polygonBounds[3],polygonPoint[1]);
1145 double polygonNormal[3] = {0.0, 0.0, 1.0};
1147 int numberOfCenterlines = 0;
1148 for (
const std::array<double,3>& crossing : crossingPoints)
1150 vtkMath::Subtract(crossing.data(),origin,point);
1151 planePoint[0] = vtkMath::Dot(point,axis0);
1152 planePoint[1] = vtkMath::Dot(point,axis1);
1153 planePoint[2] = 0.0;
1154 if (vtkPolygon::PointInPolygon(planePoint,numberOfPolygonPoints,polygon.data(),polygonBounds,polygonNormal) == 1)
1156 ++numberOfCenterlines;
1160 return numberOfCenterlines;
1165 vtkDataArray* idsArray = section->GetPointData()->GetArray(idsArrayName);
1166 if (idsArray == NULL)
1171 std::set<vtkIdType> ids;
1172 for (vtkIdType i=0; i<idsArray->GetNumberOfTuples(); i++)
1174 ids.insert(
static_cast<vtkIdType
>(idsArray->GetComponent(i,0)));
1177 return static_cast<int>(ids.size());
1182 this->Superclass::PrintSelf(os,indent);
static void FindNTouchingSphereCenter(vtkPolyData *centerlines, const char *radiusArrayName, vtkIdType cellId, vtkIdType subId, double pcoord, int numberOfTouchingSpheres, vtkIdType &touchingSubId, double &touchingPCoord, bool forward=true)
static void InterpolatePoint(vtkPolyData *centerlines, int cellId, int subId, double pcoord, double interpolatedPoint[3])
static void GetGroupUniqueCellIds(vtkPolyData *centerlines, const char *groupIdsArrayName, vtkIdType groupId, vtkIdList *groupCellIds)
static void InterpolateTuple1(vtkPolyData *centerlines, const char *arrayName, int cellId, int subId, double pcoord, double &interpolatedTuple1)
static void GetNonBlankedGroupsIdList(vtkPolyData *centerlines, const char *groupIdsArrayName, const char *blankingArrayName, vtkIdList *groupIds)
Extract a vessel (cross) section n-spheres distance from from the start point of a branch.
char * CenterlineRadiusArrayName
vtkvmtkPolyDataBranchSections()
static int CountBranchSectionIds(vtkPolyData *section, const char *idsArrayName)
void PrintSelf(std::ostream &os, vtkIndent indent) override
char * BranchSectionShapeArrayName
vtkPolyData * Centerlines
char * BranchSectionAreaArrayName
char * CenterlineGroupIdsArrayName
char * BranchSectionMaxSizeArrayName
static double ComputeBranchSectionArea(vtkPolyData *branchSection)
~vtkvmtkPolyDataBranchSections()
char * BranchSectionClosedArrayName
char * BranchSectionMinSizeArrayName
char * CenterlineIdsArrayName
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
char * BranchSectionDistanceSpheresArrayName
void ComputeBranchSections(vtkPolyData *input, int groupId, vtkPolyData *output)
char * BranchSectionGroupIdsArrayName
static void ExtractCylinderSection(vtkPolyData *cylinder, double origin[3], double normal[3], vtkPolyData *section, bool &closed, const char *idsArrayName=nullptr)
static double ComputeBranchSectionShape(vtkPolyData *branchSection, double center[3], double sizeRange[2])
int NumberOfDistanceSpheres
char * CenterlineTractIdsArrayName
static int CountBranchSectionCenterlines(vtkPolyData *section, vtkPolyData *centerlines, double origin[3], double normal[3])
static void ExtractGroup(vtkPolyData *surface, const char *groupIdsArrayName, vtkIdType groupId, bool cleanGroupSurface, vtkPolyData *groupSurface)
#define VTK_VMTK_LARGE_DOUBLE
vtkStandardNewMacro(vtkvmtkPolyDataBranchSections)