Back to index

supertuxkart  0.5+dfsg1
btConvexTriangleMeshShape.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 #include "btConvexTriangleMeshShape.h"
00016 #include "BulletCollision/CollisionShapes/btCollisionMargin.h"
00017 
00018 #include "LinearMath/btQuaternion.h"
00019 #include "BulletCollision/CollisionShapes/btStridingMeshInterface.h"
00020 
00021 
00022 btConvexTriangleMeshShape ::btConvexTriangleMeshShape (btStridingMeshInterface* meshInterface)
00023 :m_stridingMesh(meshInterface)
00024 {
00025        recalcLocalAabb();
00026 }
00027 
00028 
00029 
00030 
00033 class LocalSupportVertexCallback: public btInternalTriangleIndexCallback
00034 {
00035 
00036        btVector3 m_supportVertexLocal;
00037 public:
00038 
00039        btScalar m_maxDot;
00040        btVector3 m_supportVecLocal;
00041 
00042        LocalSupportVertexCallback(const btVector3& supportVecLocal)
00043               : m_supportVertexLocal(btScalar(0.),btScalar(0.),btScalar(0.)),
00044               m_maxDot(btScalar(-1e30)),
00045                 m_supportVecLocal(supportVecLocal)
00046        {
00047        }
00048 
00049        virtual void internalProcessTriangleIndex(btVector3* triangle,int partId,int  triangleIndex)
00050        {
00051               (void)triangleIndex;
00052               (void)partId;
00053 
00054               for (int i=0;i<3;i++)
00055               {
00056                      btScalar dot = m_supportVecLocal.dot(triangle[i]);
00057                      if (dot > m_maxDot)
00058                      {
00059                             m_maxDot = dot;
00060                             m_supportVertexLocal = triangle[i];
00061                      }
00062               }
00063        }
00064        
00065        btVector3     GetSupportVertexLocal()
00066        {
00067               return m_supportVertexLocal;
00068        }
00069 
00070 };
00071 
00072 
00073 
00074 
00075 
00076 btVector3     btConvexTriangleMeshShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const
00077 {
00078        btVector3 supVec(btScalar(0.),btScalar(0.),btScalar(0.));
00079 
00080        btVector3 vec = vec0;
00081        btScalar lenSqr = vec.length2();
00082        if (lenSqr < btScalar(0.0001))
00083        {
00084               vec.setValue(1,0,0);
00085        } else
00086        {
00087               btScalar rlen = btScalar(1.) / btSqrt(lenSqr );
00088               vec *= rlen;
00089        }
00090 
00091        LocalSupportVertexCallback  supportCallback(vec);
00092        btVector3 aabbMax(btScalar(1e30),btScalar(1e30),btScalar(1e30));
00093        m_stridingMesh->InternalProcessAllTriangles(&supportCallback,-aabbMax,aabbMax);
00094        supVec = supportCallback.GetSupportVertexLocal();
00095 
00096        return supVec;
00097 }
00098 
00099 void   btConvexTriangleMeshShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
00100 {
00101        //use 'w' component of supportVerticesOut?
00102        {
00103               for (int i=0;i<numVectors;i++)
00104               {
00105                      supportVerticesOut[i][3] = btScalar(-1e30);
00106               }
00107        }
00108        
00109        //todo: could do the batch inside the callback!
00110 
00111 
00112        for (int j=0;j<numVectors;j++)
00113        {
00114               const btVector3& vec = vectors[j];
00115               LocalSupportVertexCallback  supportCallback(vec);
00116               btVector3 aabbMax(btScalar(1e30),btScalar(1e30),btScalar(1e30));
00117               m_stridingMesh->InternalProcessAllTriangles(&supportCallback,-aabbMax,aabbMax);
00118               supportVerticesOut[j] = supportCallback.GetSupportVertexLocal();
00119        }
00120        
00121 }
00122        
00123 
00124 
00125 btVector3     btConvexTriangleMeshShape::localGetSupportingVertex(const btVector3& vec)const
00126 {
00127        btVector3 supVertex = localGetSupportingVertexWithoutMargin(vec);
00128 
00129        if ( getMargin()!=btScalar(0.) )
00130        {
00131               btVector3 vecnorm = vec;
00132               if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
00133               {
00134                      vecnorm.setValue(btScalar(-1.),btScalar(-1.),btScalar(-1.));
00135               } 
00136               vecnorm.normalize();
00137               supVertex+= getMargin() * vecnorm;
00138        }
00139        return supVertex;
00140 }
00141 
00142 
00143 
00144 
00145 
00146 
00147 
00148 
00149 
00150 //currently just for debugging (drawing), perhaps future support for algebraic continuous collision detection
00151 //Please note that you can debug-draw btConvexTriangleMeshShape with the Raytracer Demo
00152 int    btConvexTriangleMeshShape::getNumVertices() const
00153 {
00154        //cache this?
00155        return 0;
00156        
00157 }
00158 
00159 int btConvexTriangleMeshShape::getNumEdges() const
00160 {
00161        return 0;
00162 }
00163 
00164 void btConvexTriangleMeshShape::getEdge(int ,btPoint3& ,btPoint3& ) const
00165 {
00166        btAssert(0);  
00167 }
00168 
00169 void btConvexTriangleMeshShape::getVertex(int ,btPoint3& ) const
00170 {
00171        btAssert(0);
00172 }
00173 
00174 int    btConvexTriangleMeshShape::getNumPlanes() const
00175 {
00176        return 0;
00177 }
00178 
00179 void btConvexTriangleMeshShape::getPlane(btVector3& ,btPoint3& ,int  ) const
00180 {
00181        btAssert(0);
00182 }
00183 
00184 //not yet
00185 bool btConvexTriangleMeshShape::isInside(const btPoint3& ,btScalar ) const
00186 {
00187        btAssert(0);
00188        return false;
00189 }
00190 
00191 
00192 
00193 void   btConvexTriangleMeshShape::setLocalScaling(const btVector3& scaling)
00194 {
00195        m_stridingMesh->setScaling(scaling);
00196        
00197        recalcLocalAabb();
00198        
00199 }
00200 
00201 
00202 const btVector3& btConvexTriangleMeshShape::getLocalScaling() const
00203 {
00204        return m_stridingMesh->getScaling();
00205 }