Upgrade Bullet to version 2.83.
[blender.git] / extern / bullet2 / src / BulletCollision / CollisionDispatch / btDefaultCollisionConfiguration.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 "btDefaultCollisionConfiguration.h"
17
18 #include "BulletCollision/CollisionDispatch/btConvexConvexAlgorithm.h"
19 #include "BulletCollision/CollisionDispatch/btEmptyCollisionAlgorithm.h"
20 #include "BulletCollision/CollisionDispatch/btConvexConcaveCollisionAlgorithm.h"
21 #include "BulletCollision/CollisionDispatch/btCompoundCollisionAlgorithm.h"
22 #include "BulletCollision/CollisionDispatch/btCompoundCompoundCollisionAlgorithm.h"
23
24 #include "BulletCollision/CollisionDispatch/btConvexPlaneCollisionAlgorithm.h"
25 #include "BulletCollision/CollisionDispatch/btBoxBoxCollisionAlgorithm.h"
26 #include "BulletCollision/CollisionDispatch/btSphereSphereCollisionAlgorithm.h"
27 #ifdef USE_BUGGY_SPHERE_BOX_ALGORITHM
28 #include "BulletCollision/CollisionDispatch/btSphereBoxCollisionAlgorithm.h"
29 #endif //USE_BUGGY_SPHERE_BOX_ALGORITHM
30 #include "BulletCollision/CollisionDispatch/btSphereTriangleCollisionAlgorithm.h"
31 #include "BulletCollision/NarrowPhaseCollision/btGjkEpaPenetrationDepthSolver.h"
32 #include "BulletCollision/NarrowPhaseCollision/btMinkowskiPenetrationDepthSolver.h"
33 #include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
34
35
36
37 #include "LinearMath/btPoolAllocator.h"
38
39
40
41
42
43 btDefaultCollisionConfiguration::btDefaultCollisionConfiguration(const btDefaultCollisionConstructionInfo& constructionInfo)
44 //btDefaultCollisionConfiguration::btDefaultCollisionConfiguration(btStackAlloc*        stackAlloc,btPoolAllocator*     persistentManifoldPool,btPoolAllocator* collisionAlgorithmPool)
45 {
46
47         void* mem = btAlignedAlloc(sizeof(btVoronoiSimplexSolver),16);
48         m_simplexSolver = new (mem)btVoronoiSimplexSolver();
49
50         if (constructionInfo.m_useEpaPenetrationAlgorithm)
51         {
52                 mem = btAlignedAlloc(sizeof(btGjkEpaPenetrationDepthSolver),16);
53                 m_pdSolver = new (mem)btGjkEpaPenetrationDepthSolver;
54         }else
55         {
56                 mem = btAlignedAlloc(sizeof(btMinkowskiPenetrationDepthSolver),16);
57                 m_pdSolver = new (mem)btMinkowskiPenetrationDepthSolver;
58         }
59         
60         //default CreationFunctions, filling the m_doubleDispatch table
61         mem = btAlignedAlloc(sizeof(btConvexConvexAlgorithm::CreateFunc),16);
62         m_convexConvexCreateFunc = new(mem) btConvexConvexAlgorithm::CreateFunc(m_simplexSolver,m_pdSolver);
63         mem = btAlignedAlloc(sizeof(btConvexConcaveCollisionAlgorithm::CreateFunc),16);
64         m_convexConcaveCreateFunc = new (mem)btConvexConcaveCollisionAlgorithm::CreateFunc;
65         mem = btAlignedAlloc(sizeof(btConvexConcaveCollisionAlgorithm::CreateFunc),16);
66         m_swappedConvexConcaveCreateFunc = new (mem)btConvexConcaveCollisionAlgorithm::SwappedCreateFunc;
67         mem = btAlignedAlloc(sizeof(btCompoundCollisionAlgorithm::CreateFunc),16);
68         m_compoundCreateFunc = new (mem)btCompoundCollisionAlgorithm::CreateFunc;
69
70         mem = btAlignedAlloc(sizeof(btCompoundCompoundCollisionAlgorithm::CreateFunc),16);
71         m_compoundCompoundCreateFunc = new (mem)btCompoundCompoundCollisionAlgorithm::CreateFunc;
72
73         mem = btAlignedAlloc(sizeof(btCompoundCollisionAlgorithm::SwappedCreateFunc),16);
74         m_swappedCompoundCreateFunc = new (mem)btCompoundCollisionAlgorithm::SwappedCreateFunc;
75         mem = btAlignedAlloc(sizeof(btEmptyAlgorithm::CreateFunc),16);
76         m_emptyCreateFunc = new(mem) btEmptyAlgorithm::CreateFunc;
77         
78         mem = btAlignedAlloc(sizeof(btSphereSphereCollisionAlgorithm::CreateFunc),16);
79         m_sphereSphereCF = new(mem) btSphereSphereCollisionAlgorithm::CreateFunc;
80 #ifdef USE_BUGGY_SPHERE_BOX_ALGORITHM
81         mem = btAlignedAlloc(sizeof(btSphereBoxCollisionAlgorithm::CreateFunc),16);
82         m_sphereBoxCF = new(mem) btSphereBoxCollisionAlgorithm::CreateFunc;
83         mem = btAlignedAlloc(sizeof(btSphereBoxCollisionAlgorithm::CreateFunc),16);
84         m_boxSphereCF = new (mem)btSphereBoxCollisionAlgorithm::CreateFunc;
85         m_boxSphereCF->m_swapped = true;
86 #endif //USE_BUGGY_SPHERE_BOX_ALGORITHM
87
88         mem = btAlignedAlloc(sizeof(btSphereTriangleCollisionAlgorithm::CreateFunc),16);
89         m_sphereTriangleCF = new (mem)btSphereTriangleCollisionAlgorithm::CreateFunc;
90         mem = btAlignedAlloc(sizeof(btSphereTriangleCollisionAlgorithm::CreateFunc),16);
91         m_triangleSphereCF = new (mem)btSphereTriangleCollisionAlgorithm::CreateFunc;
92         m_triangleSphereCF->m_swapped = true;
93         
94         mem = btAlignedAlloc(sizeof(btBoxBoxCollisionAlgorithm::CreateFunc),16);
95         m_boxBoxCF = new(mem)btBoxBoxCollisionAlgorithm::CreateFunc;
96
97         //convex versus plane
98         mem = btAlignedAlloc (sizeof(btConvexPlaneCollisionAlgorithm::CreateFunc),16);
99         m_convexPlaneCF = new (mem) btConvexPlaneCollisionAlgorithm::CreateFunc;
100         mem = btAlignedAlloc (sizeof(btConvexPlaneCollisionAlgorithm::CreateFunc),16);
101         m_planeConvexCF = new (mem) btConvexPlaneCollisionAlgorithm::CreateFunc;
102         m_planeConvexCF->m_swapped = true;
103         
104         ///calculate maximum element size, big enough to fit any collision algorithm in the memory pool
105         int maxSize = sizeof(btConvexConvexAlgorithm);
106         int maxSize2 = sizeof(btConvexConcaveCollisionAlgorithm);
107         int maxSize3 = sizeof(btCompoundCollisionAlgorithm);
108         int maxSize4 = sizeof(btCompoundCompoundCollisionAlgorithm);
109
110         int     collisionAlgorithmMaxElementSize = btMax(maxSize,constructionInfo.m_customCollisionAlgorithmMaxElementSize);
111         collisionAlgorithmMaxElementSize = btMax(collisionAlgorithmMaxElementSize,maxSize2);
112         collisionAlgorithmMaxElementSize = btMax(collisionAlgorithmMaxElementSize,maxSize3);
113         collisionAlgorithmMaxElementSize = btMax(collisionAlgorithmMaxElementSize,maxSize4);
114                 
115         if (constructionInfo.m_persistentManifoldPool)
116         {
117                 m_ownsPersistentManifoldPool = false;
118                 m_persistentManifoldPool = constructionInfo.m_persistentManifoldPool;
119         } else
120         {
121                 m_ownsPersistentManifoldPool = true;
122                 void* mem = btAlignedAlloc(sizeof(btPoolAllocator),16);
123                 m_persistentManifoldPool = new (mem) btPoolAllocator(sizeof(btPersistentManifold),constructionInfo.m_defaultMaxPersistentManifoldPoolSize);
124         }
125         
126         if (constructionInfo.m_collisionAlgorithmPool)
127         {
128                 m_ownsCollisionAlgorithmPool = false;
129                 m_collisionAlgorithmPool = constructionInfo.m_collisionAlgorithmPool;
130         } else
131         {
132                 m_ownsCollisionAlgorithmPool = true;
133                 void* mem = btAlignedAlloc(sizeof(btPoolAllocator),16);
134                 m_collisionAlgorithmPool = new(mem) btPoolAllocator(collisionAlgorithmMaxElementSize,constructionInfo.m_defaultMaxCollisionAlgorithmPoolSize);
135         }
136
137
138 }
139
140 btDefaultCollisionConfiguration::~btDefaultCollisionConfiguration()
141 {
142         if (m_ownsCollisionAlgorithmPool)
143         {
144                 m_collisionAlgorithmPool->~btPoolAllocator();
145                 btAlignedFree(m_collisionAlgorithmPool);
146         }
147         if (m_ownsPersistentManifoldPool)
148         {
149                 m_persistentManifoldPool->~btPoolAllocator();
150                 btAlignedFree(m_persistentManifoldPool);
151         }
152
153         m_convexConvexCreateFunc->~btCollisionAlgorithmCreateFunc();
154         btAlignedFree(  m_convexConvexCreateFunc);
155
156         m_convexConcaveCreateFunc->~btCollisionAlgorithmCreateFunc();
157         btAlignedFree( m_convexConcaveCreateFunc);
158         m_swappedConvexConcaveCreateFunc->~btCollisionAlgorithmCreateFunc();
159         btAlignedFree( m_swappedConvexConcaveCreateFunc);
160
161         m_compoundCreateFunc->~btCollisionAlgorithmCreateFunc();
162         btAlignedFree( m_compoundCreateFunc);
163
164         m_compoundCompoundCreateFunc->~btCollisionAlgorithmCreateFunc();
165         btAlignedFree(m_compoundCompoundCreateFunc);
166
167         m_swappedCompoundCreateFunc->~btCollisionAlgorithmCreateFunc();
168         btAlignedFree( m_swappedCompoundCreateFunc);
169
170         m_emptyCreateFunc->~btCollisionAlgorithmCreateFunc();
171         btAlignedFree( m_emptyCreateFunc);
172
173         m_sphereSphereCF->~btCollisionAlgorithmCreateFunc();
174         btAlignedFree( m_sphereSphereCF);
175
176 #ifdef USE_BUGGY_SPHERE_BOX_ALGORITHM
177         m_sphereBoxCF->~btCollisionAlgorithmCreateFunc();
178         btAlignedFree( m_sphereBoxCF);
179         m_boxSphereCF->~btCollisionAlgorithmCreateFunc();
180         btAlignedFree( m_boxSphereCF);
181 #endif //USE_BUGGY_SPHERE_BOX_ALGORITHM
182
183         m_sphereTriangleCF->~btCollisionAlgorithmCreateFunc();
184         btAlignedFree( m_sphereTriangleCF);
185         m_triangleSphereCF->~btCollisionAlgorithmCreateFunc();
186         btAlignedFree( m_triangleSphereCF);
187         m_boxBoxCF->~btCollisionAlgorithmCreateFunc();
188         btAlignedFree( m_boxBoxCF);
189
190         m_convexPlaneCF->~btCollisionAlgorithmCreateFunc();
191         btAlignedFree( m_convexPlaneCF);
192         m_planeConvexCF->~btCollisionAlgorithmCreateFunc();
193         btAlignedFree( m_planeConvexCF);
194
195         m_simplexSolver->~btVoronoiSimplexSolver();
196         btAlignedFree(m_simplexSolver);
197
198         m_pdSolver->~btConvexPenetrationDepthSolver();
199         
200         btAlignedFree(m_pdSolver);
201
202
203 }
204
205
206 btCollisionAlgorithmCreateFunc* btDefaultCollisionConfiguration::getCollisionAlgorithmCreateFunc(int proxyType0,int proxyType1)
207 {
208
209
210
211         if ((proxyType0 == SPHERE_SHAPE_PROXYTYPE) && (proxyType1==SPHERE_SHAPE_PROXYTYPE))
212         {
213                 return  m_sphereSphereCF;
214         }
215 #ifdef USE_BUGGY_SPHERE_BOX_ALGORITHM
216         if ((proxyType0 == SPHERE_SHAPE_PROXYTYPE) && (proxyType1==BOX_SHAPE_PROXYTYPE))
217         {
218                 return  m_sphereBoxCF;
219         }
220
221         if ((proxyType0 == BOX_SHAPE_PROXYTYPE ) && (proxyType1==SPHERE_SHAPE_PROXYTYPE))
222         {
223                 return  m_boxSphereCF;
224         }
225 #endif //USE_BUGGY_SPHERE_BOX_ALGORITHM
226
227
228         if ((proxyType0 == SPHERE_SHAPE_PROXYTYPE ) && (proxyType1==TRIANGLE_SHAPE_PROXYTYPE))
229         {
230                 return  m_sphereTriangleCF;
231         }
232
233         if ((proxyType0 == TRIANGLE_SHAPE_PROXYTYPE  ) && (proxyType1==SPHERE_SHAPE_PROXYTYPE))
234         {
235                 return  m_triangleSphereCF;
236         } 
237
238         if ((proxyType0 == BOX_SHAPE_PROXYTYPE) && (proxyType1 == BOX_SHAPE_PROXYTYPE))
239         {
240                 return m_boxBoxCF;
241         }
242         
243         if (btBroadphaseProxy::isConvex(proxyType0) && (proxyType1 == STATIC_PLANE_PROXYTYPE))
244         {
245                 return m_convexPlaneCF;
246         }
247
248         if (btBroadphaseProxy::isConvex(proxyType1) && (proxyType0 == STATIC_PLANE_PROXYTYPE))
249         {
250                 return m_planeConvexCF;
251         }
252         
253
254
255         if (btBroadphaseProxy::isConvex(proxyType0) && btBroadphaseProxy::isConvex(proxyType1))
256         {
257                 return m_convexConvexCreateFunc;
258         }
259
260         if (btBroadphaseProxy::isConvex(proxyType0) && btBroadphaseProxy::isConcave(proxyType1))
261         {
262                 return m_convexConcaveCreateFunc;
263         }
264
265         if (btBroadphaseProxy::isConvex(proxyType1) && btBroadphaseProxy::isConcave(proxyType0))
266         {
267                 return m_swappedConvexConcaveCreateFunc;
268         }
269
270
271         if (btBroadphaseProxy::isCompound(proxyType0) && btBroadphaseProxy::isCompound(proxyType1))
272         {
273                 return m_compoundCompoundCreateFunc;
274         }
275
276         if (btBroadphaseProxy::isCompound(proxyType0))
277         {
278                 return m_compoundCreateFunc;
279         } else
280         {
281                 if (btBroadphaseProxy::isCompound(proxyType1))
282                 {
283                         return m_swappedCompoundCreateFunc;
284                 }
285         }
286
287         //failed to find an algorithm
288         return m_emptyCreateFunc;
289 }
290
291 void btDefaultCollisionConfiguration::setConvexConvexMultipointIterations(int numPerturbationIterations, int minimumPointsPerturbationThreshold)
292 {
293         btConvexConvexAlgorithm::CreateFunc* convexConvex = (btConvexConvexAlgorithm::CreateFunc*) m_convexConvexCreateFunc;
294         convexConvex->m_numPerturbationIterations = numPerturbationIterations;
295         convexConvex->m_minimumPointsPerturbationThreshold = minimumPointsPerturbationThreshold;
296 }
297
298 void    btDefaultCollisionConfiguration::setPlaneConvexMultipointIterations(int numPerturbationIterations, int minimumPointsPerturbationThreshold)
299 {
300         btConvexPlaneCollisionAlgorithm::CreateFunc* cpCF = (btConvexPlaneCollisionAlgorithm::CreateFunc*)m_convexPlaneCF;
301         cpCF->m_numPerturbationIterations = numPerturbationIterations;
302         cpCF->m_minimumPointsPerturbationThreshold = minimumPointsPerturbationThreshold;
303         
304         btConvexPlaneCollisionAlgorithm::CreateFunc* pcCF = (btConvexPlaneCollisionAlgorithm::CreateFunc*)m_planeConvexCF;
305         pcCF->m_numPerturbationIterations = numPerturbationIterations;
306         pcCF->m_minimumPointsPerturbationThreshold = minimumPointsPerturbationThreshold;
307 }