IntrRay3Plane3.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/IntrLine3Plane3.h>
  9. #include <Mathematics/Ray.h>
  10. namespace WwiseGTE
  11. {
  12. template <typename Real>
  13. class TIQuery<Real, Ray3<Real>, Plane3<Real>>
  14. {
  15. public:
  16. struct Result
  17. {
  18. bool intersect;
  19. };
  20. Result operator()(Ray3<Real> const& ray, Plane3<Real> const& plane)
  21. {
  22. Result result;
  23. // Compute the (signed) distance from the ray origin to the plane.
  24. DCPQuery<Real, Vector3<Real>, Plane3<Real>> vpQuery;
  25. auto vpResult = vpQuery(ray.origin, plane);
  26. Real DdN = Dot(ray.direction, plane.normal);
  27. if (DdN > (Real)0)
  28. {
  29. // The ray is not parallel to the plane and is directed toward
  30. // the +normal side of the plane.
  31. result.intersect = (vpResult.signedDistance <= (Real)0);
  32. }
  33. else if (DdN < (Real)0)
  34. {
  35. // The ray is not parallel to the plane and is directed toward
  36. // the -normal side of the plane.
  37. result.intersect = (vpResult.signedDistance >= (Real)0);
  38. }
  39. else
  40. {
  41. // The ray and plane are parallel.
  42. result.intersect = (vpResult.distance == (Real)0);
  43. }
  44. return result;
  45. }
  46. };
  47. template <typename Real>
  48. class FIQuery<Real, Ray3<Real>, Plane3<Real>>
  49. :
  50. public FIQuery<Real, Line3<Real>, Plane3<Real>>
  51. {
  52. public:
  53. struct Result
  54. :
  55. public FIQuery<Real, Line3<Real>, Plane3<Real>>::Result
  56. {
  57. // No additional information to compute.
  58. };
  59. Result operator()(Ray3<Real> const& ray, Plane3<Real> const& plane)
  60. {
  61. Result result;
  62. DoQuery(ray.origin, ray.direction, plane, result);
  63. if (result.intersect)
  64. {
  65. result.point = ray.origin + result.parameter * ray.direction;
  66. }
  67. return result;
  68. }
  69. protected:
  70. void DoQuery(Vector3<Real> const& rayOrigin,
  71. Vector3<Real> const& rayDirection, Plane3<Real> const& plane,
  72. Result& result)
  73. {
  74. FIQuery<Real, Line3<Real>, Plane3<Real>>::DoQuery(rayOrigin,
  75. rayDirection, plane, result);
  76. if (result.intersect)
  77. {
  78. // The line intersects the plane in a point that might not be
  79. // on the ray.
  80. if (result.parameter < (Real)0)
  81. {
  82. result.intersect = false;
  83. result.numIntersections = 0;
  84. }
  85. }
  86. }
  87. };
  88. }