2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
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:
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.
16 #ifndef BT_OVERLAPPING_PAIR_CACHE_H
17 #define BT_OVERLAPPING_PAIR_CACHE_H
19 #include "btBroadphaseInterface.h"
20 #include "btBroadphaseProxy.h"
21 #include "btOverlappingPairCallback.h"
23 #include "LinearMath/btAlignedObjectArray.h"
26 typedef btAlignedObjectArray<btBroadphasePair> btBroadphasePairArray;
28 struct btOverlapCallback
30 virtual ~btOverlapCallback()
33 //return true for deletion of the pair
34 virtual bool processOverlap(btBroadphasePair& pair) = 0;
37 struct btOverlapFilterCallback
39 virtual ~btOverlapFilterCallback()
42 // return true when pairs need collision
43 virtual bool needBroadphaseCollision(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1) const = 0;
46 const int BT_NULL_PAIR = 0xffffffff;
48 ///The btOverlappingPairCache provides an interface for overlapping pair management (add, remove, storage), used by the btBroadphaseInterface broadphases.
49 ///The btHashedOverlappingPairCache and btSortedOverlappingPairCache classes are two implementations.
50 class btOverlappingPairCache : public btOverlappingPairCallback
53 virtual ~btOverlappingPairCache() {} // this is needed so we can get to the derived class destructor
55 virtual btBroadphasePair* getOverlappingPairArrayPtr() = 0;
57 virtual const btBroadphasePair* getOverlappingPairArrayPtr() const = 0;
59 virtual btBroadphasePairArray& getOverlappingPairArray() = 0;
61 virtual void cleanOverlappingPair(btBroadphasePair& pair, btDispatcher* dispatcher) = 0;
63 virtual int getNumOverlappingPairs() const = 0;
64 virtual bool needsBroadphaseCollision(btBroadphaseProxy * proxy0, btBroadphaseProxy * proxy1) const = 0;
65 virtual btOverlapFilterCallback* getOverlapFilterCallback() = 0;
66 virtual void cleanProxyFromPairs(btBroadphaseProxy* proxy, btDispatcher* dispatcher) = 0;
68 virtual void setOverlapFilterCallback(btOverlapFilterCallback* callback) = 0;
70 virtual void processAllOverlappingPairs(btOverlapCallback*, btDispatcher* dispatcher) = 0;
72 virtual void processAllOverlappingPairs(btOverlapCallback* callback, btDispatcher* dispatcher, const struct btDispatcherInfo& /*dispatchInfo*/)
74 processAllOverlappingPairs(callback, dispatcher);
76 virtual btBroadphasePair* findPair(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1) = 0;
78 virtual bool hasDeferredRemoval() = 0;
80 virtual void setInternalGhostPairCallback(btOverlappingPairCallback* ghostPairCallback) = 0;
82 virtual void sortOverlappingPairs(btDispatcher* dispatcher) = 0;
85 /// Hash-space based Pair Cache, thanks to Erin Catto, Box2D, http://www.box2d.org, and Pierre Terdiman, Codercorner, http://codercorner.com
87 ATTRIBUTE_ALIGNED16(class)
88 btHashedOverlappingPairCache : public btOverlappingPairCache
90 btBroadphasePairArray m_overlappingPairArray;
91 btOverlapFilterCallback* m_overlapFilterCallback;
94 btAlignedObjectArray<int> m_hashTable;
95 btAlignedObjectArray<int> m_next;
96 btOverlappingPairCallback* m_ghostPairCallback;
99 BT_DECLARE_ALIGNED_ALLOCATOR();
101 btHashedOverlappingPairCache();
102 virtual ~btHashedOverlappingPairCache();
104 void removeOverlappingPairsContainingProxy(btBroadphaseProxy * proxy, btDispatcher * dispatcher);
106 virtual void* removeOverlappingPair(btBroadphaseProxy * proxy0, btBroadphaseProxy * proxy1, btDispatcher * dispatcher);
108 SIMD_FORCE_INLINE bool needsBroadphaseCollision(btBroadphaseProxy * proxy0, btBroadphaseProxy * proxy1) const
110 if (m_overlapFilterCallback)
111 return m_overlapFilterCallback->needBroadphaseCollision(proxy0, proxy1);
113 bool collides = (proxy0->m_collisionFilterGroup & proxy1->m_collisionFilterMask) != 0;
114 collides = collides && (proxy1->m_collisionFilterGroup & proxy0->m_collisionFilterMask);
119 // Add a pair and return the new pair. If the pair already exists,
120 // no new pair is created and the old one is returned.
121 virtual btBroadphasePair* addOverlappingPair(btBroadphaseProxy * proxy0, btBroadphaseProxy * proxy1)
123 if (!needsBroadphaseCollision(proxy0, proxy1))
126 return internalAddPair(proxy0, proxy1);
129 void cleanProxyFromPairs(btBroadphaseProxy * proxy, btDispatcher * dispatcher);
131 virtual void processAllOverlappingPairs(btOverlapCallback*, btDispatcher * dispatcher);
133 virtual void processAllOverlappingPairs(btOverlapCallback * callback, btDispatcher * dispatcher, const struct btDispatcherInfo& dispatchInfo);
135 virtual btBroadphasePair* getOverlappingPairArrayPtr()
137 return &m_overlappingPairArray[0];
140 const btBroadphasePair* getOverlappingPairArrayPtr() const
142 return &m_overlappingPairArray[0];
145 btBroadphasePairArray& getOverlappingPairArray()
147 return m_overlappingPairArray;
150 const btBroadphasePairArray& getOverlappingPairArray() const
152 return m_overlappingPairArray;
155 void cleanOverlappingPair(btBroadphasePair & pair, btDispatcher * dispatcher);
157 btBroadphasePair* findPair(btBroadphaseProxy * proxy0, btBroadphaseProxy * proxy1);
159 int GetCount() const { return m_overlappingPairArray.size(); }
160 // btBroadphasePair* GetPairs() { return m_pairs; }
162 btOverlapFilterCallback* getOverlapFilterCallback()
164 return m_overlapFilterCallback;
167 void setOverlapFilterCallback(btOverlapFilterCallback * callback)
169 m_overlapFilterCallback = callback;
172 int getNumOverlappingPairs() const
174 return m_overlappingPairArray.size();
178 btBroadphasePair* internalAddPair(btBroadphaseProxy * proxy0, btBroadphaseProxy * proxy1);
182 SIMD_FORCE_INLINE bool equalsPair(const btBroadphasePair& pair, int proxyId1, int proxyId2)
184 return pair.m_pProxy0->getUid() == proxyId1 && pair.m_pProxy1->getUid() == proxyId2;
188 // Thomas Wang's hash, see: http://www.concentric.net/~Ttwang/tech/inthash.htm
189 // This assumes proxyId1 and proxyId2 are 16-bit.
190 SIMD_FORCE_INLINE int getHash(int proxyId1, int proxyId2)
192 int key = (proxyId2 << 16) | proxyId1;
193 key = ~key + (key << 15);
194 key = key ^ (key >> 12);
195 key = key + (key << 2);
196 key = key ^ (key >> 4);
198 key = key ^ (key >> 16);
203 SIMD_FORCE_INLINE unsigned int getHash(unsigned int proxyId1, unsigned int proxyId2)
205 unsigned int key = proxyId1 | (proxyId2 << 16);
206 // Thomas Wang's hash
217 SIMD_FORCE_INLINE btBroadphasePair* internalFindPair(btBroadphaseProxy * proxy0, btBroadphaseProxy * proxy1, int hash)
219 int proxyId1 = proxy0->getUid();
220 int proxyId2 = proxy1->getUid();
221 #if 0 // wrong, 'equalsPair' use unsorted uids, copy-past devil striked again. Nat.
222 if (proxyId1 > proxyId2)
223 btSwap(proxyId1, proxyId2);
226 int index = m_hashTable[hash];
228 while (index != BT_NULL_PAIR && equalsPair(m_overlappingPairArray[index], proxyId1, proxyId2) == false)
230 index = m_next[index];
233 if (index == BT_NULL_PAIR)
238 btAssert(index < m_overlappingPairArray.size());
240 return &m_overlappingPairArray[index];
243 virtual bool hasDeferredRemoval()
248 virtual void setInternalGhostPairCallback(btOverlappingPairCallback * ghostPairCallback)
250 m_ghostPairCallback = ghostPairCallback;
253 virtual void sortOverlappingPairs(btDispatcher * dispatcher);
256 ///btSortedOverlappingPairCache maintains the objects with overlapping AABB
257 ///Typically managed by the Broadphase, Axis3Sweep or btSimpleBroadphase
258 class btSortedOverlappingPairCache : public btOverlappingPairCache
261 //avoid brute-force finding all the time
262 btBroadphasePairArray m_overlappingPairArray;
264 //during the dispatch, check that user doesn't destroy/create proxy
265 bool m_blockedForChanges;
267 ///by default, do the removal during the pair traversal
268 bool m_hasDeferredRemoval;
270 //if set, use the callback instead of the built in filter in needBroadphaseCollision
271 btOverlapFilterCallback* m_overlapFilterCallback;
273 btOverlappingPairCallback* m_ghostPairCallback;
276 btSortedOverlappingPairCache();
277 virtual ~btSortedOverlappingPairCache();
279 virtual void processAllOverlappingPairs(btOverlapCallback*, btDispatcher* dispatcher);
281 void* removeOverlappingPair(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1, btDispatcher* dispatcher);
283 void cleanOverlappingPair(btBroadphasePair& pair, btDispatcher* dispatcher);
285 btBroadphasePair* addOverlappingPair(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1);
287 btBroadphasePair* findPair(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1);
289 void cleanProxyFromPairs(btBroadphaseProxy* proxy, btDispatcher* dispatcher);
291 void removeOverlappingPairsContainingProxy(btBroadphaseProxy* proxy, btDispatcher* dispatcher);
293 inline bool needsBroadphaseCollision(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1) const
295 if (m_overlapFilterCallback)
296 return m_overlapFilterCallback->needBroadphaseCollision(proxy0, proxy1);
298 bool collides = (proxy0->m_collisionFilterGroup & proxy1->m_collisionFilterMask) != 0;
299 collides = collides && (proxy1->m_collisionFilterGroup & proxy0->m_collisionFilterMask);
304 btBroadphasePairArray& getOverlappingPairArray()
306 return m_overlappingPairArray;
309 const btBroadphasePairArray& getOverlappingPairArray() const
311 return m_overlappingPairArray;
314 btBroadphasePair* getOverlappingPairArrayPtr()
316 return &m_overlappingPairArray[0];
319 const btBroadphasePair* getOverlappingPairArrayPtr() const
321 return &m_overlappingPairArray[0];
324 int getNumOverlappingPairs() const
326 return m_overlappingPairArray.size();
329 btOverlapFilterCallback* getOverlapFilterCallback()
331 return m_overlapFilterCallback;
334 void setOverlapFilterCallback(btOverlapFilterCallback* callback)
336 m_overlapFilterCallback = callback;
339 virtual bool hasDeferredRemoval()
341 return m_hasDeferredRemoval;
344 virtual void setInternalGhostPairCallback(btOverlappingPairCallback* ghostPairCallback)
346 m_ghostPairCallback = ghostPairCallback;
349 virtual void sortOverlappingPairs(btDispatcher* dispatcher);
352 ///btNullPairCache skips add/removal of overlapping pairs. Userful for benchmarking and unit testing.
353 class btNullPairCache : public btOverlappingPairCache
355 btBroadphasePairArray m_overlappingPairArray;
358 virtual btBroadphasePair* getOverlappingPairArrayPtr()
360 return &m_overlappingPairArray[0];
362 const btBroadphasePair* getOverlappingPairArrayPtr() const
364 return &m_overlappingPairArray[0];
366 btBroadphasePairArray& getOverlappingPairArray()
368 return m_overlappingPairArray;
371 virtual void cleanOverlappingPair(btBroadphasePair& /*pair*/, btDispatcher* /*dispatcher*/)
375 virtual int getNumOverlappingPairs() const
380 virtual void cleanProxyFromPairs(btBroadphaseProxy* /*proxy*/, btDispatcher* /*dispatcher*/)
384 bool needsBroadphaseCollision(btBroadphaseProxy*, btBroadphaseProxy*) const
388 btOverlapFilterCallback* getOverlapFilterCallback()
392 virtual void setOverlapFilterCallback(btOverlapFilterCallback* /*callback*/)
396 virtual void processAllOverlappingPairs(btOverlapCallback*, btDispatcher* /*dispatcher*/)
400 virtual btBroadphasePair* findPair(btBroadphaseProxy* /*proxy0*/, btBroadphaseProxy* /*proxy1*/)
405 virtual bool hasDeferredRemoval()
410 virtual void setInternalGhostPairCallback(btOverlappingPairCallback* /* ghostPairCallback */)
414 virtual btBroadphasePair* addOverlappingPair(btBroadphaseProxy* /*proxy0*/, btBroadphaseProxy* /*proxy1*/)
419 virtual void* removeOverlappingPair(btBroadphaseProxy* /*proxy0*/, btBroadphaseProxy* /*proxy1*/, btDispatcher* /*dispatcher*/)
424 virtual void removeOverlappingPairsContainingProxy(btBroadphaseProxy* /*proxy0*/, btDispatcher* /*dispatcher*/)
428 virtual void sortOverlappingPairs(btDispatcher* dispatcher)
434 #endif //BT_OVERLAPPING_PAIR_CACHE_H