btDbvt.cpp 35 KB

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