== SoC Bullet - Bullet Upgrade to 2.76 ==
[blender.git] / extern / bullet2 / BulletCollision / CollisionShapes / btCylinderShape.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 CYLINDER_MINKOWSKI_H
17 #define CYLINDER_MINKOWSKI_H
18
19 #include "btBoxShape.h"
20 #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h" // for the types
21 #include "LinearMath/btVector3.h"
22
23 /// The btCylinderShape class implements a cylinder shape primitive, centered around the origin. Its central axis aligned with the Y axis. btCylinderShapeX is aligned with the X axis and btCylinderShapeZ around the Z axis.
24 class btCylinderShape : public btConvexInternalShape
25
26 {
27
28 protected:
29
30         int     m_upAxis;
31
32 public:
33
34         btVector3 getHalfExtentsWithMargin() const
35         {
36                 btVector3 halfExtents = getHalfExtentsWithoutMargin();
37                 btVector3 margin(getMargin(),getMargin(),getMargin());
38                 halfExtents += margin;
39                 return halfExtents;
40         }
41         
42         const btVector3& getHalfExtentsWithoutMargin() const
43         {
44                 return m_implicitShapeDimensions;//changed in Bullet 2.63: assume the scaling and margin are included
45         }
46
47         btCylinderShape (const btVector3& halfExtents);
48         
49         void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const;
50
51         virtual void    calculateLocalInertia(btScalar mass,btVector3& inertia) const;
52
53         virtual btVector3       localGetSupportingVertexWithoutMargin(const btVector3& vec)const;
54
55         virtual void    batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const;
56
57         virtual void setMargin(btScalar collisionMargin)
58         {
59                 //correct the m_implicitShapeDimensions for the margin
60                 btVector3 oldMargin(getMargin(),getMargin(),getMargin());
61                 btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions+oldMargin;
62                 
63                 btConvexInternalShape::setMargin(collisionMargin);
64                 btVector3 newMargin(getMargin(),getMargin(),getMargin());
65                 m_implicitShapeDimensions = implicitShapeDimensionsWithMargin - newMargin;
66
67         }
68
69         virtual btVector3       localGetSupportingVertex(const btVector3& vec) const
70         {
71
72                 btVector3 supVertex;
73                 supVertex = localGetSupportingVertexWithoutMargin(vec);
74                 
75                 if ( getMargin()!=btScalar(0.) )
76                 {
77                         btVector3 vecnorm = vec;
78                         if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
79                         {
80                                 vecnorm.setValue(btScalar(-1.),btScalar(-1.),btScalar(-1.));
81                         } 
82                         vecnorm.normalize();
83                         supVertex+= getMargin() * vecnorm;
84                 }
85                 return supVertex;
86         }
87
88
89         //use box inertia
90         //      virtual void    calculateLocalInertia(btScalar mass,btVector3& inertia) const;
91
92
93         int     getUpAxis() const
94         {
95                 return m_upAxis;
96         }
97
98         virtual btScalar getRadius() const
99         {
100                 return getHalfExtentsWithMargin().getX();
101         }
102
103         //debugging
104         virtual const char*     getName()const
105         {
106                 return "CylinderY";
107         }
108
109         virtual int     calculateSerializeBufferSize() const;
110
111         ///fills the dataBuffer and returns the struct name (and 0 on failure)
112         virtual const char*     serialize(void* dataBuffer, btSerializer* serializer) const;
113
114 };
115
116 class btCylinderShapeX : public btCylinderShape
117 {
118 public:
119         btCylinderShapeX (const btVector3& halfExtents);
120
121         virtual btVector3       localGetSupportingVertexWithoutMargin(const btVector3& vec)const;
122         virtual void    batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const;
123         
124                 //debugging
125         virtual const char*     getName()const
126         {
127                 return "CylinderX";
128         }
129
130         virtual btScalar getRadius() const
131         {
132                 return getHalfExtentsWithMargin().getY();
133         }
134
135 };
136
137 class btCylinderShapeZ : public btCylinderShape
138 {
139 public:
140         btCylinderShapeZ (const btVector3& halfExtents);
141
142         virtual btVector3       localGetSupportingVertexWithoutMargin(const btVector3& vec)const;
143         virtual void    batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const;
144
145         virtual int     getUpAxis() const
146         {
147                 return 2;
148         }
149                 //debugging
150         virtual const char*     getName()const
151         {
152                 return "CylinderZ";
153         }
154
155         virtual btScalar getRadius() const
156         {
157                 return getHalfExtentsWithMargin().getX();
158         }
159
160 };
161
162 ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
163 struct  btCylinderShapeData
164 {
165         btConvexInternalShapeData       m_convexInternalShapeData;
166
167         int     m_upAxis;
168
169         char    m_padding[4];
170 };
171
172 SIMD_FORCE_INLINE       int     btCylinderShape::calculateSerializeBufferSize() const
173 {
174         return sizeof(btCylinderShapeData);
175 }
176
177         ///fills the dataBuffer and returns the struct name (and 0 on failure)
178 SIMD_FORCE_INLINE       const char*     btCylinderShape::serialize(void* dataBuffer, btSerializer* serializer) const
179 {
180         btCylinderShapeData* shapeData = (btCylinderShapeData*) dataBuffer;
181         
182         btConvexInternalShape::serialize(&shapeData->m_convexInternalShapeData,serializer);
183
184         shapeData->m_upAxis = m_upAxis;
185         
186         return "btCylinderShapeData";
187 }
188
189
190
191 #endif //CYLINDER_MINKOWSKI_H
192