IntrSegment3Ellipsoid3.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/IntrIntervals.h>
  9. #include <Mathematics/IntrLine3Ellipsoid3.h>
  10. #include <Mathematics/Segment.h>
  11. #include <Mathematics/Matrix3x3.h>
  12. // The queries consider the ellipsoid to be a solid.
  13. namespace WwiseGTE
  14. {
  15. template <typename Real>
  16. class TIQuery<Real, Segment3<Real>, Ellipsoid3<Real>>
  17. {
  18. public:
  19. struct Result
  20. {
  21. bool intersect;
  22. };
  23. Result operator()(Segment3<Real> const& segment, Ellipsoid3<Real> const& ellipsoid)
  24. {
  25. // The ellipsoid is (X-K)^T*M*(X-K)-1 = 0 and the line is
  26. // X = P+t*D. Substitute the line equation into the ellipsoid
  27. // equation to obtain a quadratic equation
  28. // Q(t) = a2*t^2 + 2*a1*t + a0 = 0
  29. // where a2 = D^T*M*D, a1 = D^T*M*(P-K) and
  30. // a0 = (P-K)^T*M*(P-K)-1.
  31. Result result;
  32. Vector3<Real> segOrigin, segDirection;
  33. Real segExtent;
  34. segment.GetCenteredForm(segOrigin, segDirection, segExtent);
  35. Matrix3x3<Real> M;
  36. ellipsoid.GetM(M);
  37. Vector3<Real> diff = segOrigin - ellipsoid.center;
  38. Vector3<Real> matDir = M * segDirection;
  39. Vector3<Real> matDiff = M * diff;
  40. Real a2 = Dot(segDirection, matDir);
  41. Real a1 = Dot(segDirection, matDiff);
  42. Real a0 = Dot(diff, matDiff) - (Real)1;
  43. Real discr = a1 * a1 - a0 * a2;
  44. if (discr >= (Real)0)
  45. {
  46. // Test whether ray origin is inside ellipsoid.
  47. if (a0 <= (Real)0)
  48. {
  49. result.intersect = true;
  50. }
  51. else
  52. {
  53. // At this point, Q(0) = a0 > 0 and Q(t) has real roots.
  54. // It is also the case that a2 > 0, since M is positive
  55. // definite, implying that D^T*M*D > 0 for any nonzero
  56. // vector D.
  57. Real q, qder;
  58. if (a1 >= (Real)0)
  59. {
  60. // Roots are possible only on [-e,0], e is the segment
  61. // extent. At least one root occurs if Q(-e) <= 0 or
  62. // if Q(-e) > 0 and Q'(-e) < 0.
  63. q = a0 + segExtent * ((Real)-2 * a1 + a2 * segExtent);
  64. if (q <= (Real)0)
  65. {
  66. result.intersect = true;
  67. }
  68. else
  69. {
  70. qder = a1 - a2 * segExtent;
  71. result.intersect = (qder < (Real)0);
  72. }
  73. }
  74. else
  75. {
  76. // Roots are only possible on [0,e], e is the segment
  77. // extent. At least one root occurs if Q(e) <= 0 or
  78. // if Q(e) > 0 and Q'(e) > 0.
  79. q = a0 + segExtent * ((Real)2 * a1 + a2 * segExtent);
  80. if (q <= (Real)0.0)
  81. {
  82. result.intersect = true;
  83. }
  84. else
  85. {
  86. qder = a1 + a2 * segExtent;
  87. result.intersect = (qder < (Real)0);
  88. }
  89. }
  90. }
  91. }
  92. else
  93. {
  94. // No intersection if Q(t) has no real roots.
  95. result.intersect = false;
  96. }
  97. return result;
  98. }
  99. };
  100. template <typename Real>
  101. class FIQuery<Real, Segment3<Real>, Ellipsoid3<Real>>
  102. :
  103. public FIQuery<Real, Line3<Real>, Ellipsoid3<Real>>
  104. {
  105. public:
  106. struct Result
  107. :
  108. public FIQuery<Real, Line3<Real>, Ellipsoid3<Real>>::Result
  109. {
  110. // No additional information to compute.
  111. };
  112. Result operator()(Segment3<Real> const& segment, Ellipsoid3<Real> const& ellipsoid)
  113. {
  114. Vector3<Real> segOrigin, segDirection;
  115. Real segExtent;
  116. segment.GetCenteredForm(segOrigin, segDirection, segExtent);
  117. Result result;
  118. DoQuery(segOrigin, segDirection, segExtent, ellipsoid, result);
  119. for (int i = 0; i < result.numIntersections; ++i)
  120. {
  121. result.point[i] = segOrigin + result.parameter[i] * segDirection;
  122. }
  123. return result;
  124. }
  125. protected:
  126. void DoQuery(Vector3<Real> const& segOrigin,
  127. Vector3<Real> const& segDirection, Real segExtent,
  128. Ellipsoid3<Real> const& ellipsoid, Result& result)
  129. {
  130. FIQuery<Real, Line3<Real>, Ellipsoid3<Real>>::DoQuery(segOrigin,
  131. segDirection, ellipsoid, result);
  132. if (result.intersect)
  133. {
  134. // The line containing the segment intersects the ellipsoid;
  135. // the t-interval is [t0,t1]. The segment intersects the
  136. // ellipsoid as long as [t0,t1] overlaps the segment
  137. // t-interval [-segExtent,+segExtent].
  138. std::array<Real, 2> segInterval = { -segExtent, segExtent };
  139. FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> iiQuery;
  140. auto iiResult = iiQuery(result.parameter, segInterval);
  141. if (iiResult.intersect)
  142. {
  143. result.numIntersections = iiResult.numIntersections;
  144. result.parameter = iiResult.overlap;
  145. }
  146. else
  147. {
  148. result.intersect = false;
  149. result.numIntersections = 0;
  150. }
  151. }
  152. }
  153. };
  154. }