ClosestPoint.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. JPH_NAMESPACE_BEGIN
  5. // Turn off fused multiply add instruction because it makes the equations of the form a * b - c * d inaccurate below
  6. JPH_PRECISE_MATH_ON
  7. /// Helper utils to find the closest point to a line segment, triangle or tetrahedron
  8. namespace ClosestPoint
  9. {
  10. /// Compute barycentric coordinates of closest point to origin for infinite line defined by (inA, inB)
  11. /// Point can then be computed as inA * outU + inB * outV
  12. inline void GetBaryCentricCoordinates(Vec3Arg inA, Vec3Arg inB, float &outU, float &outV)
  13. {
  14. Vec3 ab = inB - inA;
  15. float denominator = ab.LengthSq();
  16. if (denominator < Square(FLT_EPSILON))
  17. {
  18. // Degenerate line segment, fallback to points
  19. if (inA.LengthSq() < inB.LengthSq())
  20. {
  21. // A closest
  22. outU = 1.0f;
  23. outV = 0.0f;
  24. }
  25. else
  26. {
  27. // B closest
  28. outU = 0.0f;
  29. outV = 1.0f;
  30. }
  31. }
  32. else
  33. {
  34. outV = -inA.Dot(ab) / denominator;
  35. outU = 1.0f - outV;
  36. }
  37. }
  38. /// Compute barycentric coordinates of closest point to origin for plane defined by (inA, inB, inC)
  39. /// Point can then be computed as inA * outU + inB * outV + inC * outW
  40. inline void GetBaryCentricCoordinates(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, float &outU, float &outV, float &outW)
  41. {
  42. // Taken from: Real-Time Collision Detection - Christer Ericson (Section: Barycentric Coordinates)
  43. // With p = 0
  44. // Adjusted to always include the shortest edge of the triangle in the calculation to improve numerical accuracy
  45. // First calculate the three edges
  46. Vec3 v0 = inB - inA;
  47. Vec3 v1 = inC - inA;
  48. Vec3 v2 = inC - inB;
  49. // Make sure that the shortest edge is included in the calculation to keep the products a * b - c * d as small as possible to preserve accuracy
  50. float d00 = v0.Dot(v0);
  51. float d11 = v1.Dot(v1);
  52. float d22 = v2.Dot(v2);
  53. if (d00 <= d22)
  54. {
  55. // Use v0 and v1 to calculate barycentric coordinates
  56. float d01 = v0.Dot(v1);
  57. float denominator = d00 * d11 - d01 * d01;
  58. if (abs(denominator) < FLT_EPSILON)
  59. {
  60. // Degenerate triangle, return coordinates along longest edge
  61. if (d00 > d11)
  62. {
  63. GetBaryCentricCoordinates(inA, inB, outU, outV);
  64. outW = 0.0f;
  65. }
  66. else
  67. {
  68. GetBaryCentricCoordinates(inA, inC, outU, outW);
  69. outV = 0.0f;
  70. }
  71. }
  72. else
  73. {
  74. float a0 = inA.Dot(v0);
  75. float a1 = inA.Dot(v1);
  76. outV = (d01 * a1 - d11 * a0) / denominator;
  77. outW = (d01 * a0 - d00 * a1) / denominator;
  78. outU = 1.0f - outV - outW;
  79. }
  80. }
  81. else
  82. {
  83. // Use v1 and v2 to calculate barycentric coordinates
  84. float d12 = v1.Dot(v2);
  85. float denominator = d11 * d22 - d12 * d12;
  86. if (abs(denominator) < FLT_EPSILON)
  87. {
  88. // Degenerate triangle, return coordinates along longest edge
  89. if (d11 > d22)
  90. {
  91. GetBaryCentricCoordinates(inA, inC, outU, outW);
  92. outV = 0.0f;
  93. }
  94. else
  95. {
  96. GetBaryCentricCoordinates(inB, inC, outV, outW);
  97. outU = 0.0f;
  98. }
  99. }
  100. else
  101. {
  102. float c1 = inC.Dot(v1);
  103. float c2 = inC.Dot(v2);
  104. outU = (d22 * c1 - d12 * c2) / denominator;
  105. outV = (d11 * c2 - d12 * c1) / denominator;
  106. outW = 1.0f - outU - outV;
  107. }
  108. }
  109. }
  110. /// Get the closest point to the origin of line (inA, inB)
  111. /// outSet describes which features are closest: 1 = a, 2 = b, 3 = line segment ab
  112. inline Vec3 GetClosestPointOnLine(Vec3Arg inA, Vec3Arg inB, uint32 &outSet)
  113. {
  114. float u, v;
  115. GetBaryCentricCoordinates(inA, inB, u, v);
  116. if (v <= 0.0f)
  117. {
  118. // inA is closest point
  119. outSet = 0b0001;
  120. return inA;
  121. }
  122. else if (u <= 0.0f)
  123. {
  124. // inB is closest point
  125. outSet = 0b0010;
  126. return inB;
  127. }
  128. else
  129. {
  130. // Closest point lies on line inA inB
  131. outSet = 0b0011;
  132. return u * inA + v * inB;
  133. }
  134. }
  135. /// Get the closest point to the origin of triangle (inA, inB, inC)
  136. /// outSet describes which features are closest: 1 = a, 2 = b, 4 = c, 5 = line segment ac, 7 = triangle interior etc.
  137. inline Vec3 GetClosestPointOnTriangle(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, uint32 &outSet)
  138. {
  139. // Taken from: Real-Time Collision Detection - Christer Ericson (Section: Closest Point on Triangle to Point)
  140. // With p = 0
  141. // Calculate edges
  142. Vec3 ab = inB - inA;
  143. Vec3 ac = inC - inA;
  144. Vec3 bc = inC - inB;
  145. // The most accurate normal is calculated by using the two shortest edges
  146. // See: https://box2d.org/posts/2014/01/troublesome-triangle/
  147. // The difference in normals is most pronounced when one edge is much smaller than the others (in which case the other 2 must have roughly the same length).
  148. // Therefore we can suffice by just picking the shortest from 2 edges and use that with the 3rd edge to calculate the normal.
  149. // We first check which of the edges is shorter
  150. UVec4 bc_shorter_than_ac = Vec4::sLess(bc.DotV4(bc), ac.DotV4(ac));
  151. // We calculate both normals and then select the one that had the shortest edge for our normal (this avoids branching)
  152. Vec3 normal_bc = ab.Cross(bc);
  153. Vec3 normal_ac = ab.Cross(ac);
  154. Vec3 n = Vec3::sSelect(normal_ac, normal_bc, bc_shorter_than_ac);
  155. float n_len_sq = n.LengthSq();
  156. // Check degenerate
  157. if (n_len_sq < 1.0e-13f) // Square(FLT_EPSILON) was too small and caused numerical problems, see test case TestCollideParallelTriangleVsCapsule
  158. {
  159. // Degenerate, fallback to edges
  160. // Edge AB
  161. uint32 closest_set;
  162. Vec3 closest_point = GetClosestPointOnLine(inA, inB, closest_set);
  163. float best_dist_sq = closest_point.LengthSq();
  164. // Edge AC
  165. uint32 set;
  166. Vec3 q = GetClosestPointOnLine(inA, inC, set);
  167. float dist_sq = q.LengthSq();
  168. if (dist_sq < best_dist_sq)
  169. {
  170. closest_point = q;
  171. best_dist_sq = dist_sq;
  172. closest_set = (set & 0b0001) + ((set & 0b0010) << 1);
  173. }
  174. // Edge BC
  175. q = GetClosestPointOnLine(inB, inC, set);
  176. dist_sq = q.LengthSq();
  177. if (dist_sq < best_dist_sq)
  178. {
  179. closest_point = q;
  180. closest_set = set << 1;
  181. }
  182. outSet = closest_set;
  183. return closest_point;
  184. }
  185. // Check if P in vertex region outside A
  186. Vec3 ap = -inA;
  187. float d1 = ab.Dot(ap);
  188. float d2 = ac.Dot(ap);
  189. if (d1 <= 0.0f && d2 <= 0.0f)
  190. {
  191. outSet = 0b0001;
  192. return inA; // barycentric coordinates (1,0,0)
  193. }
  194. // Check if P in vertex region outside B
  195. Vec3 bp = -inB;
  196. float d3 = ab.Dot(bp);
  197. float d4 = ac.Dot(bp);
  198. if (d3 >= 0.0f && d4 <= d3)
  199. {
  200. outSet = 0b0010;
  201. return inB; // barycentric coordinates (0,1,0)
  202. }
  203. // Check if P in edge region of AB, if so return projection of P onto AB
  204. float vc = d1 * d4 - d3 * d2;
  205. if (vc <= 0.0f && d1 >= 0.0f && d3 <= 0.0f)
  206. {
  207. float v = d1 / (d1 - d3);
  208. outSet = 0b0011;
  209. return inA + v * ab; // barycentric coordinates (1-v,v,0)
  210. }
  211. // Check if P in vertex region outside C
  212. Vec3 cp = -inC;
  213. float d5 = ab.Dot(cp);
  214. float d6 = ac.Dot(cp);
  215. if (d6 >= 0.0f && d5 <= d6)
  216. {
  217. outSet = 0b0100;
  218. return inC; // barycentric coordinates (0,0,1)
  219. }
  220. // Check if P in edge region of AC, if so return projection of P onto AC
  221. float vb = d5 * d2 - d1 * d6;
  222. if (vb <= 0.0f && d2 >= 0.0f && d6 <= 0.0f)
  223. {
  224. float w = d2 / (d2 - d6);
  225. outSet = 0b0101;
  226. return inA + w * ac; // barycentric coordinates (1-w,0,w)
  227. }
  228. // Check if P in edge region of BC, if so return projection of P onto BC
  229. float va = d3 * d6 - d5 * d4;
  230. float d4_d3 = d4 - d3;
  231. float d5_d6 = d5 - d6;
  232. if (va <= 0.0f && d4_d3 >= 0.0f && d5_d6 >= 0.0f)
  233. {
  234. float w = d4_d3 / (d4_d3 + d5_d6);
  235. outSet = 0b0110;
  236. return inB + w * bc; // barycentric coordinates (0,1-w,w)
  237. }
  238. // P inside face region.
  239. // Here we deviate from Christer Ericson's article to improve accuracy.
  240. // Determine distance between triangle and origin: distance = (centroid - origin) . normal / |normal|
  241. // Closest point to origin is then: distance . normal / |normal|
  242. // Note that this way of calculating the closest point is much more accurate than first calculating barycentric coordinates
  243. // and then calculating the closest point based on those coordinates.
  244. outSet = 0b0111;
  245. return n * (inA + inB + inC).Dot(n) / (3.0f * n_len_sq);
  246. }
  247. /// Check if the origin is outside the plane of triangle (inA, inB, inC). inD specifies the front side of the plane.
  248. inline bool OriginOutsideOfPlane(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD)
  249. {
  250. // Taken from: Real-Time Collision Detection - Christer Ericson (Section: Closest Point on Tetrahedron to Point)
  251. // With p = 0
  252. // Test if point p and d lie on opposite sides of plane through abc
  253. Vec3 n = (inB - inA).Cross(inC - inA);
  254. float signp = inA.Dot(n); // [AP AB AC]
  255. float signd = (inD - inA).Dot(n); // [AD AB AC]
  256. // Points on opposite sides if expression signs are the same
  257. // Note that we left out the minus sign in signp so we need to check > 0 instead of < 0 as in Christer's book
  258. // We compare against a small negative value to allow for a little bit of slop in the calculations
  259. return signp * signd > -FLT_EPSILON;
  260. }
  261. /// Returns for each of the planes of the tetrahedron if the origin is inside it
  262. /// Roughly equivalent to:
  263. /// [OriginOutsideOfPlane(inA, inB, inC, inD),
  264. /// OriginOutsideOfPlane(inA, inC, inD, inB),
  265. /// OriginOutsideOfPlane(inA, inD, inB, inC),
  266. /// OriginOutsideOfPlane(inB, inD, inC, inA)]
  267. inline UVec4 OriginOutsideOfTetrahedronPlanes(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD)
  268. {
  269. Vec3 ab = inB - inA;
  270. Vec3 ac = inC - inA;
  271. Vec3 ad = inD - inA;
  272. Vec3 bd = inD - inB;
  273. Vec3 bc = inC - inB;
  274. Vec3 ab_cross_ac = ab.Cross(ac);
  275. Vec3 ac_cross_ad = ac.Cross(ad);
  276. Vec3 ad_cross_ab = ad.Cross(ab);
  277. Vec3 bd_cross_bc = bd.Cross(bc);
  278. // For each plane get the side on which the origin is
  279. float signp0 = inA.Dot(ab_cross_ac); // ABC
  280. float signp1 = inA.Dot(ac_cross_ad); // ACD
  281. float signp2 = inA.Dot(ad_cross_ab); // ADB
  282. float signp3 = inB.Dot(bd_cross_bc); // BDC
  283. Vec4 signp(signp0, signp1, signp2, signp3);
  284. // For each plane get the side that is outside (determined by the 4th point)
  285. float signd0 = ad.Dot(ab_cross_ac); // D
  286. float signd1 = ab.Dot(ac_cross_ad); // B
  287. float signd2 = ac.Dot(ad_cross_ab); // C
  288. float signd3 = -ab.Dot(bd_cross_bc); // A
  289. Vec4 signd(signd0, signd1, signd2, signd3);
  290. // The winding of all triangles has been chosen so that signd should have the
  291. // same sign for all components. If this is not the case the tetrahedron
  292. // is degenerate and we return that the origin is in front of all sides
  293. int sign_bits = signd.GetSignBits();
  294. switch (sign_bits)
  295. {
  296. case 0:
  297. // All positive
  298. return Vec4::sGreaterOrEqual(signp, Vec4::sReplicate(-FLT_EPSILON));
  299. case 0xf:
  300. // All negative
  301. return Vec4::sLessOrEqual(signp, Vec4::sReplicate(FLT_EPSILON));
  302. default:
  303. // Mixed signs, degenerate tetrahedron
  304. return UVec4::sReplicate(0xffffffff);
  305. }
  306. }
  307. /// Get the closest point between tetrahedron (inA, inB, inC, inD) to the origin
  308. /// outSet specifies which feature was closest, 1 = a, 2 = b, 4 = c, 8 = d. Edges have 2 bits set, triangles 3 and if the point is in the interior 4 bits are set.
  309. inline Vec3 GetClosestPointOnTetrahedron(Vec3Arg inA, Vec3Arg inB, Vec3Arg inC, Vec3Arg inD, uint32 &outSet)
  310. {
  311. // Taken from: Real-Time Collision Detection - Christer Ericson (Section: Closest Point on Tetrahedron to Point)
  312. // With p = 0
  313. // Start out assuming point inside all halfspaces, so closest to itself
  314. uint32 closest_set = 0b1111;
  315. Vec3 closest_point = Vec3::sZero();
  316. float best_dist_sq = FLT_MAX;
  317. // Determine for each of the faces of the tetrahedron if the origin is in front of the plane
  318. UVec4 origin_out_of_planes = OriginOutsideOfTetrahedronPlanes(inA, inB, inC, inD);
  319. // If point outside face abc then compute closest point on abc
  320. if (origin_out_of_planes.GetX()) // OriginOutsideOfPlane(inA, inB, inC, inD)
  321. {
  322. Vec3 q = GetClosestPointOnTriangle(inA, inB, inC, closest_set);
  323. float dist_sq = q.LengthSq();
  324. // Update best closest point if (squared) distance is less than current best
  325. if (dist_sq < best_dist_sq)
  326. {
  327. best_dist_sq = dist_sq;
  328. closest_point = q;
  329. }
  330. }
  331. // Repeat test for face acd
  332. if (origin_out_of_planes.GetY()) // OriginOutsideOfPlane(inA, inC, inD, inB)
  333. {
  334. uint32 set;
  335. Vec3 q = GetClosestPointOnTriangle(inA, inC, inD, set);
  336. float dist_sq = q.LengthSq();
  337. if (dist_sq < best_dist_sq)
  338. {
  339. best_dist_sq = dist_sq;
  340. closest_point = q;
  341. closest_set = (set & 0b0001) + ((set & 0b0110) << 1);
  342. }
  343. }
  344. // Repeat test for face adb
  345. if (origin_out_of_planes.GetZ()) // OriginOutsideOfPlane(inA, inD, inB, inC)
  346. {
  347. uint32 set;
  348. Vec3 q = GetClosestPointOnTriangle(inA, inD, inB, set);
  349. float dist_sq = q.LengthSq();
  350. if (dist_sq < best_dist_sq)
  351. {
  352. best_dist_sq = dist_sq;
  353. closest_point = q;
  354. closest_set = (set & 0b0001) + ((set & 0b0010) << 2) + ((set & 0b0100) >> 1);
  355. }
  356. }
  357. // Repeat test for face bdc
  358. if (origin_out_of_planes.GetW()) // OriginOutsideOfPlane(inB, inD, inC, inA)
  359. {
  360. uint32 set;
  361. Vec3 q = GetClosestPointOnTriangle(inB, inD, inC, set);
  362. float dist_sq = q.LengthSq();
  363. if (dist_sq < best_dist_sq)
  364. {
  365. closest_point = q;
  366. closest_set = ((set & 0b0001) << 1) + ((set & 0b0010) << 2) + (set & 0b0100);
  367. }
  368. }
  369. outSet = closest_set;
  370. return closest_point;
  371. }
  372. };
  373. JPH_PRECISE_MATH_OFF
  374. JPH_NAMESPACE_END