Welcome to mirror list, hosted at ThFree Co, Russian Federation.

btDbvtBroadphase.cpp « BroadphaseCollision « BulletCollision « src « bullet2 « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e00fc6aa5e38d1d3cc1507d8ac3841515cb37ced (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2007 Erwin Coumans  http://continuousphysics.com/Bullet/

This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, 
including commercial applications, and to alter it and redistribute it freely, 
subject to the following restrictions:

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.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
///btDbvtBroadphase implementation by Nathanael Presson

#include "btDbvtBroadphase.h"

//
// Profiling
//

#if DBVT_BP_PROFILE||DBVT_BP_ENABLE_BENCHMARK
#include <stdio.h>
#endif

#if DBVT_BP_PROFILE
struct	ProfileScope
	{
	__forceinline ProfileScope(btClock& clock,unsigned long& value) :
		m_clock(&clock),m_value(&value),m_base(clock.getTimeMicroseconds())
		{
		}
	__forceinline ~ProfileScope()
		{
		(*m_value)+=m_clock->getTimeMicroseconds()-m_base;
		}
	btClock*		m_clock;
	unsigned long*	m_value;
	unsigned long	m_base;
	};
#define	SPC(_value_)	ProfileScope	spc_scope(m_clock,_value_)
#else
#define	SPC(_value_)
#endif

//
// Helpers
//

//
template <typename T>
static inline void	listappend(T* item,T*& list)
{
item->links[0]=0;
item->links[1]=list;
if(list) list->links[0]=item;
list=item;
}

//
template <typename T>
static inline void	listremove(T* item,T*& list)
{
if(item->links[0]) item->links[0]->links[1]=item->links[1]; else list=item->links[1];
if(item->links[1]) item->links[1]->links[0]=item->links[0];
}

//
template <typename T>
static inline int	listcount(T* root)
{
int	n=0;
while(root) { ++n;root=root->links[1]; }
return(n);
}

//
template <typename T>
static inline void	clear(T& value)
{
static const struct ZeroDummy : T {} zerodummy;
value=zerodummy;
}

//
// Colliders
//

/* Tree collider	*/ 
struct	btDbvtTreeCollider : btDbvt::ICollide
{
btDbvtBroadphase*	pbp;
btDbvtProxy*		proxy;
		btDbvtTreeCollider(btDbvtBroadphase* p) : pbp(p) {}
void	Process(const btDbvtNode* na,const btDbvtNode* nb)
	{
	if(na!=nb)
		{
		btDbvtProxy*	pa=(btDbvtProxy*)na->data;
		btDbvtProxy*	pb=(btDbvtProxy*)nb->data;
		#if DBVT_BP_SORTPAIRS
		if(pa>pb) btSwap(pa,pb);
		#endif
		pbp->m_paircache->addOverlappingPair(pa,pb);
		++pbp->m_newpairs;
		}
	}
void	Process(const btDbvtNode* n)
	{
	Process(n,proxy->leaf);
	}
};

//
// btDbvtBroadphase
//

//
btDbvtBroadphase::btDbvtBroadphase(btOverlappingPairCache* paircache)
{
m_deferedcollide	=	false;
m_needcleanup		=	true;
m_releasepaircache	=	(paircache!=0)?false:true;
m_prediction		=	1/(btScalar)2;
m_stageCurrent		=	0;
m_fixedleft			=	0;
m_fupdates			=	1;
m_dupdates			=	0;
m_cupdates			=	10;
m_newpairs			=	1;
m_updates_call		=	0;
m_updates_done		=	0;
m_updates_ratio		=	0;
m_paircache			=	paircache?
						paircache	:
						new(btAlignedAlloc(sizeof(btHashedOverlappingPairCache),16)) btHashedOverlappingPairCache();
m_gid				=	0;
m_pid				=	0;
m_cid				=	0;
for(int i=0;i<=STAGECOUNT;++i)
	{
	m_stageRoots[i]=0;
	}
#if DBVT_BP_PROFILE
clear(m_profiling);
#endif
}

//
btDbvtBroadphase::~btDbvtBroadphase()
{
if(m_releasepaircache) 
{
	m_paircache->~btOverlappingPairCache();
	btAlignedFree(m_paircache);
}
}

//
btBroadphaseProxy*				btDbvtBroadphase::createProxy(	const btVector3& aabbMin,
																const btVector3& aabbMax,
																int /*shapeType*/,
																void* userPtr,
																short int collisionFilterGroup,
																short int collisionFilterMask,
																btDispatcher* /*dispatcher*/,
																void* /*multiSapProxy*/)
{
btDbvtProxy*		proxy=new(btAlignedAlloc(sizeof(btDbvtProxy),16)) btDbvtProxy(	userPtr,
																					collisionFilterGroup,
																					collisionFilterMask);
proxy->aabb			=	btDbvtVolume::FromMM(aabbMin,aabbMax);
proxy->stage		=	m_stageCurrent;
proxy->m_uniqueId	=	++m_gid;
proxy->leaf			=	m_sets[0].insert(proxy->aabb,proxy);
listappend(proxy,m_stageRoots[m_stageCurrent]);
if(!m_deferedcollide)
	{
	btDbvtTreeCollider	collider(this);
	collider.proxy=proxy;
	btDbvt::collideTV(m_sets[0].m_root,proxy->aabb,collider);
	btDbvt::collideTV(m_sets[1].m_root,proxy->aabb,collider);
	}
return(proxy);
}

//
void							btDbvtBroadphase::destroyProxy(	btBroadphaseProxy* absproxy,
																btDispatcher* dispatcher)
{
btDbvtProxy*	proxy=(btDbvtProxy*)absproxy;
if(proxy->stage==STAGECOUNT)
	m_sets[1].remove(proxy->leaf);
	else
	m_sets[0].remove(proxy->leaf);
listremove(proxy,m_stageRoots[proxy->stage]);
m_paircache->removeOverlappingPairsContainingProxy(proxy,dispatcher);
btAlignedFree(proxy);
m_needcleanup=true;
}

//
void							btDbvtBroadphase::setAabb(		btBroadphaseProxy* absproxy,
																const btVector3& aabbMin,
																const btVector3& aabbMax,
																btDispatcher* /*dispatcher*/)
{
btDbvtProxy*						proxy=(btDbvtProxy*)absproxy;
ATTRIBUTE_ALIGNED16(btDbvtVolume)	aabb=btDbvtVolume::FromMM(aabbMin,aabbMax);
#if DBVT_BP_PREVENTFALSEUPDATE
if(NotEqual(aabb,proxy->leaf->volume))
#endif
	{
	bool	docollide=false;
	if(proxy->stage==STAGECOUNT)
		{/* fixed -> dynamic set	*/ 
		m_sets[1].remove(proxy->leaf);
		proxy->leaf=m_sets[0].insert(aabb,proxy);
		docollide=true;
		}
		else
		{/* dynamic set				*/ 
		++m_updates_call;
		if(Intersect(proxy->leaf->volume,aabb))
			{/* Moving				*/ 
			const btVector3	delta=aabbMin-proxy->aabb.Mins();
			btVector3		velocity(aabb.Extents()*m_prediction);
			if(delta[0]<0) velocity[0]=-velocity[0];
			if(delta[1]<0) velocity[1]=-velocity[1];
			if(delta[2]<0) velocity[2]=-velocity[2];
			if	(
				#ifdef DBVT_BP_MARGIN				
				m_sets[0].update(proxy->leaf,aabb,velocity,DBVT_BP_MARGIN)
				#else
				m_sets[0].update(proxy->leaf,aabb,velocity)
				#endif
				)
				{
				++m_updates_done;
				docollide=true;
				}
			}
			else
			{/* Teleporting			*/ 
			m_sets[0].update(proxy->leaf,aabb);
			++m_updates_done;
			docollide=true;
			}	
		}
	listremove(proxy,m_stageRoots[proxy->stage]);
	proxy->aabb		=	aabb;
	proxy->stage	=	m_stageCurrent;
	listappend(proxy,m_stageRoots[m_stageCurrent]);
	if(docollide)
		{
		m_needcleanup=true;
		if(!m_deferedcollide)
			{
			btDbvtTreeCollider	collider(this);
			btDbvt::collideTT(m_sets[1].m_root,proxy->leaf,collider);
			btDbvt::collideTT(m_sets[0].m_root,proxy->leaf,collider);
			}
		}	
	}
}

//
void							btDbvtBroadphase::calculateOverlappingPairs(btDispatcher* dispatcher)
{
collide(dispatcher);
#if DBVT_BP_PROFILE
if(0==(m_pid%DBVT_BP_PROFILING_RATE))
	{	
	printf("fixed(%u) dynamics(%u) pairs(%u)\r\n",m_sets[1].m_leaves,m_sets[0].m_leaves,m_paircache->getNumOverlappingPairs());
	unsigned int	total=m_profiling.m_total;
	if(total<=0) total=1;
	printf("ddcollide: %u%% (%uus)\r\n",(50+m_profiling.m_ddcollide*100)/total,m_profiling.m_ddcollide/DBVT_BP_PROFILING_RATE);
	printf("fdcollide: %u%% (%uus)\r\n",(50+m_profiling.m_fdcollide*100)/total,m_profiling.m_fdcollide/DBVT_BP_PROFILING_RATE);
	printf("cleanup:   %u%% (%uus)\r\n",(50+m_profiling.m_cleanup*100)/total,m_profiling.m_cleanup/DBVT_BP_PROFILING_RATE);
	printf("total:     %uus\r\n",total/DBVT_BP_PROFILING_RATE);
	const unsigned long	sum=m_profiling.m_ddcollide+
							m_profiling.m_fdcollide+
							m_profiling.m_cleanup;
	printf("leaked: %u%% (%uus)\r\n",100-((50+sum*100)/total),(total-sum)/DBVT_BP_PROFILING_RATE);
	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));
	clear(m_profiling);
	m_clock.reset();
	}
#endif
}

//
void							btDbvtBroadphase::collide(btDispatcher* dispatcher)
{
SPC(m_profiling.m_total);
/* optimize				*/ 
m_sets[0].optimizeIncremental(1+(m_sets[0].m_leaves*m_dupdates)/100);
if(m_fixedleft)
	{
	const int count=1+(m_sets[1].m_leaves*m_fupdates)/100;
	m_sets[1].optimizeIncremental(1+(m_sets[1].m_leaves*m_fupdates)/100);
	m_fixedleft=btMax<int>(0,m_fixedleft-count);
	}
/* dynamic -> fixed set	*/ 
m_stageCurrent=(m_stageCurrent+1)%STAGECOUNT;
btDbvtProxy*	current=m_stageRoots[m_stageCurrent];
if(current)
	{
	btDbvtTreeCollider	collider(this);
	do	{
		btDbvtProxy*	next=current->links[1];
		listremove(current,m_stageRoots[current->stage]);
		listappend(current,m_stageRoots[STAGECOUNT]);
		#if DBVT_BP_ACCURATESLEEPING
		m_paircache->removeOverlappingPairsContainingProxy(current,dispatcher);
		collider.proxy=current;
		btDbvt::collideTV(m_sets[0].m_root,current->aabb,collider);
		btDbvt::collideTV(m_sets[1].m_root,current->aabb,collider);
		#endif
		m_sets[0].remove(current->leaf);
		current->leaf	=	m_sets[1].insert(current->aabb,current);
		current->stage	=	STAGECOUNT;	
		current			=	next;
		} while(current);
	m_fixedleft=m_sets[1].m_leaves;
	m_needcleanup=true;
	}
/* collide dynamics		*/ 
	{
	btDbvtTreeCollider	collider(this);
	if(m_deferedcollide)
		{
		SPC(m_profiling.m_fdcollide);
		btDbvt::collideTT(m_sets[0].m_root,m_sets[1].m_root,collider);
		}
	if(m_deferedcollide)
		{
		SPC(m_profiling.m_ddcollide);
		btDbvt::collideTT(m_sets[0].m_root,m_sets[0].m_root,collider);
		}
	}
/* clean up				*/ 
if(m_needcleanup)
	{
	SPC(m_profiling.m_cleanup);
	btBroadphasePairArray&	pairs=m_paircache->getOverlappingPairArray();
	if(pairs.size()>0)
		{
		const int	ci=pairs.size();
		int			ni=btMin(ci,btMax<int>(m_newpairs,(ci*m_cupdates)/100));
		for(int i=0;i<ni;++i)
			{
			btBroadphasePair&	p=pairs[(m_cid+i)%ci];
			btDbvtProxy*		pa=(btDbvtProxy*)p.m_pProxy0;
			btDbvtProxy*		pb=(btDbvtProxy*)p.m_pProxy1;
			if(!Intersect(pa->leaf->volume,pb->leaf->volume))
				{
				#if DBVT_BP_SORTPAIRS
				if(pa>pb) btSwap(pa,pb);
				#endif
				m_paircache->removeOverlappingPair(pa,pb,dispatcher);
				--ni;--i;
				}
			}
		if(pairs.size()>0) m_cid=(m_cid+ni)%pairs.size(); else m_cid=0;
		}
	}
++m_pid;
m_newpairs=1;
m_needcleanup=false;
if(m_updates_call>0)
	{ m_updates_ratio=m_updates_done/(btScalar)m_updates_call; }
	else
	{ m_updates_ratio=0; }
m_updates_done/=2;
m_updates_call/=2;
}

//
void							btDbvtBroadphase::optimize()
{
m_sets[0].optimizeTopDown();
m_sets[1].optimizeTopDown();
}

//
btOverlappingPairCache*			btDbvtBroadphase::getOverlappingPairCache()
{
return(m_paircache);
}

//
const btOverlappingPairCache*	btDbvtBroadphase::getOverlappingPairCache() const
{
return(m_paircache);
}

//
void							btDbvtBroadphase::getBroadphaseAabb(btVector3& aabbMin,btVector3& aabbMax) const
{

	ATTRIBUTE_ALIGNED16(btDbvtVolume)	bounds;

if(!m_sets[0].empty())
	if(!m_sets[1].empty())	Merge(	m_sets[0].m_root->volume,
									m_sets[1].m_root->volume,bounds);
							else
							bounds=m_sets[0].m_root->volume;
else if(!m_sets[1].empty())	bounds=m_sets[1].m_root->volume;
							else
							bounds=btDbvtVolume::FromCR(btVector3(0,0,0),0);
aabbMin=bounds.Mins();
aabbMax=bounds.Maxs();
}

//
void							btDbvtBroadphase::printStats()
{}

//
#if DBVT_BP_ENABLE_BENCHMARK

struct	btBroadphaseBenchmark
	{
	struct	Experiment
		{
		const char*			name;
		int					object_count;
		int					update_count;
		int					spawn_count;
		int					iterations;
		btScalar			speed;
		btScalar			amplitude;
		};
	struct	Object
		{
		btVector3			center;
		btVector3			extents;
		btBroadphaseProxy*	proxy;
		btScalar			time;
		void				update(btScalar speed,btScalar amplitude,btBroadphaseInterface* pbi)
			{
			time		+=	speed;
			center[0]	=	btCos(time*(btScalar)2.17)*amplitude+
							btSin(time)*amplitude/2;
			center[1]	=	btCos(time*(btScalar)1.38)*amplitude+
							btSin(time)*amplitude;
			center[2]	=	btSin(time*(btScalar)0.777)*amplitude;
			pbi->setAabb(proxy,center-extents,center+extents,0);
			}
		};
	static int		UnsignedRand(int range=RAND_MAX-1)	{ return(rand()%(range+1)); }
	static btScalar	UnitRand()							{ return(UnsignedRand(16384)/(btScalar)16384); }
	static void		OutputTime(const char* name,btClock& c,unsigned count=0)
		{
		const unsigned long	us=c.getTimeMicroseconds();
		const unsigned long	ms=(us+500)/1000;
		const btScalar		sec=us/(btScalar)(1000*1000);
		if(count>0)
			printf("%s : %u us (%u ms), %.2f/s\r\n",name,us,ms,count/sec);
			else
			printf("%s : %u us (%u ms)\r\n",name,us,ms);
		}
	};

void							btDbvtBroadphase::benchmark(btBroadphaseInterface* pbi)
{
static const btBroadphaseBenchmark::Experiment		experiments[]=
	{
	{"1024o.10%",1024,10,0,8192,(btScalar)0.005,(btScalar)100},
	/*{"4096o.10%",4096,10,0,8192,(btScalar)0.005,(btScalar)100},
	{"8192o.10%",8192,10,0,8192,(btScalar)0.005,(btScalar)100},*/
	};
static const int										nexperiments=sizeof(experiments)/sizeof(experiments[0]);
btAlignedObjectArray<btBroadphaseBenchmark::Object*>	objects;
btClock													wallclock;
/* Begin			*/ 
for(int iexp=0;iexp<nexperiments;++iexp)
	{
	const btBroadphaseBenchmark::Experiment&	experiment=experiments[iexp];
	const int									object_count=experiment.object_count;
	const int									update_count=(object_count*experiment.update_count)/100;
	const int									spawn_count=(object_count*experiment.spawn_count)/100;
	const btScalar								speed=experiment.speed;	
	const btScalar								amplitude=experiment.amplitude;
	printf("Experiment #%u '%s':\r\n",iexp,experiment.name);
	printf("\tObjects: %u\r\n",object_count);
	printf("\tUpdate: %u\r\n",update_count);
	printf("\tSpawn: %u\r\n",spawn_count);
	printf("\tSpeed: %f\r\n",speed);
	printf("\tAmplitude: %f\r\n",amplitude);
	srand(180673);
	/* Create objects	*/ 
	wallclock.reset();
	objects.reserve(object_count);
	for(int i=0;i<object_count;++i)
		{
		btBroadphaseBenchmark::Object*	po=new btBroadphaseBenchmark::Object();
		po->center[0]=btBroadphaseBenchmark::UnitRand()*50;
		po->center[1]=btBroadphaseBenchmark::UnitRand()*50;
		po->center[2]=btBroadphaseBenchmark::UnitRand()*50;
		po->extents[0]=btBroadphaseBenchmark::UnitRand()*2+2;
		po->extents[1]=btBroadphaseBenchmark::UnitRand()*2+2;
		po->extents[2]=btBroadphaseBenchmark::UnitRand()*2+2;
		po->time=btBroadphaseBenchmark::UnitRand()*2000;
		po->proxy=pbi->createProxy(po->center-po->extents,po->center+po->extents,0,po,1,1,0,0);
		objects.push_back(po);
		}
	btBroadphaseBenchmark::OutputTime("\tInitialization",wallclock);
	/* First update		*/ 
	wallclock.reset();
	for(int i=0;i<objects.size();++i)
		{
		objects[i]->update(speed,amplitude,pbi);
		}
	btBroadphaseBenchmark::OutputTime("\tFirst update",wallclock);
	/* Updates			*/ 
	wallclock.reset();
	for(int i=0;i<experiment.iterations;++i)
		{
		for(int j=0;j<update_count;++j)
			{				
			objects[j]->update(speed,amplitude,pbi);
			}
		pbi->calculateOverlappingPairs(0);
		}
	btBroadphaseBenchmark::OutputTime("\tUpdate",wallclock,experiment.iterations);
	/* Clean up			*/ 
	wallclock.reset();
	for(int i=0;i<objects.size();++i)
		{
		pbi->destroyProxy(objects[i]->proxy,0);
		delete objects[i];
		}
	objects.resize(0);
	btBroadphaseBenchmark::OutputTime("\tRelease",wallclock);
	}

}
#else
void							btDbvtBroadphase::benchmark(btBroadphaseInterface*)
{}
#endif

#if DBVT_BP_PROFILE
#undef	SPC
#endif