CustusX  15.8
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxRouteToTarget.cpp
Go to the documentation of this file.
1 
2 
3 #include "cxRouteToTarget.h"
4 #include <vtkPolyData.h>
5 //#include "cxDoubleDataAdapterXml.h"
6 #include "cxBranchList.h"
7 #include "cxBranch.h"
8 #include <vtkCellArray.h>
9 
10 namespace cx
11 {
12 
14  mBranchListPtr(new BranchList)
15 {
16 }
17 
18 
20 {
21 }
22 
24 {
25  mCLpoints = this->getCenterlinePositions(centerline);
26 }
27 
29 {
30 
31  int N = centerline->GetNumberOfPoints();
32  Eigen::MatrixXd CLpoints(3,N);
33  for(vtkIdType i = 0; i < N; i++)
34  {
35  double p[3];
36  centerline->GetPoint(i,p);
37  Eigen::Vector3d position;
38  position(0) = p[0]; position(1) = p[1]; position(2) = p[2];
39  CLpoints.block(0 , i , 3 , 1) = position;
40  }
41  return CLpoints;
42 }
43 
45 {
46  if (mBranchListPtr)
47  mBranchListPtr->deleteAllBranches();
48 
49  Eigen::MatrixXd CLpoints = getCenterlinePositions(centerline);
50 
51  mBranchListPtr->findBranchesInCenterline(CLpoints);
52  mBranchListPtr->calculateOrientations();
53  mBranchListPtr->smoothOrientations();
54 
55  std::cout << "Number of branches in CT centerline: " << mBranchListPtr->getBranches().size() << std::endl;
56 }
57 
58 
60 {
61 
62  double minDistance = 100000;
63  int minDistancePositionIndex;
64  BranchPtr minDistanceBranch;
65  std::vector<BranchPtr> branches = mBranchListPtr->getBranches();
66  for (int i = 0; i < branches.size(); i++)
67  {
68  Eigen::MatrixXd positions = branches[i]->getPositions();
69  for (int j = 0; j < positions.cols(); j++)
70  {
71  double D = findDistance(positions.col(j), targetCoordinate);
72  if (D < minDistance)
73  {
74  minDistance = D;
75  minDistanceBranch = branches[i];
76  minDistancePositionIndex = j;
77  }
78  }
79  }
80 
81  mProjectedBranchPtr = minDistanceBranch;
82  mProjectedIndex = minDistancePositionIndex;
83 }
84 
85 
87 {
88  mRoutePositions.clear();
89 
90  searchBranchUp(mProjectedBranchPtr, mProjectedIndex);
91 }
92 
93 void RouteToTarget::searchBranchUp(BranchPtr searchBranchPtr, int startIndex)
94 {
95  Eigen::MatrixXd positions = searchBranchPtr->getPositions();
96 
97  for (int i = startIndex; i>=0; i--)
98  mRoutePositions.push_back(positions.col(i));
99 
100  BranchPtr parentBranchPtr = searchBranchPtr->getParentBranch();
101  if (parentBranchPtr)
102  searchBranchUp(parentBranchPtr, parentBranchPtr->getPositions().cols()-1);
103 }
104 
105 
107 {
108 
109  findClosestPointInBranches(targetCoordinate);
111 
112  vtkPolyDataPtr retval = addVTKPoints();
113 
114  return retval;
115 }
116 
118 {
119  vtkPolyDataPtr retval = vtkPolyDataPtr::New();
120  vtkPointsPtr points = vtkPointsPtr::New();
121  vtkCellArrayPtr lines = vtkCellArrayPtr::New();
122  for (int j = mRoutePositions.size() - 1; j >= 0; j--)
123  {
124  vtkIdType cells[1] = { points->GetNumberOfPoints() };
125  points->InsertNextPoint(mRoutePositions[j](0),mRoutePositions[j](1),mRoutePositions[j](2));
126  lines->InsertNextCell(1, cells);
127  }
128  retval->SetPoints(points);
129  retval->SetVerts(lines);
130  return retval;
131 }
132 
133 double findDistance(Eigen::MatrixXd p1, Eigen::MatrixXd p2)
134 {
135  double d0 = p1(0) - p2(0);
136  double d1 = p1(1) - p2(1);
137  double d2 = p1(2) - p2(2);
138 
139  double D = sqrt( d0*d0 + d1*d1 + d2*d2 );
140 
141  return D;
142 }
143 
144 
145 } /* namespace cx */
void findClosestPointInBranches(Vector3D targetCoordinate)
vtkSmartPointer< class vtkCellArray > vtkCellArrayPtr
void setCenterline(vtkPolyDataPtr centerline)
void processCenterline(vtkPolyDataPtr centerline)
vtkSmartPointer< class vtkPolyData > vtkPolyDataPtr
Definition: cxProbeSector.h:47
boost::shared_ptr< class Branch > BranchPtr
double findDistance(Eigen::MatrixXd p1, Eigen::MatrixXd p2)
void searchBranchUp(BranchPtr searchBranchPtr, int startIndex)
vtkPolyDataPtr findRouteToTarget(Vector3D targetCoordinate)
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
vtkPolyDataPtr addVTKPoints()
vtkSmartPointer< class vtkPoints > vtkPointsPtr
Eigen::MatrixXd getCenterlinePositions(vtkPolyDataPtr centerline)