IntrRay2Segment2.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/IntrLine2Line2.h>
  10. #include <Mathematics/Ray.h>
  11. #include <Mathematics/Segment.h>
  12. namespace WwiseGTE
  13. {
  14. template <typename Real>
  15. class TIQuery<Real, Ray2<Real>, Segment2<Real>>
  16. {
  17. public:
  18. struct Result
  19. {
  20. bool intersect;
  21. // The number is 0 (no intersection), 1 (ray and segment intersect
  22. // in a single point), or 2 (ray and segment are collinear and
  23. // intersect in a segment).
  24. int numIntersections;
  25. };
  26. Result operator()(Ray2<Real> const& ray, Segment2<Real> const& segment)
  27. {
  28. Result result;
  29. Vector2<Real> segOrigin, segDirection;
  30. Real segExtent;
  31. segment.GetCenteredForm(segOrigin, segDirection, segExtent);
  32. FIQuery<Real, Line2<Real>, Line2<Real>> llQuery;
  33. Line2<Real> line0(ray.origin, ray.direction);
  34. Line2<Real> line1(segOrigin, segDirection);
  35. auto llResult = llQuery(line0, line1);
  36. if (llResult.numIntersections == 1)
  37. {
  38. // Test whether the line-line intersection is on the ray and
  39. // segment.
  40. if (llResult.line0Parameter[0] >= (Real)0
  41. && std::fabs(llResult.line1Parameter[0]) <= segExtent)
  42. {
  43. result.intersect = true;
  44. result.numIntersections = 1;
  45. }
  46. else
  47. {
  48. result.intersect = false;
  49. result.numIntersections = 0;
  50. }
  51. }
  52. else if (llResult.numIntersections == std::numeric_limits<int>::max())
  53. {
  54. // Compute the location of the right-most point of the segment
  55. // relative to the ray direction.
  56. Vector2<Real> diff = segOrigin - ray.origin;
  57. Real t = Dot(ray.direction, diff) + segExtent;
  58. if (t > (Real)0)
  59. {
  60. result.intersect = true;
  61. result.numIntersections = 2;
  62. }
  63. else if (t < (Real)0)
  64. {
  65. result.intersect = false;
  66. result.numIntersections = 0;
  67. }
  68. else // t == 0
  69. {
  70. result.intersect = true;
  71. result.numIntersections = 1;
  72. }
  73. }
  74. else
  75. {
  76. result.intersect = false;
  77. result.numIntersections = 0;
  78. }
  79. return result;
  80. }
  81. };
  82. template <typename Real>
  83. class FIQuery<Real, Ray2<Real>, Segment2<Real>>
  84. {
  85. public:
  86. struct Result
  87. {
  88. bool intersect;
  89. // The number is 0 (no intersection), 1 (ray and segment intersect
  90. // in a single point), or 2 (ray and segment are collinear and
  91. // intersect in a segment).
  92. int numIntersections;
  93. // If numIntersections is 1, the intersection is
  94. // point[0] = ray.origin + rayParameter[0] * ray.direction
  95. // = segment.center + segmentParameter[0] * segment.direction
  96. // If numIntersections is 2, the endpoints of the segment of
  97. // intersection are
  98. // point[i] = ray.origin + rayParameter[i] * ray.direction
  99. // = segment.center + segmentParameter[i] * segment.direction
  100. // with rayParameter[0] <= rayParameter[1] and
  101. // segmentParameter[0] <= segmentParameter[1].
  102. Real rayParameter[2], segmentParameter[2];
  103. Vector2<Real> point[2];
  104. };
  105. Result operator()(Ray2<Real> const& ray, Segment2<Real> const& segment)
  106. {
  107. Result result;
  108. Vector2<Real> segOrigin, segDirection;
  109. Real segExtent;
  110. segment.GetCenteredForm(segOrigin, segDirection, segExtent);
  111. FIQuery<Real, Line2<Real>, Line2<Real>> llQuery;
  112. Line2<Real> line0(ray.origin, ray.direction);
  113. Line2<Real> line1(segOrigin, segDirection);
  114. auto llResult = llQuery(line0, line1);
  115. if (llResult.numIntersections == 1)
  116. {
  117. // Test whether the line-line intersection is on the ray and
  118. // segment.
  119. if (llResult.line0Parameter[0] >= (Real)0
  120. && std::fabs(llResult.line1Parameter[0]) <= segExtent)
  121. {
  122. result.intersect = true;
  123. result.numIntersections = 1;
  124. result.rayParameter[0] = llResult.line0Parameter[0];
  125. result.segmentParameter[0] = llResult.line1Parameter[0];
  126. result.point[0] = llResult.point;
  127. }
  128. else
  129. {
  130. result.intersect = false;
  131. result.numIntersections = 0;
  132. }
  133. }
  134. else if (llResult.numIntersections == std::numeric_limits<int>::max())
  135. {
  136. // Compute t for which segment.origin =
  137. // ray.origin + t*ray.direction.
  138. Vector2<Real> diff = segOrigin - ray.origin;
  139. Real t = Dot(ray.direction, diff);
  140. // Get the ray interval.
  141. std::array<Real, 2> interval0 =
  142. {
  143. (Real)0, std::numeric_limits<Real>::max()
  144. };
  145. // Compute the location of the segment endpoints relative to
  146. // the ray.
  147. std::array<Real, 2> interval1 = { t - segExtent, t + segExtent };
  148. // Compute the intersection of [0,+infinity) and [tmin,tmax].
  149. FIQuery<Real, std::array<Real, 2>, std::array<Real, 2>> iiQuery;
  150. auto iiResult = iiQuery(interval0, interval1);
  151. if (iiResult.intersect)
  152. {
  153. result.intersect = true;
  154. result.numIntersections = iiResult.numIntersections;
  155. for (int i = 0; i < iiResult.numIntersections; ++i)
  156. {
  157. result.rayParameter[i] = iiResult.overlap[i];
  158. result.segmentParameter[i] = iiResult.overlap[i] - t;
  159. result.point[i] = ray.origin + result.rayParameter[i] * ray.direction;
  160. }
  161. }
  162. else
  163. {
  164. result.intersect = false;
  165. result.numIntersections = 0;
  166. }
  167. }
  168. else
  169. {
  170. result.intersect = false;
  171. result.numIntersections = 0;
  172. }
  173. return result;
  174. }
  175. };
  176. }