btBvhTriangleMeshShape.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. //#define DISABLE_BVH
  14. #include "bullet/BulletCollision//CollisionShapes/btBvhTriangleMeshShape.h"
  15. #include "bullet/BulletCollision//CollisionShapes/btOptimizedBvh.h"
  16. #include "bullet/LinearMath/btSerializer.h"
  17. ///Bvh Concave triangle mesh is a static-triangle mesh shape with Bounding Volume Hierarchy optimization.
  18. ///Uses an interface to access the triangles to allow for sharing graphics/physics triangles.
  19. btBvhTriangleMeshShape::btBvhTriangleMeshShape(btStridingMeshInterface* meshInterface, bool useQuantizedAabbCompression, bool buildBvh)
  20. :btTriangleMeshShape(meshInterface),
  21. m_bvh(0),
  22. m_triangleInfoMap(0),
  23. m_useQuantizedAabbCompression(useQuantizedAabbCompression),
  24. m_ownsBvh(false)
  25. {
  26. m_shapeType = TRIANGLE_MESH_SHAPE_PROXYTYPE;
  27. //construct bvh from meshInterface
  28. #ifndef DISABLE_BVH
  29. if (buildBvh)
  30. {
  31. buildOptimizedBvh();
  32. }
  33. #endif //DISABLE_BVH
  34. }
  35. btBvhTriangleMeshShape::btBvhTriangleMeshShape(btStridingMeshInterface* meshInterface, bool useQuantizedAabbCompression,const btVector3& bvhAabbMin,const btVector3& bvhAabbMax,bool buildBvh)
  36. :btTriangleMeshShape(meshInterface),
  37. m_bvh(0),
  38. m_triangleInfoMap(0),
  39. m_useQuantizedAabbCompression(useQuantizedAabbCompression),
  40. m_ownsBvh(false)
  41. {
  42. m_shapeType = TRIANGLE_MESH_SHAPE_PROXYTYPE;
  43. //construct bvh from meshInterface
  44. #ifndef DISABLE_BVH
  45. if (buildBvh)
  46. {
  47. void* mem = btAlignedAlloc(sizeof(btOptimizedBvh),16);
  48. m_bvh = new (mem) btOptimizedBvh();
  49. m_bvh->build(meshInterface,m_useQuantizedAabbCompression,bvhAabbMin,bvhAabbMax);
  50. m_ownsBvh = true;
  51. }
  52. #endif //DISABLE_BVH
  53. }
  54. void btBvhTriangleMeshShape::partialRefitTree(const btVector3& aabbMin,const btVector3& aabbMax)
  55. {
  56. m_bvh->refitPartial( m_meshInterface,aabbMin,aabbMax );
  57. m_localAabbMin.setMin(aabbMin);
  58. m_localAabbMax.setMax(aabbMax);
  59. }
  60. void btBvhTriangleMeshShape::refitTree(const btVector3& aabbMin,const btVector3& aabbMax)
  61. {
  62. m_bvh->refit( m_meshInterface, aabbMin,aabbMax );
  63. recalcLocalAabb();
  64. }
  65. btBvhTriangleMeshShape::~btBvhTriangleMeshShape()
  66. {
  67. if (m_ownsBvh)
  68. {
  69. m_bvh->~btOptimizedBvh();
  70. btAlignedFree(m_bvh);
  71. }
  72. }
  73. void btBvhTriangleMeshShape::performRaycast (btTriangleCallback* callback, const btVector3& raySource, const btVector3& rayTarget)
  74. {
  75. struct MyNodeOverlapCallback : public btNodeOverlapCallback
  76. {
  77. btStridingMeshInterface* m_meshInterface;
  78. btTriangleCallback* m_callback;
  79. MyNodeOverlapCallback(btTriangleCallback* callback,btStridingMeshInterface* meshInterface)
  80. :m_meshInterface(meshInterface),
  81. m_callback(callback)
  82. {
  83. }
  84. virtual void processNode(int nodeSubPart, int nodeTriangleIndex)
  85. {
  86. btVector3 m_triangle[3];
  87. const unsigned char *vertexbase;
  88. int numverts;
  89. PHY_ScalarType type;
  90. int stride;
  91. const unsigned char *indexbase;
  92. int indexstride;
  93. int numfaces;
  94. PHY_ScalarType indicestype;
  95. m_meshInterface->getLockedReadOnlyVertexIndexBase(
  96. &vertexbase,
  97. numverts,
  98. type,
  99. stride,
  100. &indexbase,
  101. indexstride,
  102. numfaces,
  103. indicestype,
  104. nodeSubPart);
  105. unsigned int* gfxbase = (unsigned int*)(indexbase+nodeTriangleIndex*indexstride);
  106. btAssert(indicestype==PHY_INTEGER||indicestype==PHY_SHORT);
  107. const btVector3& meshScaling = m_meshInterface->getScaling();
  108. for (int j=2;j>=0;j--)
  109. {
  110. int graphicsindex = indicestype==PHY_SHORT?((unsigned short*)gfxbase)[j]:gfxbase[j];
  111. if (type == PHY_FLOAT)
  112. {
  113. float* graphicsbase = (float*)(vertexbase+graphicsindex*stride);
  114. m_triangle[j] = btVector3(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(),graphicsbase[2]*meshScaling.getZ());
  115. }
  116. else
  117. {
  118. double* graphicsbase = (double*)(vertexbase+graphicsindex*stride);
  119. m_triangle[j] = btVector3(btScalar(graphicsbase[0])*meshScaling.getX(),btScalar(graphicsbase[1])*meshScaling.getY(),btScalar(graphicsbase[2])*meshScaling.getZ());
  120. }
  121. }
  122. /* Perform ray vs. triangle collision here */
  123. m_callback->processTriangle(m_triangle,nodeSubPart,nodeTriangleIndex);
  124. m_meshInterface->unLockReadOnlyVertexBase(nodeSubPart);
  125. }
  126. };
  127. MyNodeOverlapCallback myNodeCallback(callback,m_meshInterface);
  128. m_bvh->reportRayOverlappingNodex(&myNodeCallback,raySource,rayTarget);
  129. }
  130. void btBvhTriangleMeshShape::performConvexcast (btTriangleCallback* callback, const btVector3& raySource, const btVector3& rayTarget, const btVector3& aabbMin, const btVector3& aabbMax)
  131. {
  132. struct MyNodeOverlapCallback : public btNodeOverlapCallback
  133. {
  134. btStridingMeshInterface* m_meshInterface;
  135. btTriangleCallback* m_callback;
  136. MyNodeOverlapCallback(btTriangleCallback* callback,btStridingMeshInterface* meshInterface)
  137. :m_meshInterface(meshInterface),
  138. m_callback(callback)
  139. {
  140. }
  141. virtual void processNode(int nodeSubPart, int nodeTriangleIndex)
  142. {
  143. btVector3 m_triangle[3];
  144. const unsigned char *vertexbase;
  145. int numverts;
  146. PHY_ScalarType type;
  147. int stride;
  148. const unsigned char *indexbase;
  149. int indexstride;
  150. int numfaces;
  151. PHY_ScalarType indicestype;
  152. m_meshInterface->getLockedReadOnlyVertexIndexBase(
  153. &vertexbase,
  154. numverts,
  155. type,
  156. stride,
  157. &indexbase,
  158. indexstride,
  159. numfaces,
  160. indicestype,
  161. nodeSubPart);
  162. unsigned int* gfxbase = (unsigned int*)(indexbase+nodeTriangleIndex*indexstride);
  163. btAssert(indicestype==PHY_INTEGER||indicestype==PHY_SHORT);
  164. const btVector3& meshScaling = m_meshInterface->getScaling();
  165. for (int j=2;j>=0;j--)
  166. {
  167. int graphicsindex = indicestype==PHY_SHORT?((unsigned short*)gfxbase)[j]:gfxbase[j];
  168. if (type == PHY_FLOAT)
  169. {
  170. float* graphicsbase = (float*)(vertexbase+graphicsindex*stride);
  171. m_triangle[j] = btVector3(graphicsbase[0]*meshScaling.getX(),graphicsbase[1]*meshScaling.getY(),graphicsbase[2]*meshScaling.getZ());
  172. }
  173. else
  174. {
  175. double* graphicsbase = (double*)(vertexbase+graphicsindex*stride);
  176. m_triangle[j] = btVector3(btScalar(graphicsbase[0])*meshScaling.getX(),btScalar(graphicsbase[1])*meshScaling.getY(),btScalar(graphicsbase[2])*meshScaling.getZ());
  177. }
  178. }
  179. /* Perform ray vs. triangle collision here */
  180. m_callback->processTriangle(m_triangle,nodeSubPart,nodeTriangleIndex);
  181. m_meshInterface->unLockReadOnlyVertexBase(nodeSubPart);
  182. }
  183. };
  184. MyNodeOverlapCallback myNodeCallback(callback,m_meshInterface);
  185. m_bvh->reportBoxCastOverlappingNodex (&myNodeCallback, raySource, rayTarget, aabbMin, aabbMax);
  186. }
  187. //perform bvh tree traversal and report overlapping triangles to 'callback'
  188. void btBvhTriangleMeshShape::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
  189. {
  190. #ifdef DISABLE_BVH
  191. //brute force traverse all triangles
  192. btTriangleMeshShape::processAllTriangles(callback,aabbMin,aabbMax);
  193. #else
  194. //first get all the nodes
  195. struct MyNodeOverlapCallback : public btNodeOverlapCallback
  196. {
  197. btStridingMeshInterface* m_meshInterface;
  198. btTriangleCallback* m_callback;
  199. btVector3 m_triangle[3];
  200. MyNodeOverlapCallback(btTriangleCallback* callback,btStridingMeshInterface* meshInterface)
  201. :m_meshInterface(meshInterface),
  202. m_callback(callback)
  203. {
  204. }
  205. virtual void processNode(int nodeSubPart, int nodeTriangleIndex)
  206. {
  207. const unsigned char *vertexbase;
  208. int numverts;
  209. PHY_ScalarType type;
  210. int stride;
  211. const unsigned char *indexbase;
  212. int indexstride;
  213. int numfaces;
  214. PHY_ScalarType indicestype;
  215. m_meshInterface->getLockedReadOnlyVertexIndexBase(
  216. &vertexbase,
  217. numverts,
  218. type,
  219. stride,
  220. &indexbase,
  221. indexstride,
  222. numfaces,
  223. indicestype,
  224. nodeSubPart);
  225. unsigned int* gfxbase = (unsigned int*)(indexbase+nodeTriangleIndex*indexstride);
  226. btAssert(indicestype==PHY_INTEGER||indicestype==PHY_SHORT||indicestype==PHY_UCHAR);
  227. const btVector3& meshScaling = m_meshInterface->getScaling();
  228. for (int j=2;j>=0;j--)
  229. {
  230. int graphicsindex = indicestype==PHY_SHORT?((unsigned short*)gfxbase)[j]:indicestype==PHY_INTEGER?gfxbase[j]:((unsigned char*)gfxbase)[j];
  231. #ifdef DEBUG_TRIANGLE_MESH
  232. printf("%d ,",graphicsindex);
  233. #endif //DEBUG_TRIANGLE_MESH
  234. if (type == PHY_FLOAT)
  235. {
  236. float* graphicsbase = (float*)(vertexbase+graphicsindex*stride);
  237. m_triangle[j] = btVector3(
  238. graphicsbase[0]*meshScaling.getX(),
  239. graphicsbase[1]*meshScaling.getY(),
  240. graphicsbase[2]*meshScaling.getZ());
  241. }
  242. else
  243. {
  244. double* graphicsbase = (double*)(vertexbase+graphicsindex*stride);
  245. m_triangle[j] = btVector3(
  246. btScalar(graphicsbase[0])*meshScaling.getX(),
  247. btScalar(graphicsbase[1])*meshScaling.getY(),
  248. btScalar(graphicsbase[2])*meshScaling.getZ());
  249. }
  250. #ifdef DEBUG_TRIANGLE_MESH
  251. printf("triangle vertices:%f,%f,%f\n",triangle[j].x(),triangle[j].y(),triangle[j].z());
  252. #endif //DEBUG_TRIANGLE_MESH
  253. }
  254. m_callback->processTriangle(m_triangle,nodeSubPart,nodeTriangleIndex);
  255. m_meshInterface->unLockReadOnlyVertexBase(nodeSubPart);
  256. }
  257. };
  258. MyNodeOverlapCallback myNodeCallback(callback,m_meshInterface);
  259. m_bvh->reportAabbOverlappingNodex(&myNodeCallback,aabbMin,aabbMax);
  260. #endif//DISABLE_BVH
  261. }
  262. void btBvhTriangleMeshShape::setLocalScaling(const btVector3& scaling)
  263. {
  264. if ((getLocalScaling() -scaling).length2() > SIMD_EPSILON)
  265. {
  266. btTriangleMeshShape::setLocalScaling(scaling);
  267. buildOptimizedBvh();
  268. }
  269. }
  270. void btBvhTriangleMeshShape::buildOptimizedBvh()
  271. {
  272. if (m_ownsBvh)
  273. {
  274. m_bvh->~btOptimizedBvh();
  275. btAlignedFree(m_bvh);
  276. }
  277. ///m_localAabbMin/m_localAabbMax is already re-calculated in btTriangleMeshShape. We could just scale aabb, but this needs some more work
  278. void* mem = btAlignedAlloc(sizeof(btOptimizedBvh),16);
  279. m_bvh = new(mem) btOptimizedBvh();
  280. //rebuild the bvh...
  281. m_bvh->build(m_meshInterface,m_useQuantizedAabbCompression,m_localAabbMin,m_localAabbMax);
  282. m_ownsBvh = true;
  283. }
  284. void btBvhTriangleMeshShape::setOptimizedBvh(btOptimizedBvh* bvh, const btVector3& scaling)
  285. {
  286. btAssert(!m_bvh);
  287. btAssert(!m_ownsBvh);
  288. m_bvh = bvh;
  289. m_ownsBvh = false;
  290. // update the scaling without rebuilding the bvh
  291. if ((getLocalScaling() -scaling).length2() > SIMD_EPSILON)
  292. {
  293. btTriangleMeshShape::setLocalScaling(scaling);
  294. }
  295. }
  296. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  297. const char* btBvhTriangleMeshShape::serialize(void* dataBuffer, btSerializer* serializer) const
  298. {
  299. btTriangleMeshShapeData* trimeshData = (btTriangleMeshShapeData*) dataBuffer;
  300. btCollisionShape::serialize(&trimeshData->m_collisionShapeData,serializer);
  301. m_meshInterface->serialize(&trimeshData->m_meshInterface, serializer);
  302. trimeshData->m_collisionMargin = float(m_collisionMargin);
  303. if (m_bvh && !(serializer->getSerializationFlags()&BT_SERIALIZE_NO_BVH))
  304. {
  305. void* chunk = serializer->findPointer(m_bvh);
  306. if (chunk)
  307. {
  308. #ifdef BT_USE_DOUBLE_PRECISION
  309. trimeshData->m_quantizedDoubleBvh = (btQuantizedBvhData*)chunk;
  310. trimeshData->m_quantizedFloatBvh = 0;
  311. #else
  312. trimeshData->m_quantizedFloatBvh = (btQuantizedBvhData*)chunk;
  313. trimeshData->m_quantizedDoubleBvh= 0;
  314. #endif //BT_USE_DOUBLE_PRECISION
  315. } else
  316. {
  317. #ifdef BT_USE_DOUBLE_PRECISION
  318. trimeshData->m_quantizedDoubleBvh = (btQuantizedBvhData*)serializer->getUniquePointer(m_bvh);
  319. trimeshData->m_quantizedFloatBvh = 0;
  320. #else
  321. trimeshData->m_quantizedFloatBvh = (btQuantizedBvhData*)serializer->getUniquePointer(m_bvh);
  322. trimeshData->m_quantizedDoubleBvh= 0;
  323. #endif //BT_USE_DOUBLE_PRECISION
  324. int sz = m_bvh->calculateSerializeBufferSizeNew();
  325. btChunk* chunk = serializer->allocate(sz,1);
  326. const char* structType = m_bvh->serialize(chunk->m_oldPtr, serializer);
  327. serializer->finalizeChunk(chunk,structType,BT_QUANTIZED_BVH_CODE,m_bvh);
  328. }
  329. } else
  330. {
  331. trimeshData->m_quantizedFloatBvh = 0;
  332. trimeshData->m_quantizedDoubleBvh = 0;
  333. }
  334. if (m_triangleInfoMap && !(serializer->getSerializationFlags()&BT_SERIALIZE_NO_TRIANGLEINFOMAP))
  335. {
  336. void* chunk = serializer->findPointer(m_triangleInfoMap);
  337. if (chunk)
  338. {
  339. trimeshData->m_triangleInfoMap = (btTriangleInfoMapData*)chunk;
  340. } else
  341. {
  342. trimeshData->m_triangleInfoMap = (btTriangleInfoMapData*)serializer->getUniquePointer(m_triangleInfoMap);
  343. int sz = m_triangleInfoMap->calculateSerializeBufferSize();
  344. btChunk* chunk = serializer->allocate(sz,1);
  345. const char* structType = m_triangleInfoMap->serialize(chunk->m_oldPtr, serializer);
  346. serializer->finalizeChunk(chunk,structType,BT_TRIANLGE_INFO_MAP,m_triangleInfoMap);
  347. }
  348. } else
  349. {
  350. trimeshData->m_triangleInfoMap = 0;
  351. }
  352. return "btTriangleMeshShapeData";
  353. }
  354. void btBvhTriangleMeshShape::serializeSingleBvh(btSerializer* serializer) const
  355. {
  356. if (m_bvh)
  357. {
  358. int len = m_bvh->calculateSerializeBufferSizeNew(); //make sure not to use calculateSerializeBufferSize because it is used for in-place
  359. btChunk* chunk = serializer->allocate(len,1);
  360. const char* structType = m_bvh->serialize(chunk->m_oldPtr, serializer);
  361. serializer->finalizeChunk(chunk,structType,BT_QUANTIZED_BVH_CODE,(void*)m_bvh);
  362. }
  363. }
  364. void btBvhTriangleMeshShape::serializeSingleTriangleInfoMap(btSerializer* serializer) const
  365. {
  366. if (m_triangleInfoMap)
  367. {
  368. int len = m_triangleInfoMap->calculateSerializeBufferSize();
  369. btChunk* chunk = serializer->allocate(len,1);
  370. const char* structType = m_triangleInfoMap->serialize(chunk->m_oldPtr, serializer);
  371. serializer->finalizeChunk(chunk,structType,BT_TRIANLGE_INFO_MAP,(void*)m_triangleInfoMap);
  372. }
  373. }