IntrRay2AlignedBox2.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/Ray.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, Ray2<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()(Ray2<Real> const& ray, 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 ray to the aligned-box coordinate system.
  39. Vector2<Real> rayOrigin = ray.origin - boxCenter;
  40. Result result;
  41. DoQuery(rayOrigin, ray.direction, boxExtent, result);
  42. return result;
  43. }
  44. protected:
  45. void DoQuery(Vector2<Real> const& rayOrigin,
  46. Vector2<Real> const& rayDirection, Vector2<Real> const& boxExtent,
  47. Result& result)
  48. {
  49. for (int i = 0; i < 2; ++i)
  50. {
  51. if (std::fabs(rayOrigin[i]) > boxExtent[i]
  52. && rayOrigin[i] * rayDirection[i] >= (Real)0)
  53. {
  54. result.intersect = false;
  55. return;
  56. }
  57. }
  58. TIQuery<Real, Line2<Real>, AlignedBox2<Real>>::DoQuery(rayOrigin,
  59. rayDirection, boxExtent, result);
  60. }
  61. };
  62. template <typename Real>
  63. class FIQuery<Real, Ray2<Real>, AlignedBox2<Real>>
  64. :
  65. public FIQuery<Real, Line2<Real>, AlignedBox2<Real>>
  66. {
  67. public:
  68. struct Result
  69. :
  70. public FIQuery<Real, Line2<Real>, AlignedBox2<Real>>::Result
  71. {
  72. // No additional information to compute.
  73. };
  74. Result operator()(Ray2<Real> const& ray, AlignedBox2<Real> const& box)
  75. {
  76. // Get the centered form of the aligned box. The axes are
  77. // implicitly Axis[d] = Vector2<Real>::Unit(d).
  78. Vector2<Real> boxCenter, boxExtent;
  79. box.GetCenteredForm(boxCenter, boxExtent);
  80. // Transform the ray to the aligned-box coordinate system.
  81. Vector2<Real> rayOrigin = ray.origin - boxCenter;
  82. Result result;
  83. DoQuery(rayOrigin, ray.direction, boxExtent, result);
  84. for (int i = 0; i < result.numIntersections; ++i)
  85. {
  86. result.point[i] = ray.origin + result.parameter[i] * ray.direction;
  87. }
  88. return result;
  89. }
  90. protected:
  91. void DoQuery(Vector2<Real> const& rayOrigin,
  92. Vector2<Real> const& rayDirection, Vector2<Real> const& boxExtent,
  93. Result& result)
  94. {
  95. FIQuery<Real, Line2<Real>, AlignedBox2<Real>>::DoQuery(rayOrigin,
  96. rayDirection, boxExtent, result);
  97. if (result.intersect)
  98. {
  99. // The line containing the ray intersects the box; the
  100. // t-interval is [t0,t1]. The ray intersects the box as long
  101. // as [t0,t1] overlaps the ray t-interval [0,+infinity).
  102. std::array<Real, 2> rayInterval =
  103. { (Real)0, std::numeric_limits<Real>::max() };
  104. FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> iiQuery;
  105. auto iiResult = iiQuery(result.parameter, rayInterval);
  106. result.intersect = iiResult.intersect;
  107. result.numIntersections = iiResult.numIntersections;
  108. result.parameter = iiResult.overlap;
  109. }
  110. }
  111. };
  112. }