IntrSegment3AlignedBox3.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/IntrLine3AlignedBox3.h>
  10. #include <Mathematics/Segment.h>
  11. // The test-intersection queries use the method of separating axes.
  12. // https://www.geometrictools.com/Documentation/MethodOfSeparatingAxes.pdf
  13. // The find-intersection queries use parametric clipping against the six
  14. // faces of the box. The find-intersection queries use Liang-Barsky
  15. // clipping. The queries consider the box to be a solid. The algorithms
  16. // are described in
  17. // https://www.geometrictools.com/Documentation/IntersectionLineBox.pdf
  18. namespace WwiseGTE
  19. {
  20. template <typename Real>
  21. class TIQuery<Real, Segment3<Real>, AlignedBox3<Real>>
  22. :
  23. public TIQuery<Real, Line3<Real>, AlignedBox3<Real>>
  24. {
  25. public:
  26. struct Result
  27. :
  28. public TIQuery<Real, Line3<Real>, AlignedBox3<Real>>::Result
  29. {
  30. // No additional information to compute.
  31. };
  32. Result operator()(Segment3<Real> const& segment, AlignedBox3<Real> const& box)
  33. {
  34. // Get the centered form of the aligned box. The axes are
  35. // implicitly Axis[d] = Vector3<Real>::Unit(d).
  36. Vector3<Real> boxCenter, boxExtent;
  37. box.GetCenteredForm(boxCenter, boxExtent);
  38. // Transform the segment to a centered form in the aligned-box
  39. // coordinate system.
  40. Vector3<Real> transformedP0 = segment.p[0] - boxCenter;
  41. Vector3<Real> transformedP1 = segment.p[1] - boxCenter;
  42. Segment3<Real> transformedSegment(transformedP0, transformedP1);
  43. Vector3<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(Vector3<Real> const& segOrigin, Vector3<Real> const& segDirection,
  52. Real segExtent, Vector3<Real> const& boxExtent, Result& result)
  53. {
  54. for (int i = 0; i < 3; ++i)
  55. {
  56. if (std::fabs(segOrigin[i]) > boxExtent[i] + segExtent * std::fabs(segDirection[i]))
  57. {
  58. result.intersect = false;
  59. return;
  60. }
  61. }
  62. TIQuery<Real, Line3<Real>, AlignedBox3<Real>>::DoQuery(segOrigin, segDirection, boxExtent, result);
  63. }
  64. };
  65. template <typename Real>
  66. class FIQuery<Real, Segment3<Real>, AlignedBox3<Real>>
  67. :
  68. public FIQuery<Real, Line3<Real>, AlignedBox3<Real>>
  69. {
  70. public:
  71. struct Result
  72. :
  73. public FIQuery<Real, Line3<Real>, AlignedBox3<Real>>::Result
  74. {
  75. // No additional information to compute.
  76. };
  77. Result operator()(Segment3<Real> const& segment, AlignedBox3<Real> const& box)
  78. {
  79. // Get the centered form of the aligned box. The axes are
  80. // implicitly Axis[d] = Vector3<Real>::Unit(d).
  81. Vector3<Real> boxCenter, boxExtent;
  82. box.GetCenteredForm(boxCenter, boxExtent);
  83. // Transform the segment to a centered form in the aligned-box
  84. // coordinate system.
  85. Vector3<Real> transformedP0 = segment.p[0] - boxCenter;
  86. Vector3<Real> transformedP1 = segment.p[1] - boxCenter;
  87. Segment3<Real> transformedSegment(transformedP0, transformedP1);
  88. Vector3<Real> segOrigin, segDirection;
  89. Real segExtent;
  90. transformedSegment.GetCenteredForm(segOrigin, segDirection, segExtent);
  91. Result result;
  92. DoQuery(segOrigin, segDirection, segExtent, boxExtent, result);
  93. // The segment origin is in aligned-box coordinates. Transform it
  94. // back to the original space.
  95. segOrigin += boxCenter;
  96. for (int i = 0; i < result.numPoints; ++i)
  97. {
  98. result.point[i] = segOrigin + result.lineParameter[i] * segDirection;
  99. }
  100. return result;
  101. }
  102. protected:
  103. void DoQuery(Vector3<Real> const& segOrigin, Vector3<Real> const& segDirection,
  104. Real segExtent, Vector3<Real> const& boxExtent, Result& result)
  105. {
  106. FIQuery<Real, Line3<Real>, AlignedBox3<Real>>::DoQuery(segOrigin, segDirection, boxExtent, result);
  107. if (result.intersect)
  108. {
  109. // The line containing the segment intersects the box; the
  110. // t-interval is [t0,t1]. The segment intersects the box as
  111. // long as [t0,t1] overlaps the segment t-interval
  112. // [-segExtent,+segExtent].
  113. FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> iiQuery;
  114. std::array<Real, 2> interval0 =
  115. {
  116. result.lineParameter[0], result.lineParameter[1]
  117. };
  118. std::array<Real, 2> interval1 =
  119. {
  120. -segExtent, segExtent
  121. };
  122. auto iiResult = iiQuery(interval0, interval1);
  123. if (iiResult.numIntersections > 0)
  124. {
  125. result.numPoints = iiResult.numIntersections;
  126. for (int i = 0; i < result.numPoints; ++i)
  127. {
  128. result.lineParameter[i] = iiResult.overlap[i];
  129. }
  130. }
  131. else
  132. {
  133. result.intersect = false;
  134. result.numPoints = 0;
  135. }
  136. }
  137. }
  138. };
  139. }