ray_box_intersect.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "ray_box_intersect.h"
  2. template <
  3. typename Derivedsource,
  4. typename Deriveddir,
  5. typename Scalar>
  6. IGL_INLINE bool igl::ray_box_intersect(
  7. const Eigen::PlainObjectBase<Derivedsource> & origin,
  8. const Eigen::PlainObjectBase<Deriveddir> & dir,
  9. const Eigen::AlignedBox<Scalar,3> & box,
  10. const Scalar & t0,
  11. const Scalar & t1,
  12. Scalar & tmin,
  13. Scalar & tmax)
  14. {
  15. #ifdef false
  16. // https://github.com/RMonica/basic_next_best_view/blob/master/src/RayTracer.cpp
  17. const auto & intersectRayBox = [](
  18. const Eigen::Vector3f& rayo,
  19. const Eigen::Vector3f& rayd,
  20. const Eigen::Vector3f& bmin,
  21. const Eigen::Vector3f& bmax,
  22. float & tnear,
  23. float & tfar
  24. )->bool
  25. {
  26. Eigen::Vector3f bnear;
  27. Eigen::Vector3f bfar;
  28. // Checks for intersection testing on each direction coordinate
  29. // Computes
  30. float t1, t2;
  31. tnear = -1e+6f, tfar = 1e+6f; //, tCube;
  32. bool intersectFlag = true;
  33. for (int i = 0; i < 3; ++i) {
  34. // std::cout << "coordinate " << i << ": bmin " << bmin(i) << ", bmax " << bmax(i) << std::endl;
  35. assert(bmin(i) <= bmax(i));
  36. if (::fabs(rayd(i)) < 1e-6) { // Ray parallel to axis i-th
  37. if (rayo(i) < bmin(i) || rayo(i) > bmax(i)) {
  38. intersectFlag = false;
  39. }
  40. }
  41. else {
  42. // Finds the nearest and the farthest vertices of the box from the ray origin
  43. if (::fabs(bmin(i) - rayo(i)) < ::fabs(bmax(i) - rayo(i))) {
  44. bnear(i) = bmin(i);
  45. bfar(i) = bmax(i);
  46. }
  47. else {
  48. bnear(i) = bmax(i);
  49. bfar(i) = bmin(i);
  50. }
  51. // std::cout << " bnear " << bnear(i) << ", bfar " << bfar(i) << std::endl;
  52. // Finds the distance parameters t1 and t2 of the two ray-box intersections:
  53. // t1 must be the closest to the ray origin rayo.
  54. t1 = (bnear(i) - rayo(i)) / rayd(i);
  55. t2 = (bfar(i) - rayo(i)) / rayd(i);
  56. if (t1 > t2) {
  57. std::swap(t1,t2);
  58. }
  59. // The two intersection values are used to saturate tnear and tfar
  60. if (t1 > tnear) {
  61. tnear = t1;
  62. }
  63. if (t2 < tfar) {
  64. tfar = t2;
  65. }
  66. // std::cout << " t1 " << t1 << ", t2 " << t2 << ", tnear " << tnear << ", tfar " << tfar
  67. // << " tnear > tfar? " << (tnear > tfar) << ", tfar < 0? " << (tfar < 0) << std::endl;
  68. if(tnear > tfar) {
  69. intersectFlag = false;
  70. }
  71. if(tfar < 0) {
  72. intersectFlag = false;
  73. }
  74. }
  75. }
  76. // Checks whether intersection occurs or not
  77. return intersectFlag;
  78. };
  79. float tmin_f, tmax_f;
  80. bool ret = intersectRayBox(
  81. origin. template cast<float>(),
  82. dir. template cast<float>(),
  83. box.min().template cast<float>(),
  84. box.max().template cast<float>(),
  85. tmin_f,
  86. tmax_f);
  87. tmin = tmin_f;
  88. tmax = tmax_f;
  89. return ret;
  90. #else
  91. using namespace Eigen;
  92. // This should be precomputed and provided as input
  93. typedef Matrix<Scalar,1,3> RowVector3S;
  94. const RowVector3S inv_dir( 1./dir(0),1./dir(1),1./dir(2));
  95. const std::vector<bool> sign = { inv_dir(0)<0, inv_dir(1)<0, inv_dir(2)<0};
  96. // http://people.csail.mit.edu/amy/papers/box-jgt.pdf
  97. // "An Efficient and Robust Ray–Box Intersection Algorithm"
  98. Scalar tymin, tymax, tzmin, tzmax;
  99. std::vector<RowVector3S> bounds = {box.min(),box.max()};
  100. tmin = ( bounds[sign[0]](0) - origin(0)) * inv_dir(0);
  101. tmax = ( bounds[1-sign[0]](0) - origin(0)) * inv_dir(0);
  102. tymin = (bounds[sign[1]](1) - origin(1)) * inv_dir(1);
  103. tymax = (bounds[1-sign[1]](1) - origin(1)) * inv_dir(1);
  104. if ( (tmin > tymax) || (tymin > tmax) )
  105. {
  106. return false;
  107. }
  108. if (tymin > tmin)
  109. {
  110. tmin = tymin;
  111. }
  112. if (tymax < tmax)
  113. {
  114. tmax = tymax;
  115. }
  116. tzmin = (bounds[sign[2]](2) - origin(2)) * inv_dir(2);
  117. tzmax = (bounds[1-sign[2]](2) - origin(2)) * inv_dir(2);
  118. if ( (tmin > tzmax) || (tzmin > tmax) )
  119. {
  120. return false;
  121. }
  122. if (tzmin > tmin)
  123. {
  124. tmin = tzmin;
  125. }
  126. if (tzmax < tmax)
  127. {
  128. tmax = tzmax;
  129. }
  130. if(!( (tmin < t1) && (tmax > t0) ))
  131. {
  132. return false;
  133. }
  134. return true;
  135. #endif
  136. }