removed collision template, some code style cleanup, added btDbvhBroadphase to BenchmarkDemo

This commit is contained in:
erwin.coumans
2008-05-06 02:45:56 +00:00
parent 1d5050f584
commit b9acc820d6
7 changed files with 369 additions and 249 deletions

View File

@@ -25,7 +25,7 @@ subject to the following restrictions:
#include <stdio.h> //printf debugging #include <stdio.h> //printf debugging
#include "Taru.mdl" #include "Taru.mdl"
#include "landscape.mdl" #include "landscape.mdl"
#include "BulletCollision/BroadphaseCollision/btDbvtBroadphase.h"
@@ -272,7 +272,9 @@ void BenchmarkDemo::initPhysics()
///Don't make the world AABB size too large, it will harm simulation quality and performance ///Don't make the world AABB size too large, it will harm simulation quality and performance
btVector3 worldAabbMin(-10000,-10000,-10000); btVector3 worldAabbMin(-10000,-10000,-10000);
btVector3 worldAabbMax(10000,10000,10000); btVector3 worldAabbMax(10000,10000,10000);
m_overlappingPairCache = new btAxisSweep3(worldAabbMin,worldAabbMax,3500); //m_overlappingPairCache = new btAxisSweep3(worldAabbMin,worldAabbMax,3500);
m_overlappingPairCache = new btDbvtBroadphase();
///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded) ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver; btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver;

View File

@@ -26,13 +26,13 @@ int main(int argc,char** argv)
{ {
GLDebugDrawer gDebugDrawer; GLDebugDrawer gDebugDrawer;
// BenchmarkDemo1 benchmarkDemo; BenchmarkDemo1 benchmarkDemo;
// BenchmarkDemo2 benchmarkDemo; // BenchmarkDemo2 benchmarkDemo;
// BenchmarkDemo3 benchmarkDemo; // BenchmarkDemo3 benchmarkDemo;
// BenchmarkDemo4 benchmarkDemo; // BenchmarkDemo4 benchmarkDemo;
// BenchmarkDemo5 benchmarkDemo; // BenchmarkDemo5 benchmarkDemo;
// BenchmarkDemo6 benchmarkDemo; // BenchmarkDemo6 benchmarkDemo;
BenchmarkDemo7 benchmarkDemo; // BenchmarkDemo7 benchmarkDemo;
benchmarkDemo.initPhysics(); benchmarkDemo.initPhysics();

View File

@@ -279,7 +279,7 @@ static btDbvt::Node* topdown(btDbvt* pdbvt,
{ {
if((splitcount[i][0]>0)&&(splitcount[i][1]>0)) if((splitcount[i][0]>0)&&(splitcount[i][1]>0))
{ {
const int midp=abs(splitcount[i][0]-splitcount[i][1]); const int midp=btFabs(splitcount[i][0]-splitcount[i][1]);
if(midp<bestmidp) if(midp<bestmidp)
{ {
bestaxis=i; bestaxis=i;
@@ -470,25 +470,25 @@ void btDbvt::remove(Node* leaf)
void btDbvt::collide(btDbvt* tree, void btDbvt::collide(btDbvt* tree,
ICollide* icollide) const ICollide* icollide) const
{ {
collideGeneric(tree,GCollide(icollide)); collideGeneric(tree,icollide);
} }
// //
void btDbvt::collide(btDbvt::Node* node, void btDbvt::collide(btDbvt::Node* node,
ICollide* icollide) const ICollide* icollide) const
{ {
collideGeneric(node,GCollide(icollide)); collideGeneric(node,icollide);
} }
// //
void btDbvt::collide(const Volume& volume, void btDbvt::collide(const Volume& volume,
ICollide* icollide) const ICollide* icollide) const
{ {
collideGeneric(volume,GCollide(icollide)); collideGeneric(volume,icollide);
} }
// //
void btDbvt::collide(ICollide* icollide) const void btDbvt::collide(ICollide* icollide) const
{ {
collideGeneric(GCollide(icollide)); collideGeneric(icollide);
} }

View File

@@ -87,18 +87,9 @@ struct btDbvt
/* ICollide */ /* ICollide */
struct ICollide struct ICollide
{ {
virtual void Process(const Node*,const Node*) {} virtual void Process(const Node*,const Node*)=0;
virtual void Process(const Node*) {} virtual void Process(const Node*)=0;
virtual bool Descent(const Node*) { return(false); } virtual bool Descent(const Node*)=0;
};
/* GCollide */
struct GCollide
{
ICollide* icollide;
GCollide(ICollide* ic) : icollide(ic) {}
void Process(const Node* a,const Node* b) { icollide->Process(a,b); }
void Process(const Node* a) { icollide->Process(a); }
bool Descent(const Node* a) { return(icollide->Descent(a)); }
}; };
// Constants // Constants
@@ -137,14 +128,14 @@ struct btDbvt
ICollide* icollide) const; ICollide* icollide) const;
void collide(ICollide* icollide) const; void collide(ICollide* icollide) const;
// Generics : T must implement ICollide // Generics : T must implement ICollide
template <typename T>
void collideGeneric( btDbvt* tree,T& policy) const; void collideGeneric( btDbvt* tree,ICollide* policy) const;
template <typename T>
void collideGeneric( btDbvt::Node* node,T& policy) const; void collideGeneric( btDbvt::Node* node,ICollide* policy) const;
template <typename T>
void collideGeneric(const Volume& volume,T& policy) const; void collideGeneric(const Volume& volume,ICollide* policy) const;
template <typename T>
void collideGeneric(T& policy) const; void collideGeneric(ICollide* policy) const;
// //
private: private:
btDbvt(const btDbvt&) {} btDbvt(const btDbvt&) {}
@@ -300,8 +291,7 @@ inline bool NotEqual( const btDbvtAabbMm& a,
// //
// //
template <typename T> inline void btDbvt::collideGeneric( btDbvt::Node* node,ICollide* policy) const
inline void btDbvt::collideGeneric( btDbvt::Node* node,T& policy) const
{ {
if(m_root&&node) if(m_root&&node)
{ {
@@ -346,7 +336,7 @@ inline void btDbvt::collideGeneric( btDbvt::Node* node,T& policy) const
} }
else else
{ {
policy.Process(p.a,p.b); policy->Process(p.a,p.b);
} }
} }
} }
@@ -355,15 +345,13 @@ inline void btDbvt::collideGeneric( btDbvt::Node* node,T& policy) const
} }
// //
template <typename T> inline void btDbvt::collideGeneric( btDbvt* tree,ICollide* policy) const
inline void btDbvt::collideGeneric( btDbvt* tree,T& policy) const
{ {
collideGeneric<T>(tree->m_root,policy); collideGeneric(tree->m_root,policy);
} }
// //
template <typename T> inline void btDbvt::collideGeneric(const Volume& volume,ICollide* policy) const
inline void btDbvt::collideGeneric(const Volume& volume,T& policy) const
{ {
if(m_root) if(m_root)
{ {
@@ -382,7 +370,7 @@ inline void btDbvt::collideGeneric(const Volume& volume,T& policy) const
} }
else else
{ {
policy.Process(n); policy->Process(n);
} }
} }
} while(stack.size()>0); } while(stack.size()>0);
@@ -390,8 +378,8 @@ inline void btDbvt::collideGeneric(const Volume& volume,T& policy) const
} }
// //
template <typename T>
inline void btDbvt::collideGeneric(T& policy) const inline void btDbvt::collideGeneric(ICollide* policy) const
{ {
if(m_root) if(m_root)
{ {
@@ -401,12 +389,12 @@ inline void btDbvt::collideGeneric(T& policy) const
do { do {
const Node* n=stack[stack.size()-1]; const Node* n=stack[stack.size()-1];
stack.pop_back(); stack.pop_back();
if(policy.Descent(n)) if(policy->Descent(n))
{ {
if(n->isinternal()) if(n->isinternal())
{ stack.push_back(n->childs[0]);stack.push_back(n->childs[1]); } { stack.push_back(n->childs[0]);stack.push_back(n->childs[1]); }
else else
{ policy.Process(n); } { policy->Process(n); }
} }
} while(stack.size()>0); } while(stack.size()>0);
} }

View File

@@ -103,7 +103,16 @@ struct btDbvtBroadphaseCollider : btDbvt::ICollide
btDbvtBroadphase* pbp; btDbvtBroadphase* pbp;
int pid; int pid;
btDbvtBroadphaseCollider(btDbvtBroadphase* p,int id) : pbp(p),pid(id) {} btDbvtBroadphaseCollider(btDbvtBroadphase* p,int id) : pbp(p),pid(id) {}
void Process(const btDbvt::Node* na,const btDbvt::Node* nb)
virtual void Process(const btDbvt::Node* na)
{
}
virtual bool Descent(const btDbvt::Node*)
{
return false;
}
virtual void Process(const btDbvt::Node* na,const btDbvt::Node* nb)
{ {
btDbvtProxy* pa=(btDbvtProxy*)na->data; btDbvtProxy* pa=(btDbvtProxy*)na->data;
btDbvtProxy* pb=(btDbvtProxy*)nb->data; btDbvtProxy* pb=(btDbvtProxy*)nb->data;
@@ -112,7 +121,8 @@ void Process(const btDbvt::Node* na,const btDbvt::Node* nb)
#endif #endif
{ {
btBroadphasePair* pp=pbp->m_paircache->addOverlappingPair(pa,pb); btBroadphasePair* pp=pbp->m_paircache->addOverlappingPair(pa,pb);
if(pp) pp->m_userInfo=*(void**)&pid; if(pp)
pp->m_userInfo=*(void**)&pid;
} }
} }
}; };
@@ -124,12 +134,15 @@ void Process(const btDbvt::Node* na,const btDbvt::Node* nb)
// //
btDbvtBroadphase::btDbvtBroadphase() btDbvtBroadphase::btDbvtBroadphase()
{ {
m_invalidPair = 0;
m_fcursor = 0; m_fcursor = 0;
m_dcursor = 0; m_dcursor = 0;
m_stageCurrent = 0; m_stageCurrent = 0;
m_fupdates = 1; m_fupdates = 1;
m_dupdates = 0; m_dupdates = 0;
//m_paircache = new btSortedOverlappingPairCache();
m_paircache = new btHashedOverlappingPairCache(); m_paircache = new btHashedOverlappingPairCache();
m_gid = 0; m_gid = 0;
m_pid = 0; m_pid = 0;
for(int i=0;i<=STAGECOUNT;++i) for(int i=0;i<=STAGECOUNT;++i)
@@ -239,7 +252,8 @@ if(m_stageRoots[m_stageCurrent]&&(m_dupdates>0))
const int count=1+(m_sets[0].m_leafs*m_dupdates)/100; const int count=1+(m_sets[0].m_leafs*m_dupdates)/100;
for(int i=0;i<count;++i) for(int i=0;i<count;++i)
{ {
if(!m_dcursor) m_dcursor=m_stageRoots[m_stageCurrent]; if(!m_dcursor)
m_dcursor=m_stageRoots[m_stageCurrent];
m_sets[0].update(m_dcursor->leaf); m_sets[0].update(m_dcursor->leaf);
m_dcursor=m_dcursor->links[1]; m_dcursor=m_dcursor->links[1];
} }
@@ -255,7 +269,7 @@ if(current)
if(m_dcursor==current) m_dcursor=0; if(m_dcursor==current) m_dcursor=0;
listremove(current,m_stageRoots[current->stage]); listremove(current,m_stageRoots[current->stage]);
listappend(current,m_stageRoots[STAGECOUNT]); listappend(current,m_stageRoots[STAGECOUNT]);
m_sets[1].collideGeneric(current->leaf,collider); m_sets[1].collideGeneric(current->leaf,&collider);
m_sets[0].remove(current->leaf); m_sets[0].remove(current->leaf);
current->leaf = m_sets[1].insert(current->aabb,current); current->leaf = m_sets[1].insert(current->aabb,current);
current->stage = STAGECOUNT; current->stage = STAGECOUNT;
@@ -277,15 +291,18 @@ if(m_stageRoots[STAGECOUNT]&&(m_fupdates>0))
btDbvtBroadphaseCollider collider(this,m_pid); btDbvtBroadphaseCollider collider(this,m_pid);
{ {
SPC(m_profiling.m_fdcollide); SPC(m_profiling.m_fdcollide);
m_sets[0].collideGeneric(&m_sets[1],collider); m_sets[0].collideGeneric(&m_sets[1],&collider);
} }
{ {
SPC(m_profiling.m_ddcollide); SPC(m_profiling.m_ddcollide);
m_sets[0].collideGeneric(&m_sets[0],collider); m_sets[0].collideGeneric(&m_sets[0],&collider);
} }
/* clean up */ /* clean up */
///m_paircache->processAllOverlappingPairs(0,dispatcher);
{ {
SPC(m_profiling.m_cleanup); SPC(m_profiling.m_cleanup);
if (!m_paircache->hasDeferredRemoval())
{
btBroadphasePairArray& pairs=m_paircache->getOverlappingPairArray(); btBroadphasePairArray& pairs=m_paircache->getOverlappingPairArray();
for(int i=0,ni=pairs.size();i<ni;++i) for(int i=0,ni=pairs.size();i<ni;++i)
{ {
@@ -301,7 +318,92 @@ btDbvtBroadphaseCollider collider(this,m_pid);
} }
} }
} }
} else
{
if ( m_paircache->hasDeferredRemoval())
{
btBroadphasePairArray& overlappingPairArray = m_paircache->getOverlappingPairArray();
//perform a sort, to find duplicates and to sort 'invalid' pairs to the end
overlappingPairArray.quickSort(btBroadphasePairSortPredicate());
overlappingPairArray.resize(overlappingPairArray.size() - m_invalidPair);
m_invalidPair = 0;
btBroadphasePair previousPair;
previousPair.m_pProxy0 = 0;
previousPair.m_pProxy1 = 0;
previousPair.m_algorithm = 0;
int i;
for (i=0;i<overlappingPairArray.size();i++)
{
btBroadphasePair& pair = overlappingPairArray[i];
bool isDuplicate = (pair == previousPair);
previousPair = pair;
bool needsRemoval = false;
if (!isDuplicate)
{
btDbvtProxy* pa=(btDbvtProxy*)pair.m_pProxy0;
btDbvtProxy* pb=(btDbvtProxy*)pair.m_pProxy1;
bool hasOverlap = Intersect(pa->aabb,pb->aabb);
if (hasOverlap)
{
needsRemoval = false;//callback->processOverlap(pair);
} else
{
needsRemoval = true;
} }
} else
{
//remove duplicate
needsRemoval = true;
//should have no algorithm
btAssert(!pair.m_algorithm);
}
if (needsRemoval)
{
m_paircache->cleanOverlappingPair(pair,dispatcher);
// m_overlappingPairArray.swap(i,m_overlappingPairArray.size()-1);
// m_overlappingPairArray.pop_back();
pair.m_pProxy0 = 0;
pair.m_pProxy1 = 0;
m_invalidPair++;
}
}
///if you don't like to skip the invalid pairs in the array, execute following code:
#define CLEAN_INVALID_PAIRS 1
#ifdef CLEAN_INVALID_PAIRS
//perform a sort, to sort 'invalid' pairs to the end
overlappingPairArray.quickSort(btBroadphasePairSortPredicate());
overlappingPairArray.resize(overlappingPairArray.size() - m_invalidPair);
m_invalidPair = 0;
#endif//CLEAN_INVALID_PAIRS
}
}
}
++m_pid; ++m_pid;
} }

View File

@@ -30,7 +30,8 @@ subject to the following restrictions:
// Compile time config // Compile time config
// //
#define DBVT_BP_PROFILE 1 //#define DBVT_BP_PROFILE 1
#define DBVT_BP_DISCRETPAIRS 0 #define DBVT_BP_DISCRETPAIRS 0
#define DBVT_BP_MARGIN (btScalar)0.05 #define DBVT_BP_MARGIN (btScalar)0.05
@@ -80,6 +81,8 @@ int m_fupdates; // % of fixed updates per frame
int m_dupdates; // % of dynamic updates per frame int m_dupdates; // % of dynamic updates per frame
int m_pid; // Parse id int m_pid; // Parse id
int m_gid; // Gen id int m_gid; // Gen id
int m_invalidPair;
#if DBVT_BP_PROFILE #if DBVT_BP_PROFILE
btClock m_clock; btClock m_clock;
struct { struct {

View File

@@ -622,7 +622,13 @@ struct RayCaster : public btDbvt::ICollide
face = 0; face = 0;
tests = 0; tests = 0;
} }
void Process(const btDbvt::Node* leaf)
virtual void Process(const btDbvt::Node* a,const btDbvt::Node* b)
{
}
virtual void Process(const btDbvt::Node* leaf)
{ {
btSoftBody::Face& f=*(btSoftBody::Face*)leaf->data; btSoftBody::Face& f=*(btSoftBody::Face*)leaf->data;
const btScalar t=RayTriangle( o,d, const btScalar t=RayTriangle( o,d,
@@ -637,7 +643,7 @@ struct RayCaster : public btDbvt::ICollide
} }
++tests; ++tests;
} }
bool Descent(const btDbvt::Node* node) virtual bool Descent(const btDbvt::Node* node)
{ {
const btVector3 ctr=node->volume.Center()-o; const btVector3 ctr=node->volume.Center()-o;
const btScalar sqr=node->volume.Lengths().length2()/4; const btScalar sqr=node->volume.Lengths().length2()/4;
@@ -715,7 +721,7 @@ static int RaycastInternal(const btSoftBody* psb,
else else
{/* Use dbvt */ {/* Use dbvt */
RayCaster collider(org,dir,mint); RayCaster collider(org,dir,mint);
psb->m_fdbvt.collideGeneric(collider); psb->m_fdbvt.collideGeneric(&collider);
if(collider.face) if(collider.face)
{ {
mint=collider.mint; mint=collider.mint;
@@ -2533,7 +2539,17 @@ switch(m_cfg.collisions&fCollision::RVSmask)
{ {
struct DoCollide : btDbvt::ICollide struct DoCollide : btDbvt::ICollide
{ {
void Process(const btDbvt::Node* leaf)
virtual void Process(const btDbvt::Node* a,const btDbvt::Node* b)
{
}
virtual bool Descent(const btDbvt::Node*)
{
return false;
}
virtual void Process(const btDbvt::Node* leaf)
{ {
Node* node=(Node*)leaf->data; Node* node=(Node*)leaf->data;
DoNode(*node); DoNode(*node);
@@ -2608,6 +2624,15 @@ switch(cf&fCollision::SVSmask)
{ {
struct DoCollide : btDbvt::ICollide struct DoCollide : btDbvt::ICollide
{ {
virtual bool Descent(const btDbvt::Node*)
{
return false;
}
virtual void Process(const btDbvt::Node* leaf)
{
}
void Process(const btDbvt::Node* lnode, void Process(const btDbvt::Node* lnode,
const btDbvt::Node* lface) const btDbvt::Node* lface)
{ {