Line data Source code
1 : #include "DrivingDirectionTraversal.h" 2 : 3 : namespace elsa 4 : { 5 : 6 : template <int dim> 7 : DrivingDirectionTraversal<dim>::DrivingDirectionTraversal(const BoundingBox& aabb, 8 : const RealRay_t& r) 9 173740 : { 10 173740 : static_assert(dim == 2 || dim == 3); 11 : 12 : // --> calculate intersection parameter and if the volume is hit 13 173740 : auto opt = intersectRay(aabb, r); 14 : 15 173740 : r.direction().cwiseAbs().maxCoeff(&_drivingDirection); 16 : 17 173740 : if (opt) { 18 : 19 : // --> get points at which they intersect 20 157359 : _currentPos = r.pointAt(opt->_tmin); 21 : 22 157359 : RealArray_t<dim> aabbMin = aabb.min(); 23 157359 : RealArray_t<dim> aabbMax = aabb.max(); 24 : 25 : // --> because of floating point error it can happen, that values are out of 26 : // the bounding box, this can lead to errors 27 157359 : _currentPos = (_currentPos < aabbMin).select(aabbMin, _currentPos); 28 157359 : _currentPos = (_currentPos > aabbMax).select(aabbMax, _currentPos); 29 : 30 157359 : RealArray_t<dim> exitPoint = r.pointAt(opt->_tmax); 31 : 32 157359 : exitPoint = (exitPoint < aabbMin).select(aabbMin, exitPoint); 33 157359 : exitPoint = (exitPoint > aabbMax).select(aabbMax, exitPoint); 34 : 35 : // the step is 1 in driving direction so that we step in increments of 1 along the grid 36 : // the other directions are set so that we step along the course of the ray 37 157359 : _nextStep = r.direction() / abs(r.direction()[_drivingDirection]); 38 : 39 : // if the entryPoint is before .5 in driving direction, move _currentPos forward along 40 : // the ray to .5, otherwise move backward along the ray to .5 41 157359 : real_t dist = _currentPos(_drivingDirection) - floor(_currentPos(_drivingDirection)); 42 157359 : _currentPos += _nextStep * (0.5f - dist) * _nextStep.sign()(_drivingDirection); 43 : 44 : // this is to make sure that along _drivingDirection, _currentPos is exactly .5 after 45 : // the decimal point 46 157359 : _currentPos(_drivingDirection) = floor(_currentPos(_drivingDirection)) + 0.5f; 47 : 48 : // number of steps is the distance between exit and entry along the driving direction 49 157359 : _numSteps = static_cast<index_t>( 50 157359 : ceil(abs(exitPoint(_drivingDirection) - _currentPos(_drivingDirection)))); 51 : 52 157359 : _intersectionLength = _nextStep.matrix().norm(); 53 157359 : } else { 54 16381 : _stepCount = _numSteps; 55 16381 : return; 56 16381 : } 57 173740 : } 58 : 59 : template <int dim> 60 : void DrivingDirectionTraversal<dim>::updateTraverse() 61 1847255 : { 62 1847255 : _currentPos += _nextStep; 63 1847255 : _stepCount++; 64 1847255 : } 65 : 66 : template <int dim> 67 : IndexArray_t<dim> DrivingDirectionTraversal<dim>::getCurrentVoxel() const 68 1287 : { 69 1287 : return (_currentPos).floor().template cast<index_t>(); 70 1287 : } 71 : 72 : template <int dim> 73 : IndexArray_t<dim> DrivingDirectionTraversal<dim>::getCurrentVoxelFloor() const 74 1811345 : { 75 1811345 : return (_currentPos - 0.5f).floor().template cast<index_t>(); 76 1811345 : } 77 : 78 : template <int dim> 79 : IndexArray_t<dim> DrivingDirectionTraversal<dim>::getCurrentVoxelCeil() const 80 1807637 : { 81 1807637 : return (_currentPos - 0.5f).ceil().template cast<index_t>(); 82 1807637 : } 83 : 84 : template <int dim> 85 : index_t DrivingDirectionTraversal<dim>::getDrivingDirection() const 86 172611 : { 87 172611 : return _drivingDirection; 88 172611 : } 89 : 90 : template <int dim> 91 : bool DrivingDirectionTraversal<dim>::isInBoundingBox() const 92 1973831 : { 93 1973831 : return (_stepCount < _numSteps); 94 1973831 : } 95 : 96 : template <int dim> 97 : real_t DrivingDirectionTraversal<dim>::getIntersectionLength() const 98 172713 : { 99 172713 : return _intersectionLength; 100 172713 : } 101 : 102 : template <int dim> 103 : RealArray_t<dim> DrivingDirectionTraversal<dim>::getCurrentPos() const 104 1805935 : { 105 1805935 : return _currentPos; 106 1805935 : } 107 : 108 : template class DrivingDirectionTraversal<2>; 109 : template class DrivingDirectionTraversal<3>; 110 : } // namespace elsa