IntrLine2AlignedBox2.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/Line.h>
  11. #include <Mathematics/AlignedBox.h>
  12. #include <Mathematics/Vector2.h>
  13. // The queries consider the box to be a solid.
  14. //
  15. // The test-intersection queries use the method of separating axes.
  16. // https://www.geometrictools.com/Documentation/MethodOfSeparatingAxes.pdf
  17. // The find-intersection queries use parametric clipping against the four
  18. // edges of the box.
  19. namespace WwiseGTE
  20. {
  21. template <typename Real>
  22. class TIQuery<Real, Line2<Real>, AlignedBox2<Real>>
  23. {
  24. public:
  25. struct Result
  26. {
  27. bool intersect;
  28. };
  29. Result operator()(Line2<Real> const& line, AlignedBox2<Real> const& box)
  30. {
  31. // Get the centered form of the aligned box. The axes are
  32. // implicitly Axis[d] = Vector2<Real>::Unit(d).
  33. Vector2<Real> boxCenter, boxExtent;
  34. box.GetCenteredForm(boxCenter, boxExtent);
  35. // Transform the line to the aligned-box coordinate system.
  36. Vector2<Real> lineOrigin = line.origin - boxCenter;
  37. Result result;
  38. DoQuery(lineOrigin, line.direction, boxExtent, result);
  39. return result;
  40. }
  41. protected:
  42. void DoQuery(Vector2<Real> const& lineOrigin,
  43. Vector2<Real> const& lineDirection, Vector2<Real> const& boxExtent,
  44. Result& result)
  45. {
  46. Real LHS = std::fabs(DotPerp(lineDirection, lineOrigin));
  47. Real RHS =
  48. boxExtent[0] * std::fabs(lineDirection[1]) +
  49. boxExtent[1] * std::fabs(lineDirection[0]);
  50. result.intersect = (LHS <= RHS);
  51. }
  52. };
  53. template <typename Real>
  54. class FIQuery<Real, Line2<Real>, AlignedBox2<Real>>
  55. {
  56. public:
  57. struct Result
  58. {
  59. bool intersect;
  60. int numIntersections;
  61. std::array<Real, 2> parameter;
  62. std::array<Vector2<Real>, 2> point;
  63. };
  64. Result operator()(Line2<Real> const& line, AlignedBox2<Real> const& box)
  65. {
  66. // Get the centered form of the aligned box. The axes are
  67. // implicitly Axis[d] = Vector2<Real>::Unit(d).
  68. Vector2<Real> boxCenter, boxExtent;
  69. box.GetCenteredForm(boxCenter, boxExtent);
  70. // Transform the line to the aligned-box coordinate system.
  71. Vector2<Real> lineOrigin = line.origin - boxCenter;
  72. Result result;
  73. DoQuery(lineOrigin, line.direction, boxExtent, result);
  74. for (int i = 0; i < result.numIntersections; ++i)
  75. {
  76. result.point[i] = line.origin + result.parameter[i] * line.direction;
  77. }
  78. return result;
  79. }
  80. protected:
  81. void DoQuery(Vector2<Real> const& lineOrigin,
  82. Vector2<Real> const& lineDirection, Vector2<Real> const& boxExtent,
  83. Result& result)
  84. {
  85. // The line t-values are in the interval (-infinity,+infinity).
  86. // Clip the line against all four planes of an aligned box in
  87. // centered form. The result.numPoints is
  88. // 0, no intersection
  89. // 1, intersect in a single point (t0 is line parameter of point)
  90. // 2, intersect in a segment (line parameter interval is [t0,t1])
  91. Real t0 = -std::numeric_limits<Real>::max();
  92. Real t1 = std::numeric_limits<Real>::max();
  93. if (Clip(+lineDirection[0], -lineOrigin[0] - boxExtent[0], t0, t1) &&
  94. Clip(-lineDirection[0], +lineOrigin[0] - boxExtent[0], t0, t1) &&
  95. Clip(+lineDirection[1], -lineOrigin[1] - boxExtent[1], t0, t1) &&
  96. Clip(-lineDirection[1], +lineOrigin[1] - boxExtent[1], t0, t1))
  97. {
  98. result.intersect = true;
  99. if (t1 > t0)
  100. {
  101. result.numIntersections = 2;
  102. result.parameter[0] = t0;
  103. result.parameter[1] = t1;
  104. }
  105. else
  106. {
  107. result.numIntersections = 1;
  108. result.parameter[0] = t0;
  109. result.parameter[1] = t0; // Used by derived classes.
  110. }
  111. return;
  112. }
  113. result.intersect = false;
  114. result.numIntersections = 0;
  115. }
  116. private:
  117. // Test whether the current clipped segment intersects the current
  118. // test plane. If the return value is 'true', the segment does
  119. // intersect the plane and is clipped; otherwise, the segment is
  120. // culled (no intersection with box).
  121. static bool Clip(Real denom, Real numer, Real& t0, Real& t1)
  122. {
  123. if (denom > (Real)0)
  124. {
  125. if (numer > denom * t1)
  126. {
  127. return false;
  128. }
  129. if (numer > denom * t0)
  130. {
  131. t0 = numer / denom;
  132. }
  133. return true;
  134. }
  135. else if (denom < (Real)0)
  136. {
  137. if (numer > denom * t0)
  138. {
  139. return false;
  140. }
  141. if (numer > denom * t1)
  142. {
  143. t1 = numer / denom;
  144. }
  145. return true;
  146. }
  147. else
  148. {
  149. return numer <= (Real)0;
  150. }
  151. }
  152. };
  153. }