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"
20 #include "LinearMath/btAabbUtil2.h"
22 
24  : btConcaveShape(), m_meshInterface(meshInterface)
25 {
27  if (meshInterface->hasPremadeAabb())
28  {
29  meshInterface->getPremadeAabb(&m_localAabbMin, &m_localAabbMax);
30  }
31  else
32  {
34  }
35 }
36 
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 
57 {
58  for (int i = 0; i < 3; i++)
59  {
60  btVector3 vec(btScalar(0.), btScalar(0.), btScalar(0.));
61  vec[i] = btScalar(1.);
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 
71 {
73 
74 public:
78 
79  SupportVertexCallback(const btVector3& supportVecWorld, const btTransform& trans)
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 
102  {
104  }
105 
107  {
108  return m_supportVertexLocal;
109  }
110 };
111 
113 {
114  m_meshInterface->setScaling(scaling);
115  recalcLocalAabb();
116 }
117 
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 
156 {
157  (void)mass;
158  //moving concave objects not supported
159  btAssert(0);
160  inertia.setValue(btScalar(0.), btScalar(0.), btScalar(0.));
161 }
162 
164 {
165  btVector3 supportVertex;
166 
167  btTransform ident;
168  ident.setIdentity();
169 
170  SupportVertexCallback supportCallback(vec, ident);
171 
173 
174  processAllTriangles(&supportCallback, -aabbMax, aabbMax);
175 
176  supportVertex = supportCallback.GetSupportVertexLocal();
177 
178  return supportVertex;
179 }
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...
virtual bool hasPremadeAabb() const
#define BT_LARGE_FLOAT
Definition: btScalar.h:296
SupportVertexCallback(const btVector3 &supportVecWorld, const btTransform &trans)
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:640
btVector3 dot3(const btVector3 &v0, const btVector3 &v1, const btVector3 &v2) const
Definition: btVector3.h:720
btStridingMeshInterface * m_meshInterface
virtual void InternalProcessAllTriangles(btInternalTriangleIndexCallback *callback, const btVector3 &aabbMin, const btVector3 &aabbMax) const
virtual void processTriangle(btVector3 *triangle, int partId, int triangleIndex)=0
void setIdentity()
Set this transformation to the identity.
Definition: btTransform.h:166
#define btAssert(x)
Definition: btScalar.h:133
btScalar m_collisionMargin
void setScaling(const btVector3 &scaling)
btTriangleMeshShape(btStridingMeshInterface *meshInterface)
btTriangleMeshShape constructor has been disabled/protected, so that users will not mistakenly use th...
virtual const btVector3 & getLocalScaling() const
The btTriangleCallback provides a callback for each overlapping triangle when calling processAllTrian...
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:229
btMatrix3x3 & getBasis()
Return the basis matrix for the rotation.
Definition: btTransform.h:108
virtual void processTriangle(btVector3 *triangle, int partId, int triangleIndex)
virtual void setLocalScaling(const btVector3 &scaling)
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:80
virtual void processAllTriangles(btTriangleCallback *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:28
virtual void getPremadeAabb(btVector3 *aabbMin, btVector3 *aabbMax) const
The btStridingMeshInterface is the interface class for high performance generic access to triangle me...
btMatrix3x3 absolute() const
Return the matrix with all values non negative.
Definition: btMatrix3x3.h:1005
The btConcaveShape class provides an interface for non-moving (static) concave shapes.
const btVector3 & getScaling() const
The btMatrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with...
Definition: btMatrix3x3.h:46
btScalar dot(const btQuaternion &q1, const btQuaternion &q2)
Calculate the dot product between two quaternions.
Definition: btQuaternion.h:888
virtual btScalar getMargin() const
virtual void calculateLocalInertia(btScalar mass, btVector3 &inertia) const
bool TestTriangleAgainstAabb2(const btVector3 *vertices, const btVector3 &aabbMin, const btVector3 &aabbMax)
conservative test for overlap between triangle and aabb
Definition: btAabbUtil2.h:54
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:294