249 vtkInformation *vtkNotUsed(request),
250 vtkInformationVector **inputVector,
251 vtkInformationVector *outputVector)
253 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
254 vtkInformation *outInfo = outputVector->GetInformationObject(0);
256 vtkPolyData *input = vtkPolyData::SafeDownCast(
257 inInfo->Get(vtkDataObject::DATA_OBJECT()));
258 vtkPolyData *output = vtkPolyData::SafeDownCast(
259 outInfo->Get(vtkDataObject::DATA_OBJECT()));
265 vtkErrorMacro(<<
"Centerlines not set.");
270 vtkNew<vtkPoints> outputPoints;
271 vtkNew<vtkCellArray> outputPolys;
273 outputPoints->DeepCopy(input->GetPoints());
274 outputPolys->DeepCopy(input->GetPolys());
280 bool useBoundaryLabels =
false;
284 vtkNew<vtkIdList> boundaryLabels;
285 vtkSmartPointer<vtkPolyData> boundaries;
289 vtkNew<vtkPolyData> labeledBoundaries;
292 labeledBoundaries, boundaryLabels);
293 if (useBoundaryLabels)
295 boundaries = labeledBoundaries;
299 vtkWarningMacro(<<
"The boundary label arrays are missing from the input surface or no longer describe it; "
300 <<
"extracting its boundaries instead, and leaving the output unlabeled.");
305 vtkNew<vtkvmtkPolyDataBoundaryExtractor> boundaryExtractor;
306 boundaryExtractor->SetInputData(input);
307 boundaryExtractor->Update();
308 boundaries = boundaryExtractor->GetOutput();
311 vtkNew<vtkPolyData> centerlines;
312 vtkNew<vtkvmtkPolyBallLine> tube;
313 vtkNew<vtkDoubleArray> zeroRadiusArray;
319 const char zeroRadiusArrayName[] =
"ZeroRadiusArray";
321 zeroRadiusArray->SetName(zeroRadiusArrayName);
322 zeroRadiusArray->SetNumberOfTuples(centerlines->GetNumberOfPoints());
323 zeroRadiusArray->FillComponent(0,0.0);
325 centerlines->GetPointData()->AddArray(zeroRadiusArray);
327 tube->SetInput(centerlines);
328 tube->SetPolyBallRadiusArrayName(zeroRadiusArrayName);
337 std::vector<std::vector<vtkIdType> > originalRingByBoundary(boundaries->GetNumberOfCells());
338 std::vector<std::vector<vtkIdType> > tipRingByBoundary(boundaries->GetNumberOfCells());
341 for (i=0; i<boundaries->GetNumberOfCells(); i++)
347 if (this->
BoundaryIds->IsId(useBoundaryLabels ? boundaryLabels->GetId(i) : i) == -1)
353 vtkPolyLine* boundary = vtkPolyLine::SafeDownCast(boundaries->GetCell(i));
357 vtkErrorMacro(<<
"Boundary not a vtkPolyLine");
361 int numberOfBoundaryPoints = boundary->GetNumberOfPoints();
363 vtkNew<vtkIdList> boundaryIds;
365 for (j=0; j<numberOfBoundaryPoints; j++)
367 boundaryIds->InsertNextId(
static_cast<vtkIdType
>(vtkMath::Round(boundaries->GetPointData()->GetScalars()->GetComponent(boundary->GetPointId(j),0))));
370 double barycenter[3];
371 double normal[3], outwardNormal[3];
379 double flowExtensionNormal[3];
380 flowExtensionNormal[0] = flowExtensionNormal[1] = flowExtensionNormal[2] = 0.0;
384 tube->EvaluateFunction(barycenter);
386 double centerlinePoint[3];
387 vtkIdType cellId, subId;
389 tube->GetLastPolyBallCenter(centerlinePoint);
390 cellId = tube->GetLastPolyBallCellId();
391 subId = tube->GetLastPolyBallCellSubId();
392 pcoord = tube->GetLastPolyBallCellPCoord();
394 vtkCell* centerline = centerlines->GetCell(cellId);
396 vtkIdType pointId0, pointId1;
399 double point0[3], point1[3];
402 abscissa = sqrt(vtkMath::Distance2BetweenPoints(centerlinePoint,centerline->GetPoints()->GetPoint(subId)));
403 for (j=subId-1; j>=0; j--)
405 centerline->GetPoints()->GetPoint(j,point0);
406 centerline->GetPoints()->GetPoint(j+1,point1);
407 abscissa += sqrt(vtkMath::Distance2BetweenPoints(point0,point1));
415 pointId1 = centerline->GetNumberOfPoints()-1;
416 abscissa = sqrt(vtkMath::Distance2BetweenPoints(centerlinePoint,centerline->GetPoints()->GetPoint(subId+1)));
417 for (j=subId+1; j<centerline->GetNumberOfPoints()-2; j++)
419 centerline->GetPoints()->GetPoint(j,point0);
420 centerline->GetPoints()->GetPoint(j+1,point1);
421 abscissa += sqrt(vtkMath::Distance2BetweenPoints(point0,point1));
431 double centerlineNormal[3];
433 centerline->GetPoints()->GetPoint(pointId0,point0);
434 centerline->GetPoints()->GetPoint(pointId1,point1);
436 double toleranceFactor = 1E-4;
440 centerlineNormal[k] = 0.0;
442 if (sqrt(vtkMath::Distance2BetweenPoints(point1,centerlinePoint)) > toleranceFactor*meanRadius)
446 centerlineNormal[k] += point1[k] - centerlinePoint[k];
449 if (sqrt(vtkMath::Distance2BetweenPoints(centerlinePoint,point0)) > toleranceFactor*meanRadius)
453 centerlineNormal[k] += centerlinePoint[k] - point0[k];
457 vtkMath::Normalize(centerlineNormal);
461 flowExtensionNormal[k] = centerlineNormal[k];
464 if (vtkMath::Dot(outwardNormal,centerlineNormal) < 0.0)
468 flowExtensionNormal[k] *= -1.0;
476 flowExtensionNormal[k] = outwardNormal[k];
481 vtkErrorMacro(<<
"Invalid ExtensionMode.");
485 double extensionLength;
498 vtkIdType boundaryId = useBoundaryLabels ? boundaryLabels->GetId(i) : i;
505 double point[3], extensionPoint[3];
510 vtkNew<vtkPoints> projectedBoundaryPoints;
511 double meanProjectedRadius = 0.0;
513 double barycenterToPoint[3];
514 double outOfPlaneDistance;
515 double projectedPoint[3];
516 for (j=0; j<numberOfBoundaryPoints; j++)
518 boundary->GetPoints()->GetPoint(j,point);
521 barycenterToPoint[k] = point[k] - barycenter[k];
523 outOfPlaneDistance = vtkMath::Dot(barycenterToPoint,flowExtensionNormal);
526 barycenterToPoint[k] -= outOfPlaneDistance*flowExtensionNormal[k];
527 projectedPoint[k] = barycenter[k] + barycenterToPoint[k];
529 projectedBoundaryPoints->InsertNextPoint(projectedPoint);
530 meanProjectedRadius += vtkMath::Norm(barycenterToPoint);
532 meanProjectedRadius /= numberOfBoundaryPoints;
534 double targetRadius = 0.0;
538 targetRadius = meanProjectedRadius;
547 vtkWarningMacro(<<
"Degenerate boundary outline, skipping flow extension for boundary "<<i);
551 vtkNew<vtkIdList> newBoundaryIds;
552 vtkNew<vtkIdList> previousBoundaryIds;
555 previousBoundaryIds->DeepCopy(boundaryIds);
562 targetNumberOfBoundaryPoints = numberOfBoundaryPoints;
565 double targetDistanceBetweenPoints = 0.0;
567 vtkNew<vtkThinPlateSplineTransform> thinPlateSplineTransform;
568 thinPlateSplineTransform->SetSigma(this->
Sigma);
569 thinPlateSplineTransform->SetBasisToR2LogR();
572 vtkNew<vtkPoints> sourceLandmarks;
573 vtkNew<vtkPoints> targetLandmarks;
575 vtkNew<vtkPoints> targetBoundaryPoints;
576 vtkNew<vtkPoints> targetStaggeredBoundaryPoints;
582 vtkNew<vtkPoints> rimBoundaryPoints;
583 vtkNew<vtkPoints> rimStaggeredBoundaryPoints;
585 int startNumberOfBoundaryPoints = numberOfBoundaryPoints;
594 vtkPoints* rimPoints = useRampInterpolation ? rimBoundaryPoints.GetPointer() :
nullptr;
595 vtkPoints* rimStaggeredPoints = useRampInterpolation ? rimStaggeredBoundaryPoints.GetPointer() :
nullptr;
596 double perimeter = ResampleClosedPolygon(projectedBoundaryPoints,targetNumberOfBoundaryPoints,0.0,targetBoundaryPoints,boundary->GetPoints(),rimPoints);
597 ResampleClosedPolygon(projectedBoundaryPoints,targetNumberOfBoundaryPoints,0.5,targetStaggeredBoundaryPoints,boundary->GetPoints(),rimStaggeredPoints);
599 double targetPoint[3];
606 double meanResampledRadius = 0.0;
607 for (j=0; j<targetNumberOfBoundaryPoints; j++)
609 targetBoundaryPoints->GetPoint(j,targetPoint);
612 targetPoint[k] -= barycenter[k];
614 meanResampledRadius += vtkMath::Norm(targetPoint);
616 meanResampledRadius /= targetNumberOfBoundaryPoints;
617 scale = targetRadius / meanResampledRadius;
619 for (j=0; j<targetNumberOfBoundaryPoints; j++)
621 targetBoundaryPoints->GetPoint(j,targetPoint);
624 targetPoint[k] = barycenter[k] + scale * (targetPoint[k] - barycenter[k]);
626 targetBoundaryPoints->SetPoint(j,targetPoint);
627 targetStaggeredBoundaryPoints->GetPoint(j,targetPoint);
630 targetPoint[k] = barycenter[k] + scale * (targetPoint[k] - barycenter[k]);
632 targetStaggeredBoundaryPoints->SetPoint(j,targetPoint);
636 targetDistanceBetweenPoints = scale * perimeter / targetNumberOfBoundaryPoints;
640 targetDistanceBetweenPoints = 2.0 * sin (vtkMath::Pi() / targetNumberOfBoundaryPoints) * targetRadius;
642 double baseRadialNormal[3];
643 input->GetPoint(previousBoundaryIds->GetId(0),point);
646 baseRadialNormal[k] = point[k] - barycenter[k];
648 double outOfPlaneComponent = vtkMath::Dot(baseRadialNormal,flowExtensionNormal);
651 baseRadialNormal[k] -= outOfPlaneComponent * flowExtensionNormal[k];
653 vtkMath::Normalize(baseRadialNormal);
654 double angle = 360.0 / targetNumberOfBoundaryPoints;
655 vtkNew<vtkTransform> transform;
656 transform->RotateWXYZ(0.5*angle,flowExtensionNormal);
657 double testRadialNormal[3];
658 transform->TransformPoint(baseRadialNormal,testRadialNormal);
659 double cross[3], testCross[3], point1[3];
660 vtkMath::Cross(baseRadialNormal,testRadialNormal,testCross);
663 int numberOfPreviousBoundaryIds = previousBoundaryIds->GetNumberOfIds();
664 while (dist < 1E-8 && testId < numberOfPreviousBoundaryIds)
666 input->GetPoint(previousBoundaryIds->GetId(testId),point1);
667 dist = sqrt(vtkMath::Distance2BetweenPoints(point,point1));
670 double testRadialVector[3];
673 testRadialVector[k] = point1[k] - barycenter[k];
675 vtkMath::Cross(baseRadialNormal,testRadialVector,cross);
676 if (vtkMath::Dot(cross,testCross) < 0.0)
679 transform->Identity();
680 transform->RotateWXYZ(0.5*angle,flowExtensionNormal);
682 double radialVector[3];
685 radialVector[k] = targetRadius * baseRadialNormal[k];
687 double targetPoint[3];
688 for (j=0; j<targetNumberOfBoundaryPoints; j++)
692 targetPoint[k] = barycenter[k] + radialVector[k];
694 targetBoundaryPoints->InsertNextPoint(targetPoint);
695 transform->TransformPoint(radialVector,radialVector);
698 targetPoint[k] = barycenter[k] + radialVector[k];
700 targetStaggeredBoundaryPoints->InsertNextPoint(targetPoint);
701 transform->TransformPoint(radialVector,radialVector);
704 if (useRampInterpolation)
711 if (!SampleClosedPolygonByAngle(projectedBoundaryPoints,boundary->GetPoints(),barycenter,flowExtensionNormal,baseRadialNormal,angle,0.0,targetNumberOfBoundaryPoints,rimBoundaryPoints)
712 || !SampleClosedPolygonByAngle(projectedBoundaryPoints,boundary->GetPoints(),barycenter,flowExtensionNormal,baseRadialNormal,angle,0.5,targetNumberOfBoundaryPoints,rimStaggeredBoundaryPoints))
714 vtkWarningMacro(<<
"Boundary "<<i<<
" is not star-shaped about its barycenter, pairing it with the extension by arc length instead of by angle");
715 rimBoundaryPoints->Initialize();
716 rimStaggeredBoundaryPoints->Initialize();
717 vtkNew<vtkPoints> discardedPoints;
718 ResampleClosedPolygon(projectedBoundaryPoints,targetNumberOfBoundaryPoints,0.0,discardedPoints,boundary->GetPoints(),rimBoundaryPoints);
719 discardedPoints->Initialize();
720 ResampleClosedPolygon(projectedBoundaryPoints,targetNumberOfBoundaryPoints,0.5,discardedPoints,boundary->GetPoints(),rimStaggeredBoundaryPoints);
729 std::vector<double> displacement, staggeredDisplacement;
731 if (useRampInterpolation)
733 displacement.resize(3*targetNumberOfBoundaryPoints);
734 staggeredDisplacement.resize(3*targetNumberOfBoundaryPoints);
735 double targetPoint[3], rimPoint[3];
736 for (j=0; j<targetNumberOfBoundaryPoints; j++)
738 targetBoundaryPoints->GetPoint(j,targetPoint);
739 rimBoundaryPoints->GetPoint(j,rimPoint);
742 displacement[3*j+k] = rimPoint[k] - targetPoint[k];
744 targetStaggeredBoundaryPoints->GetPoint(j,targetPoint);
745 rimStaggeredBoundaryPoints->GetPoint(j,rimPoint);
748 staggeredDisplacement[3*j+k] = rimPoint[k] - targetPoint[k];
755 for (j=0; j<targetNumberOfBoundaryPoints; j++)
757 double firstBoundaryPoint[3], lastBoundaryPoint[3];
758 targetBoundaryPoints->GetPoint(j,firstBoundaryPoint);
759 double distance = 1E20;
760 double currentPoint[3];
761 for (
int j2=0; j2<startNumberOfBoundaryPoints; j2++)
763 input->GetPoint(previousBoundaryIds->GetId(j2),currentPoint);
764 double currentDistance = vtkMath::Distance2BetweenPoints(currentPoint,firstBoundaryPoint);
765 if (currentDistance < distance)
767 distance = currentDistance;
770 point[k] = currentPoint[k];
774 sourceLandmarks->InsertNextPoint(firstBoundaryPoint);
775 targetLandmarks->InsertNextPoint(point);
778 lastBoundaryPoint[k] = firstBoundaryPoint[k] + extensionLength * this->
TransitionRatio * flowExtensionNormal[k];
780 sourceLandmarks->InsertNextPoint(lastBoundaryPoint);
781 targetLandmarks->InsertNextPoint(lastBoundaryPoint);
783 thinPlateSplineTransform->SetSourceLandmarks(sourceLandmarks);
784 thinPlateSplineTransform->SetTargetLandmarks(targetLandmarks);
788 int numberOfLayers = extensionLength / targetDistanceBetweenPoints;
789 int numberOfTransitionLayers = transitionLength / targetDistanceBetweenPoints;
791 for (l=0; l<numberOfLayers; l++)
793 newBoundaryIds->Initialize();
794 for (j=0; j<targetNumberOfBoundaryPoints; j++)
798 targetBoundaryPoints->GetPoint(j,extensionPoint);
802 targetStaggeredBoundaryPoints->GetPoint(j,extensionPoint);
806 extensionPoint[k] += (l+1) * targetDistanceBetweenPoints * flowExtensionNormal[k];
808 if (l<numberOfTransitionLayers)
812 thinPlateSplineTransform->TransformPoint(extensionPoint,extensionPoint);
820 double parametricHeight = (l+1) * targetDistanceBetweenPoints / transitionLength;
821 parametricHeight = parametricHeight > 1.0 ? 1.0 : parametricHeight;
825 weight = 1.0 - parametricHeight;
831 weight = 1.0 - parametricHeight * parametricHeight * (3.0 - 2.0 * parametricHeight);
833 const std::vector<double>& layerDisplacement = (l%2 != 0) ? displacement : staggeredDisplacement;
836 extensionPoint[k] += weight * layerDisplacement[3*j+k];
840 pointId = outputPoints->InsertNextPoint(extensionPoint);
841 newBoundaryIds->InsertNextId(pointId);
848 for (j=0; j<targetNumberOfBoundaryPoints; j++)
850 double point0[3], point1[3], point2[3], point3[3];
851 outputPoints->GetPoint(previousBoundaryIds->GetId(j2%startNumberOfBoundaryPoints),point0);
852 outputPoints->GetPoint(previousBoundaryIds->GetId((j2+1)%startNumberOfBoundaryPoints),point1);
853 outputPoints->GetPoint(newBoundaryIds->GetId(j),point2);
854 outputPoints->GetPoint(newBoundaryIds->GetId((j+1)%targetNumberOfBoundaryPoints),point3);
856 bool advance =
false;
857 if ((j==0) || (j==targetNumberOfBoundaryPoints-1) || (vtkMath::Distance2BetweenPoints(point0,point3) > vtkMath::Distance2BetweenPoints(point1,point2)))
861 if (j2 == startNumberOfBoundaryPoints)
867 pts[0] = previousBoundaryIds->GetId(j2%startNumberOfBoundaryPoints);
868 pts[1] = newBoundaryIds->GetId(j);
869 pts[2] = previousBoundaryIds->GetId((j2+1)%startNumberOfBoundaryPoints);
870 outputPolys->InsertNextCell(3,pts);
873 outputPoints->GetPoint(previousBoundaryIds->GetId(j2%startNumberOfBoundaryPoints),point0);
874 outputPoints->GetPoint(previousBoundaryIds->GetId((j2+1)%startNumberOfBoundaryPoints),point1);
875 outputPoints->GetPoint(newBoundaryIds->GetId(j),point2);
876 outputPoints->GetPoint(newBoundaryIds->GetId((j+1)%targetNumberOfBoundaryPoints),point3);
878 if (j2 == startNumberOfBoundaryPoints || vtkMath::Distance2BetweenPoints(point0,point3) < vtkMath::Distance2BetweenPoints(point1,point2))
881 if (j2 < startNumberOfBoundaryPoints && j == targetNumberOfBoundaryPoints-1)
887 pts[0] = newBoundaryIds->GetId(j);
888 pts[1] = newBoundaryIds->GetId((j+1)%targetNumberOfBoundaryPoints);
889 pts[2] = previousBoundaryIds->GetId(j2%startNumberOfBoundaryPoints);
890 outputPolys->InsertNextCell(3,pts);
896 for (j=0; j<targetNumberOfBoundaryPoints; j++)
900 pts[0] = newBoundaryIds->GetId(j);
901 pts[1] = newBoundaryIds->GetId((j-1+targetNumberOfBoundaryPoints)%targetNumberOfBoundaryPoints);
902 pts[2] = previousBoundaryIds->GetId((j-1+targetNumberOfBoundaryPoints)%targetNumberOfBoundaryPoints);
903 outputPolys->InsertNextCell(3,pts);
905 pts[0] = previousBoundaryIds->GetId(j);
906 pts[1] = newBoundaryIds->GetId(j);
907 pts[2] = previousBoundaryIds->GetId((j-1+targetNumberOfBoundaryPoints)%targetNumberOfBoundaryPoints);
908 outputPolys->InsertNextCell(3,pts);
912 pts[0] = newBoundaryIds->GetId(j);
913 pts[1] = newBoundaryIds->GetId((j-1+targetNumberOfBoundaryPoints)%targetNumberOfBoundaryPoints);
914 pts[2] = previousBoundaryIds->GetId(j);
915 outputPolys->InsertNextCell(3,pts);
917 pts[0] = previousBoundaryIds->GetId(j);
918 pts[1] = newBoundaryIds->GetId((j-1+targetNumberOfBoundaryPoints)%targetNumberOfBoundaryPoints);
919 pts[2] = previousBoundaryIds->GetId((j-1+targetNumberOfBoundaryPoints)%targetNumberOfBoundaryPoints);
920 outputPolys->InsertNextCell(3,pts);
925 previousBoundaryIds->DeepCopy(newBoundaryIds);
931 if (useBoundaryLabels)
933 for (j=0; j<boundaryIds->GetNumberOfIds(); j++)
935 originalRingByBoundary[i].push_back(boundaryIds->GetId(j));
937 for (j=0; j<previousBoundaryIds->GetNumberOfIds(); j++)
939 tipRingByBoundary[i].push_back(previousBoundaryIds->GetId(j));
945 output->SetPoints(outputPoints);
946 output->SetPolys(outputPolys);
948 if (useBoundaryLabels)
956 vtkIdType numberOfInputPoints = input->GetNumberOfPoints();
957 vtkIdType numberOfOutputPoints = outputPoints->GetNumberOfPoints();
962 vtkNew<vtkIntArray> outputLabels;
964 outputLabels->SetNumberOfTuples(numberOfOutputPoints);
965 outputLabels->FillComponent(0,
static_cast<double>(invalidLabel));
967 vtkNew<vtkIntArray> outputOrder;
969 outputOrder->SetNumberOfTuples(numberOfOutputPoints);
970 outputOrder->FillComponent(0,
static_cast<double>(invalidLabel));
972 for (vtkIdType pointId=0; pointId<numberOfInputPoints; pointId++)
974 outputLabels->SetValue(pointId,
static_cast<int>(vtkMath::Round(inputLabels->GetComponent(pointId,0))));
975 outputOrder->SetValue(pointId,
static_cast<int>(vtkMath::Round(inputOrder->GetComponent(pointId,0))));
978 for (
size_t boundaryIndex=0; boundaryIndex<tipRingByBoundary.size(); boundaryIndex++)
980 if (tipRingByBoundary[boundaryIndex].empty())
984 vtkIdType boundaryLabel = boundaryLabels->GetId(
static_cast<vtkIdType
>(boundaryIndex));
985 for (
size_t j2=0; j2<originalRingByBoundary[boundaryIndex].size(); j2++)
987 outputLabels->SetValue(originalRingByBoundary[boundaryIndex][j2],
static_cast<int>(invalidLabel));
988 outputOrder->SetValue(originalRingByBoundary[boundaryIndex][j2],
static_cast<int>(invalidLabel));
990 for (
size_t j2=0; j2<tipRingByBoundary[boundaryIndex].size(); j2++)
992 outputLabels->SetValue(tipRingByBoundary[boundaryIndex][j2],
static_cast<int>(boundaryLabel));
993 outputOrder->SetValue(tipRingByBoundary[boundaryIndex][j2],
static_cast<int>(j2));
997 output->GetPointData()->AddArray(outputLabels);
998 output->GetPointData()->AddArray(outputOrder);