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 
23 
25 : btConcaveShape (), m_meshInterface(meshInterface)
26 {
28  if(meshInterface->hasPremadeAabb())
29  {
30  meshInterface->getPremadeAabb(&m_localAabbMin, &m_localAabbMax);
31  }
32  else
33  {
35  }
36 }
37 
38 
40 {
41 
42 }
43 
44 
45 
46 
47 void btTriangleMeshShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const
48 {
49 
50  btVector3 localHalfExtents = btScalar(0.5)*(m_localAabbMax-m_localAabbMin);
51  localHalfExtents += btVector3(getMargin(),getMargin(),getMargin());
52  btVector3 localCenter = btScalar(0.5)*(m_localAabbMax+m_localAabbMin);
53 
54  btMatrix3x3 abs_b = trans.getBasis().absolute();
55 
56  btVector3 center = trans(localCenter);
57 
58  btVector3 extent = localHalfExtents.dot3(abs_b[0], abs_b[1], abs_b[2]);
59  aabbMin = center - extent;
60  aabbMax = center + extent;
61 }
62 
64 {
65  for (int i=0;i<3;i++)
66  {
67  btVector3 vec(btScalar(0.),btScalar(0.),btScalar(0.));
68  vec[i] = btScalar(1.);
71  vec[i] = btScalar(-1.);
72  tmp = localGetSupportingVertex(vec);
74  }
75 }
76 
77 
78 
80 {
81 
83 public:
84 
88 
89  SupportVertexCallback(const btVector3& supportVecWorld,const btTransform& trans)
91 
92  {
93  m_supportVecLocal = supportVecWorld * m_worldTrans.getBasis();
94  }
95 
96  virtual void processTriangle( btVector3* triangle,int partId, int triangleIndex)
97  {
98  (void)partId;
99  (void)triangleIndex;
100  for (int i=0;i<3;i++)
101  {
102  btScalar dot = m_supportVecLocal.dot(triangle[i]);
103  if (dot > m_maxDot)
104  {
105  m_maxDot = dot;
106  m_supportVertexLocal = triangle[i];
107  }
108  }
109  }
110 
112  {
114  }
115 
117  {
118  return m_supportVertexLocal;
119  }
120 
121 };
122 
123 
125 {
126  m_meshInterface->setScaling(scaling);
127  recalcLocalAabb();
128 }
129 
131 {
132  return m_meshInterface->getScaling();
133 }
134 
135 
136 
137 
138 
139 
140 //#define DEBUG_TRIANGLE_MESH
141 
142 
143 
144 void btTriangleMeshShape::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
145 {
146  struct FilteredCallback : public btInternalTriangleIndexCallback
147  {
148  btTriangleCallback* m_callback;
149  btVector3 m_aabbMin;
150  btVector3 m_aabbMax;
151 
152  FilteredCallback(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax)
153  :m_callback(callback),
154  m_aabbMin(aabbMin),
155  m_aabbMax(aabbMax)
156  {
157  }
158 
159  virtual void internalProcessTriangleIndex(btVector3* triangle,int partId,int triangleIndex)
160  {
161  if (TestTriangleAgainstAabb2(&triangle[0],m_aabbMin,m_aabbMax))
162  {
163  //check aabb in triangle-space, before doing this
164  m_callback->processTriangle(triangle,partId,triangleIndex);
165  }
166 
167  }
168 
169  };
170 
171  FilteredCallback filterCallback(callback,aabbMin,aabbMax);
172 
173  m_meshInterface->InternalProcessAllTriangles(&filterCallback,aabbMin,aabbMax);
174 }
175 
176 
177 
178 
179 
181 {
182  (void)mass;
183  //moving concave objects not supported
184  btAssert(0);
185  inertia.setValue(btScalar(0.),btScalar(0.),btScalar(0.));
186 }
187 
188 
190 {
191  btVector3 supportVertex;
192 
193  btTransform ident;
194  ident.setIdentity();
195 
196  SupportVertexCallback supportCallback(vec,ident);
197 
199 
200  processAllTriangles(&supportCallback,-aabbMax,aabbMax);
201 
202  supportVertex = supportCallback.GetSupportVertexLocal();
203 
204  return supportVertex;
205 }
206 
207 
#define BT_LARGE_FLOAT
Definition: btScalar.h:268
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...
SupportVertexCallback(const btVector3 &supportVecWorld, const btTransform &trans)
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:640
btStridingMeshInterface * m_meshInterface
void setIdentity()
Set this transformation to the identity.
Definition: btTransform.h:172
#define btAssert(x)
Definition: btScalar.h:101
btScalar m_collisionMargin
virtual void calculateLocalInertia(btScalar mass, btVector3 &inertia) const
const btVector3 & getScaling() const
virtual void getPremadeAabb(btVector3 *aabbMin, btVector3 *aabbMax) const
void setScaling(const btVector3 &scaling)
btTriangleMeshShape(btStridingMeshInterface *meshInterface)
btTriangleMeshShape constructor has been disabled/protected, so that users will not mistakenly use th...
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:235
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const
btMatrix3x3 absolute() const
Return the matrix with all values non negative.
Definition: btMatrix3x3.h:959
The btTriangleCallback provides a callback for each overlapping triangle when calling processAllTrian...
virtual bool hasPremadeAabb() const
btMatrix3x3 & getBasis()
Return the basis matrix for the rotation.
Definition: btTransform.h:112
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:83
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:34
The btStridingMeshInterface is the interface class for high performance generic access to triangle me...
The btConcaveShape class provides an interface for non-moving (static) concave shapes.
btVector3 dot3(const btVector3 &v0, const btVector3 &v1, const btVector3 &v2) const
Definition: btVector3.h:718
The btMatrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with...
Definition: btMatrix3x3.h:48
btScalar dot(const btQuaternion &q1, const btQuaternion &q2)
Calculate the dot product between two quaternions.
Definition: btQuaternion.h:827
virtual btScalar getMargin() const
virtual const btVector3 & getLocalScaling() const
bool TestTriangleAgainstAabb2(const btVector3 *vertices, const btVector3 &aabbMin, const btVector3 &aabbMax)
conservative test for overlap between triangle and aabb
Definition: btAabbUtil2.h:59
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:266
virtual void InternalProcessAllTriangles(btInternalTriangleIndexCallback *callback, const btVector3 &aabbMin, const btVector3 &aabbMax) const