b3DynamicBvh.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2013 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. ///b3DynamicBvh implementation by Nathanael Presson
  14. #include "b3DynamicBvh.h"
  15. //
  16. typedef b3AlignedObjectArray<b3DbvtNode*> b3NodeArray;
  17. typedef b3AlignedObjectArray<const b3DbvtNode*> b3ConstNodeArray;
  18. //
  19. struct b3DbvtNodeEnumerator : b3DynamicBvh::ICollide
  20. {
  21. b3ConstNodeArray nodes;
  22. void Process(const b3DbvtNode* n) { nodes.push_back(n); }
  23. };
  24. //
  25. static B3_DBVT_INLINE int b3IndexOf(const b3DbvtNode* node)
  26. {
  27. return (node->parent->childs[1] == node);
  28. }
  29. //
  30. static B3_DBVT_INLINE b3DbvtVolume b3Merge(const b3DbvtVolume& a,
  31. const b3DbvtVolume& b)
  32. {
  33. #if (B3_DBVT_MERGE_IMPL == B3_DBVT_IMPL_SSE)
  34. B3_ATTRIBUTE_ALIGNED16(char locals[sizeof(b3DbvtAabbMm)]);
  35. b3DbvtVolume& res = *(b3DbvtVolume*)locals;
  36. #else
  37. b3DbvtVolume res;
  38. #endif
  39. b3Merge(a, b, res);
  40. return (res);
  41. }
  42. // volume+edge lengths
  43. static B3_DBVT_INLINE b3Scalar b3Size(const b3DbvtVolume& a)
  44. {
  45. const b3Vector3 edges = a.Lengths();
  46. return (edges.x * edges.y * edges.z +
  47. edges.x + edges.y + edges.z);
  48. }
  49. //
  50. static void b3GetMaxDepth(const b3DbvtNode* node, int depth, int& maxdepth)
  51. {
  52. if (node->isinternal())
  53. {
  54. b3GetMaxDepth(node->childs[0], depth + 1, maxdepth);
  55. b3GetMaxDepth(node->childs[1], depth + 1, maxdepth);
  56. }
  57. else
  58. maxdepth = b3Max(maxdepth, depth);
  59. }
  60. //
  61. static B3_DBVT_INLINE void b3DeleteNode(b3DynamicBvh* pdbvt,
  62. b3DbvtNode* node)
  63. {
  64. b3AlignedFree(pdbvt->m_free);
  65. pdbvt->m_free = node;
  66. }
  67. //
  68. static void b3RecurseDeleteNode(b3DynamicBvh* pdbvt,
  69. b3DbvtNode* node)
  70. {
  71. if (!node->isleaf())
  72. {
  73. b3RecurseDeleteNode(pdbvt, node->childs[0]);
  74. b3RecurseDeleteNode(pdbvt, node->childs[1]);
  75. }
  76. if (node == pdbvt->m_root) pdbvt->m_root = 0;
  77. b3DeleteNode(pdbvt, node);
  78. }
  79. //
  80. static B3_DBVT_INLINE b3DbvtNode* b3CreateNode(b3DynamicBvh* pdbvt,
  81. b3DbvtNode* parent,
  82. void* data)
  83. {
  84. b3DbvtNode* node;
  85. if (pdbvt->m_free)
  86. {
  87. node = pdbvt->m_free;
  88. pdbvt->m_free = 0;
  89. }
  90. else
  91. {
  92. node = new (b3AlignedAlloc(sizeof(b3DbvtNode), 16)) b3DbvtNode();
  93. }
  94. node->parent = parent;
  95. node->data = data;
  96. node->childs[1] = 0;
  97. return (node);
  98. }
  99. //
  100. static B3_DBVT_INLINE b3DbvtNode* b3CreateNode(b3DynamicBvh* pdbvt,
  101. b3DbvtNode* parent,
  102. const b3DbvtVolume& volume,
  103. void* data)
  104. {
  105. b3DbvtNode* node = b3CreateNode(pdbvt, parent, data);
  106. node->volume = volume;
  107. return (node);
  108. }
  109. //
  110. static B3_DBVT_INLINE b3DbvtNode* b3CreateNode(b3DynamicBvh* pdbvt,
  111. b3DbvtNode* parent,
  112. const b3DbvtVolume& volume0,
  113. const b3DbvtVolume& volume1,
  114. void* data)
  115. {
  116. b3DbvtNode* node = b3CreateNode(pdbvt, parent, data);
  117. b3Merge(volume0, volume1, node->volume);
  118. return (node);
  119. }
  120. //
  121. static void b3InsertLeaf(b3DynamicBvh* pdbvt,
  122. b3DbvtNode* root,
  123. b3DbvtNode* leaf)
  124. {
  125. if (!pdbvt->m_root)
  126. {
  127. pdbvt->m_root = leaf;
  128. leaf->parent = 0;
  129. }
  130. else
  131. {
  132. if (!root->isleaf())
  133. {
  134. do
  135. {
  136. root = root->childs[b3Select(leaf->volume,
  137. root->childs[0]->volume,
  138. root->childs[1]->volume)];
  139. } while (!root->isleaf());
  140. }
  141. b3DbvtNode* prev = root->parent;
  142. b3DbvtNode* node = b3CreateNode(pdbvt, prev, leaf->volume, root->volume, 0);
  143. if (prev)
  144. {
  145. prev->childs[b3IndexOf(root)] = node;
  146. node->childs[0] = root;
  147. root->parent = node;
  148. node->childs[1] = leaf;
  149. leaf->parent = node;
  150. do
  151. {
  152. if (!prev->volume.Contain(node->volume))
  153. b3Merge(prev->childs[0]->volume, prev->childs[1]->volume, prev->volume);
  154. else
  155. break;
  156. node = prev;
  157. } while (0 != (prev = node->parent));
  158. }
  159. else
  160. {
  161. node->childs[0] = root;
  162. root->parent = node;
  163. node->childs[1] = leaf;
  164. leaf->parent = node;
  165. pdbvt->m_root = node;
  166. }
  167. }
  168. }
  169. //
  170. static b3DbvtNode* b3RemoveLeaf(b3DynamicBvh* pdbvt,
  171. b3DbvtNode* leaf)
  172. {
  173. if (leaf == pdbvt->m_root)
  174. {
  175. pdbvt->m_root = 0;
  176. return (0);
  177. }
  178. else
  179. {
  180. b3DbvtNode* parent = leaf->parent;
  181. b3DbvtNode* prev = parent->parent;
  182. b3DbvtNode* sibling = parent->childs[1 - b3IndexOf(leaf)];
  183. if (prev)
  184. {
  185. prev->childs[b3IndexOf(parent)] = sibling;
  186. sibling->parent = prev;
  187. b3DeleteNode(pdbvt, parent);
  188. while (prev)
  189. {
  190. const b3DbvtVolume pb = prev->volume;
  191. b3Merge(prev->childs[0]->volume, prev->childs[1]->volume, prev->volume);
  192. if (b3NotEqual(pb, prev->volume))
  193. {
  194. prev = prev->parent;
  195. }
  196. else
  197. break;
  198. }
  199. return (prev ? prev : pdbvt->m_root);
  200. }
  201. else
  202. {
  203. pdbvt->m_root = sibling;
  204. sibling->parent = 0;
  205. b3DeleteNode(pdbvt, parent);
  206. return (pdbvt->m_root);
  207. }
  208. }
  209. }
  210. //
  211. static void b3FetchLeaves(b3DynamicBvh* pdbvt,
  212. b3DbvtNode* root,
  213. b3NodeArray& leaves,
  214. int depth = -1)
  215. {
  216. if (root->isinternal() && depth)
  217. {
  218. b3FetchLeaves(pdbvt, root->childs[0], leaves, depth - 1);
  219. b3FetchLeaves(pdbvt, root->childs[1], leaves, depth - 1);
  220. b3DeleteNode(pdbvt, root);
  221. }
  222. else
  223. {
  224. leaves.push_back(root);
  225. }
  226. }
  227. static bool b3LeftOfAxis(const b3DbvtNode* node,
  228. const b3Vector3& org,
  229. const b3Vector3& axis)
  230. {
  231. return b3Dot(axis, node->volume.Center() - org) <= 0;
  232. }
  233. // Partitions leaves such that leaves[0, n) are on the
  234. // left of axis, and leaves[n, count) are on the right
  235. // of axis. returns N.
  236. static int b3Split(b3DbvtNode** leaves,
  237. int count,
  238. const b3Vector3& org,
  239. const b3Vector3& axis)
  240. {
  241. int begin = 0;
  242. int end = count;
  243. for (;;)
  244. {
  245. while (begin != end && b3LeftOfAxis(leaves[begin], org, axis))
  246. {
  247. ++begin;
  248. }
  249. if (begin == end)
  250. {
  251. break;
  252. }
  253. while (begin != end && !b3LeftOfAxis(leaves[end - 1], org, axis))
  254. {
  255. --end;
  256. }
  257. if (begin == end)
  258. {
  259. break;
  260. }
  261. // swap out of place nodes
  262. --end;
  263. b3DbvtNode* temp = leaves[begin];
  264. leaves[begin] = leaves[end];
  265. leaves[end] = temp;
  266. ++begin;
  267. }
  268. return begin;
  269. }
  270. //
  271. static b3DbvtVolume b3Bounds(b3DbvtNode** leaves,
  272. int count)
  273. {
  274. #if B3_DBVT_MERGE_IMPL == B3_DBVT_IMPL_SSE
  275. B3_ATTRIBUTE_ALIGNED16(char locals[sizeof(b3DbvtVolume)]);
  276. b3DbvtVolume& volume = *(b3DbvtVolume*)locals;
  277. volume = leaves[0]->volume;
  278. #else
  279. b3DbvtVolume volume = leaves[0]->volume;
  280. #endif
  281. for (int i = 1, ni = count; i < ni; ++i)
  282. {
  283. b3Merge(volume, leaves[i]->volume, volume);
  284. }
  285. return (volume);
  286. }
  287. //
  288. static void b3BottomUp(b3DynamicBvh* pdbvt,
  289. b3DbvtNode** leaves,
  290. int count)
  291. {
  292. while (count > 1)
  293. {
  294. b3Scalar minsize = B3_INFINITY;
  295. int minidx[2] = {-1, -1};
  296. for (int i = 0; i < count; ++i)
  297. {
  298. for (int j = i + 1; j < count; ++j)
  299. {
  300. const b3Scalar sz = b3Size(b3Merge(leaves[i]->volume, leaves[j]->volume));
  301. if (sz < minsize)
  302. {
  303. minsize = sz;
  304. minidx[0] = i;
  305. minidx[1] = j;
  306. }
  307. }
  308. }
  309. b3DbvtNode* n[] = {leaves[minidx[0]], leaves[minidx[1]]};
  310. b3DbvtNode* p = b3CreateNode(pdbvt, 0, n[0]->volume, n[1]->volume, 0);
  311. p->childs[0] = n[0];
  312. p->childs[1] = n[1];
  313. n[0]->parent = p;
  314. n[1]->parent = p;
  315. leaves[minidx[0]] = p;
  316. leaves[minidx[1]] = leaves[count - 1];
  317. --count;
  318. }
  319. }
  320. //
  321. static b3DbvtNode* b3TopDown(b3DynamicBvh* pdbvt,
  322. b3DbvtNode** leaves,
  323. int count,
  324. int bu_treshold)
  325. {
  326. static const b3Vector3 axis[] = {b3MakeVector3(1, 0, 0),
  327. b3MakeVector3(0, 1, 0),
  328. b3MakeVector3(0, 0, 1)};
  329. b3Assert(bu_treshold > 1);
  330. if (count > 1)
  331. {
  332. if (count > bu_treshold)
  333. {
  334. const b3DbvtVolume vol = b3Bounds(leaves, count);
  335. const b3Vector3 org = vol.Center();
  336. int partition;
  337. int bestaxis = -1;
  338. int bestmidp = count;
  339. int splitcount[3][2] = {{0, 0}, {0, 0}, {0, 0}};
  340. int i;
  341. for (i = 0; i < count; ++i)
  342. {
  343. const b3Vector3 x = leaves[i]->volume.Center() - org;
  344. for (int j = 0; j < 3; ++j)
  345. {
  346. ++splitcount[j][b3Dot(x, axis[j]) > 0 ? 1 : 0];
  347. }
  348. }
  349. for (i = 0; i < 3; ++i)
  350. {
  351. if ((splitcount[i][0] > 0) && (splitcount[i][1] > 0))
  352. {
  353. const int midp = (int)b3Fabs(b3Scalar(splitcount[i][0] - splitcount[i][1]));
  354. if (midp < bestmidp)
  355. {
  356. bestaxis = i;
  357. bestmidp = midp;
  358. }
  359. }
  360. }
  361. if (bestaxis >= 0)
  362. {
  363. partition = b3Split(leaves, count, org, axis[bestaxis]);
  364. b3Assert(partition != 0 && partition != count);
  365. }
  366. else
  367. {
  368. partition = count / 2 + 1;
  369. }
  370. b3DbvtNode* node = b3CreateNode(pdbvt, 0, vol, 0);
  371. node->childs[0] = b3TopDown(pdbvt, &leaves[0], partition, bu_treshold);
  372. node->childs[1] = b3TopDown(pdbvt, &leaves[partition], count - partition, bu_treshold);
  373. node->childs[0]->parent = node;
  374. node->childs[1]->parent = node;
  375. return (node);
  376. }
  377. else
  378. {
  379. b3BottomUp(pdbvt, leaves, count);
  380. return (leaves[0]);
  381. }
  382. }
  383. return (leaves[0]);
  384. }
  385. //
  386. static B3_DBVT_INLINE b3DbvtNode* b3Sort(b3DbvtNode* n, b3DbvtNode*& r)
  387. {
  388. b3DbvtNode* p = n->parent;
  389. b3Assert(n->isinternal());
  390. if (p > n)
  391. {
  392. const int i = b3IndexOf(n);
  393. const int j = 1 - i;
  394. b3DbvtNode* s = p->childs[j];
  395. b3DbvtNode* q = p->parent;
  396. b3Assert(n == p->childs[i]);
  397. if (q)
  398. q->childs[b3IndexOf(p)] = n;
  399. else
  400. r = n;
  401. s->parent = n;
  402. p->parent = n;
  403. n->parent = q;
  404. p->childs[0] = n->childs[0];
  405. p->childs[1] = n->childs[1];
  406. n->childs[0]->parent = p;
  407. n->childs[1]->parent = p;
  408. n->childs[i] = p;
  409. n->childs[j] = s;
  410. b3Swap(p->volume, n->volume);
  411. return (p);
  412. }
  413. return (n);
  414. }
  415. #if 0
  416. static B3_DBVT_INLINE b3DbvtNode* walkup(b3DbvtNode* n,int count)
  417. {
  418. while(n&&(count--)) n=n->parent;
  419. return(n);
  420. }
  421. #endif
  422. //
  423. // Api
  424. //
  425. //
  426. b3DynamicBvh::b3DynamicBvh()
  427. {
  428. m_root = 0;
  429. m_free = 0;
  430. m_lkhd = -1;
  431. m_leaves = 0;
  432. m_opath = 0;
  433. }
  434. //
  435. b3DynamicBvh::~b3DynamicBvh()
  436. {
  437. clear();
  438. }
  439. //
  440. void b3DynamicBvh::clear()
  441. {
  442. if (m_root)
  443. b3RecurseDeleteNode(this, m_root);
  444. b3AlignedFree(m_free);
  445. m_free = 0;
  446. m_lkhd = -1;
  447. m_stkStack.clear();
  448. m_opath = 0;
  449. }
  450. //
  451. void b3DynamicBvh::optimizeBottomUp()
  452. {
  453. if (m_root)
  454. {
  455. b3NodeArray leaves;
  456. leaves.reserve(m_leaves);
  457. b3FetchLeaves(this, m_root, leaves);
  458. b3BottomUp(this, &leaves[0], leaves.size());
  459. m_root = leaves[0];
  460. }
  461. }
  462. //
  463. void b3DynamicBvh::optimizeTopDown(int bu_treshold)
  464. {
  465. if (m_root)
  466. {
  467. b3NodeArray leaves;
  468. leaves.reserve(m_leaves);
  469. b3FetchLeaves(this, m_root, leaves);
  470. m_root = b3TopDown(this, &leaves[0], leaves.size(), bu_treshold);
  471. }
  472. }
  473. //
  474. void b3DynamicBvh::optimizeIncremental(int passes)
  475. {
  476. if (passes < 0) passes = m_leaves;
  477. if (m_root && (passes > 0))
  478. {
  479. do
  480. {
  481. b3DbvtNode* node = m_root;
  482. unsigned bit = 0;
  483. while (node->isinternal())
  484. {
  485. node = b3Sort(node, m_root)->childs[(m_opath >> bit) & 1];
  486. bit = (bit + 1) & (sizeof(unsigned) * 8 - 1);
  487. }
  488. update(node);
  489. ++m_opath;
  490. } while (--passes);
  491. }
  492. }
  493. //
  494. b3DbvtNode* b3DynamicBvh::insert(const b3DbvtVolume& volume, void* data)
  495. {
  496. b3DbvtNode* leaf = b3CreateNode(this, 0, volume, data);
  497. b3InsertLeaf(this, m_root, leaf);
  498. ++m_leaves;
  499. return (leaf);
  500. }
  501. //
  502. void b3DynamicBvh::update(b3DbvtNode* leaf, int lookahead)
  503. {
  504. b3DbvtNode* root = b3RemoveLeaf(this, leaf);
  505. if (root)
  506. {
  507. if (lookahead >= 0)
  508. {
  509. for (int i = 0; (i < lookahead) && root->parent; ++i)
  510. {
  511. root = root->parent;
  512. }
  513. }
  514. else
  515. root = m_root;
  516. }
  517. b3InsertLeaf(this, root, leaf);
  518. }
  519. //
  520. void b3DynamicBvh::update(b3DbvtNode* leaf, b3DbvtVolume& volume)
  521. {
  522. b3DbvtNode* root = b3RemoveLeaf(this, leaf);
  523. if (root)
  524. {
  525. if (m_lkhd >= 0)
  526. {
  527. for (int i = 0; (i < m_lkhd) && root->parent; ++i)
  528. {
  529. root = root->parent;
  530. }
  531. }
  532. else
  533. root = m_root;
  534. }
  535. leaf->volume = volume;
  536. b3InsertLeaf(this, root, leaf);
  537. }
  538. //
  539. bool b3DynamicBvh::update(b3DbvtNode* leaf, b3DbvtVolume& volume, const b3Vector3& velocity, b3Scalar margin)
  540. {
  541. if (leaf->volume.Contain(volume)) return (false);
  542. volume.Expand(b3MakeVector3(margin, margin, margin));
  543. volume.SignedExpand(velocity);
  544. update(leaf, volume);
  545. return (true);
  546. }
  547. //
  548. bool b3DynamicBvh::update(b3DbvtNode* leaf, b3DbvtVolume& volume, const b3Vector3& velocity)
  549. {
  550. if (leaf->volume.Contain(volume)) return (false);
  551. volume.SignedExpand(velocity);
  552. update(leaf, volume);
  553. return (true);
  554. }
  555. //
  556. bool b3DynamicBvh::update(b3DbvtNode* leaf, b3DbvtVolume& volume, b3Scalar margin)
  557. {
  558. if (leaf->volume.Contain(volume)) return (false);
  559. volume.Expand(b3MakeVector3(margin, margin, margin));
  560. update(leaf, volume);
  561. return (true);
  562. }
  563. //
  564. void b3DynamicBvh::remove(b3DbvtNode* leaf)
  565. {
  566. b3RemoveLeaf(this, leaf);
  567. b3DeleteNode(this, leaf);
  568. --m_leaves;
  569. }
  570. //
  571. void b3DynamicBvh::write(IWriter* iwriter) const
  572. {
  573. b3DbvtNodeEnumerator nodes;
  574. nodes.nodes.reserve(m_leaves * 2);
  575. enumNodes(m_root, nodes);
  576. iwriter->Prepare(m_root, nodes.nodes.size());
  577. for (int i = 0; i < nodes.nodes.size(); ++i)
  578. {
  579. const b3DbvtNode* n = nodes.nodes[i];
  580. int p = -1;
  581. if (n->parent) p = nodes.nodes.findLinearSearch(n->parent);
  582. if (n->isinternal())
  583. {
  584. const int c0 = nodes.nodes.findLinearSearch(n->childs[0]);
  585. const int c1 = nodes.nodes.findLinearSearch(n->childs[1]);
  586. iwriter->WriteNode(n, i, p, c0, c1);
  587. }
  588. else
  589. {
  590. iwriter->WriteLeaf(n, i, p);
  591. }
  592. }
  593. }
  594. //
  595. void b3DynamicBvh::clone(b3DynamicBvh& dest, IClone* iclone) const
  596. {
  597. dest.clear();
  598. if (m_root != 0)
  599. {
  600. b3AlignedObjectArray<sStkCLN> stack;
  601. stack.reserve(m_leaves);
  602. stack.push_back(sStkCLN(m_root, 0));
  603. do
  604. {
  605. const int i = stack.size() - 1;
  606. const sStkCLN e = stack[i];
  607. b3DbvtNode* n = b3CreateNode(&dest, e.parent, e.node->volume, e.node->data);
  608. stack.pop_back();
  609. if (e.parent != 0)
  610. e.parent->childs[i & 1] = n;
  611. else
  612. dest.m_root = n;
  613. if (e.node->isinternal())
  614. {
  615. stack.push_back(sStkCLN(e.node->childs[0], n));
  616. stack.push_back(sStkCLN(e.node->childs[1], n));
  617. }
  618. else
  619. {
  620. iclone->CloneLeaf(n);
  621. }
  622. } while (stack.size() > 0);
  623. }
  624. }
  625. //
  626. int b3DynamicBvh::maxdepth(const b3DbvtNode* node)
  627. {
  628. int depth = 0;
  629. if (node) b3GetMaxDepth(node, 1, depth);
  630. return (depth);
  631. }
  632. //
  633. int b3DynamicBvh::countLeaves(const b3DbvtNode* node)
  634. {
  635. if (node->isinternal())
  636. return (countLeaves(node->childs[0]) + countLeaves(node->childs[1]));
  637. else
  638. return (1);
  639. }
  640. //
  641. void b3DynamicBvh::extractLeaves(const b3DbvtNode* node, b3AlignedObjectArray<const b3DbvtNode*>& leaves)
  642. {
  643. if (node->isinternal())
  644. {
  645. extractLeaves(node->childs[0], leaves);
  646. extractLeaves(node->childs[1], leaves);
  647. }
  648. else
  649. {
  650. leaves.push_back(node);
  651. }
  652. }
  653. //
  654. #if B3_DBVT_ENABLE_BENCHMARK
  655. #include <stdio.h>
  656. #include <stdlib.h>
  657. /*
  658. q6600,2.4ghz
  659. /Ox /Ob2 /Oi /Ot /I "." /I "..\.." /I "..\..\src" /D "NDEBUG" /D "_LIB" /D "_WINDOWS" /D "_CRT_SECURE_NO_DEPRECATE" /D "_CRT_NONSTDC_NO_DEPRECATE" /D "WIN32"
  660. /GF /FD /MT /GS- /Gy /arch:SSE2 /Zc:wchar_t- /Fp"..\..\out\release8\build\libbulletcollision\libbulletcollision.pch"
  661. /Fo"..\..\out\release8\build\libbulletcollision\\"
  662. /Fd"..\..\out\release8\build\libbulletcollision\bulletcollision.pdb"
  663. /W3 /nologo /c /Wp64 /Zi /errorReport:prompt
  664. Benchmarking dbvt...
  665. World scale: 100.000000
  666. Extents base: 1.000000
  667. Extents range: 4.000000
  668. Leaves: 8192
  669. sizeof(b3DbvtVolume): 32 bytes
  670. sizeof(b3DbvtNode): 44 bytes
  671. [1] b3DbvtVolume intersections: 3499 ms (-1%)
  672. [2] b3DbvtVolume merges: 1934 ms (0%)
  673. [3] b3DynamicBvh::collideTT: 5485 ms (-21%)
  674. [4] b3DynamicBvh::collideTT self: 2814 ms (-20%)
  675. [5] b3DynamicBvh::collideTT xform: 7379 ms (-1%)
  676. [6] b3DynamicBvh::collideTT xform,self: 7270 ms (-2%)
  677. [7] b3DynamicBvh::rayTest: 6314 ms (0%),(332143 r/s)
  678. [8] insert/remove: 2093 ms (0%),(1001983 ir/s)
  679. [9] updates (teleport): 1879 ms (-3%),(1116100 u/s)
  680. [10] updates (jitter): 1244 ms (-4%),(1685813 u/s)
  681. [11] optimize (incremental): 2514 ms (0%),(1668000 o/s)
  682. [12] b3DbvtVolume notequal: 3659 ms (0%)
  683. [13] culling(OCL+fullsort): 2218 ms (0%),(461 t/s)
  684. [14] culling(OCL+qsort): 3688 ms (5%),(2221 t/s)
  685. [15] culling(KDOP+qsort): 1139 ms (-1%),(7192 t/s)
  686. [16] insert/remove batch(256): 5092 ms (0%),(823704 bir/s)
  687. [17] b3DbvtVolume select: 3419 ms (0%)
  688. */
  689. struct b3DbvtBenchmark
  690. {
  691. struct NilPolicy : b3DynamicBvh::ICollide
  692. {
  693. NilPolicy() : m_pcount(0), m_depth(-B3_INFINITY), m_checksort(true) {}
  694. void Process(const b3DbvtNode*, const b3DbvtNode*) { ++m_pcount; }
  695. void Process(const b3DbvtNode*) { ++m_pcount; }
  696. void Process(const b3DbvtNode*, b3Scalar depth)
  697. {
  698. ++m_pcount;
  699. if (m_checksort)
  700. {
  701. if (depth >= m_depth)
  702. m_depth = depth;
  703. else
  704. printf("wrong depth: %f (should be >= %f)\r\n", depth, m_depth);
  705. }
  706. }
  707. int m_pcount;
  708. b3Scalar m_depth;
  709. bool m_checksort;
  710. };
  711. struct P14 : b3DynamicBvh::ICollide
  712. {
  713. struct Node
  714. {
  715. const b3DbvtNode* leaf;
  716. b3Scalar depth;
  717. };
  718. void Process(const b3DbvtNode* leaf, b3Scalar depth)
  719. {
  720. Node n;
  721. n.leaf = leaf;
  722. n.depth = depth;
  723. }
  724. static int sortfnc(const Node& a, const Node& b)
  725. {
  726. if (a.depth < b.depth) return (+1);
  727. if (a.depth > b.depth) return (-1);
  728. return (0);
  729. }
  730. b3AlignedObjectArray<Node> m_nodes;
  731. };
  732. struct P15 : b3DynamicBvh::ICollide
  733. {
  734. struct Node
  735. {
  736. const b3DbvtNode* leaf;
  737. b3Scalar depth;
  738. };
  739. void Process(const b3DbvtNode* leaf)
  740. {
  741. Node n;
  742. n.leaf = leaf;
  743. n.depth = dot(leaf->volume.Center(), m_axis);
  744. }
  745. static int sortfnc(const Node& a, const Node& b)
  746. {
  747. if (a.depth < b.depth) return (+1);
  748. if (a.depth > b.depth) return (-1);
  749. return (0);
  750. }
  751. b3AlignedObjectArray<Node> m_nodes;
  752. b3Vector3 m_axis;
  753. };
  754. static b3Scalar RandUnit()
  755. {
  756. return (rand() / (b3Scalar)RAND_MAX);
  757. }
  758. static b3Vector3 RandVector3()
  759. {
  760. return (b3Vector3(RandUnit(), RandUnit(), RandUnit()));
  761. }
  762. static b3Vector3 RandVector3(b3Scalar cs)
  763. {
  764. return (RandVector3() * cs - b3Vector3(cs, cs, cs) / 2);
  765. }
  766. static b3DbvtVolume RandVolume(b3Scalar cs, b3Scalar eb, b3Scalar es)
  767. {
  768. return (b3DbvtVolume::FromCE(RandVector3(cs), b3Vector3(eb, eb, eb) + RandVector3() * es));
  769. }
  770. static b3Transform RandTransform(b3Scalar cs)
  771. {
  772. b3Transform t;
  773. t.setOrigin(RandVector3(cs));
  774. t.setRotation(b3Quaternion(RandUnit() * B3_PI * 2, RandUnit() * B3_PI * 2, RandUnit() * B3_PI * 2).normalized());
  775. return (t);
  776. }
  777. static void RandTree(b3Scalar cs, b3Scalar eb, b3Scalar es, int leaves, b3DynamicBvh& dbvt)
  778. {
  779. dbvt.clear();
  780. for (int i = 0; i < leaves; ++i)
  781. {
  782. dbvt.insert(RandVolume(cs, eb, es), 0);
  783. }
  784. }
  785. };
  786. void b3DynamicBvh::benchmark()
  787. {
  788. static const b3Scalar cfgVolumeCenterScale = 100;
  789. static const b3Scalar cfgVolumeExentsBase = 1;
  790. static const b3Scalar cfgVolumeExentsScale = 4;
  791. static const int cfgLeaves = 8192;
  792. static const bool cfgEnable = true;
  793. //[1] b3DbvtVolume intersections
  794. bool cfgBenchmark1_Enable = cfgEnable;
  795. static const int cfgBenchmark1_Iterations = 8;
  796. static const int cfgBenchmark1_Reference = 3499;
  797. //[2] b3DbvtVolume merges
  798. bool cfgBenchmark2_Enable = cfgEnable;
  799. static const int cfgBenchmark2_Iterations = 4;
  800. static const int cfgBenchmark2_Reference = 1945;
  801. //[3] b3DynamicBvh::collideTT
  802. bool cfgBenchmark3_Enable = cfgEnable;
  803. static const int cfgBenchmark3_Iterations = 512;
  804. static const int cfgBenchmark3_Reference = 5485;
  805. //[4] b3DynamicBvh::collideTT self
  806. bool cfgBenchmark4_Enable = cfgEnable;
  807. static const int cfgBenchmark4_Iterations = 512;
  808. static const int cfgBenchmark4_Reference = 2814;
  809. //[5] b3DynamicBvh::collideTT xform
  810. bool cfgBenchmark5_Enable = cfgEnable;
  811. static const int cfgBenchmark5_Iterations = 512;
  812. static const b3Scalar cfgBenchmark5_OffsetScale = 2;
  813. static const int cfgBenchmark5_Reference = 7379;
  814. //[6] b3DynamicBvh::collideTT xform,self
  815. bool cfgBenchmark6_Enable = cfgEnable;
  816. static const int cfgBenchmark6_Iterations = 512;
  817. static const b3Scalar cfgBenchmark6_OffsetScale = 2;
  818. static const int cfgBenchmark6_Reference = 7270;
  819. //[7] b3DynamicBvh::rayTest
  820. bool cfgBenchmark7_Enable = cfgEnable;
  821. static const int cfgBenchmark7_Passes = 32;
  822. static const int cfgBenchmark7_Iterations = 65536;
  823. static const int cfgBenchmark7_Reference = 6307;
  824. //[8] insert/remove
  825. bool cfgBenchmark8_Enable = cfgEnable;
  826. static const int cfgBenchmark8_Passes = 32;
  827. static const int cfgBenchmark8_Iterations = 65536;
  828. static const int cfgBenchmark8_Reference = 2105;
  829. //[9] updates (teleport)
  830. bool cfgBenchmark9_Enable = cfgEnable;
  831. static const int cfgBenchmark9_Passes = 32;
  832. static const int cfgBenchmark9_Iterations = 65536;
  833. static const int cfgBenchmark9_Reference = 1879;
  834. //[10] updates (jitter)
  835. bool cfgBenchmark10_Enable = cfgEnable;
  836. static const b3Scalar cfgBenchmark10_Scale = cfgVolumeCenterScale / 10000;
  837. static const int cfgBenchmark10_Passes = 32;
  838. static const int cfgBenchmark10_Iterations = 65536;
  839. static const int cfgBenchmark10_Reference = 1244;
  840. //[11] optimize (incremental)
  841. bool cfgBenchmark11_Enable = cfgEnable;
  842. static const int cfgBenchmark11_Passes = 64;
  843. static const int cfgBenchmark11_Iterations = 65536;
  844. static const int cfgBenchmark11_Reference = 2510;
  845. //[12] b3DbvtVolume notequal
  846. bool cfgBenchmark12_Enable = cfgEnable;
  847. static const int cfgBenchmark12_Iterations = 32;
  848. static const int cfgBenchmark12_Reference = 3677;
  849. //[13] culling(OCL+fullsort)
  850. bool cfgBenchmark13_Enable = cfgEnable;
  851. static const int cfgBenchmark13_Iterations = 1024;
  852. static const int cfgBenchmark13_Reference = 2231;
  853. //[14] culling(OCL+qsort)
  854. bool cfgBenchmark14_Enable = cfgEnable;
  855. static const int cfgBenchmark14_Iterations = 8192;
  856. static const int cfgBenchmark14_Reference = 3500;
  857. //[15] culling(KDOP+qsort)
  858. bool cfgBenchmark15_Enable = cfgEnable;
  859. static const int cfgBenchmark15_Iterations = 8192;
  860. static const int cfgBenchmark15_Reference = 1151;
  861. //[16] insert/remove batch
  862. bool cfgBenchmark16_Enable = cfgEnable;
  863. static const int cfgBenchmark16_BatchCount = 256;
  864. static const int cfgBenchmark16_Passes = 16384;
  865. static const int cfgBenchmark16_Reference = 5138;
  866. //[17] select
  867. bool cfgBenchmark17_Enable = cfgEnable;
  868. static const int cfgBenchmark17_Iterations = 4;
  869. static const int cfgBenchmark17_Reference = 3390;
  870. b3Clock wallclock;
  871. printf("Benchmarking dbvt...\r\n");
  872. printf("\tWorld scale: %f\r\n", cfgVolumeCenterScale);
  873. printf("\tExtents base: %f\r\n", cfgVolumeExentsBase);
  874. printf("\tExtents range: %f\r\n", cfgVolumeExentsScale);
  875. printf("\tLeaves: %u\r\n", cfgLeaves);
  876. printf("\tsizeof(b3DbvtVolume): %u bytes\r\n", sizeof(b3DbvtVolume));
  877. printf("\tsizeof(b3DbvtNode): %u bytes\r\n", sizeof(b3DbvtNode));
  878. if (cfgBenchmark1_Enable)
  879. { // Benchmark 1
  880. srand(380843);
  881. b3AlignedObjectArray<b3DbvtVolume> volumes;
  882. b3AlignedObjectArray<bool> results;
  883. volumes.resize(cfgLeaves);
  884. results.resize(cfgLeaves);
  885. for (int i = 0; i < cfgLeaves; ++i)
  886. {
  887. volumes[i] = b3DbvtBenchmark::RandVolume(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale);
  888. }
  889. printf("[1] b3DbvtVolume intersections: ");
  890. wallclock.reset();
  891. for (int i = 0; i < cfgBenchmark1_Iterations; ++i)
  892. {
  893. for (int j = 0; j < cfgLeaves; ++j)
  894. {
  895. for (int k = 0; k < cfgLeaves; ++k)
  896. {
  897. results[k] = Intersect(volumes[j], volumes[k]);
  898. }
  899. }
  900. }
  901. const int time = (int)wallclock.getTimeMilliseconds();
  902. printf("%u ms (%i%%)\r\n", time, (time - cfgBenchmark1_Reference) * 100 / time);
  903. }
  904. if (cfgBenchmark2_Enable)
  905. { // Benchmark 2
  906. srand(380843);
  907. b3AlignedObjectArray<b3DbvtVolume> volumes;
  908. b3AlignedObjectArray<b3DbvtVolume> results;
  909. volumes.resize(cfgLeaves);
  910. results.resize(cfgLeaves);
  911. for (int i = 0; i < cfgLeaves; ++i)
  912. {
  913. volumes[i] = b3DbvtBenchmark::RandVolume(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale);
  914. }
  915. printf("[2] b3DbvtVolume merges: ");
  916. wallclock.reset();
  917. for (int i = 0; i < cfgBenchmark2_Iterations; ++i)
  918. {
  919. for (int j = 0; j < cfgLeaves; ++j)
  920. {
  921. for (int k = 0; k < cfgLeaves; ++k)
  922. {
  923. Merge(volumes[j], volumes[k], results[k]);
  924. }
  925. }
  926. }
  927. const int time = (int)wallclock.getTimeMilliseconds();
  928. printf("%u ms (%i%%)\r\n", time, (time - cfgBenchmark2_Reference) * 100 / time);
  929. }
  930. if (cfgBenchmark3_Enable)
  931. { // Benchmark 3
  932. srand(380843);
  933. b3DynamicBvh dbvt[2];
  934. b3DbvtBenchmark::NilPolicy policy;
  935. b3DbvtBenchmark::RandTree(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale, cfgLeaves, dbvt[0]);
  936. b3DbvtBenchmark::RandTree(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale, cfgLeaves, dbvt[1]);
  937. dbvt[0].optimizeTopDown();
  938. dbvt[1].optimizeTopDown();
  939. printf("[3] b3DynamicBvh::collideTT: ");
  940. wallclock.reset();
  941. for (int i = 0; i < cfgBenchmark3_Iterations; ++i)
  942. {
  943. b3DynamicBvh::collideTT(dbvt[0].m_root, dbvt[1].m_root, policy);
  944. }
  945. const int time = (int)wallclock.getTimeMilliseconds();
  946. printf("%u ms (%i%%)\r\n", time, (time - cfgBenchmark3_Reference) * 100 / time);
  947. }
  948. if (cfgBenchmark4_Enable)
  949. { // Benchmark 4
  950. srand(380843);
  951. b3DynamicBvh dbvt;
  952. b3DbvtBenchmark::NilPolicy policy;
  953. b3DbvtBenchmark::RandTree(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale, cfgLeaves, dbvt);
  954. dbvt.optimizeTopDown();
  955. printf("[4] b3DynamicBvh::collideTT self: ");
  956. wallclock.reset();
  957. for (int i = 0; i < cfgBenchmark4_Iterations; ++i)
  958. {
  959. b3DynamicBvh::collideTT(dbvt.m_root, dbvt.m_root, policy);
  960. }
  961. const int time = (int)wallclock.getTimeMilliseconds();
  962. printf("%u ms (%i%%)\r\n", time, (time - cfgBenchmark4_Reference) * 100 / time);
  963. }
  964. if (cfgBenchmark5_Enable)
  965. { // Benchmark 5
  966. srand(380843);
  967. b3DynamicBvh dbvt[2];
  968. b3AlignedObjectArray<b3Transform> transforms;
  969. b3DbvtBenchmark::NilPolicy policy;
  970. transforms.resize(cfgBenchmark5_Iterations);
  971. for (int i = 0; i < transforms.size(); ++i)
  972. {
  973. transforms[i] = b3DbvtBenchmark::RandTransform(cfgVolumeCenterScale * cfgBenchmark5_OffsetScale);
  974. }
  975. b3DbvtBenchmark::RandTree(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale, cfgLeaves, dbvt[0]);
  976. b3DbvtBenchmark::RandTree(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale, cfgLeaves, dbvt[1]);
  977. dbvt[0].optimizeTopDown();
  978. dbvt[1].optimizeTopDown();
  979. printf("[5] b3DynamicBvh::collideTT xform: ");
  980. wallclock.reset();
  981. for (int i = 0; i < cfgBenchmark5_Iterations; ++i)
  982. {
  983. b3DynamicBvh::collideTT(dbvt[0].m_root, dbvt[1].m_root, transforms[i], policy);
  984. }
  985. const int time = (int)wallclock.getTimeMilliseconds();
  986. printf("%u ms (%i%%)\r\n", time, (time - cfgBenchmark5_Reference) * 100 / time);
  987. }
  988. if (cfgBenchmark6_Enable)
  989. { // Benchmark 6
  990. srand(380843);
  991. b3DynamicBvh dbvt;
  992. b3AlignedObjectArray<b3Transform> transforms;
  993. b3DbvtBenchmark::NilPolicy policy;
  994. transforms.resize(cfgBenchmark6_Iterations);
  995. for (int i = 0; i < transforms.size(); ++i)
  996. {
  997. transforms[i] = b3DbvtBenchmark::RandTransform(cfgVolumeCenterScale * cfgBenchmark6_OffsetScale);
  998. }
  999. b3DbvtBenchmark::RandTree(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale, cfgLeaves, dbvt);
  1000. dbvt.optimizeTopDown();
  1001. printf("[6] b3DynamicBvh::collideTT xform,self: ");
  1002. wallclock.reset();
  1003. for (int i = 0; i < cfgBenchmark6_Iterations; ++i)
  1004. {
  1005. b3DynamicBvh::collideTT(dbvt.m_root, dbvt.m_root, transforms[i], policy);
  1006. }
  1007. const int time = (int)wallclock.getTimeMilliseconds();
  1008. printf("%u ms (%i%%)\r\n", time, (time - cfgBenchmark6_Reference) * 100 / time);
  1009. }
  1010. if (cfgBenchmark7_Enable)
  1011. { // Benchmark 7
  1012. srand(380843);
  1013. b3DynamicBvh dbvt;
  1014. b3AlignedObjectArray<b3Vector3> rayorg;
  1015. b3AlignedObjectArray<b3Vector3> raydir;
  1016. b3DbvtBenchmark::NilPolicy policy;
  1017. rayorg.resize(cfgBenchmark7_Iterations);
  1018. raydir.resize(cfgBenchmark7_Iterations);
  1019. for (int i = 0; i < rayorg.size(); ++i)
  1020. {
  1021. rayorg[i] = b3DbvtBenchmark::RandVector3(cfgVolumeCenterScale * 2);
  1022. raydir[i] = b3DbvtBenchmark::RandVector3(cfgVolumeCenterScale * 2);
  1023. }
  1024. b3DbvtBenchmark::RandTree(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale, cfgLeaves, dbvt);
  1025. dbvt.optimizeTopDown();
  1026. printf("[7] b3DynamicBvh::rayTest: ");
  1027. wallclock.reset();
  1028. for (int i = 0; i < cfgBenchmark7_Passes; ++i)
  1029. {
  1030. for (int j = 0; j < cfgBenchmark7_Iterations; ++j)
  1031. {
  1032. b3DynamicBvh::rayTest(dbvt.m_root, rayorg[j], rayorg[j] + raydir[j], policy);
  1033. }
  1034. }
  1035. const int time = (int)wallclock.getTimeMilliseconds();
  1036. unsigned rays = cfgBenchmark7_Passes * cfgBenchmark7_Iterations;
  1037. printf("%u ms (%i%%),(%u r/s)\r\n", time, (time - cfgBenchmark7_Reference) * 100 / time, (rays * 1000) / time);
  1038. }
  1039. if (cfgBenchmark8_Enable)
  1040. { // Benchmark 8
  1041. srand(380843);
  1042. b3DynamicBvh dbvt;
  1043. b3DbvtBenchmark::RandTree(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale, cfgLeaves, dbvt);
  1044. dbvt.optimizeTopDown();
  1045. printf("[8] insert/remove: ");
  1046. wallclock.reset();
  1047. for (int i = 0; i < cfgBenchmark8_Passes; ++i)
  1048. {
  1049. for (int j = 0; j < cfgBenchmark8_Iterations; ++j)
  1050. {
  1051. dbvt.remove(dbvt.insert(b3DbvtBenchmark::RandVolume(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale), 0));
  1052. }
  1053. }
  1054. const int time = (int)wallclock.getTimeMilliseconds();
  1055. const int ir = cfgBenchmark8_Passes * cfgBenchmark8_Iterations;
  1056. printf("%u ms (%i%%),(%u ir/s)\r\n", time, (time - cfgBenchmark8_Reference) * 100 / time, ir * 1000 / time);
  1057. }
  1058. if (cfgBenchmark9_Enable)
  1059. { // Benchmark 9
  1060. srand(380843);
  1061. b3DynamicBvh dbvt;
  1062. b3AlignedObjectArray<const b3DbvtNode*> leaves;
  1063. b3DbvtBenchmark::RandTree(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale, cfgLeaves, dbvt);
  1064. dbvt.optimizeTopDown();
  1065. dbvt.extractLeaves(dbvt.m_root, leaves);
  1066. printf("[9] updates (teleport): ");
  1067. wallclock.reset();
  1068. for (int i = 0; i < cfgBenchmark9_Passes; ++i)
  1069. {
  1070. for (int j = 0; j < cfgBenchmark9_Iterations; ++j)
  1071. {
  1072. dbvt.update(const_cast<b3DbvtNode*>(leaves[rand() % cfgLeaves]),
  1073. b3DbvtBenchmark::RandVolume(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale));
  1074. }
  1075. }
  1076. const int time = (int)wallclock.getTimeMilliseconds();
  1077. const int up = cfgBenchmark9_Passes * cfgBenchmark9_Iterations;
  1078. printf("%u ms (%i%%),(%u u/s)\r\n", time, (time - cfgBenchmark9_Reference) * 100 / time, up * 1000 / time);
  1079. }
  1080. if (cfgBenchmark10_Enable)
  1081. { // Benchmark 10
  1082. srand(380843);
  1083. b3DynamicBvh dbvt;
  1084. b3AlignedObjectArray<const b3DbvtNode*> leaves;
  1085. b3AlignedObjectArray<b3Vector3> vectors;
  1086. vectors.resize(cfgBenchmark10_Iterations);
  1087. for (int i = 0; i < vectors.size(); ++i)
  1088. {
  1089. vectors[i] = (b3DbvtBenchmark::RandVector3() * 2 - b3Vector3(1, 1, 1)) * cfgBenchmark10_Scale;
  1090. }
  1091. b3DbvtBenchmark::RandTree(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale, cfgLeaves, dbvt);
  1092. dbvt.optimizeTopDown();
  1093. dbvt.extractLeaves(dbvt.m_root, leaves);
  1094. printf("[10] updates (jitter): ");
  1095. wallclock.reset();
  1096. for (int i = 0; i < cfgBenchmark10_Passes; ++i)
  1097. {
  1098. for (int j = 0; j < cfgBenchmark10_Iterations; ++j)
  1099. {
  1100. const b3Vector3& d = vectors[j];
  1101. b3DbvtNode* l = const_cast<b3DbvtNode*>(leaves[rand() % cfgLeaves]);
  1102. b3DbvtVolume v = b3DbvtVolume::FromMM(l->volume.Mins() + d, l->volume.Maxs() + d);
  1103. dbvt.update(l, v);
  1104. }
  1105. }
  1106. const int time = (int)wallclock.getTimeMilliseconds();
  1107. const int up = cfgBenchmark10_Passes * cfgBenchmark10_Iterations;
  1108. printf("%u ms (%i%%),(%u u/s)\r\n", time, (time - cfgBenchmark10_Reference) * 100 / time, up * 1000 / time);
  1109. }
  1110. if (cfgBenchmark11_Enable)
  1111. { // Benchmark 11
  1112. srand(380843);
  1113. b3DynamicBvh dbvt;
  1114. b3DbvtBenchmark::RandTree(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale, cfgLeaves, dbvt);
  1115. dbvt.optimizeTopDown();
  1116. printf("[11] optimize (incremental): ");
  1117. wallclock.reset();
  1118. for (int i = 0; i < cfgBenchmark11_Passes; ++i)
  1119. {
  1120. dbvt.optimizeIncremental(cfgBenchmark11_Iterations);
  1121. }
  1122. const int time = (int)wallclock.getTimeMilliseconds();
  1123. const int op = cfgBenchmark11_Passes * cfgBenchmark11_Iterations;
  1124. printf("%u ms (%i%%),(%u o/s)\r\n", time, (time - cfgBenchmark11_Reference) * 100 / time, op / time * 1000);
  1125. }
  1126. if (cfgBenchmark12_Enable)
  1127. { // Benchmark 12
  1128. srand(380843);
  1129. b3AlignedObjectArray<b3DbvtVolume> volumes;
  1130. b3AlignedObjectArray<bool> results;
  1131. volumes.resize(cfgLeaves);
  1132. results.resize(cfgLeaves);
  1133. for (int i = 0; i < cfgLeaves; ++i)
  1134. {
  1135. volumes[i] = b3DbvtBenchmark::RandVolume(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale);
  1136. }
  1137. printf("[12] b3DbvtVolume notequal: ");
  1138. wallclock.reset();
  1139. for (int i = 0; i < cfgBenchmark12_Iterations; ++i)
  1140. {
  1141. for (int j = 0; j < cfgLeaves; ++j)
  1142. {
  1143. for (int k = 0; k < cfgLeaves; ++k)
  1144. {
  1145. results[k] = NotEqual(volumes[j], volumes[k]);
  1146. }
  1147. }
  1148. }
  1149. const int time = (int)wallclock.getTimeMilliseconds();
  1150. printf("%u ms (%i%%)\r\n", time, (time - cfgBenchmark12_Reference) * 100 / time);
  1151. }
  1152. if (cfgBenchmark13_Enable)
  1153. { // Benchmark 13
  1154. srand(380843);
  1155. b3DynamicBvh dbvt;
  1156. b3AlignedObjectArray<b3Vector3> vectors;
  1157. b3DbvtBenchmark::NilPolicy policy;
  1158. vectors.resize(cfgBenchmark13_Iterations);
  1159. for (int i = 0; i < vectors.size(); ++i)
  1160. {
  1161. vectors[i] = (b3DbvtBenchmark::RandVector3() * 2 - b3Vector3(1, 1, 1)).normalized();
  1162. }
  1163. b3DbvtBenchmark::RandTree(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale, cfgLeaves, dbvt);
  1164. dbvt.optimizeTopDown();
  1165. printf("[13] culling(OCL+fullsort): ");
  1166. wallclock.reset();
  1167. for (int i = 0; i < cfgBenchmark13_Iterations; ++i)
  1168. {
  1169. static const b3Scalar offset = 0;
  1170. policy.m_depth = -B3_INFINITY;
  1171. dbvt.collideOCL(dbvt.m_root, &vectors[i], &offset, vectors[i], 1, policy);
  1172. }
  1173. const int time = (int)wallclock.getTimeMilliseconds();
  1174. const int t = cfgBenchmark13_Iterations;
  1175. printf("%u ms (%i%%),(%u t/s)\r\n", time, (time - cfgBenchmark13_Reference) * 100 / time, (t * 1000) / time);
  1176. }
  1177. if (cfgBenchmark14_Enable)
  1178. { // Benchmark 14
  1179. srand(380843);
  1180. b3DynamicBvh dbvt;
  1181. b3AlignedObjectArray<b3Vector3> vectors;
  1182. b3DbvtBenchmark::P14 policy;
  1183. vectors.resize(cfgBenchmark14_Iterations);
  1184. for (int i = 0; i < vectors.size(); ++i)
  1185. {
  1186. vectors[i] = (b3DbvtBenchmark::RandVector3() * 2 - b3Vector3(1, 1, 1)).normalized();
  1187. }
  1188. b3DbvtBenchmark::RandTree(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale, cfgLeaves, dbvt);
  1189. dbvt.optimizeTopDown();
  1190. policy.m_nodes.reserve(cfgLeaves);
  1191. printf("[14] culling(OCL+qsort): ");
  1192. wallclock.reset();
  1193. for (int i = 0; i < cfgBenchmark14_Iterations; ++i)
  1194. {
  1195. static const b3Scalar offset = 0;
  1196. policy.m_nodes.resize(0);
  1197. dbvt.collideOCL(dbvt.m_root, &vectors[i], &offset, vectors[i], 1, policy, false);
  1198. policy.m_nodes.quickSort(b3DbvtBenchmark::P14::sortfnc);
  1199. }
  1200. const int time = (int)wallclock.getTimeMilliseconds();
  1201. const int t = cfgBenchmark14_Iterations;
  1202. printf("%u ms (%i%%),(%u t/s)\r\n", time, (time - cfgBenchmark14_Reference) * 100 / time, (t * 1000) / time);
  1203. }
  1204. if (cfgBenchmark15_Enable)
  1205. { // Benchmark 15
  1206. srand(380843);
  1207. b3DynamicBvh dbvt;
  1208. b3AlignedObjectArray<b3Vector3> vectors;
  1209. b3DbvtBenchmark::P15 policy;
  1210. vectors.resize(cfgBenchmark15_Iterations);
  1211. for (int i = 0; i < vectors.size(); ++i)
  1212. {
  1213. vectors[i] = (b3DbvtBenchmark::RandVector3() * 2 - b3Vector3(1, 1, 1)).normalized();
  1214. }
  1215. b3DbvtBenchmark::RandTree(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale, cfgLeaves, dbvt);
  1216. dbvt.optimizeTopDown();
  1217. policy.m_nodes.reserve(cfgLeaves);
  1218. printf("[15] culling(KDOP+qsort): ");
  1219. wallclock.reset();
  1220. for (int i = 0; i < cfgBenchmark15_Iterations; ++i)
  1221. {
  1222. static const b3Scalar offset = 0;
  1223. policy.m_nodes.resize(0);
  1224. policy.m_axis = vectors[i];
  1225. dbvt.collideKDOP(dbvt.m_root, &vectors[i], &offset, 1, policy);
  1226. policy.m_nodes.quickSort(b3DbvtBenchmark::P15::sortfnc);
  1227. }
  1228. const int time = (int)wallclock.getTimeMilliseconds();
  1229. const int t = cfgBenchmark15_Iterations;
  1230. printf("%u ms (%i%%),(%u t/s)\r\n", time, (time - cfgBenchmark15_Reference) * 100 / time, (t * 1000) / time);
  1231. }
  1232. if (cfgBenchmark16_Enable)
  1233. { // Benchmark 16
  1234. srand(380843);
  1235. b3DynamicBvh dbvt;
  1236. b3AlignedObjectArray<b3DbvtNode*> batch;
  1237. b3DbvtBenchmark::RandTree(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale, cfgLeaves, dbvt);
  1238. dbvt.optimizeTopDown();
  1239. batch.reserve(cfgBenchmark16_BatchCount);
  1240. printf("[16] insert/remove batch(%u): ", cfgBenchmark16_BatchCount);
  1241. wallclock.reset();
  1242. for (int i = 0; i < cfgBenchmark16_Passes; ++i)
  1243. {
  1244. for (int j = 0; j < cfgBenchmark16_BatchCount; ++j)
  1245. {
  1246. batch.push_back(dbvt.insert(b3DbvtBenchmark::RandVolume(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale), 0));
  1247. }
  1248. for (int j = 0; j < cfgBenchmark16_BatchCount; ++j)
  1249. {
  1250. dbvt.remove(batch[j]);
  1251. }
  1252. batch.resize(0);
  1253. }
  1254. const int time = (int)wallclock.getTimeMilliseconds();
  1255. const int ir = cfgBenchmark16_Passes * cfgBenchmark16_BatchCount;
  1256. printf("%u ms (%i%%),(%u bir/s)\r\n", time, (time - cfgBenchmark16_Reference) * 100 / time, int(ir * 1000.0 / time));
  1257. }
  1258. if (cfgBenchmark17_Enable)
  1259. { // Benchmark 17
  1260. srand(380843);
  1261. b3AlignedObjectArray<b3DbvtVolume> volumes;
  1262. b3AlignedObjectArray<int> results;
  1263. b3AlignedObjectArray<int> indices;
  1264. volumes.resize(cfgLeaves);
  1265. results.resize(cfgLeaves);
  1266. indices.resize(cfgLeaves);
  1267. for (int i = 0; i < cfgLeaves; ++i)
  1268. {
  1269. indices[i] = i;
  1270. volumes[i] = b3DbvtBenchmark::RandVolume(cfgVolumeCenterScale, cfgVolumeExentsBase, cfgVolumeExentsScale);
  1271. }
  1272. for (int i = 0; i < cfgLeaves; ++i)
  1273. {
  1274. b3Swap(indices[i], indices[rand() % cfgLeaves]);
  1275. }
  1276. printf("[17] b3DbvtVolume select: ");
  1277. wallclock.reset();
  1278. for (int i = 0; i < cfgBenchmark17_Iterations; ++i)
  1279. {
  1280. for (int j = 0; j < cfgLeaves; ++j)
  1281. {
  1282. for (int k = 0; k < cfgLeaves; ++k)
  1283. {
  1284. const int idx = indices[k];
  1285. results[idx] = Select(volumes[idx], volumes[j], volumes[k]);
  1286. }
  1287. }
  1288. }
  1289. const int time = (int)wallclock.getTimeMilliseconds();
  1290. printf("%u ms (%i%%)\r\n", time, (time - cfgBenchmark17_Reference) * 100 / time);
  1291. }
  1292. printf("\r\n\r\n");
  1293. }
  1294. #endif