LCOV - code coverage report
Current view: top level - elsa/projectors - TraverseAABB.cpp (source / functions) Hit Total Coverage
Test: coverage-all.lcov Lines: 92 92 100.0 %
Date: 2024-05-16 04:22:26 Functions: 30 30 100.0 %

          Line data    Source code
       1             : #include "TraverseAABB.h"
       2             : #include "Intersection.h"
       3             : 
       4             : namespace elsa
       5             : {
       6             :     template <int dim>
       7             :     TraverseAABB<dim>::TraverseAABB(const BoundingBox& aabb, const RealRay_t& r,
       8             :                                     IndexArray_t<dim> productOfCoefficientsPerDimension)
       9             :         : _productOfCoefficientsPerDimension{std::move(productOfCoefficientsPerDimension)}
      10       61918 :     {
      11       61918 :         static_assert(dim == 2 || dim == 3);
      12       61918 :         _aabbMin = aabb.min();
      13       61918 :         _aabbMax = aabb.max();
      14             : 
      15             :         // compute the first intersection
      16       61918 :         const RealArray_t<dim> entryPoint = calculateAABBIntersections(r, aabb);
      17       61918 :         if (!isInBoundingBox()) // early abort if necessary
      18        5860 :             return;
      19             : 
      20             :         // constant array containing epsilon
      21       56058 :         const RealArray_t<dim> EPS{
      22       56058 :             RealArray_t<dim>().setConstant(std::numeric_limits<real_t>::epsilon())};
      23             : 
      24             :         // constant vector containing the maximum number
      25       56058 :         const RealArray_t<dim> MAX{
      26       56058 :             RealArray_t<dim>().setConstant(std::numeric_limits<real_t>::max())};
      27             : 
      28             :         // determine whether we go up/down or left/right
      29       56058 :         initStepDirection(r.direction());
      30             : 
      31             :         // select the initial voxel (closest to the entry point)
      32       56058 :         selectClosestVoxel(entryPoint);
      33             : 
      34             :         // initialize the step sizes for the step parameter
      35       56058 :         initDelta(r.direction(), EPS, MAX);
      36             : 
      37             :         // initialize the maximum step parameters
      38       56058 :         initT(r.direction(), EPS, MAX, entryPoint);
      39       56058 :         initCurrentIndex();
      40       56058 :     }
      41             : 
      42             :     template <int dim>
      43             :     void TraverseAABB<dim>::updateTraverse()
      44     2162517 :     {
      45             :         // --> calculate the mask that masks out all but the lowest t values
      46     2162517 :         calcMask();
      47             : 
      48             :         // --> step into the current direction
      49     2162517 :         _currentPos += _mask.select(_stepDirection.template cast<real_t>(), 0);
      50             : 
      51             :         // --> update the T for next iteration
      52     2162517 :         _T += _mask.select(_tDelta, 0);
      53             : 
      54             :         // --> check if we are still in bounding box
      55     2162517 :         _isInAABB = isCurrentPositionInAABB();
      56     2162517 :     }
      57             : 
      58             :     template <int dim>
      59             :     void TraverseAABB<dim>::calcMask()
      60     2063344 :     {
      61     2063344 :         if constexpr (dim == 2) {
      62       62639 :             _mask[0] = (_T[0] <= _T[1]);
      63       62639 :             _mask[1] = (_T[1] <= _T[0]);
      64       62829 :         } else if constexpr (dim == 3) {
      65       62829 :             _mask[0] = ((_T[0] <= _T[1]) && (_T[0] <= _T[2]));
      66       62829 :             _mask[1] = ((_T[1] <= _T[0]) && (_T[1] <= _T[2]));
      67       62829 :             _mask[2] = ((_T[2] <= _T[0]) && (_T[2] <= _T[1]));
      68       62829 :         }
      69     2063344 :     }
      70             : 
      71             :     template <int dim>
      72             :     real_t TraverseAABB<dim>::updateTraverseAndGetDistance()
      73     1706394 :     {
      74             :         // --> compute the distance
      75     1706394 :         real_t tEntry = _tExit;
      76     1706394 :         _tExit = _T.minCoeff();
      77             : 
      78             :         // --> do the update
      79     1706394 :         updateTraverse();
      80     1706394 :         updateCurrentIndex();
      81             : 
      82     1706394 :         return (_tExit - tEntry);
      83     1706394 :     }
      84             : 
      85             :     template <int dim>
      86             :     bool TraverseAABB<dim>::isInBoundingBox() const
      87     2393587 :     {
      88     2393587 :         return _isInAABB;
      89     2393587 :     }
      90             : 
      91             :     template <int dim>
      92             :     IndexArray_t<dim> TraverseAABB<dim>::getCurrentVoxel() const
      93      603188 :     {
      94      603188 :         return _currentPos.template cast<index_t>();
      95      603188 :     }
      96             : 
      97             :     template <int dim>
      98             :     index_t TraverseAABB<dim>::getCurrentIndex() const
      99     1855340 :     {
     100     1855340 :         return _currentIndex;
     101     1855340 :     }
     102             : 
     103             :     template <int dim>
     104             :     RealArray_t<dim> TraverseAABB<dim>::calculateAABBIntersections(const RealRay_t& r,
     105             :                                                                    const BoundingBox& aabb)
     106       61782 :     {
     107       61782 :         RealArray_t<dim> entryPoint;
     108             :         // entry and exit point parameters
     109       61782 :         real_t tmin;
     110             : 
     111             :         // --> calculate intersection parameter and if the volume is hit
     112       61782 :         auto opt = intersectRay(aabb, r);
     113             : 
     114       61782 :         if (opt) { // hit!
     115       56478 :             _isInAABB = true;
     116       56478 :             tmin = opt->_tmin;
     117             : 
     118             :             // --> get points at which they intersect
     119       56478 :             entryPoint = r.pointAt(tmin);
     120             : 
     121             :             // --> because of floating point error it can happen, that values are out of
     122             :             // the bounding box, this can lead to errors
     123       56478 :             entryPoint = (entryPoint < _aabbMin).select(_aabbMin, entryPoint);
     124       56478 :             entryPoint = (entryPoint > _aabbMax).select(_aabbMax, entryPoint);
     125       56478 :         }
     126       61782 :         return entryPoint;
     127       61782 :     }
     128             : 
     129             :     template <int dim>
     130             :     void TraverseAABB<dim>::initStepDirection(const RealArray_t<dim>& rd)
     131       56132 :     {
     132       56132 :         _stepDirection = rd.sign().template cast<index_t>();
     133       56132 :     }
     134             : 
     135             :     template <int dim>
     136             :     void TraverseAABB<dim>::selectClosestVoxel(const RealArray_t<dim>& entryPoint)
     137       56198 :     {
     138       56198 :         RealArray_t<dim> lowerCorner = entryPoint.floor();
     139       56198 :         lowerCorner = ((lowerCorner == entryPoint) && (_stepDirection < 0.0))
     140       56198 :                           .select(lowerCorner - 1, lowerCorner);
     141             : 
     142       56198 :         _currentPos = lowerCorner;
     143             : 
     144             :         // check if we are still inside the aabb
     145       56310 :         if ((_currentPos >= _aabbMax).any() || (_currentPos < _aabbMin).any())
     146          12 :             _isInAABB = false;
     147       56198 :     }
     148             : 
     149             :     template <int dim>
     150             :     void TraverseAABB<dim>::initDelta(const RealArray_t<dim>& rd, const RealArray_t<dim>& EPS,
     151             :                                       const RealArray_t<dim>& MAX)
     152       56189 :     {
     153       56189 :         RealArray_t<dim> tdelta = _stepDirection.template cast<real_t>() / rd;
     154             : 
     155       56189 :         _tDelta = (Eigen::abs(rd) > EPS).select(tdelta, MAX);
     156       56189 :     }
     157             : 
     158             :     template <int dim>
     159             :     void TraverseAABB<dim>::initT(const RealArray_t<dim>& rd, const RealArray_t<dim>& EPS,
     160             :                                   const RealArray_t<dim>& MAX, const RealArray_t<dim>& entryPoint)
     161       56155 :     {
     162       56155 :         RealArray_t<dim> T =
     163       56155 :             (((rd > 0.0f).select(_currentPos + 1., _currentPos)) - entryPoint) / rd;
     164             : 
     165       56155 :         _T = (Eigen::abs(rd) > EPS).select(T, MAX);
     166       56155 :     }
     167             : 
     168             :     template <int dim>
     169             :     bool TraverseAABB<dim>::isCurrentPositionInAABB() const
     170     2076288 :     {
     171     2189967 :         return (_currentPos < _aabbMax).all() && (_currentPos >= _aabbMin).all();
     172     2076288 :     }
     173             : 
     174             :     template <int dim>
     175             :     void TraverseAABB<dim>::updateCurrentIndex()
     176     1688160 :     {
     177     1688160 :         _currentIndex +=
     178     1688160 :             (_stepDirection * _mask.select(_productOfCoefficientsPerDimension, 0)).sum();
     179     1688160 :     }
     180             :     template <int dim>
     181             :     void TraverseAABB<dim>::initCurrentIndex()
     182       56215 :     {
     183       56215 :         _currentIndex = (_productOfCoefficientsPerDimension * getCurrentVoxel()).sum();
     184       56215 :     }
     185             : 
     186             :     template class TraverseAABB<2>;
     187             :     template class TraverseAABB<3>;
     188             : } // namespace elsa

Generated by: LCOV version 1.14