== SoC Bullet - Bullet Upgrade to 2.76 ==
[blender.git] / extern / bullet2 / BulletCollision / CollisionShapes / btTriangleShape.h
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 OBB_TRIANGLE_MINKOWSKI_H
17 #define OBB_TRIANGLE_MINKOWSKI_H
18
19 #include "btConvexShape.h"
20 #include "btBoxShape.h"
21
22 ATTRIBUTE_ALIGNED16(class) btTriangleShape : public btPolyhedralConvexShape
23 {
24
25
26 public:
27
28         btVector3       m_vertices1[3];
29
30         virtual int getNumVertices() const
31         {
32                 return 3;
33         }
34
35         btVector3& getVertexPtr(int index)
36         {
37                 return m_vertices1[index];
38         }
39
40         const btVector3& getVertexPtr(int index) const
41         {
42                 return m_vertices1[index];
43         }
44         virtual void getVertex(int index,btVector3& vert) const
45         {
46                 vert = m_vertices1[index];
47         }
48
49         virtual int getNumEdges() const
50         {
51                 return 3;
52         }
53         
54         virtual void getEdge(int i,btVector3& pa,btVector3& pb) const
55         {
56                 getVertex(i,pa);
57                 getVertex((i+1)%3,pb);
58         }
59
60
61         virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax)const 
62         {
63 //              btAssert(0);
64                 getAabbSlow(t,aabbMin,aabbMax);
65         }
66
67         btVector3 localGetSupportingVertexWithoutMargin(const btVector3& dir)const 
68         {
69                 btVector3 dots(dir.dot(m_vertices1[0]), dir.dot(m_vertices1[1]), dir.dot(m_vertices1[2]));
70                 return m_vertices1[dots.maxAxis()];
71
72         }
73
74         virtual void    batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
75         {
76                 for (int i=0;i<numVectors;i++)
77                 {
78                         const btVector3& dir = vectors[i];
79                         btVector3 dots(dir.dot(m_vertices1[0]), dir.dot(m_vertices1[1]), dir.dot(m_vertices1[2]));
80                         supportVerticesOut[i] = m_vertices1[dots.maxAxis()];
81                 }
82
83         }
84
85         btTriangleShape() : btPolyhedralConvexShape ()
86     {
87                 m_shapeType = TRIANGLE_SHAPE_PROXYTYPE;
88         }
89
90         btTriangleShape(const btVector3& p0,const btVector3& p1,const btVector3& p2) : btPolyhedralConvexShape ()
91     {
92                 m_shapeType = TRIANGLE_SHAPE_PROXYTYPE;
93         m_vertices1[0] = p0;
94         m_vertices1[1] = p1;
95         m_vertices1[2] = p2;
96     }
97
98
99         virtual void getPlane(btVector3& planeNormal,btVector3& planeSupport,int i) const
100         {
101                 getPlaneEquation(i,planeNormal,planeSupport);
102         }
103
104         virtual int     getNumPlanes() const
105         {
106                 return 1;
107         }
108
109         void calcNormal(btVector3& normal) const
110         {
111                 normal = (m_vertices1[1]-m_vertices1[0]).cross(m_vertices1[2]-m_vertices1[0]);
112                 normal.normalize();
113         }
114
115         virtual void getPlaneEquation(int i, btVector3& planeNormal,btVector3& planeSupport) const
116         {
117                 (void)i;
118                 calcNormal(planeNormal);
119                 planeSupport = m_vertices1[0];
120         }
121
122         virtual void    calculateLocalInertia(btScalar mass,btVector3& inertia) const
123         {
124                 (void)mass;
125                 btAssert(0);
126                 inertia.setValue(btScalar(0.),btScalar(0.),btScalar(0.));
127         }
128
129                 virtual bool isInside(const btVector3& pt,btScalar tolerance) const
130         {
131                 btVector3 normal;
132                 calcNormal(normal);
133                 //distance to plane
134                 btScalar dist = pt.dot(normal);
135                 btScalar planeconst = m_vertices1[0].dot(normal);
136                 dist -= planeconst;
137                 if (dist >= -tolerance && dist <= tolerance)
138                 {
139                         //inside check on edge-planes
140                         int i;
141                         for (i=0;i<3;i++)
142                         {
143                                 btVector3 pa,pb;
144                                 getEdge(i,pa,pb);
145                                 btVector3 edge = pb-pa;
146                                 btVector3 edgeNormal = edge.cross(normal);
147                                 edgeNormal.normalize();
148                                 btScalar dist = pt.dot( edgeNormal);
149                                 btScalar edgeConst = pa.dot(edgeNormal);
150                                 dist -= edgeConst;
151                                 if (dist < -tolerance)
152                                         return false;
153                         }
154                         
155                         return true;
156                 }
157
158                 return false;
159         }
160                 //debugging
161                 virtual const char*     getName()const
162                 {
163                         return "Triangle";
164                 }
165
166                 virtual int             getNumPreferredPenetrationDirections() const
167                 {
168                         return 2;
169                 }
170                 
171                 virtual void    getPreferredPenetrationDirection(int index, btVector3& penetrationVector) const
172                 {
173                         calcNormal(penetrationVector);
174                         if (index)
175                                 penetrationVector *= btScalar(-1.);
176                 }
177
178
179 };
180
181 #endif //OBB_TRIANGLE_MINKOWSKI_H
182