Back to index

supertuxkart  0.5+dfsg1
btConvexHullShape.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 "btConvexHullShape.h"
00016 #include "BulletCollision/CollisionShapes/btCollisionMargin.h"
00017 
00018 #include "LinearMath/btQuaternion.h"
00019 
00020 
00021 
00022 btConvexHullShape ::btConvexHullShape (const btScalar* points,int numPoints,int stride)
00023 {
00024        m_points.resize(numPoints);
00025 
00026        unsigned char* pointsBaseAddress = (unsigned char*)points;
00027 
00028        for (int i=0;i<numPoints;i++)
00029        {
00030               btPoint3* point = (btPoint3*)(pointsBaseAddress + i*stride);
00031               m_points[i] = point[0];
00032        }
00033 
00034        recalcLocalAabb();
00035 
00036 }
00037 
00038 
00039 void btConvexHullShape::addPoint(const btPoint3& point)
00040 {
00041        m_points.push_back(point);
00042        recalcLocalAabb();
00043 
00044 }
00045 
00046 btVector3     btConvexHullShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const
00047 {
00048        btVector3 supVec(btScalar(0.),btScalar(0.),btScalar(0.));
00049        btScalar newDot,maxDot = btScalar(-1e30);
00050 
00051        btVector3 vec = vec0;
00052        btScalar lenSqr = vec.length2();
00053        if (lenSqr < btScalar(0.0001))
00054        {
00055               vec.setValue(1,0,0);
00056        } else
00057        {
00058               btScalar rlen = btScalar(1.) / btSqrt(lenSqr );
00059               vec *= rlen;
00060        }
00061 
00062 
00063        for (int i=0;i<m_points.size();i++)
00064        {
00065               btPoint3 vtx = m_points[i] * m_localScaling;
00066 
00067               newDot = vec.dot(vtx);
00068               if (newDot > maxDot)
00069               {
00070                      maxDot = newDot;
00071                      supVec = vtx;
00072               }
00073        }
00074        return supVec;
00075 }
00076 
00077 void   btConvexHullShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
00078 {
00079        btScalar newDot;
00080        //use 'w' component of supportVerticesOut?
00081        {
00082               for (int i=0;i<numVectors;i++)
00083               {
00084                      supportVerticesOut[i][3] = btScalar(-1e30);
00085               }
00086        }
00087        for (int i=0;i<m_points.size();i++)
00088        {
00089               btPoint3 vtx = m_points[i] * m_localScaling;
00090 
00091               for (int j=0;j<numVectors;j++)
00092               {
00093                      const btVector3& vec = vectors[j];
00094                      
00095                      newDot = vec.dot(vtx);
00096                      if (newDot > supportVerticesOut[j][3])
00097                      {
00098                             //WARNING: don't swap next lines, the w component would get overwritten!
00099                             supportVerticesOut[j] = vtx;
00100                             supportVerticesOut[j][3] = newDot;
00101                      }
00102               }
00103        }
00104 
00105 
00106 
00107 }
00108        
00109 
00110 
00111 btVector3     btConvexHullShape::localGetSupportingVertex(const btVector3& vec)const
00112 {
00113        btVector3 supVertex = localGetSupportingVertexWithoutMargin(vec);
00114 
00115        if ( getMargin()!=btScalar(0.) )
00116        {
00117               btVector3 vecnorm = vec;
00118               if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
00119               {
00120                      vecnorm.setValue(btScalar(-1.),btScalar(-1.),btScalar(-1.));
00121               } 
00122               vecnorm.normalize();
00123               supVertex+= getMargin() * vecnorm;
00124        }
00125        return supVertex;
00126 }
00127 
00128 
00129 
00130 
00131 
00132 
00133 
00134 
00135 
00136 //currently just for debugging (drawing), perhaps future support for algebraic continuous collision detection
00137 //Please note that you can debug-draw btConvexHullShape with the Raytracer Demo
00138 int    btConvexHullShape::getNumVertices() const
00139 {
00140        return m_points.size();
00141 }
00142 
00143 int btConvexHullShape::getNumEdges() const
00144 {
00145        return m_points.size();
00146 }
00147 
00148 void btConvexHullShape::getEdge(int i,btPoint3& pa,btPoint3& pb) const
00149 {
00150 
00151        int index0 = i%m_points.size();
00152        int index1 = (i+1)%m_points.size();
00153        pa = m_points[index0]*m_localScaling;
00154        pb = m_points[index1]*m_localScaling;
00155 }
00156 
00157 void btConvexHullShape::getVertex(int i,btPoint3& vtx) const
00158 {
00159        vtx = m_points[i]*m_localScaling;
00160 }
00161 
00162 int    btConvexHullShape::getNumPlanes() const
00163 {
00164        return 0;
00165 }
00166 
00167 void btConvexHullShape::getPlane(btVector3& ,btPoint3& ,int ) const
00168 {
00169 
00170        btAssert(0);
00171 }
00172 
00173 //not yet
00174 bool btConvexHullShape::isInside(const btPoint3& ,btScalar ) const
00175 {
00176        assert(0);
00177        return false;
00178 }
00179