15 #include <vtkPolyData.h> 16 #include <vtkCardinalSpline.h> 38 mBranches.push_back(b);
43 for(
int i = 0; i < mBranches.size(); i++ )
45 if (b == mBranches[i])
47 mBranches.erase(mBranches.begin() + i);
65 std::vector<int> branchNumbersToBeDeleted;
66 for(
int i = 0; i < mBranches.size(); i++ )
68 int generationCounter = 1;
70 while (currentBranch->getParentBranch()){
72 currentBranch = currentBranch->getParentBranch();
73 if (generationCounter > maxGeneration)
75 branchNumbersToBeDeleted.push_back(i);
82 for (
int i = branchNumbersToBeDeleted.size() - 1; i >= 0; i-- )
89 for (
int i = 0; i < mBranches.size(); i++)
91 Eigen::MatrixXd positions = mBranches[i]->getPositions();
92 Eigen::MatrixXd diff = positions.rightCols(positions.cols() - 1) - positions.leftCols(positions.cols() - 1);
93 Eigen::MatrixXd orientations(positions.rows(),positions.cols());
94 orientations.leftCols(orientations.cols() - 1) = diff / diff.norm();
95 orientations.rightCols(1) = orientations.col(orientations.cols() - 1);
96 mBranches[i]->setOrientations(orientations);
102 for (
int i = 0; i < mBranches.size(); i++)
104 Eigen::MatrixXd orientations = mBranches[i]->getOrientations();
105 Eigen::MatrixXd newOrientations(orientations.rows(),orientations.cols());
106 int numberOfColumns = orientations.cols();
107 for (
int j = 0; j < numberOfColumns; j++)
109 newOrientations.col(j) = orientations.block(0,std::max(j-2,0),orientations.rows(),std::min(5,numberOfColumns-j)).rowwise().mean();
110 newOrientations.col(j) = newOrientations.col(j) / newOrientations.col(j).norm();
112 mBranches[i]->setOrientations(newOrientations);
118 for (
int i = 0; i < mBranches.size(); i++)
120 Eigen::MatrixXd positions = mBranches[i]->getPositions();
121 std::vector<Eigen::Vector3d> interpolatedPositions;
122 for (
int j = 0; j < positions.cols()-1; j++)
124 for (
int k = 0; k < interpolationFactor; k++){
125 Eigen::Vector3d interpolationPoint;
126 interpolationPoint[0] = (positions(0,j)*(interpolationFactor-k) + positions(0,j+1)*(k) ) / interpolationFactor;
127 interpolationPoint[1] = (positions(1,j)*(interpolationFactor-k) + positions(1,j+1)*(k) ) / interpolationFactor;
128 interpolationPoint[2] = (positions(2,j)*(interpolationFactor-k) + positions(2,j+1)*(k) ) / interpolationFactor;
129 interpolatedPositions.push_back(interpolationPoint);
132 Eigen::MatrixXd interpolationResult(3 , interpolatedPositions.size());
133 for (
int j = 0; j < interpolatedPositions.size(); j++)
135 interpolationResult(0,j) = interpolatedPositions[j](0);
136 interpolationResult(1,j) = interpolatedPositions[j](1);
137 interpolationResult(2,j) = interpolatedPositions[j](2);
139 mBranches[i]->setPositions(interpolationResult);
146 for (
int i = 0; i < mBranches.size(); i++)
148 Eigen::MatrixXd positions = mBranches[i]->getPositions();
149 int numberOfInputPoints = positions.cols();
152 double branchLength = (positions.rightCols(1) - positions.leftCols(1)).norm();
153 int numberOfControlPoints =
std::ceil(branchLength/controlPointDistance);
154 numberOfControlPoints = std::max(numberOfControlPoints, 2);
161 for(
int j=0; j<numberOfControlPoints; j++)
163 int indexP = (j*numberOfInputPoints)/numberOfControlPoints;
165 splineX->AddPoint(indexP,positions(0,indexP));
166 splineY->AddPoint(indexP,positions(1,indexP));
167 splineZ->AddPoint(indexP,positions(2,indexP));
170 splineX->AddPoint(numberOfInputPoints-1,positions(0,numberOfInputPoints-1));
171 splineY->AddPoint(numberOfInputPoints-1,positions(1,numberOfInputPoints-1));
172 splineZ->AddPoint(numberOfInputPoints-1,positions(2,numberOfInputPoints-1));
175 Eigen::MatrixXd smoothingResult(3 , numberOfInputPoints);
176 for(
int j=0; j<numberOfInputPoints; j++)
178 double splineParameter = j;
179 smoothingResult(0,j) = splineX->Evaluate(splineParameter);
180 smoothingResult(1,j) = splineY->Evaluate(splineParameter);
181 smoothingResult(2,j) = splineZ->Evaluate(splineParameter);
183 mBranches[i]->setPositions(smoothingResult);
190 Eigen::MatrixXd positionsNotUsed_r = positions_r;
195 Eigen::MatrixXd::Index startIndex;
197 while (positionsNotUsed_r.cols() > 0)
199 if (!mBranches.empty())
201 double minDistance = 1000;
202 for (
int i = 0; i < mBranches.size(); i++)
204 std::pair<std::vector<Eigen::MatrixXd::Index>, Eigen::VectorXd> distances;
205 distances =
dsearchn(positionsNotUsed_r, mBranches[i]->getPositions());
206 double d = distances.second.minCoeff(&index);
210 branchToSplit = mBranches[i];
216 std::pair<Eigen::MatrixXd::Index, double> dsearchResult =
dsearch(positionsNotUsed_r.col(startIndex) , branchToSplit->getPositions());
217 splitIndex = dsearchResult.first;
220 startIndex = positionsNotUsed_r.cols() - 1;
222 std::pair<Eigen::MatrixXd,Eigen::MatrixXd > connectedPointsResult =
findConnectedPointsInCT(startIndex , positionsNotUsed_r);
223 Eigen::MatrixXd branchPositions = connectedPointsResult.first;
224 positionsNotUsed_r = connectedPointsResult.second;
226 if (branchPositions.cols() >= 5)
229 newBranch->setPositions(branchPositions);
230 mBranches.push_back(newBranch);
232 if (mBranches.size() > 1)
234 if ((splitIndex + 1 >= 5) && (branchToSplit->getPositions().cols() - splitIndex - 1 >= 5))
240 Eigen::MatrixXd branchToSplitPositions = branchToSplit->getPositions();
241 newBranchFromSplit->setPositions(branchToSplitPositions.rightCols(branchToSplitPositions.cols() - splitIndex - 1));
242 branchToSplit->setPositions(branchToSplitPositions.leftCols(splitIndex + 1));
243 mBranches.push_back(newBranchFromSplit);
244 newBranchFromSplit->setParentBranch(branchToSplit);
245 newBranch->setParentBranch(branchToSplit);
246 newBranchFromSplit->setChildBranches(branchToSplit->getChildBranches());
247 branchVector branchToSplitChildren = branchToSplit->getChildBranches();
248 for (
int i = 0; i < branchToSplitChildren.size(); i++)
249 branchToSplitChildren[i]->setParentBranch(newBranchFromSplit);
250 branchToSplit->deleteChildBranches();
251 branchToSplit->addChildBranch(newBranchFromSplit);
252 branchToSplit->addChildBranch(newBranch);
254 else if (splitIndex + 1 < 5)
259 newBranch->setParentBranch(branchToSplit->getParentBranch());
260 if(branchToSplit->getParentBranch())
261 branchToSplit->getParentBranch()->addChildBranch(newBranch);
263 else if (branchToSplit->getPositions().cols() - splitIndex - 1 < 5)
267 newBranch->setParentBranch(branchToSplit);
268 branchToSplit->addChildBranch(newBranch);
282 for (
int i = 0; i < mBranches.size(); i++)
285 b->setPositions(mBranches[i]->getPositions());
286 b->setOrientations(mBranches[i]->getOrientations());
287 retval->addBranch(b);
290 std::vector<BranchPtr> branches = retval->getBranches();
291 Eigen::MatrixXd positions;
292 Eigen::MatrixXd orientations;
293 for (
int i = 0; i < branches.size(); i++)
295 positions = branches[i]->getPositions();
296 orientations = branches[i]->getOrientations();
297 std::pair<std::vector<Eigen::MatrixXd::Index>, Eigen::VectorXd> distanceData;
298 distanceData =
dsearchn(positions, trackingPositions);
299 Eigen::VectorXd distance = distanceData.second;
300 for (
int j = positions.cols() - 1; j >= 0; j--)
302 if (distance(j) > maxDistance)
305 orientations =
eraseCol(j, orientations);
308 branches[i]->setPositions(positions);
309 branches[i]->setOrientations(orientations);
338 int positionCounter = 0;
339 for (
size_t i = 0; i < mBranches.size(); ++i)
341 Eigen::MatrixXd positions = mBranches[i]->getPositions();
345 points->InsertNextPoint(positions(0,0),positions(1,0),positions(2,0));
346 points->InsertNextPoint(positions(0,positions.cols()-1),positions(1,positions.cols()-1),positions(2,positions.cols()-1));
347 vtkIdType connection[2] = {positionCounter-1, positionCounter};
348 lines->InsertNextCell(2, connection);
353 for (
int j = 0; j < positions.cols(); ++j)
356 points->InsertNextPoint(positions(0,j),positions(1,j),positions(2,j));
357 if (j < positions.cols()-1)
359 vtkIdType connection[2] = {positionCounter-1, positionCounter};
360 lines->InsertNextCell(2, connection);
367 int this_branchs_first_point_in_full_polydata_point_list = 0;
368 for(
size_t i = 0; i < mBranches.size(); ++i)
372 if(!straightBranches)
373 this_branchs_first_point_in_full_polydata_point_list += mBranches[i-1]->getPositions().cols();
375 this_branchs_first_point_in_full_polydata_point_list += 2;
377 int parent_index_in_branch_list = mBranches[i]->findParentIndex(mBranches);
379 if(parent_index_in_branch_list > -1)
381 int parent_branch_last_point_in_full_polydata = 0;
382 for(
int j = 0; j <= parent_index_in_branch_list; ++j)
384 if(!straightBranches)
385 parent_branch_last_point_in_full_polydata += mBranches[j]->getPositions().cols() - 1;
387 parent_branch_last_point_in_full_polydata += (1 + j*2);
389 vtkIdType connection[2] = {parent_branch_last_point_in_full_polydata, this_branchs_first_point_in_full_polydata_point_list};
390 lines->InsertNextCell(2, connection);
396 retval->SetPoints(points);
397 retval->SetLines(lines);
402 Eigen::MatrixXd
sortMatrix(
int rowNumber, Eigen::MatrixXd matrix)
404 for (
int i = 0; i < matrix.cols() - 1; i++) {
405 for (
int j = i + 1; j < matrix.cols(); j++) {
406 if (matrix(rowNumber,i) > matrix(rowNumber,j)){
407 matrix.col(i).swap(matrix.col(j));
416 Eigen::MatrixXd
eraseCol(
int removeIndex, Eigen::MatrixXd positions)
418 positions.block(0 , removeIndex , positions.rows() , positions.cols() - removeIndex - 1) = positions.rightCols(positions.cols() - removeIndex - 1);
419 positions.conservativeResize(Eigen::NoChange, positions.cols() - 1);
423 std::pair<Eigen::MatrixXd::Index, double>
dsearch(Eigen::Vector3d p, Eigen::MatrixXd positions)
425 Eigen::MatrixXd::Index index;
427 (positions.colwise() - p).colwise().squaredNorm().minCoeff(&index);
428 double d = (positions.col(index) - p).norm();
430 return std::make_pair(index , d);
433 std::pair<std::vector<Eigen::MatrixXd::Index>, Eigen::VectorXd >
dsearchn(Eigen::MatrixXd p1, Eigen::MatrixXd p2)
435 Eigen::MatrixXd::Index index;
436 std::vector<Eigen::MatrixXd::Index> indexVector;
437 Eigen::VectorXd D(p1.cols());
438 for (
int i = 0; i < p1.cols(); i++)
441 (p2.colwise() - p1.col(i)).colwise().squaredNorm().minCoeff(&index);
442 D(i) = (p2.col(index) - p1.col(i)).norm();
443 indexVector.push_back(index);
445 return std::make_pair(indexVector , D);
451 Eigen::MatrixXd thisPosition(3,1);
452 std::vector<Eigen::MatrixXd> branchPositionsVector;
453 thisPosition = positionsNotUsed.col(startIndex);
454 branchPositionsVector.push_back(thisPosition);
455 positionsNotUsed =
eraseCol(startIndex,positionsNotUsed);;
457 while (positionsNotUsed.cols() > 0)
459 std::pair<Eigen::MatrixXd::Index, double > minDistance =
dsearch(thisPosition, positionsNotUsed);
460 Eigen::MatrixXd::Index index = minDistance.first;
461 double d = minDistance.second;
465 thisPosition = positionsNotUsed.col(index);
466 positionsNotUsed =
eraseCol(index,positionsNotUsed);
468 branchPositionsVector.push_back(thisPosition);
472 Eigen::MatrixXd branchPositions(3,branchPositionsVector.size());
474 for (
int j = 0; j < branchPositionsVector.size(); j++)
476 branchPositions.block(0,j,3,1) = branchPositionsVector[j];
479 return std::make_pair(branchPositions, positionsNotUsed);
Vector3D ceil(const Vector3D &a)
std::pair< Eigen::MatrixXd, Eigen::MatrixXd > findConnectedPointsInCT(int startIndex, Eigen::MatrixXd positionsNotUsed)
void interpolateBranchPositions(int interpolationFactor)
vtkSmartPointer< class vtkCellArray > vtkCellArrayPtr
boost::shared_ptr< class BranchList > BranchListPtr
BranchListPtr removePositionsForLocalRegistration(Eigen::MatrixXd trackingPositions, double maxDistance)
vtkSmartPointer< vtkPoints > vtkPointsPtr
void addBranch(BranchPtr b)
boost::shared_ptr< class Branch > BranchPtr
vtkSmartPointer< class vtkCardinalSpline > vtkCardinalSplinePtr
vtkSmartPointer< class vtkCardinalSpline > vtkCardinalSplinePtr
vtkPolyDataPtr createVtkPolyDataFromBranches(bool fullyConnected=false, bool straightBranches=false) const
BranchList::createVtkPolyDataFromBranches Return a VtkPolyData object created from the branches in th...
std::pair< Eigen::MatrixXd::Index, double > dsearch(Eigen::Vector3d p, Eigen::MatrixXd positions)
void calculateOrientations()
void smoothBranchPositions(int controlPointDistance)
std::vector< BranchPtr > getBranches()
void deleteBranch(BranchPtr b)
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
void findBranchesInCenterline(Eigen::MatrixXd positions_r)
class org_custusx_registration_method_bronchoscopy_EXPORT Branch
std::pair< std::vector< Eigen::MatrixXd::Index >, Eigen::VectorXd > dsearchn(Eigen::MatrixXd p1, Eigen::MatrixXd p2)
Eigen::MatrixXd eraseCol(int removeIndex, Eigen::MatrixXd positions)
void selectGenerations(int maxGeneration)
Eigen::MatrixXd sortMatrix(int rowNumber, Eigen::MatrixXd matrix)
void smoothOrientations()
std::vector< BranchPtr > branchVector
Namespace for all CustusX production code.