DistSegmentSegment.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 is
  12. // robust even for nearly parallel segments. Effectively, it uses a conjugate
  13. // gradient search for the minimum of the squared distance function, which
  14. // avoids the numerical problems introduced by divisions in the case the
  15. // minimum is located at an interior point of the domain. See the document
  16. // https://www.geometrictools.com/Documentation/DistanceLine3Line3.pdf
  17. // for details.
  18. namespace WwiseGTE
  19. {
  20. template <int N, typename Real>
  21. class DCPQuery<Real, Segment<N, Real>, Segment<N, Real>>
  22. {
  23. public:
  24. struct Result
  25. {
  26. Real distance, sqrDistance;
  27. Real parameter[2];
  28. Vector<N, Real> closest[2];
  29. };
  30. Result operator()(Segment<N, Real> const& segment0,
  31. Segment<N, Real> const& segment1)
  32. {
  33. return operator()(segment0.p[0], segment0.p[1], segment1.p[0], segment1.p[1]);
  34. }
  35. Result operator()(Vector<N, Real> const& P0, Vector<N, Real> const& P1,
  36. Vector<N, Real> const& Q0, Vector<N, Real> const& Q1)
  37. {
  38. Result result;
  39. // The code allows degenerate line segments; that is, P0 and P1
  40. // can be the same point or Q0 and Q1 can be the same point. The
  41. // quadratic function for squared distance between the segment is
  42. // R(s,t) = a*s^2 - 2*b*s*t + c*t^2 + 2*d*s - 2*e*t + f
  43. // for (s,t) in [0,1]^2 where
  44. // a = Dot(P1-P0,P1-P0), b = Dot(P1-P0,Q1-Q0), c = Dot(Q1-Q0,Q1-Q0),
  45. // d = Dot(P1-P0,P0-Q0), e = Dot(Q1-Q0,P0-Q0), f = Dot(P0-Q0,P0-Q0)
  46. Vector<N, Real> P1mP0 = P1 - P0;
  47. Vector<N, Real> Q1mQ0 = Q1 - Q0;
  48. Vector<N, Real> P0mQ0 = P0 - Q0;
  49. mA = Dot(P1mP0, P1mP0);
  50. mB = Dot(P1mP0, Q1mQ0);
  51. mC = Dot(Q1mQ0, Q1mQ0);
  52. mD = Dot(P1mP0, P0mQ0);
  53. mE = Dot(Q1mQ0, P0mQ0);
  54. mF00 = mD;
  55. mF10 = mF00 + mA;
  56. mF01 = mF00 - mB;
  57. mF11 = mF10 - mB;
  58. mG00 = -mE;
  59. mG10 = mG00 - mB;
  60. mG01 = mG00 + mC;
  61. mG11 = mG10 + mC;
  62. if (mA > (Real)0 && mC > (Real)0)
  63. {
  64. // Compute the solutions to dR/ds(s0,0) = 0 and
  65. // dR/ds(s1,1) = 0. The location of sI on the s-axis is
  66. // stored in classifyI (I = 0 or 1). If sI <= 0, classifyI
  67. // is -1. If sI >= 1, classifyI is 1. If 0 < sI < 1,
  68. // classifyI is 0. This information helps determine where to
  69. // search for the minimum point (s,t). The fij values are
  70. // dR/ds(i,j) for i and j in {0,1}.
  71. Real sValue[2];
  72. sValue[0] = GetClampedRoot(mA, mF00, mF10);
  73. sValue[1] = GetClampedRoot(mA, mF01, mF11);
  74. int classify[2];
  75. for (int i = 0; i < 2; ++i)
  76. {
  77. if (sValue[i] <= (Real)0)
  78. {
  79. classify[i] = -1;
  80. }
  81. else if (sValue[i] >= (Real)1)
  82. {
  83. classify[i] = +1;
  84. }
  85. else
  86. {
  87. classify[i] = 0;
  88. }
  89. }
  90. if (classify[0] == -1 && classify[1] == -1)
  91. {
  92. // The minimum must occur on s = 0 for 0 <= t <= 1.
  93. result.parameter[0] = (Real)0;
  94. result.parameter[1] = GetClampedRoot(mC, mG00, mG01);
  95. }
  96. else if (classify[0] == +1 && classify[1] == +1)
  97. {
  98. // The minimum must occur on s = 1 for 0 <= t <= 1.
  99. result.parameter[0] = (Real)1;
  100. result.parameter[1] = GetClampedRoot(mC, mG10, mG11);
  101. }
  102. else
  103. {
  104. // The line dR/ds = 0 intersects the domain [0,1]^2 in a
  105. // nondegenerate segment. Compute the endpoints of that
  106. // segment, end[0] and end[1]. The edge[i] flag tells you
  107. // on which domain edge end[i] lives: 0 (s=0), 1 (s=1),
  108. // 2 (t=0), 3 (t=1).
  109. int edge[2];
  110. Real end[2][2];
  111. ComputeIntersection(sValue, classify, edge, end);
  112. // The directional derivative of R along the segment of
  113. // intersection is
  114. // H(z) = (end[1][1]-end[1][0]) *
  115. // dR/dt((1-z)*end[0] + z*end[1])
  116. // for z in [0,1]. The formula uses the fact that
  117. // dR/ds = 0 on the segment. Compute the minimum of
  118. // H on [0,1].
  119. ComputeMinimumParameters(edge, end, result.parameter);
  120. }
  121. }
  122. else
  123. {
  124. if (mA > (Real)0)
  125. {
  126. // The Q-segment is degenerate (Q0 and Q1 are the same
  127. // point) and the quadratic is R(s,0) = a*s^2 + 2*d*s + f
  128. // and has (half) first derivative F(t) = a*s + d. The
  129. // closest P-point is interior to the P-segment when
  130. // F(0) < 0 and F(1) > 0.
  131. result.parameter[0] = GetClampedRoot(mA, mF00, mF10);
  132. result.parameter[1] = (Real)0;
  133. }
  134. else if (mC > (Real)0)
  135. {
  136. // The P-segment is degenerate (P0 and P1 are the same
  137. // point) and the quadratic is R(0,t) = c*t^2 - 2*e*t + f
  138. // and has (half) first derivative G(t) = c*t - e. The
  139. // closest Q-point is interior to the Q-segment when
  140. // G(0) < 0 and G(1) > 0.
  141. result.parameter[0] = (Real)0;
  142. result.parameter[1] = GetClampedRoot(mC, mG00, mG01);
  143. }
  144. else
  145. {
  146. // P-segment and Q-segment are degenerate.
  147. result.parameter[0] = (Real)0;
  148. result.parameter[1] = (Real)0;
  149. }
  150. }
  151. result.closest[0] =
  152. ((Real)1 - result.parameter[0]) * P0 + result.parameter[0] * P1;
  153. result.closest[1] =
  154. ((Real)1 - result.parameter[1]) * Q0 + result.parameter[1] * Q1;
  155. Vector<N, Real> diff = result.closest[0] - result.closest[1];
  156. result.sqrDistance = Dot(diff, diff);
  157. result.distance = std::sqrt(result.sqrDistance);
  158. return result;
  159. }
  160. private:
  161. // Compute the root of h(z) = h0 + slope*z and clamp it to the interval
  162. // [0,1]. It is required that for h1 = h(1), either (h0 < 0 and h1 > 0)
  163. // or (h0 > 0 and h1 < 0).
  164. Real GetClampedRoot(Real slope, Real h0, Real h1)
  165. {
  166. // Theoretically, r is in (0,1). However, when the slope is
  167. // nearly zero, then so are h0 and h1. Significant numerical
  168. // rounding problems can occur when using floating-point
  169. // arithmetic. If the rounding causes r to be outside the
  170. // interval, clamp it. It is possible that r is in (0,1) and has
  171. // rounding errors, but because h0 and h1 are both nearly zero,
  172. // the quadratic is nearly constant on (0,1). Any choice of p
  173. // should not cause undesirable accuracy problems for the final
  174. // distance computation.
  175. //
  176. // NOTE: You can use bisection to recompute the root or even use
  177. // bisection to compute the root and skip the division. This is
  178. // generally slower, which might be a problem for high-performance
  179. // applications.
  180. Real r;
  181. if (h0 < (Real)0)
  182. {
  183. if (h1 > (Real)0)
  184. {
  185. r = -h0 / slope;
  186. if (r > (Real)1)
  187. {
  188. r = (Real)0.5;
  189. }
  190. // The slope is positive and -h0 is positive, so there is
  191. // no need to test for a negative value and clamp it.
  192. }
  193. else
  194. {
  195. r = (Real)1;
  196. }
  197. }
  198. else
  199. {
  200. r = (Real)0;
  201. }
  202. return r;
  203. }
  204. // Compute the intersection of the line dR/ds = 0 with the domain
  205. // [0,1]^2. The direction of the line dR/ds is conjugate to (1,0),
  206. // so the algorithm for minimization is effectively the conjugate
  207. // gradient algorithm for a quadratic function.
  208. void ComputeIntersection(Real const sValue[2], int const classify[2],
  209. int edge[2], Real end[2][2])
  210. {
  211. // The divisions are theoretically numbers in [0,1]. Numerical
  212. // rounding errors might cause the result to be outside the
  213. // interval. When this happens, it must be that both numerator
  214. // and denominator are nearly zero. The denominator is nearly
  215. // zero when the segments are nearly perpendicular. The
  216. // numerator is nearly zero when the P-segment is nearly
  217. // degenerate (mF00 = a is small). The choice of 0.5 should not
  218. // cause significant accuracy problems.
  219. //
  220. // NOTE: You can use bisection to recompute the root or even use
  221. // bisection to compute the root and skip the division. This is
  222. // generally slower, which might be a problem for high-performance
  223. // applications.
  224. if (classify[0] < 0)
  225. {
  226. edge[0] = 0;
  227. end[0][0] = (Real)0;
  228. end[0][1] = mF00 / mB;
  229. if (end[0][1] < (Real)0 || end[0][1] > (Real)1)
  230. {
  231. end[0][1] = (Real)0.5;
  232. }
  233. if (classify[1] == 0)
  234. {
  235. edge[1] = 3;
  236. end[1][0] = sValue[1];
  237. end[1][1] = (Real)1;
  238. }
  239. else // classify[1] > 0
  240. {
  241. edge[1] = 1;
  242. end[1][0] = (Real)1;
  243. end[1][1] = mF10 / mB;
  244. if (end[1][1] < (Real)0 || end[1][1] > (Real)1)
  245. {
  246. end[1][1] = (Real)0.5;
  247. }
  248. }
  249. }
  250. else if (classify[0] == 0)
  251. {
  252. edge[0] = 2;
  253. end[0][0] = sValue[0];
  254. end[0][1] = (Real)0;
  255. if (classify[1] < 0)
  256. {
  257. edge[1] = 0;
  258. end[1][0] = (Real)0;
  259. end[1][1] = mF00 / mB;
  260. if (end[1][1] < (Real)0 || end[1][1] > (Real)1)
  261. {
  262. end[1][1] = (Real)0.5;
  263. }
  264. }
  265. else if (classify[1] == 0)
  266. {
  267. edge[1] = 3;
  268. end[1][0] = sValue[1];
  269. end[1][1] = (Real)1;
  270. }
  271. else
  272. {
  273. edge[1] = 1;
  274. end[1][0] = (Real)1;
  275. end[1][1] = mF10 / mB;
  276. if (end[1][1] < (Real)0 || end[1][1] > (Real)1)
  277. {
  278. end[1][1] = (Real)0.5;
  279. }
  280. }
  281. }
  282. else // classify[0] > 0
  283. {
  284. edge[0] = 1;
  285. end[0][0] = (Real)1;
  286. end[0][1] = mF10 / mB;
  287. if (end[0][1] < (Real)0 || end[0][1] > (Real)1)
  288. {
  289. end[0][1] = (Real)0.5;
  290. }
  291. if (classify[1] == 0)
  292. {
  293. edge[1] = 3;
  294. end[1][0] = sValue[1];
  295. end[1][1] = (Real)1;
  296. }
  297. else
  298. {
  299. edge[1] = 0;
  300. end[1][0] = (Real)0;
  301. end[1][1] = mF00 / mB;
  302. if (end[1][1] < (Real)0 || end[1][1] > (Real)1)
  303. {
  304. end[1][1] = (Real)0.5;
  305. }
  306. }
  307. }
  308. }
  309. // Compute the location of the minimum of R on the segment of
  310. // intersection for the line dR/ds = 0 and the domain [0,1]^2.
  311. void ComputeMinimumParameters(int const edge[2], Real const end[2][2],
  312. Real parameter[2])
  313. {
  314. Real delta = end[1][1] - end[0][1];
  315. Real h0 = delta * (-mB * end[0][0] + mC * end[0][1] - mE);
  316. if (h0 >= (Real)0)
  317. {
  318. if (edge[0] == 0)
  319. {
  320. parameter[0] = (Real)0;
  321. parameter[1] = GetClampedRoot(mC, mG00, mG01);
  322. }
  323. else if (edge[0] == 1)
  324. {
  325. parameter[0] = (Real)1;
  326. parameter[1] = GetClampedRoot(mC, mG10, mG11);
  327. }
  328. else
  329. {
  330. parameter[0] = end[0][0];
  331. parameter[1] = end[0][1];
  332. }
  333. }
  334. else
  335. {
  336. Real h1 = delta * (-mB * end[1][0] + mC * end[1][1] - mE);
  337. if (h1 <= (Real)0)
  338. {
  339. if (edge[1] == 0)
  340. {
  341. parameter[0] = (Real)0;
  342. parameter[1] = GetClampedRoot(mC, mG00, mG01);
  343. }
  344. else if (edge[1] == 1)
  345. {
  346. parameter[0] = (Real)1;
  347. parameter[1] = GetClampedRoot(mC, mG10, mG11);
  348. }
  349. else
  350. {
  351. parameter[0] = end[1][0];
  352. parameter[1] = end[1][1];
  353. }
  354. }
  355. else // h0 < 0 and h1 > 0
  356. {
  357. Real z = std::min(std::max(h0 / (h0 - h1), (Real)0), (Real)1);
  358. Real omz = (Real)1 - z;
  359. parameter[0] = omz * end[0][0] + z * end[1][0];
  360. parameter[1] = omz * end[0][1] + z * end[1][1];
  361. }
  362. }
  363. }
  364. // The coefficients of R(s,t), not including the constant term.
  365. Real mA, mB, mC, mD, mE;
  366. // dR/ds(i,j) at the four corners of the domain
  367. Real mF00, mF10, mF01, mF11;
  368. // dR/dt(i,j) at the four corners of the domain
  369. Real mG00, mG10, mG01, mG11;
  370. };
  371. // Template aliases for convenience.
  372. template <int N, typename Real>
  373. using DCPSegmentSegment = DCPQuery<Real, Segment<N, Real>, Segment<N, Real>>;
  374. template <typename Real>
  375. using DCPSegment2Segment2 = DCPSegmentSegment<2, Real>;
  376. template <typename Real>
  377. using DCPSegment3Segment3 = DCPSegmentSegment<3, Real>;
  378. }