b3CpuNarrowPhase.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #include "b3CpuNarrowPhase.h"
  2. #include "Bullet3Collision/NarrowPhaseCollision/b3ConvexUtility.h"
  3. #include "Bullet3Collision/NarrowPhaseCollision/b3Config.h"
  4. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3ConvexPolyhedronData.h"
  5. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3ContactConvexConvexSAT.h"
  6. struct b3CpuNarrowPhaseInternalData
  7. {
  8. b3AlignedObjectArray<b3Aabb> m_localShapeAABBCPU;
  9. b3AlignedObjectArray<b3Collidable> m_collidablesCPU;
  10. b3AlignedObjectArray<b3ConvexUtility*> m_convexData;
  11. b3Config m_config;
  12. b3AlignedObjectArray<b3ConvexPolyhedronData> m_convexPolyhedra;
  13. b3AlignedObjectArray<b3Vector3> m_uniqueEdges;
  14. b3AlignedObjectArray<b3Vector3> m_convexVertices;
  15. b3AlignedObjectArray<int> m_convexIndices;
  16. b3AlignedObjectArray<b3GpuFace> m_convexFaces;
  17. b3AlignedObjectArray<b3Contact4Data> m_contacts;
  18. int m_numAcceleratedShapes;
  19. };
  20. const b3AlignedObjectArray<b3Contact4Data>& b3CpuNarrowPhase::getContacts() const
  21. {
  22. return m_data->m_contacts;
  23. }
  24. b3Collidable& b3CpuNarrowPhase::getCollidableCpu(int collidableIndex)
  25. {
  26. return m_data->m_collidablesCPU[collidableIndex];
  27. }
  28. const b3Collidable& b3CpuNarrowPhase::getCollidableCpu(int collidableIndex) const
  29. {
  30. return m_data->m_collidablesCPU[collidableIndex];
  31. }
  32. b3CpuNarrowPhase::b3CpuNarrowPhase(const struct b3Config& config)
  33. {
  34. m_data = new b3CpuNarrowPhaseInternalData;
  35. m_data->m_config = config;
  36. m_data->m_numAcceleratedShapes = 0;
  37. }
  38. b3CpuNarrowPhase::~b3CpuNarrowPhase()
  39. {
  40. delete m_data;
  41. }
  42. void b3CpuNarrowPhase::computeContacts(b3AlignedObjectArray<b3Int4>& pairs, b3AlignedObjectArray<b3Aabb>& aabbsWorldSpace, b3AlignedObjectArray<b3RigidBodyData>& bodies)
  43. {
  44. int nPairs = pairs.size();
  45. int numContacts = 0;
  46. int maxContactCapacity = m_data->m_config.m_maxContactCapacity;
  47. m_data->m_contacts.resize(maxContactCapacity);
  48. for (int i = 0; i < nPairs; i++)
  49. {
  50. int bodyIndexA = pairs[i].x;
  51. int bodyIndexB = pairs[i].y;
  52. int collidableIndexA = bodies[bodyIndexA].m_collidableIdx;
  53. int collidableIndexB = bodies[bodyIndexB].m_collidableIdx;
  54. if (m_data->m_collidablesCPU[collidableIndexA].m_shapeType == SHAPE_SPHERE &&
  55. m_data->m_collidablesCPU[collidableIndexB].m_shapeType == SHAPE_CONVEX_HULL)
  56. {
  57. // computeContactSphereConvex(i,bodyIndexA,bodyIndexB,collidableIndexA,collidableIndexB,&bodies[0],
  58. // &m_data->m_collidablesCPU[0],&hostConvexData[0],&hostVertices[0],&hostIndices[0],&hostFaces[0],&hostContacts[0],nContacts,maxContactCapacity);
  59. }
  60. if (m_data->m_collidablesCPU[collidableIndexA].m_shapeType == SHAPE_CONVEX_HULL &&
  61. m_data->m_collidablesCPU[collidableIndexB].m_shapeType == SHAPE_SPHERE)
  62. {
  63. // computeContactSphereConvex(i,bodyIndexB,bodyIndexA,collidableIndexB,collidableIndexA,&bodies[0],
  64. // &m_data->m_collidablesCPU[0],&hostConvexData[0],&hostVertices[0],&hostIndices[0],&hostFaces[0],&hostContacts[0],nContacts,maxContactCapacity);
  65. //printf("convex-sphere\n");
  66. }
  67. if (m_data->m_collidablesCPU[collidableIndexA].m_shapeType == SHAPE_CONVEX_HULL &&
  68. m_data->m_collidablesCPU[collidableIndexB].m_shapeType == SHAPE_PLANE)
  69. {
  70. // computeContactPlaneConvex(i,bodyIndexB,bodyIndexA,collidableIndexB,collidableIndexA,&bodies[0],
  71. // &m_data->m_collidablesCPU[0],&hostConvexData[0],&hostVertices[0],&hostIndices[0],&hostFaces[0],&hostContacts[0],nContacts,maxContactCapacity);
  72. // printf("convex-plane\n");
  73. }
  74. if (m_data->m_collidablesCPU[collidableIndexA].m_shapeType == SHAPE_PLANE &&
  75. m_data->m_collidablesCPU[collidableIndexB].m_shapeType == SHAPE_CONVEX_HULL)
  76. {
  77. // computeContactPlaneConvex(i,bodyIndexA,bodyIndexB,collidableIndexA,collidableIndexB,&bodies[0],
  78. // &m_data->m_collidablesCPU[0],&hostConvexData[0],&hostVertices[0],&hostIndices[0],&hostFaces[0],&hostContacts[0],nContacts,maxContactCapacity);
  79. // printf("plane-convex\n");
  80. }
  81. if (m_data->m_collidablesCPU[collidableIndexA].m_shapeType == SHAPE_COMPOUND_OF_CONVEX_HULLS &&
  82. m_data->m_collidablesCPU[collidableIndexB].m_shapeType == SHAPE_COMPOUND_OF_CONVEX_HULLS)
  83. {
  84. // computeContactCompoundCompound(i,bodyIndexB,bodyIndexA,collidableIndexB,collidableIndexA,&bodies[0],
  85. // &m_data->m_collidablesCPU[0],&hostConvexData[0],&cpuChildShapes[0], hostAabbsWorldSpace,hostAabbsLocalSpace,hostVertices,hostUniqueEdges,hostIndices,hostFaces,&hostContacts[0],
  86. // nContacts,maxContactCapacity,treeNodesCPU,subTreesCPU,bvhInfoCPU);
  87. // printf("convex-plane\n");
  88. }
  89. if (m_data->m_collidablesCPU[collidableIndexA].m_shapeType == SHAPE_COMPOUND_OF_CONVEX_HULLS &&
  90. m_data->m_collidablesCPU[collidableIndexB].m_shapeType == SHAPE_PLANE)
  91. {
  92. // computeContactPlaneCompound(i,bodyIndexB,bodyIndexA,collidableIndexB,collidableIndexA,&bodies[0],
  93. // &m_data->m_collidablesCPU[0],&hostConvexData[0],&cpuChildShapes[0], &hostVertices[0],&hostIndices[0],&hostFaces[0],&hostContacts[0],nContacts,maxContactCapacity);
  94. // printf("convex-plane\n");
  95. }
  96. if (m_data->m_collidablesCPU[collidableIndexA].m_shapeType == SHAPE_PLANE &&
  97. m_data->m_collidablesCPU[collidableIndexB].m_shapeType == SHAPE_COMPOUND_OF_CONVEX_HULLS)
  98. {
  99. // computeContactPlaneCompound(i,bodyIndexA,bodyIndexB,collidableIndexA,collidableIndexB,&bodies[0],
  100. // &m_data->m_collidablesCPU[0],&hostConvexData[0],&cpuChildShapes[0],&hostVertices[0],&hostIndices[0],&hostFaces[0],&hostContacts[0],nContacts,maxContactCapacity);
  101. // printf("plane-convex\n");
  102. }
  103. if (m_data->m_collidablesCPU[collidableIndexA].m_shapeType == SHAPE_CONVEX_HULL &&
  104. m_data->m_collidablesCPU[collidableIndexB].m_shapeType == SHAPE_CONVEX_HULL)
  105. {
  106. //printf("pairs[i].z=%d\n",pairs[i].z);
  107. //int contactIndex = computeContactConvexConvex2(i,bodyIndexA,bodyIndexB,collidableIndexA,collidableIndexB,bodies,
  108. // m_data->m_collidablesCPU,hostConvexData,hostVertices,hostUniqueEdges,hostIndices,hostFaces,hostContacts,nContacts,maxContactCapacity,oldHostContacts);
  109. int contactIndex = b3ContactConvexConvexSAT(i, bodyIndexA, bodyIndexB, collidableIndexA, collidableIndexB, bodies,
  110. m_data->m_collidablesCPU, m_data->m_convexPolyhedra, m_data->m_convexVertices, m_data->m_uniqueEdges, m_data->m_convexIndices, m_data->m_convexFaces, m_data->m_contacts, numContacts, maxContactCapacity);
  111. if (contactIndex >= 0)
  112. {
  113. pairs[i].z = contactIndex;
  114. }
  115. // printf("plane-convex\n");
  116. }
  117. }
  118. m_data->m_contacts.resize(numContacts);
  119. }
  120. int b3CpuNarrowPhase::registerConvexHullShape(b3ConvexUtility* utilPtr)
  121. {
  122. int collidableIndex = allocateCollidable();
  123. if (collidableIndex < 0)
  124. return collidableIndex;
  125. b3Collidable& col = m_data->m_collidablesCPU[collidableIndex];
  126. col.m_shapeType = SHAPE_CONVEX_HULL;
  127. col.m_shapeIndex = -1;
  128. {
  129. b3Vector3 localCenter = b3MakeVector3(0, 0, 0);
  130. for (int i = 0; i < utilPtr->m_vertices.size(); i++)
  131. localCenter += utilPtr->m_vertices[i];
  132. localCenter *= (1.f / utilPtr->m_vertices.size());
  133. utilPtr->m_localCenter = localCenter;
  134. col.m_shapeIndex = registerConvexHullShapeInternal(utilPtr, col);
  135. }
  136. if (col.m_shapeIndex >= 0)
  137. {
  138. b3Aabb aabb;
  139. b3Vector3 myAabbMin = b3MakeVector3(1e30f, 1e30f, 1e30f);
  140. b3Vector3 myAabbMax = b3MakeVector3(-1e30f, -1e30f, -1e30f);
  141. for (int i = 0; i < utilPtr->m_vertices.size(); i++)
  142. {
  143. myAabbMin.setMin(utilPtr->m_vertices[i]);
  144. myAabbMax.setMax(utilPtr->m_vertices[i]);
  145. }
  146. aabb.m_min[0] = myAabbMin[0];
  147. aabb.m_min[1] = myAabbMin[1];
  148. aabb.m_min[2] = myAabbMin[2];
  149. aabb.m_minIndices[3] = 0;
  150. aabb.m_max[0] = myAabbMax[0];
  151. aabb.m_max[1] = myAabbMax[1];
  152. aabb.m_max[2] = myAabbMax[2];
  153. aabb.m_signedMaxIndices[3] = 0;
  154. m_data->m_localShapeAABBCPU.push_back(aabb);
  155. }
  156. return collidableIndex;
  157. }
  158. int b3CpuNarrowPhase::allocateCollidable()
  159. {
  160. int curSize = m_data->m_collidablesCPU.size();
  161. if (curSize < m_data->m_config.m_maxConvexShapes)
  162. {
  163. m_data->m_collidablesCPU.expand();
  164. return curSize;
  165. }
  166. else
  167. {
  168. b3Error("allocateCollidable out-of-range %d\n", m_data->m_config.m_maxConvexShapes);
  169. }
  170. return -1;
  171. }
  172. int b3CpuNarrowPhase::registerConvexHullShape(const float* vertices, int strideInBytes, int numVertices, const float* scaling)
  173. {
  174. b3AlignedObjectArray<b3Vector3> verts;
  175. unsigned char* vts = (unsigned char*)vertices;
  176. for (int i = 0; i < numVertices; i++)
  177. {
  178. float* vertex = (float*)&vts[i * strideInBytes];
  179. verts.push_back(b3MakeVector3(vertex[0] * scaling[0], vertex[1] * scaling[1], vertex[2] * scaling[2]));
  180. }
  181. b3ConvexUtility* utilPtr = new b3ConvexUtility();
  182. bool merge = true;
  183. if (numVertices)
  184. {
  185. utilPtr->initializePolyhedralFeatures(&verts[0], verts.size(), merge);
  186. }
  187. int collidableIndex = registerConvexHullShape(utilPtr);
  188. delete utilPtr;
  189. return collidableIndex;
  190. }
  191. int b3CpuNarrowPhase::registerConvexHullShapeInternal(b3ConvexUtility* convexPtr, b3Collidable& col)
  192. {
  193. m_data->m_convexData.resize(m_data->m_numAcceleratedShapes + 1);
  194. m_data->m_convexPolyhedra.resize(m_data->m_numAcceleratedShapes + 1);
  195. b3ConvexPolyhedronData& convex = m_data->m_convexPolyhedra.at(m_data->m_convexPolyhedra.size() - 1);
  196. convex.mC = convexPtr->mC;
  197. convex.mE = convexPtr->mE;
  198. convex.m_extents = convexPtr->m_extents;
  199. convex.m_localCenter = convexPtr->m_localCenter;
  200. convex.m_radius = convexPtr->m_radius;
  201. convex.m_numUniqueEdges = convexPtr->m_uniqueEdges.size();
  202. int edgeOffset = m_data->m_uniqueEdges.size();
  203. convex.m_uniqueEdgesOffset = edgeOffset;
  204. m_data->m_uniqueEdges.resize(edgeOffset + convex.m_numUniqueEdges);
  205. //convex data here
  206. int i;
  207. for (i = 0; i < convexPtr->m_uniqueEdges.size(); i++)
  208. {
  209. m_data->m_uniqueEdges[edgeOffset + i] = convexPtr->m_uniqueEdges[i];
  210. }
  211. int faceOffset = m_data->m_convexFaces.size();
  212. convex.m_faceOffset = faceOffset;
  213. convex.m_numFaces = convexPtr->m_faces.size();
  214. m_data->m_convexFaces.resize(faceOffset + convex.m_numFaces);
  215. for (i = 0; i < convexPtr->m_faces.size(); i++)
  216. {
  217. m_data->m_convexFaces[convex.m_faceOffset + i].m_plane = b3MakeVector3(convexPtr->m_faces[i].m_plane[0],
  218. convexPtr->m_faces[i].m_plane[1],
  219. convexPtr->m_faces[i].m_plane[2],
  220. convexPtr->m_faces[i].m_plane[3]);
  221. int indexOffset = m_data->m_convexIndices.size();
  222. int numIndices = convexPtr->m_faces[i].m_indices.size();
  223. m_data->m_convexFaces[convex.m_faceOffset + i].m_numIndices = numIndices;
  224. m_data->m_convexFaces[convex.m_faceOffset + i].m_indexOffset = indexOffset;
  225. m_data->m_convexIndices.resize(indexOffset + numIndices);
  226. for (int p = 0; p < numIndices; p++)
  227. {
  228. m_data->m_convexIndices[indexOffset + p] = convexPtr->m_faces[i].m_indices[p];
  229. }
  230. }
  231. convex.m_numVertices = convexPtr->m_vertices.size();
  232. int vertexOffset = m_data->m_convexVertices.size();
  233. convex.m_vertexOffset = vertexOffset;
  234. m_data->m_convexVertices.resize(vertexOffset + convex.m_numVertices);
  235. for (int i = 0; i < convexPtr->m_vertices.size(); i++)
  236. {
  237. m_data->m_convexVertices[vertexOffset + i] = convexPtr->m_vertices[i];
  238. }
  239. (m_data->m_convexData)[m_data->m_numAcceleratedShapes] = convexPtr;
  240. return m_data->m_numAcceleratedShapes++;
  241. }
  242. const b3Aabb& b3CpuNarrowPhase::getLocalSpaceAabb(int collidableIndex) const
  243. {
  244. return m_data->m_localShapeAABBCPU[collidableIndex];
  245. }