Blender V2.61 - r43446

btScaledBvhTriangleMeshShape.cpp

Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2009 Erwin Coumans  http://bulletphysics.org
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 
00017 #include "btScaledBvhTriangleMeshShape.h"
00018 
00019 btScaledBvhTriangleMeshShape::btScaledBvhTriangleMeshShape(btBvhTriangleMeshShape* childShape,const btVector3& localScaling)
00020 :m_localScaling(localScaling),m_bvhTriMeshShape(childShape)
00021 {
00022     m_shapeType = SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE;
00023 }
00024 
00025 btScaledBvhTriangleMeshShape::~btScaledBvhTriangleMeshShape()
00026 {
00027 }
00028 
00029 
00030 class btScaledTriangleCallback : public btTriangleCallback
00031 {
00032     btTriangleCallback* m_originalCallback;
00033 
00034     btVector3   m_localScaling;
00035 
00036 public:
00037 
00038     btScaledTriangleCallback(btTriangleCallback* originalCallback,const btVector3& localScaling)
00039         :m_originalCallback(originalCallback),
00040         m_localScaling(localScaling)
00041     {
00042     }
00043 
00044     virtual void processTriangle(btVector3* triangle, int partId, int triangleIndex)
00045     {
00046         btVector3 newTriangle[3];
00047         newTriangle[0] = triangle[0]*m_localScaling;
00048         newTriangle[1] = triangle[1]*m_localScaling;
00049         newTriangle[2] = triangle[2]*m_localScaling;
00050         m_originalCallback->processTriangle(&newTriangle[0],partId,triangleIndex);
00051     }
00052 };
00053 
00054 void    btScaledBvhTriangleMeshShape::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
00055 {
00056     btScaledTriangleCallback scaledCallback(callback,m_localScaling);
00057     
00058     btVector3 invLocalScaling(1.f/m_localScaling.getX(),1.f/m_localScaling.getY(),1.f/m_localScaling.getZ());
00059     btVector3 scaledAabbMin,scaledAabbMax;
00060 
00062     scaledAabbMin[0] = m_localScaling.getX() >= 0. ? aabbMin[0] * invLocalScaling[0] : aabbMax[0] * invLocalScaling[0];
00063     scaledAabbMin[1] = m_localScaling.getY() >= 0. ? aabbMin[1] * invLocalScaling[1] : aabbMax[1] * invLocalScaling[1];
00064     scaledAabbMin[2] = m_localScaling.getZ() >= 0. ? aabbMin[2] * invLocalScaling[2] : aabbMax[2] * invLocalScaling[2];
00065     scaledAabbMin[3] = 0.f;
00066     
00067     scaledAabbMax[0] = m_localScaling.getX() <= 0. ? aabbMin[0] * invLocalScaling[0] : aabbMax[0] * invLocalScaling[0];
00068     scaledAabbMax[1] = m_localScaling.getY() <= 0. ? aabbMin[1] * invLocalScaling[1] : aabbMax[1] * invLocalScaling[1];
00069     scaledAabbMax[2] = m_localScaling.getZ() <= 0. ? aabbMin[2] * invLocalScaling[2] : aabbMax[2] * invLocalScaling[2];
00070     scaledAabbMax[3] = 0.f;
00071     
00072     
00073     m_bvhTriMeshShape->processAllTriangles(&scaledCallback,scaledAabbMin,scaledAabbMax);
00074 }
00075 
00076 
00077 void    btScaledBvhTriangleMeshShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const
00078 {
00079     btVector3 localAabbMin = m_bvhTriMeshShape->getLocalAabbMin();
00080     btVector3 localAabbMax = m_bvhTriMeshShape->getLocalAabbMax();
00081 
00082     btVector3 tmpLocalAabbMin = localAabbMin * m_localScaling;
00083     btVector3 tmpLocalAabbMax = localAabbMax * m_localScaling;
00084 
00085     localAabbMin[0] = (m_localScaling.getX() >= 0.) ? tmpLocalAabbMin[0] : tmpLocalAabbMax[0];
00086     localAabbMin[1] = (m_localScaling.getY() >= 0.) ? tmpLocalAabbMin[1] : tmpLocalAabbMax[1];
00087     localAabbMin[2] = (m_localScaling.getZ() >= 0.) ? tmpLocalAabbMin[2] : tmpLocalAabbMax[2];
00088     localAabbMax[0] = (m_localScaling.getX() <= 0.) ? tmpLocalAabbMin[0] : tmpLocalAabbMax[0];
00089     localAabbMax[1] = (m_localScaling.getY() <= 0.) ? tmpLocalAabbMin[1] : tmpLocalAabbMax[1];
00090     localAabbMax[2] = (m_localScaling.getZ() <= 0.) ? tmpLocalAabbMin[2] : tmpLocalAabbMax[2];
00091 
00092     btVector3 localHalfExtents = btScalar(0.5)*(localAabbMax-localAabbMin);
00093     btScalar margin = m_bvhTriMeshShape->getMargin();
00094     localHalfExtents += btVector3(margin,margin,margin);
00095     btVector3 localCenter = btScalar(0.5)*(localAabbMax+localAabbMin);
00096     
00097     btMatrix3x3 abs_b = trans.getBasis().absolute();  
00098 
00099     btVector3 center = trans(localCenter);
00100 
00101     btVector3 extent = btVector3(abs_b[0].dot(localHalfExtents),
00102            abs_b[1].dot(localHalfExtents),
00103           abs_b[2].dot(localHalfExtents));
00104     aabbMin = center - extent;
00105     aabbMax = center + extent;
00106 
00107 }
00108 
00109 void    btScaledBvhTriangleMeshShape::setLocalScaling(const btVector3& scaling)
00110 {
00111     m_localScaling = scaling;
00112 }
00113 
00114 const btVector3& btScaledBvhTriangleMeshShape::getLocalScaling() const
00115 {
00116     return m_localScaling;
00117 }
00118 
00119 void    btScaledBvhTriangleMeshShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
00120 {
00122 //  btAssert(0);
00123 }