btConvexPolyhedron.cpp 7.8 KB

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