DistSegmentSegmentExact.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/Segment.h>
  10. // Compute the closest points on the line segments P(s) = (1-s)*P0 + s*P1 and
  11. // Q(t) = (1-t)*Q0 + t*Q1 for 0 <= s <= 1 and 0 <= t <= 1. The algorithm
  12. // relies on exact rational arithmetic. See the document
  13. // https://www.geometrictools.com/Documentation/DistanceLine3Line3.pdf
  14. // for details.
  15. namespace WwiseGTE
  16. {
  17. template <int N, typename Rational>
  18. class DistanceSegmentSegmentExact
  19. {
  20. public:
  21. struct Result
  22. {
  23. Rational sqrDistance;
  24. Rational parameter[2];
  25. Vector<N, Rational> closest[2];
  26. };
  27. Result operator()(Segment<N, Rational> const& segment0,
  28. Segment<N, Rational> const& segment1)
  29. {
  30. return operator()(segment0.p[0], segment0.p[1], segment1.p[0], segment1.p[1]);
  31. }
  32. Result operator()(
  33. Vector<N, Rational> const& P0, Vector<N, Rational> const& P1,
  34. Vector<N, Rational> const& Q0, Vector<N, Rational> const& Q1)
  35. {
  36. Vector<N, Rational> P1mP0 = P1 - P0;
  37. Vector<N, Rational> Q1mQ0 = Q1 - Q0;
  38. Vector<N, Rational> P0mQ0 = P0 - Q0;
  39. Rational a = Dot(P1mP0, P1mP0);
  40. Rational b = Dot(P1mP0, Q1mQ0);
  41. Rational c = Dot(Q1mQ0, Q1mQ0);
  42. Rational d = Dot(P1mP0, P0mQ0);
  43. Rational e = Dot(Q1mQ0, P0mQ0);
  44. Rational const zero = (Rational)0;
  45. Rational const one = (Rational)1;
  46. Rational det = a * c - b * b;
  47. Rational s, t, nd, bmd, bte, ctd, bpe, ate, btd;
  48. if (det > zero)
  49. {
  50. bte = b * e;
  51. ctd = c * d;
  52. if (bte <= ctd) // s <= 0
  53. {
  54. s = zero;
  55. if (e <= zero) // t <= 0
  56. {
  57. // region 6
  58. t = zero;
  59. nd = -d;
  60. if (nd >= a)
  61. {
  62. s = one;
  63. }
  64. else if (nd > zero)
  65. {
  66. s = nd / a;
  67. }
  68. // else: s is already zero
  69. }
  70. else if (e < c) // 0 < t < 1
  71. {
  72. // region 5
  73. t = e / c;
  74. }
  75. else // t >= 1
  76. {
  77. // region 4
  78. t = one;
  79. bmd = b - d;
  80. if (bmd >= a)
  81. {
  82. s = one;
  83. }
  84. else if (bmd > zero)
  85. {
  86. s = bmd / a;
  87. }
  88. // else: s is already zero
  89. }
  90. }
  91. else // s > 0
  92. {
  93. s = bte - ctd;
  94. if (s >= det) // s >= 1
  95. {
  96. // s = 1
  97. s = one;
  98. bpe = b + e;
  99. if (bpe <= zero) // t <= 0
  100. {
  101. // region 8
  102. t = zero;
  103. nd = -d;
  104. if (nd <= zero)
  105. {
  106. s = zero;
  107. }
  108. else if (nd < a)
  109. {
  110. s = nd / a;
  111. }
  112. // else: s is already one
  113. }
  114. else if (bpe < c) // 0 < t < 1
  115. {
  116. // region 1
  117. t = bpe / c;
  118. }
  119. else // t >= 1
  120. {
  121. // region 2
  122. t = one;
  123. bmd = b - d;
  124. if (bmd <= zero)
  125. {
  126. s = zero;
  127. }
  128. else if (bmd < a)
  129. {
  130. s = bmd / a;
  131. }
  132. // else: s is already one
  133. }
  134. }
  135. else // 0 < s < 1
  136. {
  137. ate = a * e;
  138. btd = b * d;
  139. if (ate <= btd) // t <= 0
  140. {
  141. // region 7
  142. t = zero;
  143. nd = -d;
  144. if (nd <= zero)
  145. {
  146. s = zero;
  147. }
  148. else if (nd >= a)
  149. {
  150. s = one;
  151. }
  152. else
  153. {
  154. s = nd / a;
  155. }
  156. }
  157. else // t > 0
  158. {
  159. t = ate - btd;
  160. if (t >= det) // t >= 1
  161. {
  162. // region 3
  163. t = one;
  164. bmd = b - d;
  165. if (bmd <= zero)
  166. {
  167. s = zero;
  168. }
  169. else if (bmd >= a)
  170. {
  171. s = one;
  172. }
  173. else
  174. {
  175. s = bmd / a;
  176. }
  177. }
  178. else // 0 < t < 1
  179. {
  180. // region 0
  181. s /= det;
  182. t /= det;
  183. }
  184. }
  185. }
  186. }
  187. }
  188. else
  189. {
  190. // The segments are parallel. The quadratic factors to
  191. // R(s,t) = a*(s-(b/a)*t)^2 + 2*d*(s - (b/a)*t) + f
  192. // where a*c = b^2, e = b*d/a, f = |P0-Q0|^2, and b is not
  193. // zero. R is constant along lines of the form s-(b/a)*t = k
  194. // and its occurs on the line a*s - b*t + d = 0. This line
  195. // must intersect both the s-axis and the t-axis because 'a'
  196. // and 'b' are not zero. Because of parallelism, the line is
  197. // also represented by -b*s + c*t - e = 0.
  198. //
  199. // The code determines an edge of the domain [0,1]^2 that
  200. // intersects the minimum line, or if none of the edges
  201. // intersect, it determines the closest corner to the minimum
  202. // line. The conditionals are designed to test first for
  203. // intersection with the t-axis (s = 0) using
  204. // -b*s + c*t - e = 0 and then with the s-axis (t = 0) using
  205. // a*s - b*t + d = 0.
  206. // When s = 0, solve c*t - e = 0 (t = e/c).
  207. if (e <= zero) // t <= 0
  208. {
  209. // Now solve a*s - b*t + d = 0 for t = 0 (s = -d/a).
  210. t = zero;
  211. nd = -d;
  212. if (nd <= zero) // s <= 0
  213. {
  214. // region 6
  215. s = zero;
  216. }
  217. else if (nd >= a) // s >= 1
  218. {
  219. // region 8
  220. s = one;
  221. }
  222. else // 0 < s < 1
  223. {
  224. // region 7
  225. s = nd / a;
  226. }
  227. }
  228. else if (e >= c) // t >= 1
  229. {
  230. // Now solve a*s - b*t + d = 0 for t = 1 (s = (b-d)/a).
  231. t = one;
  232. bmd = b - d;
  233. if (bmd <= zero) // s <= 0
  234. {
  235. // region 4
  236. s = zero;
  237. }
  238. else if (bmd >= a) // s >= 1
  239. {
  240. // region 2
  241. s = one;
  242. }
  243. else // 0 < s < 1
  244. {
  245. // region 3
  246. s = bmd / a;
  247. }
  248. }
  249. else // 0 < t < 1
  250. {
  251. // The point (0,e/c) is on the line and domain, so we have
  252. // one point at which R is a minimum.
  253. s = zero;
  254. t = e / c;
  255. }
  256. }
  257. Result result;
  258. result.parameter[0] = s;
  259. result.parameter[1] = t;
  260. result.closest[0] = P0 + s * P1mP0;
  261. result.closest[1] = Q0 + t * Q1mQ0;
  262. Vector<N, Rational> diff = result.closest[1] - result.closest[0];
  263. result.sqrDistance = Dot(diff, diff);
  264. return result;
  265. }
  266. };
  267. }