Bullet Collision Detection & Physics Library: btTriangleMesh.cpp Source File

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btTriangleMesh.cpp
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16
17 #include "btTriangleMesh.h"
18
19
20
21 btTriangleMesh::btTriangleMesh (bool use32bitIndices,bool use4componentVertices)
22 :m_use32bitIndices(use32bitIndices),
23 m_use4componentVertices(use4componentVertices),
24 m_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
36 if (m_use32bitIndices)
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
50 if (m_use4componentVertices)
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
65 void btTriangleMesh::addIndex(int index)
66 {
67 if (m_use32bitIndices)
68 {
69 m_32bitIndices.push_back(index);
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
78 void 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
86 int btTriangleMesh::findOrAddVertex(const btVector3& vertex, bool removeDuplicateVertices)
87 {
88 //return index of new/existing vertex
90 if (m_use4componentVertices)
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++;
103 m_4componentVertices.push_back(vertex);
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 {
115 btVector3 vtx(m_3componentVertices[i],m_3componentVertices[i+1],m_3componentVertices[i+2]);
116 if ((vtx-vertex).length2() <= m_weldingThreshold)
117 {
118 return i/3;
119 }
120 }
121 }
122 m_3componentVertices.push_back(vertex.getX());
123 m_3componentVertices.push_back(vertex.getY());
124 m_3componentVertices.push_back(vertex.getZ());
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
132 void 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
140 int btTriangleMesh::getNumTriangles() const
141 {
142 if (m_use32bitIndices)
143 {
144 return m_32bitIndices.size() / 3;
145 }
146 return m_16bitIndices.size() / 3;
147 }
148
149 void btTriangleMesh::preallocateVertices(int numverts)
150 {
151 if (m_use4componentVertices)
152 {
153 m_4componentVertices.reserve(numverts);
154 } else
155 {
156 m_3componentVertices.reserve(numverts);
157 }
158 }
159
160 void btTriangleMesh::preallocateIndices(int numindices)
161 {
162 if (m_use32bitIndices)
163 {
164 m_32bitIndices.reserve(numindices);
165 } else
166 {
167 m_16bitIndices.reserve(numindices);
168 }
169 }
btIndexedMesh::m_numTriangles
int m_numTriangles
Definition: btTriangleIndexVertexArray.h:30
btAlignedObjectArray::reserve
void reserve(int _Count)
Definition: btAlignedObjectArray.h:292
btAlignedObjectArray::push_back
void push_back(const T &_Val)
Definition: btAlignedObjectArray.h:268
btIndexedMesh
The btIndexedMesh indexes a single vertex and index array.
Definition: btTriangleIndexVertexArray.h:26
btTriangleMesh::m_3componentVertices
btAlignedObjectArray< btScalar > m_3componentVertices
Definition: btTriangleMesh.h:30
btTriangleMesh::m_32bitIndices
btAlignedObjectArray< unsigned int > m_32bitIndices
Definition: btTriangleMesh.h:32
btTriangleMesh::m_weldingThreshold
btScalar m_weldingThreshold
Definition: btTriangleMesh.h:39
btIndexedMesh::m_indexType
PHY_ScalarType m_indexType
Definition: btTriangleIndexVertexArray.h:41
btVector3::getZ
const btScalar & getZ() const
Return the z value.
Definition: btVector3.h:565
btTriangleMesh::addIndex
void addIndex(int index)
addIndex is an internal method, use addTriangle instead
Definition: btTriangleMesh.cpp:65
btTriangleIndexVertexArray::m_indexedMeshes
IndexedMeshArray m_indexedMeshes
Definition: btTriangleIndexVertexArray.h:71
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:149
btIndexedMesh::m_triangleIndexStride
int m_triangleIndexStride
Definition: btTriangleIndexVertexArray.h:33
btTriangleMesh::addTriangleIndices
void addTriangleIndices(int index1, int index2, int index3)
Add a triangle using its indices. Make sure the indices are pointing within the vertices array...
Definition: btTriangleMesh.cpp:78
btTriangleMesh::m_use4componentVertices
bool m_use4componentVertices
Definition: btTriangleMesh.h:35
PHY_SHORT
Definition: btConcaveShape.h:29
btVector3::getY
const btScalar & getY() const
Return the y value.
Definition: btVector3.h:563
btVector3::getX
const btScalar & getX() const
Return the x value.
Definition: btVector3.h:561
btIndexedMesh::m_triangleIndexBase
const unsigned char * m_triangleIndexBase
Definition: btTriangleIndexVertexArray.h:31
btTriangleMesh::m_use32bitIndices
bool m_use32bitIndices
Definition: btTriangleMesh.h:34
btTriangleMesh::preallocateIndices
virtual void preallocateIndices(int numindices)
Definition: btTriangleMesh.cpp:160
btTriangleMesh::m_16bitIndices
btAlignedObjectArray< unsigned short int > m_16bitIndices
Definition: btTriangleMesh.h:33
btTriangleMesh::btTriangleMesh
btTriangleMesh(bool use32bitIndices=true, bool use4componentVertices=true)
Definition: btTriangleMesh.cpp:21
btTriangleMesh::findOrAddVertex
int findOrAddVertex(const btVector3 &vertex, bool removeDuplicateVertices)
findOrAddVertex is an internal method, use addTriangle instead
Definition: btTriangleMesh.cpp:86
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
btTriangleMesh::m_4componentVertices
btAlignedObjectArray< btVector3 > m_4componentVertices
Definition: btTriangleMesh.h:29
btTriangleMesh::preallocateVertices
virtual void preallocateVertices(int numverts)
Definition: btTriangleMesh.cpp:149
PHY_INTEGER
Definition: btConcaveShape.h:28
btIndexedMesh::m_numVertices
int m_numVertices
Definition: btTriangleIndexVertexArray.h:34
btTriangleMesh.h
btIndexedMesh::m_vertexStride
int m_vertexStride
Definition: btTriangleIndexVertexArray.h:37
btTriangleMesh::addTriangle
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...
Definition: btTriangleMesh.cpp:132
btTriangleMesh::getNumTriangles
int getNumTriangles() const
Definition: btTriangleMesh.cpp:140
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:278
btIndexedMesh::m_vertexBase
const unsigned char * m_vertexBase
Definition: btTriangleIndexVertexArray.h:35