merge runk 16887:16950
[blender.git] / extern / bullet2 / src / BulletSoftBody / btSoftBodyRigidBodyCollisionConfiguration.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 "btSoftBodyRigidBodyCollisionConfiguration.h"
17 #include "btSoftRigidCollisionAlgorithm.h"
18 #include "btSoftBodyConcaveCollisionAlgorithm.h"
19 #include "btSoftSoftCollisionAlgorithm.h"
20
21 #include "LinearMath/btPoolAllocator.h"
22
23 #define ENABLE_SOFTBODY_CONCAVE_COLLISIONS 1
24
25 btSoftBodyRigidBodyCollisionConfiguration::btSoftBodyRigidBodyCollisionConfiguration(const btDefaultCollisionConstructionInfo& constructionInfo)
26 :btDefaultCollisionConfiguration(constructionInfo)
27 {
28         void* mem;
29
30         mem = btAlignedAlloc(sizeof(btSoftSoftCollisionAlgorithm::CreateFunc),16);
31         m_softSoftCreateFunc = new(mem) btSoftSoftCollisionAlgorithm::CreateFunc;
32         
33         mem = btAlignedAlloc(sizeof(btSoftRigidCollisionAlgorithm::CreateFunc),16);
34         m_softRigidConvexCreateFunc = new(mem) btSoftRigidCollisionAlgorithm::CreateFunc;
35         
36         mem = btAlignedAlloc(sizeof(btSoftRigidCollisionAlgorithm::CreateFunc),16);
37         m_swappedSoftRigidConvexCreateFunc = new(mem) btSoftRigidCollisionAlgorithm::CreateFunc;
38         m_swappedSoftRigidConvexCreateFunc->m_swapped=true;
39
40 #ifdef ENABLE_SOFTBODY_CONCAVE_COLLISIONS
41         mem = btAlignedAlloc(sizeof(btSoftBodyConcaveCollisionAlgorithm::CreateFunc),16);
42         m_softRigidConcaveCreateFunc = new(mem) btSoftBodyConcaveCollisionAlgorithm::CreateFunc;
43         
44         mem = btAlignedAlloc(sizeof(btSoftBodyConcaveCollisionAlgorithm::CreateFunc),16);
45         m_swappedSoftRigidConcaveCreateFunc = new(mem) btSoftBodyConcaveCollisionAlgorithm::SwappedCreateFunc;
46         m_swappedSoftRigidConcaveCreateFunc->m_swapped=true;
47 #endif
48
49         //replace pool by a new one, with potential larger size
50         
51         if (m_ownsCollisionAlgorithmPool && m_collisionAlgorithmPool)
52         {
53                 int curElemSize = m_collisionAlgorithmPool->getElementSize();
54                 ///calculate maximum element size, big enough to fit any collision algorithm in the memory pool
55                 
56                 
57                 int maxSize0 = sizeof(btSoftSoftCollisionAlgorithm);
58                 int maxSize1 = sizeof(btSoftRigidCollisionAlgorithm);
59                 int maxSize2 = sizeof(btSoftBodyConcaveCollisionAlgorithm);
60
61                 int     collisionAlgorithmMaxElementSize = btMax(maxSize0,maxSize1);
62                 collisionAlgorithmMaxElementSize = btMax(collisionAlgorithmMaxElementSize,maxSize2);
63                 if (collisionAlgorithmMaxElementSize > curElemSize)
64                 {
65                         btAlignedFree(m_collisionAlgorithmPool);
66                         void* mem = btAlignedAlloc(sizeof(btPoolAllocator),16);
67                         m_collisionAlgorithmPool = new(mem) btPoolAllocator(collisionAlgorithmMaxElementSize,constructionInfo.m_defaultMaxCollisionAlgorithmPoolSize);
68                 }
69         }
70
71
72
73
74 }
75
76 btSoftBodyRigidBodyCollisionConfiguration::~btSoftBodyRigidBodyCollisionConfiguration()
77 {
78         m_softSoftCreateFunc->~btCollisionAlgorithmCreateFunc();
79         btAlignedFree(  m_softSoftCreateFunc);
80
81         m_softRigidConvexCreateFunc->~btCollisionAlgorithmCreateFunc();
82         btAlignedFree(  m_softRigidConvexCreateFunc);
83
84         m_swappedSoftRigidConvexCreateFunc->~btCollisionAlgorithmCreateFunc();
85         btAlignedFree(  m_swappedSoftRigidConvexCreateFunc);
86
87 #ifdef ENABLE_SOFTBODY_CONCAVE_COLLISIONS
88         m_softRigidConcaveCreateFunc->~btCollisionAlgorithmCreateFunc();
89         btAlignedFree(  m_softRigidConcaveCreateFunc);
90
91         m_swappedSoftRigidConcaveCreateFunc->~btCollisionAlgorithmCreateFunc();
92         btAlignedFree(  m_swappedSoftRigidConcaveCreateFunc);
93 #endif
94 }
95         
96 ///creation of soft-soft and soft-rigid, and otherwise fallback to base class implementation
97 btCollisionAlgorithmCreateFunc* btSoftBodyRigidBodyCollisionConfiguration::getCollisionAlgorithmCreateFunc(int proxyType0,int proxyType1)
98 {
99
100         ///try to handle the softbody interactions first
101
102         if ((proxyType0 == SOFTBODY_SHAPE_PROXYTYPE  ) && (proxyType1==SOFTBODY_SHAPE_PROXYTYPE))
103         {
104                 return  m_softSoftCreateFunc;
105         }
106
107         ///softbody versus convex
108         if (proxyType0 == SOFTBODY_SHAPE_PROXYTYPE  && btBroadphaseProxy::isConvex(proxyType1))
109         {
110                 return  m_softRigidConvexCreateFunc;
111         }
112
113         ///convex versus soft body
114         if (btBroadphaseProxy::isConvex(proxyType0) && proxyType1 == SOFTBODY_SHAPE_PROXYTYPE )
115         {
116                 return  m_swappedSoftRigidConvexCreateFunc;
117         }
118
119 #ifdef ENABLE_SOFTBODY_CONCAVE_COLLISIONS
120         ///softbody versus convex
121         if (proxyType0 == SOFTBODY_SHAPE_PROXYTYPE  && btBroadphaseProxy::isConcave(proxyType1))
122         {
123                 return  m_softRigidConcaveCreateFunc;
124         }
125
126         ///convex versus soft body
127         if (btBroadphaseProxy::isConcave(proxyType0) && proxyType1 == SOFTBODY_SHAPE_PROXYTYPE )
128         {
129                 return  m_swappedSoftRigidConcaveCreateFunc;
130         }
131 #endif
132
133         ///fallback to the regular rigid collision shape
134         return btDefaultCollisionConfiguration::getCollisionAlgorithmCreateFunc(proxyType0,proxyType1);
135 }