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

Bullet Collision Detection & Physics Library

btTriangleMeshShape.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 "btTriangleMeshShape.h"
17 #include "LinearMath/btVector3.h"
18 #include "LinearMath/btQuaternion.h"
19 #include "btStridingMeshInterface.h"
20 #include "LinearMath/btAabbUtil2.h"
21 #include "BulletCollision/CollisionShapes/btCollisionMargin.h"
22
23 btTriangleMeshShape::btTriangleMeshShape(btStridingMeshInterface* meshInterface)
24 : btConcaveShape(), m_meshInterface(meshInterface)
25 {
26 m_shapeType = TRIANGLE_MESH_SHAPE_PROXYTYPE;
27 if (meshInterface->hasPremadeAabb())
28 {
29 meshInterface->getPremadeAabb(&m_localAabbMin, &m_localAabbMax);
30 }
31 else
32 {
33 recalcLocalAabb();
34 }
35 }
36
37 btTriangleMeshShape::~btTriangleMeshShape()
38 {
39 }
40
41 void btTriangleMeshShape::getAabb(const btTransform& trans, btVector3& aabbMin, btVector3& aabbMax) const
42 {
43 btVector3 localHalfExtents = btScalar(0.5) * (m_localAabbMax - m_localAabbMin);
44 localHalfExtents += btVector3(getMargin(), getMargin(), getMargin());
45 btVector3 localCenter = btScalar(0.5) * (m_localAabbMax + m_localAabbMin);
46
47 btMatrix3x3 abs_b = trans.getBasis().absolute();
48
49 btVector3 center = trans(localCenter);
50
51 btVector3 extent = localHalfExtents.dot3(abs_b[0], abs_b[1], abs_b[2]);
52 aabbMin = center - extent;
53 aabbMax = center + extent;
54 }
55
56 void btTriangleMeshShape::recalcLocalAabb()
57 {
58 for (int i = 0; i < 3; i++)
59 {
60 btVector3 vec(btScalar(0.), btScalar(0.), btScalar(0.));
61 vec[i] = btScalar(1.);
62 btVector3 tmp = localGetSupportingVertex(vec);
63 m_localAabbMax[i] = tmp[i] + m_collisionMargin;
64 vec[i] = btScalar(-1.);
65 tmp = localGetSupportingVertex(vec);
66 m_localAabbMin[i] = tmp[i] - m_collisionMargin;
67 }
68 }
69
70 class SupportVertexCallback : public btTriangleCallback
71 {
72 btVector3 m_supportVertexLocal;
73
74 public:
75 btTransform m_worldTrans;
76 btScalar m_maxDot;
77 btVector3 m_supportVecLocal;
78
79 SupportVertexCallback(const btVector3& supportVecWorld, const btTransform& trans)
80 : m_supportVertexLocal(btScalar(0.), btScalar(0.), btScalar(0.)), m_worldTrans(trans), m_maxDot(btScalar(-BT_LARGE_FLOAT))
81
82 {
83 m_supportVecLocal = supportVecWorld * m_worldTrans.getBasis();
84 }
85
86 virtual void processTriangle(btVector3* triangle, int partId, int triangleIndex)
87 {
88 (void)partId;
89 (void)triangleIndex;
90 for (int i = 0; i < 3; i++)
91 {
92 btScalar dot = m_supportVecLocal.dot(triangle[i]);
93 if (dot > m_maxDot)
94 {
95 m_maxDot = dot;
96 m_supportVertexLocal = triangle[i];
97 }
98 }
99 }
100
101 btVector3 GetSupportVertexWorldSpace()
102 {
103 return m_worldTrans(m_supportVertexLocal);
104 }
105
106 btVector3 GetSupportVertexLocal()
107 {
108 return m_supportVertexLocal;
109 }
110 };
111
112 void btTriangleMeshShape::setLocalScaling(const btVector3& scaling)
113 {
114 m_meshInterface->setScaling(scaling);
115 recalcLocalAabb();
116 }
117
118 const btVector3& btTriangleMeshShape::getLocalScaling() const
119 {
120 return m_meshInterface->getScaling();
121 }
122
123 //#define DEBUG_TRIANGLE_MESH
124
125 void btTriangleMeshShape::processAllTriangles(btTriangleCallback* callback, const btVector3& aabbMin, const btVector3& aabbMax) const
126 {
127 struct FilteredCallback : public btInternalTriangleIndexCallback
128 {
129 btTriangleCallback* m_callback;
130 btVector3 m_aabbMin;
131 btVector3 m_aabbMax;
132
133 FilteredCallback(btTriangleCallback* callback, const btVector3& aabbMin, const btVector3& aabbMax)
134 : m_callback(callback),
135 m_aabbMin(aabbMin),
136 m_aabbMax(aabbMax)
137 {
138 }
139
140 virtual void internalProcessTriangleIndex(btVector3* triangle, int partId, int triangleIndex)
141 {
142 if (TestTriangleAgainstAabb2(&triangle[0], m_aabbMin, m_aabbMax))
143 {
144 //check aabb in triangle-space, before doing this
145 m_callback->processTriangle(triangle, partId, triangleIndex);
146 }
147 }
148 };
149
150 FilteredCallback filterCallback(callback, aabbMin, aabbMax);
151
152 m_meshInterface->InternalProcessAllTriangles(&filterCallback, aabbMin, aabbMax);
153 }
154
155 void btTriangleMeshShape::calculateLocalInertia(btScalar mass, btVector3& inertia) const
156 {
157 (void)mass;
158 //moving concave objects not supported
159 btAssert(0);
160 inertia.setValue(btScalar(0.), btScalar(0.), btScalar(0.));
161 }
162
163 btVector3 btTriangleMeshShape::localGetSupportingVertex(const btVector3& vec) const
164 {
165 btVector3 supportVertex;
166
167 btTransform ident;
168 ident.setIdentity();
169
170 SupportVertexCallback supportCallback(vec, ident);
171
172 btVector3 aabbMax(btScalar(BT_LARGE_FLOAT), btScalar(BT_LARGE_FLOAT), btScalar(BT_LARGE_FLOAT));
173
174 processAllTriangles(&supportCallback, -aabbMax, aabbMax);
175
176 supportVertex = supportCallback.GetSupportVertexLocal();
177
178 return supportVertex;
179 }
btTriangleMeshShape::btTriangleMeshShape
btTriangleMeshShape(btStridingMeshInterface *meshInterface)
btTriangleMeshShape constructor has been disabled/protected, so that users will not mistakenly use th...
Definition: btTriangleMeshShape.cpp:23
btTriangleMeshShape::setLocalScaling
virtual void setLocalScaling(const btVector3 &scaling)
Definition: btTriangleMeshShape.cpp:112
dot
btScalar dot(const btQuaternion &q1, const btQuaternion &q2)
Calculate the dot product between two quaternions.
Definition: btQuaternion.h:888
btInternalTriangleIndexCallback
Definition: btTriangleCallback.h:31
btConcaveShape::m_collisionMargin
btScalar m_collisionMargin
Definition: btConcaveShape.h:41
btVector3::dot3
btVector3 dot3(const btVector3 &v0, const btVector3 &v1, const btVector3 &v2) const
Definition: btVector3.h:720
btVector3::setValue
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:640
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
btTriangleMeshShape::getLocalScaling
virtual const btVector3 & getLocalScaling() const
Definition: btTriangleMeshShape.cpp:118
btConcaveShape
The btConcaveShape class provides an interface for non-moving (static) concave shapes.
Definition: btConcaveShape.h:39
btStridingMeshInterface.h
btStridingMeshInterface::setScaling
void setScaling(const btVector3 &scaling)
Definition: btStridingMeshInterface.h:84
btVector3::dot
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:229
btStridingMeshInterface::InternalProcessAllTriangles
virtual void InternalProcessAllTriangles(btInternalTriangleIndexCallback *callback, const btVector3 &aabbMin, const btVector3 &aabbMax) const
Definition: btStridingMeshInterface.cpp:23
TestTriangleAgainstAabb2
bool TestTriangleAgainstAabb2(const btVector3 *vertices, const btVector3 &aabbMin, const btVector3 &aabbMax)
conservative test for overlap between triangle and aabb
Definition: btAabbUtil2.h:54
btCollisionMargin.h
btTriangleMeshShape::getAabb
virtual void getAabb(const btTransform &t, btVector3 &aabbMin, btVector3 &aabbMax) const
getAabb returns the axis aligned bounding box in the coordinate frame of the given transform t.
Definition: btTriangleMeshShape.cpp:41
btTransform::setIdentity
void setIdentity()
Set this transformation to the identity.
Definition: btTransform.h:166
btStridingMeshInterface
The btStridingMeshInterface is the interface class for high performance generic access to triangle me...
Definition: btStridingMeshInterface.h:28
SupportVertexCallback
Definition: btTriangleMeshShape.cpp:71
btCollisionShape::m_shapeType
int m_shapeType
Definition: btCollisionShape.h:30
btVector3.h
btAssert
#define btAssert(x)
Definition: btScalar.h:153
BT_LARGE_FLOAT
#define BT_LARGE_FLOAT
Definition: btScalar.h:316
SupportVertexCallback::m_supportVecLocal
btVector3 m_supportVecLocal
Definition: btTriangleMeshShape.cpp:77
btTransform::getBasis
btMatrix3x3 & getBasis()
Return the basis matrix for the rotation.
Definition: btTransform.h:108
btMatrix3x3
The btMatrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with...
Definition: btMatrix3x3.h:48
btTriangleMeshShape::processAllTriangles
virtual void processAllTriangles(btTriangleCallback *callback, const btVector3 &aabbMin, const btVector3 &aabbMax) const
Definition: btTriangleMeshShape.cpp:125
btTriangleCallback
The btTriangleCallback provides a callback for each overlapping triangle when calling processAllTrian...
Definition: btTriangleCallback.h:24
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:30
btQuaternion.h
btTriangleMeshShape::calculateLocalInertia
virtual void calculateLocalInertia(btScalar mass, btVector3 &inertia) const
Definition: btTriangleMeshShape.cpp:155
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:82
SupportVertexCallback::GetSupportVertexLocal
btVector3 GetSupportVertexLocal()
Definition: btTriangleMeshShape.cpp:106
SupportVertexCallback::processTriangle
virtual void processTriangle(btVector3 *triangle, int partId, int triangleIndex)
Definition: btTriangleMeshShape.cpp:86
btAabbUtil2.h
btTriangleCallback::processTriangle
virtual void processTriangle(btVector3 *triangle, int partId, int triangleIndex)=0
btStridingMeshInterface::getScaling
const btVector3 & getScaling() const
Definition: btStridingMeshInterface.h:80
SupportVertexCallback::m_worldTrans
btTransform m_worldTrans
Definition: btTriangleMeshShape.cpp:75
btTriangleMeshShape::m_meshInterface
btStridingMeshInterface * m_meshInterface
Definition: btTriangleMeshShape.h:29
SupportVertexCallback::m_maxDot
btScalar m_maxDot
Definition: btTriangleMeshShape.cpp:76
SupportVertexCallback::m_supportVertexLocal
btVector3 m_supportVertexLocal
Definition: btTriangleMeshShape.cpp:72
TRIANGLE_MESH_SHAPE_PROXYTYPE
@ TRIANGLE_MESH_SHAPE_PROXYTYPE
Definition: btBroadphaseProxy.h:54
SupportVertexCallback::SupportVertexCallback
SupportVertexCallback(const btVector3 &supportVecWorld, const btTransform &trans)
Definition: btTriangleMeshShape.cpp:79
btTriangleMeshShape::localGetSupportingVertex
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const
Definition: btTriangleMeshShape.cpp:163
btTriangleMeshShape::m_localAabbMax
btVector3 m_localAabbMax
Definition: btTriangleMeshShape.h:28
btTriangleMeshShape::recalcLocalAabb
void recalcLocalAabb()
Definition: btTriangleMeshShape.cpp:56
btStridingMeshInterface::getPremadeAabb
virtual void getPremadeAabb(btVector3 *aabbMin, btVector3 *aabbMax) const
Definition: btStridingMeshInterface.h:74
btMatrix3x3::absolute
btMatrix3x3 absolute() const
Return the matrix with all values non negative.
Definition: btMatrix3x3.h:1012
btConcaveShape::getMargin
virtual btScalar getMargin() const
Definition: btConcaveShape.h:52
SupportVertexCallback::GetSupportVertexWorldSpace
btVector3 GetSupportVertexWorldSpace()
Definition: btTriangleMeshShape.cpp:101
btTriangleMeshShape.h
btTriangleMeshShape::~btTriangleMeshShape
virtual ~btTriangleMeshShape()
Definition: btTriangleMeshShape.cpp:37
btStridingMeshInterface::hasPremadeAabb
virtual bool hasPremadeAabb() const
Definition: btStridingMeshInterface.h:68
btTriangleMeshShape::m_localAabbMin
btVector3 m_localAabbMin
Definition: btTriangleMeshShape.h:27