btTriangleShape.h

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 #ifndef BT_OBB_TRIANGLE_MINKOWSKI_H
00017 #define BT_OBB_TRIANGLE_MINKOWSKI_H
00018 
00019 #include "btConvexShape.h"
00020 #include "btBoxShape.h"
00021 
00022 ATTRIBUTE_ALIGNED16(class) btTriangleShape : public btPolyhedralConvexShape
00023 {
00024 
00025 
00026 public:
00027 
00028 BT_DECLARE_ALIGNED_ALLOCATOR();
00029 
00030         btVector3       m_vertices1[3];
00031 
00032         virtual int getNumVertices() const
00033         {
00034                 return 3;
00035         }
00036 
00037         btVector3& getVertexPtr(int index)
00038         {
00039                 return m_vertices1[index];
00040         }
00041 
00042         const btVector3& getVertexPtr(int index) const
00043         {
00044                 return m_vertices1[index];
00045         }
00046         virtual void getVertex(int index,btVector3& vert) const
00047         {
00048                 vert = m_vertices1[index];
00049         }
00050 
00051         virtual int getNumEdges() const
00052         {
00053                 return 3;
00054         }
00055         
00056         virtual void getEdge(int i,btVector3& pa,btVector3& pb) const
00057         {
00058                 getVertex(i,pa);
00059                 getVertex((i+1)%3,pb);
00060         }
00061 
00062 
00063         virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax)const 
00064         {
00065 //              btAssert(0);
00066                 getAabbSlow(t,aabbMin,aabbMax);
00067         }
00068 
00069         btVector3 localGetSupportingVertexWithoutMargin(const btVector3& dir)const 
00070         {
00071         btVector3 dots = dir.dot3(m_vertices1[0], m_vertices1[1], m_vertices1[2]);
00072                 return m_vertices1[dots.maxAxis()];
00073 
00074         }
00075 
00076         virtual void    batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
00077         {
00078                 for (int i=0;i<numVectors;i++)
00079                 {
00080                         const btVector3& dir = vectors[i];
00081             btVector3 dots = dir.dot3(m_vertices1[0], m_vertices1[1], m_vertices1[2]);
00082                         supportVerticesOut[i] = m_vertices1[dots.maxAxis()];
00083                 }
00084 
00085         }
00086 
00087         btTriangleShape() : btPolyhedralConvexShape ()
00088     {
00089                 m_shapeType = TRIANGLE_SHAPE_PROXYTYPE;
00090         }
00091 
00092         btTriangleShape(const btVector3& p0,const btVector3& p1,const btVector3& p2) : btPolyhedralConvexShape ()
00093     {
00094                 m_shapeType = TRIANGLE_SHAPE_PROXYTYPE;
00095         m_vertices1[0] = p0;
00096         m_vertices1[1] = p1;
00097         m_vertices1[2] = p2;
00098     }
00099 
00100 
00101         virtual void getPlane(btVector3& planeNormal,btVector3& planeSupport,int i) const
00102         {
00103                 getPlaneEquation(i,planeNormal,planeSupport);
00104         }
00105 
00106         virtual int     getNumPlanes() const
00107         {
00108                 return 1;
00109         }
00110 
00111         void calcNormal(btVector3& normal) const
00112         {
00113                 normal = (m_vertices1[1]-m_vertices1[0]).cross(m_vertices1[2]-m_vertices1[0]);
00114                 normal.normalize();
00115         }
00116 
00117         virtual void getPlaneEquation(int i, btVector3& planeNormal,btVector3& planeSupport) const
00118         {
00119                 (void)i;
00120                 calcNormal(planeNormal);
00121                 planeSupport = m_vertices1[0];
00122         }
00123 
00124         virtual void    calculateLocalInertia(btScalar mass,btVector3& inertia) const
00125         {
00126                 (void)mass;
00127                 btAssert(0);
00128                 inertia.setValue(btScalar(0.),btScalar(0.),btScalar(0.));
00129         }
00130 
00131                 virtual bool isInside(const btVector3& pt,btScalar tolerance) const
00132         {
00133                 btVector3 normal;
00134                 calcNormal(normal);
00135                 //distance to plane
00136                 btScalar dist = pt.dot(normal);
00137                 btScalar planeconst = m_vertices1[0].dot(normal);
00138                 dist -= planeconst;
00139                 if (dist >= -tolerance && dist <= tolerance)
00140                 {
00141                         //inside check on edge-planes
00142                         int i;
00143                         for (i=0;i<3;i++)
00144                         {
00145                                 btVector3 pa,pb;
00146                                 getEdge(i,pa,pb);
00147                                 btVector3 edge = pb-pa;
00148                                 btVector3 edgeNormal = edge.cross(normal);
00149                                 edgeNormal.normalize();
00150                                 btScalar dist = pt.dot( edgeNormal);
00151                                 btScalar edgeConst = pa.dot(edgeNormal);
00152                                 dist -= edgeConst;
00153                                 if (dist < -tolerance)
00154                                         return false;
00155                         }
00156                         
00157                         return true;
00158                 }
00159 
00160                 return false;
00161         }
00162                 //debugging
00163                 virtual const char*     getName()const
00164                 {
00165                         return "Triangle";
00166                 }
00167 
00168                 virtual int             getNumPreferredPenetrationDirections() const
00169                 {
00170                         return 2;
00171                 }
00172                 
00173                 virtual void    getPreferredPenetrationDirection(int index, btVector3& penetrationVector) const
00174                 {
00175                         calcNormal(penetrationVector);
00176                         if (index)
00177                                 penetrationVector *= btScalar(-1.);
00178                 }
00179 
00180 
00181 };
00182 
00183 #endif //BT_OBB_TRIANGLE_MINKOWSKI_H
00184