IntrRay3AlignedBox3.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/IntrLine3AlignedBox3.h>
  9. #include <Mathematics/Ray.h>
  10. // The test-intersection queries use the method of separating axes.
  11. // https://www.geometrictools.com/Documentation/MethodOfSeparatingAxes.pdf
  12. // The find-intersection queries use parametric clipping against the six
  13. // faces of the box. The find-intersection queries use Liang-Barsky
  14. // clipping. The queries consider the box to be a solid. The algorithms
  15. // are described in
  16. // https://www.geometrictools.com/Documentation/IntersectionLineBox.pdf
  17. namespace WwiseGTE
  18. {
  19. template <typename Real>
  20. class TIQuery<Real, Ray3<Real>, AlignedBox3<Real>>
  21. :
  22. public TIQuery<Real, Line3<Real>, AlignedBox3<Real>>
  23. {
  24. public:
  25. struct Result
  26. :
  27. public TIQuery<Real, Line3<Real>, AlignedBox3<Real>>::Result
  28. {
  29. // No additional information to compute.
  30. };
  31. Result operator()(Ray3<Real> const& ray, AlignedBox3<Real> const& box)
  32. {
  33. // Get the centered form of the aligned box. The axes are
  34. // implicitly Axis[d] = Vector3<Real>::Unit(d).
  35. Vector3<Real> boxCenter, boxExtent;
  36. box.GetCenteredForm(boxCenter, boxExtent);
  37. // Transform the ray to the aligned-box coordinate system.
  38. Vector3<Real> rayOrigin = ray.origin - boxCenter;
  39. Result result;
  40. DoQuery(rayOrigin, ray.direction, boxExtent, result);
  41. return result;
  42. }
  43. protected:
  44. void DoQuery(Vector3<Real> const& rayOrigin, Vector3<Real> const& rayDirection,
  45. Vector3<Real> const& boxExtent, Result& result)
  46. {
  47. for (int i = 0; i < 3; ++i)
  48. {
  49. if (std::fabs(rayOrigin[i]) > boxExtent[i] && rayOrigin[i] * rayDirection[i] >= (Real)0)
  50. {
  51. result.intersect = false;
  52. return;
  53. }
  54. }
  55. TIQuery<Real, Line3<Real>, AlignedBox3<Real>>::DoQuery(rayOrigin, rayDirection, boxExtent, result);
  56. }
  57. };
  58. template <typename Real>
  59. class FIQuery<Real, Ray3<Real>, AlignedBox3<Real>>
  60. :
  61. public FIQuery<Real, Line3<Real>, AlignedBox3<Real>>
  62. {
  63. public:
  64. struct Result
  65. :
  66. public FIQuery<Real, Line3<Real>, AlignedBox3<Real>>::Result
  67. {
  68. // No additional information to compute.
  69. };
  70. Result operator()(Ray3<Real> const& ray, AlignedBox3<Real> const& box)
  71. {
  72. // Get the centered form of the aligned box. The axes are
  73. // implicitly Axis[d] = Vector3<Real>::Unit(d).
  74. Vector3<Real> boxCenter, boxExtent;
  75. box.GetCenteredForm(boxCenter, boxExtent);
  76. // Transform the ray to the aligned-box coordinate system.
  77. Vector3<Real> rayOrigin = ray.origin - boxCenter;
  78. Result result;
  79. DoQuery(rayOrigin, ray.direction, boxExtent, result);
  80. for (int i = 0; i < result.numPoints; ++i)
  81. {
  82. result.point[i] = ray.origin + result.lineParameter[i] * ray.direction;
  83. }
  84. return result;
  85. }
  86. protected:
  87. void DoQuery(Vector3<Real> const& rayOrigin, Vector3<Real> const& rayDirection,
  88. Vector3<Real> const& boxExtent, Result& result)
  89. {
  90. FIQuery<Real, Line3<Real>, AlignedBox3<Real>>::DoQuery(rayOrigin, rayDirection, boxExtent, result);
  91. if (result.intersect)
  92. {
  93. // The line containing the ray intersects the box; the
  94. // t-interval is [t0,t1]. The ray intersects the box as long
  95. // as [t0,t1] overlaps the ray t-interval (0,+infinity).
  96. if (result.lineParameter[1] >= (Real)0)
  97. {
  98. if (result.lineParameter[0] < (Real)0)
  99. {
  100. result.lineParameter[0] = (Real)0;
  101. }
  102. }
  103. else
  104. {
  105. result.intersect = false;
  106. result.numPoints = 0;
  107. }
  108. }
  109. }
  110. };
  111. }