merge with 2.5 at r18679
[blender.git] / extern / bullet2 / src / BulletCollision / CollisionShapes / btCompoundShape.cpp
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
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 "btCompoundShape.h"
17 #include "btCollisionShape.h"
18 #include "BulletCollision/BroadphaseCollision/btDbvt.h"
19
20 btCompoundShape::btCompoundShape()
21 :m_localAabbMin(btScalar(1e30),btScalar(1e30),btScalar(1e30)),
22 m_localAabbMax(btScalar(-1e30),btScalar(-1e30),btScalar(-1e30)),
23 m_collisionMargin(btScalar(0.)),
24 m_localScaling(btScalar(1.),btScalar(1.),btScalar(1.)),
25 m_dynamicAabbTree(0)
26 {
27         void* mem = btAlignedAlloc(sizeof(btDbvt),16);
28         m_dynamicAabbTree = new(mem) btDbvt();
29         btAssert(mem==m_dynamicAabbTree);
30 }
31
32
33 btCompoundShape::~btCompoundShape()
34 {
35         if (m_dynamicAabbTree)
36         {
37                 m_dynamicAabbTree->~btDbvt();
38                 btAlignedFree(m_dynamicAabbTree);
39         }
40 }
41
42 void    btCompoundShape::addChildShape(const btTransform& localTransform,btCollisionShape* shape)
43 {
44         //m_childTransforms.push_back(localTransform);
45         //m_childShapes.push_back(shape);
46         btCompoundShapeChild child;
47         child.m_transform = localTransform;
48         child.m_childShape = shape;
49         child.m_childShapeType = shape->getShapeType();
50         child.m_childMargin = shape->getMargin();
51
52         //extend the local aabbMin/aabbMax
53         btVector3 localAabbMin,localAabbMax;
54         shape->getAabb(localTransform,localAabbMin,localAabbMax);
55         for (int i=0;i<3;i++)
56         {
57                 if (m_localAabbMin[i] > localAabbMin[i])
58                 {
59                         m_localAabbMin[i] = localAabbMin[i];
60                 }
61                 if (m_localAabbMax[i] < localAabbMax[i])
62                 {
63                         m_localAabbMax[i] = localAabbMax[i];
64                 }
65
66         }
67         if (m_dynamicAabbTree)
68         {
69                 const btDbvtVolume      bounds=btDbvtVolume::FromMM(localAabbMin,localAabbMax);
70                 int index = m_children.size();
71                 child.m_node = m_dynamicAabbTree->insert(bounds,(void*)index);
72         }
73
74         m_children.push_back(child);
75 }
76
77 void btCompoundShape::removeChildShapeByIndex(int childShapeIndex)
78 {
79         btAssert(childShapeIndex >=0 && childShapeIndex < m_children.size());
80         if (m_dynamicAabbTree)
81         {
82                 m_dynamicAabbTree->remove(m_children[childShapeIndex].m_node);
83         }
84         m_children.swap(childShapeIndex,m_children.size()-1);
85         m_children.pop_back();
86
87 }
88
89 void btCompoundShape::removeChildShape(btCollisionShape* shape)
90 {
91         // Find the children containing the shape specified, and remove those children.
92         //note: there might be multiple children using the same shape!
93         for(int i = m_children.size()-1; i >= 0 ; i--)
94         {
95                 if(m_children[i].m_childShape == shape)
96                 {
97                         m_children.swap(i,m_children.size()-1);
98                         m_children.pop_back();
99                         //remove it from the m_dynamicAabbTree too
100                         //m_dynamicAabbTree->remove(m_aabbProxies[i]);
101                         //m_aabbProxies.swap(i,m_children.size()-1);
102                         //m_aabbProxies.pop_back();
103                 }
104         }
105
106
107
108         recalculateLocalAabb();
109 }
110
111 void btCompoundShape::recalculateLocalAabb()
112 {
113         // Recalculate the local aabb
114         // Brute force, it iterates over all the shapes left.
115         m_localAabbMin = btVector3(btScalar(1e30),btScalar(1e30),btScalar(1e30));
116         m_localAabbMax = btVector3(btScalar(-1e30),btScalar(-1e30),btScalar(-1e30));
117
118         //extend the local aabbMin/aabbMax
119         for (int j = 0; j < m_children.size(); j++)
120         {
121                 btVector3 localAabbMin,localAabbMax;
122                 m_children[j].m_childShape->getAabb(m_children[j].m_transform, localAabbMin, localAabbMax);
123                 for (int i=0;i<3;i++)
124                 {
125                         if (m_localAabbMin[i] > localAabbMin[i])
126                                 m_localAabbMin[i] = localAabbMin[i];
127                         if (m_localAabbMax[i] < localAabbMax[i])
128                                 m_localAabbMax[i] = localAabbMax[i];
129                 }
130         }
131 }
132
133 ///getAabb's default implementation is brute force, expected derived classes to implement a fast dedicated version
134 void btCompoundShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const
135 {
136         btVector3 localHalfExtents = btScalar(0.5)*(m_localAabbMax-m_localAabbMin);
137         localHalfExtents += btVector3(getMargin(),getMargin(),getMargin());
138         btVector3 localCenter = btScalar(0.5)*(m_localAabbMax+m_localAabbMin);
139
140         btMatrix3x3 abs_b = trans.getBasis().absolute();  
141
142         btPoint3 center = trans(localCenter);
143
144         btVector3 extent = btVector3(abs_b[0].dot(localHalfExtents),
145                 abs_b[1].dot(localHalfExtents),
146                 abs_b[2].dot(localHalfExtents));
147         aabbMin = center-extent;
148         aabbMax = center+extent;
149
150 }
151
152 void    btCompoundShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
153 {
154         //approximation: take the inertia from the aabb for now
155         btTransform ident;
156         ident.setIdentity();
157         btVector3 aabbMin,aabbMax;
158         getAabb(ident,aabbMin,aabbMax);
159
160         btVector3 halfExtents = (aabbMax-aabbMin)*btScalar(0.5);
161
162         btScalar lx=btScalar(2.)*(halfExtents.x());
163         btScalar ly=btScalar(2.)*(halfExtents.y());
164         btScalar lz=btScalar(2.)*(halfExtents.z());
165
166         inertia[0] = mass/(btScalar(12.0)) * (ly*ly + lz*lz);
167         inertia[1] = mass/(btScalar(12.0)) * (lx*lx + lz*lz);
168         inertia[2] = mass/(btScalar(12.0)) * (lx*lx + ly*ly);
169
170 }
171
172
173
174
175 void btCompoundShape::calculatePrincipalAxisTransform(btScalar* masses, btTransform& principal, btVector3& inertia) const
176 {
177         int n = m_children.size();
178
179         btScalar totalMass = 0;
180         btVector3 center(0, 0, 0);
181         for (int k = 0; k < n; k++)
182         {
183                 center += m_children[k].m_transform.getOrigin() * masses[k];
184                 totalMass += masses[k];
185         }
186         center /= totalMass;
187         principal.setOrigin(center);
188
189         btMatrix3x3 tensor(0, 0, 0, 0, 0, 0, 0, 0, 0);
190         for (int k = 0; k < n; k++)
191         {
192                 btVector3 i;
193                 m_children[k].m_childShape->calculateLocalInertia(masses[k], i);
194
195                 const btTransform& t = m_children[k].m_transform;
196                 btVector3 o = t.getOrigin() - center;
197
198                 //compute inertia tensor in coordinate system of compound shape
199                 btMatrix3x3 j = t.getBasis().transpose();
200                 j[0] *= i[0];
201                 j[1] *= i[1];
202                 j[2] *= i[2];
203                 j = t.getBasis() * j;
204
205                 //add inertia tensor
206                 tensor[0] += j[0];
207                 tensor[1] += j[1];
208                 tensor[2] += j[2];
209
210                 //compute inertia tensor of pointmass at o
211                 btScalar o2 = o.length2();
212                 j[0].setValue(o2, 0, 0);
213                 j[1].setValue(0, o2, 0);
214                 j[2].setValue(0, 0, o2);
215                 j[0] += o * -o.x(); 
216                 j[1] += o * -o.y(); 
217                 j[2] += o * -o.z();
218
219                 //add inertia tensor of pointmass
220                 tensor[0] += masses[k] * j[0];
221                 tensor[1] += masses[k] * j[1];
222                 tensor[2] += masses[k] * j[2];
223         }
224
225         tensor.diagonalize(principal.getBasis(), btScalar(0.00001), 20);
226         inertia.setValue(tensor[0][0], tensor[1][1], tensor[2][2]);
227 }
228
229
230