btConvexPolyhedron.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 "btConvexPolyhedron.h"
  17. #include "bullet/LinearMath/btHashMap.h"
  18. btConvexPolyhedron::btConvexPolyhedron()
  19. {
  20. }
  21. btConvexPolyhedron::~btConvexPolyhedron()
  22. {
  23. }
  24. inline bool IsAlmostZero(const btVector3& v)
  25. {
  26. if(fabsf(v.x())>1e-6 || fabsf(v.y())>1e-6 || fabsf(v.z())>1e-6) return false;
  27. return true;
  28. }
  29. struct btInternalVertexPair
  30. {
  31. btInternalVertexPair(short int v0,short int v1)
  32. :m_v0(v0),
  33. m_v1(v1)
  34. {
  35. if (m_v1>m_v0)
  36. btSwap(m_v0,m_v1);
  37. }
  38. short int m_v0;
  39. short int m_v1;
  40. int getHash() const
  41. {
  42. return m_v0+(m_v1<<16);
  43. }
  44. bool equals(const btInternalVertexPair& other) const
  45. {
  46. return m_v0==other.m_v0 && m_v1==other.m_v1;
  47. }
  48. };
  49. struct btInternalEdge
  50. {
  51. btInternalEdge()
  52. :m_face0(-1),
  53. m_face1(-1)
  54. {
  55. }
  56. short int m_face0;
  57. short int m_face1;
  58. };
  59. //
  60. #ifdef TEST_INTERNAL_OBJECTS
  61. bool btConvexPolyhedron::testContainment() const
  62. {
  63. for(int p=0;p<8;p++)
  64. {
  65. btVector3 LocalPt;
  66. if(p==0) LocalPt = m_localCenter + btVector3(m_extents[0], m_extents[1], m_extents[2]);
  67. else if(p==1) LocalPt = m_localCenter + btVector3(m_extents[0], m_extents[1], -m_extents[2]);
  68. else if(p==2) LocalPt = m_localCenter + btVector3(m_extents[0], -m_extents[1], m_extents[2]);
  69. else if(p==3) LocalPt = m_localCenter + btVector3(m_extents[0], -m_extents[1], -m_extents[2]);
  70. else if(p==4) LocalPt = m_localCenter + btVector3(-m_extents[0], m_extents[1], m_extents[2]);
  71. else if(p==5) LocalPt = m_localCenter + btVector3(-m_extents[0], m_extents[1], -m_extents[2]);
  72. else if(p==6) LocalPt = m_localCenter + btVector3(-m_extents[0], -m_extents[1], m_extents[2]);
  73. else if(p==7) LocalPt = m_localCenter + btVector3(-m_extents[0], -m_extents[1], -m_extents[2]);
  74. for(int i=0;i<m_faces.size();i++)
  75. {
  76. const btVector3 Normal(m_faces[i].m_plane[0], m_faces[i].m_plane[1], m_faces[i].m_plane[2]);
  77. const btScalar d = LocalPt.dot(Normal) + m_faces[i].m_plane[3];
  78. if(d>0.0f)
  79. return false;
  80. }
  81. }
  82. return true;
  83. }
  84. #endif
  85. void btConvexPolyhedron::initialize()
  86. {
  87. btHashMap<btInternalVertexPair,btInternalEdge> edges;
  88. btScalar TotalArea = 0.0f;
  89. m_localCenter.setValue(0, 0, 0);
  90. for(int i=0;i<m_faces.size();i++)
  91. {
  92. int numVertices = m_faces[i].m_indices.size();
  93. int NbTris = numVertices;
  94. for(int j=0;j<NbTris;j++)
  95. {
  96. int k = (j+1)%numVertices;
  97. btInternalVertexPair vp(m_faces[i].m_indices[j],m_faces[i].m_indices[k]);
  98. btInternalEdge* edptr = edges.find(vp);
  99. btVector3 edge = m_vertices[vp.m_v1]-m_vertices[vp.m_v0];
  100. edge.normalize();
  101. bool found = false;
  102. for (int p=0;p<m_uniqueEdges.size();p++)
  103. {
  104. if (IsAlmostZero(m_uniqueEdges[p]-edge) ||
  105. IsAlmostZero(m_uniqueEdges[p]+edge))
  106. {
  107. found = true;
  108. break;
  109. }
  110. }
  111. if (!found)
  112. {
  113. m_uniqueEdges.push_back(edge);
  114. }
  115. if (edptr)
  116. {
  117. btAssert(edptr->m_face0>=0);
  118. btAssert(edptr->m_face1<0);
  119. edptr->m_face1 = i;
  120. } else
  121. {
  122. btInternalEdge ed;
  123. ed.m_face0 = i;
  124. edges.insert(vp,ed);
  125. }
  126. }
  127. }
  128. #ifdef USE_CONNECTED_FACES
  129. for(int i=0;i<m_faces.size();i++)
  130. {
  131. int numVertices = m_faces[i].m_indices.size();
  132. m_faces[i].m_connectedFaces.resize(numVertices);
  133. for(int j=0;j<numVertices;j++)
  134. {
  135. int k = (j+1)%numVertices;
  136. btInternalVertexPair vp(m_faces[i].m_indices[j],m_faces[i].m_indices[k]);
  137. btInternalEdge* edptr = edges.find(vp);
  138. btAssert(edptr);
  139. btAssert(edptr->m_face0>=0);
  140. btAssert(edptr->m_face1>=0);
  141. int connectedFace = (edptr->m_face0==i)?edptr->m_face1:edptr->m_face0;
  142. m_faces[i].m_connectedFaces[j] = connectedFace;
  143. }
  144. }
  145. #endif//USE_CONNECTED_FACES
  146. for(int i=0;i<m_faces.size();i++)
  147. {
  148. int numVertices = m_faces[i].m_indices.size();
  149. int NbTris = numVertices-2;
  150. const btVector3& p0 = m_vertices[m_faces[i].m_indices[0]];
  151. for(int j=1;j<=NbTris;j++)
  152. {
  153. int k = (j+1)%numVertices;
  154. const btVector3& p1 = m_vertices[m_faces[i].m_indices[j]];
  155. const btVector3& p2 = m_vertices[m_faces[i].m_indices[k]];
  156. btScalar Area = ((p0 - p1).cross(p0 - p2)).length() * 0.5f;
  157. btVector3 Center = (p0+p1+p2)/3.0f;
  158. m_localCenter += Area * Center;
  159. TotalArea += Area;
  160. }
  161. }
  162. m_localCenter /= TotalArea;
  163. #ifdef TEST_INTERNAL_OBJECTS
  164. if(1)
  165. {
  166. m_radius = FLT_MAX;
  167. for(int i=0;i<m_faces.size();i++)
  168. {
  169. const btVector3 Normal(m_faces[i].m_plane[0], m_faces[i].m_plane[1], m_faces[i].m_plane[2]);
  170. const btScalar dist = btFabs(m_localCenter.dot(Normal) + m_faces[i].m_plane[3]);
  171. if(dist<m_radius)
  172. m_radius = dist;
  173. }
  174. btScalar MinX = FLT_MAX;
  175. btScalar MinY = FLT_MAX;
  176. btScalar MinZ = FLT_MAX;
  177. btScalar MaxX = -FLT_MAX;
  178. btScalar MaxY = -FLT_MAX;
  179. btScalar MaxZ = -FLT_MAX;
  180. for(int i=0; i<m_vertices.size(); i++)
  181. {
  182. const btVector3& pt = m_vertices[i];
  183. if(pt.x()<MinX) MinX = pt.x();
  184. if(pt.x()>MaxX) MaxX = pt.x();
  185. if(pt.y()<MinY) MinY = pt.y();
  186. if(pt.y()>MaxY) MaxY = pt.y();
  187. if(pt.z()<MinZ) MinZ = pt.z();
  188. if(pt.z()>MaxZ) MaxZ = pt.z();
  189. }
  190. mC.setValue(MaxX+MinX, MaxY+MinY, MaxZ+MinZ);
  191. mE.setValue(MaxX-MinX, MaxY-MinY, MaxZ-MinZ);
  192. // const btScalar r = m_radius / sqrtf(2.0f);
  193. const btScalar r = m_radius / sqrtf(3.0f);
  194. const int LargestExtent = mE.maxAxis();
  195. const btScalar Step = (mE[LargestExtent]*0.5f - r)/1024.0f;
  196. m_extents[0] = m_extents[1] = m_extents[2] = r;
  197. m_extents[LargestExtent] = mE[LargestExtent]*0.5f;
  198. bool FoundBox = false;
  199. for(int j=0;j<1024;j++)
  200. {
  201. if(testContainment())
  202. {
  203. FoundBox = true;
  204. break;
  205. }
  206. m_extents[LargestExtent] -= Step;
  207. }
  208. if(!FoundBox)
  209. {
  210. m_extents[0] = m_extents[1] = m_extents[2] = r;
  211. }
  212. else
  213. {
  214. // Refine the box
  215. const btScalar Step = (m_radius - r)/1024.0f;
  216. const int e0 = (1<<LargestExtent) & 3;
  217. const int e1 = (1<<e0) & 3;
  218. for(int j=0;j<1024;j++)
  219. {
  220. const btScalar Saved0 = m_extents[e0];
  221. const btScalar Saved1 = m_extents[e1];
  222. m_extents[e0] += Step;
  223. m_extents[e1] += Step;
  224. if(!testContainment())
  225. {
  226. m_extents[e0] = Saved0;
  227. m_extents[e1] = Saved1;
  228. break;
  229. }
  230. }
  231. }
  232. }
  233. #endif
  234. }
  235. void btConvexPolyhedron::project(const btTransform& trans, const btVector3& dir, btScalar& minProj, btScalar& maxProj, btVector3& witnesPtMin,btVector3& witnesPtMax) const
  236. {
  237. minProj = FLT_MAX;
  238. maxProj = -FLT_MAX;
  239. int numVerts = m_vertices.size();
  240. for(int i=0;i<numVerts;i++)
  241. {
  242. btVector3 pt = trans * m_vertices[i];
  243. btScalar dp = pt.dot(dir);
  244. if(dp < minProj)
  245. {
  246. minProj = dp;
  247. witnesPtMin = pt;
  248. }
  249. if(dp > maxProj)
  250. {
  251. maxProj = dp;
  252. witnesPtMax = pt;
  253. }
  254. }
  255. if(minProj>maxProj)
  256. {
  257. btSwap(minProj,maxProj);
  258. btSwap(witnesPtMin,witnesPtMax);
  259. }
  260. }