IntrLine3Plane3.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/FIQuery.h>
  9. #include <Mathematics/TIQuery.h>
  10. #include <Mathematics/Line.h>
  11. #include <Mathematics/DistPoint3Plane3.h>
  12. namespace WwiseGTE
  13. {
  14. template <typename Real>
  15. class TIQuery<Real, Line3<Real>, Plane3<Real>>
  16. {
  17. public:
  18. struct Result
  19. {
  20. bool intersect;
  21. };
  22. Result operator()(Line3<Real> const& line, Plane3<Real> const& plane)
  23. {
  24. Result result;
  25. Real DdN = Dot(line.direction, plane.normal);
  26. if (DdN != (Real)0)
  27. {
  28. // The line is not parallel to the plane, so they must
  29. // intersect.
  30. result.intersect = true;
  31. }
  32. else
  33. {
  34. // The line and plane are parallel.
  35. DCPQuery<Real, Vector3<Real>, Plane3<Real>> vpQuery;
  36. result.intersect = (vpQuery(line.origin, plane).distance == (Real)0);
  37. }
  38. return result;
  39. }
  40. };
  41. template <typename Real>
  42. class FIQuery<Real, Line3<Real>, Plane3<Real>>
  43. {
  44. public:
  45. struct Result
  46. {
  47. Result()
  48. :
  49. intersect(false),
  50. parameter((Real)0),
  51. point{ (Real)0, (Real)0, (Real)0 }
  52. {
  53. }
  54. bool intersect;
  55. // The number of intersections is 0 (no intersection), 1 (linear
  56. // component and plane intersect in a point), or
  57. // std::numeric_limits<int>::max() (linear component is on the
  58. // plane). If the linear component is on the plane, 'point'
  59. // component's origin and 'parameter' is zero.
  60. int numIntersections;
  61. Real parameter;
  62. Vector3<Real> point;
  63. };
  64. Result operator()(Line3<Real> const& line, Plane3<Real> const& plane)
  65. {
  66. Result result;
  67. DoQuery(line.origin, line.direction, plane, result);
  68. if (result.intersect)
  69. {
  70. result.point = line.origin + result.parameter * line.direction;
  71. }
  72. return result;
  73. }
  74. protected:
  75. void DoQuery(Vector3<Real> const& lineOrigin,
  76. Vector3<Real> const& lineDirection, Plane3<Real> const& plane,
  77. Result& result)
  78. {
  79. Real DdN = Dot(lineDirection, plane.normal);
  80. DCPQuery<Real, Vector3<Real>, Plane3<Real>> vpQuery;
  81. auto vpResult = vpQuery(lineOrigin, plane);
  82. if (DdN != (Real)0)
  83. {
  84. // The line is not parallel to the plane, so they must
  85. // intersect.
  86. result.intersect = true;
  87. result.numIntersections = 1;
  88. result.parameter = -vpResult.signedDistance / DdN;
  89. }
  90. else
  91. {
  92. // The line and plane are parallel. Determine whether the
  93. // line is on the plane.
  94. if (vpResult.distance == (Real)0)
  95. {
  96. // The line is coincident with the plane, so choose t = 0
  97. // for the parameter.
  98. result.intersect = true;
  99. result.numIntersections = std::numeric_limits<int>::max();
  100. result.parameter = (Real)0;
  101. }
  102. else
  103. {
  104. // The line is not on the plane.
  105. result.intersect = false;
  106. result.numIntersections = 0;
  107. }
  108. }
  109. }
  110. };
  111. }