btPolyhedralContactClipping.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2011 Advanced Micro Devices, Inc. http://bulletphysics.org
  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 use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. ///This file was written by Erwin Coumans
  14. ///Separating axis rest based on work from Pierre Terdiman, see
  15. ///And contact clipping based on work from Simon Hobbs
  16. #include "btPolyhedralContactClipping.h"
  17. #include "BulletCollision/CollisionShapes/btConvexPolyhedron.h"
  18. #include <float.h> //for FLT_MAX
  19. int gExpectedNbTests = 0;
  20. int gActualNbTests = 0;
  21. bool gUseInternalObject = true;
  22. // Clips a face to the back of a plane
  23. void btPolyhedralContactClipping::clipFace(const btVertexArray& pVtxIn, btVertexArray& ppVtxOut, const btVector3& planeNormalWS, btScalar planeEqWS)
  24. {
  25. int ve;
  26. btScalar ds, de;
  27. int numVerts = pVtxIn.size();
  28. if (numVerts < 2)
  29. return;
  30. btVector3 firstVertex = pVtxIn[pVtxIn.size() - 1];
  31. btVector3 endVertex = pVtxIn[0];
  32. ds = planeNormalWS.dot(firstVertex) + planeEqWS;
  33. for (ve = 0; ve < numVerts; ve++)
  34. {
  35. endVertex = pVtxIn[ve];
  36. de = planeNormalWS.dot(endVertex) + planeEqWS;
  37. if (ds < 0)
  38. {
  39. if (de < 0)
  40. {
  41. // Start < 0, end < 0, so output endVertex
  42. ppVtxOut.push_back(endVertex);
  43. }
  44. else
  45. {
  46. // Start < 0, end >= 0, so output intersection
  47. ppVtxOut.push_back(firstVertex.lerp(endVertex, btScalar(ds * 1.f / (ds - de))));
  48. }
  49. }
  50. else
  51. {
  52. if (de < 0)
  53. {
  54. // Start >= 0, end < 0 so output intersection and end
  55. ppVtxOut.push_back(firstVertex.lerp(endVertex, btScalar(ds * 1.f / (ds - de))));
  56. ppVtxOut.push_back(endVertex);
  57. }
  58. }
  59. firstVertex = endVertex;
  60. ds = de;
  61. }
  62. }
  63. static bool TestSepAxis(const btConvexPolyhedron& hullA, const btConvexPolyhedron& hullB, const btTransform& transA, const btTransform& transB, const btVector3& sep_axis, btScalar& depth, btVector3& witnessPointA, btVector3& witnessPointB)
  64. {
  65. btScalar Min0, Max0;
  66. btScalar Min1, Max1;
  67. btVector3 witnesPtMinA, witnesPtMaxA;
  68. btVector3 witnesPtMinB, witnesPtMaxB;
  69. hullA.project(transA, sep_axis, Min0, Max0, witnesPtMinA, witnesPtMaxA);
  70. hullB.project(transB, sep_axis, Min1, Max1, witnesPtMinB, witnesPtMaxB);
  71. if (Max0 < Min1 || Max1 < Min0)
  72. return false;
  73. btScalar d0 = Max0 - Min1;
  74. btAssert(d0 >= 0.0f);
  75. btScalar d1 = Max1 - Min0;
  76. btAssert(d1 >= 0.0f);
  77. if (d0 < d1)
  78. {
  79. depth = d0;
  80. witnessPointA = witnesPtMaxA;
  81. witnessPointB = witnesPtMinB;
  82. }
  83. else
  84. {
  85. depth = d1;
  86. witnessPointA = witnesPtMinA;
  87. witnessPointB = witnesPtMaxB;
  88. }
  89. return true;
  90. }
  91. static int gActualSATPairTests = 0;
  92. inline bool IsAlmostZero(const btVector3& v)
  93. {
  94. if (btFabs(v.x()) > 1e-6 || btFabs(v.y()) > 1e-6 || btFabs(v.z()) > 1e-6) return false;
  95. return true;
  96. }
  97. #ifdef TEST_INTERNAL_OBJECTS
  98. inline void BoxSupport(const btScalar extents[3], const btScalar sv[3], btScalar p[3])
  99. {
  100. // This version is ~11.000 cycles (4%) faster overall in one of the tests.
  101. // IR(p[0]) = IR(extents[0])|(IR(sv[0])&SIGN_BITMASK);
  102. // IR(p[1]) = IR(extents[1])|(IR(sv[1])&SIGN_BITMASK);
  103. // IR(p[2]) = IR(extents[2])|(IR(sv[2])&SIGN_BITMASK);
  104. p[0] = sv[0] < 0.0f ? -extents[0] : extents[0];
  105. p[1] = sv[1] < 0.0f ? -extents[1] : extents[1];
  106. p[2] = sv[2] < 0.0f ? -extents[2] : extents[2];
  107. }
  108. void InverseTransformPoint3x3(btVector3& out, const btVector3& in, const btTransform& tr)
  109. {
  110. const btMatrix3x3& rot = tr.getBasis();
  111. const btVector3& r0 = rot[0];
  112. const btVector3& r1 = rot[1];
  113. const btVector3& r2 = rot[2];
  114. const btScalar x = r0.x() * in.x() + r1.x() * in.y() + r2.x() * in.z();
  115. const btScalar y = r0.y() * in.x() + r1.y() * in.y() + r2.y() * in.z();
  116. const btScalar z = r0.z() * in.x() + r1.z() * in.y() + r2.z() * in.z();
  117. out.setValue(x, y, z);
  118. }
  119. bool TestInternalObjects(const btTransform& trans0, const btTransform& trans1, const btVector3& delta_c, const btVector3& axis, const btConvexPolyhedron& convex0, const btConvexPolyhedron& convex1, btScalar dmin)
  120. {
  121. const btScalar dp = delta_c.dot(axis);
  122. btVector3 localAxis0;
  123. InverseTransformPoint3x3(localAxis0, axis, trans0);
  124. btVector3 localAxis1;
  125. InverseTransformPoint3x3(localAxis1, axis, trans1);
  126. btScalar p0[3];
  127. BoxSupport(convex0.m_extents, localAxis0, p0);
  128. btScalar p1[3];
  129. BoxSupport(convex1.m_extents, localAxis1, p1);
  130. const btScalar Radius0 = p0[0] * localAxis0.x() + p0[1] * localAxis0.y() + p0[2] * localAxis0.z();
  131. const btScalar Radius1 = p1[0] * localAxis1.x() + p1[1] * localAxis1.y() + p1[2] * localAxis1.z();
  132. const btScalar MinRadius = Radius0 > convex0.m_radius ? Radius0 : convex0.m_radius;
  133. const btScalar MaxRadius = Radius1 > convex1.m_radius ? Radius1 : convex1.m_radius;
  134. const btScalar MinMaxRadius = MaxRadius + MinRadius;
  135. const btScalar d0 = MinMaxRadius + dp;
  136. const btScalar d1 = MinMaxRadius - dp;
  137. const btScalar depth = d0 < d1 ? d0 : d1;
  138. if (depth > dmin)
  139. return false;
  140. return true;
  141. }
  142. #endif //TEST_INTERNAL_OBJECTS
  143. SIMD_FORCE_INLINE void btSegmentsClosestPoints(
  144. btVector3& ptsVector,
  145. btVector3& offsetA,
  146. btVector3& offsetB,
  147. btScalar& tA, btScalar& tB,
  148. const btVector3& translation,
  149. const btVector3& dirA, btScalar hlenA,
  150. const btVector3& dirB, btScalar hlenB)
  151. {
  152. // compute the parameters of the closest points on each line segment
  153. btScalar dirA_dot_dirB = btDot(dirA, dirB);
  154. btScalar dirA_dot_trans = btDot(dirA, translation);
  155. btScalar dirB_dot_trans = btDot(dirB, translation);
  156. btScalar denom = 1.0f - dirA_dot_dirB * dirA_dot_dirB;
  157. if (denom == 0.0f)
  158. {
  159. tA = 0.0f;
  160. }
  161. else
  162. {
  163. tA = (dirA_dot_trans - dirB_dot_trans * dirA_dot_dirB) / denom;
  164. if (tA < -hlenA)
  165. tA = -hlenA;
  166. else if (tA > hlenA)
  167. tA = hlenA;
  168. }
  169. tB = tA * dirA_dot_dirB - dirB_dot_trans;
  170. if (tB < -hlenB)
  171. {
  172. tB = -hlenB;
  173. tA = tB * dirA_dot_dirB + dirA_dot_trans;
  174. if (tA < -hlenA)
  175. tA = -hlenA;
  176. else if (tA > hlenA)
  177. tA = hlenA;
  178. }
  179. else if (tB > hlenB)
  180. {
  181. tB = hlenB;
  182. tA = tB * dirA_dot_dirB + dirA_dot_trans;
  183. if (tA < -hlenA)
  184. tA = -hlenA;
  185. else if (tA > hlenA)
  186. tA = hlenA;
  187. }
  188. // compute the closest points relative to segment centers.
  189. offsetA = dirA * tA;
  190. offsetB = dirB * tB;
  191. ptsVector = translation - offsetA + offsetB;
  192. }
  193. bool btPolyhedralContactClipping::findSeparatingAxis(const btConvexPolyhedron& hullA, const btConvexPolyhedron& hullB, const btTransform& transA, const btTransform& transB, btVector3& sep, btDiscreteCollisionDetectorInterface::Result& resultOut)
  194. {
  195. gActualSATPairTests++;
  196. //#ifdef TEST_INTERNAL_OBJECTS
  197. const btVector3 c0 = transA * hullA.m_localCenter;
  198. const btVector3 c1 = transB * hullB.m_localCenter;
  199. const btVector3 DeltaC2 = c0 - c1;
  200. //#endif
  201. btScalar dmin = FLT_MAX;
  202. int curPlaneTests = 0;
  203. int numFacesA = hullA.m_faces.size();
  204. // Test normals from hullA
  205. for (int i = 0; i < numFacesA; i++)
  206. {
  207. const btVector3 Normal(hullA.m_faces[i].m_plane[0], hullA.m_faces[i].m_plane[1], hullA.m_faces[i].m_plane[2]);
  208. btVector3 faceANormalWS = transA.getBasis() * Normal;
  209. if (DeltaC2.dot(faceANormalWS) < 0)
  210. faceANormalWS *= -1.f;
  211. curPlaneTests++;
  212. #ifdef TEST_INTERNAL_OBJECTS
  213. gExpectedNbTests++;
  214. if (gUseInternalObject && !TestInternalObjects(transA, transB, DeltaC2, faceANormalWS, hullA, hullB, dmin))
  215. continue;
  216. gActualNbTests++;
  217. #endif
  218. btScalar d;
  219. btVector3 wA, wB;
  220. if (!TestSepAxis(hullA, hullB, transA, transB, faceANormalWS, d, wA, wB))
  221. return false;
  222. if (d < dmin)
  223. {
  224. dmin = d;
  225. sep = faceANormalWS;
  226. }
  227. }
  228. int numFacesB = hullB.m_faces.size();
  229. // Test normals from hullB
  230. for (int i = 0; i < numFacesB; i++)
  231. {
  232. const btVector3 Normal(hullB.m_faces[i].m_plane[0], hullB.m_faces[i].m_plane[1], hullB.m_faces[i].m_plane[2]);
  233. btVector3 WorldNormal = transB.getBasis() * Normal;
  234. if (DeltaC2.dot(WorldNormal) < 0)
  235. WorldNormal *= -1.f;
  236. curPlaneTests++;
  237. #ifdef TEST_INTERNAL_OBJECTS
  238. gExpectedNbTests++;
  239. if (gUseInternalObject && !TestInternalObjects(transA, transB, DeltaC2, WorldNormal, hullA, hullB, dmin))
  240. continue;
  241. gActualNbTests++;
  242. #endif
  243. btScalar d;
  244. btVector3 wA, wB;
  245. if (!TestSepAxis(hullA, hullB, transA, transB, WorldNormal, d, wA, wB))
  246. return false;
  247. if (d < dmin)
  248. {
  249. dmin = d;
  250. sep = WorldNormal;
  251. }
  252. }
  253. btVector3 edgeAstart, edgeAend, edgeBstart, edgeBend;
  254. int edgeA = -1;
  255. int edgeB = -1;
  256. btVector3 worldEdgeA;
  257. btVector3 worldEdgeB;
  258. btVector3 witnessPointA(0, 0, 0), witnessPointB(0, 0, 0);
  259. int curEdgeEdge = 0;
  260. // Test edges
  261. for (int e0 = 0; e0 < hullA.m_uniqueEdges.size(); e0++)
  262. {
  263. const btVector3 edge0 = hullA.m_uniqueEdges[e0];
  264. const btVector3 WorldEdge0 = transA.getBasis() * edge0;
  265. for (int e1 = 0; e1 < hullB.m_uniqueEdges.size(); e1++)
  266. {
  267. const btVector3 edge1 = hullB.m_uniqueEdges[e1];
  268. const btVector3 WorldEdge1 = transB.getBasis() * edge1;
  269. btVector3 Cross = WorldEdge0.cross(WorldEdge1);
  270. curEdgeEdge++;
  271. if (!IsAlmostZero(Cross))
  272. {
  273. Cross = Cross.normalize();
  274. if (DeltaC2.dot(Cross) < 0)
  275. Cross *= -1.f;
  276. #ifdef TEST_INTERNAL_OBJECTS
  277. gExpectedNbTests++;
  278. if (gUseInternalObject && !TestInternalObjects(transA, transB, DeltaC2, Cross, hullA, hullB, dmin))
  279. continue;
  280. gActualNbTests++;
  281. #endif
  282. btScalar dist;
  283. btVector3 wA, wB;
  284. if (!TestSepAxis(hullA, hullB, transA, transB, Cross, dist, wA, wB))
  285. return false;
  286. if (dist < dmin)
  287. {
  288. dmin = dist;
  289. sep = Cross;
  290. edgeA = e0;
  291. edgeB = e1;
  292. worldEdgeA = WorldEdge0;
  293. worldEdgeB = WorldEdge1;
  294. witnessPointA = wA;
  295. witnessPointB = wB;
  296. }
  297. }
  298. }
  299. }
  300. if (edgeA >= 0 && edgeB >= 0)
  301. {
  302. // printf("edge-edge\n");
  303. //add an edge-edge contact
  304. btVector3 ptsVector;
  305. btVector3 offsetA;
  306. btVector3 offsetB;
  307. btScalar tA;
  308. btScalar tB;
  309. btVector3 translation = witnessPointB - witnessPointA;
  310. btVector3 dirA = worldEdgeA;
  311. btVector3 dirB = worldEdgeB;
  312. btScalar hlenB = 1e30f;
  313. btScalar hlenA = 1e30f;
  314. btSegmentsClosestPoints(ptsVector, offsetA, offsetB, tA, tB,
  315. translation,
  316. dirA, hlenA,
  317. dirB, hlenB);
  318. btScalar nlSqrt = ptsVector.length2();
  319. if (nlSqrt > SIMD_EPSILON)
  320. {
  321. btScalar nl = btSqrt(nlSqrt);
  322. ptsVector *= 1.f / nl;
  323. if (ptsVector.dot(DeltaC2) < 0.f)
  324. {
  325. ptsVector *= -1.f;
  326. }
  327. btVector3 ptOnB = witnessPointB + offsetB;
  328. btScalar distance = nl;
  329. resultOut.addContactPoint(ptsVector, ptOnB, -distance);
  330. }
  331. }
  332. if ((DeltaC2.dot(sep)) < 0.0f)
  333. sep = -sep;
  334. return true;
  335. }
  336. void btPolyhedralContactClipping::clipFaceAgainstHull(const btVector3& separatingNormal, const btConvexPolyhedron& hullA, const btTransform& transA, btVertexArray& worldVertsB1, btVertexArray& worldVertsB2, const btScalar minDist, btScalar maxDist, btDiscreteCollisionDetectorInterface::Result& resultOut)
  337. {
  338. worldVertsB2.resize(0);
  339. btVertexArray* pVtxIn = &worldVertsB1;
  340. btVertexArray* pVtxOut = &worldVertsB2;
  341. pVtxOut->reserve(pVtxIn->size());
  342. int closestFaceA = -1;
  343. {
  344. btScalar dmin = FLT_MAX;
  345. for (int face = 0; face < hullA.m_faces.size(); face++)
  346. {
  347. const btVector3 Normal(hullA.m_faces[face].m_plane[0], hullA.m_faces[face].m_plane[1], hullA.m_faces[face].m_plane[2]);
  348. const btVector3 faceANormalWS = transA.getBasis() * Normal;
  349. btScalar d = faceANormalWS.dot(separatingNormal);
  350. if (d < dmin)
  351. {
  352. dmin = d;
  353. closestFaceA = face;
  354. }
  355. }
  356. }
  357. if (closestFaceA < 0)
  358. return;
  359. const btFace& polyA = hullA.m_faces[closestFaceA];
  360. // clip polygon to back of planes of all faces of hull A that are adjacent to witness face
  361. int numVerticesA = polyA.m_indices.size();
  362. for (int e0 = 0; e0 < numVerticesA; e0++)
  363. {
  364. const btVector3& a = hullA.m_vertices[polyA.m_indices[e0]];
  365. const btVector3& b = hullA.m_vertices[polyA.m_indices[(e0 + 1) % numVerticesA]];
  366. const btVector3 edge0 = a - b;
  367. const btVector3 WorldEdge0 = transA.getBasis() * edge0;
  368. btVector3 worldPlaneAnormal1 = transA.getBasis() * btVector3(polyA.m_plane[0], polyA.m_plane[1], polyA.m_plane[2]);
  369. btVector3 planeNormalWS1 = -WorldEdge0.cross(worldPlaneAnormal1); //.cross(WorldEdge0);
  370. btVector3 worldA1 = transA * a;
  371. btScalar planeEqWS1 = -worldA1.dot(planeNormalWS1);
  372. //int otherFace=0;
  373. #ifdef BLA1
  374. int otherFace = polyA.m_connectedFaces[e0];
  375. btVector3 localPlaneNormal(hullA.m_faces[otherFace].m_plane[0], hullA.m_faces[otherFace].m_plane[1], hullA.m_faces[otherFace].m_plane[2]);
  376. btScalar localPlaneEq = hullA.m_faces[otherFace].m_plane[3];
  377. btVector3 planeNormalWS = transA.getBasis() * localPlaneNormal;
  378. btScalar planeEqWS = localPlaneEq - planeNormalWS.dot(transA.getOrigin());
  379. #else
  380. btVector3 planeNormalWS = planeNormalWS1;
  381. btScalar planeEqWS = planeEqWS1;
  382. #endif
  383. //clip face
  384. clipFace(*pVtxIn, *pVtxOut, planeNormalWS, planeEqWS);
  385. btSwap(pVtxIn, pVtxOut);
  386. pVtxOut->resize(0);
  387. }
  388. //#define ONLY_REPORT_DEEPEST_POINT
  389. btVector3 point;
  390. // only keep points that are behind the witness face
  391. {
  392. btVector3 localPlaneNormal(polyA.m_plane[0], polyA.m_plane[1], polyA.m_plane[2]);
  393. btScalar localPlaneEq = polyA.m_plane[3];
  394. btVector3 planeNormalWS = transA.getBasis() * localPlaneNormal;
  395. btScalar planeEqWS = localPlaneEq - planeNormalWS.dot(transA.getOrigin());
  396. for (int i = 0; i < pVtxIn->size(); i++)
  397. {
  398. btVector3 vtx = pVtxIn->at(i);
  399. btScalar depth = planeNormalWS.dot(vtx) + planeEqWS;
  400. if (depth <= minDist)
  401. {
  402. // printf("clamped: depth=%f to minDist=%f\n",depth,minDist);
  403. depth = minDist;
  404. }
  405. if (depth <= maxDist)
  406. {
  407. btVector3 point = pVtxIn->at(i);
  408. #ifdef ONLY_REPORT_DEEPEST_POINT
  409. curMaxDist = depth;
  410. #else
  411. #if 0
  412. if (depth<-3)
  413. {
  414. printf("error in btPolyhedralContactClipping depth = %f\n", depth);
  415. printf("likely wrong separatingNormal passed in\n");
  416. }
  417. #endif
  418. resultOut.addContactPoint(separatingNormal, point, depth);
  419. #endif
  420. }
  421. }
  422. }
  423. #ifdef ONLY_REPORT_DEEPEST_POINT
  424. if (curMaxDist < maxDist)
  425. {
  426. resultOut.addContactPoint(separatingNormal, point, curMaxDist);
  427. }
  428. #endif //ONLY_REPORT_DEEPEST_POINT
  429. }
  430. void btPolyhedralContactClipping::clipHullAgainstHull(const btVector3& separatingNormal1, const btConvexPolyhedron& hullA, const btConvexPolyhedron& hullB, const btTransform& transA, const btTransform& transB, const btScalar minDist, btScalar maxDist, btVertexArray& worldVertsB1, btVertexArray& worldVertsB2, btDiscreteCollisionDetectorInterface::Result& resultOut)
  431. {
  432. btVector3 separatingNormal = separatingNormal1.normalized();
  433. // const btVector3 c0 = transA * hullA.m_localCenter;
  434. // const btVector3 c1 = transB * hullB.m_localCenter;
  435. //const btVector3 DeltaC2 = c0 - c1;
  436. int closestFaceB = -1;
  437. btScalar dmax = -FLT_MAX;
  438. {
  439. for (int face = 0; face < hullB.m_faces.size(); face++)
  440. {
  441. const btVector3 Normal(hullB.m_faces[face].m_plane[0], hullB.m_faces[face].m_plane[1], hullB.m_faces[face].m_plane[2]);
  442. const btVector3 WorldNormal = transB.getBasis() * Normal;
  443. btScalar d = WorldNormal.dot(separatingNormal);
  444. if (d > dmax)
  445. {
  446. dmax = d;
  447. closestFaceB = face;
  448. }
  449. }
  450. }
  451. worldVertsB1.resize(0);
  452. {
  453. const btFace& polyB = hullB.m_faces[closestFaceB];
  454. const int numVertices = polyB.m_indices.size();
  455. for (int e0 = 0; e0 < numVertices; e0++)
  456. {
  457. const btVector3& b = hullB.m_vertices[polyB.m_indices[e0]];
  458. worldVertsB1.push_back(transB * b);
  459. }
  460. }
  461. if (closestFaceB >= 0)
  462. clipFaceAgainstHull(separatingNormal, hullA, transA, worldVertsB1, worldVertsB2, minDist, maxDist, resultOut);
  463. }