Back to index

supertuxkart  0.5+dfsg1
btTriangleMeshShape.cpp
Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 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.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 #include "btTriangleMeshShape.h"
00017 #include "LinearMath/btVector3.h"
00018 #include "LinearMath/btQuaternion.h"
00019 #include "btStridingMeshInterface.h"
00020 #include "LinearMath/btAabbUtil2.h"
00021 #include "BulletCollision/CollisionShapes/btCollisionMargin.h"
00022 
00023 
00024 btTriangleMeshShape::btTriangleMeshShape(btStridingMeshInterface* meshInterface)
00025 : m_meshInterface(meshInterface)
00026 {
00027        recalcLocalAabb();
00028 }
00029 
00030 
00031 btTriangleMeshShape::~btTriangleMeshShape()
00032 {
00033               
00034 }
00035 
00036 
00037 
00038 
00039 void btTriangleMeshShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const
00040 {
00041 
00042        btVector3 localHalfExtents = btScalar(0.5)*(m_localAabbMax-m_localAabbMin);
00043        btVector3 localCenter = btScalar(0.5)*(m_localAabbMax+m_localAabbMin);
00044        
00045        btMatrix3x3 abs_b = trans.getBasis().absolute();  
00046 
00047        btPoint3 center = trans(localCenter);
00048 
00049        btVector3 extent = btVector3(abs_b[0].dot(localHalfExtents),
00050                  abs_b[1].dot(localHalfExtents),
00051                 abs_b[2].dot(localHalfExtents));
00052        extent += btVector3(getMargin(),getMargin(),getMargin());
00053 
00054        aabbMin = center - extent;
00055        aabbMax = center + extent;
00056 
00057        
00058 }
00059 
00060 void   btTriangleMeshShape::recalcLocalAabb()
00061 {
00062        for (int i=0;i<3;i++)
00063        {
00064               btVector3 vec(btScalar(0.),btScalar(0.),btScalar(0.));
00065               vec[i] = btScalar(1.);
00066               btVector3 tmp = localGetSupportingVertex(vec);
00067               m_localAabbMax[i] = tmp[i]+m_collisionMargin;
00068               vec[i] = btScalar(-1.);
00069               tmp = localGetSupportingVertex(vec);
00070               m_localAabbMin[i] = tmp[i]-m_collisionMargin;
00071        }
00072 }
00073 
00074 
00075 
00076 class SupportVertexCallback : public btTriangleCallback
00077 {
00078 
00079        btVector3 m_supportVertexLocal;
00080 public:
00081 
00082        btTransform   m_worldTrans;
00083        btScalar m_maxDot;
00084        btVector3 m_supportVecLocal;
00085 
00086        SupportVertexCallback(const btVector3& supportVecWorld,const btTransform& trans)
00087               : m_supportVertexLocal(btScalar(0.),btScalar(0.),btScalar(0.)), m_worldTrans(trans) ,m_maxDot(btScalar(-1e30))
00088               
00089        {
00090               m_supportVecLocal = supportVecWorld * m_worldTrans.getBasis();
00091        }
00092 
00093        virtual void processTriangle( btVector3* triangle,int partId, int triangleIndex)
00094        {
00095               (void)partId;
00096               (void)triangleIndex;
00097               for (int i=0;i<3;i++)
00098               {
00099                      btScalar dot = m_supportVecLocal.dot(triangle[i]);
00100                      if (dot > m_maxDot)
00101                      {
00102                             m_maxDot = dot;
00103                             m_supportVertexLocal = triangle[i];
00104                      }
00105               }
00106        }
00107 
00108        btVector3 GetSupportVertexWorldSpace()
00109        {
00110               return m_worldTrans(m_supportVertexLocal);
00111        }
00112 
00113        btVector3     GetSupportVertexLocal()
00114        {
00115               return m_supportVertexLocal;
00116        }
00117 
00118 };
00119 
00120        
00121 void btTriangleMeshShape::setLocalScaling(const btVector3& scaling)
00122 {
00123        m_meshInterface->setScaling(scaling);
00124        recalcLocalAabb();
00125 }
00126 
00127 const btVector3& btTriangleMeshShape::getLocalScaling() const
00128 {
00129        return m_meshInterface->getScaling();
00130 }
00131 
00132 
00133 
00134 
00135 
00136 
00137 //#define DEBUG_TRIANGLE_MESH
00138 
00139 
00140 
00141 void   btTriangleMeshShape::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
00142 {
00143               struct FilteredCallback : public btInternalTriangleIndexCallback
00144        {
00145               btTriangleCallback* m_callback;
00146               btVector3 m_aabbMin;
00147               btVector3 m_aabbMax;
00148 
00149               FilteredCallback(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax)
00150                      :m_callback(callback),
00151                      m_aabbMin(aabbMin),
00152                      m_aabbMax(aabbMax)
00153               {
00154               }
00155 
00156               virtual void internalProcessTriangleIndex(btVector3* triangle,int partId,int triangleIndex)
00157               {
00158                      if (TestTriangleAgainstAabb2(&triangle[0],m_aabbMin,m_aabbMax))
00159                      {
00160                             //check aabb in triangle-space, before doing this
00161                             m_callback->processTriangle(triangle,partId,triangleIndex);
00162                      }
00163                      
00164               }
00165 
00166        };
00167 
00168        FilteredCallback filterCallback(callback,aabbMin,aabbMax);
00169 
00170        m_meshInterface->InternalProcessAllTriangles(&filterCallback,aabbMin,aabbMax);
00171 }
00172 
00173 
00174 
00175 
00176 
00177 void   btTriangleMeshShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
00178 {
00179        (void)mass;
00180        //moving concave objects not supported
00181        btAssert(0);
00182        inertia.setValue(btScalar(0.),btScalar(0.),btScalar(0.));
00183 }
00184 
00185 
00186 btVector3 btTriangleMeshShape::localGetSupportingVertex(const btVector3& vec) const
00187 {
00188        btVector3 supportVertex;
00189 
00190        btTransform ident;
00191        ident.setIdentity();
00192 
00193        SupportVertexCallback supportCallback(vec,ident);
00194 
00195        btVector3 aabbMax(btScalar(1e30),btScalar(1e30),btScalar(1e30));
00196        
00197        processAllTriangles(&supportCallback,-aabbMax,aabbMax);
00198               
00199        supportVertex = supportCallback.GetSupportVertexLocal();
00200 
00201        return supportVertex;
00202 }