15 #include <vtkPolyData.h>
16 #include <vtkCardinalSpline.h>
18 #include <boost/math/special_functions/fpclassify.hpp>
39 mBranches.push_back(b);
44 if(b->getParentBranch())
45 b->getParentBranch()->deleteChildBranches();
47 for(
int i = 0; i < mBranches.size(); i++ )
49 if (b == mBranches[i])
51 mBranches.erase(mBranches.begin() + i);
69 std::vector<int> branchNumbersToBeDeleted;
70 for(
int i = 0; i < mBranches.size(); i++ )
72 int generationCounter = 1;
74 while (currentBranch->getParentBranch()){
76 currentBranch = currentBranch->getParentBranch();
77 if (generationCounter > maxGeneration)
79 branchNumbersToBeDeleted.push_back(i);
86 for (
int i = branchNumbersToBeDeleted.size() - 1; i >= 0; i-- )
100 if(!branch->getParentBranch())
101 branch->setBronchoscopeRotation(0);
104 double parentRotation = branch->getParentBranch()->getBronchoscopeRotation();
105 Eigen::MatrixXd branchOrientations = branch->getOrientations();
106 Vector3D branchOrientationStart = branchOrientations.leftCols(std::min(25, (
int) branchOrientations.cols())).rowwise().mean();
107 Eigen::MatrixXd parentBranchOrientations = branch->getParentBranch()->getOrientations();
108 Vector3D parentBranchOrientationEnd = parentBranchOrientations.rightCols(std::min(50, (
int) parentBranchOrientations.cols())).rowwise().mean();
113 branch->setBronchoscopeRotation(bronchoscopeRotation);
116 branchVector childBranches = branch->getChildBranches();
117 for(
int i=0; i<childBranches.size(); i++)
123 double bronchoscopeRotation;
126 Vector3D up =
cross(parentBranchOrientation, xVector).normalized();
130 bronchoscopeRotation = acos( up.dot(bendingDirection) );
134 if(parentBranchOrientation.dot(N) < 0)
135 bronchoscopeRotation = -bronchoscopeRotation;
137 double rotationDifferenceFromParent = bronchoscopeRotation - parentRotation;
140 if ( rotationDifferenceFromParent >
M_PI )
141 rotationDifferenceFromParent = rotationDifferenceFromParent - 2*
M_PI;
142 else if ( rotationDifferenceFromParent < -
M_PI )
143 rotationDifferenceFromParent = rotationDifferenceFromParent + 2*
M_PI;
147 bronchoscopeRotation = bronchoscopeRotation +
M_PI;
149 bronchoscopeRotation = bronchoscopeRotation -
M_PI;
152 rotationDifferenceFromParent = bronchoscopeRotation - parentRotation;
153 if( rotationDifferenceFromParent >
M_PI )
154 bronchoscopeRotation = bronchoscopeRotation -
M_PI;
155 else if( rotationDifferenceFromParent < -
M_PI )
156 bronchoscopeRotation = bronchoscopeRotation +
M_PI;
162 return bronchoscopeRotation;
174 C(1) = - ( ( N(2) - N(0)*A(2)/A(0) ) / ( N(1) - N(0)*A(1)/A(0) ) ) * C(2);
175 if(boost::math::isnan(C(1)) || boost::math::isinf(C(1)))
180 C(0) = - ( A(1)*C(1) + A(2)*C(2) ) / A(0);
192 for (
int i = 0; i < mBranches.size(); i++)
194 Eigen::MatrixXd orientations = mBranches[i]->getOrientations();
195 Eigen::MatrixXd newOrientations(orientations.rows(),orientations.cols());
196 int numberOfColumns = orientations.cols();
197 for (
int j = 0; j < numberOfColumns; j++)
199 newOrientations.col(j) = orientations.block(0,std::max(j-2,0),orientations.rows(),std::min(5,numberOfColumns-j)).rowwise().mean();
200 newOrientations.col(j) = newOrientations.col(j) / newOrientations.col(j).norm();
202 mBranches[i]->setOrientations(newOrientations);
208 for (
int i = 0; i < mBranches.size(); i++)
210 Eigen::VectorXd radius = mBranches[i]->getRadius();
211 Eigen::VectorXd newRadius(radius.rows(),radius.cols());
212 int numberOfPoints = radius.size();
213 for (
int j = 0; j < numberOfPoints; j++)
215 newRadius[j] = radius.segment(std::max(j-2,0), std::min(5,numberOfPoints-j)).mean();
217 mBranches[i]->setRadius(newRadius);
223 BranchPtr branchWithLargestRadius = mBranches[0];
224 double largestRadius = mBranches[0]->getAverageRadius();
226 for (
int i = 1; i < mBranches.size(); i++)
228 if (mBranches[i]->getAverageRadius() > largestRadius)
230 largestRadius = mBranches[i]->getAverageRadius();
231 branchWithLargestRadius = mBranches[i];
235 return branchWithLargestRadius;
241 for (
int i = 0; i < mBranches.size(); i++)
243 Eigen::MatrixXd positions = mBranches[i]->getPositions();
245 if (mBranches[i]->getParentBranch())
247 Eigen::MatrixXd parentPositions = mBranches[i]->getParentBranch()->getPositions();
248 Eigen::MatrixXd positionsResized(positions.rows(), positions.cols()+1);
249 positionsResized.col(0) = parentPositions.rightCols(1);
250 positionsResized.rightCols(positions.cols()) = positions;
251 positions = positionsResized;
254 std::vector<Eigen::Vector3d> interpolatedPositions;
255 for (
int j = 0; j < positions.cols()-1; j++)
257 int interpolationFactor =
static_cast<int>(
std::ceil( ( positions.col(j+1) - positions.col(j) ).norm() / resolution ));
258 for (
int k = 0; k < interpolationFactor; k++){
259 Eigen::Vector3d interpolationPoint;
260 interpolationPoint[0] = (positions(0,j)*(interpolationFactor-k) + positions(0,j+1)*(k) ) / interpolationFactor;
261 interpolationPoint[1] = (positions(1,j)*(interpolationFactor-k) + positions(1,j+1)*(k) ) / interpolationFactor;
262 interpolationPoint[2] = (positions(2,j)*(interpolationFactor-k) + positions(2,j+1)*(k) ) / interpolationFactor;
263 interpolatedPositions.push_back(interpolationPoint);
266 if (mBranches[i]->getParentBranch())
268 Eigen::MatrixXd positionsResized(positions.rows(), positions.cols()-1);
269 positionsResized = positions.rightCols(positionsResized.cols());
270 positions = positionsResized;
272 Eigen::MatrixXd interpolationResult(3 , interpolatedPositions.size());
273 for (
int j = 0; j < interpolatedPositions.size(); j++)
275 interpolationResult(0,j) = interpolatedPositions[j](0);
276 interpolationResult(1,j) = interpolatedPositions[j](1);
277 interpolationResult(2,j) = interpolatedPositions[j](2);
279 mBranches[i]->setPositions(interpolationResult);
286 for (
int i = 0; i < mBranches.size(); i++)
288 Eigen::MatrixXd positions = mBranches[i]->getPositions();
289 int numberOfInputPoints = positions.cols();
292 double branchLength = (positions.rightCols(1) - positions.leftCols(1)).norm();
293 int numberOfControlPoints =
std::ceil(branchLength/controlPointDistance);
294 numberOfControlPoints = std::max(numberOfControlPoints, 2);
301 for(
int j=0; j<numberOfControlPoints; j++)
303 int indexP = (j*numberOfInputPoints)/numberOfControlPoints;
305 splineX->AddPoint(indexP,positions(0,indexP));
306 splineY->AddPoint(indexP,positions(1,indexP));
307 splineZ->AddPoint(indexP,positions(2,indexP));
310 splineX->AddPoint(numberOfInputPoints-1,positions(0,numberOfInputPoints-1));
311 splineY->AddPoint(numberOfInputPoints-1,positions(1,numberOfInputPoints-1));
312 splineZ->AddPoint(numberOfInputPoints-1,positions(2,numberOfInputPoints-1));
315 Eigen::MatrixXd smoothingResult(3 , numberOfInputPoints);
316 for(
int j=0; j<numberOfInputPoints; j++)
318 double splineParameter = j;
319 smoothingResult(0,j) = splineX->Evaluate(splineParameter);
320 smoothingResult(1,j) = splineY->Evaluate(splineParameter);
321 smoothingResult(2,j) = splineZ->Evaluate(splineParameter);
323 mBranches[i]->setPositions(smoothingResult);
332 Eigen::MatrixXd positionsNotUsed_r = positions_r;
337 Eigen::MatrixXd::Index startIndex;
339 while (positionsNotUsed_r.cols() > 0)
341 if (!mBranches.empty())
343 double minDistance = 1000;
344 for (
int i = 0; i < mBranches.size(); i++)
346 std::pair<std::vector<Eigen::MatrixXd::Index>, Eigen::VectorXd> distances;
347 distances =
dsearchn(positionsNotUsed_r, mBranches[i]->getPositions());
348 double d = distances.second.minCoeff(&index);
352 branchToSplit = mBranches[i];
358 std::pair<Eigen::MatrixXd::Index, double> dsearchResult =
dsearch(positionsNotUsed_r.col(startIndex) , branchToSplit->getPositions());
359 splitIndex = dsearchResult.first;
362 startIndex = positionsNotUsed_r.cols() - 1;
364 std::pair<Eigen::MatrixXd,Eigen::MatrixXd > connectedPointsResult =
findConnectedPointsInCT(startIndex , positionsNotUsed_r);
365 Eigen::MatrixXd branchPositions = connectedPointsResult.first;
366 positionsNotUsed_r = connectedPointsResult.second;
368 if (branchPositions.cols() >= 5)
371 newBranch->setPositions(branchPositions);
372 mBranches.push_back(newBranch);
374 if (mBranches.size() > 1)
376 if ((splitIndex + 1 >= 5) && (branchToSplit->getPositions().cols() - splitIndex - 1 >= 5))
382 Eigen::MatrixXd branchToSplitPositions = branchToSplit->getPositions();
383 newBranchFromSplit->setPositions(branchToSplitPositions.rightCols(branchToSplitPositions.cols() - splitIndex - 1));
384 branchToSplit->setPositions(branchToSplitPositions.leftCols(splitIndex + 1));
385 mBranches.push_back(newBranchFromSplit);
386 newBranchFromSplit->setParentBranch(branchToSplit);
387 newBranch->setParentBranch(branchToSplit);
388 newBranchFromSplit->setChildBranches(branchToSplit->getChildBranches());
389 branchVector branchToSplitChildren = branchToSplit->getChildBranches();
390 for (
int i = 0; i < branchToSplitChildren.size(); i++)
391 branchToSplitChildren[i]->setParentBranch(newBranchFromSplit);
392 branchToSplit->deleteChildBranches();
393 branchToSplit->addChildBranch(newBranchFromSplit);
394 branchToSplit->addChildBranch(newBranch);
396 else if (splitIndex + 1 < 5)
401 newBranch->setParentBranch(branchToSplit->getParentBranch());
402 if(branchToSplit->getParentBranch())
403 branchToSplit->getParentBranch()->addChildBranch(newBranch);
405 else if (branchToSplit->getPositions().cols() - splitIndex - 1 < 5)
409 newBranch->setParentBranch(branchToSplit);
410 branchToSplit->addChildBranch(newBranch);
423 for (
int i = 0; i < mBranches.size(); i++)
426 b->setPositions(mBranches[i]->getPositions());
427 retval->addBranch(b);
430 std::vector<BranchPtr> branches = retval->getBranches();
431 Eigen::MatrixXd positions;
432 Eigen::MatrixXd orientations;
433 for (
int i = 0; i < branches.size(); i++)
435 positions = branches[i]->getPositions();
436 orientations = branches[i]->getOrientations();
437 std::pair<std::vector<Eigen::MatrixXd::Index>, Eigen::VectorXd> distanceData;
438 distanceData =
dsearchn(positions, trackingPositions);
439 Eigen::VectorXd distance = distanceData.second;
440 for (
int j = positions.cols() - 1; j >= 0; j--)
442 if (distance(j) > maxDistance)
445 orientations =
eraseCol(j, orientations);
448 branches[i]->setPositions(positions);
458 Eigen::MatrixXd positions =
branchVector[i]->getPositions();
459 Eigen::MatrixXd orientations =
branchVector[i]->getOrientations();
461 for (
int i = positions.cols()-2; i > 0; i--){
462 double distance = (positions.col(i) - positions.col(i+1)).norm();
463 if ( distance < minPointDistance )
466 orientations =
eraseCol(i,orientations);
483 branch->setLap(name);
485 for(
int i=0; i<bv.size(); i++)
492 return branch->getLap();
497 double d0 = p1(0) - p2(0);
498 double d1 = p1(1) - p2(1);
499 double d2 = p1(2) - p2(2);
501 double D = sqrt( d0*d0 + d1*d1 + d2*d2 );
508 double minDistance = 100000;
510 for (
int i = 0; i < mBranches.size(); i++)
512 Eigen::MatrixXd positions = mBranches[i]->getPositions();
513 for (
int j = 0; j < positions.cols(); j++)
515 double D =
findDistance(positions.col(j), targetCoordinate_r);
519 minDistanceBranch = mBranches[i];
524 return minDistanceBranch;
551 int positionCounter = 0;
552 for (
size_t i = 0; i < mBranches.size(); ++i)
554 Eigen::MatrixXd positions = mBranches[i]->getPositions();
558 points->InsertNextPoint(positions(0,0),positions(1,0),positions(2,0));
559 points->InsertNextPoint(positions(0,positions.cols()-1),positions(1,positions.cols()-1),positions(2,positions.cols()-1));
560 vtkIdType connection[2] = {positionCounter-1, positionCounter};
561 lines->InsertNextCell(2, connection);
566 for (
int j = 0; j < positions.cols(); ++j)
569 points->InsertNextPoint(positions(0,j),positions(1,j),positions(2,j));
570 if (j < positions.cols()-1)
572 vtkIdType connection[2] = {positionCounter-1, positionCounter};
573 lines->InsertNextCell(2, connection);
580 int this_branchs_first_point_in_full_polydata_point_list = 0;
581 for(
size_t i = 0; i < mBranches.size(); ++i)
585 if(!straightBranches)
586 this_branchs_first_point_in_full_polydata_point_list += mBranches[i-1]->getPositions().cols();
588 this_branchs_first_point_in_full_polydata_point_list += 2;
590 int parent_index_in_branch_list = mBranches[i]->findParentIndex(mBranches);
592 if(parent_index_in_branch_list > -1)
594 int parent_branch_last_point_in_full_polydata = 0;
595 for(
int j = 0; j <= parent_index_in_branch_list; ++j)
597 if(!straightBranches)
598 parent_branch_last_point_in_full_polydata += mBranches[j]->getPositions().cols() - 1;
600 parent_branch_last_point_in_full_polydata += (1 + j*2);
602 vtkIdType connection[2] = {parent_branch_last_point_in_full_polydata, this_branchs_first_point_in_full_polydata_point_list};
603 lines->InsertNextCell(2, connection);
609 retval->SetPoints(points);
610 retval->SetLines(lines);
615 Eigen::MatrixXd
sortMatrix(
int rowNumber, Eigen::MatrixXd matrix)
617 for (
int i = 0; i < matrix.cols() - 1; i++) {
618 for (
int j = i + 1; j < matrix.cols(); j++) {
619 if (matrix(rowNumber,i) > matrix(rowNumber,j)){
620 matrix.col(i).swap(matrix.col(j));
629 Eigen::MatrixXd
eraseCol(
int removeIndex, Eigen::MatrixXd positions)
631 positions.block(0 , removeIndex , positions.rows() , positions.cols() - removeIndex - 1) = positions.rightCols(positions.cols() - removeIndex - 1);
632 positions.conservativeResize(Eigen::NoChange, positions.cols() - 1);
636 std::pair<Eigen::MatrixXd::Index, double>
dsearch(Eigen::Vector3d p, Eigen::MatrixXd positions)
638 Eigen::MatrixXd::Index index;
640 (positions.colwise() - p).colwise().squaredNorm().minCoeff(&index);
641 double d = (positions.col(index) - p).norm();
643 return std::make_pair(index , d);
646 std::pair<std::vector<Eigen::MatrixXd::Index>, Eigen::VectorXd >
dsearchn(Eigen::MatrixXd p1, Eigen::MatrixXd p2)
648 Eigen::MatrixXd::Index index;
649 std::vector<Eigen::MatrixXd::Index> indexVector;
650 Eigen::VectorXd D(p1.cols());
651 for (
int i = 0; i < p1.cols(); i++)
654 (p2.colwise() - p1.col(i)).colwise().squaredNorm().minCoeff(&index);
655 D(i) = (p2.col(index) - p1.col(i)).norm();
656 indexVector.push_back(index);
658 return std::make_pair(indexVector , D);
664 Eigen::MatrixXd thisPosition(3,1);
665 std::vector<Eigen::MatrixXd> branchPositionsVector;
666 thisPosition = positionsNotUsed.col(startIndex);
667 branchPositionsVector.push_back(thisPosition);
668 positionsNotUsed =
eraseCol(startIndex,positionsNotUsed);;
670 while (positionsNotUsed.cols() > 0)
672 std::pair<Eigen::MatrixXd::Index, double > minDistance =
dsearch(thisPosition, positionsNotUsed);
673 Eigen::MatrixXd::Index index = minDistance.first;
674 double d = minDistance.second;
678 thisPosition = positionsNotUsed.col(index);
679 positionsNotUsed =
eraseCol(index,positionsNotUsed);
681 branchPositionsVector.push_back(thisPosition);
685 Eigen::MatrixXd branchPositions(3,branchPositionsVector.size());
687 for (
int j = 0; j < branchPositionsVector.size(); j++)
689 branchPositions.block(0,j,3,1) = branchPositionsVector[j];
692 return std::make_pair(branchPositions, positionsNotUsed);
711 double branchRadius = branchPtr->findBranchRadius()/2;
716 Eigen::MatrixXd positions = branchPtr->getPositions();
717 splineX->AddPoint(0,startPosition(0));
718 splineY->AddPoint(0,startPosition(1));
719 splineZ->AddPoint(0,startPosition(2));
724 if(!branchPtr->getParentBranch())
726 splineX->AddPoint(startIndex,positions(0,0));
727 splineY->AddPoint(startIndex,positions(1,0));
728 splineZ->AddPoint(startIndex,positions(2,0));
732 Eigen::MatrixXd parentPositions = branchPtr->getParentBranch()->getPositions();
733 splineX->AddPoint(startIndex,parentPositions(0,parentPositions.cols()-1));
734 splineY->AddPoint(startIndex,parentPositions(1,parentPositions.cols()-1));
735 splineZ->AddPoint(startIndex,parentPositions(2,parentPositions.cols()-1));
740 double maxAcceptedDistanceToOriginalPosition = std::max(branchRadius - 1, 1.0);
741 double maxDistanceToOriginalPosition = maxAcceptedDistanceToOriginalPosition + 1;
742 int maxDistanceIndex = -1;
743 std::vector< Eigen::Vector3d > smoothingResult;
746 while (maxDistanceToOriginalPosition >= maxAcceptedDistanceToOriginalPosition && splineX->GetNumberOfPoints() < startIndex)
748 if(maxDistanceIndex > 0)
751 splineX->AddPoint(maxDistanceIndex,positions(0,startIndex - maxDistanceIndex));
752 splineY->AddPoint(maxDistanceIndex,positions(1,startIndex - maxDistanceIndex));
753 splineZ->AddPoint(maxDistanceIndex,positions(2,startIndex - maxDistanceIndex));
757 maxDistanceToOriginalPosition = 0.0;
758 smoothingResult.clear();
759 for(
int j=0; j<=startIndex; j++)
761 double splineParameter = j;
762 Eigen::Vector3d tempPoint;
763 tempPoint(0) = splineX->Evaluate(splineParameter);
764 tempPoint(1) = splineY->Evaluate(splineParameter);
765 tempPoint(2) = splineZ->Evaluate(splineParameter);
766 smoothingResult.push_back(tempPoint);
769 double distance =
dsearch(tempPoint, positions).second;
771 if (distance > maxDistanceToOriginalPosition)
773 maxDistanceToOriginalPosition = distance;
774 maxDistanceIndex = j;
779 return smoothingResult;