You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1376 regels
49 KiB

  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  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 "btQuantizedBvh.h"
  14. #include "LinearMath/btAabbUtil2.h"
  15. #include "LinearMath/btIDebugDraw.h"
  16. #include "LinearMath/btSerializer.h"
  17. #define RAYAABB2
  18. btQuantizedBvh::btQuantizedBvh() :
  19. m_bulletVersion(BT_BULLET_VERSION),
  20. m_useQuantization(false),
  21. //m_traversalMode(TRAVERSAL_STACKLESS_CACHE_FRIENDLY)
  22. m_traversalMode(TRAVERSAL_STACKLESS)
  23. //m_traversalMode(TRAVERSAL_RECURSIVE)
  24. ,m_subtreeHeaderCount(0) //PCK: add this line
  25. {
  26. m_bvhAabbMin.setValue(-SIMD_INFINITY,-SIMD_INFINITY,-SIMD_INFINITY);
  27. m_bvhAabbMax.setValue(SIMD_INFINITY,SIMD_INFINITY,SIMD_INFINITY);
  28. }
  29. void btQuantizedBvh::buildInternal()
  30. {
  31. ///assumes that caller filled in the m_quantizedLeafNodes
  32. m_useQuantization = true;
  33. int numLeafNodes = 0;
  34. if (m_useQuantization)
  35. {
  36. //now we have an array of leafnodes in m_leafNodes
  37. numLeafNodes = m_quantizedLeafNodes.size();
  38. m_quantizedContiguousNodes.resize(2*numLeafNodes);
  39. }
  40. m_curNodeIndex = 0;
  41. buildTree(0,numLeafNodes);
  42. ///if the entire tree is small then subtree size, we need to create a header info for the tree
  43. if(m_useQuantization && !m_SubtreeHeaders.size())
  44. {
  45. btBvhSubtreeInfo& subtree = m_SubtreeHeaders.expand();
  46. subtree.setAabbFromQuantizeNode(m_quantizedContiguousNodes[0]);
  47. subtree.m_rootNodeIndex = 0;
  48. subtree.m_subtreeSize = m_quantizedContiguousNodes[0].isLeafNode() ? 1 : m_quantizedContiguousNodes[0].getEscapeIndex();
  49. }
  50. //PCK: update the copy of the size
  51. m_subtreeHeaderCount = m_SubtreeHeaders.size();
  52. //PCK: clear m_quantizedLeafNodes and m_leafNodes, they are temporary
  53. m_quantizedLeafNodes.clear();
  54. m_leafNodes.clear();
  55. }
  56. ///just for debugging, to visualize the individual patches/subtrees
  57. #ifdef DEBUG_PATCH_COLORS
  58. btVector3 color[4]=
  59. {
  60. btVector3(1,0,0),
  61. btVector3(0,1,0),
  62. btVector3(0,0,1),
  63. btVector3(0,1,1)
  64. };
  65. #endif //DEBUG_PATCH_COLORS
  66. void btQuantizedBvh::setQuantizationValues(const btVector3& bvhAabbMin,const btVector3& bvhAabbMax,btScalar quantizationMargin)
  67. {
  68. //enlarge the AABB to avoid division by zero when initializing the quantization values
  69. btVector3 clampValue(quantizationMargin,quantizationMargin,quantizationMargin);
  70. m_bvhAabbMin = bvhAabbMin - clampValue;
  71. m_bvhAabbMax = bvhAabbMax + clampValue;
  72. btVector3 aabbSize = m_bvhAabbMax - m_bvhAabbMin;
  73. m_bvhQuantization = btVector3(btScalar(65533.0),btScalar(65533.0),btScalar(65533.0)) / aabbSize;
  74. m_useQuantization = true;
  75. }
  76. btQuantizedBvh::~btQuantizedBvh()
  77. {
  78. }
  79. #ifdef DEBUG_TREE_BUILDING
  80. int gStackDepth = 0;
  81. int gMaxStackDepth = 0;
  82. #endif //DEBUG_TREE_BUILDING
  83. void btQuantizedBvh::buildTree (int startIndex,int endIndex)
  84. {
  85. #ifdef DEBUG_TREE_BUILDING
  86. gStackDepth++;
  87. if (gStackDepth > gMaxStackDepth)
  88. gMaxStackDepth = gStackDepth;
  89. #endif //DEBUG_TREE_BUILDING
  90. int splitAxis, splitIndex, i;
  91. int numIndices =endIndex-startIndex;
  92. int curIndex = m_curNodeIndex;
  93. btAssert(numIndices>0);
  94. if (numIndices==1)
  95. {
  96. #ifdef DEBUG_TREE_BUILDING
  97. gStackDepth--;
  98. #endif //DEBUG_TREE_BUILDING
  99. assignInternalNodeFromLeafNode(m_curNodeIndex,startIndex);
  100. m_curNodeIndex++;
  101. return;
  102. }
  103. //calculate Best Splitting Axis and where to split it. Sort the incoming 'leafNodes' array within range 'startIndex/endIndex'.
  104. splitAxis = calcSplittingAxis(startIndex,endIndex);
  105. splitIndex = sortAndCalcSplittingIndex(startIndex,endIndex,splitAxis);
  106. int internalNodeIndex = m_curNodeIndex;
  107. //set the min aabb to 'inf' or a max value, and set the max aabb to a -inf/minimum value.
  108. //the aabb will be expanded during buildTree/mergeInternalNodeAabb with actual node values
  109. setInternalNodeAabbMin(m_curNodeIndex,m_bvhAabbMax);//can't use btVector3(SIMD_INFINITY,SIMD_INFINITY,SIMD_INFINITY)) because of quantization
  110. setInternalNodeAabbMax(m_curNodeIndex,m_bvhAabbMin);//can't use btVector3(-SIMD_INFINITY,-SIMD_INFINITY,-SIMD_INFINITY)) because of quantization
  111. for (i=startIndex;i<endIndex;i++)
  112. {
  113. mergeInternalNodeAabb(m_curNodeIndex,getAabbMin(i),getAabbMax(i));
  114. }
  115. m_curNodeIndex++;
  116. //internalNode->m_escapeIndex;
  117. int leftChildNodexIndex = m_curNodeIndex;
  118. //build left child tree
  119. buildTree(startIndex,splitIndex);
  120. int rightChildNodexIndex = m_curNodeIndex;
  121. //build right child tree
  122. buildTree(splitIndex,endIndex);
  123. #ifdef DEBUG_TREE_BUILDING
  124. gStackDepth--;
  125. #endif //DEBUG_TREE_BUILDING
  126. int escapeIndex = m_curNodeIndex - curIndex;
  127. if (m_useQuantization)
  128. {
  129. //escapeIndex is the number of nodes of this subtree
  130. const int sizeQuantizedNode =sizeof(btQuantizedBvhNode);
  131. const int treeSizeInBytes = escapeIndex * sizeQuantizedNode;
  132. if (treeSizeInBytes > MAX_SUBTREE_SIZE_IN_BYTES)
  133. {
  134. updateSubtreeHeaders(leftChildNodexIndex,rightChildNodexIndex);
  135. }
  136. } else
  137. {
  138. }
  139. setInternalNodeEscapeIndex(internalNodeIndex,escapeIndex);
  140. }
  141. void btQuantizedBvh::updateSubtreeHeaders(int leftChildNodexIndex,int rightChildNodexIndex)
  142. {
  143. btAssert(m_useQuantization);
  144. btQuantizedBvhNode& leftChildNode = m_quantizedContiguousNodes[leftChildNodexIndex];
  145. int leftSubTreeSize = leftChildNode.isLeafNode() ? 1 : leftChildNode.getEscapeIndex();
  146. int leftSubTreeSizeInBytes = leftSubTreeSize * static_cast<int>(sizeof(btQuantizedBvhNode));
  147. btQuantizedBvhNode& rightChildNode = m_quantizedContiguousNodes[rightChildNodexIndex];
  148. int rightSubTreeSize = rightChildNode.isLeafNode() ? 1 : rightChildNode.getEscapeIndex();
  149. int rightSubTreeSizeInBytes = rightSubTreeSize * static_cast<int>(sizeof(btQuantizedBvhNode));
  150. if(leftSubTreeSizeInBytes <= MAX_SUBTREE_SIZE_IN_BYTES)
  151. {
  152. btBvhSubtreeInfo& subtree = m_SubtreeHeaders.expand();
  153. subtree.setAabbFromQuantizeNode(leftChildNode);
  154. subtree.m_rootNodeIndex = leftChildNodexIndex;
  155. subtree.m_subtreeSize = leftSubTreeSize;
  156. }
  157. if(rightSubTreeSizeInBytes <= MAX_SUBTREE_SIZE_IN_BYTES)
  158. {
  159. btBvhSubtreeInfo& subtree = m_SubtreeHeaders.expand();
  160. subtree.setAabbFromQuantizeNode(rightChildNode);
  161. subtree.m_rootNodeIndex = rightChildNodexIndex;
  162. subtree.m_subtreeSize = rightSubTreeSize;
  163. }
  164. //PCK: update the copy of the size
  165. m_subtreeHeaderCount = m_SubtreeHeaders.size();
  166. }
  167. int btQuantizedBvh::sortAndCalcSplittingIndex(int startIndex,int endIndex,int splitAxis)
  168. {
  169. int i;
  170. int splitIndex =startIndex;
  171. int numIndices = endIndex - startIndex;
  172. btScalar splitValue;
  173. btVector3 means(btScalar(0.),btScalar(0.),btScalar(0.));
  174. for (i=startIndex;i<endIndex;i++)
  175. {
  176. btVector3 center = btScalar(0.5)*(getAabbMax(i)+getAabbMin(i));
  177. means+=center;
  178. }
  179. means *= (btScalar(1.)/(btScalar)numIndices);
  180. splitValue = means[splitAxis];
  181. //sort leafNodes so all values larger then splitValue comes first, and smaller values start from 'splitIndex'.
  182. for (i=startIndex;i<endIndex;i++)
  183. {
  184. btVector3 center = btScalar(0.5)*(getAabbMax(i)+getAabbMin(i));
  185. if (center[splitAxis] > splitValue)
  186. {
  187. //swap
  188. swapLeafNodes(i,splitIndex);
  189. splitIndex++;
  190. }
  191. }
  192. //if the splitIndex causes unbalanced trees, fix this by using the center in between startIndex and endIndex
  193. //otherwise the tree-building might fail due to stack-overflows in certain cases.
  194. //unbalanced1 is unsafe: it can cause stack overflows
  195. //bool unbalanced1 = ((splitIndex==startIndex) || (splitIndex == (endIndex-1)));
  196. //unbalanced2 should work too: always use center (perfect balanced trees)
  197. //bool unbalanced2 = true;
  198. //this should be safe too:
  199. int rangeBalancedIndices = numIndices/3;
  200. bool unbalanced = ((splitIndex<=(startIndex+rangeBalancedIndices)) || (splitIndex >=(endIndex-1-rangeBalancedIndices)));
  201. if (unbalanced)
  202. {
  203. splitIndex = startIndex+ (numIndices>>1);
  204. }
  205. bool unbal = (splitIndex==startIndex) || (splitIndex == (endIndex));
  206. (void)unbal;
  207. btAssert(!unbal);
  208. return splitIndex;
  209. }
  210. int btQuantizedBvh::calcSplittingAxis(int startIndex,int endIndex)
  211. {
  212. int i;
  213. btVector3 means(btScalar(0.),btScalar(0.),btScalar(0.));
  214. btVector3 variance(btScalar(0.),btScalar(0.),btScalar(0.));
  215. int numIndices = endIndex-startIndex;
  216. for (i=startIndex;i<endIndex;i++)
  217. {
  218. btVector3 center = btScalar(0.5)*(getAabbMax(i)+getAabbMin(i));
  219. means+=center;
  220. }
  221. means *= (btScalar(1.)/(btScalar)numIndices);
  222. for (i=startIndex;i<endIndex;i++)
  223. {
  224. btVector3 center = btScalar(0.5)*(getAabbMax(i)+getAabbMin(i));
  225. btVector3 diff2 = center-means;
  226. diff2 = diff2 * diff2;
  227. variance += diff2;
  228. }
  229. variance *= (btScalar(1.)/ ((btScalar)numIndices-1) );
  230. return variance.maxAxis();
  231. }
  232. void btQuantizedBvh::reportAabbOverlappingNodex(btNodeOverlapCallback* nodeCallback,const btVector3& aabbMin,const btVector3& aabbMax) const
  233. {
  234. //either choose recursive traversal (walkTree) or stackless (walkStacklessTree)
  235. if (m_useQuantization)
  236. {
  237. ///quantize query AABB
  238. unsigned short int quantizedQueryAabbMin[3];
  239. unsigned short int quantizedQueryAabbMax[3];
  240. quantizeWithClamp(quantizedQueryAabbMin,aabbMin,0);
  241. quantizeWithClamp(quantizedQueryAabbMax,aabbMax,1);
  242. switch (m_traversalMode)
  243. {
  244. case TRAVERSAL_STACKLESS:
  245. walkStacklessQuantizedTree(nodeCallback,quantizedQueryAabbMin,quantizedQueryAabbMax,0,m_curNodeIndex);
  246. break;
  247. case TRAVERSAL_STACKLESS_CACHE_FRIENDLY:
  248. walkStacklessQuantizedTreeCacheFriendly(nodeCallback,quantizedQueryAabbMin,quantizedQueryAabbMax);
  249. break;
  250. case TRAVERSAL_RECURSIVE:
  251. {
  252. const btQuantizedBvhNode* rootNode = &m_quantizedContiguousNodes[0];
  253. walkRecursiveQuantizedTreeAgainstQueryAabb(rootNode,nodeCallback,quantizedQueryAabbMin,quantizedQueryAabbMax);
  254. }
  255. break;
  256. default:
  257. //unsupported
  258. btAssert(0);
  259. }
  260. } else
  261. {
  262. walkStacklessTree(nodeCallback,aabbMin,aabbMax);
  263. }
  264. }
  265. int maxIterations = 0;
  266. void btQuantizedBvh::walkStacklessTree(btNodeOverlapCallback* nodeCallback,const btVector3& aabbMin,const btVector3& aabbMax) const
  267. {
  268. btAssert(!m_useQuantization);
  269. const btOptimizedBvhNode* rootNode = &m_contiguousNodes[0];
  270. int escapeIndex, curIndex = 0;
  271. int walkIterations = 0;
  272. bool isLeafNode;
  273. //PCK: unsigned instead of bool
  274. unsigned aabbOverlap;
  275. while (curIndex < m_curNodeIndex)
  276. {
  277. //catch bugs in tree data
  278. btAssert (walkIterations < m_curNodeIndex);
  279. walkIterations++;
  280. aabbOverlap = TestAabbAgainstAabb2(aabbMin,aabbMax,rootNode->m_aabbMinOrg,rootNode->m_aabbMaxOrg);
  281. isLeafNode = rootNode->m_escapeIndex == -1;
  282. //PCK: unsigned instead of bool
  283. if (isLeafNode && (aabbOverlap != 0))
  284. {
  285. nodeCallback->processNode(rootNode->m_subPart,rootNode->m_triangleIndex);
  286. }
  287. //PCK: unsigned instead of bool
  288. if ((aabbOverlap != 0) || isLeafNode)
  289. {
  290. rootNode++;
  291. curIndex++;
  292. } else
  293. {
  294. escapeIndex = rootNode->m_escapeIndex;
  295. rootNode += escapeIndex;
  296. curIndex += escapeIndex;
  297. }
  298. }
  299. if (maxIterations < walkIterations)
  300. maxIterations = walkIterations;
  301. }
  302. /*
  303. ///this was the original recursive traversal, before we optimized towards stackless traversal
  304. void btQuantizedBvh::walkTree(btOptimizedBvhNode* rootNode,btNodeOverlapCallback* nodeCallback,const btVector3& aabbMin,const btVector3& aabbMax) const
  305. {
  306. bool isLeafNode, aabbOverlap = TestAabbAgainstAabb2(aabbMin,aabbMax,rootNode->m_aabbMin,rootNode->m_aabbMax);
  307. if (aabbOverlap)
  308. {
  309. isLeafNode = (!rootNode->m_leftChild && !rootNode->m_rightChild);
  310. if (isLeafNode)
  311. {
  312. nodeCallback->processNode(rootNode);
  313. } else
  314. {
  315. walkTree(rootNode->m_leftChild,nodeCallback,aabbMin,aabbMax);
  316. walkTree(rootNode->m_rightChild,nodeCallback,aabbMin,aabbMax);
  317. }
  318. }
  319. }
  320. */
  321. void btQuantizedBvh::walkRecursiveQuantizedTreeAgainstQueryAabb(const btQuantizedBvhNode* currentNode,btNodeOverlapCallback* nodeCallback,unsigned short int* quantizedQueryAabbMin,unsigned short int* quantizedQueryAabbMax) const
  322. {
  323. btAssert(m_useQuantization);
  324. bool isLeafNode;
  325. //PCK: unsigned instead of bool
  326. unsigned aabbOverlap;
  327. //PCK: unsigned instead of bool
  328. aabbOverlap = testQuantizedAabbAgainstQuantizedAabb(quantizedQueryAabbMin,quantizedQueryAabbMax,currentNode->m_quantizedAabbMin,currentNode->m_quantizedAabbMax);
  329. isLeafNode = currentNode->isLeafNode();
  330. //PCK: unsigned instead of bool
  331. if (aabbOverlap != 0)
  332. {
  333. if (isLeafNode)
  334. {
  335. nodeCallback->processNode(currentNode->getPartId(),currentNode->getTriangleIndex());
  336. } else
  337. {
  338. //process left and right children
  339. const btQuantizedBvhNode* leftChildNode = currentNode+1;
  340. walkRecursiveQuantizedTreeAgainstQueryAabb(leftChildNode,nodeCallback,quantizedQueryAabbMin,quantizedQueryAabbMax);
  341. const btQuantizedBvhNode* rightChildNode = leftChildNode->isLeafNode() ? leftChildNode+1:leftChildNode+leftChildNode->getEscapeIndex();
  342. walkRecursiveQuantizedTreeAgainstQueryAabb(rightChildNode,nodeCallback,quantizedQueryAabbMin,quantizedQueryAabbMax);
  343. }
  344. }
  345. }
  346. void btQuantizedBvh::walkStacklessTreeAgainstRay(btNodeOverlapCallback* nodeCallback, const btVector3& raySource, const btVector3& rayTarget, const btVector3& aabbMin, const btVector3& aabbMax, int startNodeIndex,int endNodeIndex) const
  347. {
  348. btAssert(!m_useQuantization);
  349. const btOptimizedBvhNode* rootNode = &m_contiguousNodes[0];
  350. int escapeIndex, curIndex = 0;
  351. int walkIterations = 0;
  352. bool isLeafNode;
  353. //PCK: unsigned instead of bool
  354. unsigned aabbOverlap=0;
  355. unsigned rayBoxOverlap=0;
  356. btScalar lambda_max = 1.0;
  357. /* Quick pruning by quantized box */
  358. btVector3 rayAabbMin = raySource;
  359. btVector3 rayAabbMax = raySource;
  360. rayAabbMin.setMin(rayTarget);
  361. rayAabbMax.setMax(rayTarget);
  362. /* Add box cast extents to bounding box */
  363. rayAabbMin += aabbMin;
  364. rayAabbMax += aabbMax;
  365. #ifdef RAYAABB2
  366. btVector3 rayDir = (rayTarget-raySource);
  367. rayDir.normalize ();
  368. lambda_max = rayDir.dot(rayTarget-raySource);
  369. ///what about division by zero? --> just set rayDirection[i] to 1.0
  370. btVector3 rayDirectionInverse;
  371. rayDirectionInverse[0] = rayDir[0] == btScalar(0.0) ? btScalar(BT_LARGE_FLOAT) : btScalar(1.0) / rayDir[0];
  372. rayDirectionInverse[1] = rayDir[1] == btScalar(0.0) ? btScalar(BT_LARGE_FLOAT) : btScalar(1.0) / rayDir[1];
  373. rayDirectionInverse[2] = rayDir[2] == btScalar(0.0) ? btScalar(BT_LARGE_FLOAT) : btScalar(1.0) / rayDir[2];
  374. unsigned int sign[3] = { rayDirectionInverse[0] < 0.0, rayDirectionInverse[1] < 0.0, rayDirectionInverse[2] < 0.0};
  375. #endif
  376. btVector3 bounds[2];
  377. while (curIndex < m_curNodeIndex)
  378. {
  379. btScalar param = 1.0;
  380. //catch bugs in tree data
  381. btAssert (walkIterations < m_curNodeIndex);
  382. walkIterations++;
  383. bounds[0] = rootNode->m_aabbMinOrg;
  384. bounds[1] = rootNode->m_aabbMaxOrg;
  385. /* Add box cast extents */
  386. bounds[0] -= aabbMax;
  387. bounds[1] -= aabbMin;
  388. aabbOverlap = TestAabbAgainstAabb2(rayAabbMin,rayAabbMax,rootNode->m_aabbMinOrg,rootNode->m_aabbMaxOrg);
  389. //perhaps profile if it is worth doing the aabbOverlap test first
  390. #ifdef RAYAABB2
  391. ///careful with this check: need to check division by zero (above) and fix the unQuantize method
  392. ///thanks Joerg/hiker for the reproduction case!
  393. ///http://www.bulletphysics.com/Bullet/phpBB3/viewtopic.php?f=9&t=1858
  394. rayBoxOverlap = aabbOverlap ? btRayAabb2 (raySource, rayDirectionInverse, sign, bounds, param, 0.0f, lambda_max) : false;
  395. #else
  396. btVector3 normal;
  397. rayBoxOverlap = btRayAabb(raySource, rayTarget,bounds[0],bounds[1],param, normal);
  398. #endif
  399. isLeafNode = rootNode->m_escapeIndex == -1;
  400. //PCK: unsigned instead of bool
  401. if (isLeafNode && (rayBoxOverlap != 0))
  402. {
  403. nodeCallback->processNode(rootNode->m_subPart,rootNode->m_triangleIndex);
  404. }
  405. //PCK: unsigned instead of bool
  406. if ((rayBoxOverlap != 0) || isLeafNode)
  407. {
  408. rootNode++;
  409. curIndex++;
  410. } else
  411. {
  412. escapeIndex = rootNode->m_escapeIndex;
  413. rootNode += escapeIndex;
  414. curIndex += escapeIndex;
  415. }
  416. }
  417. if (maxIterations < walkIterations)
  418. maxIterations = walkIterations;
  419. }
  420. void btQuantizedBvh::walkStacklessQuantizedTreeAgainstRay(btNodeOverlapCallback* nodeCallback, const btVector3& raySource, const btVector3& rayTarget, const btVector3& aabbMin, const btVector3& aabbMax, int startNodeIndex,int endNodeIndex) const
  421. {
  422. btAssert(m_useQuantization);
  423. int curIndex = startNodeIndex;
  424. int walkIterations = 0;
  425. int subTreeSize = endNodeIndex - startNodeIndex;
  426. (void)subTreeSize;
  427. const btQuantizedBvhNode* rootNode = &m_quantizedContiguousNodes[startNodeIndex];
  428. int escapeIndex;
  429. bool isLeafNode;
  430. //PCK: unsigned instead of bool
  431. unsigned boxBoxOverlap = 0;
  432. unsigned rayBoxOverlap = 0;
  433. btScalar lambda_max = 1.0;
  434. #ifdef RAYAABB2
  435. btVector3 rayDirection = (rayTarget-raySource);
  436. rayDirection.normalize ();
  437. lambda_max = rayDirection.dot(rayTarget-raySource);
  438. ///what about division by zero? --> just set rayDirection[i] to 1.0
  439. rayDirection[0] = rayDirection[0] == btScalar(0.0) ? btScalar(BT_LARGE_FLOAT) : btScalar(1.0) / rayDirection[0];
  440. rayDirection[1] = rayDirection[1] == btScalar(0.0) ? btScalar(BT_LARGE_FLOAT) : btScalar(1.0) / rayDirection[1];
  441. rayDirection[2] = rayDirection[2] == btScalar(0.0) ? btScalar(BT_LARGE_FLOAT) : btScalar(1.0) / rayDirection[2];
  442. unsigned int sign[3] = { rayDirection[0] < 0.0, rayDirection[1] < 0.0, rayDirection[2] < 0.0};
  443. #endif
  444. /* Quick pruning by quantized box */
  445. btVector3 rayAabbMin = raySource;
  446. btVector3 rayAabbMax = raySource;
  447. rayAabbMin.setMin(rayTarget);
  448. rayAabbMax.setMax(rayTarget);
  449. /* Add box cast extents to bounding box */
  450. rayAabbMin += aabbMin;
  451. rayAabbMax += aabbMax;
  452. unsigned short int quantizedQueryAabbMin[3];
  453. unsigned short int quantizedQueryAabbMax[3];
  454. quantizeWithClamp(quantizedQueryAabbMin,rayAabbMin,0);
  455. quantizeWithClamp(quantizedQueryAabbMax,rayAabbMax,1);
  456. while (curIndex < endNodeIndex)
  457. {
  458. //#define VISUALLY_ANALYZE_BVH 1
  459. #ifdef VISUALLY_ANALYZE_BVH
  460. //some code snippet to debugDraw aabb, to visually analyze bvh structure
  461. static int drawPatch = 0;
  462. //need some global access to a debugDrawer
  463. extern btIDebugDraw* debugDrawerPtr;
  464. if (curIndex==drawPatch)
  465. {
  466. btVector3 aabbMin,aabbMax;
  467. aabbMin = unQuantize(rootNode->m_quantizedAabbMin);
  468. aabbMax = unQuantize(rootNode->m_quantizedAabbMax);
  469. btVector3 color(1,0,0);
  470. debugDrawerPtr->drawAabb(aabbMin,aabbMax,color);
  471. }
  472. #endif//VISUALLY_ANALYZE_BVH
  473. //catch bugs in tree data
  474. btAssert (walkIterations < subTreeSize);
  475. walkIterations++;
  476. //PCK: unsigned instead of bool
  477. // only interested if this is closer than any previous hit
  478. btScalar param = 1.0;
  479. rayBoxOverlap = 0;
  480. boxBoxOverlap = testQuantizedAabbAgainstQuantizedAabb(quantizedQueryAabbMin,quantizedQueryAabbMax,rootNode->m_quantizedAabbMin,rootNode->m_quantizedAabbMax);
  481. isLeafNode = rootNode->isLeafNode();
  482. if (boxBoxOverlap)
  483. {
  484. btVector3 bounds[2];
  485. bounds[0] = unQuantize(rootNode->m_quantizedAabbMin);
  486. bounds[1] = unQuantize(rootNode->m_quantizedAabbMax);
  487. /* Add box cast extents */
  488. bounds[0] -= aabbMax;
  489. bounds[1] -= aabbMin;
  490. btVector3 normal;
  491. #if 0
  492. bool ra2 = btRayAabb2 (raySource, rayDirection, sign, bounds, param, 0.0, lambda_max);
  493. bool ra = btRayAabb (raySource, rayTarget, bounds[0], bounds[1], param, normal);
  494. if (ra2 != ra)
  495. {
  496. printf("functions don't match\n");
  497. }
  498. #endif
  499. #ifdef RAYAABB2
  500. ///careful with this check: need to check division by zero (above) and fix the unQuantize method
  501. ///thanks Joerg/hiker for the reproduction case!
  502. ///http://www.bulletphysics.com/Bullet/phpBB3/viewtopic.php?f=9&t=1858
  503. //BT_PROFILE("btRayAabb2");
  504. rayBoxOverlap = btRayAabb2 (raySource, rayDirection, sign, bounds, param, 0.0f, lambda_max);
  505. #else
  506. rayBoxOverlap = true;//btRayAabb(raySource, rayTarget, bounds[0], bounds[1], param, normal);
  507. #endif
  508. }
  509. if (isLeafNode && rayBoxOverlap)
  510. {
  511. nodeCallback->processNode(rootNode->getPartId(),rootNode->getTriangleIndex());
  512. }
  513. //PCK: unsigned instead of bool
  514. if ((rayBoxOverlap != 0) || isLeafNode)
  515. {
  516. rootNode++;
  517. curIndex++;
  518. } else
  519. {
  520. escapeIndex = rootNode->getEscapeIndex();
  521. rootNode += escapeIndex;
  522. curIndex += escapeIndex;
  523. }
  524. }
  525. if (maxIterations < walkIterations)
  526. maxIterations = walkIterations;
  527. }
  528. void btQuantizedBvh::walkStacklessQuantizedTree(btNodeOverlapCallback* nodeCallback,unsigned short int* quantizedQueryAabbMin,unsigned short int* quantizedQueryAabbMax,int startNodeIndex,int endNodeIndex) const
  529. {
  530. btAssert(m_useQuantization);
  531. int curIndex = startNodeIndex;
  532. int walkIterations = 0;
  533. int subTreeSize = endNodeIndex - startNodeIndex;
  534. (void)subTreeSize;
  535. const btQuantizedBvhNode* rootNode = &m_quantizedContiguousNodes[startNodeIndex];
  536. int escapeIndex;
  537. bool isLeafNode;
  538. //PCK: unsigned instead of bool
  539. unsigned aabbOverlap;
  540. while (curIndex < endNodeIndex)
  541. {
  542. //#define VISUALLY_ANALYZE_BVH 1
  543. #ifdef VISUALLY_ANALYZE_BVH
  544. //some code snippet to debugDraw aabb, to visually analyze bvh structure
  545. static int drawPatch = 0;
  546. //need some global access to a debugDrawer
  547. extern btIDebugDraw* debugDrawerPtr;
  548. if (curIndex==drawPatch)
  549. {
  550. btVector3 aabbMin,aabbMax;
  551. aabbMin = unQuantize(rootNode->m_quantizedAabbMin);
  552. aabbMax = unQuantize(rootNode->m_quantizedAabbMax);
  553. btVector3 color(1,0,0);
  554. debugDrawerPtr->drawAabb(aabbMin,aabbMax,color);
  555. }
  556. #endif//VISUALLY_ANALYZE_BVH
  557. //catch bugs in tree data
  558. btAssert (walkIterations < subTreeSize);
  559. walkIterations++;
  560. //PCK: unsigned instead of bool
  561. aabbOverlap = testQuantizedAabbAgainstQuantizedAabb(quantizedQueryAabbMin,quantizedQueryAabbMax,rootNode->m_quantizedAabbMin,rootNode->m_quantizedAabbMax);
  562. isLeafNode = rootNode->isLeafNode();
  563. if (isLeafNode && aabbOverlap)
  564. {
  565. nodeCallback->processNode(rootNode->getPartId(),rootNode->getTriangleIndex());
  566. }
  567. //PCK: unsigned instead of bool
  568. if ((aabbOverlap != 0) || isLeafNode)
  569. {
  570. rootNode++;
  571. curIndex++;
  572. } else
  573. {
  574. escapeIndex = rootNode->getEscapeIndex();
  575. rootNode += escapeIndex;
  576. curIndex += escapeIndex;
  577. }
  578. }
  579. if (maxIterations < walkIterations)
  580. maxIterations = walkIterations;
  581. }
  582. //This traversal can be called from Playstation 3 SPU
  583. void btQuantizedBvh::walkStacklessQuantizedTreeCacheFriendly(btNodeOverlapCallback* nodeCallback,unsigned short int* quantizedQueryAabbMin,unsigned short int* quantizedQueryAabbMax) const
  584. {
  585. btAssert(m_useQuantization);
  586. int i;
  587. for (i=0;i<this->m_SubtreeHeaders.size();i++)
  588. {
  589. const btBvhSubtreeInfo& subtree = m_SubtreeHeaders[i];
  590. //PCK: unsigned instead of bool
  591. unsigned overlap = testQuantizedAabbAgainstQuantizedAabb(quantizedQueryAabbMin,quantizedQueryAabbMax,subtree.m_quantizedAabbMin,subtree.m_quantizedAabbMax);
  592. if (overlap != 0)
  593. {
  594. walkStacklessQuantizedTree(nodeCallback,quantizedQueryAabbMin,quantizedQueryAabbMax,
  595. subtree.m_rootNodeIndex,
  596. subtree.m_rootNodeIndex+subtree.m_subtreeSize);
  597. }
  598. }
  599. }
  600. void btQuantizedBvh::reportRayOverlappingNodex (btNodeOverlapCallback* nodeCallback, const btVector3& raySource, const btVector3& rayTarget) const
  601. {
  602. reportBoxCastOverlappingNodex(nodeCallback,raySource,rayTarget,btVector3(0,0,0),btVector3(0,0,0));
  603. }
  604. void btQuantizedBvh::reportBoxCastOverlappingNodex(btNodeOverlapCallback* nodeCallback, const btVector3& raySource, const btVector3& rayTarget, const btVector3& aabbMin,const btVector3& aabbMax) const
  605. {
  606. //always use stackless
  607. if (m_useQuantization)
  608. {
  609. walkStacklessQuantizedTreeAgainstRay(nodeCallback, raySource, rayTarget, aabbMin, aabbMax, 0, m_curNodeIndex);
  610. }
  611. else
  612. {
  613. walkStacklessTreeAgainstRay(nodeCallback, raySource, rayTarget, aabbMin, aabbMax, 0, m_curNodeIndex);
  614. }
  615. /*
  616. {
  617. //recursive traversal
  618. btVector3 qaabbMin = raySource;
  619. btVector3 qaabbMax = raySource;
  620. qaabbMin.setMin(rayTarget);
  621. qaabbMax.setMax(rayTarget);
  622. qaabbMin += aabbMin;
  623. qaabbMax += aabbMax;
  624. reportAabbOverlappingNodex(nodeCallback,qaabbMin,qaabbMax);
  625. }
  626. */
  627. }
  628. void btQuantizedBvh::swapLeafNodes(int i,int splitIndex)
  629. {
  630. if (m_useQuantization)
  631. {
  632. btQuantizedBvhNode tmp = m_quantizedLeafNodes[i];
  633. m_quantizedLeafNodes[i] = m_quantizedLeafNodes[splitIndex];
  634. m_quantizedLeafNodes[splitIndex] = tmp;
  635. } else
  636. {
  637. btOptimizedBvhNode tmp = m_leafNodes[i];
  638. m_leafNodes[i] = m_leafNodes[splitIndex];
  639. m_leafNodes[splitIndex] = tmp;
  640. }
  641. }
  642. void btQuantizedBvh::assignInternalNodeFromLeafNode(int internalNode,int leafNodeIndex)
  643. {
  644. if (m_useQuantization)
  645. {
  646. m_quantizedContiguousNodes[internalNode] = m_quantizedLeafNodes[leafNodeIndex];
  647. } else
  648. {
  649. m_contiguousNodes[internalNode] = m_leafNodes[leafNodeIndex];
  650. }
  651. }
  652. //PCK: include
  653. #include <new>
  654. #if 0
  655. //PCK: consts
  656. static const unsigned BVH_ALIGNMENT = 16;
  657. static const unsigned BVH_ALIGNMENT_MASK = BVH_ALIGNMENT-1;
  658. static const unsigned BVH_ALIGNMENT_BLOCKS = 2;
  659. #endif
  660. unsigned int btQuantizedBvh::getAlignmentSerializationPadding()
  661. {
  662. // I changed this to 0 since the extra padding is not needed or used.
  663. return 0;//BVH_ALIGNMENT_BLOCKS * BVH_ALIGNMENT;
  664. }
  665. unsigned btQuantizedBvh::calculateSerializeBufferSize() const
  666. {
  667. unsigned baseSize = sizeof(btQuantizedBvh) + getAlignmentSerializationPadding();
  668. baseSize += sizeof(btBvhSubtreeInfo) * m_subtreeHeaderCount;
  669. if (m_useQuantization)
  670. {
  671. return baseSize + m_curNodeIndex * sizeof(btQuantizedBvhNode);
  672. }
  673. return baseSize + m_curNodeIndex * sizeof(btOptimizedBvhNode);
  674. }
  675. bool btQuantizedBvh::serialize(void *o_alignedDataBuffer, unsigned /*i_dataBufferSize */, bool i_swapEndian) const
  676. {
  677. btAssert(m_subtreeHeaderCount == m_SubtreeHeaders.size());
  678. m_subtreeHeaderCount = m_SubtreeHeaders.size();
  679. /* if (i_dataBufferSize < calculateSerializeBufferSize() || o_alignedDataBuffer == NULL || (((unsigned)o_alignedDataBuffer & BVH_ALIGNMENT_MASK) != 0))
  680. {
  681. ///check alignedment for buffer?
  682. btAssert(0);
  683. return false;
  684. }
  685. */
  686. btQuantizedBvh *targetBvh = (btQuantizedBvh *)o_alignedDataBuffer;
  687. // construct the class so the virtual function table, etc will be set up
  688. // Also, m_leafNodes and m_quantizedLeafNodes will be initialized to default values by the constructor
  689. new (targetBvh) btQuantizedBvh;
  690. if (i_swapEndian)
  691. {
  692. targetBvh->m_curNodeIndex = static_cast<int>(btSwapEndian(m_curNodeIndex));
  693. btSwapVector3Endian(m_bvhAabbMin,targetBvh->m_bvhAabbMin);
  694. btSwapVector3Endian(m_bvhAabbMax,targetBvh->m_bvhAabbMax);
  695. btSwapVector3Endian(m_bvhQuantization,targetBvh->m_bvhQuantization);
  696. targetBvh->m_traversalMode = (btTraversalMode)btSwapEndian(m_traversalMode);
  697. targetBvh->m_subtreeHeaderCount = static_cast<int>(btSwapEndian(m_subtreeHeaderCount));
  698. }
  699. else
  700. {
  701. targetBvh->m_curNodeIndex = m_curNodeIndex;
  702. targetBvh->m_bvhAabbMin = m_bvhAabbMin;
  703. targetBvh->m_bvhAabbMax = m_bvhAabbMax;
  704. targetBvh->m_bvhQuantization = m_bvhQuantization;
  705. targetBvh->m_traversalMode = m_traversalMode;
  706. targetBvh->m_subtreeHeaderCount = m_subtreeHeaderCount;
  707. }
  708. targetBvh->m_useQuantization = m_useQuantization;
  709. unsigned char *nodeData = (unsigned char *)targetBvh;
  710. nodeData += sizeof(btQuantizedBvh);
  711. unsigned sizeToAdd = 0;//(BVH_ALIGNMENT-((unsigned)nodeData & BVH_ALIGNMENT_MASK))&BVH_ALIGNMENT_MASK;
  712. nodeData += sizeToAdd;
  713. int nodeCount = m_curNodeIndex;
  714. if (m_useQuantization)
  715. {
  716. targetBvh->m_quantizedContiguousNodes.initializeFromBuffer(nodeData, nodeCount, nodeCount);
  717. if (i_swapEndian)
  718. {
  719. for (int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++)
  720. {
  721. targetBvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[0] = btSwapEndian(m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[0]);
  722. targetBvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[1] = btSwapEndian(m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[1]);
  723. targetBvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[2] = btSwapEndian(m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[2]);
  724. targetBvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[0] = btSwapEndian(m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[0]);
  725. targetBvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[1] = btSwapEndian(m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[1]);
  726. targetBvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[2] = btSwapEndian(m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[2]);
  727. targetBvh->m_quantizedContiguousNodes[nodeIndex].m_escapeIndexOrTriangleIndex = static_cast<int>(btSwapEndian(m_quantizedContiguousNodes[nodeIndex].m_escapeIndexOrTriangleIndex));
  728. }
  729. }
  730. else
  731. {
  732. for (int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++)
  733. {
  734. targetBvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[0] = m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[0];
  735. targetBvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[1] = m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[1];
  736. targetBvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[2] = m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[2];
  737. targetBvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[0] = m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[0];
  738. targetBvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[1] = m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[1];
  739. targetBvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[2] = m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[2];
  740. targetBvh->m_quantizedContiguousNodes[nodeIndex].m_escapeIndexOrTriangleIndex = m_quantizedContiguousNodes[nodeIndex].m_escapeIndexOrTriangleIndex;
  741. }
  742. }
  743. nodeData += sizeof(btQuantizedBvhNode) * nodeCount;
  744. // this clears the pointer in the member variable it doesn't really do anything to the data
  745. // it does call the destructor on the contained objects, but they are all classes with no destructor defined
  746. // so the memory (which is not freed) is left alone
  747. targetBvh->m_quantizedContiguousNodes.initializeFromBuffer(NULL, 0, 0);
  748. }
  749. else
  750. {
  751. targetBvh->m_contiguousNodes.initializeFromBuffer(nodeData, nodeCount, nodeCount);
  752. if (i_swapEndian)
  753. {
  754. for (int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++)
  755. {
  756. btSwapVector3Endian(m_contiguousNodes[nodeIndex].m_aabbMinOrg, targetBvh->m_contiguousNodes[nodeIndex].m_aabbMinOrg);
  757. btSwapVector3Endian(m_contiguousNodes[nodeIndex].m_aabbMaxOrg, targetBvh->m_contiguousNodes[nodeIndex].m_aabbMaxOrg);
  758. targetBvh->m_contiguousNodes[nodeIndex].m_escapeIndex = static_cast<int>(btSwapEndian(m_contiguousNodes[nodeIndex].m_escapeIndex));
  759. targetBvh->m_contiguousNodes[nodeIndex].m_subPart = static_cast<int>(btSwapEndian(m_contiguousNodes[nodeIndex].m_subPart));
  760. targetBvh->m_contiguousNodes[nodeIndex].m_triangleIndex = static_cast<int>(btSwapEndian(m_contiguousNodes[nodeIndex].m_triangleIndex));
  761. }
  762. }
  763. else
  764. {
  765. for (int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++)
  766. {
  767. targetBvh->m_contiguousNodes[nodeIndex].m_aabbMinOrg = m_contiguousNodes[nodeIndex].m_aabbMinOrg;
  768. targetBvh->m_contiguousNodes[nodeIndex].m_aabbMaxOrg = m_contiguousNodes[nodeIndex].m_aabbMaxOrg;
  769. targetBvh->m_contiguousNodes[nodeIndex].m_escapeIndex = m_contiguousNodes[nodeIndex].m_escapeIndex;
  770. targetBvh->m_contiguousNodes[nodeIndex].m_subPart = m_contiguousNodes[nodeIndex].m_subPart;
  771. targetBvh->m_contiguousNodes[nodeIndex].m_triangleIndex = m_contiguousNodes[nodeIndex].m_triangleIndex;
  772. }
  773. }
  774. nodeData += sizeof(btOptimizedBvhNode) * nodeCount;
  775. // this clears the pointer in the member variable it doesn't really do anything to the data
  776. // it does call the destructor on the contained objects, but they are all classes with no destructor defined
  777. // so the memory (which is not freed) is left alone
  778. targetBvh->m_contiguousNodes.initializeFromBuffer(NULL, 0, 0);
  779. }
  780. sizeToAdd = 0;//(BVH_ALIGNMENT-((unsigned)nodeData & BVH_ALIGNMENT_MASK))&BVH_ALIGNMENT_MASK;
  781. nodeData += sizeToAdd;
  782. // Now serialize the subtree headers
  783. targetBvh->m_SubtreeHeaders.initializeFromBuffer(nodeData, m_subtreeHeaderCount, m_subtreeHeaderCount);
  784. if (i_swapEndian)
  785. {
  786. for (int i = 0; i < m_subtreeHeaderCount; i++)
  787. {
  788. targetBvh->m_SubtreeHeaders[i].m_quantizedAabbMin[0] = btSwapEndian(m_SubtreeHeaders[i].m_quantizedAabbMin[0]);
  789. targetBvh->m_SubtreeHeaders[i].m_quantizedAabbMin[1] = btSwapEndian(m_SubtreeHeaders[i].m_quantizedAabbMin[1]);
  790. targetBvh->m_SubtreeHeaders[i].m_quantizedAabbMin[2] = btSwapEndian(m_SubtreeHeaders[i].m_quantizedAabbMin[2]);
  791. targetBvh->m_SubtreeHeaders[i].m_quantizedAabbMax[0] = btSwapEndian(m_SubtreeHeaders[i].m_quantizedAabbMax[0]);
  792. targetBvh->m_SubtreeHeaders[i].m_quantizedAabbMax[1] = btSwapEndian(m_SubtreeHeaders[i].m_quantizedAabbMax[1]);
  793. targetBvh->m_SubtreeHeaders[i].m_quantizedAabbMax[2] = btSwapEndian(m_SubtreeHeaders[i].m_quantizedAabbMax[2]);
  794. targetBvh->m_SubtreeHeaders[i].m_rootNodeIndex = static_cast<int>(btSwapEndian(m_SubtreeHeaders[i].m_rootNodeIndex));
  795. targetBvh->m_SubtreeHeaders[i].m_subtreeSize = static_cast<int>(btSwapEndian(m_SubtreeHeaders[i].m_subtreeSize));
  796. }
  797. }
  798. else
  799. {
  800. for (int i = 0; i < m_subtreeHeaderCount; i++)
  801. {
  802. targetBvh->m_SubtreeHeaders[i].m_quantizedAabbMin[0] = (m_SubtreeHeaders[i].m_quantizedAabbMin[0]);
  803. targetBvh->m_SubtreeHeaders[i].m_quantizedAabbMin[1] = (m_SubtreeHeaders[i].m_quantizedAabbMin[1]);
  804. targetBvh->m_SubtreeHeaders[i].m_quantizedAabbMin[2] = (m_SubtreeHeaders[i].m_quantizedAabbMin[2]);
  805. targetBvh->m_SubtreeHeaders[i].m_quantizedAabbMax[0] = (m_SubtreeHeaders[i].m_quantizedAabbMax[0]);
  806. targetBvh->m_SubtreeHeaders[i].m_quantizedAabbMax[1] = (m_SubtreeHeaders[i].m_quantizedAabbMax[1]);
  807. targetBvh->m_SubtreeHeaders[i].m_quantizedAabbMax[2] = (m_SubtreeHeaders[i].m_quantizedAabbMax[2]);
  808. targetBvh->m_SubtreeHeaders[i].m_rootNodeIndex = (m_SubtreeHeaders[i].m_rootNodeIndex);
  809. targetBvh->m_SubtreeHeaders[i].m_subtreeSize = (m_SubtreeHeaders[i].m_subtreeSize);
  810. // need to clear padding in destination buffer
  811. targetBvh->m_SubtreeHeaders[i].m_padding[0] = 0;
  812. targetBvh->m_SubtreeHeaders[i].m_padding[1] = 0;
  813. targetBvh->m_SubtreeHeaders[i].m_padding[2] = 0;
  814. }
  815. }
  816. nodeData += sizeof(btBvhSubtreeInfo) * m_subtreeHeaderCount;
  817. // this clears the pointer in the member variable it doesn't really do anything to the data
  818. // it does call the destructor on the contained objects, but they are all classes with no destructor defined
  819. // so the memory (which is not freed) is left alone
  820. targetBvh->m_SubtreeHeaders.initializeFromBuffer(NULL, 0, 0);
  821. // this wipes the virtual function table pointer at the start of the buffer for the class
  822. *((void**)o_alignedDataBuffer) = NULL;
  823. return true;
  824. }
  825. btQuantizedBvh *btQuantizedBvh::deSerializeInPlace(void *i_alignedDataBuffer, unsigned int i_dataBufferSize, bool i_swapEndian)
  826. {
  827. if (i_alignedDataBuffer == NULL)// || (((unsigned)i_alignedDataBuffer & BVH_ALIGNMENT_MASK) != 0))
  828. {
  829. return NULL;
  830. }
  831. btQuantizedBvh *bvh = (btQuantizedBvh *)i_alignedDataBuffer;
  832. if (i_swapEndian)
  833. {
  834. bvh->m_curNodeIndex = static_cast<int>(btSwapEndian(bvh->m_curNodeIndex));
  835. btUnSwapVector3Endian(bvh->m_bvhAabbMin);
  836. btUnSwapVector3Endian(bvh->m_bvhAabbMax);
  837. btUnSwapVector3Endian(bvh->m_bvhQuantization);
  838. bvh->m_traversalMode = (btTraversalMode)btSwapEndian(bvh->m_traversalMode);
  839. bvh->m_subtreeHeaderCount = static_cast<int>(btSwapEndian(bvh->m_subtreeHeaderCount));
  840. }
  841. unsigned int calculatedBufSize = bvh->calculateSerializeBufferSize();
  842. btAssert(calculatedBufSize <= i_dataBufferSize);
  843. if (calculatedBufSize > i_dataBufferSize)
  844. {
  845. return NULL;
  846. }
  847. unsigned char *nodeData = (unsigned char *)bvh;
  848. nodeData += sizeof(btQuantizedBvh);
  849. unsigned sizeToAdd = 0;//(BVH_ALIGNMENT-((unsigned)nodeData & BVH_ALIGNMENT_MASK))&BVH_ALIGNMENT_MASK;
  850. nodeData += sizeToAdd;
  851. int nodeCount = bvh->m_curNodeIndex;
  852. // Must call placement new to fill in virtual function table, etc, but we don't want to overwrite most data, so call a special version of the constructor
  853. // Also, m_leafNodes and m_quantizedLeafNodes will be initialized to default values by the constructor
  854. new (bvh) btQuantizedBvh(*bvh, false);
  855. if (bvh->m_useQuantization)
  856. {
  857. bvh->m_quantizedContiguousNodes.initializeFromBuffer(nodeData, nodeCount, nodeCount);
  858. if (i_swapEndian)
  859. {
  860. for (int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++)
  861. {
  862. bvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[0] = btSwapEndian(bvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[0]);
  863. bvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[1] = btSwapEndian(bvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[1]);
  864. bvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[2] = btSwapEndian(bvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[2]);
  865. bvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[0] = btSwapEndian(bvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[0]);
  866. bvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[1] = btSwapEndian(bvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[1]);
  867. bvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[2] = btSwapEndian(bvh->m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[2]);
  868. bvh->m_quantizedContiguousNodes[nodeIndex].m_escapeIndexOrTriangleIndex = static_cast<int>(btSwapEndian(bvh->m_quantizedContiguousNodes[nodeIndex].m_escapeIndexOrTriangleIndex));
  869. }
  870. }
  871. nodeData += sizeof(btQuantizedBvhNode) * nodeCount;
  872. }
  873. else
  874. {
  875. bvh->m_contiguousNodes.initializeFromBuffer(nodeData, nodeCount, nodeCount);
  876. if (i_swapEndian)
  877. {
  878. for (int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++)
  879. {
  880. btUnSwapVector3Endian(bvh->m_contiguousNodes[nodeIndex].m_aabbMinOrg);
  881. btUnSwapVector3Endian(bvh->m_contiguousNodes[nodeIndex].m_aabbMaxOrg);
  882. bvh->m_contiguousNodes[nodeIndex].m_escapeIndex = static_cast<int>(btSwapEndian(bvh->m_contiguousNodes[nodeIndex].m_escapeIndex));
  883. bvh->m_contiguousNodes[nodeIndex].m_subPart = static_cast<int>(btSwapEndian(bvh->m_contiguousNodes[nodeIndex].m_subPart));
  884. bvh->m_contiguousNodes[nodeIndex].m_triangleIndex = static_cast<int>(btSwapEndian(bvh->m_contiguousNodes[nodeIndex].m_triangleIndex));
  885. }
  886. }
  887. nodeData += sizeof(btOptimizedBvhNode) * nodeCount;
  888. }
  889. sizeToAdd = 0;//(BVH_ALIGNMENT-((unsigned)nodeData & BVH_ALIGNMENT_MASK))&BVH_ALIGNMENT_MASK;
  890. nodeData += sizeToAdd;
  891. // Now serialize the subtree headers
  892. bvh->m_SubtreeHeaders.initializeFromBuffer(nodeData, bvh->m_subtreeHeaderCount, bvh->m_subtreeHeaderCount);
  893. if (i_swapEndian)
  894. {
  895. for (int i = 0; i < bvh->m_subtreeHeaderCount; i++)
  896. {
  897. bvh->m_SubtreeHeaders[i].m_quantizedAabbMin[0] = btSwapEndian(bvh->m_SubtreeHeaders[i].m_quantizedAabbMin[0]);
  898. bvh->m_SubtreeHeaders[i].m_quantizedAabbMin[1] = btSwapEndian(bvh->m_SubtreeHeaders[i].m_quantizedAabbMin[1]);
  899. bvh->m_SubtreeHeaders[i].m_quantizedAabbMin[2] = btSwapEndian(bvh->m_SubtreeHeaders[i].m_quantizedAabbMin[2]);
  900. bvh->m_SubtreeHeaders[i].m_quantizedAabbMax[0] = btSwapEndian(bvh->m_SubtreeHeaders[i].m_quantizedAabbMax[0]);
  901. bvh->m_SubtreeHeaders[i].m_quantizedAabbMax[1] = btSwapEndian(bvh->m_SubtreeHeaders[i].m_quantizedAabbMax[1]);
  902. bvh->m_SubtreeHeaders[i].m_quantizedAabbMax[2] = btSwapEndian(bvh->m_SubtreeHeaders[i].m_quantizedAabbMax[2]);
  903. bvh->m_SubtreeHeaders[i].m_rootNodeIndex = static_cast<int>(btSwapEndian(bvh->m_SubtreeHeaders[i].m_rootNodeIndex));
  904. bvh->m_SubtreeHeaders[i].m_subtreeSize = static_cast<int>(btSwapEndian(bvh->m_SubtreeHeaders[i].m_subtreeSize));
  905. }
  906. }
  907. return bvh;
  908. }
  909. // Constructor that prevents btVector3's default constructor from being called
  910. btQuantizedBvh::btQuantizedBvh(btQuantizedBvh &self, bool /* ownsMemory */) :
  911. m_bvhAabbMin(self.m_bvhAabbMin),
  912. m_bvhAabbMax(self.m_bvhAabbMax),
  913. m_bvhQuantization(self.m_bvhQuantization),
  914. m_bulletVersion(BT_BULLET_VERSION)
  915. {
  916. }
  917. void btQuantizedBvh::deSerializeFloat(struct btQuantizedBvhFloatData& quantizedBvhFloatData)
  918. {
  919. m_bvhAabbMax.deSerializeFloat(quantizedBvhFloatData.m_bvhAabbMax);
  920. m_bvhAabbMin.deSerializeFloat(quantizedBvhFloatData.m_bvhAabbMin);
  921. m_bvhQuantization.deSerializeFloat(quantizedBvhFloatData.m_bvhQuantization);
  922. m_curNodeIndex = quantizedBvhFloatData.m_curNodeIndex;
  923. m_useQuantization = quantizedBvhFloatData.m_useQuantization!=0;
  924. {
  925. int numElem = quantizedBvhFloatData.m_numContiguousLeafNodes;
  926. m_contiguousNodes.resize(numElem);
  927. if (numElem)
  928. {
  929. btOptimizedBvhNodeFloatData* memPtr = quantizedBvhFloatData.m_contiguousNodesPtr;
  930. for (int i=0;i<numElem;i++,memPtr++)
  931. {
  932. m_contiguousNodes[i].m_aabbMaxOrg.deSerializeFloat(memPtr->m_aabbMaxOrg);
  933. m_contiguousNodes[i].m_aabbMinOrg.deSerializeFloat(memPtr->m_aabbMinOrg);
  934. m_contiguousNodes[i].m_escapeIndex = memPtr->m_escapeIndex;
  935. m_contiguousNodes[i].m_subPart = memPtr->m_subPart;
  936. m_contiguousNodes[i].m_triangleIndex = memPtr->m_triangleIndex;
  937. }
  938. }
  939. }
  940. {
  941. int numElem = quantizedBvhFloatData.m_numQuantizedContiguousNodes;
  942. m_quantizedContiguousNodes.resize(numElem);
  943. if (numElem)
  944. {
  945. btQuantizedBvhNodeData* memPtr = quantizedBvhFloatData.m_quantizedContiguousNodesPtr;
  946. for (int i=0;i<numElem;i++,memPtr++)
  947. {
  948. m_quantizedContiguousNodes[i].m_escapeIndexOrTriangleIndex = memPtr->m_escapeIndexOrTriangleIndex;
  949. m_quantizedContiguousNodes[i].m_quantizedAabbMax[0] = memPtr->m_quantizedAabbMax[0];
  950. m_quantizedContiguousNodes[i].m_quantizedAabbMax[1] = memPtr->m_quantizedAabbMax[1];
  951. m_quantizedContiguousNodes[i].m_quantizedAabbMax[2] = memPtr->m_quantizedAabbMax[2];
  952. m_quantizedContiguousNodes[i].m_quantizedAabbMin[0] = memPtr->m_quantizedAabbMin[0];
  953. m_quantizedContiguousNodes[i].m_quantizedAabbMin[1] = memPtr->m_quantizedAabbMin[1];
  954. m_quantizedContiguousNodes[i].m_quantizedAabbMin[2] = memPtr->m_quantizedAabbMin[2];
  955. }
  956. }
  957. }
  958. m_traversalMode = btTraversalMode(quantizedBvhFloatData.m_traversalMode);
  959. {
  960. int numElem = quantizedBvhFloatData.m_numSubtreeHeaders;
  961. m_SubtreeHeaders.resize(numElem);
  962. if (numElem)
  963. {
  964. btBvhSubtreeInfoData* memPtr = quantizedBvhFloatData.m_subTreeInfoPtr;
  965. for (int i=0;i<numElem;i++,memPtr++)
  966. {
  967. m_SubtreeHeaders[i].m_quantizedAabbMax[0] = memPtr->m_quantizedAabbMax[0] ;
  968. m_SubtreeHeaders[i].m_quantizedAabbMax[1] = memPtr->m_quantizedAabbMax[1];
  969. m_SubtreeHeaders[i].m_quantizedAabbMax[2] = memPtr->m_quantizedAabbMax[2];
  970. m_SubtreeHeaders[i].m_quantizedAabbMin[0] = memPtr->m_quantizedAabbMin[0];
  971. m_SubtreeHeaders[i].m_quantizedAabbMin[1] = memPtr->m_quantizedAabbMin[1];
  972. m_SubtreeHeaders[i].m_quantizedAabbMin[2] = memPtr->m_quantizedAabbMin[2];
  973. m_SubtreeHeaders[i].m_rootNodeIndex = memPtr->m_rootNodeIndex;
  974. m_SubtreeHeaders[i].m_subtreeSize = memPtr->m_subtreeSize;
  975. }
  976. }
  977. }
  978. }
  979. void btQuantizedBvh::deSerializeDouble(struct btQuantizedBvhDoubleData& quantizedBvhDoubleData)
  980. {
  981. m_bvhAabbMax.deSerializeDouble(quantizedBvhDoubleData.m_bvhAabbMax);
  982. m_bvhAabbMin.deSerializeDouble(quantizedBvhDoubleData.m_bvhAabbMin);
  983. m_bvhQuantization.deSerializeDouble(quantizedBvhDoubleData.m_bvhQuantization);
  984. m_curNodeIndex = quantizedBvhDoubleData.m_curNodeIndex;
  985. m_useQuantization = quantizedBvhDoubleData.m_useQuantization!=0;
  986. {
  987. int numElem = quantizedBvhDoubleData.m_numContiguousLeafNodes;
  988. m_contiguousNodes.resize(numElem);
  989. if (numElem)
  990. {
  991. btOptimizedBvhNodeDoubleData* memPtr = quantizedBvhDoubleData.m_contiguousNodesPtr;
  992. for (int i=0;i<numElem;i++,memPtr++)
  993. {
  994. m_contiguousNodes[i].m_aabbMaxOrg.deSerializeDouble(memPtr->m_aabbMaxOrg);
  995. m_contiguousNodes[i].m_aabbMinOrg.deSerializeDouble(memPtr->m_aabbMinOrg);
  996. m_contiguousNodes[i].m_escapeIndex = memPtr->m_escapeIndex;
  997. m_contiguousNodes[i].m_subPart = memPtr->m_subPart;
  998. m_contiguousNodes[i].m_triangleIndex = memPtr->m_triangleIndex;
  999. }
  1000. }
  1001. }
  1002. {
  1003. int numElem = quantizedBvhDoubleData.m_numQuantizedContiguousNodes;
  1004. m_quantizedContiguousNodes.resize(numElem);
  1005. if (numElem)
  1006. {
  1007. btQuantizedBvhNodeData* memPtr = quantizedBvhDoubleData.m_quantizedContiguousNodesPtr;
  1008. for (int i=0;i<numElem;i++,memPtr++)
  1009. {
  1010. m_quantizedContiguousNodes[i].m_escapeIndexOrTriangleIndex = memPtr->m_escapeIndexOrTriangleIndex;
  1011. m_quantizedContiguousNodes[i].m_quantizedAabbMax[0] = memPtr->m_quantizedAabbMax[0];
  1012. m_quantizedContiguousNodes[i].m_quantizedAabbMax[1] = memPtr->m_quantizedAabbMax[1];
  1013. m_quantizedContiguousNodes[i].m_quantizedAabbMax[2] = memPtr->m_quantizedAabbMax[2];
  1014. m_quantizedContiguousNodes[i].m_quantizedAabbMin[0] = memPtr->m_quantizedAabbMin[0];
  1015. m_quantizedContiguousNodes[i].m_quantizedAabbMin[1] = memPtr->m_quantizedAabbMin[1];
  1016. m_quantizedContiguousNodes[i].m_quantizedAabbMin[2] = memPtr->m_quantizedAabbMin[2];
  1017. }
  1018. }
  1019. }
  1020. m_traversalMode = btTraversalMode(quantizedBvhDoubleData.m_traversalMode);
  1021. {
  1022. int numElem = quantizedBvhDoubleData.m_numSubtreeHeaders;
  1023. m_SubtreeHeaders.resize(numElem);
  1024. if (numElem)
  1025. {
  1026. btBvhSubtreeInfoData* memPtr = quantizedBvhDoubleData.m_subTreeInfoPtr;
  1027. for (int i=0;i<numElem;i++,memPtr++)
  1028. {
  1029. m_SubtreeHeaders[i].m_quantizedAabbMax[0] = memPtr->m_quantizedAabbMax[0] ;
  1030. m_SubtreeHeaders[i].m_quantizedAabbMax[1] = memPtr->m_quantizedAabbMax[1];
  1031. m_SubtreeHeaders[i].m_quantizedAabbMax[2] = memPtr->m_quantizedAabbMax[2];
  1032. m_SubtreeHeaders[i].m_quantizedAabbMin[0] = memPtr->m_quantizedAabbMin[0];
  1033. m_SubtreeHeaders[i].m_quantizedAabbMin[1] = memPtr->m_quantizedAabbMin[1];
  1034. m_SubtreeHeaders[i].m_quantizedAabbMin[2] = memPtr->m_quantizedAabbMin[2];
  1035. m_SubtreeHeaders[i].m_rootNodeIndex = memPtr->m_rootNodeIndex;
  1036. m_SubtreeHeaders[i].m_subtreeSize = memPtr->m_subtreeSize;
  1037. }
  1038. }
  1039. }
  1040. }
  1041. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  1042. const char* btQuantizedBvh::serialize(void* dataBuffer, btSerializer* serializer) const
  1043. {
  1044. btQuantizedBvhData* quantizedData = (btQuantizedBvhData*)dataBuffer;
  1045. m_bvhAabbMax.serialize(quantizedData->m_bvhAabbMax);
  1046. m_bvhAabbMin.serialize(quantizedData->m_bvhAabbMin);
  1047. m_bvhQuantization.serialize(quantizedData->m_bvhQuantization);
  1048. quantizedData->m_curNodeIndex = m_curNodeIndex;
  1049. quantizedData->m_useQuantization = m_useQuantization;
  1050. quantizedData->m_numContiguousLeafNodes = m_contiguousNodes.size();
  1051. quantizedData->m_contiguousNodesPtr = (btOptimizedBvhNodeData*) (m_contiguousNodes.size() ? serializer->getUniquePointer((void*)&m_contiguousNodes[0]) : 0);
  1052. if (quantizedData->m_contiguousNodesPtr)
  1053. {
  1054. int sz = sizeof(btOptimizedBvhNodeData);
  1055. int numElem = m_contiguousNodes.size();
  1056. btChunk* chunk = serializer->allocate(sz,numElem);
  1057. btOptimizedBvhNodeData* memPtr = (btOptimizedBvhNodeData*)chunk->m_oldPtr;
  1058. for (int i=0;i<numElem;i++,memPtr++)
  1059. {
  1060. m_contiguousNodes[i].m_aabbMaxOrg.serialize(memPtr->m_aabbMaxOrg);
  1061. m_contiguousNodes[i].m_aabbMinOrg.serialize(memPtr->m_aabbMinOrg);
  1062. memPtr->m_escapeIndex = m_contiguousNodes[i].m_escapeIndex;
  1063. memPtr->m_subPart = m_contiguousNodes[i].m_subPart;
  1064. memPtr->m_triangleIndex = m_contiguousNodes[i].m_triangleIndex;
  1065. }
  1066. serializer->finalizeChunk(chunk,"btOptimizedBvhNodeData",BT_ARRAY_CODE,(void*)&m_contiguousNodes[0]);
  1067. }
  1068. quantizedData->m_numQuantizedContiguousNodes = m_quantizedContiguousNodes.size();
  1069. // printf("quantizedData->m_numQuantizedContiguousNodes=%d\n",quantizedData->m_numQuantizedContiguousNodes);
  1070. quantizedData->m_quantizedContiguousNodesPtr =(btQuantizedBvhNodeData*) (m_quantizedContiguousNodes.size() ? serializer->getUniquePointer((void*)&m_quantizedContiguousNodes[0]) : 0);
  1071. if (quantizedData->m_quantizedContiguousNodesPtr)
  1072. {
  1073. int sz = sizeof(btQuantizedBvhNodeData);
  1074. int numElem = m_quantizedContiguousNodes.size();
  1075. btChunk* chunk = serializer->allocate(sz,numElem);
  1076. btQuantizedBvhNodeData* memPtr = (btQuantizedBvhNodeData*)chunk->m_oldPtr;
  1077. for (int i=0;i<numElem;i++,memPtr++)
  1078. {
  1079. memPtr->m_escapeIndexOrTriangleIndex = m_quantizedContiguousNodes[i].m_escapeIndexOrTriangleIndex;
  1080. memPtr->m_quantizedAabbMax[0] = m_quantizedContiguousNodes[i].m_quantizedAabbMax[0];
  1081. memPtr->m_quantizedAabbMax[1] = m_quantizedContiguousNodes[i].m_quantizedAabbMax[1];
  1082. memPtr->m_quantizedAabbMax[2] = m_quantizedContiguousNodes[i].m_quantizedAabbMax[2];
  1083. memPtr->m_quantizedAabbMin[0] = m_quantizedContiguousNodes[i].m_quantizedAabbMin[0];
  1084. memPtr->m_quantizedAabbMin[1] = m_quantizedContiguousNodes[i].m_quantizedAabbMin[1];
  1085. memPtr->m_quantizedAabbMin[2] = m_quantizedContiguousNodes[i].m_quantizedAabbMin[2];
  1086. }
  1087. serializer->finalizeChunk(chunk,"btQuantizedBvhNodeData",BT_ARRAY_CODE,(void*)&m_quantizedContiguousNodes[0]);
  1088. }
  1089. quantizedData->m_traversalMode = int(m_traversalMode);
  1090. quantizedData->m_numSubtreeHeaders = m_SubtreeHeaders.size();
  1091. quantizedData->m_subTreeInfoPtr = (btBvhSubtreeInfoData*) (m_SubtreeHeaders.size() ? serializer->getUniquePointer((void*)&m_SubtreeHeaders[0]) : 0);
  1092. if (quantizedData->m_subTreeInfoPtr)
  1093. {
  1094. int sz = sizeof(btBvhSubtreeInfoData);
  1095. int numElem = m_SubtreeHeaders.size();
  1096. btChunk* chunk = serializer->allocate(sz,numElem);
  1097. btBvhSubtreeInfoData* memPtr = (btBvhSubtreeInfoData*)chunk->m_oldPtr;
  1098. for (int i=0;i<numElem;i++,memPtr++)
  1099. {
  1100. memPtr->m_quantizedAabbMax[0] = m_SubtreeHeaders[i].m_quantizedAabbMax[0];
  1101. memPtr->m_quantizedAabbMax[1] = m_SubtreeHeaders[i].m_quantizedAabbMax[1];
  1102. memPtr->m_quantizedAabbMax[2] = m_SubtreeHeaders[i].m_quantizedAabbMax[2];
  1103. memPtr->m_quantizedAabbMin[0] = m_SubtreeHeaders[i].m_quantizedAabbMin[0];
  1104. memPtr->m_quantizedAabbMin[1] = m_SubtreeHeaders[i].m_quantizedAabbMin[1];
  1105. memPtr->m_quantizedAabbMin[2] = m_SubtreeHeaders[i].m_quantizedAabbMin[2];
  1106. memPtr->m_rootNodeIndex = m_SubtreeHeaders[i].m_rootNodeIndex;
  1107. memPtr->m_subtreeSize = m_SubtreeHeaders[i].m_subtreeSize;
  1108. }
  1109. serializer->finalizeChunk(chunk,"btBvhSubtreeInfoData",BT_ARRAY_CODE,(void*)&m_SubtreeHeaders[0]);
  1110. }
  1111. return btQuantizedBvhDataName;
  1112. }