btTriangleMesh.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 
00017 #include "btTriangleMesh.h"
00018 
00019 
00020 
00021 btTriangleMesh::btTriangleMesh (bool use32bitIndices,bool use4componentVertices)
00022 :m_use32bitIndices(use32bitIndices),
00023 m_use4componentVertices(use4componentVertices),
00024 m_weldingThreshold(0.0)
00025 {
00026         btIndexedMesh meshIndex;
00027         meshIndex.m_numTriangles = 0;
00028         meshIndex.m_numVertices = 0;
00029         meshIndex.m_indexType = PHY_INTEGER;
00030         meshIndex.m_triangleIndexBase = 0;
00031         meshIndex.m_triangleIndexStride = 3*sizeof(int);
00032         meshIndex.m_vertexBase = 0;
00033         meshIndex.m_vertexStride = sizeof(btVector3);
00034         m_indexedMeshes.push_back(meshIndex);
00035 
00036         if (m_use32bitIndices)
00037         {
00038                 m_indexedMeshes[0].m_numTriangles = m_32bitIndices.size()/3;
00039                 m_indexedMeshes[0].m_triangleIndexBase = 0;
00040                 m_indexedMeshes[0].m_indexType = PHY_INTEGER;
00041                 m_indexedMeshes[0].m_triangleIndexStride = 3*sizeof(int);
00042         } else
00043         {
00044                 m_indexedMeshes[0].m_numTriangles = m_16bitIndices.size()/3;
00045                 m_indexedMeshes[0].m_triangleIndexBase = 0;
00046                 m_indexedMeshes[0].m_indexType = PHY_SHORT;
00047                 m_indexedMeshes[0].m_triangleIndexStride = 3*sizeof(short int);
00048         }
00049 
00050         if (m_use4componentVertices)
00051         {
00052                 m_indexedMeshes[0].m_numVertices = m_4componentVertices.size();
00053                 m_indexedMeshes[0].m_vertexBase = 0;
00054                 m_indexedMeshes[0].m_vertexStride = sizeof(btVector3);
00055         } else
00056         {
00057                 m_indexedMeshes[0].m_numVertices = m_3componentVertices.size()/3;
00058                 m_indexedMeshes[0].m_vertexBase = 0;
00059                 m_indexedMeshes[0].m_vertexStride = 3*sizeof(btScalar);
00060         }
00061 
00062 
00063 }
00064 
00065 void    btTriangleMesh::addIndex(int index)
00066 {
00067         if (m_use32bitIndices)
00068         {
00069                 m_32bitIndices.push_back(index);
00070                 m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_32bitIndices[0];
00071         } else
00072         {
00073                 m_16bitIndices.push_back(index);
00074                 m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_16bitIndices[0];
00075         }
00076 }
00077 
00078 
00079 int     btTriangleMesh::findOrAddVertex(const btVector3& vertex, bool removeDuplicateVertices)
00080 {
00081         //return index of new/existing vertex
00083         if (m_use4componentVertices)
00084         {
00085                 if (removeDuplicateVertices)
00086                         {
00087                         for (int i=0;i< m_4componentVertices.size();i++)
00088                         {
00089                                 if ((m_4componentVertices[i]-vertex).length2() <= m_weldingThreshold)
00090                                 {
00091                                         return i;
00092                                 }
00093                         }
00094                 }
00095                 m_indexedMeshes[0].m_numVertices++;
00096                 m_4componentVertices.push_back(vertex);
00097                 m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_4componentVertices[0];
00098 
00099                 return m_4componentVertices.size()-1;
00100                 
00101         } else
00102         {
00103                 
00104                 if (removeDuplicateVertices)
00105                 {
00106                         for (int i=0;i< m_3componentVertices.size();i+=3)
00107                         {
00108                                 btVector3 vtx(m_3componentVertices[i],m_3componentVertices[i+1],m_3componentVertices[i+2]);
00109                                 if ((vtx-vertex).length2() <= m_weldingThreshold)
00110                                 {
00111                                         return i/3;
00112                                 }
00113                         }
00114         }
00115                 m_3componentVertices.push_back((float)vertex.getX());
00116                 m_3componentVertices.push_back((float)vertex.getY());
00117                 m_3componentVertices.push_back((float)vertex.getZ());
00118                 m_indexedMeshes[0].m_numVertices++;
00119                 m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_3componentVertices[0];
00120                 return (m_3componentVertices.size()/3)-1;
00121         }
00122 
00123 }
00124                 
00125 void    btTriangleMesh::addTriangle(const btVector3& vertex0,const btVector3& vertex1,const btVector3& vertex2,bool removeDuplicateVertices)
00126 {
00127         m_indexedMeshes[0].m_numTriangles++;
00128         addIndex(findOrAddVertex(vertex0,removeDuplicateVertices));
00129         addIndex(findOrAddVertex(vertex1,removeDuplicateVertices));
00130         addIndex(findOrAddVertex(vertex2,removeDuplicateVertices));
00131 }
00132 
00133 int btTriangleMesh::getNumTriangles() const
00134 {
00135         if (m_use32bitIndices)
00136         {
00137                 return m_32bitIndices.size() / 3;
00138         }
00139         return m_16bitIndices.size() / 3;
00140 }
00141 
00142 void btTriangleMesh::preallocateVertices(int numverts)
00143 {
00144         if (m_use4componentVertices)
00145         {
00146                 m_4componentVertices.reserve(numverts);
00147         } else
00148         {
00149                 m_3componentVertices.reserve(numverts);
00150         }
00151 }
00152 
00153 void btTriangleMesh::preallocateIndices(int numindices)
00154 {
00155         if (m_use32bitIndices)
00156         {
00157                 m_32bitIndices.reserve(numindices);
00158         } else
00159         {
00160                 m_16bitIndices.reserve(numindices);
00161         }
00162 }