Bullet Collision Detection & Physics Library
btTriangleShape.h
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 #ifndef BT_OBB_TRIANGLE_MINKOWSKI_H
17 #define BT_OBB_TRIANGLE_MINKOWSKI_H
18 
19 #include "btConvexShape.h"
20 #include "btBoxShape.h"
21 
23 {
24 
25 
26 public:
27 
29 
30  btVector3 m_vertices1[3];
31 
32  virtual int getNumVertices() const
33  {
34  return 3;
35  }
36 
37  btVector3& getVertexPtr(int index)
38  {
39  return m_vertices1[index];
40  }
41 
42  const btVector3& getVertexPtr(int index) const
43  {
44  return m_vertices1[index];
45  }
46  virtual void getVertex(int index,btVector3& vert) const
47  {
48  vert = m_vertices1[index];
49  }
50 
51  virtual int getNumEdges() const
52  {
53  return 3;
54  }
55 
56  virtual void getEdge(int i,btVector3& pa,btVector3& pb) const
57  {
58  getVertex(i,pa);
59  getVertex((i+1)%3,pb);
60  }
61 
62 
63  virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax)const
64  {
65 // btAssert(0);
66  getAabbSlow(t,aabbMin,aabbMax);
67  }
68 
70  {
71  btVector3 dots = dir.dot3(m_vertices1[0], m_vertices1[1], m_vertices1[2]);
72  return m_vertices1[dots.maxAxis()];
73 
74  }
75 
76  virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
77  {
78  for (int i=0;i<numVectors;i++)
79  {
80  const btVector3& dir = vectors[i];
81  btVector3 dots = dir.dot3(m_vertices1[0], m_vertices1[1], m_vertices1[2]);
82  supportVerticesOut[i] = m_vertices1[dots.maxAxis()];
83  }
84 
85  }
86 
88  {
89  m_shapeType = TRIANGLE_SHAPE_PROXYTYPE;
90  }
91 
93  {
94  m_shapeType = TRIANGLE_SHAPE_PROXYTYPE;
95  m_vertices1[0] = p0;
96  m_vertices1[1] = p1;
97  m_vertices1[2] = p2;
98  }
99 
100 
101  virtual void getPlane(btVector3& planeNormal,btVector3& planeSupport,int i) const
102  {
103  getPlaneEquation(i,planeNormal,planeSupport);
104  }
105 
106  virtual int getNumPlanes() const
107  {
108  return 1;
109  }
110 
111  void calcNormal(btVector3& normal) const
112  {
113  normal = (m_vertices1[1]-m_vertices1[0]).cross(m_vertices1[2]-m_vertices1[0]);
114  normal.normalize();
115  }
116 
117  virtual void getPlaneEquation(int i, btVector3& planeNormal,btVector3& planeSupport) const
118  {
119  (void)i;
120  calcNormal(planeNormal);
121  planeSupport = m_vertices1[0];
122  }
123 
124  virtual void calculateLocalInertia(btScalar mass,btVector3& inertia) const
125  {
126  (void)mass;
127  btAssert(0);
128  inertia.setValue(btScalar(0.),btScalar(0.),btScalar(0.));
129  }
130 
131  virtual bool isInside(const btVector3& pt,btScalar tolerance) const
132  {
133  btVector3 normal;
134  calcNormal(normal);
135  //distance to plane
136  btScalar dist = pt.dot(normal);
137  btScalar planeconst = m_vertices1[0].dot(normal);
138  dist -= planeconst;
139  if (dist >= -tolerance && dist <= tolerance)
140  {
141  //inside check on edge-planes
142  int i;
143  for (i=0;i<3;i++)
144  {
145  btVector3 pa,pb;
146  getEdge(i,pa,pb);
147  btVector3 edge = pb-pa;
148  btVector3 edgeNormal = edge.cross(normal);
149  edgeNormal.normalize();
150  btScalar dist = pt.dot( edgeNormal);
151  btScalar edgeConst = pa.dot(edgeNormal);
152  dist -= edgeConst;
153  if (dist < -tolerance)
154  return false;
155  }
156 
157  return true;
158  }
159 
160  return false;
161  }
162  //debugging
163  virtual const char* getName()const
164  {
165  return "Triangle";
166  }
167 
169  {
170  return 2;
171  }
172 
173  virtual void getPreferredPenetrationDirection(int index, btVector3& penetrationVector) const
174  {
175  calcNormal(penetrationVector);
176  if (index)
177  penetrationVector *= btScalar(-1.);
178  }
179 
180 
181 };
182 
183 #endif //BT_OBB_TRIANGLE_MINKOWSKI_H
184 
btTriangleShape::getPlane
virtual void getPlane(btVector3 &planeNormal, btVector3 &planeSupport, int i) const
Definition: btTriangleShape.h:101
btPolyhedralConvexShape
The btPolyhedralConvexShape is an internal interface class for polyhedral convex shapes.
Definition: btPolyhedralConvexShape.h:25
btTriangleShape::calcNormal
void calcNormal(btVector3 &normal) const
Definition: btTriangleShape.h:111
btTriangleShape::getName
virtual const char * getName() const
Definition: btTriangleShape.h:163
btTriangleShape::getVertexPtr
const btVector3 & getVertexPtr(int index) const
Definition: btTriangleShape.h:42
btVector3::dot3
btVector3 dot3(const btVector3 &v0, const btVector3 &v1, const btVector3 &v2) const
Definition: btVector3.h:731
btVector3::setValue
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:652
btTriangleShape::getAabb
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...
Definition: btTriangleShape.h:63
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292
btVector3::cross
btVector3 cross(const btVector3 &v) const
Return the cross product between this and another vector.
Definition: btVector3.h:389
btVector3::maxAxis
int maxAxis() const
Return the axis with the largest value Note return values are 0,1,2 for x, y, or z.
Definition: btVector3.h:487
btConvexShape.h
btVector3::dot
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:235
btTriangleShape::isInside
virtual bool isInside(const btVector3 &pt, btScalar tolerance) const
Definition: btTriangleShape.h:131
btTriangleShape
Definition: btTriangleShape.h:22
btTriangleShape::btTriangleShape
btTriangleShape()
Definition: btTriangleShape.h:87
btTriangleShape::getEdge
virtual void getEdge(int i, btVector3 &pa, btVector3 &pb) const
Definition: btTriangleShape.h:56
btAssert
#define btAssert(x)
Definition: btScalar.h:131
TRIANGLE_SHAPE_PROXYTYPE
@ TRIANGLE_SHAPE_PROXYTYPE
Definition: btBroadphaseProxy.h:32
btTriangleShape::getNumPreferredPenetrationDirections
virtual int getNumPreferredPenetrationDirections() const
Definition: btTriangleShape.h:168
btTriangleShape::getPreferredPenetrationDirection
virtual void getPreferredPenetrationDirection(int index, btVector3 &penetrationVector) const
Definition: btTriangleShape.h:173
btTriangleShape::getPlaneEquation
virtual void getPlaneEquation(int i, btVector3 &planeNormal, btVector3 &planeSupport) const
Definition: btTriangleShape.h:117
btTriangleShape::batchedUnitVectorGetSupportingVertexWithoutMargin
virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3 *vectors, btVector3 *supportVerticesOut, int numVectors) const
Definition: btTriangleShape.h:76
btTriangleShape::calculateLocalInertia
virtual void calculateLocalInertia(btScalar mass, btVector3 &inertia) const
Definition: btTriangleShape.h:124
btTriangleShape::localGetSupportingVertexWithoutMargin
btVector3 localGetSupportingVertexWithoutMargin(const btVector3 &dir) const
Definition: btTriangleShape.h:69
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
BT_DECLARE_ALIGNED_ALLOCATOR
#define BT_DECLARE_ALIGNED_ALLOCATOR()
Definition: btScalar.h:403
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
btTriangleShape::getVertex
virtual void getVertex(int index, btVector3 &vert) const
Definition: btTriangleShape.h:46
btBoxShape.h
ATTRIBUTE_ALIGNED16
#define ATTRIBUTE_ALIGNED16(a)
Definition: btScalar.h:82
btTriangleShape::getNumPlanes
virtual int getNumPlanes() const
Definition: btTriangleShape.h:106
btTriangleShape::getNumEdges
virtual int getNumEdges() const
Definition: btTriangleShape.h:51
btTriangleShape::btTriangleShape
btTriangleShape(const btVector3 &p0, const btVector3 &p1, const btVector3 &p2)
Definition: btTriangleShape.h:92
btTriangleShape::getNumVertices
virtual int getNumVertices() const
Definition: btTriangleShape.h:32
btVector3::normalize
btVector3 & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition: btVector3.h:309
btTriangleShape::getVertexPtr
btVector3 & getVertexPtr(int index)
Definition: btTriangleShape.h:37