Blender  V3.3
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 #include "btTriangleMesh.h"
17 
18 btTriangleMesh::btTriangleMesh(bool use32bitIndices, bool use4componentVertices)
19  : m_use32bitIndices(use32bitIndices),
20  m_use4componentVertices(use4componentVertices),
21  m_weldingThreshold(0.0)
22 {
23  btIndexedMesh meshIndex;
24  meshIndex.m_numTriangles = 0;
25  meshIndex.m_numVertices = 0;
26  meshIndex.m_indexType = PHY_INTEGER;
27  meshIndex.m_triangleIndexBase = 0;
28  meshIndex.m_triangleIndexStride = 3 * sizeof(int);
29  meshIndex.m_vertexBase = 0;
30  meshIndex.m_vertexStride = sizeof(btVector3);
31  m_indexedMeshes.push_back(meshIndex);
32 
33  if (m_use32bitIndices)
34  {
35  m_indexedMeshes[0].m_numTriangles = m_32bitIndices.size() / 3;
36  m_indexedMeshes[0].m_triangleIndexBase = 0;
37  m_indexedMeshes[0].m_indexType = PHY_INTEGER;
38  m_indexedMeshes[0].m_triangleIndexStride = 3 * sizeof(int);
39  }
40  else
41  {
42  m_indexedMeshes[0].m_numTriangles = m_16bitIndices.size() / 3;
43  m_indexedMeshes[0].m_triangleIndexBase = 0;
44  m_indexedMeshes[0].m_indexType = PHY_SHORT;
45  m_indexedMeshes[0].m_triangleIndexStride = 3 * sizeof(short int);
46  }
47 
48  if (m_use4componentVertices)
49  {
50  m_indexedMeshes[0].m_numVertices = m_4componentVertices.size();
51  m_indexedMeshes[0].m_vertexBase = 0;
52  m_indexedMeshes[0].m_vertexStride = sizeof(btVector3);
53  }
54  else
55  {
56  m_indexedMeshes[0].m_numVertices = m_3componentVertices.size() / 3;
57  m_indexedMeshes[0].m_vertexBase = 0;
58  m_indexedMeshes[0].m_vertexStride = 3 * sizeof(btScalar);
59  }
60 }
61 
62 void btTriangleMesh::addIndex(int index)
63 {
64  if (m_use32bitIndices)
65  {
66  m_32bitIndices.push_back(index);
67  m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*)&m_32bitIndices[0];
68  }
69  else
70  {
71  m_16bitIndices.push_back(index);
72  m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*)&m_16bitIndices[0];
73  }
74 }
75 
76 void btTriangleMesh::addTriangleIndices(int index1, int index2, int index3)
77 {
78  m_indexedMeshes[0].m_numTriangles++;
79  addIndex(index1);
80  addIndex(index2);
81  addIndex(index3);
82 }
83 
84 int btTriangleMesh::findOrAddVertex(const btVector3& vertex, bool removeDuplicateVertices)
85 {
86  //return index of new/existing vertex
88  if (m_use4componentVertices)
89  {
90  if (removeDuplicateVertices)
91  {
92  for (int i = 0; i < m_4componentVertices.size(); i++)
93  {
94  if ((m_4componentVertices[i] - vertex).length2() <= m_weldingThreshold)
95  {
96  return i;
97  }
98  }
99  }
100  m_indexedMeshes[0].m_numVertices++;
101  m_4componentVertices.push_back(vertex);
102  m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_4componentVertices[0];
103 
104  return m_4componentVertices.size() - 1;
105  }
106  else
107  {
108  if (removeDuplicateVertices)
109  {
110  for (int i = 0; i < m_3componentVertices.size(); i += 3)
111  {
112  btVector3 vtx(m_3componentVertices[i], m_3componentVertices[i + 1], m_3componentVertices[i + 2]);
113  if ((vtx - vertex).length2() <= m_weldingThreshold)
114  {
115  return i / 3;
116  }
117  }
118  }
119  m_3componentVertices.push_back(vertex.getX());
120  m_3componentVertices.push_back(vertex.getY());
121  m_3componentVertices.push_back(vertex.getZ());
122  m_indexedMeshes[0].m_numVertices++;
123  m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_3componentVertices[0];
124  return (m_3componentVertices.size() / 3) - 1;
125  }
126 }
127 
128 void btTriangleMesh::addTriangle(const btVector3& vertex0, const btVector3& vertex1, const btVector3& vertex2, bool removeDuplicateVertices)
129 {
130  m_indexedMeshes[0].m_numTriangles++;
131  addIndex(findOrAddVertex(vertex0, removeDuplicateVertices));
132  addIndex(findOrAddVertex(vertex1, removeDuplicateVertices));
133  addIndex(findOrAddVertex(vertex2, removeDuplicateVertices));
134 }
135 
137 {
138  if (m_use32bitIndices)
139  {
140  return m_32bitIndices.size() / 3;
141  }
142  return m_16bitIndices.size() / 3;
143 }
144 
146 {
147  if (m_use4componentVertices)
148  {
149  m_4componentVertices.reserve(numverts);
150  }
151  else
152  {
153  m_3componentVertices.reserve(numverts);
154  }
155 }
156 
158 {
159  if (m_use32bitIndices)
160  {
161  m_32bitIndices.reserve(numindices);
162  }
163  else
164  {
165  m_16bitIndices.reserve(numindices);
166  }
167 }
@ PHY_SHORT
@ PHY_INTEGER
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
SIMD_FORCE_INLINE btScalar length2() const
Return the length of the vector squared.
Definition: btVector3.h:251
btVector3
btVector3 can be used to represent 3D points and vectors. It has an un-used w component to suit 16-by...
Definition: btVector3.h:82
SIMD_FORCE_INLINE void reserve(int _Count)
SIMD_FORCE_INLINE int size() const
return the number of elements in the array
SIMD_FORCE_INLINE void push_back(const T &_Val)
virtual void preallocateIndices(int numindices)
btScalar m_weldingThreshold
void addTriangle(const btVector3 &vertex0, const btVector3 &vertex1, const btVector3 &vertex2, bool removeDuplicateVertices=false)
int getNumTriangles() const
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)
virtual void preallocateVertices(int numverts)
void addIndex(int index)
addIndex is an internal method, use addTriangle instead