Bullet Collision Detection & Physics Library
btCollisionDispatcherMt.cpp
Go to the documentation of this file.
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 
17 
19 #include "LinearMath/btQuickprof.h"
20 
22 
29 
30 
32  : btCollisionDispatcher( config )
33 {
34  m_batchUpdating = false;
35  m_grainSize = grainSize; // iterations per task
36 }
37 
38 
40 {
41  //optional relative contact breaking threshold, turned on by default (use setDispatcherFlags to switch off feature for improved performance)
42 
46 
47  btScalar contactProcessingThreshold = btMin( body0->getContactProcessingThreshold(), body1->getContactProcessingThreshold() );
48 
50  if ( NULL == mem )
51  {
52  //we got a pool memory overflow, by default we fallback to dynamically allocate memory. If we require a contiguous contact pool then assert.
54  {
55  mem = btAlignedAlloc( sizeof( btPersistentManifold ), 16 );
56  }
57  else
58  {
59  btAssert( 0 );
60  //make sure to increase the m_defaultMaxPersistentManifoldPoolSize in the btDefaultCollisionConstructionInfo/btDefaultCollisionConfiguration
61  return 0;
62  }
63  }
64  btPersistentManifold* manifold = new( mem ) btPersistentManifold( body0, body1, 0, contactBreakingThreshold, contactProcessingThreshold );
65  if ( !m_batchUpdating )
66  {
67  // batch updater will update manifold pointers array after finishing, so
68  // only need to update array when not batch-updating
69  //btAssert( !btThreadsAreRunning() );
70  manifold->m_index1a = m_manifoldsPtr.size();
71  m_manifoldsPtr.push_back( manifold );
72  }
73 
74  return manifold;
75 }
76 
78 {
79  clearManifold( manifold );
80  //btAssert( !btThreadsAreRunning() );
81  if ( !m_batchUpdating )
82  {
83  // batch updater will update manifold pointers array after finishing, so
84  // only need to update array when not batch-updating
85  int findIndex = manifold->m_index1a;
86  btAssert( findIndex < m_manifoldsPtr.size() );
87  m_manifoldsPtr.swap( findIndex, m_manifoldsPtr.size() - 1 );
88  m_manifoldsPtr[ findIndex ]->m_index1a = findIndex;
90  }
91 
92  manifold->~btPersistentManifold();
94  {
96  }
97  else
98  {
99  btAlignedFree( manifold );
100  }
101 }
102 
104 {
109 
111  {
112  mPairArray = NULL;
113  mCallback = NULL;
114  mDispatcher = NULL;
115  mInfo = NULL;
116  }
117  void forLoop( int iBegin, int iEnd ) const
118  {
119  for ( int i = iBegin; i < iEnd; ++i )
120  {
121  btBroadphasePair* pair = &mPairArray[ i ];
122  mCallback( *pair, *mDispatcher, *mInfo );
123  }
124  }
125 };
126 
127 
129 {
130  int pairCount = pairCache->getNumOverlappingPairs();
131  if ( pairCount == 0 )
132  {
133  return;
134  }
136  updater.mCallback = getNearCallback();
137  updater.mPairArray = pairCache->getOverlappingPairArrayPtr();
138  updater.mDispatcher = this;
139  updater.mInfo = &info;
140 
141  m_batchUpdating = true;
142  btParallelFor( 0, pairCount, m_grainSize, updater );
143  m_batchUpdating = false;
144 
145  // reconstruct the manifolds array to ensure determinism
147 
148  btBroadphasePair* pairs = pairCache->getOverlappingPairArrayPtr();
149  for ( int i = 0; i < pairCount; ++i )
150  {
151  if (btCollisionAlgorithm* algo = pairs[ i ].m_algorithm)
152  {
153  algo->getAllContactManifolds( m_manifoldsPtr );
154  }
155  }
156 
157  // update the indices (used when releasing manifolds)
158  for ( int i = 0; i < m_manifoldsPtr.size(); ++i )
159  {
160  m_manifoldsPtr[ i ]->m_index1a = i;
161  }
162 }
163 
164 
btCollisionConfiguration.h
btCollisionDispatcher::m_persistentManifoldPoolAllocator
btPoolAllocator * m_persistentManifoldPoolAllocator
Definition: btCollisionDispatcher.h:58
btCollisionObject
btCollisionObject can be used to manage collision detection objects.
Definition: btCollisionObject.h:49
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
btCollisionDispatcher::getNearCallback
btNearCallback getNearCallback() const
Definition: btCollisionDispatcher.h:135
btAlignedFree
#define btAlignedFree(ptr)
Definition: btAlignedAllocator.h:48
btCollisionShape.h
btCollisionDispatcher::m_manifoldsPtr
btAlignedObjectArray< btPersistentManifold * > m_manifoldsPtr
Definition: btCollisionDispatcher.h:50
btCollisionShape::getContactBreakingThreshold
virtual btScalar getContactBreakingThreshold(btScalar defaultContactThresholdFactor) const
Definition: btCollisionShape.cpp:45
CollisionDispatcherUpdater::mDispatcher
btCollisionDispatcher * mDispatcher
Definition: btCollisionDispatcherMt.cpp:107
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292
btCollisionObjectWrapper.h
btDispatcher
The btDispatcher interface class can be used in combination with broadphase to dispatch calculations ...
Definition: btDispatcher.h:75
CollisionDispatcherUpdater::mCallback
btNearCallback mCallback
Definition: btCollisionDispatcherMt.cpp:106
btOverlappingPairCache::getNumOverlappingPairs
virtual int getNumOverlappingPairs() const =0
btCollisionDispatcher::clearManifold
virtual void clearManifold(btPersistentManifold *manifold)
Definition: btCollisionDispatcher.cpp:115
btCollisionDispatcherMt::btCollisionDispatcherMt
btCollisionDispatcherMt(btCollisionConfiguration *config, int grainSize=40)
Definition: btCollisionDispatcherMt.cpp:31
btAlignedAlloc
#define btAlignedAlloc(size, alignment)
Definition: btAlignedAllocator.h:47
btCollisionDispatcherMt::getNewManifold
virtual btPersistentManifold * getNewManifold(const btCollisionObject *body0, const btCollisionObject *body1) BT_OVERRIDE
Definition: btCollisionDispatcherMt.cpp:39
btPoolAllocator::freeMemory
void freeMemory(void *ptr)
Definition: btPoolAllocator.h:100
btCollisionDispatcherMt::m_grainSize
int m_grainSize
Definition: btCollisionDispatcherMt.h:35
btCollisionDispatcher
btCollisionDispatcher supports algorithms that handle ConvexConvex and ConvexConcave collision pairs.
Definition: btCollisionDispatcher.h:43
CollisionDispatcherUpdater::CollisionDispatcherUpdater
CollisionDispatcherUpdater()
Definition: btCollisionDispatcherMt.cpp:110
btMin
const T & btMin(const T &a, const T &b)
Definition: btMinMax.h:23
btCollisionAlgorithm
btCollisionAlgorithm is an collision interface that is compatible with the Broadphase and btDispatche...
Definition: btCollisionAlgorithm.h:55
btAssert
#define btAssert(x)
Definition: btScalar.h:131
btAlignedObjectArray::swap
void swap(int index0, int index1)
Definition: btAlignedObjectArray.h:405
btOverlappingPairCache.h
btOverlappingPairCache
The btOverlappingPairCache provides an interface for overlapping pair management (add,...
Definition: btOverlappingPairCache.h:60
btAlignedObjectArray::pop_back
void pop_back()
Definition: btAlignedObjectArray.h:199
btOverlappingPairCache::getOverlappingPairArrayPtr
virtual btBroadphasePair * getOverlappingPairArrayPtr()=0
btCollisionDispatcherMt::releaseManifold
virtual void releaseManifold(btPersistentManifold *manifold) BT_OVERRIDE
Definition: btCollisionDispatcherMt.cpp:77
btCollisionObject::getContactProcessingThreshold
btScalar getContactProcessingThreshold() const
Definition: btCollisionObject.h:193
btPoolAllocator::allocate
void * allocate(int size)
Definition: btPoolAllocator.h:72
btPersistentManifold
btPersistentManifold is a contact point cache, it stays persistent as long as objects are overlapping...
Definition: btPersistentManifold.h:63
CollisionDispatcherUpdater::mPairArray
btBroadphasePair * mPairArray
Definition: btCollisionDispatcherMt.cpp:105
btDispatcherInfo
Definition: btDispatcher.h:30
btCollisionDispatcher::CD_USE_RELATIVE_CONTACT_BREAKING_THRESHOLD
@ CD_USE_RELATIVE_CONTACT_BREAKING_THRESHOLD
Definition: btCollisionDispatcher.h:72
btParallelFor
void btParallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody &body)
Definition: btThreads.cpp:429
btPersistentManifold::m_index1a
int m_index1a
Definition: btPersistentManifold.h:90
btCollisionDispatcher::m_dispatcherFlags
int m_dispatcherFlags
Definition: btCollisionDispatcher.h:48
CollisionDispatcherUpdater::mInfo
const btDispatcherInfo * mInfo
Definition: btCollisionDispatcherMt.cpp:108
btCollisionDispatcherMt::dispatchAllCollisionPairs
virtual void dispatchAllCollisionPairs(btOverlappingPairCache *pairCache, const btDispatcherInfo &info, btDispatcher *dispatcher) BT_OVERRIDE
Definition: btCollisionDispatcherMt.cpp:128
btQuickprof.h
CollisionDispatcherUpdater::forLoop
void forLoop(int iBegin, int iEnd) const
Definition: btCollisionDispatcherMt.cpp:117
CollisionDispatcherUpdater
Definition: btCollisionDispatcherMt.cpp:103
btCollisionObject.h
btPoolAllocator::validPtr
bool validPtr(void *ptr)
Definition: btPoolAllocator.h:89
btAlignedObjectArray::resizeNoInitialize
void resizeNoInitialize(int newsize)
resize changes the number of elements in the array.
Definition: btAlignedObjectArray.h:209
btIParallelForBody
Definition: btThreads.h:98
gContactBreakingThreshold
btScalar gContactBreakingThreshold
Definition: btPersistentManifold.cpp:21
btAlignedObjectArray::push_back
void push_back(const T &_Val)
Definition: btAlignedObjectArray.h:274
btCollisionDispatcher::CD_DISABLE_CONTACTPOOL_DYNAMIC_ALLOCATION
@ CD_DISABLE_CONTACTPOOL_DYNAMIC_ALLOCATION
Definition: btCollisionDispatcher.h:73
btCollisionConfiguration
btCollisionConfiguration allows to configure Bullet collision detection stack allocator size,...
Definition: btCollisionConfiguration.h:26
btPoolAllocator.h
btCollisionDispatcherMt::m_batchUpdating
bool m_batchUpdating
Definition: btCollisionDispatcherMt.h:34
btCollisionDispatcherMt.h
btBroadphasePair
The btBroadphasePair class contains a pair of aabb-overlapping objects.
Definition: btBroadphaseProxy.h:185
btCollisionAlgorithm.h
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:155
btCollisionObject::getCollisionShape
const btCollisionShape * getCollisionShape() const
Definition: btCollisionObject.h:228