b3QuantizedBvh.cpp 46 KB

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