btQuantizedBvh.cpp 50 KB

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