Bullet Collision Detection & Physics Library
btConvexTriangleMeshShape.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
18
21
22btConvexTriangleMeshShape ::btConvexTriangleMeshShape(btStridingMeshInterface* meshInterface, bool calcAabb)
23 : btPolyhedralConvexAabbCachingShape(), m_stridingMesh(meshInterface)
24{
26 if (calcAabb)
28}
29
33{
35
36public:
39
40 LocalSupportVertexCallback(const btVector3& supportVecLocal)
43 m_supportVecLocal(supportVecLocal)
44 {
45 }
46
47 virtual void internalProcessTriangleIndex(btVector3* triangle, int partId, int triangleIndex)
48 {
49 (void)triangleIndex;
50 (void)partId;
51
52 for (int i = 0; i < 3; i++)
53 {
54 btScalar dot = m_supportVecLocal.dot(triangle[i]);
55 if (dot > m_maxDot)
56 {
57 m_maxDot = dot;
58 m_supportVertexLocal = triangle[i];
59 }
60 }
61 }
62
67};
68
70{
71 btVector3 supVec(btScalar(0.), btScalar(0.), btScalar(0.));
72
73 btVector3 vec = vec0;
74 btScalar lenSqr = vec.length2();
75 if (lenSqr < btScalar(0.0001))
76 {
77 vec.setValue(1, 0, 0);
78 }
79 else
80 {
81 btScalar rlen = btScalar(1.) / btSqrt(lenSqr);
82 vec *= rlen;
83 }
84
85 LocalSupportVertexCallback supportCallback(vec);
87 m_stridingMesh->InternalProcessAllTriangles(&supportCallback, -aabbMax, aabbMax);
88 supVec = supportCallback.GetSupportVertexLocal();
89
90 return supVec;
91}
92
93void btConvexTriangleMeshShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const
94{
95 //use 'w' component of supportVerticesOut?
96 {
97 for (int i = 0; i < numVectors; i++)
98 {
99 supportVerticesOut[i][3] = btScalar(-BT_LARGE_FLOAT);
100 }
101 }
102
104
105 for (int j = 0; j < numVectors; j++)
106 {
107 const btVector3& vec = vectors[j];
108 LocalSupportVertexCallback supportCallback(vec);
110 m_stridingMesh->InternalProcessAllTriangles(&supportCallback, -aabbMax, aabbMax);
111 supportVerticesOut[j] = supportCallback.GetSupportVertexLocal();
112 }
113}
114
116{
118
119 if (getMargin() != btScalar(0.))
120 {
121 btVector3 vecnorm = vec;
122 if (vecnorm.length2() < (SIMD_EPSILON * SIMD_EPSILON))
123 {
124 vecnorm.setValue(btScalar(-1.), btScalar(-1.), btScalar(-1.));
125 }
126 vecnorm.normalize();
127 supVertex += getMargin() * vecnorm;
128 }
129 return supVertex;
130}
131
132//currently just for debugging (drawing), perhaps future support for algebraic continuous collision detection
133//Please note that you can debug-draw btConvexTriangleMeshShape with the Raytracer Demo
135{
136 //cache this?
137 return 0;
138}
139
141{
142 return 0;
143}
144
149
151{
152 btAssert(0);
153}
154
156{
157 return 0;
158}
159
164
165//not yet
167{
168 btAssert(0);
169 return false;
170}
171
178
183
185{
186 class CenterCallback : public btInternalTriangleIndexCallback
187 {
188 bool first;
189 btVector3 ref;
191 btScalar volume;
192
193 public:
194 CenterCallback() : first(true), ref(0, 0, 0), sum(0, 0, 0), volume(0)
195 {
196 }
197
198 virtual void internalProcessTriangleIndex(btVector3* triangle, int partId, int triangleIndex)
199 {
200 (void)triangleIndex;
201 (void)partId;
202 if (first)
203 {
204 ref = triangle[0];
205 first = false;
206 }
207 else
208 {
209 btScalar vol = btFabs((triangle[0] - ref).triple(triangle[1] - ref, triangle[2] - ref));
210 sum += (btScalar(0.25) * vol) * ((triangle[0] + triangle[1] + triangle[2] + ref));
211 volume += vol;
212 }
213 }
214
215 btVector3 getCenter()
216 {
217 return (volume > 0) ? sum / volume : ref;
218 }
219
220 btScalar getVolume()
221 {
222 return volume * btScalar(1. / 6);
223 }
224 };
225
226 class InertiaCallback : public btInternalTriangleIndexCallback
227 {
229 btVector3 center;
230
231 public:
232 InertiaCallback(btVector3& center) : sum(0, 0, 0, 0, 0, 0, 0, 0, 0), center(center)
233 {
234 }
235
236 virtual void internalProcessTriangleIndex(btVector3* triangle, int partId, int triangleIndex)
237 {
238 (void)triangleIndex;
239 (void)partId;
240 btMatrix3x3 i;
241 btVector3 a = triangle[0] - center;
242 btVector3 b = triangle[1] - center;
243 btVector3 c = triangle[2] - center;
244 btScalar volNeg = -btFabs(a.triple(b, c)) * btScalar(1. / 6);
245 for (int j = 0; j < 3; j++)
246 {
247 for (int k = 0; k <= j; k++)
248 {
249 i[j][k] = i[k][j] = volNeg * (btScalar(0.1) * (a[j] * a[k] + b[j] * b[k] + c[j] * c[k]) + 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]));
250 }
251 }
252 btScalar i00 = -i[0][0];
253 btScalar i11 = -i[1][1];
254 btScalar i22 = -i[2][2];
255 i[0][0] = i11 + i22;
256 i[1][1] = i22 + i00;
257 i[2][2] = i00 + i11;
258 sum[0] += i[0];
259 sum[1] += i[1];
260 sum[2] += i[2];
261 }
262
263 btMatrix3x3& getInertia()
264 {
265 return sum;
266 }
267 };
268
269 CenterCallback centerCallback;
271 m_stridingMesh->InternalProcessAllTriangles(&centerCallback, -aabbMax, aabbMax);
272 btVector3 center = centerCallback.getCenter();
273 principal.setOrigin(center);
274 volume = centerCallback.getVolume();
275
276 InertiaCallback inertiaCallback(center);
277 m_stridingMesh->InternalProcessAllTriangles(&inertiaCallback, -aabbMax, aabbMax);
278
279 btMatrix3x3& i = inertiaCallback.getInertia();
280 i.diagonalize(principal.getBasis(), btScalar(0.00001), 20);
281 inertia.setValue(i[0][0], i[1][1], i[2][2]);
282 inertia /= volume;
283}
@ CONVEX_TRIANGLEMESH_SHAPE_PROXYTYPE
btScalar dot(const btQuaternion &q1, const btQuaternion &q2)
Calculate the dot product between two quaternions.
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition btScalar.h:314
#define BT_LARGE_FLOAT
Definition btScalar.h:316
btScalar btSqrt(btScalar y)
Definition btScalar.h:466
btScalar btFabs(btScalar x)
Definition btScalar.h:497
#define SIMD_EPSILON
Definition btScalar.h:543
#define btAssert(x)
Definition btScalar.h:153
static T sum(const btAlignedObjectArray< T > &items)
It's not nice to have all this virtual function overhead, so perhaps we can also gather the points on...
LocalSupportVertexCallback(const btVector3 &supportVecLocal)
virtual void internalProcessTriangleIndex(btVector3 *triangle, int partId, int triangleIndex)
virtual btScalar getMargin() const
class btStridingMeshInterface * m_stridingMesh
virtual const btVector3 & getLocalScaling() const
virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3 *vectors, btVector3 *supportVerticesOut, int numVectors) const
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const
virtual void setLocalScaling(const btVector3 &scaling)
virtual bool isInside(const btVector3 &pt, btScalar tolerance) const
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...
virtual void getVertex(int i, btVector3 &vtx) const
virtual void getEdge(int i, btVector3 &pa, btVector3 &pb) const
virtual void getPlane(btVector3 &planeNormal, btVector3 &planeSupport, int i) const
virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3 &vec) const
The btMatrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with...
Definition btMatrix3x3.h:50
void diagonalize(btMatrix3x3 &rot, btScalar threshold, int maxSteps)
diagonalizes this matrix by the Jacobi method.
The btPolyhedralConvexAabbCachingShape adds aabb caching to the btPolyhedralConvexShape.
The btStridingMeshInterface is the interface class for high performance generic access to triangle me...
const btVector3 & getScaling() const
void setScaling(const btVector3 &scaling)
virtual void InternalProcessAllTriangles(btInternalTriangleIndexCallback *callback, const btVector3 &aabbMin, const btVector3 &aabbMax) const
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition btTransform.h:30
btMatrix3x3 & getBasis()
Return the basis matrix for the rotation.
void setOrigin(const btVector3 &origin)
Set the translational element.
btVector3 can be used to represent 3D points and vectors.
Definition btVector3.h:82
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition btVector3.h:229
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition btVector3.h:640
btScalar triple(const btVector3 &v1, const btVector3 &v2) const
Definition btVector3.h:419
btScalar length2() const
Return the length of the vector squared.
Definition btVector3.h:251
btVector3 & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition btVector3.h:303