btGjkEpa2.cpp 25 KB

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