Bullet Collision Detection & Physics Library
btTriangleMesh.cpp
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16
17#include "btTriangleMesh.h"
18
19
20
21btTriangleMesh::btTriangleMesh (bool use32bitIndices,bool use4componentVertices)
22:m_use32bitIndices(use32bitIndices),
23m_use4componentVertices(use4componentVertices),
24m_weldingThreshold(0.0)
25{
26 btIndexedMesh meshIndex;
27 meshIndex.m_numTriangles = 0;
28 meshIndex.m_numVertices = 0;
29 meshIndex.m_indexType = PHY_INTEGER;
30 meshIndex.m_triangleIndexBase = 0;
31 meshIndex.m_triangleIndexStride = 3*sizeof(int);
32 meshIndex.m_vertexBase = 0;
33 meshIndex.m_vertexStride = sizeof(btVector3);
34 m_indexedMeshes.push_back(meshIndex);
35
37 {
38 m_indexedMeshes[0].m_numTriangles = m_32bitIndices.size()/3;
39 m_indexedMeshes[0].m_triangleIndexBase = 0;
40 m_indexedMeshes[0].m_indexType = PHY_INTEGER;
41 m_indexedMeshes[0].m_triangleIndexStride = 3*sizeof(int);
42 } else
43 {
44 m_indexedMeshes[0].m_numTriangles = m_16bitIndices.size()/3;
45 m_indexedMeshes[0].m_triangleIndexBase = 0;
46 m_indexedMeshes[0].m_indexType = PHY_SHORT;
47 m_indexedMeshes[0].m_triangleIndexStride = 3*sizeof(short int);
48 }
49
51 {
52 m_indexedMeshes[0].m_numVertices = m_4componentVertices.size();
53 m_indexedMeshes[0].m_vertexBase = 0;
54 m_indexedMeshes[0].m_vertexStride = sizeof(btVector3);
55 } else
56 {
57 m_indexedMeshes[0].m_numVertices = m_3componentVertices.size()/3;
58 m_indexedMeshes[0].m_vertexBase = 0;
59 m_indexedMeshes[0].m_vertexStride = 3*sizeof(btScalar);
60 }
61
62
63}
64
66{
68 {
70 m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_32bitIndices[0];
71 } else
72 {
73 m_16bitIndices.push_back(index);
74 m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_16bitIndices[0];
75 }
76}
77
78void btTriangleMesh::addTriangleIndices(int index1, int index2, int index3 )
79{
80 m_indexedMeshes[0].m_numTriangles++;
81 addIndex( index1 );
82 addIndex( index2 );
83 addIndex( index3 );
84}
85
86int btTriangleMesh::findOrAddVertex(const btVector3& vertex, bool removeDuplicateVertices)
87{
88 //return index of new/existing vertex
91 {
92 if (removeDuplicateVertices)
93 {
94 for (int i=0;i< m_4componentVertices.size();i++)
95 {
96 if ((m_4componentVertices[i]-vertex).length2() <= m_weldingThreshold)
97 {
98 return i;
99 }
100 }
101 }
102 m_indexedMeshes[0].m_numVertices++;
104 m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_4componentVertices[0];
105
106 return m_4componentVertices.size()-1;
107
108 } else
109 {
110
111 if (removeDuplicateVertices)
112 {
113 for (int i=0;i< m_3componentVertices.size();i+=3)
114 {
116 if ((vtx-vertex).length2() <= m_weldingThreshold)
117 {
118 return i/3;
119 }
120 }
121 }
125 m_indexedMeshes[0].m_numVertices++;
126 m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_3componentVertices[0];
127 return (m_3componentVertices.size()/3)-1;
128 }
129
130}
131
132void btTriangleMesh::addTriangle(const btVector3& vertex0,const btVector3& vertex1,const btVector3& vertex2,bool removeDuplicateVertices)
133{
134 m_indexedMeshes[0].m_numTriangles++;
135 addIndex(findOrAddVertex(vertex0,removeDuplicateVertices));
136 addIndex(findOrAddVertex(vertex1,removeDuplicateVertices));
137 addIndex(findOrAddVertex(vertex2,removeDuplicateVertices));
138}
139
141{
143 {
144 return m_32bitIndices.size() / 3;
145 }
146 return m_16bitIndices.size() / 3;
147}
148
150{
152 {
154 } else
155 {
157 }
158}
159
161{
163 {
164 m_32bitIndices.reserve(numindices);
165 } else
166 {
167 m_16bitIndices.reserve(numindices);
168 }
169}
@ PHY_SHORT
@ PHY_INTEGER
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292
int size() const
return the number of elements in the array
void push_back(const T &_Val)
virtual void preallocateIndices(int numindices)
btScalar m_weldingThreshold
btAlignedObjectArray< btVector3 > m_4componentVertices
void addTriangle(const btVector3 &vertex0, const btVector3 &vertex1, const btVector3 &vertex2, bool removeDuplicateVertices=false)
By default addTriangle won't search for duplicate vertices, because the search is very slow for large...
int getNumTriangles() const
btAlignedObjectArray< unsigned short int > m_16bitIndices
int findOrAddVertex(const btVector3 &vertex, bool removeDuplicateVertices)
findOrAddVertex is an internal method, use addTriangle instead
void addTriangleIndices(int index1, int index2, int index3)
Add a triangle using its indices. Make sure the indices are pointing within the vertices array,...
btTriangleMesh(bool use32bitIndices=true, bool use4componentVertices=true)
btAlignedObjectArray< unsigned int > m_32bitIndices
virtual void preallocateVertices(int numverts)
btAlignedObjectArray< btScalar > m_3componentVertices
void addIndex(int index)
addIndex is an internal method, use addTriangle instead
bool m_use4componentVertices
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:84
const btScalar & getZ() const
Return the z value.
Definition: btVector3.h:577
const btScalar & getY() const
Return the y value.
Definition: btVector3.h:575
const btScalar & getX() const
Return the x value.
Definition: btVector3.h:573
The btIndexedMesh indexes a single vertex and index array.
PHY_ScalarType m_indexType
const unsigned char * m_vertexBase
const unsigned char * m_triangleIndexBase