2
0

btPolyhedralConvexShape.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2009 Erwin Coumans 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. #if defined(_WIN32) || defined(__i386__)
  14. #define BT_USE_SSE_IN_API
  15. #endif
  16. #include "BulletCollision/CollisionShapes/btPolyhedralConvexShape.h"
  17. #include "btConvexPolyhedron.h"
  18. #include "LinearMath/btConvexHullComputer.h"
  19. #include <new>
  20. #include "LinearMath/btGeometryUtil.h"
  21. #include "LinearMath/btGrahamScan2dConvexHull.h"
  22. btPolyhedralConvexShape::btPolyhedralConvexShape() : btConvexInternalShape(),
  23. m_polyhedron(0)
  24. {
  25. }
  26. btPolyhedralConvexShape::~btPolyhedralConvexShape()
  27. {
  28. if (m_polyhedron)
  29. {
  30. m_polyhedron->~btConvexPolyhedron();
  31. btAlignedFree(m_polyhedron);
  32. }
  33. }
  34. void btPolyhedralConvexShape::setPolyhedralFeatures(btConvexPolyhedron& polyhedron)
  35. {
  36. if (m_polyhedron)
  37. {
  38. *m_polyhedron = polyhedron;
  39. }
  40. else
  41. {
  42. void* mem = btAlignedAlloc(sizeof(btConvexPolyhedron), 16);
  43. m_polyhedron = new (mem) btConvexPolyhedron(polyhedron);
  44. }
  45. }
  46. bool btPolyhedralConvexShape::initializePolyhedralFeatures(int shiftVerticesByMargin)
  47. {
  48. if (m_polyhedron)
  49. {
  50. m_polyhedron->~btConvexPolyhedron();
  51. btAlignedFree(m_polyhedron);
  52. }
  53. void* mem = btAlignedAlloc(sizeof(btConvexPolyhedron), 16);
  54. m_polyhedron = new (mem) btConvexPolyhedron;
  55. btAlignedObjectArray<btVector3> orgVertices;
  56. for (int i = 0; i < getNumVertices(); i++)
  57. {
  58. btVector3& newVertex = orgVertices.expand();
  59. getVertex(i, newVertex);
  60. }
  61. btConvexHullComputer conv;
  62. if (shiftVerticesByMargin)
  63. {
  64. btAlignedObjectArray<btVector3> planeEquations;
  65. btGeometryUtil::getPlaneEquationsFromVertices(orgVertices, planeEquations);
  66. btAlignedObjectArray<btVector3> shiftedPlaneEquations;
  67. for (int p = 0; p < planeEquations.size(); p++)
  68. {
  69. btVector3 plane = planeEquations[p];
  70. // btScalar margin = getMargin();
  71. plane[3] -= getMargin();
  72. shiftedPlaneEquations.push_back(plane);
  73. }
  74. btAlignedObjectArray<btVector3> tmpVertices;
  75. btGeometryUtil::getVerticesFromPlaneEquations(shiftedPlaneEquations, tmpVertices);
  76. conv.compute(&tmpVertices[0].getX(), sizeof(btVector3), tmpVertices.size(), 0.f, 0.f);
  77. }
  78. else
  79. {
  80. conv.compute(&orgVertices[0].getX(), sizeof(btVector3), orgVertices.size(), 0.f, 0.f);
  81. }
  82. #ifndef BT_RECONSTRUCT_FACES
  83. int numVertices = conv.vertices.size();
  84. m_polyhedron->m_vertices.resize(numVertices);
  85. for (int p = 0; p < numVertices; p++)
  86. {
  87. m_polyhedron->m_vertices[p] = conv.vertices[p];
  88. }
  89. int v0, v1;
  90. for (int j = 0; j < conv.faces.size(); j++)
  91. {
  92. btVector3 edges[3];
  93. int numEdges = 0;
  94. btFace combinedFace;
  95. const btConvexHullComputer::Edge* edge = &conv.edges[conv.faces[j]];
  96. v0 = edge->getSourceVertex();
  97. int prevVertex = v0;
  98. combinedFace.m_indices.push_back(v0);
  99. v1 = edge->getTargetVertex();
  100. while (v1 != v0)
  101. {
  102. btVector3 wa = conv.vertices[prevVertex];
  103. btVector3 wb = conv.vertices[v1];
  104. btVector3 newEdge = wb - wa;
  105. newEdge.normalize();
  106. if (numEdges < 2)
  107. edges[numEdges++] = newEdge;
  108. //face->addIndex(v1);
  109. combinedFace.m_indices.push_back(v1);
  110. edge = edge->getNextEdgeOfFace();
  111. prevVertex = v1;
  112. int v01 = edge->getSourceVertex();
  113. v1 = edge->getTargetVertex();
  114. }
  115. btAssert(combinedFace.m_indices.size() > 2);
  116. btVector3 faceNormal = edges[0].cross(edges[1]);
  117. faceNormal.normalize();
  118. btScalar planeEq = 1e30f;
  119. for (int v = 0; v < combinedFace.m_indices.size(); v++)
  120. {
  121. btScalar eq = m_polyhedron->m_vertices[combinedFace.m_indices[v]].dot(faceNormal);
  122. if (planeEq > eq)
  123. {
  124. planeEq = eq;
  125. }
  126. }
  127. combinedFace.m_plane[0] = faceNormal.getX();
  128. combinedFace.m_plane[1] = faceNormal.getY();
  129. combinedFace.m_plane[2] = faceNormal.getZ();
  130. combinedFace.m_plane[3] = -planeEq;
  131. m_polyhedron->m_faces.push_back(combinedFace);
  132. }
  133. #else //BT_RECONSTRUCT_FACES
  134. btAlignedObjectArray<btVector3> faceNormals;
  135. int numFaces = conv.faces.size();
  136. faceNormals.resize(numFaces);
  137. btConvexHullComputer* convexUtil = &conv;
  138. btAlignedObjectArray<btFace> tmpFaces;
  139. tmpFaces.resize(numFaces);
  140. int numVertices = convexUtil->vertices.size();
  141. m_polyhedron->m_vertices.resize(numVertices);
  142. for (int p = 0; p < numVertices; p++)
  143. {
  144. m_polyhedron->m_vertices[p] = convexUtil->vertices[p];
  145. }
  146. for (int i = 0; i < numFaces; i++)
  147. {
  148. int face = convexUtil->faces[i];
  149. //printf("face=%d\n",face);
  150. const btConvexHullComputer::Edge* firstEdge = &convexUtil->edges[face];
  151. const btConvexHullComputer::Edge* edge = firstEdge;
  152. btVector3 edges[3];
  153. int numEdges = 0;
  154. //compute face normals
  155. do
  156. {
  157. int src = edge->getSourceVertex();
  158. tmpFaces[i].m_indices.push_back(src);
  159. int targ = edge->getTargetVertex();
  160. btVector3 wa = convexUtil->vertices[src];
  161. btVector3 wb = convexUtil->vertices[targ];
  162. btVector3 newEdge = wb - wa;
  163. newEdge.normalize();
  164. if (numEdges < 2)
  165. edges[numEdges++] = newEdge;
  166. edge = edge->getNextEdgeOfFace();
  167. } while (edge != firstEdge);
  168. btScalar planeEq = 1e30f;
  169. if (numEdges == 2)
  170. {
  171. faceNormals[i] = edges[0].cross(edges[1]);
  172. faceNormals[i].normalize();
  173. tmpFaces[i].m_plane[0] = faceNormals[i].getX();
  174. tmpFaces[i].m_plane[1] = faceNormals[i].getY();
  175. tmpFaces[i].m_plane[2] = faceNormals[i].getZ();
  176. tmpFaces[i].m_plane[3] = planeEq;
  177. }
  178. else
  179. {
  180. btAssert(0); //degenerate?
  181. faceNormals[i].setZero();
  182. }
  183. for (int v = 0; v < tmpFaces[i].m_indices.size(); v++)
  184. {
  185. btScalar eq = m_polyhedron->m_vertices[tmpFaces[i].m_indices[v]].dot(faceNormals[i]);
  186. if (planeEq > eq)
  187. {
  188. planeEq = eq;
  189. }
  190. }
  191. tmpFaces[i].m_plane[3] = -planeEq;
  192. }
  193. //merge coplanar faces and copy them to m_polyhedron
  194. btScalar faceWeldThreshold = 0.999f;
  195. btAlignedObjectArray<int> todoFaces;
  196. for (int i = 0; i < tmpFaces.size(); i++)
  197. todoFaces.push_back(i);
  198. while (todoFaces.size())
  199. {
  200. btAlignedObjectArray<int> coplanarFaceGroup;
  201. int refFace = todoFaces[todoFaces.size() - 1];
  202. coplanarFaceGroup.push_back(refFace);
  203. btFace& faceA = tmpFaces[refFace];
  204. todoFaces.pop_back();
  205. btVector3 faceNormalA(faceA.m_plane[0], faceA.m_plane[1], faceA.m_plane[2]);
  206. for (int j = todoFaces.size() - 1; j >= 0; j--)
  207. {
  208. int i = todoFaces[j];
  209. btFace& faceB = tmpFaces[i];
  210. btVector3 faceNormalB(faceB.m_plane[0], faceB.m_plane[1], faceB.m_plane[2]);
  211. if (faceNormalA.dot(faceNormalB) > faceWeldThreshold)
  212. {
  213. coplanarFaceGroup.push_back(i);
  214. todoFaces.remove(i);
  215. }
  216. }
  217. bool did_merge = false;
  218. if (coplanarFaceGroup.size() > 1)
  219. {
  220. //do the merge: use Graham Scan 2d convex hull
  221. btAlignedObjectArray<GrahamVector3> orgpoints;
  222. btVector3 averageFaceNormal(0, 0, 0);
  223. for (int i = 0; i < coplanarFaceGroup.size(); i++)
  224. {
  225. // m_polyhedron->m_faces.push_back(tmpFaces[coplanarFaceGroup[i]]);
  226. btFace& face = tmpFaces[coplanarFaceGroup[i]];
  227. btVector3 faceNormal(face.m_plane[0], face.m_plane[1], face.m_plane[2]);
  228. averageFaceNormal += faceNormal;
  229. for (int f = 0; f < face.m_indices.size(); f++)
  230. {
  231. int orgIndex = face.m_indices[f];
  232. btVector3 pt = m_polyhedron->m_vertices[orgIndex];
  233. bool found = false;
  234. for (int i = 0; i < orgpoints.size(); i++)
  235. {
  236. //if ((orgpoints[i].m_orgIndex == orgIndex) || ((rotatedPt-orgpoints[i]).length2()<0.0001))
  237. if (orgpoints[i].m_orgIndex == orgIndex)
  238. {
  239. found = true;
  240. break;
  241. }
  242. }
  243. if (!found)
  244. orgpoints.push_back(GrahamVector3(pt, orgIndex));
  245. }
  246. }
  247. btFace combinedFace;
  248. for (int i = 0; i < 4; i++)
  249. combinedFace.m_plane[i] = tmpFaces[coplanarFaceGroup[0]].m_plane[i];
  250. btAlignedObjectArray<GrahamVector3> hull;
  251. averageFaceNormal.normalize();
  252. GrahamScanConvexHull2D(orgpoints, hull, averageFaceNormal);
  253. for (int i = 0; i < hull.size(); i++)
  254. {
  255. combinedFace.m_indices.push_back(hull[i].m_orgIndex);
  256. for (int k = 0; k < orgpoints.size(); k++)
  257. {
  258. if (orgpoints[k].m_orgIndex == hull[i].m_orgIndex)
  259. {
  260. orgpoints[k].m_orgIndex = -1; // invalidate...
  261. break;
  262. }
  263. }
  264. }
  265. // are there rejected vertices?
  266. bool reject_merge = false;
  267. for (int i = 0; i < orgpoints.size(); i++)
  268. {
  269. if (orgpoints[i].m_orgIndex == -1)
  270. continue; // this is in the hull...
  271. // this vertex is rejected -- is anybody else using this vertex?
  272. for (int j = 0; j < tmpFaces.size(); j++)
  273. {
  274. btFace& face = tmpFaces[j];
  275. // is this a face of the current coplanar group?
  276. bool is_in_current_group = false;
  277. for (int k = 0; k < coplanarFaceGroup.size(); k++)
  278. {
  279. if (coplanarFaceGroup[k] == j)
  280. {
  281. is_in_current_group = true;
  282. break;
  283. }
  284. }
  285. if (is_in_current_group) // ignore this face...
  286. continue;
  287. // does this face use this rejected vertex?
  288. for (int v = 0; v < face.m_indices.size(); v++)
  289. {
  290. if (face.m_indices[v] == orgpoints[i].m_orgIndex)
  291. {
  292. // this rejected vertex is used in another face -- reject merge
  293. reject_merge = true;
  294. break;
  295. }
  296. }
  297. if (reject_merge)
  298. break;
  299. }
  300. if (reject_merge)
  301. break;
  302. }
  303. if (!reject_merge)
  304. {
  305. // do this merge!
  306. did_merge = true;
  307. m_polyhedron->m_faces.push_back(combinedFace);
  308. }
  309. }
  310. if (!did_merge)
  311. {
  312. for (int i = 0; i < coplanarFaceGroup.size(); i++)
  313. {
  314. btFace face = tmpFaces[coplanarFaceGroup[i]];
  315. m_polyhedron->m_faces.push_back(face);
  316. }
  317. }
  318. }
  319. #endif //BT_RECONSTRUCT_FACES
  320. m_polyhedron->initialize();
  321. return true;
  322. }
  323. #ifndef MIN
  324. #define MIN(_a, _b) ((_a) < (_b) ? (_a) : (_b))
  325. #endif
  326. btVector3 btPolyhedralConvexShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0) const
  327. {
  328. btVector3 supVec(0, 0, 0);
  329. #ifndef __SPU__
  330. int i;
  331. btScalar maxDot(btScalar(-BT_LARGE_FLOAT));
  332. btVector3 vec = vec0;
  333. btScalar lenSqr = vec.length2();
  334. if (lenSqr < btScalar(0.0001))
  335. {
  336. vec.setValue(1, 0, 0);
  337. }
  338. else
  339. {
  340. btScalar rlen = btScalar(1.) / btSqrt(lenSqr);
  341. vec *= rlen;
  342. }
  343. btVector3 vtx;
  344. btScalar newDot;
  345. for (int k = 0; k < getNumVertices(); k += 128)
  346. {
  347. btVector3 temp[128];
  348. int inner_count = MIN(getNumVertices() - k, 128);
  349. for (i = 0; i < inner_count; i++)
  350. getVertex(i, temp[i]);
  351. i = (int)vec.maxDot(temp, inner_count, newDot);
  352. if (newDot > maxDot)
  353. {
  354. maxDot = newDot;
  355. supVec = temp[i];
  356. }
  357. }
  358. #endif //__SPU__
  359. return supVec;
  360. }
  361. void btPolyhedralConvexShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const
  362. {
  363. #ifndef __SPU__
  364. int i;
  365. btVector3 vtx;
  366. btScalar newDot;
  367. for (i = 0; i < numVectors; i++)
  368. {
  369. supportVerticesOut[i][3] = btScalar(-BT_LARGE_FLOAT);
  370. }
  371. for (int j = 0; j < numVectors; j++)
  372. {
  373. const btVector3& vec = vectors[j];
  374. for (int k = 0; k < getNumVertices(); k += 128)
  375. {
  376. btVector3 temp[128];
  377. int inner_count = MIN(getNumVertices() - k, 128);
  378. for (i = 0; i < inner_count; i++)
  379. getVertex(i, temp[i]);
  380. i = (int)vec.maxDot(temp, inner_count, newDot);
  381. if (newDot > supportVerticesOut[j][3])
  382. {
  383. supportVerticesOut[j] = temp[i];
  384. supportVerticesOut[j][3] = newDot;
  385. }
  386. }
  387. }
  388. #endif //__SPU__
  389. }
  390. void btPolyhedralConvexShape::calculateLocalInertia(btScalar mass, btVector3& inertia) const
  391. {
  392. #ifndef __SPU__
  393. //not yet, return box inertia
  394. btScalar margin = getMargin();
  395. btTransform ident;
  396. ident.setIdentity();
  397. btVector3 aabbMin, aabbMax;
  398. getAabb(ident, aabbMin, aabbMax);
  399. btVector3 halfExtents = (aabbMax - aabbMin) * btScalar(0.5);
  400. btScalar lx = btScalar(2.) * (halfExtents.x() + margin);
  401. btScalar ly = btScalar(2.) * (halfExtents.y() + margin);
  402. btScalar lz = btScalar(2.) * (halfExtents.z() + margin);
  403. const btScalar x2 = lx * lx;
  404. const btScalar y2 = ly * ly;
  405. const btScalar z2 = lz * lz;
  406. const btScalar scaledmass = mass * btScalar(0.08333333);
  407. inertia = scaledmass * (btVector3(y2 + z2, x2 + z2, x2 + y2));
  408. #endif //__SPU__
  409. }
  410. void btPolyhedralConvexAabbCachingShape::setLocalScaling(const btVector3& scaling)
  411. {
  412. btConvexInternalShape::setLocalScaling(scaling);
  413. recalcLocalAabb();
  414. }
  415. btPolyhedralConvexAabbCachingShape::btPolyhedralConvexAabbCachingShape()
  416. : btPolyhedralConvexShape(),
  417. m_localAabbMin(1, 1, 1),
  418. m_localAabbMax(-1, -1, -1),
  419. m_isLocalAabbValid(false)
  420. {
  421. }
  422. void btPolyhedralConvexAabbCachingShape::getAabb(const btTransform& trans, btVector3& aabbMin, btVector3& aabbMax) const
  423. {
  424. getNonvirtualAabb(trans, aabbMin, aabbMax, getMargin());
  425. }
  426. void btPolyhedralConvexAabbCachingShape::recalcLocalAabb()
  427. {
  428. m_isLocalAabbValid = true;
  429. #if 1
  430. static const btVector3 _directions[] =
  431. {
  432. btVector3(1., 0., 0.),
  433. btVector3(0., 1., 0.),
  434. btVector3(0., 0., 1.),
  435. btVector3(-1., 0., 0.),
  436. btVector3(0., -1., 0.),
  437. btVector3(0., 0., -1.)};
  438. btVector3 _supporting[] =
  439. {
  440. btVector3(0., 0., 0.),
  441. btVector3(0., 0., 0.),
  442. btVector3(0., 0., 0.),
  443. btVector3(0., 0., 0.),
  444. btVector3(0., 0., 0.),
  445. btVector3(0., 0., 0.)};
  446. batchedUnitVectorGetSupportingVertexWithoutMargin(_directions, _supporting, 6);
  447. for (int i = 0; i < 3; ++i)
  448. {
  449. m_localAabbMax[i] = _supporting[i][i] + m_collisionMargin;
  450. m_localAabbMin[i] = _supporting[i + 3][i] - m_collisionMargin;
  451. }
  452. #else
  453. for (int i = 0; i < 3; i++)
  454. {
  455. btVector3 vec(btScalar(0.), btScalar(0.), btScalar(0.));
  456. vec[i] = btScalar(1.);
  457. btVector3 tmp = localGetSupportingVertex(vec);
  458. m_localAabbMax[i] = tmp[i];
  459. vec[i] = btScalar(-1.);
  460. tmp = localGetSupportingVertex(vec);
  461. m_localAabbMin[i] = tmp[i];
  462. }
  463. #endif
  464. }