Back to index

supertuxkart  0.5+dfsg1
btDefaultCollisionConfiguration.cpp
Go to the documentation of this file.
00001 
00002 /*
00003 Bullet Continuous Collision Detection and Physics Library
00004 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
00005 
00006 This software is provided 'as-is', without any express or implied warranty.
00007 In no event will the authors be held liable for any damages arising from the use of this software.
00008 Permission is granted to anyone to use this software for any purpose, 
00009 including commercial applications, and to alter it and redistribute it freely, 
00010 subject to the following restrictions:
00011 
00012 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.
00013 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00014 3. This notice may not be removed or altered from any source distribution.
00015 */
00016 
00017 #include "btDefaultCollisionConfiguration.h"
00018 
00019 #include "BulletCollision/CollisionDispatch/btConvexConvexAlgorithm.h"
00020 #include "BulletCollision/CollisionDispatch/btEmptyCollisionAlgorithm.h"
00021 #include "BulletCollision/CollisionDispatch/btConvexConcaveCollisionAlgorithm.h"
00022 #include "BulletCollision/CollisionDispatch/btCompoundCollisionAlgorithm.h"
00023 #include "BulletCollision/CollisionDispatch/btConvexPlaneCollisionAlgorithm.h"
00024 #include "BulletCollision/CollisionDispatch/btBoxBoxCollisionAlgorithm.h"
00025 #include "BulletCollision/CollisionDispatch/btSphereSphereCollisionAlgorithm.h"
00026 #include "BulletCollision/CollisionDispatch/btSphereBoxCollisionAlgorithm.h"
00027 #include "BulletCollision/CollisionDispatch/btSphereTriangleCollisionAlgorithm.h"
00028 #include "BulletCollision/NarrowPhaseCollision/btGjkEpaPenetrationDepthSolver.h"
00029 #include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
00030 
00031 
00032 
00033 #include "LinearMath/btStackAlloc.h"
00034 #include "LinearMath/btPoolAllocator.h"
00035 
00036 
00037 
00038 #define DEFAULT_MAX_OVERLAPPING_PAIRS 65535
00039 #define DEFAULT_STACK_ALLOCATOR_SIZE      (5*1024*1024)
00040 
00041 
00042 btDefaultCollisionConfiguration::btDefaultCollisionConfiguration(btStackAlloc*      stackAlloc,btPoolAllocator* persistentManifoldPool,btPoolAllocator*   collisionAlgorithmPool)
00043 {
00044 
00045        void* mem = btAlignedAlloc(sizeof(btVoronoiSimplexSolver),16);
00046        m_simplexSolver = new (mem)btVoronoiSimplexSolver();
00047        mem = btAlignedAlloc(sizeof(btGjkEpaPenetrationDepthSolver),16);
00048        m_pdSolver = new (mem)btGjkEpaPenetrationDepthSolver;
00049 
00050        //default CreationFunctions, filling the m_doubleDispatch table
00051        mem = btAlignedAlloc(sizeof(btConvexConvexAlgorithm::CreateFunc),16);
00052        m_convexConvexCreateFunc = new(mem) btConvexConvexAlgorithm::CreateFunc(m_simplexSolver,m_pdSolver);
00053        mem = btAlignedAlloc(sizeof(btConvexConcaveCollisionAlgorithm::CreateFunc),16);
00054        m_convexConcaveCreateFunc = new (mem)btConvexConcaveCollisionAlgorithm::CreateFunc;
00055        mem = btAlignedAlloc(sizeof(btConvexConcaveCollisionAlgorithm::CreateFunc),16);
00056        m_swappedConvexConcaveCreateFunc = new (mem)btConvexConcaveCollisionAlgorithm::SwappedCreateFunc;
00057        mem = btAlignedAlloc(sizeof(btCompoundCollisionAlgorithm::CreateFunc),16);
00058        m_compoundCreateFunc = new (mem)btCompoundCollisionAlgorithm::CreateFunc;
00059        mem = btAlignedAlloc(sizeof(btCompoundCollisionAlgorithm::SwappedCreateFunc),16);
00060        m_swappedCompoundCreateFunc = new (mem)btCompoundCollisionAlgorithm::SwappedCreateFunc;
00061        mem = btAlignedAlloc(sizeof(btEmptyAlgorithm::CreateFunc),16);
00062        m_emptyCreateFunc = new(mem) btEmptyAlgorithm::CreateFunc;
00063        
00064        mem = btAlignedAlloc(sizeof(btSphereSphereCollisionAlgorithm::CreateFunc),16);
00065        m_sphereSphereCF = new(mem) btSphereSphereCollisionAlgorithm::CreateFunc;
00066        mem = btAlignedAlloc(sizeof(btSphereBoxCollisionAlgorithm::CreateFunc),16);
00067        m_sphereBoxCF = new(mem) btSphereBoxCollisionAlgorithm::CreateFunc;
00068        mem = btAlignedAlloc(sizeof(btSphereBoxCollisionAlgorithm::CreateFunc),16);
00069        m_boxSphereCF = new (mem)btSphereBoxCollisionAlgorithm::CreateFunc;
00070        m_boxSphereCF->m_swapped = true;
00071        mem = btAlignedAlloc(sizeof(btSphereTriangleCollisionAlgorithm::CreateFunc),16);
00072        m_sphereTriangleCF = new (mem)btSphereTriangleCollisionAlgorithm::CreateFunc;
00073        mem = btAlignedAlloc(sizeof(btSphereTriangleCollisionAlgorithm::CreateFunc),16);
00074        m_triangleSphereCF = new (mem)btSphereTriangleCollisionAlgorithm::CreateFunc;
00075        m_triangleSphereCF->m_swapped = true;
00076        
00077        mem = btAlignedAlloc(sizeof(btBoxBoxCollisionAlgorithm::CreateFunc),16);
00078        m_boxBoxCF = new(mem)btBoxBoxCollisionAlgorithm::CreateFunc;
00079 
00080        //convex versus plane
00081        mem = btAlignedAlloc (sizeof(btConvexPlaneCollisionAlgorithm::CreateFunc),16);
00082        m_convexPlaneCF = new (mem) btConvexPlaneCollisionAlgorithm::CreateFunc;
00083        mem = btAlignedAlloc (sizeof(btConvexPlaneCollisionAlgorithm::CreateFunc),16);
00084        m_planeConvexCF = new (mem) btConvexPlaneCollisionAlgorithm::CreateFunc;
00085        m_planeConvexCF->m_swapped = true;
00086        
00088        int maxSize = sizeof(btConvexConvexAlgorithm);
00089        int maxSize2 = sizeof(btConvexConcaveCollisionAlgorithm);
00090        int maxSize3 = sizeof(btCompoundCollisionAlgorithm);
00091        int maxSize4 = sizeof(btEmptyAlgorithm);
00092        
00093        int    collisionAlgorithmMaxElementSize = btMax(maxSize,maxSize2);
00094        collisionAlgorithmMaxElementSize = btMax(collisionAlgorithmMaxElementSize,maxSize3);
00095        collisionAlgorithmMaxElementSize = btMax(collisionAlgorithmMaxElementSize,maxSize4);
00096 
00097        if (stackAlloc)
00098        {
00099               m_ownsStackAllocator = false;
00100               this->m_stackAlloc = stackAlloc;
00101        } else
00102        {
00103               m_ownsStackAllocator = true;
00104               void* mem = btAlignedAlloc(sizeof(btStackAlloc),16);
00105               m_stackAlloc = new(mem)btStackAlloc(DEFAULT_STACK_ALLOCATOR_SIZE);
00106        }
00107               
00108        if (persistentManifoldPool)
00109        {
00110               m_ownsPersistentManifoldPool = false;
00111               m_persistentManifoldPool = persistentManifoldPool;
00112        } else
00113        {
00114               m_ownsPersistentManifoldPool = true;
00115               void* mem = btAlignedAlloc(sizeof(btPoolAllocator),16);
00116               m_persistentManifoldPool = new (mem) btPoolAllocator(sizeof(btPersistentManifold),DEFAULT_MAX_OVERLAPPING_PAIRS);
00117        }
00118        
00119        if (collisionAlgorithmPool)
00120        {
00121               m_ownsCollisionAlgorithmPool = false;
00122               m_collisionAlgorithmPool = collisionAlgorithmPool;
00123        } else
00124        {
00125               m_ownsCollisionAlgorithmPool = true;
00126               void* mem = btAlignedAlloc(sizeof(btPoolAllocator),16);
00127               m_collisionAlgorithmPool = new(mem) btPoolAllocator(collisionAlgorithmMaxElementSize,DEFAULT_MAX_OVERLAPPING_PAIRS);
00128        }
00129 
00130 
00131 }
00132 
00133 btDefaultCollisionConfiguration::~btDefaultCollisionConfiguration()
00134 {
00135        if (m_ownsStackAllocator)
00136        {
00137               m_stackAlloc->destroy();
00138               m_stackAlloc->~btStackAlloc();
00139               btAlignedFree(m_stackAlloc);
00140        }
00141        if (m_ownsCollisionAlgorithmPool)
00142        {
00143               m_collisionAlgorithmPool->~btPoolAllocator();
00144               btAlignedFree(m_collisionAlgorithmPool);
00145        }
00146        if (m_ownsPersistentManifoldPool)
00147        {
00148               m_persistentManifoldPool->~btPoolAllocator();
00149               btAlignedFree(m_persistentManifoldPool);
00150        }
00151 
00152        m_convexConvexCreateFunc->~btCollisionAlgorithmCreateFunc();
00153        btAlignedFree(       m_convexConvexCreateFunc);
00154 
00155        m_convexConcaveCreateFunc->~btCollisionAlgorithmCreateFunc();
00156        btAlignedFree( m_convexConcaveCreateFunc);
00157        m_swappedConvexConcaveCreateFunc->~btCollisionAlgorithmCreateFunc();
00158        btAlignedFree( m_swappedConvexConcaveCreateFunc);
00159 
00160        m_compoundCreateFunc->~btCollisionAlgorithmCreateFunc();
00161        btAlignedFree( m_compoundCreateFunc);
00162 
00163        m_swappedCompoundCreateFunc->~btCollisionAlgorithmCreateFunc();
00164        btAlignedFree( m_swappedCompoundCreateFunc);
00165 
00166        m_emptyCreateFunc->~btCollisionAlgorithmCreateFunc();
00167        btAlignedFree( m_emptyCreateFunc);
00168 
00169        m_sphereSphereCF->~btCollisionAlgorithmCreateFunc();
00170        btAlignedFree( m_sphereSphereCF);
00171 
00172        m_sphereBoxCF->~btCollisionAlgorithmCreateFunc();
00173        btAlignedFree( m_sphereBoxCF);
00174        m_boxSphereCF->~btCollisionAlgorithmCreateFunc();
00175        btAlignedFree( m_boxSphereCF);
00176        m_sphereTriangleCF->~btCollisionAlgorithmCreateFunc();
00177        btAlignedFree( m_sphereTriangleCF);
00178        m_triangleSphereCF->~btCollisionAlgorithmCreateFunc();
00179        btAlignedFree( m_triangleSphereCF);
00180        m_boxBoxCF->~btCollisionAlgorithmCreateFunc();
00181        btAlignedFree( m_boxBoxCF);
00182 
00183        m_convexPlaneCF->~btCollisionAlgorithmCreateFunc();
00184        btAlignedFree( m_convexPlaneCF);
00185        m_planeConvexCF->~btCollisionAlgorithmCreateFunc();
00186        btAlignedFree( m_planeConvexCF);
00187 
00188        m_simplexSolver->~btVoronoiSimplexSolver();
00189        btAlignedFree(m_simplexSolver);
00190        m_pdSolver->~btGjkEpaPenetrationDepthSolver();
00191        btAlignedFree(m_pdSolver);
00192 
00193 
00194 }
00195 
00196 
00197 btCollisionAlgorithmCreateFunc* btDefaultCollisionConfiguration::getCollisionAlgorithmCreateFunc(int proxyType0,int proxyType1)
00198 {
00199 
00200 
00201 
00202        if ((proxyType0 == SPHERE_SHAPE_PROXYTYPE) && (proxyType1==SPHERE_SHAPE_PROXYTYPE))
00203        {
00204               return m_sphereSphereCF;
00205        }
00206 
00207        if ((proxyType0 == SPHERE_SHAPE_PROXYTYPE) && (proxyType1==BOX_SHAPE_PROXYTYPE))
00208        {
00209               return m_sphereBoxCF;
00210        }
00211 
00212        if ((proxyType0 == BOX_SHAPE_PROXYTYPE ) && (proxyType1==SPHERE_SHAPE_PROXYTYPE))
00213        {
00214               return m_boxSphereCF;
00215        }
00216 
00217        if ((proxyType0 == SPHERE_SHAPE_PROXYTYPE ) && (proxyType1==TRIANGLE_SHAPE_PROXYTYPE))
00218        {
00219               return m_sphereTriangleCF;
00220        }
00221 
00222        if ((proxyType0 == TRIANGLE_SHAPE_PROXYTYPE  ) && (proxyType1==SPHERE_SHAPE_PROXYTYPE))
00223        {
00224               return m_triangleSphereCF;
00225        }
00226 
00227        if ((proxyType0 == BOX_SHAPE_PROXYTYPE) && (proxyType1 == BOX_SHAPE_PROXYTYPE))
00228        {
00229               return m_boxBoxCF;
00230        }
00231        
00232        if (btBroadphaseProxy::isConvex(proxyType0) && (proxyType1 == STATIC_PLANE_PROXYTYPE))
00233        {
00234               return m_convexPlaneCF;
00235        }
00236 
00237        if (btBroadphaseProxy::isConvex(proxyType1) && (proxyType0 == STATIC_PLANE_PROXYTYPE))
00238        {
00239               return m_planeConvexCF;
00240        }
00241 
00242        if (btBroadphaseProxy::isConvex(proxyType0) && btBroadphaseProxy::isConvex(proxyType1))
00243        {
00244               return m_convexConvexCreateFunc;
00245        }
00246 
00247        if (btBroadphaseProxy::isConvex(proxyType0) && btBroadphaseProxy::isConcave(proxyType1))
00248        {
00249               return m_convexConcaveCreateFunc;
00250        }
00251 
00252        if (btBroadphaseProxy::isConvex(proxyType1) && btBroadphaseProxy::isConcave(proxyType0))
00253        {
00254               return m_swappedConvexConcaveCreateFunc;
00255        }
00256 
00257        if (btBroadphaseProxy::isCompound(proxyType0))
00258        {
00259               return m_compoundCreateFunc;
00260        } else
00261        {
00262               if (btBroadphaseProxy::isCompound(proxyType1))
00263               {
00264                      return m_swappedCompoundCreateFunc;
00265               }
00266        }
00267 
00268        //failed to find an algorithm
00269        return m_emptyCreateFunc;
00270 }