IntrPlane3Plane3.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/Hyperplane.h>
  11. #include <Mathematics/Line.h>
  12. #include <Mathematics/Vector3.h>
  13. namespace WwiseGTE
  14. {
  15. template <typename Real>
  16. class TIQuery<Real, Plane3<Real>, Plane3<Real>>
  17. {
  18. public:
  19. struct Result
  20. {
  21. bool intersect;
  22. };
  23. Result operator()(Plane3<Real> const& plane0, Plane3<Real> const& plane1)
  24. {
  25. // If Cross(N0,N1) is zero, then either planes are parallel and
  26. // separated or the same plane. In both cases, 'false' is
  27. // returned. Otherwise, the planes intersect. To avoid subtle
  28. // differences in reporting between Test() and Find(), the same
  29. // parallel test is used. Mathematically,
  30. // |Cross(N0,N1)|^2 = Dot(N0,N0)*Dot(N1,N1)-Dot(N0,N1)^2
  31. // = 1 - Dot(N0,N1)^2
  32. // The last equality is true since planes are required to have
  33. // unit-length normal vectors. The test |Cross(N0,N1)| = 0 is the
  34. // same as |Dot(N0,N1)| = 1.
  35. Result result;
  36. Real dot = Dot(plane0.normal, plane1.normal);
  37. if (std::fabs(dot) < (Real)1)
  38. {
  39. result.intersect = true;
  40. return result;
  41. }
  42. // The planes are parallel. Check whether they are coplanar.
  43. Real cDiff;
  44. if (dot >= (Real)0)
  45. {
  46. // Normals are in same direction, need to look at c0-c1.
  47. cDiff = plane0.constant - plane1.constant;
  48. }
  49. else
  50. {
  51. // Normals are in opposite directions, need to look at c0+c1.
  52. cDiff = plane0.constant + plane1.constant;
  53. }
  54. result.intersect = (std::fabs(cDiff) == (Real)0);
  55. return result;
  56. }
  57. };
  58. template <typename Real>
  59. class FIQuery<Real, Plane3<Real>, Plane3<Real>>
  60. {
  61. public:
  62. struct Result
  63. {
  64. bool intersect;
  65. // If 'intersect' is true, the intersection is either a line or
  66. // the planes are the same. When a line, 'line' is valid. When
  67. // the same plane, 'plane' is set to one of the planes.
  68. bool isLine;
  69. Line3<Real> line;
  70. Plane3<Real> plane;
  71. };
  72. Result operator()(Plane3<Real> const& plane0, Plane3<Real> const& plane1)
  73. {
  74. // If N0 and N1 are parallel, either the planes are parallel and
  75. // separated or the same plane. In both cases, 'false' is
  76. // returned. Otherwise, the intersection line is
  77. // L(t) = t*Cross(N0,N1)/|Cross(N0,N1)| + c0*N0 + c1*N1
  78. // for some coefficients c0 and c1 and for t any real number (the
  79. // line parameter). Taking dot products with the normals,
  80. // d0 = Dot(N0,L) = c0*Dot(N0,N0) + c1*Dot(N0,N1) = c0 + c1*d
  81. // d1 = Dot(N1,L) = c0*Dot(N0,N1) + c1*Dot(N1,N1) = c0*d + c1
  82. // where d = Dot(N0,N1). These are two equations in two unknowns.
  83. // The solution is
  84. // c0 = (d0 - d*d1)/det
  85. // c1 = (d1 - d*d0)/det
  86. // where det = 1 - d^2.
  87. Result result;
  88. Real dot = Dot(plane0.normal, plane1.normal);
  89. if (std::fabs(dot) >= (Real)1)
  90. {
  91. // The planes are parallel. Check if they are coplanar.
  92. Real cDiff;
  93. if (dot >= (Real)0)
  94. {
  95. // Normals are in same direction, need to look at c0-c1.
  96. cDiff = plane0.constant - plane1.constant;
  97. }
  98. else
  99. {
  100. // Normals are in opposite directions, need to look at
  101. // c0+c1.
  102. cDiff = plane0.constant + plane1.constant;
  103. }
  104. if (std::fabs(cDiff) == (Real)0)
  105. {
  106. // The planes are coplanar.
  107. result.intersect = true;
  108. result.isLine = false;
  109. result.plane = plane0;
  110. return result;
  111. }
  112. // The planes are parallel but distinct.
  113. result.intersect = false;
  114. return result;
  115. }
  116. Real invDet = (Real)1 / ((Real)1 - dot * dot);
  117. Real c0 = (plane0.constant - dot * plane1.constant) * invDet;
  118. Real c1 = (plane1.constant - dot * plane0.constant) * invDet;
  119. result.intersect = true;
  120. result.isLine = true;
  121. result.line.origin = c0 * plane0.normal + c1 * plane1.normal;
  122. result.line.direction = UnitCross(plane0.normal, plane1.normal);
  123. return result;
  124. }
  125. };
  126. }