Blender V2.61 - r43446

btShapeHull.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 //btShapeHull was implemented by John McCutchan.
00017 
00018 
00019 #include "btShapeHull.h"
00020 #include "LinearMath/btConvexHull.h"
00021 
00022 #define NUM_UNITSPHERE_POINTS 42
00023 
00024 btShapeHull::btShapeHull (const btConvexShape* shape)
00025 {
00026     m_shape = shape;
00027     m_vertices.clear ();
00028     m_indices.clear();
00029     m_numIndices = 0;
00030 }
00031 
00032 btShapeHull::~btShapeHull ()
00033 {
00034     m_indices.clear();  
00035     m_vertices.clear ();
00036 }
00037 
00038 bool
00039 btShapeHull::buildHull (btScalar /*margin*/)
00040 {
00041     int numSampleDirections = NUM_UNITSPHERE_POINTS;
00042     {
00043         int numPDA = m_shape->getNumPreferredPenetrationDirections();
00044         if (numPDA)
00045         {
00046             for (int i=0;i<numPDA;i++)
00047             {
00048                 btVector3 norm;
00049                 m_shape->getPreferredPenetrationDirection(i,norm);
00050                 getUnitSpherePoints()[numSampleDirections] = norm;
00051                 numSampleDirections++;
00052             }
00053         }
00054     }
00055 
00056     btVector3 supportPoints[NUM_UNITSPHERE_POINTS+MAX_PREFERRED_PENETRATION_DIRECTIONS*2];
00057     int i;
00058     for (i = 0; i < numSampleDirections; i++)
00059     {
00060         supportPoints[i] = m_shape->localGetSupportingVertex(getUnitSpherePoints()[i]);
00061     }
00062 
00063     HullDesc hd;
00064     hd.mFlags = QF_TRIANGLES;
00065     hd.mVcount = static_cast<unsigned int>(numSampleDirections);
00066 
00067 #ifdef BT_USE_DOUBLE_PRECISION
00068     hd.mVertices = &supportPoints[0];
00069     hd.mVertexStride = sizeof(btVector3);
00070 #else
00071     hd.mVertices = &supportPoints[0];
00072     hd.mVertexStride = sizeof (btVector3);
00073 #endif
00074 
00075     HullLibrary hl;
00076     HullResult hr;
00077     if (hl.CreateConvexHull (hd, hr) == QE_FAIL)
00078     {
00079         return false;
00080     }
00081 
00082     m_vertices.resize (static_cast<int>(hr.mNumOutputVertices));
00083 
00084 
00085     for (i = 0; i < static_cast<int>(hr.mNumOutputVertices); i++)
00086     {
00087         m_vertices[i] = hr.m_OutputVertices[i];
00088     }
00089     m_numIndices = hr.mNumIndices;
00090     m_indices.resize(static_cast<int>(m_numIndices));
00091     for (i = 0; i < static_cast<int>(m_numIndices); i++)
00092     {
00093         m_indices[i] = hr.m_Indices[i];
00094     }
00095 
00096     // free temporary hull result that we just copied
00097     hl.ReleaseResult (hr);
00098 
00099     return true;
00100 }
00101 
00102 int
00103 btShapeHull::numTriangles () const
00104 {
00105     return static_cast<int>(m_numIndices / 3);
00106 }
00107 
00108 int
00109 btShapeHull::numVertices () const
00110 {
00111     return m_vertices.size ();
00112 }
00113 
00114 int
00115 btShapeHull::numIndices () const
00116 {
00117     return static_cast<int>(m_numIndices);
00118 }
00119 
00120 
00121 btVector3* btShapeHull::getUnitSpherePoints()
00122 {
00123     static btVector3 sUnitSpherePoints[NUM_UNITSPHERE_POINTS+MAX_PREFERRED_PENETRATION_DIRECTIONS*2] = 
00124     {
00125         btVector3(btScalar(0.000000) , btScalar(-0.000000),btScalar(-1.000000)),
00126         btVector3(btScalar(0.723608) , btScalar(-0.525725),btScalar(-0.447219)),
00127         btVector3(btScalar(-0.276388) , btScalar(-0.850649),btScalar(-0.447219)),
00128         btVector3(btScalar(-0.894426) , btScalar(-0.000000),btScalar(-0.447216)),
00129         btVector3(btScalar(-0.276388) , btScalar(0.850649),btScalar(-0.447220)),
00130         btVector3(btScalar(0.723608) , btScalar(0.525725),btScalar(-0.447219)),
00131         btVector3(btScalar(0.276388) , btScalar(-0.850649),btScalar(0.447220)),
00132         btVector3(btScalar(-0.723608) , btScalar(-0.525725),btScalar(0.447219)),
00133         btVector3(btScalar(-0.723608) , btScalar(0.525725),btScalar(0.447219)),
00134         btVector3(btScalar(0.276388) , btScalar(0.850649),btScalar(0.447219)),
00135         btVector3(btScalar(0.894426) , btScalar(0.000000),btScalar(0.447216)),
00136         btVector3(btScalar(-0.000000) , btScalar(0.000000),btScalar(1.000000)),
00137         btVector3(btScalar(0.425323) , btScalar(-0.309011),btScalar(-0.850654)),
00138         btVector3(btScalar(-0.162456) , btScalar(-0.499995),btScalar(-0.850654)),
00139         btVector3(btScalar(0.262869) , btScalar(-0.809012),btScalar(-0.525738)),
00140         btVector3(btScalar(0.425323) , btScalar(0.309011),btScalar(-0.850654)),
00141         btVector3(btScalar(0.850648) , btScalar(-0.000000),btScalar(-0.525736)),
00142         btVector3(btScalar(-0.525730) , btScalar(-0.000000),btScalar(-0.850652)),
00143         btVector3(btScalar(-0.688190) , btScalar(-0.499997),btScalar(-0.525736)),
00144         btVector3(btScalar(-0.162456) , btScalar(0.499995),btScalar(-0.850654)),
00145         btVector3(btScalar(-0.688190) , btScalar(0.499997),btScalar(-0.525736)),
00146         btVector3(btScalar(0.262869) , btScalar(0.809012),btScalar(-0.525738)),
00147         btVector3(btScalar(0.951058) , btScalar(0.309013),btScalar(0.000000)),
00148         btVector3(btScalar(0.951058) , btScalar(-0.309013),btScalar(0.000000)),
00149         btVector3(btScalar(0.587786) , btScalar(-0.809017),btScalar(0.000000)),
00150         btVector3(btScalar(0.000000) , btScalar(-1.000000),btScalar(0.000000)),
00151         btVector3(btScalar(-0.587786) , btScalar(-0.809017),btScalar(0.000000)),
00152         btVector3(btScalar(-0.951058) , btScalar(-0.309013),btScalar(-0.000000)),
00153         btVector3(btScalar(-0.951058) , btScalar(0.309013),btScalar(-0.000000)),
00154         btVector3(btScalar(-0.587786) , btScalar(0.809017),btScalar(-0.000000)),
00155         btVector3(btScalar(-0.000000) , btScalar(1.000000),btScalar(-0.000000)),
00156         btVector3(btScalar(0.587786) , btScalar(0.809017),btScalar(-0.000000)),
00157         btVector3(btScalar(0.688190) , btScalar(-0.499997),btScalar(0.525736)),
00158         btVector3(btScalar(-0.262869) , btScalar(-0.809012),btScalar(0.525738)),
00159         btVector3(btScalar(-0.850648) , btScalar(0.000000),btScalar(0.525736)),
00160         btVector3(btScalar(-0.262869) , btScalar(0.809012),btScalar(0.525738)),
00161         btVector3(btScalar(0.688190) , btScalar(0.499997),btScalar(0.525736)),
00162         btVector3(btScalar(0.525730) , btScalar(0.000000),btScalar(0.850652)),
00163         btVector3(btScalar(0.162456) , btScalar(-0.499995),btScalar(0.850654)),
00164         btVector3(btScalar(-0.425323) , btScalar(-0.309011),btScalar(0.850654)),
00165         btVector3(btScalar(-0.425323) , btScalar(0.309011),btScalar(0.850654)),
00166         btVector3(btScalar(0.162456) , btScalar(0.499995),btScalar(0.850654))
00167     };
00168     return sUnitSpherePoints;
00169 }
00170