btBoxShape.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_BOX_MINKOWSKI_H
00017 #define BT_OBB_BOX_MINKOWSKI_H
00018 
00019 #include "btPolyhedralConvexShape.h"
00020 #include "btCollisionMargin.h"
00021 #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
00022 #include "LinearMath/btVector3.h"
00023 #include "LinearMath/btMinMax.h"
00024 
00026 class btBoxShape: public btPolyhedralConvexShape
00027 {
00028 
00029         //btVector3     m_boxHalfExtents1; //use m_implicitShapeDimensions instead
00030 
00031 
00032 public:
00033 
00034         btVector3 getHalfExtentsWithMargin() const
00035         {
00036                 btVector3 halfExtents = getHalfExtentsWithoutMargin();
00037                 btVector3 margin(getMargin(),getMargin(),getMargin());
00038                 halfExtents += margin;
00039                 return halfExtents;
00040         }
00041         
00042         const btVector3& getHalfExtentsWithoutMargin() const
00043         {
00044                 return m_implicitShapeDimensions;//scaling is included, margin is not
00045         }
00046         
00047 
00048         virtual btVector3       localGetSupportingVertex(const btVector3& vec) const
00049         {
00050                 btVector3 halfExtents = getHalfExtentsWithoutMargin();
00051                 btVector3 margin(getMargin(),getMargin(),getMargin());
00052                 halfExtents += margin;
00053                 
00054                 return btVector3(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
00055                         btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
00056                         btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
00057         }
00058 
00059         SIMD_FORCE_INLINE  btVector3    localGetSupportingVertexWithoutMargin(const btVector3& vec)const
00060         {
00061                 const btVector3& halfExtents = getHalfExtentsWithoutMargin();
00062                 
00063                 return btVector3(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
00064                         btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
00065                         btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
00066         }
00067 
00068         virtual void    batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
00069         {
00070                 const btVector3& halfExtents = getHalfExtentsWithoutMargin();
00071         
00072                 for (int i=0;i<numVectors;i++)
00073                 {
00074                         const btVector3& vec = vectors[i];
00075                         supportVerticesOut[i].setValue(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
00076                                 btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
00077                                 btFsels(vec.z(), halfExtents.z(), -halfExtents.z())); 
00078                 }
00079 
00080         }
00081 
00082 
00083         btBoxShape( const btVector3& boxHalfExtents);
00084 
00085         virtual void setMargin(btScalar collisionMargin)
00086         {
00087                 //correct the m_implicitShapeDimensions for the margin
00088                 btVector3 oldMargin(getMargin(),getMargin(),getMargin());
00089                 btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions+oldMargin;
00090                 
00091                 btConvexInternalShape::setMargin(collisionMargin);
00092                 btVector3 newMargin(getMargin(),getMargin(),getMargin());
00093                 m_implicitShapeDimensions = implicitShapeDimensionsWithMargin - newMargin;
00094 
00095         }
00096         virtual void    setLocalScaling(const btVector3& scaling)
00097         {
00098                 btVector3 oldMargin(getMargin(),getMargin(),getMargin());
00099                 btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions+oldMargin;
00100                 btVector3 unScaledImplicitShapeDimensionsWithMargin = implicitShapeDimensionsWithMargin / m_localScaling;
00101 
00102                 btConvexInternalShape::setLocalScaling(scaling);
00103 
00104                 m_implicitShapeDimensions = (unScaledImplicitShapeDimensionsWithMargin * m_localScaling) - oldMargin;
00105 
00106         }
00107 
00108         virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const;
00109 
00110         
00111 
00112         virtual void    calculateLocalInertia(btScalar mass,btVector3& inertia) const;
00113 
00114         virtual void getPlane(btVector3& planeNormal,btVector3& planeSupport,int i ) const
00115         {
00116                 //this plane might not be aligned...
00117                 btVector4 plane ;
00118                 getPlaneEquation(plane,i);
00119                 planeNormal = btVector3(plane.getX(),plane.getY(),plane.getZ());
00120                 planeSupport = localGetSupportingVertex(-planeNormal);
00121         }
00122 
00123         
00124         virtual int getNumPlanes() const
00125         {
00126                 return 6;
00127         }       
00128         
00129         virtual int     getNumVertices() const 
00130         {
00131                 return 8;
00132         }
00133 
00134         virtual int getNumEdges() const
00135         {
00136                 return 12;
00137         }
00138 
00139 
00140         virtual void getVertex(int i,btVector3& vtx) const
00141         {
00142                 btVector3 halfExtents = getHalfExtentsWithMargin();
00143 
00144                 vtx = btVector3(
00145                                 halfExtents.x() * (1-(i&1)) - halfExtents.x() * (i&1),
00146                                 halfExtents.y() * (1-((i&2)>>1)) - halfExtents.y() * ((i&2)>>1),
00147                                 halfExtents.z() * (1-((i&4)>>2)) - halfExtents.z() * ((i&4)>>2));
00148         }
00149         
00150 
00151         virtual void    getPlaneEquation(btVector4& plane,int i) const
00152         {
00153                 btVector3 halfExtents = getHalfExtentsWithoutMargin();
00154 
00155                 switch (i)
00156                 {
00157                 case 0:
00158                         plane.setValue(btScalar(1.),btScalar(0.),btScalar(0.),-halfExtents.x());
00159                         break;
00160                 case 1:
00161                         plane.setValue(btScalar(-1.),btScalar(0.),btScalar(0.),-halfExtents.x());
00162                         break;
00163                 case 2:
00164                         plane.setValue(btScalar(0.),btScalar(1.),btScalar(0.),-halfExtents.y());
00165                         break;
00166                 case 3:
00167                         plane.setValue(btScalar(0.),btScalar(-1.),btScalar(0.),-halfExtents.y());
00168                         break;
00169                 case 4:
00170                         plane.setValue(btScalar(0.),btScalar(0.),btScalar(1.),-halfExtents.z());
00171                         break;
00172                 case 5:
00173                         plane.setValue(btScalar(0.),btScalar(0.),btScalar(-1.),-halfExtents.z());
00174                         break;
00175                 default:
00176                         btAssert(0);
00177                 }
00178         }
00179 
00180         
00181         virtual void getEdge(int i,btVector3& pa,btVector3& pb) const
00182         //virtual void getEdge(int i,Edge& edge) const
00183         {
00184                 int edgeVert0 = 0;
00185                 int edgeVert1 = 0;
00186 
00187                 switch (i)
00188                 {
00189                 case 0:
00190                                 edgeVert0 = 0;
00191                                 edgeVert1 = 1;
00192                         break;
00193                 case 1:
00194                                 edgeVert0 = 0;
00195                                 edgeVert1 = 2;
00196                         break;
00197                 case 2:
00198                         edgeVert0 = 1;
00199                         edgeVert1 = 3;
00200 
00201                         break;
00202                 case 3:
00203                         edgeVert0 = 2;
00204                         edgeVert1 = 3;
00205                         break;
00206                 case 4:
00207                         edgeVert0 = 0;
00208                         edgeVert1 = 4;
00209                         break;
00210                 case 5:
00211                         edgeVert0 = 1;
00212                         edgeVert1 = 5;
00213 
00214                         break;
00215                 case 6:
00216                         edgeVert0 = 2;
00217                         edgeVert1 = 6;
00218                         break;
00219                 case 7:
00220                         edgeVert0 = 3;
00221                         edgeVert1 = 7;
00222                         break;
00223                 case 8:
00224                         edgeVert0 = 4;
00225                         edgeVert1 = 5;
00226                         break;
00227                 case 9:
00228                         edgeVert0 = 4;
00229                         edgeVert1 = 6;
00230                         break;
00231                 case 10:
00232                         edgeVert0 = 5;
00233                         edgeVert1 = 7;
00234                         break;
00235                 case 11:
00236                         edgeVert0 = 6;
00237                         edgeVert1 = 7;
00238                         break;
00239                 default:
00240                         btAssert(0);
00241 
00242                 }
00243 
00244                 getVertex(edgeVert0,pa );
00245                 getVertex(edgeVert1,pb );
00246         }
00247 
00248 
00249 
00250 
00251         
00252         virtual bool isInside(const btVector3& pt,btScalar tolerance) const
00253         {
00254                 btVector3 halfExtents = getHalfExtentsWithoutMargin();
00255 
00256                 //btScalar minDist = 2*tolerance;
00257                 
00258                 bool result =   (pt.x() <= (halfExtents.x()+tolerance)) &&
00259                                                 (pt.x() >= (-halfExtents.x()-tolerance)) &&
00260                                                 (pt.y() <= (halfExtents.y()+tolerance)) &&
00261                                                 (pt.y() >= (-halfExtents.y()-tolerance)) &&
00262                                                 (pt.z() <= (halfExtents.z()+tolerance)) &&
00263                                                 (pt.z() >= (-halfExtents.z()-tolerance));
00264                 
00265                 return result;
00266         }
00267 
00268 
00269         //debugging
00270         virtual const char*     getName()const
00271         {
00272                 return "Box";
00273         }
00274 
00275         virtual int             getNumPreferredPenetrationDirections() const
00276         {
00277                 return 6;
00278         }
00279         
00280         virtual void    getPreferredPenetrationDirection(int index, btVector3& penetrationVector) const
00281         {
00282                 switch (index)
00283                 {
00284                 case 0:
00285                         penetrationVector.setValue(btScalar(1.),btScalar(0.),btScalar(0.));
00286                         break;
00287                 case 1:
00288                         penetrationVector.setValue(btScalar(-1.),btScalar(0.),btScalar(0.));
00289                         break;
00290                 case 2:
00291                         penetrationVector.setValue(btScalar(0.),btScalar(1.),btScalar(0.));
00292                         break;
00293                 case 3:
00294                         penetrationVector.setValue(btScalar(0.),btScalar(-1.),btScalar(0.));
00295                         break;
00296                 case 4:
00297                         penetrationVector.setValue(btScalar(0.),btScalar(0.),btScalar(1.));
00298                         break;
00299                 case 5:
00300                         penetrationVector.setValue(btScalar(0.),btScalar(0.),btScalar(-1.));
00301                         break;
00302                 default:
00303                         btAssert(0);
00304                 }
00305         }
00306 
00307 };
00308 
00309 
00310 #endif //BT_OBB_BOX_MINKOWSKI_H
00311 
00312 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


bullet
Author(s): Erwin Coumans, ROS package maintained by Tully Foote
autogenerated on Wed Oct 31 2012 07:54:30