Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

805 строки
22 KiB

  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2013 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. ///b3DynamicBvhBroadphase implementation by Nathanael Presson
  14. #include "b3DynamicBvhBroadphase.h"
  15. #include "b3OverlappingPair.h"
  16. //
  17. // Profiling
  18. //
  19. #if B3_DBVT_BP_PROFILE||B3_DBVT_BP_ENABLE_BENCHMARK
  20. #include <stdio.h>
  21. #endif
  22. #if B3_DBVT_BP_PROFILE
  23. struct b3ProfileScope
  24. {
  25. __forceinline b3ProfileScope(b3Clock& clock,unsigned long& value) :
  26. m_clock(&clock),m_value(&value),m_base(clock.getTimeMicroseconds())
  27. {
  28. }
  29. __forceinline ~b3ProfileScope()
  30. {
  31. (*m_value)+=m_clock->getTimeMicroseconds()-m_base;
  32. }
  33. b3Clock* m_clock;
  34. unsigned long* m_value;
  35. unsigned long m_base;
  36. };
  37. #define b3SPC(_value_) b3ProfileScope spc_scope(m_clock,_value_)
  38. #else
  39. #define b3SPC(_value_)
  40. #endif
  41. //
  42. // Helpers
  43. //
  44. //
  45. template <typename T>
  46. static inline void b3ListAppend(T* item,T*& list)
  47. {
  48. item->links[0]=0;
  49. item->links[1]=list;
  50. if(list) list->links[0]=item;
  51. list=item;
  52. }
  53. //
  54. template <typename T>
  55. static inline void b3ListRemove(T* item,T*& list)
  56. {
  57. if(item->links[0]) item->links[0]->links[1]=item->links[1]; else list=item->links[1];
  58. if(item->links[1]) item->links[1]->links[0]=item->links[0];
  59. }
  60. //
  61. template <typename T>
  62. static inline int b3ListCount(T* root)
  63. {
  64. int n=0;
  65. while(root) { ++n;root=root->links[1]; }
  66. return(n);
  67. }
  68. //
  69. template <typename T>
  70. static inline void b3Clear(T& value)
  71. {
  72. static const struct ZeroDummy : T {} zerodummy;
  73. value=zerodummy;
  74. }
  75. //
  76. // Colliders
  77. //
  78. /* Tree collider */
  79. struct b3DbvtTreeCollider : b3DynamicBvh::ICollide
  80. {
  81. b3DynamicBvhBroadphase* pbp;
  82. b3DbvtProxy* proxy;
  83. b3DbvtTreeCollider(b3DynamicBvhBroadphase* p) : pbp(p) {}
  84. void Process(const b3DbvtNode* na,const b3DbvtNode* nb)
  85. {
  86. if(na!=nb)
  87. {
  88. b3DbvtProxy* pa=(b3DbvtProxy*)na->data;
  89. b3DbvtProxy* pb=(b3DbvtProxy*)nb->data;
  90. #if B3_DBVT_BP_SORTPAIRS
  91. if(pa->m_uniqueId>pb->m_uniqueId)
  92. b3Swap(pa,pb);
  93. #endif
  94. pbp->m_paircache->addOverlappingPair(pa->getUid(),pb->getUid());
  95. ++pbp->m_newpairs;
  96. }
  97. }
  98. void Process(const b3DbvtNode* n)
  99. {
  100. Process(n,proxy->leaf);
  101. }
  102. };
  103. //
  104. // b3DynamicBvhBroadphase
  105. //
  106. //
  107. b3DynamicBvhBroadphase::b3DynamicBvhBroadphase(int proxyCapacity, b3OverlappingPairCache* paircache)
  108. {
  109. m_deferedcollide = false;
  110. m_needcleanup = true;
  111. m_releasepaircache = (paircache!=0)?false:true;
  112. m_prediction = 0;
  113. m_stageCurrent = 0;
  114. m_fixedleft = 0;
  115. m_fupdates = 1;
  116. m_dupdates = 0;
  117. m_cupdates = 10;
  118. m_newpairs = 1;
  119. m_updates_call = 0;
  120. m_updates_done = 0;
  121. m_updates_ratio = 0;
  122. m_paircache = paircache? paircache : new(b3AlignedAlloc(sizeof(b3HashedOverlappingPairCache),16)) b3HashedOverlappingPairCache();
  123. m_pid = 0;
  124. m_cid = 0;
  125. for(int i=0;i<=STAGECOUNT;++i)
  126. {
  127. m_stageRoots[i]=0;
  128. }
  129. #if B3_DBVT_BP_PROFILE
  130. b3Clear(m_profiling);
  131. #endif
  132. m_proxies.resize(proxyCapacity);
  133. }
  134. //
  135. b3DynamicBvhBroadphase::~b3DynamicBvhBroadphase()
  136. {
  137. if(m_releasepaircache)
  138. {
  139. m_paircache->~b3OverlappingPairCache();
  140. b3AlignedFree(m_paircache);
  141. }
  142. }
  143. //
  144. b3BroadphaseProxy* b3DynamicBvhBroadphase::createProxy( const b3Vector3& aabbMin,
  145. const b3Vector3& aabbMax,
  146. int objectId,
  147. void* userPtr,
  148. short int collisionFilterGroup,
  149. short int collisionFilterMask)
  150. {
  151. b3DbvtProxy* mem = &m_proxies[objectId];
  152. b3DbvtProxy* proxy=new(mem) b3DbvtProxy( aabbMin,aabbMax,userPtr,
  153. collisionFilterGroup,
  154. collisionFilterMask);
  155. b3DbvtAabbMm aabb = b3DbvtVolume::FromMM(aabbMin,aabbMax);
  156. //bproxy->aabb = b3DbvtVolume::FromMM(aabbMin,aabbMax);
  157. proxy->stage = m_stageCurrent;
  158. proxy->m_uniqueId = objectId;
  159. proxy->leaf = m_sets[0].insert(aabb,proxy);
  160. b3ListAppend(proxy,m_stageRoots[m_stageCurrent]);
  161. if(!m_deferedcollide)
  162. {
  163. b3DbvtTreeCollider collider(this);
  164. collider.proxy=proxy;
  165. m_sets[0].collideTV(m_sets[0].m_root,aabb,collider);
  166. m_sets[1].collideTV(m_sets[1].m_root,aabb,collider);
  167. }
  168. return(proxy);
  169. }
  170. //
  171. void b3DynamicBvhBroadphase::destroyProxy( b3BroadphaseProxy* absproxy,
  172. b3Dispatcher* dispatcher)
  173. {
  174. b3DbvtProxy* proxy=(b3DbvtProxy*)absproxy;
  175. if(proxy->stage==STAGECOUNT)
  176. m_sets[1].remove(proxy->leaf);
  177. else
  178. m_sets[0].remove(proxy->leaf);
  179. b3ListRemove(proxy,m_stageRoots[proxy->stage]);
  180. m_paircache->removeOverlappingPairsContainingProxy(proxy->getUid(),dispatcher);
  181. m_needcleanup=true;
  182. }
  183. void b3DynamicBvhBroadphase::getAabb(int objectId,b3Vector3& aabbMin, b3Vector3& aabbMax ) const
  184. {
  185. const b3DbvtProxy* proxy=&m_proxies[objectId];
  186. aabbMin = proxy->m_aabbMin;
  187. aabbMax = proxy->m_aabbMax;
  188. }
  189. /*
  190. void b3DynamicBvhBroadphase::getAabb(b3BroadphaseProxy* absproxy,b3Vector3& aabbMin, b3Vector3& aabbMax ) const
  191. {
  192. b3DbvtProxy* proxy=(b3DbvtProxy*)absproxy;
  193. aabbMin = proxy->m_aabbMin;
  194. aabbMax = proxy->m_aabbMax;
  195. }
  196. */
  197. struct BroadphaseRayTester : b3DynamicBvh::ICollide
  198. {
  199. b3BroadphaseRayCallback& m_rayCallback;
  200. BroadphaseRayTester(b3BroadphaseRayCallback& orgCallback)
  201. :m_rayCallback(orgCallback)
  202. {
  203. }
  204. void Process(const b3DbvtNode* leaf)
  205. {
  206. b3DbvtProxy* proxy=(b3DbvtProxy*)leaf->data;
  207. m_rayCallback.process(proxy);
  208. }
  209. };
  210. void b3DynamicBvhBroadphase::rayTest(const b3Vector3& rayFrom,const b3Vector3& rayTo, b3BroadphaseRayCallback& rayCallback,const b3Vector3& aabbMin,const b3Vector3& aabbMax)
  211. {
  212. BroadphaseRayTester callback(rayCallback);
  213. m_sets[0].rayTestInternal( m_sets[0].m_root,
  214. rayFrom,
  215. rayTo,
  216. rayCallback.m_rayDirectionInverse,
  217. rayCallback.m_signs,
  218. rayCallback.m_lambda_max,
  219. aabbMin,
  220. aabbMax,
  221. callback);
  222. m_sets[1].rayTestInternal( m_sets[1].m_root,
  223. rayFrom,
  224. rayTo,
  225. rayCallback.m_rayDirectionInverse,
  226. rayCallback.m_signs,
  227. rayCallback.m_lambda_max,
  228. aabbMin,
  229. aabbMax,
  230. callback);
  231. }
  232. struct BroadphaseAabbTester : b3DynamicBvh::ICollide
  233. {
  234. b3BroadphaseAabbCallback& m_aabbCallback;
  235. BroadphaseAabbTester(b3BroadphaseAabbCallback& orgCallback)
  236. :m_aabbCallback(orgCallback)
  237. {
  238. }
  239. void Process(const b3DbvtNode* leaf)
  240. {
  241. b3DbvtProxy* proxy=(b3DbvtProxy*)leaf->data;
  242. m_aabbCallback.process(proxy);
  243. }
  244. };
  245. void b3DynamicBvhBroadphase::aabbTest(const b3Vector3& aabbMin,const b3Vector3& aabbMax,b3BroadphaseAabbCallback& aabbCallback)
  246. {
  247. BroadphaseAabbTester callback(aabbCallback);
  248. const B3_ATTRIBUTE_ALIGNED16(b3DbvtVolume) bounds=b3DbvtVolume::FromMM(aabbMin,aabbMax);
  249. //process all children, that overlap with the given AABB bounds
  250. m_sets[0].collideTV(m_sets[0].m_root,bounds,callback);
  251. m_sets[1].collideTV(m_sets[1].m_root,bounds,callback);
  252. }
  253. //
  254. void b3DynamicBvhBroadphase::setAabb(int objectId,
  255. const b3Vector3& aabbMin,
  256. const b3Vector3& aabbMax,
  257. b3Dispatcher* /*dispatcher*/)
  258. {
  259. b3DbvtProxy* proxy=&m_proxies[objectId];
  260. // b3DbvtProxy* proxy=(b3DbvtProxy*)absproxy;
  261. B3_ATTRIBUTE_ALIGNED16(b3DbvtVolume) aabb=b3DbvtVolume::FromMM(aabbMin,aabbMax);
  262. #if B3_DBVT_BP_PREVENTFALSEUPDATE
  263. if(b3NotEqual(aabb,proxy->leaf->volume))
  264. #endif
  265. {
  266. bool docollide=false;
  267. if(proxy->stage==STAGECOUNT)
  268. {/* fixed -> dynamic set */
  269. m_sets[1].remove(proxy->leaf);
  270. proxy->leaf=m_sets[0].insert(aabb,proxy);
  271. docollide=true;
  272. }
  273. else
  274. {/* dynamic set */
  275. ++m_updates_call;
  276. if(b3Intersect(proxy->leaf->volume,aabb))
  277. {/* Moving */
  278. const b3Vector3 delta=aabbMin-proxy->m_aabbMin;
  279. b3Vector3 velocity(((proxy->m_aabbMax-proxy->m_aabbMin)/2)*m_prediction);
  280. if(delta[0]<0) velocity[0]=-velocity[0];
  281. if(delta[1]<0) velocity[1]=-velocity[1];
  282. if(delta[2]<0) velocity[2]=-velocity[2];
  283. if (
  284. #ifdef B3_DBVT_BP_MARGIN
  285. m_sets[0].update(proxy->leaf,aabb,velocity,B3_DBVT_BP_MARGIN)
  286. #else
  287. m_sets[0].update(proxy->leaf,aabb,velocity)
  288. #endif
  289. )
  290. {
  291. ++m_updates_done;
  292. docollide=true;
  293. }
  294. }
  295. else
  296. {/* Teleporting */
  297. m_sets[0].update(proxy->leaf,aabb);
  298. ++m_updates_done;
  299. docollide=true;
  300. }
  301. }
  302. b3ListRemove(proxy,m_stageRoots[proxy->stage]);
  303. proxy->m_aabbMin = aabbMin;
  304. proxy->m_aabbMax = aabbMax;
  305. proxy->stage = m_stageCurrent;
  306. b3ListAppend(proxy,m_stageRoots[m_stageCurrent]);
  307. if(docollide)
  308. {
  309. m_needcleanup=true;
  310. if(!m_deferedcollide)
  311. {
  312. b3DbvtTreeCollider collider(this);
  313. m_sets[1].collideTTpersistentStack(m_sets[1].m_root,proxy->leaf,collider);
  314. m_sets[0].collideTTpersistentStack(m_sets[0].m_root,proxy->leaf,collider);
  315. }
  316. }
  317. }
  318. }
  319. //
  320. void b3DynamicBvhBroadphase::setAabbForceUpdate( b3BroadphaseProxy* absproxy,
  321. const b3Vector3& aabbMin,
  322. const b3Vector3& aabbMax,
  323. b3Dispatcher* /*dispatcher*/)
  324. {
  325. b3DbvtProxy* proxy=(b3DbvtProxy*)absproxy;
  326. B3_ATTRIBUTE_ALIGNED16(b3DbvtVolume) aabb=b3DbvtVolume::FromMM(aabbMin,aabbMax);
  327. bool docollide=false;
  328. if(proxy->stage==STAGECOUNT)
  329. {/* fixed -> dynamic set */
  330. m_sets[1].remove(proxy->leaf);
  331. proxy->leaf=m_sets[0].insert(aabb,proxy);
  332. docollide=true;
  333. }
  334. else
  335. {/* dynamic set */
  336. ++m_updates_call;
  337. /* Teleporting */
  338. m_sets[0].update(proxy->leaf,aabb);
  339. ++m_updates_done;
  340. docollide=true;
  341. }
  342. b3ListRemove(proxy,m_stageRoots[proxy->stage]);
  343. proxy->m_aabbMin = aabbMin;
  344. proxy->m_aabbMax = aabbMax;
  345. proxy->stage = m_stageCurrent;
  346. b3ListAppend(proxy,m_stageRoots[m_stageCurrent]);
  347. if(docollide)
  348. {
  349. m_needcleanup=true;
  350. if(!m_deferedcollide)
  351. {
  352. b3DbvtTreeCollider collider(this);
  353. m_sets[1].collideTTpersistentStack(m_sets[1].m_root,proxy->leaf,collider);
  354. m_sets[0].collideTTpersistentStack(m_sets[0].m_root,proxy->leaf,collider);
  355. }
  356. }
  357. }
  358. //
  359. void b3DynamicBvhBroadphase::calculateOverlappingPairs(b3Dispatcher* dispatcher)
  360. {
  361. collide(dispatcher);
  362. #if B3_DBVT_BP_PROFILE
  363. if(0==(m_pid%B3_DBVT_BP_PROFILING_RATE))
  364. {
  365. printf("fixed(%u) dynamics(%u) pairs(%u)\r\n",m_sets[1].m_leaves,m_sets[0].m_leaves,m_paircache->getNumOverlappingPairs());
  366. unsigned int total=m_profiling.m_total;
  367. if(total<=0) total=1;
  368. printf("ddcollide: %u%% (%uus)\r\n",(50+m_profiling.m_ddcollide*100)/total,m_profiling.m_ddcollide/B3_DBVT_BP_PROFILING_RATE);
  369. printf("fdcollide: %u%% (%uus)\r\n",(50+m_profiling.m_fdcollide*100)/total,m_profiling.m_fdcollide/B3_DBVT_BP_PROFILING_RATE);
  370. printf("cleanup: %u%% (%uus)\r\n",(50+m_profiling.m_cleanup*100)/total,m_profiling.m_cleanup/B3_DBVT_BP_PROFILING_RATE);
  371. printf("total: %uus\r\n",total/B3_DBVT_BP_PROFILING_RATE);
  372. const unsigned long sum=m_profiling.m_ddcollide+
  373. m_profiling.m_fdcollide+
  374. m_profiling.m_cleanup;
  375. printf("leaked: %u%% (%uus)\r\n",100-((50+sum*100)/total),(total-sum)/B3_DBVT_BP_PROFILING_RATE);
  376. printf("job counts: %u%%\r\n",(m_profiling.m_jobcount*100)/((m_sets[0].m_leaves+m_sets[1].m_leaves)*B3_DBVT_BP_PROFILING_RATE));
  377. b3Clear(m_profiling);
  378. m_clock.reset();
  379. }
  380. #endif
  381. performDeferredRemoval(dispatcher);
  382. }
  383. void b3DynamicBvhBroadphase::performDeferredRemoval(b3Dispatcher* dispatcher)
  384. {
  385. if (m_paircache->hasDeferredRemoval())
  386. {
  387. b3BroadphasePairArray& overlappingPairArray = m_paircache->getOverlappingPairArray();
  388. //perform a sort, to find duplicates and to sort 'invalid' pairs to the end
  389. overlappingPairArray.quickSort(b3BroadphasePairSortPredicate());
  390. int invalidPair = 0;
  391. int i;
  392. b3BroadphasePair previousPair = b3MakeBroadphasePair(-1,-1);
  393. for (i=0;i<overlappingPairArray.size();i++)
  394. {
  395. b3BroadphasePair& pair = overlappingPairArray[i];
  396. bool isDuplicate = (pair == previousPair);
  397. previousPair = pair;
  398. bool needsRemoval = false;
  399. if (!isDuplicate)
  400. {
  401. //important to perform AABB check that is consistent with the broadphase
  402. b3DbvtProxy* pa=&m_proxies[pair.x];
  403. b3DbvtProxy* pb=&m_proxies[pair.y];
  404. bool hasOverlap = b3Intersect(pa->leaf->volume,pb->leaf->volume);
  405. if (hasOverlap)
  406. {
  407. needsRemoval = false;
  408. } else
  409. {
  410. needsRemoval = true;
  411. }
  412. } else
  413. {
  414. //remove duplicate
  415. needsRemoval = true;
  416. //should have no algorithm
  417. }
  418. if (needsRemoval)
  419. {
  420. m_paircache->cleanOverlappingPair(pair,dispatcher);
  421. pair.x = -1;
  422. pair.y = -1;
  423. invalidPair++;
  424. }
  425. }
  426. //perform a sort, to sort 'invalid' pairs to the end
  427. overlappingPairArray.quickSort(b3BroadphasePairSortPredicate());
  428. overlappingPairArray.resize(overlappingPairArray.size() - invalidPair);
  429. }
  430. }
  431. //
  432. void b3DynamicBvhBroadphase::collide(b3Dispatcher* dispatcher)
  433. {
  434. /*printf("---------------------------------------------------------\n");
  435. printf("m_sets[0].m_leaves=%d\n",m_sets[0].m_leaves);
  436. printf("m_sets[1].m_leaves=%d\n",m_sets[1].m_leaves);
  437. printf("numPairs = %d\n",getOverlappingPairCache()->getNumOverlappingPairs());
  438. {
  439. int i;
  440. for (i=0;i<getOverlappingPairCache()->getNumOverlappingPairs();i++)
  441. {
  442. printf("pair[%d]=(%d,%d),",i,getOverlappingPairCache()->getOverlappingPairArray()[i].m_pProxy0->getUid(),
  443. getOverlappingPairCache()->getOverlappingPairArray()[i].m_pProxy1->getUid());
  444. }
  445. printf("\n");
  446. }
  447. */
  448. b3SPC(m_profiling.m_total);
  449. /* optimize */
  450. m_sets[0].optimizeIncremental(1+(m_sets[0].m_leaves*m_dupdates)/100);
  451. if(m_fixedleft)
  452. {
  453. const int count=1+(m_sets[1].m_leaves*m_fupdates)/100;
  454. m_sets[1].optimizeIncremental(1+(m_sets[1].m_leaves*m_fupdates)/100);
  455. m_fixedleft=b3Max<int>(0,m_fixedleft-count);
  456. }
  457. /* dynamic -> fixed set */
  458. m_stageCurrent=(m_stageCurrent+1)%STAGECOUNT;
  459. b3DbvtProxy* current=m_stageRoots[m_stageCurrent];
  460. if(current)
  461. {
  462. b3DbvtTreeCollider collider(this);
  463. do {
  464. b3DbvtProxy* next=current->links[1];
  465. b3ListRemove(current,m_stageRoots[current->stage]);
  466. b3ListAppend(current,m_stageRoots[STAGECOUNT]);
  467. #if B3_DBVT_BP_ACCURATESLEEPING
  468. m_paircache->removeOverlappingPairsContainingProxy(current,dispatcher);
  469. collider.proxy=current;
  470. b3DynamicBvh::collideTV(m_sets[0].m_root,current->aabb,collider);
  471. b3DynamicBvh::collideTV(m_sets[1].m_root,current->aabb,collider);
  472. #endif
  473. m_sets[0].remove(current->leaf);
  474. B3_ATTRIBUTE_ALIGNED16(b3DbvtVolume) curAabb=b3DbvtVolume::FromMM(current->m_aabbMin,current->m_aabbMax);
  475. current->leaf = m_sets[1].insert(curAabb,current);
  476. current->stage = STAGECOUNT;
  477. current = next;
  478. } while(current);
  479. m_fixedleft=m_sets[1].m_leaves;
  480. m_needcleanup=true;
  481. }
  482. /* collide dynamics */
  483. {
  484. b3DbvtTreeCollider collider(this);
  485. if(m_deferedcollide)
  486. {
  487. b3SPC(m_profiling.m_fdcollide);
  488. m_sets[0].collideTTpersistentStack(m_sets[0].m_root,m_sets[1].m_root,collider);
  489. }
  490. if(m_deferedcollide)
  491. {
  492. b3SPC(m_profiling.m_ddcollide);
  493. m_sets[0].collideTTpersistentStack(m_sets[0].m_root,m_sets[0].m_root,collider);
  494. }
  495. }
  496. /* clean up */
  497. if(m_needcleanup)
  498. {
  499. b3SPC(m_profiling.m_cleanup);
  500. b3BroadphasePairArray& pairs=m_paircache->getOverlappingPairArray();
  501. if(pairs.size()>0)
  502. {
  503. int ni=b3Min(pairs.size(),b3Max<int>(m_newpairs,(pairs.size()*m_cupdates)/100));
  504. for(int i=0;i<ni;++i)
  505. {
  506. b3BroadphasePair& p=pairs[(m_cid+i)%pairs.size()];
  507. b3DbvtProxy* pa=&m_proxies[p.x];
  508. b3DbvtProxy* pb=&m_proxies[p.y];
  509. if(!b3Intersect(pa->leaf->volume,pb->leaf->volume))
  510. {
  511. #if B3_DBVT_BP_SORTPAIRS
  512. if(pa->m_uniqueId>pb->m_uniqueId)
  513. b3Swap(pa,pb);
  514. #endif
  515. m_paircache->removeOverlappingPair(pa->getUid(),pb->getUid(),dispatcher);
  516. --ni;--i;
  517. }
  518. }
  519. if(pairs.size()>0) m_cid=(m_cid+ni)%pairs.size(); else m_cid=0;
  520. }
  521. }
  522. ++m_pid;
  523. m_newpairs=1;
  524. m_needcleanup=false;
  525. if(m_updates_call>0)
  526. { m_updates_ratio=m_updates_done/(b3Scalar)m_updates_call; }
  527. else
  528. { m_updates_ratio=0; }
  529. m_updates_done/=2;
  530. m_updates_call/=2;
  531. }
  532. //
  533. void b3DynamicBvhBroadphase::optimize()
  534. {
  535. m_sets[0].optimizeTopDown();
  536. m_sets[1].optimizeTopDown();
  537. }
  538. //
  539. b3OverlappingPairCache* b3DynamicBvhBroadphase::getOverlappingPairCache()
  540. {
  541. return(m_paircache);
  542. }
  543. //
  544. const b3OverlappingPairCache* b3DynamicBvhBroadphase::getOverlappingPairCache() const
  545. {
  546. return(m_paircache);
  547. }
  548. //
  549. void b3DynamicBvhBroadphase::getBroadphaseAabb(b3Vector3& aabbMin,b3Vector3& aabbMax) const
  550. {
  551. B3_ATTRIBUTE_ALIGNED16(b3DbvtVolume) bounds;
  552. if(!m_sets[0].empty())
  553. if(!m_sets[1].empty()) b3Merge( m_sets[0].m_root->volume,
  554. m_sets[1].m_root->volume,bounds);
  555. else
  556. bounds=m_sets[0].m_root->volume;
  557. else if(!m_sets[1].empty()) bounds=m_sets[1].m_root->volume;
  558. else
  559. bounds=b3DbvtVolume::FromCR(b3MakeVector3(0,0,0),0);
  560. aabbMin=bounds.Mins();
  561. aabbMax=bounds.Maxs();
  562. }
  563. void b3DynamicBvhBroadphase::resetPool(b3Dispatcher* dispatcher)
  564. {
  565. int totalObjects = m_sets[0].m_leaves + m_sets[1].m_leaves;
  566. if (!totalObjects)
  567. {
  568. //reset internal dynamic tree data structures
  569. m_sets[0].clear();
  570. m_sets[1].clear();
  571. m_deferedcollide = false;
  572. m_needcleanup = true;
  573. m_stageCurrent = 0;
  574. m_fixedleft = 0;
  575. m_fupdates = 1;
  576. m_dupdates = 0;
  577. m_cupdates = 10;
  578. m_newpairs = 1;
  579. m_updates_call = 0;
  580. m_updates_done = 0;
  581. m_updates_ratio = 0;
  582. m_pid = 0;
  583. m_cid = 0;
  584. for(int i=0;i<=STAGECOUNT;++i)
  585. {
  586. m_stageRoots[i]=0;
  587. }
  588. }
  589. }
  590. //
  591. void b3DynamicBvhBroadphase::printStats()
  592. {}
  593. //
  594. #if B3_DBVT_BP_ENABLE_BENCHMARK
  595. struct b3BroadphaseBenchmark
  596. {
  597. struct Experiment
  598. {
  599. const char* name;
  600. int object_count;
  601. int update_count;
  602. int spawn_count;
  603. int iterations;
  604. b3Scalar speed;
  605. b3Scalar amplitude;
  606. };
  607. struct Object
  608. {
  609. b3Vector3 center;
  610. b3Vector3 extents;
  611. b3BroadphaseProxy* proxy;
  612. b3Scalar time;
  613. void update(b3Scalar speed,b3Scalar amplitude,b3BroadphaseInterface* pbi)
  614. {
  615. time += speed;
  616. center[0] = b3Cos(time*(b3Scalar)2.17)*amplitude+
  617. b3Sin(time)*amplitude/2;
  618. center[1] = b3Cos(time*(b3Scalar)1.38)*amplitude+
  619. b3Sin(time)*amplitude;
  620. center[2] = b3Sin(time*(b3Scalar)0.777)*amplitude;
  621. pbi->setAabb(proxy,center-extents,center+extents,0);
  622. }
  623. };
  624. static int UnsignedRand(int range=RAND_MAX-1) { return(rand()%(range+1)); }
  625. static b3Scalar UnitRand() { return(UnsignedRand(16384)/(b3Scalar)16384); }
  626. static void OutputTime(const char* name,b3Clock& c,unsigned count=0)
  627. {
  628. const unsigned long us=c.getTimeMicroseconds();
  629. const unsigned long ms=(us+500)/1000;
  630. const b3Scalar sec=us/(b3Scalar)(1000*1000);
  631. if(count>0)
  632. printf("%s : %u us (%u ms), %.2f/s\r\n",name,us,ms,count/sec);
  633. else
  634. printf("%s : %u us (%u ms)\r\n",name,us,ms);
  635. }
  636. };
  637. void b3DynamicBvhBroadphase::benchmark(b3BroadphaseInterface* pbi)
  638. {
  639. static const b3BroadphaseBenchmark::Experiment experiments[]=
  640. {
  641. {"1024o.10%",1024,10,0,8192,(b3Scalar)0.005,(b3Scalar)100},
  642. /*{"4096o.10%",4096,10,0,8192,(b3Scalar)0.005,(b3Scalar)100},
  643. {"8192o.10%",8192,10,0,8192,(b3Scalar)0.005,(b3Scalar)100},*/
  644. };
  645. static const int nexperiments=sizeof(experiments)/sizeof(experiments[0]);
  646. b3AlignedObjectArray<b3BroadphaseBenchmark::Object*> objects;
  647. b3Clock wallclock;
  648. /* Begin */
  649. for(int iexp=0;iexp<nexperiments;++iexp)
  650. {
  651. const b3BroadphaseBenchmark::Experiment& experiment=experiments[iexp];
  652. const int object_count=experiment.object_count;
  653. const int update_count=(object_count*experiment.update_count)/100;
  654. const int spawn_count=(object_count*experiment.spawn_count)/100;
  655. const b3Scalar speed=experiment.speed;
  656. const b3Scalar amplitude=experiment.amplitude;
  657. printf("Experiment #%u '%s':\r\n",iexp,experiment.name);
  658. printf("\tObjects: %u\r\n",object_count);
  659. printf("\tUpdate: %u\r\n",update_count);
  660. printf("\tSpawn: %u\r\n",spawn_count);
  661. printf("\tSpeed: %f\r\n",speed);
  662. printf("\tAmplitude: %f\r\n",amplitude);
  663. srand(180673);
  664. /* Create objects */
  665. wallclock.reset();
  666. objects.reserve(object_count);
  667. for(int i=0;i<object_count;++i)
  668. {
  669. b3BroadphaseBenchmark::Object* po=new b3BroadphaseBenchmark::Object();
  670. po->center[0]=b3BroadphaseBenchmark::UnitRand()*50;
  671. po->center[1]=b3BroadphaseBenchmark::UnitRand()*50;
  672. po->center[2]=b3BroadphaseBenchmark::UnitRand()*50;
  673. po->extents[0]=b3BroadphaseBenchmark::UnitRand()*2+2;
  674. po->extents[1]=b3BroadphaseBenchmark::UnitRand()*2+2;
  675. po->extents[2]=b3BroadphaseBenchmark::UnitRand()*2+2;
  676. po->time=b3BroadphaseBenchmark::UnitRand()*2000;
  677. po->proxy=pbi->createProxy(po->center-po->extents,po->center+po->extents,0,po,1,1,0,0);
  678. objects.push_back(po);
  679. }
  680. b3BroadphaseBenchmark::OutputTime("\tInitialization",wallclock);
  681. /* First update */
  682. wallclock.reset();
  683. for(int i=0;i<objects.size();++i)
  684. {
  685. objects[i]->update(speed,amplitude,pbi);
  686. }
  687. b3BroadphaseBenchmark::OutputTime("\tFirst update",wallclock);
  688. /* Updates */
  689. wallclock.reset();
  690. for(int i=0;i<experiment.iterations;++i)
  691. {
  692. for(int j=0;j<update_count;++j)
  693. {
  694. objects[j]->update(speed,amplitude,pbi);
  695. }
  696. pbi->calculateOverlappingPairs(0);
  697. }
  698. b3BroadphaseBenchmark::OutputTime("\tUpdate",wallclock,experiment.iterations);
  699. /* Clean up */
  700. wallclock.reset();
  701. for(int i=0;i<objects.size();++i)
  702. {
  703. pbi->destroyProxy(objects[i]->proxy,0);
  704. delete objects[i];
  705. }
  706. objects.resize(0);
  707. b3BroadphaseBenchmark::OutputTime("\tRelease",wallclock);
  708. }
  709. }
  710. #else
  711. /*void b3DynamicBvhBroadphase::benchmark(b3BroadphaseInterface*)
  712. {}
  713. */
  714. #endif
  715. #if B3_DBVT_BP_PROFILE
  716. #undef b3SPC
  717. #endif