Blender  V3.3
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 {
26  m_shapeType = TRIANGLE_MESH_SHAPE_PROXYTYPE;
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 {
72  btVector3 m_supportVertexLocal;
73 
74 public:
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 
102  {
103  return m_worldTrans(m_supportVertexLocal);
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 
126 {
127  struct FilteredCallback : public btInternalTriangleIndexCallback
128  {
129  btTriangleCallback* m_callback;
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 }
NSNotificationCenter * center
SIMD_FORCE_INLINE bool TestTriangleAgainstAabb2(const btVector3 *vertices, const btVector3 &aabbMin, const btVector3 &aabbMax)
conservative test for overlap between triangle and aabb
Definition: btAabbUtil2.h:54
virtual void setLocalScaling(const btVector3 &scaling)
in case we receive negative scaling
Definition: btBox2dShape.h:120
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const
Definition: btBox2dShape.h:51
virtual void calculateLocalInertia(btScalar mass, btVector3 &inertia) const
Definition: btConeShape.h:54
virtual void getAabb(const btTransform &t, btVector3 &aabbMin, btVector3 &aabbMax) const
getAabb's default implementation is brute force, expected derived classes to implement a fast dedicat...
btVector3 m_aabbMax
@ TRIANGLE_MESH_SHAPE_PROXYTYPE
btVector3 m_aabbMin
virtual void processAllTriangles(btTriangleCallback *callback, const btVector3 &aabbMin, const btVector3 &aabbMax) const
Definition: btEmptyShape.h:57
virtual const btVector3 & getLocalScaling() const =0
virtual btScalar getMargin() const =0
btVector3 m_localAabbMin
btScalar m_collisionMargin
btVector3 m_localAabbMax
btConcaveShape()
btMatrix3x3
The btMatrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with...
Definition: btMatrix3x3.h:50
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
#define btAssert(x)
Definition: btScalar.h:295
btStridingMeshInterface
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:30
btStridingMeshInterface * m_meshInterface
virtual ~btTriangleMeshShape()
void recalcLocalAabb()
btTriangleMeshShape(btStridingMeshInterface *meshInterface)
btVector3
btVector3 can be used to represent 3D points and vectors. It has an un-used w component to suit 16-by...
Definition: btVector3.h:82
SupportVertexCallback(const btVector3 &supportVecWorld, const btTransform &trans)
virtual void processTriangle(btVector3 *triangle, int partId, int triangleIndex)
virtual void internalProcessTriangleIndex(btVector3 *triangle, int partId, int triangleIndex)=0
virtual void processTriangle(btVector3 *triangle, int partId, int triangleIndex)=0
DEGForeachIDComponentCallback callback
SyclQueue void void size_t num_bytes void
T dot(const vec_base< T, Size > &a, const vec_base< T, Size > &b)