IntrSegment2AlignedBox2.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/IntrIntervals.h>
  9. #include <Mathematics/IntrLine2AlignedBox2.h>
  10. #include <Mathematics/Segment.h>
  11. #include <Mathematics/Vector2.h>
  12. // The queries consider the box to be a solid.
  13. //
  14. // The test-intersection queries use the method of separating axes.
  15. // https://www.geometrictools.com/Documentation/MethodOfSeparatingAxes.pdf
  16. // The find-intersection queries use parametric clipping against the four
  17. // edges of the box.
  18. namespace WwiseGTE
  19. {
  20. template <typename Real>
  21. class TIQuery<Real, Segment2<Real>, AlignedBox2<Real>>
  22. :
  23. public TIQuery<Real, Line2<Real>, AlignedBox2<Real>>
  24. {
  25. public:
  26. struct Result
  27. :
  28. public TIQuery<Real, Line2<Real>, AlignedBox2<Real>>::Result
  29. {
  30. // No additional information to compute.
  31. };
  32. Result operator()(Segment2<Real> const& segment, AlignedBox2<Real> const& box)
  33. {
  34. // Get the centered form of the aligned box. The axes are
  35. // implicitly Axis[d] = Vector2<Real>::Unit(d).
  36. Vector2<Real> boxCenter, boxExtent;
  37. box.GetCenteredForm(boxCenter, boxExtent);
  38. // Transform the segment to a centered form in the aligned-box
  39. // coordinate system.
  40. Vector2<Real> transformedP0 = segment.p[0] - boxCenter;
  41. Vector2<Real> transformedP1 = segment.p[1] - boxCenter;
  42. Segment2<Real> transformedSegment(transformedP0, transformedP1);
  43. Vector2<Real> segOrigin, segDirection;
  44. Real segExtent;
  45. transformedSegment.GetCenteredForm(segOrigin, segDirection, segExtent);
  46. Result result;
  47. DoQuery(segOrigin, segDirection, segExtent, boxExtent, result);
  48. return result;
  49. }
  50. protected:
  51. void DoQuery(Vector2<Real> const& segOrigin,
  52. Vector2<Real> const& segDirection, Real segExtent,
  53. Vector2<Real> const& boxExtent, Result& result)
  54. {
  55. for (int i = 0; i < 2; ++i)
  56. {
  57. Real lhs = std::fabs(segOrigin[i]);
  58. Real rhs = boxExtent[i] + segExtent * std::fabs(segDirection[i]);
  59. if (lhs > rhs)
  60. {
  61. result.intersect = false;
  62. return;
  63. }
  64. }
  65. TIQuery<Real, Line2<Real>, AlignedBox2<Real>>::DoQuery(segOrigin,
  66. segDirection, boxExtent, result);
  67. }
  68. };
  69. template <typename Real>
  70. class FIQuery<Real, Segment2<Real>, AlignedBox2<Real>>
  71. :
  72. public FIQuery<Real, Line2<Real>, AlignedBox2<Real>>
  73. {
  74. public:
  75. struct Result
  76. :
  77. public FIQuery<Real, Line2<Real>, AlignedBox2<Real>>::Result
  78. {
  79. // The base class parameter[] values are t-values for the
  80. // segment parameterization (1-t)*p[0] + t*p[1], where t in [0,1].
  81. // The values in this class are s-values for the centered form
  82. // C + s * D, where s in [-e,e] and e is the extent of the
  83. // segment.
  84. std::array<Real, 2> cdeParameter;
  85. };
  86. Result operator()(Segment2<Real> const& segment, AlignedBox2<Real> const& box)
  87. {
  88. // Get the centered form of the aligned box. The axes are
  89. // implicitly Axis[d] = Vector2<Real>::Unit(d).
  90. Vector2<Real> boxCenter, boxExtent;
  91. box.GetCenteredForm(boxCenter, boxExtent);
  92. // Transform the segment to a centered form in the aligned-box
  93. // coordinate system.
  94. Vector2<Real> transformedP0 = segment.p[0] - boxCenter;
  95. Vector2<Real> transformedP1 = segment.p[1] - boxCenter;
  96. Segment2<Real> transformedSegment(transformedP0, transformedP1);
  97. Vector2<Real> segOrigin, segDirection;
  98. Real segExtent;
  99. transformedSegment.GetCenteredForm(segOrigin, segDirection, segExtent);
  100. Result result;
  101. DoQuery(segOrigin, segDirection, segExtent, boxExtent, result);
  102. for (int i = 0; i < result.numIntersections; ++i)
  103. {
  104. // Compute the segment in the aligned-box coordinate system
  105. // and then translate it back to the original coordinates
  106. // using the box cener.
  107. result.point[i] = boxCenter + (segOrigin + result.parameter[i] * segDirection);
  108. result.cdeParameter[i] = result.parameter[i];
  109. // Convert the parameters from the centered form to the
  110. // endpoint form.
  111. result.parameter[i] = (result.parameter[i] / segExtent + (Real)1) * (Real)0.5;
  112. }
  113. return result;
  114. }
  115. protected:
  116. void DoQuery(Vector2<Real> const& segOrigin,
  117. Vector2<Real> const& segDirection, Real segExtent,
  118. Vector2<Real> const& boxExtent, Result& result)
  119. {
  120. FIQuery<Real, Line2<Real>, AlignedBox2<Real>>::DoQuery(segOrigin,
  121. segDirection, boxExtent, result);
  122. if (result.intersect)
  123. {
  124. // The line containing the segment intersects the box; the
  125. // t-interval is [t0,t1]. The segment intersects the box as
  126. // long as [t0,t1] overlaps the segment t-interval
  127. // [-segExtent,+segExtent].
  128. std::array<Real, 2> segInterval = { -segExtent, segExtent };
  129. FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> iiQuery;
  130. auto iiResult = iiQuery(result.parameter, segInterval);
  131. result.intersect = iiResult.intersect;
  132. result.numIntersections = iiResult.numIntersections;
  133. result.parameter = iiResult.overlap;
  134. }
  135. }
  136. };
  137. }