btGjkEpa2.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2008 Erwin Coumans http://continuousphysics.com/Bullet/
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the
  6. use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely,
  10. subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not
  12. claim that you wrote the original software. If you use this software in a
  13. product, an acknowledgment in the product documentation would be appreciated
  14. but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. /*
  20. GJK-EPA collision solver by Nathanael Presson, 2008
  21. */
  22. #include "BulletCollision/CollisionShapes/btConvexInternalShape.h"
  23. #include "BulletCollision/CollisionShapes/btSphereShape.h"
  24. #include "btGjkEpa2.h"
  25. #if defined(DEBUG) || defined(_DEBUG)
  26. #include <stdio.h> //for debug printf
  27. #ifdef __SPU__
  28. #include <spu_printf.h>
  29. #define printf spu_printf
  30. #endif //__SPU__
  31. #endif
  32. namespace gjkepa2_impl
  33. {
  34. // Config
  35. /* GJK */
  36. #define GJK_MAX_ITERATIONS 128
  37. #ifdef BT_USE_DOUBLE_PRECISION
  38. #define GJK_ACCURACY ((btScalar)1e-12)
  39. #define GJK_MIN_DISTANCE ((btScalar)1e-12)
  40. #define GJK_DUPLICATED_EPS ((btScalar)1e-12)
  41. #else
  42. #define GJK_ACCURACY ((btScalar)0.0001)
  43. #define GJK_MIN_DISTANCE ((btScalar)0.0001)
  44. #define GJK_DUPLICATED_EPS ((btScalar)0.0001)
  45. #endif //BT_USE_DOUBLE_PRECISION
  46. #define GJK_SIMPLEX2_EPS ((btScalar)0.0)
  47. #define GJK_SIMPLEX3_EPS ((btScalar)0.0)
  48. #define GJK_SIMPLEX4_EPS ((btScalar)0.0)
  49. /* EPA */
  50. #define EPA_MAX_VERTICES 128
  51. #define EPA_MAX_ITERATIONS 255
  52. #ifdef BT_USE_DOUBLE_PRECISION
  53. #define EPA_ACCURACY ((btScalar)1e-12)
  54. #define EPA_PLANE_EPS ((btScalar)1e-14)
  55. #define EPA_INSIDE_EPS ((btScalar)1e-9)
  56. #else
  57. #define EPA_ACCURACY ((btScalar)0.0001)
  58. #define EPA_PLANE_EPS ((btScalar)0.00001)
  59. #define EPA_INSIDE_EPS ((btScalar)0.01)
  60. #endif
  61. #define EPA_FALLBACK (10 * EPA_ACCURACY)
  62. #define EPA_MAX_FACES (EPA_MAX_VERTICES * 2)
  63. // Shorthands
  64. typedef unsigned int U;
  65. typedef unsigned char U1;
  66. // MinkowskiDiff
  67. struct MinkowskiDiff
  68. {
  69. const btConvexShape* m_shapes[2];
  70. btMatrix3x3 m_toshape1;
  71. btTransform m_toshape0;
  72. #ifdef __SPU__
  73. bool m_enableMargin;
  74. #else
  75. btVector3 (btConvexShape::*Ls)(const btVector3&) const;
  76. #endif //__SPU__
  77. MinkowskiDiff()
  78. {
  79. }
  80. #ifdef __SPU__
  81. void EnableMargin(bool enable)
  82. {
  83. m_enableMargin = enable;
  84. }
  85. inline btVector3 Support0(const btVector3& d) const
  86. {
  87. if (m_enableMargin)
  88. {
  89. return m_shapes[0]->localGetSupportVertexNonVirtual(d);
  90. }
  91. else
  92. {
  93. return m_shapes[0]->localGetSupportVertexWithoutMarginNonVirtual(d);
  94. }
  95. }
  96. inline btVector3 Support1(const btVector3& d) const
  97. {
  98. if (m_enableMargin)
  99. {
  100. return m_toshape0 * (m_shapes[1]->localGetSupportVertexNonVirtual(m_toshape1 * d));
  101. }
  102. else
  103. {
  104. return m_toshape0 * (m_shapes[1]->localGetSupportVertexWithoutMarginNonVirtual(m_toshape1 * d));
  105. }
  106. }
  107. #else
  108. void EnableMargin(bool enable)
  109. {
  110. if (enable)
  111. Ls = &btConvexShape::localGetSupportVertexNonVirtual;
  112. else
  113. Ls = &btConvexShape::localGetSupportVertexWithoutMarginNonVirtual;
  114. }
  115. inline btVector3 Support0(const btVector3& d) const
  116. {
  117. return (((m_shapes[0])->*(Ls))(d));
  118. }
  119. inline btVector3 Support1(const btVector3& d) const
  120. {
  121. return (m_toshape0 * ((m_shapes[1])->*(Ls))(m_toshape1 * d));
  122. }
  123. #endif //__SPU__
  124. inline btVector3 Support(const btVector3& d) const
  125. {
  126. return (Support0(d) - Support1(-d));
  127. }
  128. btVector3 Support(const btVector3& d, U index) const
  129. {
  130. if (index)
  131. return (Support1(d));
  132. else
  133. return (Support0(d));
  134. }
  135. };
  136. typedef MinkowskiDiff tShape;
  137. // GJK
  138. struct GJK
  139. {
  140. /* Types */
  141. struct sSV
  142. {
  143. btVector3 d, w;
  144. };
  145. struct sSimplex
  146. {
  147. sSV* c[4];
  148. btScalar p[4];
  149. U rank;
  150. };
  151. struct eStatus
  152. {
  153. enum _
  154. {
  155. Valid,
  156. Inside,
  157. Failed
  158. };
  159. };
  160. /* Fields */
  161. tShape m_shape;
  162. btVector3 m_ray;
  163. btScalar m_distance;
  164. sSimplex m_simplices[2];
  165. sSV m_store[4];
  166. sSV* m_free[4];
  167. U m_nfree;
  168. U m_current;
  169. sSimplex* m_simplex;
  170. eStatus::_ m_status;
  171. /* Methods */
  172. GJK()
  173. {
  174. Initialize();
  175. }
  176. void Initialize()
  177. {
  178. m_ray = btVector3(0, 0, 0);
  179. m_nfree = 0;
  180. m_status = eStatus::Failed;
  181. m_current = 0;
  182. m_distance = 0;
  183. }
  184. eStatus::_ Evaluate(const tShape& shapearg, const btVector3& guess)
  185. {
  186. U iterations = 0;
  187. btScalar sqdist = 0;
  188. btScalar alpha = 0;
  189. btVector3 lastw[4];
  190. U clastw = 0;
  191. /* Initialize solver */
  192. m_free[0] = &m_store[0];
  193. m_free[1] = &m_store[1];
  194. m_free[2] = &m_store[2];
  195. m_free[3] = &m_store[3];
  196. m_nfree = 4;
  197. m_current = 0;
  198. m_status = eStatus::Valid;
  199. m_shape = shapearg;
  200. m_distance = 0;
  201. /* Initialize simplex */
  202. m_simplices[0].rank = 0;
  203. m_ray = guess;
  204. const btScalar sqrl = m_ray.length2();
  205. appendvertice(m_simplices[0], sqrl > 0 ? -m_ray : btVector3(1, 0, 0));
  206. m_simplices[0].p[0] = 1;
  207. m_ray = m_simplices[0].c[0]->w;
  208. sqdist = sqrl;
  209. lastw[0] =
  210. lastw[1] =
  211. lastw[2] =
  212. lastw[3] = m_ray;
  213. /* Loop */
  214. do
  215. {
  216. const U next = 1 - m_current;
  217. sSimplex& cs = m_simplices[m_current];
  218. sSimplex& ns = m_simplices[next];
  219. /* Check zero */
  220. const btScalar rl = m_ray.length();
  221. if (rl < GJK_MIN_DISTANCE)
  222. { /* Touching or inside */
  223. m_status = eStatus::Inside;
  224. break;
  225. }
  226. /* Append new vertice in -'v' direction */
  227. appendvertice(cs, -m_ray);
  228. const btVector3& w = cs.c[cs.rank - 1]->w;
  229. bool found = false;
  230. for (U i = 0; i < 4; ++i)
  231. {
  232. if ((w - lastw[i]).length2() < GJK_DUPLICATED_EPS)
  233. {
  234. found = true;
  235. break;
  236. }
  237. }
  238. if (found)
  239. { /* Return old simplex */
  240. removevertice(m_simplices[m_current]);
  241. break;
  242. }
  243. else
  244. { /* Update lastw */
  245. lastw[clastw = (clastw + 1) & 3] = w;
  246. }
  247. /* Check for termination */
  248. const btScalar omega = btDot(m_ray, w) / rl;
  249. alpha = btMax(omega, alpha);
  250. if (((rl - alpha) - (GJK_ACCURACY * rl)) <= 0)
  251. { /* Return old simplex */
  252. removevertice(m_simplices[m_current]);
  253. break;
  254. }
  255. /* Reduce simplex */
  256. btScalar weights[4];
  257. U mask = 0;
  258. switch (cs.rank)
  259. {
  260. case 2:
  261. sqdist = projectorigin(cs.c[0]->w,
  262. cs.c[1]->w,
  263. weights, mask);
  264. break;
  265. case 3:
  266. sqdist = projectorigin(cs.c[0]->w,
  267. cs.c[1]->w,
  268. cs.c[2]->w,
  269. weights, mask);
  270. break;
  271. case 4:
  272. sqdist = projectorigin(cs.c[0]->w,
  273. cs.c[1]->w,
  274. cs.c[2]->w,
  275. cs.c[3]->w,
  276. weights, mask);
  277. break;
  278. }
  279. if (sqdist >= 0)
  280. { /* Valid */
  281. ns.rank = 0;
  282. m_ray = btVector3(0, 0, 0);
  283. m_current = next;
  284. for (U i = 0, ni = cs.rank; i < ni; ++i)
  285. {
  286. if (mask & (1 << i))
  287. {
  288. ns.c[ns.rank] = cs.c[i];
  289. ns.p[ns.rank++] = weights[i];
  290. m_ray += cs.c[i]->w * weights[i];
  291. }
  292. else
  293. {
  294. m_free[m_nfree++] = cs.c[i];
  295. }
  296. }
  297. if (mask == 15) m_status = eStatus::Inside;
  298. }
  299. else
  300. { /* Return old simplex */
  301. removevertice(m_simplices[m_current]);
  302. break;
  303. }
  304. m_status = ((++iterations) < GJK_MAX_ITERATIONS) ? m_status : eStatus::Failed;
  305. } while (m_status == eStatus::Valid);
  306. m_simplex = &m_simplices[m_current];
  307. switch (m_status)
  308. {
  309. case eStatus::Valid:
  310. m_distance = m_ray.length();
  311. break;
  312. case eStatus::Inside:
  313. m_distance = 0;
  314. break;
  315. default:
  316. {
  317. }
  318. }
  319. return (m_status);
  320. }
  321. bool EncloseOrigin()
  322. {
  323. switch (m_simplex->rank)
  324. {
  325. case 1:
  326. {
  327. for (U i = 0; i < 3; ++i)
  328. {
  329. btVector3 axis = btVector3(0, 0, 0);
  330. axis[i] = 1;
  331. appendvertice(*m_simplex, axis);
  332. if (EncloseOrigin()) return (true);
  333. removevertice(*m_simplex);
  334. appendvertice(*m_simplex, -axis);
  335. if (EncloseOrigin()) return (true);
  336. removevertice(*m_simplex);
  337. }
  338. }
  339. break;
  340. case 2:
  341. {
  342. const btVector3 d = m_simplex->c[1]->w - m_simplex->c[0]->w;
  343. for (U i = 0; i < 3; ++i)
  344. {
  345. btVector3 axis = btVector3(0, 0, 0);
  346. axis[i] = 1;
  347. const btVector3 p = btCross(d, axis);
  348. if (p.length2() > 0)
  349. {
  350. appendvertice(*m_simplex, p);
  351. if (EncloseOrigin()) return (true);
  352. removevertice(*m_simplex);
  353. appendvertice(*m_simplex, -p);
  354. if (EncloseOrigin()) return (true);
  355. removevertice(*m_simplex);
  356. }
  357. }
  358. }
  359. break;
  360. case 3:
  361. {
  362. const btVector3 n = btCross(m_simplex->c[1]->w - m_simplex->c[0]->w,
  363. m_simplex->c[2]->w - m_simplex->c[0]->w);
  364. if (n.length2() > 0)
  365. {
  366. appendvertice(*m_simplex, n);
  367. if (EncloseOrigin()) return (true);
  368. removevertice(*m_simplex);
  369. appendvertice(*m_simplex, -n);
  370. if (EncloseOrigin()) return (true);
  371. removevertice(*m_simplex);
  372. }
  373. }
  374. break;
  375. case 4:
  376. {
  377. if (btFabs(det(m_simplex->c[0]->w - m_simplex->c[3]->w,
  378. m_simplex->c[1]->w - m_simplex->c[3]->w,
  379. m_simplex->c[2]->w - m_simplex->c[3]->w)) > 0)
  380. return (true);
  381. }
  382. break;
  383. }
  384. return (false);
  385. }
  386. /* Internals */
  387. void getsupport(const btVector3& d, sSV& sv) const
  388. {
  389. sv.d = d / d.length();
  390. sv.w = m_shape.Support(sv.d);
  391. }
  392. void removevertice(sSimplex& simplex)
  393. {
  394. m_free[m_nfree++] = simplex.c[--simplex.rank];
  395. }
  396. void appendvertice(sSimplex& simplex, const btVector3& v)
  397. {
  398. simplex.p[simplex.rank] = 0;
  399. simplex.c[simplex.rank] = m_free[--m_nfree];
  400. getsupport(v, *simplex.c[simplex.rank++]);
  401. }
  402. static btScalar det(const btVector3& a, const btVector3& b, const btVector3& c)
  403. {
  404. return (a.y() * b.z() * c.x() + a.z() * b.x() * c.y() -
  405. a.x() * b.z() * c.y() - a.y() * b.x() * c.z() +
  406. a.x() * b.y() * c.z() - a.z() * b.y() * c.x());
  407. }
  408. static btScalar projectorigin(const btVector3& a,
  409. const btVector3& b,
  410. btScalar* w, U& m)
  411. {
  412. const btVector3 d = b - a;
  413. const btScalar l = d.length2();
  414. if (l > GJK_SIMPLEX2_EPS)
  415. {
  416. const btScalar t(l > 0 ? -btDot(a, d) / l : 0);
  417. if (t >= 1)
  418. {
  419. w[0] = 0;
  420. w[1] = 1;
  421. m = 2;
  422. return (b.length2());
  423. }
  424. else if (t <= 0)
  425. {
  426. w[0] = 1;
  427. w[1] = 0;
  428. m = 1;
  429. return (a.length2());
  430. }
  431. else
  432. {
  433. w[0] = 1 - (w[1] = t);
  434. m = 3;
  435. return ((a + d * t).length2());
  436. }
  437. }
  438. return (-1);
  439. }
  440. static btScalar projectorigin(const btVector3& a,
  441. const btVector3& b,
  442. const btVector3& c,
  443. btScalar* w, U& m)
  444. {
  445. static const U imd3[] = {1, 2, 0};
  446. const btVector3* vt[] = {&a, &b, &c};
  447. const btVector3 dl[] = {a - b, b - c, c - a};
  448. const btVector3 n = btCross(dl[0], dl[1]);
  449. const btScalar l = n.length2();
  450. if (l > GJK_SIMPLEX3_EPS)
  451. {
  452. btScalar mindist = -1;
  453. btScalar subw[2] = {0.f, 0.f};
  454. U subm(0);
  455. for (U i = 0; i < 3; ++i)
  456. {
  457. if (btDot(*vt[i], btCross(dl[i], n)) > 0)
  458. {
  459. const U j = imd3[i];
  460. const btScalar subd(projectorigin(*vt[i], *vt[j], subw, subm));
  461. if ((mindist < 0) || (subd < mindist))
  462. {
  463. mindist = subd;
  464. m = static_cast<U>(((subm & 1) ? 1 << i : 0) + ((subm & 2) ? 1 << j : 0));
  465. w[i] = subw[0];
  466. w[j] = subw[1];
  467. w[imd3[j]] = 0;
  468. }
  469. }
  470. }
  471. if (mindist < 0)
  472. {
  473. const btScalar d = btDot(a, n);
  474. const btScalar s = btSqrt(l);
  475. const btVector3 p = n * (d / l);
  476. mindist = p.length2();
  477. m = 7;
  478. w[0] = (btCross(dl[1], b - p)).length() / s;
  479. w[1] = (btCross(dl[2], c - p)).length() / s;
  480. w[2] = 1 - (w[0] + w[1]);
  481. }
  482. return (mindist);
  483. }
  484. return (-1);
  485. }
  486. static btScalar projectorigin(const btVector3& a,
  487. const btVector3& b,
  488. const btVector3& c,
  489. const btVector3& d,
  490. btScalar* w, U& m)
  491. {
  492. static const U imd3[] = {1, 2, 0};
  493. const btVector3* vt[] = {&a, &b, &c, &d};
  494. const btVector3 dl[] = {a - d, b - d, c - d};
  495. const btScalar vl = det(dl[0], dl[1], dl[2]);
  496. const bool ng = (vl * btDot(a, btCross(b - c, a - b))) <= 0;
  497. if (ng && (btFabs(vl) > GJK_SIMPLEX4_EPS))
  498. {
  499. btScalar mindist = -1;
  500. btScalar subw[3] = {0.f, 0.f, 0.f};
  501. U subm(0);
  502. for (U i = 0; i < 3; ++i)
  503. {
  504. const U j = imd3[i];
  505. const btScalar s = vl * btDot(d, btCross(dl[i], dl[j]));
  506. if (s > 0)
  507. {
  508. const btScalar subd = projectorigin(*vt[i], *vt[j], d, subw, subm);
  509. if ((mindist < 0) || (subd < mindist))
  510. {
  511. mindist = subd;
  512. m = static_cast<U>((subm & 1 ? 1 << i : 0) +
  513. (subm & 2 ? 1 << j : 0) +
  514. (subm & 4 ? 8 : 0));
  515. w[i] = subw[0];
  516. w[j] = subw[1];
  517. w[imd3[j]] = 0;
  518. w[3] = subw[2];
  519. }
  520. }
  521. }
  522. if (mindist < 0)
  523. {
  524. mindist = 0;
  525. m = 15;
  526. w[0] = det(c, b, d) / vl;
  527. w[1] = det(a, c, d) / vl;
  528. w[2] = det(b, a, d) / vl;
  529. w[3] = 1 - (w[0] + w[1] + w[2]);
  530. }
  531. return (mindist);
  532. }
  533. return (-1);
  534. }
  535. };
  536. // EPA
  537. struct EPA
  538. {
  539. /* Types */
  540. typedef GJK::sSV sSV;
  541. struct sFace
  542. {
  543. btVector3 n;
  544. btScalar d;
  545. sSV* c[3];
  546. sFace* f[3];
  547. sFace* l[2];
  548. U1 e[3];
  549. U1 pass;
  550. };
  551. struct sList
  552. {
  553. sFace* root;
  554. U count;
  555. sList() : root(0), count(0) {}
  556. };
  557. struct sHorizon
  558. {
  559. sFace* cf;
  560. sFace* ff;
  561. U nf;
  562. sHorizon() : cf(0), ff(0), nf(0) {}
  563. };
  564. struct eStatus
  565. {
  566. enum _
  567. {
  568. Valid,
  569. Touching,
  570. Degenerated,
  571. NonConvex,
  572. InvalidHull,
  573. OutOfFaces,
  574. OutOfVertices,
  575. AccuraryReached,
  576. FallBack,
  577. Failed
  578. };
  579. };
  580. /* Fields */
  581. eStatus::_ m_status;
  582. GJK::sSimplex m_result;
  583. btVector3 m_normal;
  584. btScalar m_depth;
  585. sSV m_sv_store[EPA_MAX_VERTICES];
  586. sFace m_fc_store[EPA_MAX_FACES];
  587. U m_nextsv;
  588. sList m_hull;
  589. sList m_stock;
  590. /* Methods */
  591. EPA()
  592. {
  593. Initialize();
  594. }
  595. static inline void bind(sFace* fa, U ea, sFace* fb, U eb)
  596. {
  597. fa->e[ea] = (U1)eb;
  598. fa->f[ea] = fb;
  599. fb->e[eb] = (U1)ea;
  600. fb->f[eb] = fa;
  601. }
  602. static inline void append(sList& list, sFace* face)
  603. {
  604. face->l[0] = 0;
  605. face->l[1] = list.root;
  606. if (list.root) list.root->l[0] = face;
  607. list.root = face;
  608. ++list.count;
  609. }
  610. static inline void remove(sList& list, sFace* face)
  611. {
  612. if (face->l[1]) face->l[1]->l[0] = face->l[0];
  613. if (face->l[0]) face->l[0]->l[1] = face->l[1];
  614. if (face == list.root) list.root = face->l[1];
  615. --list.count;
  616. }
  617. void Initialize()
  618. {
  619. m_status = eStatus::Failed;
  620. m_normal = btVector3(0, 0, 0);
  621. m_depth = 0;
  622. m_nextsv = 0;
  623. for (U i = 0; i < EPA_MAX_FACES; ++i)
  624. {
  625. append(m_stock, &m_fc_store[EPA_MAX_FACES - i - 1]);
  626. }
  627. }
  628. eStatus::_ Evaluate(GJK& gjk, const btVector3& guess)
  629. {
  630. GJK::sSimplex& simplex = *gjk.m_simplex;
  631. if ((simplex.rank > 1) && gjk.EncloseOrigin())
  632. {
  633. /* Clean up */
  634. while (m_hull.root)
  635. {
  636. sFace* f = m_hull.root;
  637. remove(m_hull, f);
  638. append(m_stock, f);
  639. }
  640. m_status = eStatus::Valid;
  641. m_nextsv = 0;
  642. /* Orient simplex */
  643. if (gjk.det(simplex.c[0]->w - simplex.c[3]->w,
  644. simplex.c[1]->w - simplex.c[3]->w,
  645. simplex.c[2]->w - simplex.c[3]->w) < 0)
  646. {
  647. btSwap(simplex.c[0], simplex.c[1]);
  648. btSwap(simplex.p[0], simplex.p[1]);
  649. }
  650. /* Build initial hull */
  651. sFace* tetra[] = {newface(simplex.c[0], simplex.c[1], simplex.c[2], true),
  652. newface(simplex.c[1], simplex.c[0], simplex.c[3], true),
  653. newface(simplex.c[2], simplex.c[1], simplex.c[3], true),
  654. newface(simplex.c[0], simplex.c[2], simplex.c[3], true)};
  655. if (m_hull.count == 4)
  656. {
  657. sFace* best = findbest();
  658. sFace outer = *best;
  659. U pass = 0;
  660. U iterations = 0;
  661. bind(tetra[0], 0, tetra[1], 0);
  662. bind(tetra[0], 1, tetra[2], 0);
  663. bind(tetra[0], 2, tetra[3], 0);
  664. bind(tetra[1], 1, tetra[3], 2);
  665. bind(tetra[1], 2, tetra[2], 1);
  666. bind(tetra[2], 2, tetra[3], 1);
  667. m_status = eStatus::Valid;
  668. for (; iterations < EPA_MAX_ITERATIONS; ++iterations)
  669. {
  670. if (m_nextsv < EPA_MAX_VERTICES)
  671. {
  672. sHorizon horizon;
  673. sSV* w = &m_sv_store[m_nextsv++];
  674. bool valid = true;
  675. best->pass = (U1)(++pass);
  676. gjk.getsupport(best->n, *w);
  677. const btScalar wdist = btDot(best->n, w->w) - best->d;
  678. if (wdist > EPA_ACCURACY)
  679. {
  680. for (U j = 0; (j < 3) && valid; ++j)
  681. {
  682. valid &= expand(pass, w,
  683. best->f[j], best->e[j],
  684. horizon);
  685. }
  686. if (valid && (horizon.nf >= 3))
  687. {
  688. bind(horizon.cf, 1, horizon.ff, 2);
  689. remove(m_hull, best);
  690. append(m_stock, best);
  691. best = findbest();
  692. outer = *best;
  693. }
  694. else
  695. {
  696. m_status = eStatus::InvalidHull;
  697. break;
  698. }
  699. }
  700. else
  701. {
  702. m_status = eStatus::AccuraryReached;
  703. break;
  704. }
  705. }
  706. else
  707. {
  708. m_status = eStatus::OutOfVertices;
  709. break;
  710. }
  711. }
  712. const btVector3 projection = outer.n * outer.d;
  713. m_normal = outer.n;
  714. m_depth = outer.d;
  715. m_result.rank = 3;
  716. m_result.c[0] = outer.c[0];
  717. m_result.c[1] = outer.c[1];
  718. m_result.c[2] = outer.c[2];
  719. m_result.p[0] = btCross(outer.c[1]->w - projection,
  720. outer.c[2]->w - projection)
  721. .length();
  722. m_result.p[1] = btCross(outer.c[2]->w - projection,
  723. outer.c[0]->w - projection)
  724. .length();
  725. m_result.p[2] = btCross(outer.c[0]->w - projection,
  726. outer.c[1]->w - projection)
  727. .length();
  728. const btScalar sum = m_result.p[0] + m_result.p[1] + m_result.p[2];
  729. m_result.p[0] /= sum;
  730. m_result.p[1] /= sum;
  731. m_result.p[2] /= sum;
  732. return (m_status);
  733. }
  734. }
  735. /* Fallback */
  736. m_status = eStatus::FallBack;
  737. m_normal = -guess;
  738. const btScalar nl = m_normal.length();
  739. if (nl > 0)
  740. m_normal = m_normal / nl;
  741. else
  742. m_normal = btVector3(1, 0, 0);
  743. m_depth = 0;
  744. m_result.rank = 1;
  745. m_result.c[0] = simplex.c[0];
  746. m_result.p[0] = 1;
  747. return (m_status);
  748. }
  749. bool getedgedist(sFace* face, sSV* a, sSV* b, btScalar& dist)
  750. {
  751. const btVector3 ba = b->w - a->w;
  752. const btVector3 n_ab = btCross(ba, face->n); // Outward facing edge normal direction, on triangle plane
  753. const btScalar a_dot_nab = btDot(a->w, n_ab); // Only care about the sign to determine inside/outside, so not normalization required
  754. if (a_dot_nab < 0)
  755. {
  756. // Outside of edge a->b
  757. const btScalar ba_l2 = ba.length2();
  758. const btScalar a_dot_ba = btDot(a->w, ba);
  759. const btScalar b_dot_ba = btDot(b->w, ba);
  760. if (a_dot_ba > 0)
  761. {
  762. // Pick distance vertex a
  763. dist = a->w.length();
  764. }
  765. else if (b_dot_ba < 0)
  766. {
  767. // Pick distance vertex b
  768. dist = b->w.length();
  769. }
  770. else
  771. {
  772. // Pick distance to edge a->b
  773. const btScalar a_dot_b = btDot(a->w, b->w);
  774. dist = btSqrt(btMax((a->w.length2() * b->w.length2() - a_dot_b * a_dot_b) / ba_l2, (btScalar)0));
  775. }
  776. return true;
  777. }
  778. return false;
  779. }
  780. sFace* newface(sSV* a, sSV* b, sSV* c, bool forced)
  781. {
  782. if (m_stock.root)
  783. {
  784. sFace* face = m_stock.root;
  785. remove(m_stock, face);
  786. append(m_hull, face);
  787. face->pass = 0;
  788. face->c[0] = a;
  789. face->c[1] = b;
  790. face->c[2] = c;
  791. face->n = btCross(b->w - a->w, c->w - a->w);
  792. const btScalar l = face->n.length();
  793. const bool v = l > EPA_ACCURACY;
  794. if (v)
  795. {
  796. if (!(getedgedist(face, a, b, face->d) ||
  797. getedgedist(face, b, c, face->d) ||
  798. getedgedist(face, c, a, face->d)))
  799. {
  800. // Origin projects to the interior of the triangle
  801. // Use distance to triangle plane
  802. face->d = btDot(a->w, face->n) / l;
  803. }
  804. face->n /= l;
  805. if (forced || (face->d >= -EPA_PLANE_EPS))
  806. {
  807. return face;
  808. }
  809. else
  810. m_status = eStatus::NonConvex;
  811. }
  812. else
  813. m_status = eStatus::Degenerated;
  814. remove(m_hull, face);
  815. append(m_stock, face);
  816. return 0;
  817. }
  818. m_status = m_stock.root ? eStatus::OutOfVertices : eStatus::OutOfFaces;
  819. return 0;
  820. }
  821. sFace* findbest()
  822. {
  823. sFace* minf = m_hull.root;
  824. btScalar mind = minf->d * minf->d;
  825. for (sFace* f = minf->l[1]; f; f = f->l[1])
  826. {
  827. const btScalar sqd = f->d * f->d;
  828. if (sqd < mind)
  829. {
  830. minf = f;
  831. mind = sqd;
  832. }
  833. }
  834. return (minf);
  835. }
  836. bool expand(U pass, sSV* w, sFace* f, U e, sHorizon& horizon)
  837. {
  838. static const U i1m3[] = {1, 2, 0};
  839. static const U i2m3[] = {2, 0, 1};
  840. if (f->pass != pass)
  841. {
  842. const U e1 = i1m3[e];
  843. if ((btDot(f->n, w->w) - f->d) < -EPA_PLANE_EPS)
  844. {
  845. sFace* nf = newface(f->c[e1], f->c[e], w, false);
  846. if (nf)
  847. {
  848. bind(nf, 0, f, e);
  849. if (horizon.cf)
  850. bind(horizon.cf, 1, nf, 2);
  851. else
  852. horizon.ff = nf;
  853. horizon.cf = nf;
  854. ++horizon.nf;
  855. return (true);
  856. }
  857. }
  858. else
  859. {
  860. const U e2 = i2m3[e];
  861. f->pass = (U1)pass;
  862. if (expand(pass, w, f->f[e1], f->e[e1], horizon) &&
  863. expand(pass, w, f->f[e2], f->e[e2], horizon))
  864. {
  865. remove(m_hull, f);
  866. append(m_stock, f);
  867. return (true);
  868. }
  869. }
  870. }
  871. return (false);
  872. }
  873. };
  874. //
  875. static void Initialize(const btConvexShape* shape0, const btTransform& wtrs0,
  876. const btConvexShape* shape1, const btTransform& wtrs1,
  877. btGjkEpaSolver2::sResults& results,
  878. tShape& shape,
  879. bool withmargins)
  880. {
  881. /* Results */
  882. results.witnesses[0] =
  883. results.witnesses[1] = btVector3(0, 0, 0);
  884. results.status = btGjkEpaSolver2::sResults::Separated;
  885. /* Shape */
  886. shape.m_shapes[0] = shape0;
  887. shape.m_shapes[1] = shape1;
  888. shape.m_toshape1 = wtrs1.getBasis().transposeTimes(wtrs0.getBasis());
  889. shape.m_toshape0 = wtrs0.inverseTimes(wtrs1);
  890. shape.EnableMargin(withmargins);
  891. }
  892. } // namespace gjkepa2_impl
  893. //
  894. // Api
  895. //
  896. using namespace gjkepa2_impl;
  897. //
  898. int btGjkEpaSolver2::StackSizeRequirement()
  899. {
  900. return (sizeof(GJK) + sizeof(EPA));
  901. }
  902. //
  903. bool btGjkEpaSolver2::Distance(const btConvexShape* shape0,
  904. const btTransform& wtrs0,
  905. const btConvexShape* shape1,
  906. const btTransform& wtrs1,
  907. const btVector3& guess,
  908. sResults& results)
  909. {
  910. tShape shape;
  911. Initialize(shape0, wtrs0, shape1, wtrs1, results, shape, false);
  912. GJK gjk;
  913. GJK::eStatus::_ gjk_status = gjk.Evaluate(shape, guess);
  914. if (gjk_status == GJK::eStatus::Valid)
  915. {
  916. btVector3 w0 = btVector3(0, 0, 0);
  917. btVector3 w1 = btVector3(0, 0, 0);
  918. for (U i = 0; i < gjk.m_simplex->rank; ++i)
  919. {
  920. const btScalar p = gjk.m_simplex->p[i];
  921. w0 += shape.Support(gjk.m_simplex->c[i]->d, 0) * p;
  922. w1 += shape.Support(-gjk.m_simplex->c[i]->d, 1) * p;
  923. }
  924. results.witnesses[0] = wtrs0 * w0;
  925. results.witnesses[1] = wtrs0 * w1;
  926. results.normal = w0 - w1;
  927. results.distance = results.normal.length();
  928. results.normal /= results.distance > GJK_MIN_DISTANCE ? results.distance : 1;
  929. return (true);
  930. }
  931. else
  932. {
  933. results.status = gjk_status == GJK::eStatus::Inside ? sResults::Penetrating : sResults::GJK_Failed;
  934. return (false);
  935. }
  936. }
  937. //
  938. bool btGjkEpaSolver2::Penetration(const btConvexShape* shape0,
  939. const btTransform& wtrs0,
  940. const btConvexShape* shape1,
  941. const btTransform& wtrs1,
  942. const btVector3& guess,
  943. sResults& results,
  944. bool usemargins)
  945. {
  946. tShape shape;
  947. Initialize(shape0, wtrs0, shape1, wtrs1, results, shape, usemargins);
  948. GJK gjk;
  949. GJK::eStatus::_ gjk_status = gjk.Evaluate(shape, -guess);
  950. switch (gjk_status)
  951. {
  952. case GJK::eStatus::Inside:
  953. {
  954. EPA epa;
  955. EPA::eStatus::_ epa_status = epa.Evaluate(gjk, -guess);
  956. if (epa_status != EPA::eStatus::Failed)
  957. {
  958. btVector3 w0 = btVector3(0, 0, 0);
  959. for (U i = 0; i < epa.m_result.rank; ++i)
  960. {
  961. w0 += shape.Support(epa.m_result.c[i]->d, 0) * epa.m_result.p[i];
  962. }
  963. results.status = sResults::Penetrating;
  964. results.witnesses[0] = wtrs0 * w0;
  965. results.witnesses[1] = wtrs0 * (w0 - epa.m_normal * epa.m_depth);
  966. results.normal = -epa.m_normal;
  967. results.distance = -epa.m_depth;
  968. return (true);
  969. }
  970. else
  971. results.status = sResults::EPA_Failed;
  972. }
  973. break;
  974. case GJK::eStatus::Failed:
  975. results.status = sResults::GJK_Failed;
  976. break;
  977. default:
  978. {
  979. }
  980. }
  981. return (false);
  982. }
  983. #ifndef __SPU__
  984. //
  985. btScalar btGjkEpaSolver2::SignedDistance(const btVector3& position,
  986. btScalar margin,
  987. const btConvexShape* shape0,
  988. const btTransform& wtrs0,
  989. sResults& results)
  990. {
  991. tShape shape;
  992. btSphereShape shape1(margin);
  993. btTransform wtrs1(btQuaternion(0, 0, 0, 1), position);
  994. Initialize(shape0, wtrs0, &shape1, wtrs1, results, shape, false);
  995. GJK gjk;
  996. GJK::eStatus::_ gjk_status = gjk.Evaluate(shape, btVector3(1, 1, 1));
  997. if (gjk_status == GJK::eStatus::Valid)
  998. {
  999. btVector3 w0 = btVector3(0, 0, 0);
  1000. btVector3 w1 = btVector3(0, 0, 0);
  1001. for (U i = 0; i < gjk.m_simplex->rank; ++i)
  1002. {
  1003. const btScalar p = gjk.m_simplex->p[i];
  1004. w0 += shape.Support(gjk.m_simplex->c[i]->d, 0) * p;
  1005. w1 += shape.Support(-gjk.m_simplex->c[i]->d, 1) * p;
  1006. }
  1007. results.witnesses[0] = wtrs0 * w0;
  1008. results.witnesses[1] = wtrs0 * w1;
  1009. const btVector3 delta = results.witnesses[1] -
  1010. results.witnesses[0];
  1011. const btScalar margin = shape0->getMarginNonVirtual() +
  1012. shape1.getMarginNonVirtual();
  1013. const btScalar length = delta.length();
  1014. results.normal = delta / length;
  1015. results.witnesses[0] += results.normal * margin;
  1016. results.distance = length - margin;
  1017. return results.distance;
  1018. }
  1019. else
  1020. {
  1021. if (gjk_status == GJK::eStatus::Inside)
  1022. {
  1023. if (Penetration(shape0, wtrs0, &shape1, wtrs1, gjk.m_ray, results))
  1024. {
  1025. const btVector3 delta = results.witnesses[0] -
  1026. results.witnesses[1];
  1027. const btScalar length = delta.length();
  1028. if (length >= SIMD_EPSILON)
  1029. results.normal = delta / length;
  1030. return (-length);
  1031. }
  1032. }
  1033. }
  1034. return (SIMD_INFINITY);
  1035. }
  1036. //
  1037. bool btGjkEpaSolver2::SignedDistance(const btConvexShape* shape0,
  1038. const btTransform& wtrs0,
  1039. const btConvexShape* shape1,
  1040. const btTransform& wtrs1,
  1041. const btVector3& guess,
  1042. sResults& results)
  1043. {
  1044. if (!Distance(shape0, wtrs0, shape1, wtrs1, guess, results))
  1045. return (Penetration(shape0, wtrs0, shape1, wtrs1, guess, results, false));
  1046. else
  1047. return (true);
  1048. }
  1049. #endif //__SPU__
  1050. /* Symbols cleanup */
  1051. #undef GJK_MAX_ITERATIONS
  1052. #undef GJK_ACCURACY
  1053. #undef GJK_MIN_DISTANCE
  1054. #undef GJK_DUPLICATED_EPS
  1055. #undef GJK_SIMPLEX2_EPS
  1056. #undef GJK_SIMPLEX3_EPS
  1057. #undef GJK_SIMPLEX4_EPS
  1058. #undef EPA_MAX_VERTICES
  1059. #undef EPA_MAX_FACES
  1060. #undef EPA_MAX_ITERATIONS
  1061. #undef EPA_ACCURACY
  1062. #undef EPA_FALLBACK
  1063. #undef EPA_PLANE_EPS
  1064. #undef EPA_INSIDE_EPS