IntrSegment3Plane3.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/Segment.h>
  10. namespace WwiseGTE
  11. {
  12. template <typename Real>
  13. class TIQuery<Real, Segment3<Real>, Plane3<Real>>
  14. {
  15. public:
  16. struct Result
  17. {
  18. bool intersect;
  19. };
  20. Result operator()(Segment3<Real> const& segment, Plane3<Real> const& plane)
  21. {
  22. Result result;
  23. // Compute the (signed) distance from the segment endpoints to the
  24. // plane.
  25. DCPQuery<Real, Vector3<Real>, Plane3<Real>> vpQuery;
  26. Real sdistance0 = vpQuery(segment.p[0], plane).signedDistance;
  27. if (sdistance0 == (Real)0)
  28. {
  29. // Endpoint p[0] is on the plane.
  30. result.intersect = true;
  31. return result;
  32. }
  33. Real sdistance1 = vpQuery(segment.p[1], plane).signedDistance;
  34. if (sdistance1 == (Real)0)
  35. {
  36. // Endpoint p[1] is on the plane.
  37. result.intersect = true;
  38. return result;
  39. }
  40. // Test whether the segment transversely intersects the plane.
  41. result.intersect = (sdistance0 * sdistance1 < (Real)0);
  42. return result;
  43. }
  44. };
  45. template <typename Real>
  46. class FIQuery<Real, Segment3<Real>, Plane3<Real>>
  47. :
  48. public FIQuery<Real, Line3<Real>, Plane3<Real>>
  49. {
  50. public:
  51. struct Result
  52. :
  53. public FIQuery<Real, Line3<Real>, Plane3<Real>>::Result
  54. {
  55. // No additional information to compute.
  56. };
  57. Result operator()(Segment3<Real> const& segment, Plane3<Real> const& plane)
  58. {
  59. Vector3<Real> segOrigin, segDirection;
  60. Real segExtent;
  61. segment.GetCenteredForm(segOrigin, segDirection, segExtent);
  62. Result result;
  63. DoQuery(segOrigin, segDirection, segExtent, plane, result);
  64. if (result.intersect)
  65. {
  66. result.point = segOrigin + result.parameter * segDirection;
  67. }
  68. return result;
  69. }
  70. protected:
  71. void DoQuery(Vector3<Real> const& segOrigin,
  72. Vector3<Real> const& segDirection, Real segExtent,
  73. Plane3<Real> const& plane, Result& result)
  74. {
  75. FIQuery<Real, Line3<Real>, Plane3<Real>>::DoQuery(segOrigin,
  76. segDirection, plane, result);
  77. if (result.intersect)
  78. {
  79. // The line intersects the plane in a point that might not be
  80. // on the segment.
  81. if (std::fabs(result.parameter) > segExtent)
  82. {
  83. result.intersect = false;
  84. result.numIntersections = 0;
  85. }
  86. }
  87. }
  88. };
  89. }