b3OptimizedBvh.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. #include "b3OptimizedBvh.h"
  14. #include "b3StridingMeshInterface.h"
  15. #include "Bullet3Geometry/b3AabbUtil.h"
  16. b3OptimizedBvh::b3OptimizedBvh()
  17. {
  18. }
  19. b3OptimizedBvh::~b3OptimizedBvh()
  20. {
  21. }
  22. void b3OptimizedBvh::build(b3StridingMeshInterface* triangles, bool useQuantizedAabbCompression, const b3Vector3& bvhAabbMin, const b3Vector3& bvhAabbMax)
  23. {
  24. m_useQuantization = useQuantizedAabbCompression;
  25. // NodeArray triangleNodes;
  26. struct NodeTriangleCallback : public b3InternalTriangleIndexCallback
  27. {
  28. NodeArray& m_triangleNodes;
  29. NodeTriangleCallback& operator=(NodeTriangleCallback& other)
  30. {
  31. m_triangleNodes.copyFromArray(other.m_triangleNodes);
  32. return *this;
  33. }
  34. NodeTriangleCallback(NodeArray& triangleNodes)
  35. : m_triangleNodes(triangleNodes)
  36. {
  37. }
  38. virtual void internalProcessTriangleIndex(b3Vector3* triangle, int partId, int triangleIndex)
  39. {
  40. b3OptimizedBvhNode node;
  41. b3Vector3 aabbMin, aabbMax;
  42. aabbMin.setValue(b3Scalar(B3_LARGE_FLOAT), b3Scalar(B3_LARGE_FLOAT), b3Scalar(B3_LARGE_FLOAT));
  43. aabbMax.setValue(b3Scalar(-B3_LARGE_FLOAT), b3Scalar(-B3_LARGE_FLOAT), b3Scalar(-B3_LARGE_FLOAT));
  44. aabbMin.setMin(triangle[0]);
  45. aabbMax.setMax(triangle[0]);
  46. aabbMin.setMin(triangle[1]);
  47. aabbMax.setMax(triangle[1]);
  48. aabbMin.setMin(triangle[2]);
  49. aabbMax.setMax(triangle[2]);
  50. //with quantization?
  51. node.m_aabbMinOrg = aabbMin;
  52. node.m_aabbMaxOrg = aabbMax;
  53. node.m_escapeIndex = -1;
  54. //for child nodes
  55. node.m_subPart = partId;
  56. node.m_triangleIndex = triangleIndex;
  57. m_triangleNodes.push_back(node);
  58. }
  59. };
  60. struct QuantizedNodeTriangleCallback : public b3InternalTriangleIndexCallback
  61. {
  62. QuantizedNodeArray& m_triangleNodes;
  63. const b3QuantizedBvh* m_optimizedTree; // for quantization
  64. QuantizedNodeTriangleCallback& operator=(QuantizedNodeTriangleCallback& other)
  65. {
  66. m_triangleNodes.copyFromArray(other.m_triangleNodes);
  67. m_optimizedTree = other.m_optimizedTree;
  68. return *this;
  69. }
  70. QuantizedNodeTriangleCallback(QuantizedNodeArray& triangleNodes, const b3QuantizedBvh* tree)
  71. : m_triangleNodes(triangleNodes), m_optimizedTree(tree)
  72. {
  73. }
  74. virtual void internalProcessTriangleIndex(b3Vector3* triangle, int partId, int triangleIndex)
  75. {
  76. // The partId and triangle index must fit in the same (positive) integer
  77. b3Assert(partId < (1 << MAX_NUM_PARTS_IN_BITS));
  78. b3Assert(triangleIndex < (1 << (31 - MAX_NUM_PARTS_IN_BITS)));
  79. //negative indices are reserved for escapeIndex
  80. b3Assert(triangleIndex >= 0);
  81. b3QuantizedBvhNode node;
  82. b3Vector3 aabbMin, aabbMax;
  83. aabbMin.setValue(b3Scalar(B3_LARGE_FLOAT), b3Scalar(B3_LARGE_FLOAT), b3Scalar(B3_LARGE_FLOAT));
  84. aabbMax.setValue(b3Scalar(-B3_LARGE_FLOAT), b3Scalar(-B3_LARGE_FLOAT), b3Scalar(-B3_LARGE_FLOAT));
  85. aabbMin.setMin(triangle[0]);
  86. aabbMax.setMax(triangle[0]);
  87. aabbMin.setMin(triangle[1]);
  88. aabbMax.setMax(triangle[1]);
  89. aabbMin.setMin(triangle[2]);
  90. aabbMax.setMax(triangle[2]);
  91. //PCK: add these checks for zero dimensions of aabb
  92. const b3Scalar MIN_AABB_DIMENSION = b3Scalar(0.002);
  93. const b3Scalar MIN_AABB_HALF_DIMENSION = b3Scalar(0.001);
  94. if (aabbMax.getX() - aabbMin.getX() < MIN_AABB_DIMENSION)
  95. {
  96. aabbMax.setX(aabbMax.getX() + MIN_AABB_HALF_DIMENSION);
  97. aabbMin.setX(aabbMin.getX() - MIN_AABB_HALF_DIMENSION);
  98. }
  99. if (aabbMax.getY() - aabbMin.getY() < MIN_AABB_DIMENSION)
  100. {
  101. aabbMax.setY(aabbMax.getY() + MIN_AABB_HALF_DIMENSION);
  102. aabbMin.setY(aabbMin.getY() - MIN_AABB_HALF_DIMENSION);
  103. }
  104. if (aabbMax.getZ() - aabbMin.getZ() < MIN_AABB_DIMENSION)
  105. {
  106. aabbMax.setZ(aabbMax.getZ() + MIN_AABB_HALF_DIMENSION);
  107. aabbMin.setZ(aabbMin.getZ() - MIN_AABB_HALF_DIMENSION);
  108. }
  109. m_optimizedTree->quantize(&node.m_quantizedAabbMin[0], aabbMin, 0);
  110. m_optimizedTree->quantize(&node.m_quantizedAabbMax[0], aabbMax, 1);
  111. node.m_escapeIndexOrTriangleIndex = (partId << (31 - MAX_NUM_PARTS_IN_BITS)) | triangleIndex;
  112. m_triangleNodes.push_back(node);
  113. }
  114. };
  115. int numLeafNodes = 0;
  116. if (m_useQuantization)
  117. {
  118. //initialize quantization values
  119. setQuantizationValues(bvhAabbMin, bvhAabbMax);
  120. QuantizedNodeTriangleCallback callback(m_quantizedLeafNodes, this);
  121. triangles->InternalProcessAllTriangles(&callback, m_bvhAabbMin, m_bvhAabbMax);
  122. //now we have an array of leafnodes in m_leafNodes
  123. numLeafNodes = m_quantizedLeafNodes.size();
  124. m_quantizedContiguousNodes.resize(2 * numLeafNodes);
  125. }
  126. else
  127. {
  128. NodeTriangleCallback callback(m_leafNodes);
  129. b3Vector3 aabbMin = b3MakeVector3(b3Scalar(-B3_LARGE_FLOAT), b3Scalar(-B3_LARGE_FLOAT), b3Scalar(-B3_LARGE_FLOAT));
  130. b3Vector3 aabbMax = b3MakeVector3(b3Scalar(B3_LARGE_FLOAT), b3Scalar(B3_LARGE_FLOAT), b3Scalar(B3_LARGE_FLOAT));
  131. triangles->InternalProcessAllTriangles(&callback, aabbMin, aabbMax);
  132. //now we have an array of leafnodes in m_leafNodes
  133. numLeafNodes = m_leafNodes.size();
  134. m_contiguousNodes.resize(2 * numLeafNodes);
  135. }
  136. m_curNodeIndex = 0;
  137. buildTree(0, numLeafNodes);
  138. ///if the entire tree is small then subtree size, we need to create a header info for the tree
  139. if (m_useQuantization && !m_SubtreeHeaders.size())
  140. {
  141. b3BvhSubtreeInfo& subtree = m_SubtreeHeaders.expand();
  142. subtree.setAabbFromQuantizeNode(m_quantizedContiguousNodes[0]);
  143. subtree.m_rootNodeIndex = 0;
  144. subtree.m_subtreeSize = m_quantizedContiguousNodes[0].isLeafNode() ? 1 : m_quantizedContiguousNodes[0].getEscapeIndex();
  145. }
  146. //PCK: update the copy of the size
  147. m_subtreeHeaderCount = m_SubtreeHeaders.size();
  148. //PCK: clear m_quantizedLeafNodes and m_leafNodes, they are temporary
  149. m_quantizedLeafNodes.clear();
  150. m_leafNodes.clear();
  151. }
  152. void b3OptimizedBvh::refit(b3StridingMeshInterface* meshInterface, const b3Vector3& aabbMin, const b3Vector3& aabbMax)
  153. {
  154. if (m_useQuantization)
  155. {
  156. setQuantizationValues(aabbMin, aabbMax);
  157. updateBvhNodes(meshInterface, 0, m_curNodeIndex, 0);
  158. ///now update all subtree headers
  159. int i;
  160. for (i = 0; i < m_SubtreeHeaders.size(); i++)
  161. {
  162. b3BvhSubtreeInfo& subtree = m_SubtreeHeaders[i];
  163. subtree.setAabbFromQuantizeNode(m_quantizedContiguousNodes[subtree.m_rootNodeIndex]);
  164. }
  165. }
  166. else
  167. {
  168. }
  169. }
  170. void b3OptimizedBvh::refitPartial(b3StridingMeshInterface* meshInterface, const b3Vector3& aabbMin, const b3Vector3& aabbMax)
  171. {
  172. //incrementally initialize quantization values
  173. b3Assert(m_useQuantization);
  174. b3Assert(aabbMin.getX() > m_bvhAabbMin.getX());
  175. b3Assert(aabbMin.getY() > m_bvhAabbMin.getY());
  176. b3Assert(aabbMin.getZ() > m_bvhAabbMin.getZ());
  177. b3Assert(aabbMax.getX() < m_bvhAabbMax.getX());
  178. b3Assert(aabbMax.getY() < m_bvhAabbMax.getY());
  179. b3Assert(aabbMax.getZ() < m_bvhAabbMax.getZ());
  180. ///we should update all quantization values, using updateBvhNodes(meshInterface);
  181. ///but we only update chunks that overlap the given aabb
  182. unsigned short quantizedQueryAabbMin[3];
  183. unsigned short quantizedQueryAabbMax[3];
  184. quantize(&quantizedQueryAabbMin[0], aabbMin, 0);
  185. quantize(&quantizedQueryAabbMax[0], aabbMax, 1);
  186. int i;
  187. for (i = 0; i < this->m_SubtreeHeaders.size(); i++)
  188. {
  189. b3BvhSubtreeInfo& subtree = m_SubtreeHeaders[i];
  190. //PCK: unsigned instead of bool
  191. unsigned overlap = b3TestQuantizedAabbAgainstQuantizedAabb(quantizedQueryAabbMin, quantizedQueryAabbMax, subtree.m_quantizedAabbMin, subtree.m_quantizedAabbMax);
  192. if (overlap != 0)
  193. {
  194. updateBvhNodes(meshInterface, subtree.m_rootNodeIndex, subtree.m_rootNodeIndex + subtree.m_subtreeSize, i);
  195. subtree.setAabbFromQuantizeNode(m_quantizedContiguousNodes[subtree.m_rootNodeIndex]);
  196. }
  197. }
  198. }
  199. void b3OptimizedBvh::updateBvhNodes(b3StridingMeshInterface* meshInterface, int firstNode, int endNode, int index)
  200. {
  201. (void)index;
  202. b3Assert(m_useQuantization);
  203. int curNodeSubPart = -1;
  204. //get access info to trianglemesh data
  205. const unsigned char* vertexbase = 0;
  206. int numverts = 0;
  207. PHY_ScalarType type = PHY_INTEGER;
  208. int stride = 0;
  209. const unsigned char* indexbase = 0;
  210. int indexstride = 0;
  211. int numfaces = 0;
  212. PHY_ScalarType indicestype = PHY_INTEGER;
  213. b3Vector3 triangleVerts[3];
  214. b3Vector3 aabbMin, aabbMax;
  215. const b3Vector3& meshScaling = meshInterface->getScaling();
  216. int i;
  217. for (i = endNode - 1; i >= firstNode; i--)
  218. {
  219. b3QuantizedBvhNode& curNode = m_quantizedContiguousNodes[i];
  220. if (curNode.isLeafNode())
  221. {
  222. //recalc aabb from triangle data
  223. int nodeSubPart = curNode.getPartId();
  224. int nodeTriangleIndex = curNode.getTriangleIndex();
  225. if (nodeSubPart != curNodeSubPart)
  226. {
  227. if (curNodeSubPart >= 0)
  228. meshInterface->unLockReadOnlyVertexBase(curNodeSubPart);
  229. meshInterface->getLockedReadOnlyVertexIndexBase(&vertexbase, numverts, type, stride, &indexbase, indexstride, numfaces, indicestype, nodeSubPart);
  230. curNodeSubPart = nodeSubPart;
  231. }
  232. //triangles->getLockedReadOnlyVertexIndexBase(vertexBase,numVerts,
  233. unsigned int* gfxbase = (unsigned int*)(indexbase + nodeTriangleIndex * indexstride);
  234. for (int j = 2; j >= 0; j--)
  235. {
  236. int graphicsindex;
  237. switch (indicestype) {
  238. case PHY_INTEGER: graphicsindex = gfxbase[j]; break;
  239. case PHY_SHORT: graphicsindex = ((unsigned short*)gfxbase)[j]; break;
  240. case PHY_UCHAR: graphicsindex = ((unsigned char*)gfxbase)[j]; break;
  241. default: b3Assert(0);
  242. }
  243. if (type == PHY_FLOAT)
  244. {
  245. float* graphicsbase = (float*)(vertexbase + graphicsindex * stride);
  246. triangleVerts[j] = b3MakeVector3(
  247. graphicsbase[0] * meshScaling.getX(),
  248. graphicsbase[1] * meshScaling.getY(),
  249. graphicsbase[2] * meshScaling.getZ());
  250. }
  251. else
  252. {
  253. double* graphicsbase = (double*)(vertexbase + graphicsindex * stride);
  254. triangleVerts[j] = b3MakeVector3(b3Scalar(graphicsbase[0] * meshScaling.getX()), b3Scalar(graphicsbase[1] * meshScaling.getY()), b3Scalar(graphicsbase[2] * meshScaling.getZ()));
  255. }
  256. }
  257. aabbMin.setValue(b3Scalar(B3_LARGE_FLOAT), b3Scalar(B3_LARGE_FLOAT), b3Scalar(B3_LARGE_FLOAT));
  258. aabbMax.setValue(b3Scalar(-B3_LARGE_FLOAT), b3Scalar(-B3_LARGE_FLOAT), b3Scalar(-B3_LARGE_FLOAT));
  259. aabbMin.setMin(triangleVerts[0]);
  260. aabbMax.setMax(triangleVerts[0]);
  261. aabbMin.setMin(triangleVerts[1]);
  262. aabbMax.setMax(triangleVerts[1]);
  263. aabbMin.setMin(triangleVerts[2]);
  264. aabbMax.setMax(triangleVerts[2]);
  265. quantize(&curNode.m_quantizedAabbMin[0], aabbMin, 0);
  266. quantize(&curNode.m_quantizedAabbMax[0], aabbMax, 1);
  267. }
  268. else
  269. {
  270. //combine aabb from both children
  271. b3QuantizedBvhNode* leftChildNode = &m_quantizedContiguousNodes[i + 1];
  272. b3QuantizedBvhNode* rightChildNode = leftChildNode->isLeafNode() ? &m_quantizedContiguousNodes[i + 2] : &m_quantizedContiguousNodes[i + 1 + leftChildNode->getEscapeIndex()];
  273. {
  274. for (int i = 0; i < 3; i++)
  275. {
  276. curNode.m_quantizedAabbMin[i] = leftChildNode->m_quantizedAabbMin[i];
  277. if (curNode.m_quantizedAabbMin[i] > rightChildNode->m_quantizedAabbMin[i])
  278. curNode.m_quantizedAabbMin[i] = rightChildNode->m_quantizedAabbMin[i];
  279. curNode.m_quantizedAabbMax[i] = leftChildNode->m_quantizedAabbMax[i];
  280. if (curNode.m_quantizedAabbMax[i] < rightChildNode->m_quantizedAabbMax[i])
  281. curNode.m_quantizedAabbMax[i] = rightChildNode->m_quantizedAabbMax[i];
  282. }
  283. }
  284. }
  285. }
  286. if (curNodeSubPart >= 0)
  287. meshInterface->unLockReadOnlyVertexBase(curNodeSubPart);
  288. }
  289. ///deSerializeInPlace loads and initializes a BVH from a buffer in memory 'in place'
  290. b3OptimizedBvh* b3OptimizedBvh::deSerializeInPlace(void* i_alignedDataBuffer, unsigned int i_dataBufferSize, bool i_swapEndian)
  291. {
  292. b3QuantizedBvh* bvh = b3QuantizedBvh::deSerializeInPlace(i_alignedDataBuffer, i_dataBufferSize, i_swapEndian);
  293. //we don't add additional data so just do a static upcast
  294. return static_cast<b3OptimizedBvh*>(bvh);
  295. }