[1963] | 1 | /* |
---|
| 2 | Bullet Continuous Collision Detection and Physics Library |
---|
| 3 | Copyright (c) 2003-2007 Erwin Coumans http://continuousphysics.com/Bullet/ |
---|
| 4 | |
---|
| 5 | This software is provided 'as-is', without any express or implied warranty. |
---|
| 6 | In no event will the authors be held liable for any damages arising from the use of this software. |
---|
| 7 | Permission is granted to anyone to use this software for any purpose, |
---|
| 8 | including commercial applications, and to alter it and redistribute it freely, |
---|
| 9 | subject to the following restrictions: |
---|
| 10 | |
---|
| 11 | 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. |
---|
| 12 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. |
---|
| 13 | 3. This notice may not be removed or altered from any source distribution. |
---|
| 14 | */ |
---|
| 15 | ///btDbvtBroadphase implementation by Nathanael Presson |
---|
| 16 | |
---|
| 17 | #include "btDbvtBroadphase.h" |
---|
| 18 | |
---|
| 19 | // |
---|
| 20 | // Profiling |
---|
| 21 | // |
---|
| 22 | |
---|
| 23 | #if DBVT_BP_PROFILE||DBVT_BP_ENABLE_BENCHMARK |
---|
| 24 | #include <stdio.h> |
---|
| 25 | #endif |
---|
| 26 | |
---|
| 27 | #if DBVT_BP_PROFILE |
---|
| 28 | struct ProfileScope |
---|
[2430] | 29 | { |
---|
| 30 | __forceinline ProfileScope(btClock& clock,unsigned long& value) : |
---|
| 31 | m_clock(&clock),m_value(&value),m_base(clock.getTimeMicroseconds()) |
---|
[1972] | 32 | { |
---|
[2430] | 33 | } |
---|
[1963] | 34 | __forceinline ~ProfileScope() |
---|
[2430] | 35 | { |
---|
[1963] | 36 | (*m_value)+=m_clock->getTimeMicroseconds()-m_base; |
---|
[2430] | 37 | } |
---|
[1963] | 38 | btClock* m_clock; |
---|
| 39 | unsigned long* m_value; |
---|
| 40 | unsigned long m_base; |
---|
[2430] | 41 | }; |
---|
[1963] | 42 | #define SPC(_value_) ProfileScope spc_scope(m_clock,_value_) |
---|
| 43 | #else |
---|
| 44 | #define SPC(_value_) |
---|
| 45 | #endif |
---|
| 46 | |
---|
| 47 | // |
---|
| 48 | // Helpers |
---|
| 49 | // |
---|
| 50 | |
---|
| 51 | // |
---|
| 52 | template <typename T> |
---|
| 53 | static inline void listappend(T* item,T*& list) |
---|
| 54 | { |
---|
[2430] | 55 | item->links[0]=0; |
---|
| 56 | item->links[1]=list; |
---|
| 57 | if(list) list->links[0]=item; |
---|
| 58 | list=item; |
---|
[1963] | 59 | } |
---|
| 60 | |
---|
| 61 | // |
---|
| 62 | template <typename T> |
---|
| 63 | static inline void listremove(T* item,T*& list) |
---|
| 64 | { |
---|
[2430] | 65 | if(item->links[0]) item->links[0]->links[1]=item->links[1]; else list=item->links[1]; |
---|
| 66 | if(item->links[1]) item->links[1]->links[0]=item->links[0]; |
---|
[1963] | 67 | } |
---|
| 68 | |
---|
| 69 | // |
---|
| 70 | template <typename T> |
---|
| 71 | static inline int listcount(T* root) |
---|
| 72 | { |
---|
[2430] | 73 | int n=0; |
---|
| 74 | while(root) { ++n;root=root->links[1]; } |
---|
| 75 | return(n); |
---|
[1963] | 76 | } |
---|
| 77 | |
---|
| 78 | // |
---|
| 79 | template <typename T> |
---|
| 80 | static inline void clear(T& value) |
---|
| 81 | { |
---|
[2430] | 82 | static const struct ZeroDummy : T {} zerodummy; |
---|
| 83 | value=zerodummy; |
---|
[1963] | 84 | } |
---|
| 85 | |
---|
| 86 | // |
---|
| 87 | // Colliders |
---|
| 88 | // |
---|
| 89 | |
---|
| 90 | /* Tree collider */ |
---|
| 91 | struct btDbvtTreeCollider : btDbvt::ICollide |
---|
| 92 | { |
---|
[2430] | 93 | btDbvtBroadphase* pbp; |
---|
| 94 | btDbvtProxy* proxy; |
---|
| 95 | btDbvtTreeCollider(btDbvtBroadphase* p) : pbp(p) {} |
---|
| 96 | void Process(const btDbvtNode* na,const btDbvtNode* nb) |
---|
[1963] | 97 | { |
---|
[2430] | 98 | if(na!=nb) |
---|
[1963] | 99 | { |
---|
[2430] | 100 | btDbvtProxy* pa=(btDbvtProxy*)na->data; |
---|
| 101 | btDbvtProxy* pb=(btDbvtProxy*)nb->data; |
---|
| 102 | #if DBVT_BP_SORTPAIRS |
---|
| 103 | if(pa>pb) btSwap(pa,pb); |
---|
| 104 | #endif |
---|
| 105 | pbp->m_paircache->addOverlappingPair(pa,pb); |
---|
| 106 | ++pbp->m_newpairs; |
---|
[1963] | 107 | } |
---|
| 108 | } |
---|
[2430] | 109 | void Process(const btDbvtNode* n) |
---|
[1963] | 110 | { |
---|
[2430] | 111 | Process(n,proxy->leaf); |
---|
[1963] | 112 | } |
---|
| 113 | }; |
---|
| 114 | |
---|
| 115 | // |
---|
| 116 | // btDbvtBroadphase |
---|
| 117 | // |
---|
| 118 | |
---|
| 119 | // |
---|
| 120 | btDbvtBroadphase::btDbvtBroadphase(btOverlappingPairCache* paircache) |
---|
| 121 | { |
---|
[2430] | 122 | m_deferedcollide = false; |
---|
| 123 | m_needcleanup = true; |
---|
| 124 | m_releasepaircache = (paircache!=0)?false:true; |
---|
| 125 | m_prediction = 1/(btScalar)2; |
---|
| 126 | m_stageCurrent = 0; |
---|
| 127 | m_fixedleft = 0; |
---|
| 128 | m_fupdates = 1; |
---|
| 129 | m_dupdates = 0; |
---|
| 130 | m_cupdates = 10; |
---|
| 131 | m_newpairs = 1; |
---|
| 132 | m_updates_call = 0; |
---|
| 133 | m_updates_done = 0; |
---|
| 134 | m_updates_ratio = 0; |
---|
| 135 | m_paircache = paircache? |
---|
| 136 | paircache : |
---|
| 137 | new(btAlignedAlloc(sizeof(btHashedOverlappingPairCache),16)) btHashedOverlappingPairCache(); |
---|
| 138 | m_gid = 0; |
---|
| 139 | m_pid = 0; |
---|
| 140 | m_cid = 0; |
---|
| 141 | for(int i=0;i<=STAGECOUNT;++i) |
---|
[1963] | 142 | { |
---|
[2430] | 143 | m_stageRoots[i]=0; |
---|
[1963] | 144 | } |
---|
| 145 | #if DBVT_BP_PROFILE |
---|
[2430] | 146 | clear(m_profiling); |
---|
[1963] | 147 | #endif |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | // |
---|
| 151 | btDbvtBroadphase::~btDbvtBroadphase() |
---|
| 152 | { |
---|
[2430] | 153 | if(m_releasepaircache) |
---|
| 154 | { |
---|
| 155 | m_paircache->~btOverlappingPairCache(); |
---|
| 156 | btAlignedFree(m_paircache); |
---|
| 157 | } |
---|
[1963] | 158 | } |
---|
| 159 | |
---|
| 160 | // |
---|
| 161 | btBroadphaseProxy* btDbvtBroadphase::createProxy( const btVector3& aabbMin, |
---|
[2430] | 162 | const btVector3& aabbMax, |
---|
| 163 | int /*shapeType*/, |
---|
| 164 | void* userPtr, |
---|
| 165 | short int collisionFilterGroup, |
---|
| 166 | short int collisionFilterMask, |
---|
| 167 | btDispatcher* /*dispatcher*/, |
---|
| 168 | void* /*multiSapProxy*/) |
---|
[1963] | 169 | { |
---|
[2430] | 170 | btDbvtProxy* proxy=new(btAlignedAlloc(sizeof(btDbvtProxy),16)) btDbvtProxy( aabbMin,aabbMax,userPtr, |
---|
| 171 | collisionFilterGroup, |
---|
| 172 | collisionFilterMask); |
---|
| 173 | |
---|
| 174 | btDbvtAabbMm aabb = btDbvtVolume::FromMM(aabbMin,aabbMax); |
---|
| 175 | |
---|
| 176 | //bproxy->aabb = btDbvtVolume::FromMM(aabbMin,aabbMax); |
---|
| 177 | proxy->stage = m_stageCurrent; |
---|
| 178 | proxy->m_uniqueId = ++m_gid; |
---|
| 179 | proxy->leaf = m_sets[0].insert(aabb,proxy); |
---|
| 180 | listappend(proxy,m_stageRoots[m_stageCurrent]); |
---|
| 181 | if(!m_deferedcollide) |
---|
[1963] | 182 | { |
---|
[2430] | 183 | btDbvtTreeCollider collider(this); |
---|
| 184 | collider.proxy=proxy; |
---|
| 185 | m_sets[0].collideTV(m_sets[0].m_root,aabb,collider); |
---|
| 186 | m_sets[1].collideTV(m_sets[1].m_root,aabb,collider); |
---|
[1963] | 187 | } |
---|
[2430] | 188 | return(proxy); |
---|
[1963] | 189 | } |
---|
| 190 | |
---|
| 191 | // |
---|
| 192 | void btDbvtBroadphase::destroyProxy( btBroadphaseProxy* absproxy, |
---|
[2430] | 193 | btDispatcher* dispatcher) |
---|
[1963] | 194 | { |
---|
[2430] | 195 | btDbvtProxy* proxy=(btDbvtProxy*)absproxy; |
---|
| 196 | if(proxy->stage==STAGECOUNT) |
---|
| 197 | m_sets[1].remove(proxy->leaf); |
---|
[1963] | 198 | else |
---|
[2430] | 199 | m_sets[0].remove(proxy->leaf); |
---|
| 200 | listremove(proxy,m_stageRoots[proxy->stage]); |
---|
| 201 | m_paircache->removeOverlappingPairsContainingProxy(proxy,dispatcher); |
---|
| 202 | btAlignedFree(proxy); |
---|
| 203 | m_needcleanup=true; |
---|
[1963] | 204 | } |
---|
| 205 | |
---|
[2430] | 206 | void btDbvtBroadphase::getAabb(btBroadphaseProxy* absproxy,btVector3& aabbMin, btVector3& aabbMax ) const |
---|
| 207 | { |
---|
| 208 | btDbvtProxy* proxy=(btDbvtProxy*)absproxy; |
---|
| 209 | aabbMin = proxy->m_aabbMin; |
---|
| 210 | aabbMax = proxy->m_aabbMax; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | void btDbvtBroadphase::rayTest(const btVector3& rayFrom,const btVector3& rayTo, btBroadphaseRayCallback& rayCallback,const btVector3& aabbMin,const btVector3& aabbMax) |
---|
| 214 | { |
---|
| 215 | |
---|
| 216 | struct BroadphaseRayTester : btDbvt::ICollide |
---|
| 217 | { |
---|
| 218 | btBroadphaseRayCallback& m_rayCallback; |
---|
| 219 | BroadphaseRayTester(btBroadphaseRayCallback& orgCallback) |
---|
| 220 | :m_rayCallback(orgCallback) |
---|
| 221 | { |
---|
| 222 | } |
---|
| 223 | void Process(const btDbvtNode* leaf) |
---|
| 224 | { |
---|
| 225 | btDbvtProxy* proxy=(btDbvtProxy*)leaf->data; |
---|
| 226 | m_rayCallback.process(proxy); |
---|
| 227 | } |
---|
| 228 | }; |
---|
| 229 | |
---|
| 230 | BroadphaseRayTester callback(rayCallback); |
---|
| 231 | |
---|
| 232 | m_sets[0].rayTestInternal( m_sets[0].m_root, |
---|
| 233 | rayFrom, |
---|
| 234 | rayTo, |
---|
| 235 | rayCallback.m_rayDirectionInverse, |
---|
| 236 | rayCallback.m_signs, |
---|
| 237 | rayCallback.m_lambda_max, |
---|
| 238 | aabbMin, |
---|
| 239 | aabbMax, |
---|
| 240 | callback); |
---|
| 241 | |
---|
| 242 | m_sets[1].rayTestInternal( m_sets[1].m_root, |
---|
| 243 | rayFrom, |
---|
| 244 | rayTo, |
---|
| 245 | rayCallback.m_rayDirectionInverse, |
---|
| 246 | rayCallback.m_signs, |
---|
| 247 | rayCallback.m_lambda_max, |
---|
| 248 | aabbMin, |
---|
| 249 | aabbMax, |
---|
| 250 | callback); |
---|
| 251 | |
---|
| 252 | } |
---|
| 253 | |
---|
[1963] | 254 | // |
---|
| 255 | void btDbvtBroadphase::setAabb( btBroadphaseProxy* absproxy, |
---|
[2430] | 256 | const btVector3& aabbMin, |
---|
| 257 | const btVector3& aabbMax, |
---|
| 258 | btDispatcher* /*dispatcher*/) |
---|
[1963] | 259 | { |
---|
[2430] | 260 | btDbvtProxy* proxy=(btDbvtProxy*)absproxy; |
---|
| 261 | ATTRIBUTE_ALIGNED16(btDbvtVolume) aabb=btDbvtVolume::FromMM(aabbMin,aabbMax); |
---|
[1963] | 262 | #if DBVT_BP_PREVENTFALSEUPDATE |
---|
[2430] | 263 | if(NotEqual(aabb,proxy->leaf->volume)) |
---|
[1963] | 264 | #endif |
---|
| 265 | { |
---|
[2430] | 266 | bool docollide=false; |
---|
| 267 | if(proxy->stage==STAGECOUNT) |
---|
[1963] | 268 | {/* fixed -> dynamic set */ |
---|
[2430] | 269 | m_sets[1].remove(proxy->leaf); |
---|
| 270 | proxy->leaf=m_sets[0].insert(aabb,proxy); |
---|
| 271 | docollide=true; |
---|
[1963] | 272 | } |
---|
| 273 | else |
---|
| 274 | {/* dynamic set */ |
---|
[2430] | 275 | ++m_updates_call; |
---|
| 276 | if(Intersect(proxy->leaf->volume,aabb)) |
---|
[1963] | 277 | {/* Moving */ |
---|
[2430] | 278 | |
---|
| 279 | const btVector3 delta=aabbMin-proxy->m_aabbMin; |
---|
| 280 | btVector3 velocity(((proxy->m_aabbMax-proxy->m_aabbMin)/2)*m_prediction); |
---|
| 281 | if(delta[0]<0) velocity[0]=-velocity[0]; |
---|
| 282 | if(delta[1]<0) velocity[1]=-velocity[1]; |
---|
| 283 | if(delta[2]<0) velocity[2]=-velocity[2]; |
---|
| 284 | if ( |
---|
| 285 | #ifdef DBVT_BP_MARGIN |
---|
| 286 | m_sets[0].update(proxy->leaf,aabb,velocity,DBVT_BP_MARGIN) |
---|
| 287 | #else |
---|
| 288 | m_sets[0].update(proxy->leaf,aabb,velocity) |
---|
| 289 | #endif |
---|
| 290 | ) |
---|
[1963] | 291 | { |
---|
[2430] | 292 | ++m_updates_done; |
---|
| 293 | docollide=true; |
---|
[1963] | 294 | } |
---|
| 295 | } |
---|
| 296 | else |
---|
| 297 | {/* Teleporting */ |
---|
[2430] | 298 | m_sets[0].update(proxy->leaf,aabb); |
---|
| 299 | ++m_updates_done; |
---|
| 300 | docollide=true; |
---|
[1963] | 301 | } |
---|
| 302 | } |
---|
[2430] | 303 | listremove(proxy,m_stageRoots[proxy->stage]); |
---|
| 304 | proxy->m_aabbMin = aabbMin; |
---|
| 305 | proxy->m_aabbMax = aabbMax; |
---|
| 306 | proxy->stage = m_stageCurrent; |
---|
| 307 | listappend(proxy,m_stageRoots[m_stageCurrent]); |
---|
| 308 | if(docollide) |
---|
[1963] | 309 | { |
---|
[2430] | 310 | m_needcleanup=true; |
---|
| 311 | if(!m_deferedcollide) |
---|
[1963] | 312 | { |
---|
[2430] | 313 | btDbvtTreeCollider collider(this); |
---|
| 314 | m_sets[1].collideTTpersistentStack(m_sets[1].m_root,proxy->leaf,collider); |
---|
| 315 | m_sets[0].collideTTpersistentStack(m_sets[0].m_root,proxy->leaf,collider); |
---|
[1963] | 316 | } |
---|
| 317 | } |
---|
| 318 | } |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | // |
---|
| 322 | void btDbvtBroadphase::calculateOverlappingPairs(btDispatcher* dispatcher) |
---|
| 323 | { |
---|
[2430] | 324 | collide(dispatcher); |
---|
[1963] | 325 | #if DBVT_BP_PROFILE |
---|
[2430] | 326 | if(0==(m_pid%DBVT_BP_PROFILING_RATE)) |
---|
[1963] | 327 | { |
---|
[2430] | 328 | printf("fixed(%u) dynamics(%u) pairs(%u)\r\n",m_sets[1].m_leaves,m_sets[0].m_leaves,m_paircache->getNumOverlappingPairs()); |
---|
| 329 | unsigned int total=m_profiling.m_total; |
---|
| 330 | if(total<=0) total=1; |
---|
| 331 | printf("ddcollide: %u%% (%uus)\r\n",(50+m_profiling.m_ddcollide*100)/total,m_profiling.m_ddcollide/DBVT_BP_PROFILING_RATE); |
---|
| 332 | printf("fdcollide: %u%% (%uus)\r\n",(50+m_profiling.m_fdcollide*100)/total,m_profiling.m_fdcollide/DBVT_BP_PROFILING_RATE); |
---|
| 333 | printf("cleanup: %u%% (%uus)\r\n",(50+m_profiling.m_cleanup*100)/total,m_profiling.m_cleanup/DBVT_BP_PROFILING_RATE); |
---|
| 334 | printf("total: %uus\r\n",total/DBVT_BP_PROFILING_RATE); |
---|
| 335 | const unsigned long sum=m_profiling.m_ddcollide+ |
---|
| 336 | m_profiling.m_fdcollide+ |
---|
| 337 | m_profiling.m_cleanup; |
---|
| 338 | printf("leaked: %u%% (%uus)\r\n",100-((50+sum*100)/total),(total-sum)/DBVT_BP_PROFILING_RATE); |
---|
| 339 | printf("job counts: %u%%\r\n",(m_profiling.m_jobcount*100)/((m_sets[0].m_leaves+m_sets[1].m_leaves)*DBVT_BP_PROFILING_RATE)); |
---|
| 340 | clear(m_profiling); |
---|
| 341 | m_clock.reset(); |
---|
[1963] | 342 | } |
---|
| 343 | #endif |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | // |
---|
| 347 | void btDbvtBroadphase::collide(btDispatcher* dispatcher) |
---|
| 348 | { |
---|
[2430] | 349 | SPC(m_profiling.m_total); |
---|
| 350 | /* optimize */ |
---|
| 351 | m_sets[0].optimizeIncremental(1+(m_sets[0].m_leaves*m_dupdates)/100); |
---|
| 352 | if(m_fixedleft) |
---|
[1963] | 353 | { |
---|
[2430] | 354 | const int count=1+(m_sets[1].m_leaves*m_fupdates)/100; |
---|
| 355 | m_sets[1].optimizeIncremental(1+(m_sets[1].m_leaves*m_fupdates)/100); |
---|
| 356 | m_fixedleft=btMax<int>(0,m_fixedleft-count); |
---|
[1963] | 357 | } |
---|
[2430] | 358 | /* dynamic -> fixed set */ |
---|
| 359 | m_stageCurrent=(m_stageCurrent+1)%STAGECOUNT; |
---|
| 360 | btDbvtProxy* current=m_stageRoots[m_stageCurrent]; |
---|
| 361 | if(current) |
---|
[1963] | 362 | { |
---|
[2430] | 363 | btDbvtTreeCollider collider(this); |
---|
| 364 | do { |
---|
| 365 | btDbvtProxy* next=current->links[1]; |
---|
| 366 | listremove(current,m_stageRoots[current->stage]); |
---|
| 367 | listappend(current,m_stageRoots[STAGECOUNT]); |
---|
| 368 | #if DBVT_BP_ACCURATESLEEPING |
---|
| 369 | m_paircache->removeOverlappingPairsContainingProxy(current,dispatcher); |
---|
| 370 | collider.proxy=current; |
---|
| 371 | btDbvt::collideTV(m_sets[0].m_root,current->aabb,collider); |
---|
| 372 | btDbvt::collideTV(m_sets[1].m_root,current->aabb,collider); |
---|
| 373 | #endif |
---|
| 374 | m_sets[0].remove(current->leaf); |
---|
| 375 | ATTRIBUTE_ALIGNED16(btDbvtVolume) curAabb=btDbvtVolume::FromMM(current->m_aabbMin,current->m_aabbMax); |
---|
| 376 | current->leaf = m_sets[1].insert(curAabb,current); |
---|
| 377 | current->stage = STAGECOUNT; |
---|
| 378 | current = next; |
---|
[1963] | 379 | } while(current); |
---|
[2430] | 380 | m_fixedleft=m_sets[1].m_leaves; |
---|
| 381 | m_needcleanup=true; |
---|
[1963] | 382 | } |
---|
[2430] | 383 | /* collide dynamics */ |
---|
[1963] | 384 | { |
---|
[2430] | 385 | btDbvtTreeCollider collider(this); |
---|
| 386 | if(m_deferedcollide) |
---|
[1963] | 387 | { |
---|
[2430] | 388 | SPC(m_profiling.m_fdcollide); |
---|
| 389 | m_sets[0].collideTTpersistentStack(m_sets[0].m_root,m_sets[1].m_root,collider); |
---|
[1963] | 390 | } |
---|
[2430] | 391 | if(m_deferedcollide) |
---|
[1963] | 392 | { |
---|
[2430] | 393 | SPC(m_profiling.m_ddcollide); |
---|
| 394 | m_sets[0].collideTTpersistentStack(m_sets[0].m_root,m_sets[0].m_root,collider); |
---|
[1963] | 395 | } |
---|
| 396 | } |
---|
[2430] | 397 | /* clean up */ |
---|
| 398 | if(m_needcleanup) |
---|
[1963] | 399 | { |
---|
[2430] | 400 | SPC(m_profiling.m_cleanup); |
---|
| 401 | btBroadphasePairArray& pairs=m_paircache->getOverlappingPairArray(); |
---|
| 402 | if(pairs.size()>0) |
---|
[1963] | 403 | { |
---|
[2430] | 404 | const int ci=pairs.size(); |
---|
| 405 | int ni=btMin(ci,btMax<int>(m_newpairs,(ci*m_cupdates)/100)); |
---|
| 406 | for(int i=0;i<ni;++i) |
---|
[1963] | 407 | { |
---|
[2430] | 408 | btBroadphasePair& p=pairs[(m_cid+i)%ci]; |
---|
| 409 | btDbvtProxy* pa=(btDbvtProxy*)p.m_pProxy0; |
---|
| 410 | btDbvtProxy* pb=(btDbvtProxy*)p.m_pProxy1; |
---|
| 411 | if(!Intersect(pa->leaf->volume,pb->leaf->volume)) |
---|
[1963] | 412 | { |
---|
[2430] | 413 | #if DBVT_BP_SORTPAIRS |
---|
| 414 | if(pa>pb) btSwap(pa,pb); |
---|
| 415 | #endif |
---|
| 416 | m_paircache->removeOverlappingPair(pa,pb,dispatcher); |
---|
| 417 | --ni;--i; |
---|
[1963] | 418 | } |
---|
| 419 | } |
---|
[2430] | 420 | if(pairs.size()>0) m_cid=(m_cid+ni)%pairs.size(); else m_cid=0; |
---|
[1963] | 421 | } |
---|
| 422 | } |
---|
[2430] | 423 | ++m_pid; |
---|
| 424 | m_newpairs=1; |
---|
| 425 | m_needcleanup=false; |
---|
| 426 | if(m_updates_call>0) |
---|
[1963] | 427 | { m_updates_ratio=m_updates_done/(btScalar)m_updates_call; } |
---|
| 428 | else |
---|
| 429 | { m_updates_ratio=0; } |
---|
[2430] | 430 | m_updates_done/=2; |
---|
| 431 | m_updates_call/=2; |
---|
[1963] | 432 | } |
---|
| 433 | |
---|
| 434 | // |
---|
| 435 | void btDbvtBroadphase::optimize() |
---|
| 436 | { |
---|
[2430] | 437 | m_sets[0].optimizeTopDown(); |
---|
| 438 | m_sets[1].optimizeTopDown(); |
---|
[1963] | 439 | } |
---|
| 440 | |
---|
| 441 | // |
---|
| 442 | btOverlappingPairCache* btDbvtBroadphase::getOverlappingPairCache() |
---|
| 443 | { |
---|
[2430] | 444 | return(m_paircache); |
---|
[1963] | 445 | } |
---|
| 446 | |
---|
| 447 | // |
---|
| 448 | const btOverlappingPairCache* btDbvtBroadphase::getOverlappingPairCache() const |
---|
| 449 | { |
---|
[2430] | 450 | return(m_paircache); |
---|
[1963] | 451 | } |
---|
| 452 | |
---|
| 453 | // |
---|
| 454 | void btDbvtBroadphase::getBroadphaseAabb(btVector3& aabbMin,btVector3& aabbMax) const |
---|
| 455 | { |
---|
| 456 | |
---|
| 457 | ATTRIBUTE_ALIGNED16(btDbvtVolume) bounds; |
---|
| 458 | |
---|
[2430] | 459 | if(!m_sets[0].empty()) |
---|
| 460 | if(!m_sets[1].empty()) Merge( m_sets[0].m_root->volume, |
---|
| 461 | m_sets[1].m_root->volume,bounds); |
---|
| 462 | else |
---|
| 463 | bounds=m_sets[0].m_root->volume; |
---|
| 464 | else if(!m_sets[1].empty()) bounds=m_sets[1].m_root->volume; |
---|
| 465 | else |
---|
| 466 | bounds=btDbvtVolume::FromCR(btVector3(0,0,0),0); |
---|
| 467 | aabbMin=bounds.Mins(); |
---|
| 468 | aabbMax=bounds.Maxs(); |
---|
[1963] | 469 | } |
---|
| 470 | |
---|
| 471 | // |
---|
| 472 | void btDbvtBroadphase::printStats() |
---|
| 473 | {} |
---|
| 474 | |
---|
| 475 | // |
---|
| 476 | #if DBVT_BP_ENABLE_BENCHMARK |
---|
| 477 | |
---|
| 478 | struct btBroadphaseBenchmark |
---|
[2430] | 479 | { |
---|
| 480 | struct Experiment |
---|
[1972] | 481 | { |
---|
[1963] | 482 | const char* name; |
---|
| 483 | int object_count; |
---|
| 484 | int update_count; |
---|
| 485 | int spawn_count; |
---|
| 486 | int iterations; |
---|
| 487 | btScalar speed; |
---|
| 488 | btScalar amplitude; |
---|
[2430] | 489 | }; |
---|
[1963] | 490 | struct Object |
---|
[2430] | 491 | { |
---|
[1963] | 492 | btVector3 center; |
---|
| 493 | btVector3 extents; |
---|
| 494 | btBroadphaseProxy* proxy; |
---|
| 495 | btScalar time; |
---|
| 496 | void update(btScalar speed,btScalar amplitude,btBroadphaseInterface* pbi) |
---|
[2430] | 497 | { |
---|
[1963] | 498 | time += speed; |
---|
| 499 | center[0] = btCos(time*(btScalar)2.17)*amplitude+ |
---|
[2430] | 500 | btSin(time)*amplitude/2; |
---|
[1963] | 501 | center[1] = btCos(time*(btScalar)1.38)*amplitude+ |
---|
[2430] | 502 | btSin(time)*amplitude; |
---|
[1963] | 503 | center[2] = btSin(time*(btScalar)0.777)*amplitude; |
---|
| 504 | pbi->setAabb(proxy,center-extents,center+extents,0); |
---|
[2430] | 505 | } |
---|
| 506 | }; |
---|
[1963] | 507 | static int UnsignedRand(int range=RAND_MAX-1) { return(rand()%(range+1)); } |
---|
| 508 | static btScalar UnitRand() { return(UnsignedRand(16384)/(btScalar)16384); } |
---|
| 509 | static void OutputTime(const char* name,btClock& c,unsigned count=0) |
---|
[2430] | 510 | { |
---|
[1963] | 511 | const unsigned long us=c.getTimeMicroseconds(); |
---|
| 512 | const unsigned long ms=(us+500)/1000; |
---|
| 513 | const btScalar sec=us/(btScalar)(1000*1000); |
---|
| 514 | if(count>0) |
---|
| 515 | printf("%s : %u us (%u ms), %.2f/s\r\n",name,us,ms,count/sec); |
---|
[2430] | 516 | else |
---|
[1963] | 517 | printf("%s : %u us (%u ms)\r\n",name,us,ms); |
---|
[2430] | 518 | } |
---|
| 519 | }; |
---|
[1963] | 520 | |
---|
| 521 | void btDbvtBroadphase::benchmark(btBroadphaseInterface* pbi) |
---|
| 522 | { |
---|
[2430] | 523 | static const btBroadphaseBenchmark::Experiment experiments[]= |
---|
[1963] | 524 | { |
---|
[2430] | 525 | {"1024o.10%",1024,10,0,8192,(btScalar)0.005,(btScalar)100}, |
---|
| 526 | /*{"4096o.10%",4096,10,0,8192,(btScalar)0.005,(btScalar)100}, |
---|
| 527 | {"8192o.10%",8192,10,0,8192,(btScalar)0.005,(btScalar)100},*/ |
---|
[1963] | 528 | }; |
---|
[2430] | 529 | static const int nexperiments=sizeof(experiments)/sizeof(experiments[0]); |
---|
| 530 | btAlignedObjectArray<btBroadphaseBenchmark::Object*> objects; |
---|
| 531 | btClock wallclock; |
---|
| 532 | /* Begin */ |
---|
| 533 | for(int iexp=0;iexp<nexperiments;++iexp) |
---|
[1963] | 534 | { |
---|
[2430] | 535 | const btBroadphaseBenchmark::Experiment& experiment=experiments[iexp]; |
---|
| 536 | const int object_count=experiment.object_count; |
---|
| 537 | const int update_count=(object_count*experiment.update_count)/100; |
---|
| 538 | const int spawn_count=(object_count*experiment.spawn_count)/100; |
---|
| 539 | const btScalar speed=experiment.speed; |
---|
| 540 | const btScalar amplitude=experiment.amplitude; |
---|
| 541 | printf("Experiment #%u '%s':\r\n",iexp,experiment.name); |
---|
| 542 | printf("\tObjects: %u\r\n",object_count); |
---|
| 543 | printf("\tUpdate: %u\r\n",update_count); |
---|
| 544 | printf("\tSpawn: %u\r\n",spawn_count); |
---|
| 545 | printf("\tSpeed: %f\r\n",speed); |
---|
| 546 | printf("\tAmplitude: %f\r\n",amplitude); |
---|
| 547 | srand(180673); |
---|
| 548 | /* Create objects */ |
---|
| 549 | wallclock.reset(); |
---|
| 550 | objects.reserve(object_count); |
---|
| 551 | for(int i=0;i<object_count;++i) |
---|
[1963] | 552 | { |
---|
[2430] | 553 | btBroadphaseBenchmark::Object* po=new btBroadphaseBenchmark::Object(); |
---|
| 554 | po->center[0]=btBroadphaseBenchmark::UnitRand()*50; |
---|
| 555 | po->center[1]=btBroadphaseBenchmark::UnitRand()*50; |
---|
| 556 | po->center[2]=btBroadphaseBenchmark::UnitRand()*50; |
---|
| 557 | po->extents[0]=btBroadphaseBenchmark::UnitRand()*2+2; |
---|
| 558 | po->extents[1]=btBroadphaseBenchmark::UnitRand()*2+2; |
---|
| 559 | po->extents[2]=btBroadphaseBenchmark::UnitRand()*2+2; |
---|
| 560 | po->time=btBroadphaseBenchmark::UnitRand()*2000; |
---|
| 561 | po->proxy=pbi->createProxy(po->center-po->extents,po->center+po->extents,0,po,1,1,0,0); |
---|
| 562 | objects.push_back(po); |
---|
[1963] | 563 | } |
---|
[2430] | 564 | btBroadphaseBenchmark::OutputTime("\tInitialization",wallclock); |
---|
| 565 | /* First update */ |
---|
| 566 | wallclock.reset(); |
---|
| 567 | for(int i=0;i<objects.size();++i) |
---|
[1963] | 568 | { |
---|
[2430] | 569 | objects[i]->update(speed,amplitude,pbi); |
---|
[1963] | 570 | } |
---|
[2430] | 571 | btBroadphaseBenchmark::OutputTime("\tFirst update",wallclock); |
---|
| 572 | /* Updates */ |
---|
| 573 | wallclock.reset(); |
---|
| 574 | for(int i=0;i<experiment.iterations;++i) |
---|
[1963] | 575 | { |
---|
[2430] | 576 | for(int j=0;j<update_count;++j) |
---|
[1963] | 577 | { |
---|
[2430] | 578 | objects[j]->update(speed,amplitude,pbi); |
---|
[1963] | 579 | } |
---|
[2430] | 580 | pbi->calculateOverlappingPairs(0); |
---|
[1963] | 581 | } |
---|
[2430] | 582 | btBroadphaseBenchmark::OutputTime("\tUpdate",wallclock,experiment.iterations); |
---|
| 583 | /* Clean up */ |
---|
| 584 | wallclock.reset(); |
---|
| 585 | for(int i=0;i<objects.size();++i) |
---|
[1963] | 586 | { |
---|
[2430] | 587 | pbi->destroyProxy(objects[i]->proxy,0); |
---|
| 588 | delete objects[i]; |
---|
[1963] | 589 | } |
---|
[2430] | 590 | objects.resize(0); |
---|
| 591 | btBroadphaseBenchmark::OutputTime("\tRelease",wallclock); |
---|
[1963] | 592 | } |
---|
| 593 | |
---|
| 594 | } |
---|
| 595 | #else |
---|
| 596 | void btDbvtBroadphase::benchmark(btBroadphaseInterface*) |
---|
| 597 | {} |
---|
| 598 | #endif |
---|
| 599 | |
---|
| 600 | #if DBVT_BP_PROFILE |
---|
| 601 | #undef SPC |
---|
| 602 | #endif |
---|