btPolyhedralConvexShape.cpp 12 KB

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