DistRaySegment.h 5.7 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/DCPQuery.h>
  9. #include <Mathematics/Ray.h>
  10. #include <Mathematics/Segment.h>
  11. namespace WwiseGTE
  12. {
  13. template <int N, typename Real>
  14. class DCPQuery<Real, Ray<N, Real>, Segment<N, Real>>
  15. {
  16. public:
  17. struct Result
  18. {
  19. Real distance, sqrDistance;
  20. Real parameter[2];
  21. Vector<N, Real> closestPoint[2];
  22. };
  23. // The centered form of the 'segment' is used. Thus, parameter[1] of
  24. // the result is in [-e,e], where e = |segment.p[1] - segment.p[0]|/2.
  25. Result operator()(Ray<N, Real> const& ray, Segment<N, Real> const& segment)
  26. {
  27. Result result;
  28. Vector<N, Real> segCenter, segDirection;
  29. Real segExtent;
  30. segment.GetCenteredForm(segCenter, segDirection, segExtent);
  31. Vector<N, Real> diff = ray.origin - segCenter;
  32. Real a01 = -Dot(ray.direction, segDirection);
  33. Real b0 = Dot(diff, ray.direction);
  34. Real s0, s1;
  35. if (std::fabs(a01) < (Real)1)
  36. {
  37. // The ray and segment are not parallel.
  38. Real det = (Real)1 - a01 * a01;
  39. Real extDet = segExtent * det;
  40. Real b1 = -Dot(diff, segDirection);
  41. s0 = a01 * b1 - b0;
  42. s1 = a01 * b0 - b1;
  43. if (s0 >= (Real)0)
  44. {
  45. if (s1 >= -extDet)
  46. {
  47. if (s1 <= extDet) // region 0
  48. {
  49. // Minimum at interior points of ray and segment.
  50. s0 /= det;
  51. s1 /= det;
  52. }
  53. else // region 1
  54. {
  55. s1 = segExtent;
  56. s0 = std::max(-(a01 * s1 + b0), (Real)0);
  57. }
  58. }
  59. else // region 5
  60. {
  61. s1 = -segExtent;
  62. s0 = std::max(-(a01 * s1 + b0), (Real)0);
  63. }
  64. }
  65. else
  66. {
  67. if (s1 <= -extDet) // region 4
  68. {
  69. s0 = -(-a01 * segExtent + b0);
  70. if (s0 > (Real)0)
  71. {
  72. s1 = -segExtent;
  73. }
  74. else
  75. {
  76. s0 = (Real)0;
  77. s1 = -b1;
  78. if (s1 < -segExtent)
  79. {
  80. s1 = -segExtent;
  81. }
  82. else if (s1 > segExtent)
  83. {
  84. s1 = segExtent;
  85. }
  86. }
  87. }
  88. else if (s1 <= extDet) // region 3
  89. {
  90. s0 = (Real)0;
  91. s1 = -b1;
  92. if (s1 < -segExtent)
  93. {
  94. s1 = -segExtent;
  95. }
  96. else if (s1 > segExtent)
  97. {
  98. s1 = segExtent;
  99. }
  100. }
  101. else // region 2
  102. {
  103. s0 = -(a01 * segExtent + b0);
  104. if (s0 > (Real)0)
  105. {
  106. s1 = segExtent;
  107. }
  108. else
  109. {
  110. s0 = (Real)0;
  111. s1 = -b1;
  112. if (s1 < -segExtent)
  113. {
  114. s1 = -segExtent;
  115. }
  116. else if (s1 > segExtent)
  117. {
  118. s1 = segExtent;
  119. }
  120. }
  121. }
  122. }
  123. }
  124. else
  125. {
  126. // Ray and segment are parallel.
  127. if (a01 > (Real)0)
  128. {
  129. // Opposite direction vectors.
  130. s1 = -segExtent;
  131. }
  132. else
  133. {
  134. // Same direction vectors.
  135. s1 = segExtent;
  136. }
  137. s0 = std::max(-(a01 * s1 + b0), (Real)0);
  138. }
  139. result.parameter[0] = s0;
  140. result.parameter[1] = s1;
  141. result.closestPoint[0] = ray.origin + s0 * ray.direction;
  142. result.closestPoint[1] = segCenter + s1 * segDirection;
  143. diff = result.closestPoint[0] - result.closestPoint[1];
  144. result.sqrDistance = Dot(diff, diff);
  145. result.distance = std::sqrt(result.sqrDistance);
  146. return result;
  147. }
  148. };
  149. // Template aliases for convenience.
  150. template <int N, typename Real>
  151. using DCPRaySegment = DCPQuery<Real, Ray<N, Real>, Segment<N, Real>>;
  152. template <typename Real>
  153. using DCPRay2Segment2 = DCPRaySegment<2, Real>;
  154. template <typename Real>
  155. using DCPRay3Segment3 = DCPRaySegment<3, Real>;
  156. }