2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2013 Erwin Coumans http://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 B3_OVERLAPPING_PAIR_CACHE_H
17 #define B3_OVERLAPPING_PAIR_CACHE_H
19 #include "Bullet3Common/shared/b3Int2.h"
20 #include "Bullet3Common/b3AlignedObjectArray.h"
23 #include "b3OverlappingPair.h"
25 typedef b3AlignedObjectArray<b3BroadphasePair> b3BroadphasePairArray;
27 struct b3OverlapCallback
29 virtual ~b3OverlapCallback()
32 //return true for deletion of the pair
33 virtual bool processOverlap(b3BroadphasePair& pair) = 0;
36 struct b3OverlapFilterCallback
38 virtual ~b3OverlapFilterCallback()
41 // return true when pairs need collision
42 virtual bool needBroadphaseCollision(int proxy0, int proxy1) const = 0;
45 extern int b3g_removePairs;
46 extern int b3g_addedPairs;
47 extern int b3g_findPairs;
49 const int B3_NULL_PAIR = 0xffffffff;
51 ///The b3OverlappingPairCache provides an interface for overlapping pair management (add, remove, storage), used by the b3BroadphaseInterface broadphases.
52 ///The b3HashedOverlappingPairCache and b3SortedOverlappingPairCache classes are two implementations.
53 class b3OverlappingPairCache
56 virtual ~b3OverlappingPairCache() {} // this is needed so we can get to the derived class destructor
58 virtual b3BroadphasePair* getOverlappingPairArrayPtr() = 0;
60 virtual const b3BroadphasePair* getOverlappingPairArrayPtr() const = 0;
62 virtual b3BroadphasePairArray& getOverlappingPairArray() = 0;
64 virtual void cleanOverlappingPair(b3BroadphasePair& pair, b3Dispatcher* dispatcher) = 0;
66 virtual int getNumOverlappingPairs() const = 0;
68 virtual void cleanProxyFromPairs(int proxy, b3Dispatcher* dispatcher) = 0;
70 virtual void setOverlapFilterCallback(b3OverlapFilterCallback* callback) = 0;
72 virtual void processAllOverlappingPairs(b3OverlapCallback*, b3Dispatcher* dispatcher) = 0;
74 virtual b3BroadphasePair* findPair(int proxy0, int proxy1) = 0;
76 virtual bool hasDeferredRemoval() = 0;
78 //virtual void setInternalGhostPairCallback(b3OverlappingPairCallback* ghostPairCallback)=0;
80 virtual b3BroadphasePair* addOverlappingPair(int proxy0, int proxy1) = 0;
81 virtual void* removeOverlappingPair(int proxy0, int proxy1, b3Dispatcher* dispatcher) = 0;
82 virtual void removeOverlappingPairsContainingProxy(int /*proxy0*/, b3Dispatcher* /*dispatcher*/) = 0;
84 virtual void sortOverlappingPairs(b3Dispatcher* dispatcher) = 0;
87 /// Hash-space based Pair Cache, thanks to Erin Catto, Box2D, http://www.box2d.org, and Pierre Terdiman, Codercorner, http://codercorner.com
88 class b3HashedOverlappingPairCache : public b3OverlappingPairCache
90 b3BroadphasePairArray m_overlappingPairArray;
91 b3OverlapFilterCallback* m_overlapFilterCallback;
92 // bool m_blockedForChanges;
95 b3HashedOverlappingPairCache();
96 virtual ~b3HashedOverlappingPairCache();
98 virtual void removeOverlappingPairsContainingProxy(int proxy, b3Dispatcher* dispatcher);
100 virtual void* removeOverlappingPair(int proxy0, int proxy1, b3Dispatcher* dispatcher);
102 B3_FORCE_INLINE bool needsBroadphaseCollision(int proxy0, int proxy1) const
104 if (m_overlapFilterCallback)
105 return m_overlapFilterCallback->needBroadphaseCollision(proxy0, proxy1);
107 bool collides = true; //(proxy0->m_collisionFilterGroup & proxy1->m_collisionFilterMask) != 0;
108 //collides = collides && (proxy1->m_collisionFilterGroup & proxy0->m_collisionFilterMask);
113 // Add a pair and return the new pair. If the pair already exists,
114 // no new pair is created and the old one is returned.
115 virtual b3BroadphasePair* addOverlappingPair(int proxy0, int proxy1)
119 if (!needsBroadphaseCollision(proxy0, proxy1))
122 return internalAddPair(proxy0, proxy1);
125 void cleanProxyFromPairs(int proxy, b3Dispatcher* dispatcher);
127 virtual void processAllOverlappingPairs(b3OverlapCallback*, b3Dispatcher* dispatcher);
129 virtual b3BroadphasePair* getOverlappingPairArrayPtr()
131 return &m_overlappingPairArray[0];
134 const b3BroadphasePair* getOverlappingPairArrayPtr() const
136 return &m_overlappingPairArray[0];
139 b3BroadphasePairArray& getOverlappingPairArray()
141 return m_overlappingPairArray;
144 const b3BroadphasePairArray& getOverlappingPairArray() const
146 return m_overlappingPairArray;
149 void cleanOverlappingPair(b3BroadphasePair& pair, b3Dispatcher* dispatcher);
151 b3BroadphasePair* findPair(int proxy0, int proxy1);
153 int GetCount() const { return m_overlappingPairArray.size(); }
154 // b3BroadphasePair* GetPairs() { return m_pairs; }
156 b3OverlapFilterCallback* getOverlapFilterCallback()
158 return m_overlapFilterCallback;
161 void setOverlapFilterCallback(b3OverlapFilterCallback* callback)
163 m_overlapFilterCallback = callback;
166 int getNumOverlappingPairs() const
168 return m_overlappingPairArray.size();
172 b3BroadphasePair* internalAddPair(int proxy0, int proxy1);
176 B3_FORCE_INLINE bool equalsPair(const b3BroadphasePair& pair, int proxyId1, int proxyId2)
178 return pair.x == proxyId1 && pair.y == proxyId2;
182 // Thomas Wang's hash, see: http://www.concentric.net/~Ttwang/tech/inthash.htm
183 // This assumes proxyId1 and proxyId2 are 16-bit.
184 B3_FORCE_INLINE int getHash(int proxyId1, int proxyId2)
186 int key = (proxyId2 << 16) | proxyId1;
187 key = ~key + (key << 15);
188 key = key ^ (key >> 12);
189 key = key + (key << 2);
190 key = key ^ (key >> 4);
192 key = key ^ (key >> 16);
197 B3_FORCE_INLINE unsigned int getHash(unsigned int proxyId1, unsigned int proxyId2)
199 int key = static_cast<int>(((unsigned int)proxyId1) | (((unsigned int)proxyId2) << 16));
200 // Thomas Wang's hash
208 return static_cast<unsigned int>(key);
211 B3_FORCE_INLINE b3BroadphasePair* internalFindPair(int proxy0, int proxy1, int hash)
213 int proxyId1 = proxy0;
214 int proxyId2 = proxy1;
215 #if 0 // wrong, 'equalsPair' use unsorted uids, copy-past devil striked again. Nat.
216 if (proxyId1 > proxyId2)
217 b3Swap(proxyId1, proxyId2);
220 int index = m_hashTable[hash];
222 while (index != B3_NULL_PAIR && equalsPair(m_overlappingPairArray[index], proxyId1, proxyId2) == false)
224 index = m_next[index];
227 if (index == B3_NULL_PAIR)
232 b3Assert(index < m_overlappingPairArray.size());
234 return &m_overlappingPairArray[index];
237 virtual bool hasDeferredRemoval()
242 /* virtual void setInternalGhostPairCallback(b3OverlappingPairCallback* ghostPairCallback)
244 m_ghostPairCallback = ghostPairCallback;
248 virtual void sortOverlappingPairs(b3Dispatcher* dispatcher);
251 b3AlignedObjectArray<int> m_hashTable;
252 b3AlignedObjectArray<int> m_next;
253 // b3OverlappingPairCallback* m_ghostPairCallback;
256 ///b3SortedOverlappingPairCache maintains the objects with overlapping AABB
257 ///Typically managed by the Broadphase, Axis3Sweep or b3SimpleBroadphase
258 class b3SortedOverlappingPairCache : public b3OverlappingPairCache
261 //avoid brute-force finding all the time
262 b3BroadphasePairArray 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 b3OverlapFilterCallback* m_overlapFilterCallback;
273 // b3OverlappingPairCallback* m_ghostPairCallback;
276 b3SortedOverlappingPairCache();
277 virtual ~b3SortedOverlappingPairCache();
279 virtual void processAllOverlappingPairs(b3OverlapCallback*, b3Dispatcher* dispatcher);
281 void* removeOverlappingPair(int proxy0, int proxy1, b3Dispatcher* dispatcher);
283 void cleanOverlappingPair(b3BroadphasePair& pair, b3Dispatcher* dispatcher);
285 b3BroadphasePair* addOverlappingPair(int proxy0, int proxy1);
287 b3BroadphasePair* findPair(int proxy0, int proxy1);
289 void cleanProxyFromPairs(int proxy, b3Dispatcher* dispatcher);
291 virtual void removeOverlappingPairsContainingProxy(int proxy, b3Dispatcher* dispatcher);
293 inline bool needsBroadphaseCollision(int proxy0, int proxy1) const
295 if (m_overlapFilterCallback)
296 return m_overlapFilterCallback->needBroadphaseCollision(proxy0, proxy1);
298 bool collides = true; //(proxy0->m_collisionFilterGroup & proxy1->m_collisionFilterMask) != 0;
299 //collides = collides && (proxy1->m_collisionFilterGroup & proxy0->m_collisionFilterMask);
304 b3BroadphasePairArray& getOverlappingPairArray()
306 return m_overlappingPairArray;
309 const b3BroadphasePairArray& getOverlappingPairArray() const
311 return m_overlappingPairArray;
314 b3BroadphasePair* getOverlappingPairArrayPtr()
316 return &m_overlappingPairArray[0];
319 const b3BroadphasePair* getOverlappingPairArrayPtr() const
321 return &m_overlappingPairArray[0];
324 int getNumOverlappingPairs() const
326 return m_overlappingPairArray.size();
329 b3OverlapFilterCallback* getOverlapFilterCallback()
331 return m_overlapFilterCallback;
334 void setOverlapFilterCallback(b3OverlapFilterCallback* callback)
336 m_overlapFilterCallback = callback;
339 virtual bool hasDeferredRemoval()
341 return m_hasDeferredRemoval;
344 /* virtual void setInternalGhostPairCallback(b3OverlappingPairCallback* ghostPairCallback)
346 m_ghostPairCallback = ghostPairCallback;
349 virtual void sortOverlappingPairs(b3Dispatcher* dispatcher);
352 ///b3NullPairCache skips add/removal of overlapping pairs. Userful for benchmarking and unit testing.
353 class b3NullPairCache : public b3OverlappingPairCache
355 b3BroadphasePairArray m_overlappingPairArray;
358 virtual b3BroadphasePair* getOverlappingPairArrayPtr()
360 return &m_overlappingPairArray[0];
362 const b3BroadphasePair* getOverlappingPairArrayPtr() const
364 return &m_overlappingPairArray[0];
366 b3BroadphasePairArray& getOverlappingPairArray()
368 return m_overlappingPairArray;
371 virtual void cleanOverlappingPair(b3BroadphasePair& /*pair*/, b3Dispatcher* /*dispatcher*/)
375 virtual int getNumOverlappingPairs() const
380 virtual void cleanProxyFromPairs(int /*proxy*/, b3Dispatcher* /*dispatcher*/)
384 virtual void setOverlapFilterCallback(b3OverlapFilterCallback* /*callback*/)
388 virtual void processAllOverlappingPairs(b3OverlapCallback*, b3Dispatcher* /*dispatcher*/)
392 virtual b3BroadphasePair* findPair(int /*proxy0*/, int /*proxy1*/)
397 virtual bool hasDeferredRemoval()
402 // virtual void setInternalGhostPairCallback(b3OverlappingPairCallback* /* ghostPairCallback */)
407 virtual b3BroadphasePair* addOverlappingPair(int /*proxy0*/, int /*proxy1*/)
412 virtual void* removeOverlappingPair(int /*proxy0*/, int /*proxy1*/, b3Dispatcher* /*dispatcher*/)
417 virtual void removeOverlappingPairsContainingProxy(int /*proxy0*/, b3Dispatcher* /*dispatcher*/)
421 virtual void sortOverlappingPairs(b3Dispatcher* dispatcher)
427 #endif //B3_OVERLAPPING_PAIR_CACHE_H