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

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btConvexTriangleMeshShape.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 "btConvexTriangleMeshShape.h"
17 #include "BulletCollision/CollisionShapes/btCollisionMargin.h"
18
19 #include "LinearMath/btQuaternion.h"
20 #include "BulletCollision/CollisionShapes/btStridingMeshInterface.h"
21
22
23 btConvexTriangleMeshShape ::btConvexTriangleMeshShape (btStridingMeshInterface* meshInterface, bool calcAabb)
24 : btPolyhedralConvexAabbCachingShape(), m_stridingMesh(meshInterface)
25 {
26 m_shapeType = CONVEX_TRIANGLEMESH_SHAPE_PROXYTYPE;
27 if ( calcAabb )
28 recalcLocalAabb();
29 }
30
31
32
33
36 class LocalSupportVertexCallback: public btInternalTriangleIndexCallback
37 {
38
39 btVector3 m_supportVertexLocal;
40 public:
41
42 btScalar m_maxDot;
43 btVector3 m_supportVecLocal;
44
45 LocalSupportVertexCallback(const btVector3& supportVecLocal)
46 : m_supportVertexLocal(btScalar(0.),btScalar(0.),btScalar(0.)),
47 m_maxDot(btScalar(-BT_LARGE_FLOAT)),
48 m_supportVecLocal(supportVecLocal)
49 {
50 }
51
52 virtual void internalProcessTriangleIndex(btVector3* triangle,int partId,int triangleIndex)
53 {
54 (void)triangleIndex;
55 (void)partId;
56
57 for (int i=0;i<3;i++)
58 {
59 btScalar dot = m_supportVecLocal.dot(triangle[i]);
60 if (dot > m_maxDot)
61 {
62 m_maxDot = dot;
63 m_supportVertexLocal = triangle[i];
64 }
65 }
66 }
67
68 btVector3 GetSupportVertexLocal()
69 {
70 return m_supportVertexLocal;
71 }
72
73 };
74
75
76
77
78
79 btVector3 btConvexTriangleMeshShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const
80 {
81 btVector3 supVec(btScalar(0.),btScalar(0.),btScalar(0.));
82
83 btVector3 vec = vec0;
84 btScalar lenSqr = vec.length2();
85 if (lenSqr < btScalar(0.0001))
86 {
87 vec.setValue(1,0,0);
88 } else
89 {
90 btScalar rlen = btScalar(1.) / btSqrt(lenSqr );
91 vec *= rlen;
92 }
93
94 LocalSupportVertexCallback supportCallback(vec);
95 btVector3 aabbMax(btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT));
96 m_stridingMesh->InternalProcessAllTriangles(&supportCallback,-aabbMax,aabbMax);
97 supVec = supportCallback.GetSupportVertexLocal();
98
99 return supVec;
100 }
101
102 void btConvexTriangleMeshShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
103 {
104 //use 'w' component of supportVerticesOut?
105 {
106 for (int i=0;i<numVectors;i++)
107 {
108 supportVerticesOut[i][3] = btScalar(-BT_LARGE_FLOAT);
109 }
110 }
111
113
114
115 for (int j=0;j<numVectors;j++)
116 {
117 const btVector3& vec = vectors[j];
118 LocalSupportVertexCallback supportCallback(vec);
119 btVector3 aabbMax(btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT));
120 m_stridingMesh->InternalProcessAllTriangles(&supportCallback,-aabbMax,aabbMax);
121 supportVerticesOut[j] = supportCallback.GetSupportVertexLocal();
122 }
123
124 }
125
126
127
128 btVector3 btConvexTriangleMeshShape::localGetSupportingVertex(const btVector3& vec)const
129 {
130 btVector3 supVertex = localGetSupportingVertexWithoutMargin(vec);
131
132 if ( getMargin()!=btScalar(0.) )
133 {
134 btVector3 vecnorm = vec;
135 if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
136 {
137 vecnorm.setValue(btScalar(-1.),btScalar(-1.),btScalar(-1.));
138 }
139 vecnorm.normalize();
140 supVertex+= getMargin() * vecnorm;
141 }
142 return supVertex;
143 }
144
145
146
147
148
149
150
151
152
153 //currently just for debugging (drawing), perhaps future support for algebraic continuous collision detection
154 //Please note that you can debug-draw btConvexTriangleMeshShape with the Raytracer Demo
155 int btConvexTriangleMeshShape::getNumVertices() const
156 {
157 //cache this?
158 return 0;
159
160 }
161
162 int btConvexTriangleMeshShape::getNumEdges() const
163 {
164 return 0;
165 }
166
167 void btConvexTriangleMeshShape::getEdge(int ,btVector3& ,btVector3& ) const
168 {
169 btAssert(0);
170 }
171
172 void btConvexTriangleMeshShape::getVertex(int ,btVector3& ) const
173 {
174 btAssert(0);
175 }
176
177 int btConvexTriangleMeshShape::getNumPlanes() const
178 {
179 return 0;
180 }
181
182 void btConvexTriangleMeshShape::getPlane(btVector3& ,btVector3& ,int ) const
183 {
184 btAssert(0);
185 }
186
187 //not yet
188 bool btConvexTriangleMeshShape::isInside(const btVector3& ,btScalar ) const
189 {
190 btAssert(0);
191 return false;
192 }
193
194
195
196 void btConvexTriangleMeshShape::setLocalScaling(const btVector3& scaling)
197 {
198 m_stridingMesh->setScaling(scaling);
199
200 recalcLocalAabb();
201
202 }
203
204
205 const btVector3& btConvexTriangleMeshShape::getLocalScaling() const
206 {
207 return m_stridingMesh->getScaling();
208 }
209
210 void btConvexTriangleMeshShape::calculatePrincipalAxisTransform(btTransform& principal, btVector3& inertia, btScalar& volume) const
211 {
212 class CenterCallback: public btInternalTriangleIndexCallback
213 {
214 bool first;
215 btVector3 ref;
216 btVector3 sum;
217 btScalar volume;
218
219 public:
220
221 CenterCallback() : first(true), ref(0, 0, 0), sum(0, 0, 0), volume(0)
222 {
223 }
224
225 virtual void internalProcessTriangleIndex(btVector3* triangle, int partId, int triangleIndex)
226 {
227 (void) triangleIndex;
228 (void) partId;
229 if (first)
230 {
231 ref = triangle[0];
232 first = false;
233 }
234 else
235 {
236 btScalar vol = btFabs((triangle[0] - ref).triple(triangle[1] - ref, triangle[2] - ref));
237 sum += (btScalar(0.25) * vol) * ((triangle[0] + triangle[1] + triangle[2] + ref));
238 volume += vol;
239 }
240 }
241
242 btVector3 getCenter()
243 {
244 return (volume > 0) ? sum / volume : ref;
245 }
246
247 btScalar getVolume()
248 {
249 return volume * btScalar(1. / 6);
250 }
251
252 };
253
254 class InertiaCallback: public btInternalTriangleIndexCallback
255 {
256 btMatrix3x3 sum;
257 btVector3 center;
258
259 public:
260
261 InertiaCallback(btVector3& center) : sum(0, 0, 0, 0, 0, 0, 0, 0, 0), center(center)
262 {
263 }
264
265 virtual void internalProcessTriangleIndex(btVector3* triangle, int partId, int triangleIndex)
266 {
267 (void) triangleIndex;
268 (void) partId;
269 btMatrix3x3 i;
270 btVector3 a = triangle[0] - center;
271 btVector3 b = triangle[1] - center;
272 btVector3 c = triangle[2] - center;
273 btScalar volNeg = -btFabs(a.triple(b, c)) * btScalar(1. / 6);
274 for (int j = 0; j < 3; j++)
275 {
276 for (int k = 0; k <= j; k++)
277 {
278 i[j][k] = i[k][j] = volNeg * (btScalar(0.1) * (a[j] * a[k] + b[j] * b[k] + c[j] * c[k])
279 + btScalar(0.05) * (a[j] * b[k] + a[k] * b[j] + a[j] * c[k] + a[k] * c[j] + b[j] * c[k] + b[k] * c[j]));
280 }
281 }
282 btScalar i00 = -i[0][0];
283 btScalar i11 = -i[1][1];
284 btScalar i22 = -i[2][2];
285 i[0][0] = i11 + i22;
286 i[1][1] = i22 + i00;
287 i[2][2] = i00 + i11;
288 sum[0] += i[0];
289 sum[1] += i[1];
290 sum[2] += i[2];
291 }
292
293 btMatrix3x3& getInertia()
294 {
295 return sum;
296 }
297
298 };
299
300 CenterCallback centerCallback;
301 btVector3 aabbMax(btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT));
302 m_stridingMesh->InternalProcessAllTriangles(&centerCallback, -aabbMax, aabbMax);
303 btVector3 center = centerCallback.getCenter();
304 principal.setOrigin(center);
305 volume = centerCallback.getVolume();
306
307 InertiaCallback inertiaCallback(center);
308 m_stridingMesh->InternalProcessAllTriangles(&inertiaCallback, -aabbMax, aabbMax);
309
310 btMatrix3x3& i = inertiaCallback.getInertia();
311 i.diagonalize(principal.getBasis(), btScalar(0.00001), 20);
312 inertia.setValue(i[0][0], i[1][1], i[2][2]);
313 inertia /= volume;
314 }
315
btTransform::setOrigin
void setOrigin(const btVector3 &origin)
Set the translational element.
Definition: btTransform.h:150
sum
static T sum(const btAlignedObjectArray< T > &items)
Definition: btSoftBodyHelpers.cpp:84
SIMD_EPSILON
#define SIMD_EPSILON
Definition: btScalar.h:494
btConvexTriangleMeshShape::getEdge
virtual void getEdge(int i, btVector3 &pa, btVector3 &pb) const
Definition: btConvexTriangleMeshShape.cpp:167
btConvexTriangleMeshShape::localGetSupportingVertex
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const
Definition: btConvexTriangleMeshShape.cpp:128
BT_LARGE_FLOAT
#define BT_LARGE_FLOAT
Definition: btScalar.h:280
btVector3::setValue
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:640
btConvexTriangleMeshShape::isInside
virtual bool isInside(const btVector3 &pt, btScalar tolerance) const
Definition: btConvexTriangleMeshShape.cpp:188
LocalSupportVertexCallback
It's not nice to have all this virtual function overhead, so perhaps we can also gather the points on...
Definition: btConvexTriangleMeshShape.cpp:36
btSqrt
btScalar btSqrt(btScalar y)
Definition: btScalar.h:418
btAssert
#define btAssert(x)
Definition: btScalar.h:113
LocalSupportVertexCallback::LocalSupportVertexCallback
LocalSupportVertexCallback(const btVector3 &supportVecLocal)
Definition: btConvexTriangleMeshShape.cpp:45
CONVEX_TRIANGLEMESH_SHAPE_PROXYTYPE
Definition: btBroadphaseProxy.h:34
btStridingMeshInterface.h
btConvexTriangleMeshShape::calculatePrincipalAxisTransform
void calculatePrincipalAxisTransform(btTransform &principal, btVector3 &inertia, btScalar &volume) const
computes the exact moment of inertia and the transform from the coordinate system defined by the prin...
Definition: btConvexTriangleMeshShape.cpp:210
LocalSupportVertexCallback::m_maxDot
btScalar m_maxDot
Definition: btConvexTriangleMeshShape.cpp:42
btConvexTriangleMeshShape::localGetSupportingVertexWithoutMargin
virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3 &vec) const
Definition: btConvexTriangleMeshShape.cpp:79
btStridingMeshInterface::getScaling
const btVector3 & getScaling() const
Definition: btStridingMeshInterface.h:88
btConvexTriangleMeshShape::getVertex
virtual void getVertex(int i, btVector3 &vtx) const
Definition: btConvexTriangleMeshShape.cpp:172
btStridingMeshInterface::setScaling
void setScaling(const btVector3 &scaling)
Definition: btStridingMeshInterface.h:91
btConvexTriangleMeshShape::getLocalScaling
virtual const btVector3 & getLocalScaling() const
Definition: btConvexTriangleMeshShape.cpp:205
btCollisionMargin.h
btInternalTriangleIndexCallback
Definition: btTriangleCallback.h:32
btVector3::dot
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:235
btConvexInternalShape::getMargin
virtual btScalar getMargin() const
Definition: btConvexInternalShape.h:113
btVector3::normalize
btVector3 & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition: btVector3.h:297
btConvexTriangleMeshShape::getNumVertices
virtual int getNumVertices() const
Definition: btConvexTriangleMeshShape.cpp:155
LocalSupportVertexCallback::m_supportVertexLocal
btVector3 m_supportVertexLocal
Definition: btConvexTriangleMeshShape.cpp:39
btConvexTriangleMeshShape::setLocalScaling
virtual void setLocalScaling(const btVector3 &scaling)
Definition: btConvexTriangleMeshShape.cpp:196
btVector3::triple
btScalar triple(const btVector3 &v1, const btVector3 &v2) const
Definition: btVector3.h:416
LocalSupportVertexCallback::m_supportVecLocal
btVector3 m_supportVecLocal
Definition: btConvexTriangleMeshShape.cpp:43
btQuaternion.h
btMatrix3x3::diagonalize
void diagonalize(btMatrix3x3 &rot, btScalar threshold, int maxSteps)
diagonalizes this matrix by the Jacobi method.
Definition: btMatrix3x3.h:660
btConvexTriangleMeshShape::batchedUnitVectorGetSupportingVertexWithoutMargin
virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3 *vectors, btVector3 *supportVerticesOut, int numVectors) const
Definition: btConvexTriangleMeshShape.cpp:102
btConvexTriangleMeshShape::m_stridingMesh
class btStridingMeshInterface * m_stridingMesh
Definition: btConvexTriangleMeshShape.h:28
btTransform::getBasis
btMatrix3x3 & getBasis()
Return the basis matrix for the rotation.
Definition: btTransform.h:112
btConvexTriangleMeshShape.h
LocalSupportVertexCallback::internalProcessTriangleIndex
virtual void internalProcessTriangleIndex(btVector3 *triangle, int partId, int triangleIndex)
Definition: btConvexTriangleMeshShape.cpp:52
btConvexTriangleMeshShape::getPlane
virtual void getPlane(btVector3 &planeNormal, btVector3 &planeSupport, int i) const
Definition: btConvexTriangleMeshShape.cpp:182
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
btVector3::length2
btScalar length2() const
Return the length of the vector squared.
Definition: btVector3.h:257
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
btPolyhedralConvexAabbCachingShape
The btPolyhedralConvexAabbCachingShape adds aabb caching to the btPolyhedralConvexShape.
Definition: btPolyhedralConvexShape.h:73
btStridingMeshInterface
The btStridingMeshInterface is the interface class for high performance generic access to triangle me...
Definition: btStridingMeshInterface.h:30
btConvexTriangleMeshShape::getNumPlanes
virtual int getNumPlanes() const
Definition: btConvexTriangleMeshShape.cpp:177
btMatrix3x3
The btMatrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with...
Definition: btMatrix3x3.h:48
dot
btScalar dot(const btQuaternion &q1, const btQuaternion &q2)
Calculate the dot product between two quaternions.
Definition: btQuaternion.h:845
btCollisionShape::m_shapeType
int m_shapeType
Definition: btCollisionShape.h:30
btPolyhedralConvexAabbCachingShape::recalcLocalAabb
void recalcLocalAabb()
Definition: btPolyhedralConvexShape.cpp:450
LocalSupportVertexCallback::GetSupportVertexLocal
btVector3 GetSupportVertexLocal()
Definition: btConvexTriangleMeshShape.cpp:68
btConvexTriangleMeshShape::getNumEdges
virtual int getNumEdges() const
Definition: btConvexTriangleMeshShape.cpp:162
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:278
btStridingMeshInterface::InternalProcessAllTriangles
virtual void InternalProcessAllTriangles(btInternalTriangleIndexCallback *callback, const btVector3 &aabbMin, const btVector3 &aabbMax) const
Definition: btStridingMeshInterface.cpp:25
btConvexTriangleMeshShape::btConvexTriangleMeshShape
btConvexTriangleMeshShape(btStridingMeshInterface *meshInterface, bool calcAabb=true)
Definition: btConvexTriangleMeshShape.cpp:23
btFabs
btScalar btFabs(btScalar x)
Definition: btScalar.h:449