btDbvt.cpp 37 KB

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