IntrLine3AlignedBox3.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/FIQuery.h>
  9. #include <Mathematics/TIQuery.h>
  10. #include <Mathematics/AlignedBox.h>
  11. #include <Mathematics/Line.h>
  12. #include <Mathematics/Vector3.h>
  13. // The test-intersection queries use the method of separating axes.
  14. // https://www.geometrictools.com/Documentation/MethodOfSeparatingAxes.pdf
  15. // The find-intersection queries use parametric clipping against the six
  16. // faces of the box. The find-intersection queries use Liang-Barsky
  17. // clipping. The queries consider the box to be a solid. The algorithms
  18. // are described in
  19. // https://www.geometrictools.com/Documentation/IntersectionLineBox.pdf
  20. namespace WwiseGTE
  21. {
  22. template <typename Real>
  23. class TIQuery<Real, Line3<Real>, AlignedBox3<Real>>
  24. {
  25. public:
  26. struct Result
  27. {
  28. bool intersect;
  29. };
  30. Result operator()(Line3<Real> const& line, AlignedBox3<Real> const& box)
  31. {
  32. // Get the centered form of the aligned box. The axes are
  33. // implicitly Axis[d] = Vector3<Real>::Unit(d).
  34. Vector3<Real> boxCenter, boxExtent;
  35. box.GetCenteredForm(boxCenter, boxExtent);
  36. // Transform the line to the aligned-box coordinate system.
  37. Vector3<Real> lineOrigin = line.origin - boxCenter;
  38. Result result;
  39. DoQuery(lineOrigin, line.direction, boxExtent, result);
  40. return result;
  41. }
  42. protected:
  43. void DoQuery(Vector3<Real> const& lineOrigin, Vector3<Real> const& lineDirection,
  44. Vector3<Real> const& boxExtent, Result& result)
  45. {
  46. Vector3<Real> WxD = Cross(lineDirection, lineOrigin);
  47. Real absWdU[3] =
  48. {
  49. std::fabs(lineDirection[0]),
  50. std::fabs(lineDirection[1]),
  51. std::fabs(lineDirection[2])
  52. };
  53. if (std::fabs(WxD[0]) > boxExtent[1] * absWdU[2] + boxExtent[2] * absWdU[1])
  54. {
  55. result.intersect = false;
  56. return;
  57. }
  58. if (std::fabs(WxD[1]) > boxExtent[0] * absWdU[2] + boxExtent[2] * absWdU[0])
  59. {
  60. result.intersect = false;
  61. return;
  62. }
  63. if (std::fabs(WxD[2]) > boxExtent[0] * absWdU[1] + boxExtent[1] * absWdU[0])
  64. {
  65. result.intersect = false;
  66. return;
  67. }
  68. result.intersect = true;
  69. }
  70. };
  71. template <typename Real>
  72. class FIQuery<Real, Line3<Real>, AlignedBox3<Real>>
  73. {
  74. public:
  75. struct Result
  76. {
  77. bool intersect;
  78. int numPoints;
  79. Real lineParameter[2];
  80. Vector3<Real> point[2];
  81. };
  82. Result operator()(Line3<Real> const& line, AlignedBox3<Real> const& box)
  83. {
  84. // Get the centered form of the aligned box. The axes are
  85. // implicitly Axis[d] = Vector3<Real>::Unit(d).
  86. Vector3<Real> boxCenter, boxExtent;
  87. box.GetCenteredForm(boxCenter, boxExtent);
  88. // Transform the line to the aligned-box coordinate system.
  89. Vector3<Real> lineOrigin = line.origin - boxCenter;
  90. Result result;
  91. DoQuery(lineOrigin, line.direction, boxExtent, result);
  92. for (int i = 0; i < result.numPoints; ++i)
  93. {
  94. result.point[i] = line.origin + result.lineParameter[i] * line.direction;
  95. }
  96. return result;
  97. }
  98. protected:
  99. void DoQuery(Vector3<Real> const& lineOrigin, Vector3<Real> const& lineDirection,
  100. Vector3<Real> const& boxExtent, Result& result)
  101. {
  102. // The line t-values are in the interval (-infinity,+infinity).
  103. // Clip the line against all six planes of an aligned box in
  104. // centered form. The result.numPoints is
  105. // 0, no intersection
  106. // 1, intersect in a single point (t0 is line parameter of point)
  107. // 2, intersect in a segment (line parameter interval is [t0,t1])
  108. Real t0 = -std::numeric_limits<Real>::max();
  109. Real t1 = std::numeric_limits<Real>::max();
  110. if (Clip(+lineDirection[0], -lineOrigin[0] - boxExtent[0], t0, t1) &&
  111. Clip(-lineDirection[0], +lineOrigin[0] - boxExtent[0], t0, t1) &&
  112. Clip(+lineDirection[1], -lineOrigin[1] - boxExtent[1], t0, t1) &&
  113. Clip(-lineDirection[1], +lineOrigin[1] - boxExtent[1], t0, t1) &&
  114. Clip(+lineDirection[2], -lineOrigin[2] - boxExtent[2], t0, t1) &&
  115. Clip(-lineDirection[2], +lineOrigin[2] - boxExtent[2], t0, t1))
  116. {
  117. result.intersect = true;
  118. if (t1 > t0)
  119. {
  120. result.numPoints = 2;
  121. result.lineParameter[0] = t0;
  122. result.lineParameter[1] = t1;
  123. }
  124. else
  125. {
  126. result.numPoints = 1;
  127. result.lineParameter[0] = t0;
  128. result.lineParameter[1] = t0; // Used by derived classes.
  129. }
  130. return;
  131. }
  132. result.intersect = false;
  133. result.numPoints = 0;
  134. }
  135. private:
  136. // Test whether the current clipped segment intersects the current
  137. // test plane. If the return value is 'true', the segment does
  138. // intersect the plane and is clipped; otherwise, the segment is
  139. // culled (no intersection with box).
  140. static bool Clip(Real denom, Real numer, Real& t0, Real& t1)
  141. {
  142. if (denom > (Real)0)
  143. {
  144. if (numer > denom * t1)
  145. {
  146. return false;
  147. }
  148. if (numer > denom * t0)
  149. {
  150. t0 = numer / denom;
  151. }
  152. return true;
  153. }
  154. else if (denom < (Real)0)
  155. {
  156. if (numer > denom * t0)
  157. {
  158. return false;
  159. }
  160. if (numer > denom * t1)
  161. {
  162. t1 = numer / denom;
  163. }
  164. return true;
  165. }
  166. else
  167. {
  168. return numer <= (Real)0;
  169. }
  170. }
  171. };
  172. }