4 #include <vtkPolyData.h> 8 #include <vtkCellArray.h> 9 #include "vtkCardinalSpline.h" 37 int N = centerline_r->GetNumberOfPoints();
38 Eigen::MatrixXd CLpoints(3,N);
39 for(vtkIdType i = 0; i < N; i++)
42 centerline_r->GetPoint(i,p);
43 Eigen::Vector3d position;
44 position(0) = p[0]; position(1) = p[1]; position(2) = p[2];
45 CLpoints.block(0 , i , 3 , 1) = position;
53 mBranchListPtr->deleteAllBranches();
57 mBranchListPtr->findBranchesInCenterline(CLpoints_r);
59 mBranchListPtr->calculateOrientations();
60 mBranchListPtr->smoothOrientations();
63 std::cout <<
"Number of branches in CT centerline: " << mBranchListPtr->getBranches().size() << std::endl;
70 double minDistance = 100000;
71 int minDistancePositionIndex;
73 std::vector<BranchPtr> branches = mBranchListPtr->getBranches();
74 for (
int i = 0; i < branches.size(); i++)
76 Eigen::MatrixXd positions = branches[i]->getPositions();
77 for (
int j = 0; j < positions.cols(); j++)
79 double D =
findDistance(positions.col(j), targetCoordinate_r);
83 minDistanceBranch = branches[i];
84 minDistancePositionIndex = j;
89 mProjectedBranchPtr = minDistanceBranch;
90 mProjectedIndex = minDistancePositionIndex;
96 mRoutePositions.clear();
109 if (!searchBranchPtr)
112 std::vector< Eigen::Vector3d > positions = smoothBranch(searchBranchPtr, startIndex, searchBranchPtr->getPositions().col(startIndex));
114 for (
int i = 0; i<=startIndex && i<positions.size(); i++)
115 mRoutePositions.push_back(positions[i]);
117 BranchPtr parentBranchPtr = searchBranchPtr->getParentBranch();
119 searchBranchUp(parentBranchPtr, parentBranchPtr->getPositions().cols()-1);
138 float extentionPointIncrement = 0.5;
139 mExtendedRoutePositions.clear();
140 mExtendedRoutePositions = mRoutePositions;
141 if(mRoutePositions.size() > 0)
143 double extentionDistance =
findDistance(mRoutePositions.front(),targetCoordinate_r);
144 Eigen::Vector3d extentionVector = ( targetCoordinate_r - mRoutePositions.front() ) / extentionDistance;
145 int numberOfextentionPoints = (int) extentionDistance * extentionPointIncrement;
146 Eigen::Vector3d extentionPointIncrementVector = extentionVector / extentionPointIncrement;
148 for (
int i = 1; i<= numberOfextentionPoints; i++)
150 mExtendedRoutePositions.insert(mExtendedRoutePositions.begin(), mRoutePositions.front() + extentionPointIncrementVector*i);
164 int numberOfPositions = positions.size();
165 for (
int j = numberOfPositions - 1; j >= 0; j--)
167 points->InsertNextPoint(positions[j](0),positions[j](1),positions[j](2));
169 for (
int j = 0; j < numberOfPositions-1; j++)
171 vtkIdType connection[2] = {j, j+1};
172 lines->InsertNextCell(2, connection);
174 retval->SetPoints(points);
175 retval->SetLines(lines);
189 std::vector< Eigen::Vector3d > RouteToTarget::smoothBranch(
BranchPtr branchPtr,
int startIndex, Eigen::MatrixXd startPosition)
195 double branchRadius = branchPtr->findBranchRadius();
200 Eigen::MatrixXd positions = branchPtr->getPositions();
201 splineX->AddPoint(0,startPosition(0));
202 splineY->AddPoint(0,startPosition(1));
203 splineZ->AddPoint(0,startPosition(2));
209 if(!branchPtr->getParentBranch())
211 splineX->AddPoint(startIndex,positions(0,0));
212 splineY->AddPoint(startIndex,positions(1,0));
213 splineZ->AddPoint(startIndex,positions(2,0));
217 Eigen::MatrixXd parentPositions = branchPtr->getParentBranch()->getPositions();
218 splineX->AddPoint(startIndex,parentPositions(0,parentPositions.cols()-1));
219 splineY->AddPoint(startIndex,parentPositions(1,parentPositions.cols()-1));
220 splineZ->AddPoint(startIndex,parentPositions(2,parentPositions.cols()-1));
226 double maxDistanceToOriginalPosition = branchRadius;
227 int maxDistanceIndex = -1;
228 std::vector< Eigen::Vector3d > smoothingResult;
231 while (maxDistanceToOriginalPosition >= branchRadius && splineX->GetNumberOfPoints() < startIndex)
233 if(maxDistanceIndex > 0)
236 splineX->AddPoint(maxDistanceIndex,positions(0,startIndex - maxDistanceIndex));
237 splineY->AddPoint(maxDistanceIndex,positions(1,startIndex - maxDistanceIndex));
238 splineZ->AddPoint(maxDistanceIndex,positions(2,startIndex - maxDistanceIndex));
242 maxDistanceToOriginalPosition = 0.0;
243 smoothingResult.clear();
244 for(
int j=0; j<=startIndex; j++)
246 double splineParameter = j;
247 Eigen::Vector3d tempPoint;
248 tempPoint(0) = splineX->Evaluate(splineParameter);
249 tempPoint(1) = splineY->Evaluate(splineParameter);
250 tempPoint(2) = splineZ->Evaluate(splineParameter);
251 smoothingResult.push_back(tempPoint);
256 if (distance > maxDistanceToOriginalPosition)
258 maxDistanceToOriginalPosition = distance;
259 maxDistanceIndex = j;
264 return smoothingResult;
270 for (
int i=1; i<line.cols(); i++)
279 double d0 = p1(0) - p2(0);
280 double d1 = p1(1) - p2(1);
281 double d2 = p1(2) - p2(2);
283 double D = sqrt( d0*d0 + d1*d1 + d2*d2 );
vtkPolyDataPtr findExtendedRoute(Vector3D targetCoordinate_r)
double findDistanceToLine(Eigen::MatrixXd point, Eigen::MatrixXd line)
vtkSmartPointer< class vtkCellArray > vtkCellArrayPtr
vtkSmartPointer< vtkPoints > vtkPointsPtr
vtkPolyDataPtr addVTKPoints(std::vector< Eigen::Vector3d > positions)
boost::shared_ptr< class Branch > BranchPtr
double findDistance(Eigen::MatrixXd p1, Eigen::MatrixXd p2)
vtkSmartPointer< class vtkCardinalSpline > vtkCardinalSplinePtr
void findClosestPointInBranches(Vector3D targetCoordinate_r)
void searchBranchUp(BranchPtr searchBranchPtr, int startIndex)
void processCenterline(vtkPolyDataPtr centerline_r)
vtkSmartPointer< class vtkCardinalSpline > vtkCardinalSplinePtr
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
vtkPolyDataPtr findRouteToTarget(Vector3D targetCoordinate_r)
void findRoutePositions()
Eigen::MatrixXd getCenterlinePositions(vtkPolyDataPtr centerline_r)
Namespace for all CustusX production code.