btQuantizedBvh.cpp 50 KB

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