[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / BulletCollision / CollisionShapes / btCompoundShape.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 BT_COMPOUND_SHAPE_H
17 #define BT_COMPOUND_SHAPE_H
18
19 #include "btCollisionShape.h"
20
21 #include "LinearMath/btVector3.h"
22 #include "LinearMath/btTransform.h"
23 #include "LinearMath/btMatrix3x3.h"
24 #include "btCollisionMargin.h"
25 #include "LinearMath/btAlignedObjectArray.h"
26
27 //class btOptimizedBvh;
28 struct btDbvt;
29
30 ATTRIBUTE_ALIGNED16(struct)
31 btCompoundShapeChild
32 {
33         BT_DECLARE_ALIGNED_ALLOCATOR();
34
35         btTransform m_transform;
36         btCollisionShape* m_childShape;
37         int m_childShapeType;
38         btScalar m_childMargin;
39         struct btDbvtNode* m_node;
40 };
41
42 SIMD_FORCE_INLINE bool operator==(const btCompoundShapeChild& c1, const btCompoundShapeChild& c2)
43 {
44         return (c1.m_transform == c2.m_transform &&
45                         c1.m_childShape == c2.m_childShape &&
46                         c1.m_childShapeType == c2.m_childShapeType &&
47                         c1.m_childMargin == c2.m_childMargin);
48 }
49
50 /// The btCompoundShape allows to store multiple other btCollisionShapes
51 /// This allows for moving concave collision objects. This is more general then the static concave btBvhTriangleMeshShape.
52 /// It has an (optional) dynamic aabb tree to accelerate early rejection tests.
53 /// @todo: This aabb tree can also be use to speed up ray tests on btCompoundShape, see http://code.google.com/p/bullet/issues/detail?id=25
54 /// Currently, removal of child shapes is only supported when disabling the aabb tree (pass 'false' in the constructor of btCompoundShape)
55 ATTRIBUTE_ALIGNED16(class)
56 btCompoundShape : public btCollisionShape
57 {
58 protected:
59         btAlignedObjectArray<btCompoundShapeChild> m_children;
60         btVector3 m_localAabbMin;
61         btVector3 m_localAabbMax;
62
63         btDbvt* m_dynamicAabbTree;
64
65         ///increment m_updateRevision when adding/removing/replacing child shapes, so that some caches can be updated
66         int m_updateRevision;
67
68         btScalar m_collisionMargin;
69
70         btVector3 m_localScaling;
71
72 public:
73         BT_DECLARE_ALIGNED_ALLOCATOR();
74
75         explicit btCompoundShape(bool enableDynamicAabbTree = true, const int initialChildCapacity = 0);
76
77         virtual ~btCompoundShape();
78
79         void addChildShape(const btTransform& localTransform, btCollisionShape* shape);
80
81         /// Remove all children shapes that contain the specified shape
82         virtual void removeChildShape(btCollisionShape * shape);
83
84         void removeChildShapeByIndex(int childShapeindex);
85
86         int getNumChildShapes() const
87         {
88                 return int(m_children.size());
89         }
90
91         btCollisionShape* getChildShape(int index)
92         {
93                 return m_children[index].m_childShape;
94         }
95         const btCollisionShape* getChildShape(int index) const
96         {
97                 return m_children[index].m_childShape;
98         }
99
100         btTransform& getChildTransform(int index)
101         {
102                 return m_children[index].m_transform;
103         }
104         const btTransform& getChildTransform(int index) const
105         {
106                 return m_children[index].m_transform;
107         }
108
109         ///set a new transform for a child, and update internal data structures (local aabb and dynamic tree)
110         void updateChildTransform(int childIndex, const btTransform& newChildTransform, bool shouldRecalculateLocalAabb = true);
111
112         btCompoundShapeChild* getChildList()
113         {
114                 return &m_children[0];
115         }
116
117         ///getAabb's default implementation is brute force, expected derived classes to implement a fast dedicated version
118         virtual void getAabb(const btTransform& t, btVector3& aabbMin, btVector3& aabbMax) const;
119
120         /** Re-calculate the local Aabb. Is called at the end of removeChildShapes. 
121         Use this yourself if you modify the children or their transforms. */
122         virtual void recalculateLocalAabb();
123
124         virtual void setLocalScaling(const btVector3& scaling);
125
126         virtual const btVector3& getLocalScaling() const
127         {
128                 return m_localScaling;
129         }
130
131         virtual void calculateLocalInertia(btScalar mass, btVector3 & inertia) const;
132
133         virtual void setMargin(btScalar margin)
134         {
135                 m_collisionMargin = margin;
136         }
137         virtual btScalar getMargin() const
138         {
139                 return m_collisionMargin;
140         }
141         virtual const char* getName() const
142         {
143                 return "Compound";
144         }
145
146         const btDbvt* getDynamicAabbTree() const
147         {
148                 return m_dynamicAabbTree;
149         }
150
151         btDbvt* getDynamicAabbTree()
152         {
153                 return m_dynamicAabbTree;
154         }
155
156         void createAabbTreeFromChildren();
157
158         ///computes the exact moment of inertia and the transform from the coordinate system defined by the principal axes of the moment of inertia
159         ///and the center of mass to the current coordinate system. "masses" points to an array of masses of the children. The resulting transform
160         ///"principal" has to be applied inversely to all children transforms in order for the local coordinate system of the compound
161         ///shape to be centered at the center of mass and to coincide with the principal axes. This also necessitates a correction of the world transform
162         ///of the collision object by the principal transform.
163         void calculatePrincipalAxisTransform(const btScalar* masses, btTransform& principal, btVector3& inertia) const;
164
165         int getUpdateRevision() const
166         {
167                 return m_updateRevision;
168         }
169
170         virtual int calculateSerializeBufferSize() const;
171
172         ///fills the dataBuffer and returns the struct name (and 0 on failure)
173         virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const;
174 };
175
176 // clang-format off
177
178 ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
179 struct btCompoundShapeChildData
180 {
181         btTransformFloatData    m_transform;
182         btCollisionShapeData    *m_childShape;
183         int                                             m_childShapeType;
184         float                                   m_childMargin;
185 };
186
187 ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
188 struct  btCompoundShapeData
189 {
190         btCollisionShapeData            m_collisionShapeData;
191
192         btCompoundShapeChildData        *m_childShapePtr;
193
194         int                                                     m_numChildShapes;
195
196         float   m_collisionMargin;
197
198 };
199
200 // clang-format on
201
202 SIMD_FORCE_INLINE int btCompoundShape::calculateSerializeBufferSize() const
203 {
204         return sizeof(btCompoundShapeData);
205 }
206
207 #endif  //BT_COMPOUND_SHAPE_H