Bullet Collision Detection & Physics Library: btCollisionDispatcherMt.cpp Source File

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btCollisionDispatcherMt.cpp
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16#include "btCollisionDispatcherMt.h"
17#include "LinearMath/btQuickprof.h"
18
19#include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
20
21#include "BulletCollision/CollisionShapes/btCollisionShape.h"
22#include "BulletCollision/CollisionDispatch/btCollisionObject.h"
23#include "BulletCollision/BroadphaseCollision/btOverlappingPairCache.h"
24#include "LinearMath/btPoolAllocator.h"
25#include "BulletCollision/CollisionDispatch/btCollisionConfiguration.h"
26#include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h"
27
28btCollisionDispatcherMt::btCollisionDispatcherMt(btCollisionConfiguration* config, int grainSize)
29 : btCollisionDispatcher(config)
30{
31 m_batchManifoldsPtr.resize(btGetTaskScheduler()->getNumThreads());
32 m_batchReleasePtr.resize(btGetTaskScheduler()->getNumThreads());
33
34 m_batchUpdating = false;
35 m_grainSize = grainSize; // iterations per task
36}
37
38 btPersistentManifold* btCollisionDispatcherMt::getNewManifold(const btCollisionObject* body0, const btCollisionObject* body1)
39{
40 //optional relative contact breaking threshold, turned on by default (use setDispatcherFlags to switch off feature for improved performance)
41
42 btScalar contactBreakingThreshold = (m_dispatcherFlags & btCollisionDispatcher::CD_USE_RELATIVE_CONTACT_BREAKING_THRESHOLD) ? btMin(body0->getCollisionShape()->getContactBreakingThreshold(gContactBreakingThreshold), body1->getCollisionShape()->getContactBreakingThreshold(gContactBreakingThreshold))
43 : gContactBreakingThreshold;
44
45 btScalar contactProcessingThreshold = btMin(body0->getContactProcessingThreshold(), body1->getContactProcessingThreshold());
46
47 void* mem = m_persistentManifoldPoolAllocator->allocate(sizeof(btPersistentManifold));
48 if (NULL == mem)
49 {
50 //we got a pool memory overflow, by default we fallback to dynamically allocate memory. If we require a contiguous contact pool then assert.
51 if ((m_dispatcherFlags & CD_DISABLE_CONTACTPOOL_DYNAMIC_ALLOCATION) == 0)
52 {
53 mem = btAlignedAlloc(sizeof(btPersistentManifold), 16);
54 }
55 else
56 {
57 btAssert(0);
58 //make sure to increase the m_defaultMaxPersistentManifoldPoolSize in the btDefaultCollisionConstructionInfo/btDefaultCollisionConfiguration
59 return 0;
60 }
61 }
62 btPersistentManifold* manifold = new (mem) btPersistentManifold(body0, body1, 0, contactBreakingThreshold, contactProcessingThreshold);
63 if (!m_batchUpdating)
64 {
65 // batch updater will update manifold pointers array after finishing, so
66 // only need to update array when not batch-updating
67 //btAssert( !btThreadsAreRunning() );
68 manifold->m_index1a = m_manifoldsPtr.size();
69 m_manifoldsPtr.push_back(manifold);
70 }
71 else
72 {
73 m_batchManifoldsPtr[btGetCurrentThreadIndex()].push_back(manifold);
74 }
75
76 return manifold;
77}
78
79 void btCollisionDispatcherMt::releaseManifold(btPersistentManifold* manifold)
80{
81 //btAssert( !btThreadsAreRunning() );
82
83 if (!m_batchUpdating)
84 {
85 clearManifold(manifold);
86 // batch updater will update manifold pointers array after finishing, so
87 // only need to update array when not batch-updating
88 int findIndex = manifold->m_index1a;
89 btAssert(findIndex < m_manifoldsPtr.size());
90 m_manifoldsPtr.swap(findIndex, m_manifoldsPtr.size() - 1);
91 m_manifoldsPtr[findIndex]->m_index1a = findIndex;
92 m_manifoldsPtr.pop_back();
93 } else {
94 m_batchReleasePtr[btGetCurrentThreadIndex()].push_back(manifold);
95 return;
96 }
97
98 manifold->~btPersistentManifold();
99 if (m_persistentManifoldPoolAllocator->validPtr(manifold))
100 {
101 m_persistentManifoldPoolAllocator->freeMemory(manifold);
102 }
103 else
104 {
105 btAlignedFree(manifold);
106 }
107}
108
109 struct CollisionDispatcherUpdater : public btIParallelForBody
110{
111 btBroadphasePair* mPairArray;
112 btNearCallback mCallback;
113 btCollisionDispatcher* mDispatcher;
114 const btDispatcherInfo* mInfo;
115
116 CollisionDispatcherUpdater()
117 {
118 mPairArray = NULL;
119 mCallback = NULL;
120 mDispatcher = NULL;
121 mInfo = NULL;
122 }
123 void forLoop(int iBegin, int iEnd) const
124 {
125 for (int i = iBegin; i < iEnd; ++i)
126 {
127 btBroadphasePair* pair = &mPairArray[i];
128 mCallback(*pair, *mDispatcher, *mInfo);
129 }
130 }
131};
132
133 void btCollisionDispatcherMt::dispatchAllCollisionPairs(btOverlappingPairCache* pairCache, const btDispatcherInfo& info, btDispatcher* dispatcher)
134{
135 const int pairCount = pairCache->getNumOverlappingPairs();
136 if (pairCount == 0)
137 {
138 return;
139 }
140 CollisionDispatcherUpdater updater;
141 updater.mCallback = getNearCallback();
142 updater.mPairArray = pairCache->getOverlappingPairArrayPtr();
143 updater.mDispatcher = this;
144 updater.mInfo = &info;
145
146 m_batchUpdating = true;
147 btParallelFor(0, pairCount, m_grainSize, updater);
148 m_batchUpdating = false;
149
150 // merge new manifolds, if any
151 for (int i = 0; i < m_batchManifoldsPtr.size(); ++i)
152 {
153 btAlignedObjectArray<btPersistentManifold*>& batchManifoldsPtr = m_batchManifoldsPtr[i];
154
155 for (int j = 0; j < batchManifoldsPtr.size(); ++j)
156 {
157 m_manifoldsPtr.push_back(batchManifoldsPtr[j]);
158 }
159
160 batchManifoldsPtr.resizeNoInitialize(0);
161 }
162
163 // remove batched remove manifolds.
164 for (int i = 0; i < m_batchReleasePtr.size(); ++i)
165 {
166 btAlignedObjectArray<btPersistentManifold*>& batchManifoldsPtr = m_batchReleasePtr[i];
167 for (int j = 0; j < batchManifoldsPtr.size(); ++j)
168 {
169 releaseManifold(batchManifoldsPtr[j]);
170 }
171 batchManifoldsPtr.resizeNoInitialize(0);
172 }
173
174 // update the indices (used when releasing manifolds)
175 for (int i = 0; i < m_manifoldsPtr.size(); ++i)
176 {
177 m_manifoldsPtr[i]->m_index1a = i;
178 }
179}
btAlignedFree
#define btAlignedFree(ptr)
Definition: btAlignedAllocator.h:47
btAlignedAlloc
#define btAlignedAlloc(size, alignment)
Definition: btAlignedAllocator.h:46
btCollisionAlgorithm.h
btCollisionConfiguration.h
btCollisionDispatcherMt.h
btNearCallback
void(* btNearCallback)(btBroadphasePair &collisionPair, btCollisionDispatcher &dispatcher, const btDispatcherInfo &dispatchInfo)
user can override this nearcallback for collision filtering and more finegrained control over collisi...
Definition: btCollisionDispatcher.h:38
btCollisionObjectWrapper.h
btCollisionObject.h
btCollisionShape.h
gContactBreakingThreshold
btScalar gContactBreakingThreshold
Definition: btPersistentManifold.cpp:26
btMin
const T & btMin(const T &a, const T &b)
Definition: btMinMax.h:21
btOverlappingPairCache.h
btPoolAllocator.h
btQuickprof.h
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
btAssert
#define btAssert(x)
Definition: btScalar.h:153
btGetCurrentThreadIndex
unsigned int btGetCurrentThreadIndex()
Definition: btThreads.cpp:290
btGetTaskScheduler
btITaskScheduler * btGetTaskScheduler()
Definition: btThreads.cpp:407
btParallelFor
void btParallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody &body)
Definition: btThreads.cpp:412
btAlignedObjectArray
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
Definition: btAlignedObjectArray.h:46
btAlignedObjectArray::resizeNoInitialize
void resizeNoInitialize(int newsize)
resize changes the number of elements in the array.
Definition: btAlignedObjectArray.h:194
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:142
btAlignedObjectArray::resize
void resize(int newsize, const T &fillData=T())
Definition: btAlignedObjectArray.h:203
btAlignedObjectArray::swap
void swap(int index0, int index1)
Definition: btAlignedObjectArray.h:382
btAlignedObjectArray::push_back
void push_back(const T &_Val)
Definition: btAlignedObjectArray.h:257
btAlignedObjectArray::pop_back
void pop_back()
Definition: btAlignedObjectArray.h:185
btCollisionConfiguration
btCollisionConfiguration allows to configure Bullet collision detection stack allocator size,...
Definition: btCollisionConfiguration.h:27
btCollisionDispatcherMt::m_batchReleasePtr
btAlignedObjectArray< btAlignedObjectArray< btPersistentManifold * > > m_batchReleasePtr
Definition: btCollisionDispatcherMt.h:34
btCollisionDispatcherMt::getNewManifold
virtual btPersistentManifold * getNewManifold(const btCollisionObject *body0, const btCollisionObject *body1) BT_OVERRIDE
Definition: btCollisionDispatcherMt.cpp:38
btCollisionDispatcherMt::m_batchManifoldsPtr
btAlignedObjectArray< btAlignedObjectArray< btPersistentManifold * > > m_batchManifoldsPtr
Definition: btCollisionDispatcherMt.h:33
btCollisionDispatcherMt::dispatchAllCollisionPairs
virtual void dispatchAllCollisionPairs(btOverlappingPairCache *pairCache, const btDispatcherInfo &info, btDispatcher *dispatcher) BT_OVERRIDE
Definition: btCollisionDispatcherMt.cpp:133
btCollisionDispatcherMt::m_batchUpdating
bool m_batchUpdating
Definition: btCollisionDispatcherMt.h:35
btCollisionDispatcherMt::btCollisionDispatcherMt
btCollisionDispatcherMt(btCollisionConfiguration *config, int grainSize=40)
Definition: btCollisionDispatcherMt.cpp:28
btCollisionDispatcherMt::releaseManifold
virtual void releaseManifold(btPersistentManifold *manifold) BT_OVERRIDE
Definition: btCollisionDispatcherMt.cpp:79
btCollisionDispatcherMt::m_grainSize
int m_grainSize
Definition: btCollisionDispatcherMt.h:36
btCollisionDispatcher
btCollisionDispatcher supports algorithms that handle ConvexConvex and ConvexConcave collision pairs.
Definition: btCollisionDispatcher.h:43
btCollisionDispatcher::m_dispatcherFlags
int m_dispatcherFlags
Definition: btCollisionDispatcher.h:45
btCollisionDispatcher::getNearCallback
btNearCallback getNearCallback() const
Definition: btCollisionDispatcher.h:131
btCollisionDispatcher::clearManifold
virtual void clearManifold(btPersistentManifold *manifold)
Definition: btCollisionDispatcher.cpp:101
btCollisionDispatcher::CD_USE_RELATIVE_CONTACT_BREAKING_THRESHOLD
@ CD_USE_RELATIVE_CONTACT_BREAKING_THRESHOLD
Definition: btCollisionDispatcher.h:65
btCollisionDispatcher::CD_DISABLE_CONTACTPOOL_DYNAMIC_ALLOCATION
@ CD_DISABLE_CONTACTPOOL_DYNAMIC_ALLOCATION
Definition: btCollisionDispatcher.h:66
btCollisionDispatcher::m_manifoldsPtr
btAlignedObjectArray< btPersistentManifold * > m_manifoldsPtr
Definition: btCollisionDispatcher.h:47
btCollisionDispatcher::m_persistentManifoldPoolAllocator
btPoolAllocator * m_persistentManifoldPoolAllocator
Definition: btCollisionDispatcher.h:53
btCollisionObject
btCollisionObject can be used to manage collision detection objects.
Definition: btCollisionObject.h:51
btCollisionObject::getContactProcessingThreshold
btScalar getContactProcessingThreshold() const
Definition: btCollisionObject.h:192
btCollisionObject::getCollisionShape
const btCollisionShape * getCollisionShape() const
Definition: btCollisionObject.h:228
btCollisionShape::getContactBreakingThreshold
virtual btScalar getContactBreakingThreshold(btScalar defaultContactThresholdFactor) const
Definition: btCollisionShape.cpp:42
btDispatcher
The btDispatcher interface class can be used in combination with broadphase to dispatch calculations ...
Definition: btDispatcher.h:77
btIParallelForBody
Definition: btThreads.h:102
btOverlappingPairCache
The btOverlappingPairCache provides an interface for overlapping pair management (add,...
Definition: btOverlappingPairCache.h:51
btOverlappingPairCache::getNumOverlappingPairs
virtual int getNumOverlappingPairs() const =0
btOverlappingPairCache::getOverlappingPairArrayPtr
virtual btBroadphasePair * getOverlappingPairArrayPtr()=0
btPersistentManifold
btPersistentManifold is a contact point cache, it stays persistent as long as objects are overlapping...
Definition: btPersistentManifold.h:65
btPersistentManifold::m_index1a
int m_index1a
Definition: btPersistentManifold.h:88
btPoolAllocator::freeMemory
void freeMemory(void *ptr)
Definition: btPoolAllocator.h:100
btPoolAllocator::validPtr
bool validPtr(void *ptr)
Definition: btPoolAllocator.h:88
btPoolAllocator::allocate
void * allocate(int size)
Definition: btPoolAllocator.h:71
CollisionDispatcherUpdater
Definition: btCollisionDispatcherMt.cpp:110
CollisionDispatcherUpdater::CollisionDispatcherUpdater
CollisionDispatcherUpdater()
Definition: btCollisionDispatcherMt.cpp:116
CollisionDispatcherUpdater::forLoop
void forLoop(int iBegin, int iEnd) const
Definition: btCollisionDispatcherMt.cpp:123
CollisionDispatcherUpdater::mInfo
const btDispatcherInfo * mInfo
Definition: btCollisionDispatcherMt.cpp:114
CollisionDispatcherUpdater::mCallback
btNearCallback mCallback
Definition: btCollisionDispatcherMt.cpp:112
CollisionDispatcherUpdater::mPairArray
btBroadphasePair * mPairArray
Definition: btCollisionDispatcherMt.cpp:111
CollisionDispatcherUpdater::mDispatcher
btCollisionDispatcher * mDispatcher
Definition: btCollisionDispatcherMt.cpp:113
btBroadphasePair
The btBroadphasePair class contains a pair of aabb-overlapping objects.
Definition: btBroadphaseProxy.h:179
btDispatcherInfo
Definition: btDispatcher.h:31