Back to index

supertuxkart  0.5+dfsg1
btShapeHull.cpp
Go to the documentation of this file.
00001 /*
00002 btbtShapeHull implemented by John McCutchan.
00003 
00004 Bullet Continuous Collision Detection and Physics Library
00005 Copyright (c) 2003-2008 Erwin Coumans  http://bulletphysics.com
00006 
00007 This software is provided 'as-is', without any express or implied warranty.
00008 In no event will the authors be held liable for any damages arising from the use of this software.
00009 Permission is granted to anyone to use this software for any purpose,
00010 including commercial applications, and to alter it and redistribute it freely,
00011 subject to the following restrictions:
00012 
00013 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.
00014 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00015 3. This notice may not be removed or altered from any source distribution.
00016 */
00017 
00018 #include "btShapeHull.h"
00019 #include "LinearMath/btConvexHull.h"
00020 
00021 #define NUM_UNITSPHERE_POINTS 42
00022 
00023 static btVector3 btUnitSpherePoints[NUM_UNITSPHERE_POINTS+MAX_PREFERRED_PENETRATION_DIRECTIONS*2] = 
00024 {
00025        btVector3(btScalar(0.000000) , btScalar(-0.000000),btScalar(-1.000000)),
00026        btVector3(btScalar(0.723608) , btScalar(-0.525725),btScalar(-0.447219)),
00027        btVector3(btScalar(-0.276388) , btScalar(-0.850649),btScalar(-0.447219)),
00028        btVector3(btScalar(-0.894426) , btScalar(-0.000000),btScalar(-0.447216)),
00029        btVector3(btScalar(-0.276388) , btScalar(0.850649),btScalar(-0.447220)),
00030        btVector3(btScalar(0.723608) , btScalar(0.525725),btScalar(-0.447219)),
00031        btVector3(btScalar(0.276388) , btScalar(-0.850649),btScalar(0.447220)),
00032        btVector3(btScalar(-0.723608) , btScalar(-0.525725),btScalar(0.447219)),
00033        btVector3(btScalar(-0.723608) , btScalar(0.525725),btScalar(0.447219)),
00034        btVector3(btScalar(0.276388) , btScalar(0.850649),btScalar(0.447219)),
00035        btVector3(btScalar(0.894426) , btScalar(0.000000),btScalar(0.447216)),
00036        btVector3(btScalar(-0.000000) , btScalar(0.000000),btScalar(1.000000)),
00037        btVector3(btScalar(0.425323) , btScalar(-0.309011),btScalar(-0.850654)),
00038        btVector3(btScalar(-0.162456) , btScalar(-0.499995),btScalar(-0.850654)),
00039        btVector3(btScalar(0.262869) , btScalar(-0.809012),btScalar(-0.525738)),
00040        btVector3(btScalar(0.425323) , btScalar(0.309011),btScalar(-0.850654)),
00041        btVector3(btScalar(0.850648) , btScalar(-0.000000),btScalar(-0.525736)),
00042        btVector3(btScalar(-0.525730) , btScalar(-0.000000),btScalar(-0.850652)),
00043        btVector3(btScalar(-0.688190) , btScalar(-0.499997),btScalar(-0.525736)),
00044        btVector3(btScalar(-0.162456) , btScalar(0.499995),btScalar(-0.850654)),
00045        btVector3(btScalar(-0.688190) , btScalar(0.499997),btScalar(-0.525736)),
00046        btVector3(btScalar(0.262869) , btScalar(0.809012),btScalar(-0.525738)),
00047        btVector3(btScalar(0.951058) , btScalar(0.309013),btScalar(0.000000)),
00048        btVector3(btScalar(0.951058) , btScalar(-0.309013),btScalar(0.000000)),
00049        btVector3(btScalar(0.587786) , btScalar(-0.809017),btScalar(0.000000)),
00050        btVector3(btScalar(0.000000) , btScalar(-1.000000),btScalar(0.000000)),
00051        btVector3(btScalar(-0.587786) , btScalar(-0.809017),btScalar(0.000000)),
00052        btVector3(btScalar(-0.951058) , btScalar(-0.309013),btScalar(-0.000000)),
00053        btVector3(btScalar(-0.951058) , btScalar(0.309013),btScalar(-0.000000)),
00054        btVector3(btScalar(-0.587786) , btScalar(0.809017),btScalar(-0.000000)),
00055        btVector3(btScalar(-0.000000) , btScalar(1.000000),btScalar(-0.000000)),
00056        btVector3(btScalar(0.587786) , btScalar(0.809017),btScalar(-0.000000)),
00057        btVector3(btScalar(0.688190) , btScalar(-0.499997),btScalar(0.525736)),
00058        btVector3(btScalar(-0.262869) , btScalar(-0.809012),btScalar(0.525738)),
00059        btVector3(btScalar(-0.850648) , btScalar(0.000000),btScalar(0.525736)),
00060        btVector3(btScalar(-0.262869) , btScalar(0.809012),btScalar(0.525738)),
00061        btVector3(btScalar(0.688190) , btScalar(0.499997),btScalar(0.525736)),
00062        btVector3(btScalar(0.525730) , btScalar(0.000000),btScalar(0.850652)),
00063        btVector3(btScalar(0.162456) , btScalar(-0.499995),btScalar(0.850654)),
00064        btVector3(btScalar(-0.425323) , btScalar(-0.309011),btScalar(0.850654)),
00065        btVector3(btScalar(-0.425323) , btScalar(0.309011),btScalar(0.850654)),
00066        btVector3(btScalar(0.162456) , btScalar(0.499995),btScalar(0.850654))
00067 };
00068 
00069 btShapeHull::btShapeHull (const btConvexShape* shape)
00070 {
00071        m_shape = shape;
00072        m_vertices.clear ();
00073        m_indices.clear();
00074        m_numIndices = 0;
00075 }
00076 
00077 btShapeHull::~btShapeHull ()
00078 {
00079        m_indices.clear();   
00080        m_vertices.clear ();
00081 }
00082 
00083 bool
00084 btShapeHull::buildHull (btScalar margin)
00085 {
00086        int numSampleDirections = NUM_UNITSPHERE_POINTS;
00087        {
00088               int numPDA = m_shape->getNumPreferredPenetrationDirections();
00089               if (numPDA)
00090               {
00091                      for (int i=0;i<numPDA;i++)
00092                      {
00093                             btVector3 norm;
00094                             m_shape->getPreferredPenetrationDirection(i,norm);
00095                             btUnitSpherePoints[numSampleDirections] = norm;
00096                             numSampleDirections++;
00097                      }
00098               }
00099        }
00100 
00101        btVector3 supportPoints[NUM_UNITSPHERE_POINTS+MAX_PREFERRED_PENETRATION_DIRECTIONS*2];
00102        int i;
00103        for (i = 0; i < numSampleDirections; i++)
00104        {
00105               supportPoints[i] = m_shape->localGetSupportingVertex(btUnitSpherePoints[i]);
00106        }
00107 
00108        HullDesc hd;
00109        hd.mFlags = QF_TRIANGLES;
00110        hd.mVcount = numSampleDirections;
00111 
00112 #ifdef BT_USE_DOUBLE_PRECISION
00113        hd.mVertices = &supportPoints[0];
00114        hd.mVertexStride = sizeof(btVector3);
00115 #else
00116        hd.mVertices = &supportPoints[0];
00117        hd.mVertexStride = sizeof (btVector3);
00118 #endif
00119 
00120        HullLibrary hl;
00121        HullResult hr;
00122        if (hl.CreateConvexHull (hd, hr) == QE_FAIL)
00123        {
00124               return false;
00125        }
00126 
00127        m_vertices.resize (hr.mNumOutputVertices);
00128 
00129 
00130        for (i = 0; i < (int)hr.mNumOutputVertices; i++)
00131        {
00132               m_vertices[i] = hr.m_OutputVertices[i];
00133        }
00134        m_numIndices = hr.mNumIndices;
00135        m_indices.resize(m_numIndices);
00136        for (i = 0; i < (int)m_numIndices; i++)
00137        {
00138               m_indices[i] = hr.m_Indices[i];
00139        }
00140 
00141        // free temporary hull result that we just copied
00142        hl.ReleaseResult (hr);
00143 
00144        return true;
00145 }
00146 
00147 int
00148 btShapeHull::numTriangles () const
00149 {
00150        return m_numIndices / 3;
00151 }
00152 
00153 int
00154 btShapeHull::numVertices () const
00155 {
00156        return m_vertices.size ();
00157 }
00158 
00159 int
00160 btShapeHull::numIndices () const
00161 {
00162        return m_numIndices;
00163 }
00164