btOverlappingPairCache.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef BT_OVERLAPPING_PAIR_CACHE_H
  14. #define BT_OVERLAPPING_PAIR_CACHE_H
  15. #include "btBroadphaseInterface.h"
  16. #include "btBroadphaseProxy.h"
  17. #include "btOverlappingPairCallback.h"
  18. #include "bullet/LinearMath/btAlignedObjectArray.h"
  19. class btDispatcher;
  20. typedef btAlignedObjectArray<btBroadphasePair> btBroadphasePairArray;
  21. struct btOverlapCallback
  22. {
  23. virtual ~btOverlapCallback()
  24. {}
  25. //return true for deletion of the pair
  26. virtual bool processOverlap(btBroadphasePair& pair) = 0;
  27. };
  28. struct btOverlapFilterCallback
  29. {
  30. virtual ~btOverlapFilterCallback()
  31. {}
  32. // return true when pairs need collision
  33. virtual bool needBroadphaseCollision(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1) const = 0;
  34. };
  35. extern int gRemovePairs;
  36. extern int gAddedPairs;
  37. extern int gFindPairs;
  38. const int BT_NULL_PAIR=0xffffffff;
  39. ///The btOverlappingPairCache provides an interface for overlapping pair management (add, remove, storage), used by the btBroadphaseInterface broadphases.
  40. ///The btHashedOverlappingPairCache and btSortedOverlappingPairCache classes are two implementations.
  41. class btOverlappingPairCache : public btOverlappingPairCallback
  42. {
  43. public:
  44. virtual ~btOverlappingPairCache() {} // this is needed so we can get to the derived class destructor
  45. virtual btBroadphasePair* getOverlappingPairArrayPtr() = 0;
  46. virtual const btBroadphasePair* getOverlappingPairArrayPtr() const = 0;
  47. virtual btBroadphasePairArray& getOverlappingPairArray() = 0;
  48. virtual void cleanOverlappingPair(btBroadphasePair& pair,btDispatcher* dispatcher) = 0;
  49. virtual int getNumOverlappingPairs() const = 0;
  50. virtual void cleanProxyFromPairs(btBroadphaseProxy* proxy,btDispatcher* dispatcher) = 0;
  51. virtual void setOverlapFilterCallback(btOverlapFilterCallback* callback) = 0;
  52. virtual void processAllOverlappingPairs(btOverlapCallback*,btDispatcher* dispatcher) = 0;
  53. virtual btBroadphasePair* findPair(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1) = 0;
  54. virtual bool hasDeferredRemoval() = 0;
  55. virtual void setInternalGhostPairCallback(btOverlappingPairCallback* ghostPairCallback)=0;
  56. virtual void sortOverlappingPairs(btDispatcher* dispatcher) = 0;
  57. };
  58. /// Hash-space based Pair Cache, thanks to Erin Catto, Box2D, http://www.box2d.org, and Pierre Terdiman, Codercorner, http://codercorner.com
  59. class btHashedOverlappingPairCache : public btOverlappingPairCache
  60. {
  61. btBroadphasePairArray m_overlappingPairArray;
  62. btOverlapFilterCallback* m_overlapFilterCallback;
  63. bool m_blockedForChanges;
  64. protected:
  65. btAlignedObjectArray<int> m_hashTable;
  66. btAlignedObjectArray<int> m_next;
  67. btOverlappingPairCallback* m_ghostPairCallback;
  68. public:
  69. btHashedOverlappingPairCache();
  70. virtual ~btHashedOverlappingPairCache();
  71. void removeOverlappingPairsContainingProxy(btBroadphaseProxy* proxy,btDispatcher* dispatcher);
  72. virtual void* removeOverlappingPair(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1,btDispatcher* dispatcher);
  73. SIMD_FORCE_INLINE bool needsBroadphaseCollision(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1) const
  74. {
  75. if (m_overlapFilterCallback)
  76. return m_overlapFilterCallback->needBroadphaseCollision(proxy0,proxy1);
  77. bool collides = (proxy0->m_collisionFilterGroup & proxy1->m_collisionFilterMask) != 0;
  78. collides = collides && (proxy1->m_collisionFilterGroup & proxy0->m_collisionFilterMask);
  79. return collides;
  80. }
  81. // Add a pair and return the new pair. If the pair already exists,
  82. // no new pair is created and the old one is returned.
  83. virtual btBroadphasePair* addOverlappingPair(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1)
  84. {
  85. gAddedPairs++;
  86. if (!needsBroadphaseCollision(proxy0,proxy1))
  87. return 0;
  88. return internalAddPair(proxy0,proxy1);
  89. }
  90. void cleanProxyFromPairs(btBroadphaseProxy* proxy,btDispatcher* dispatcher);
  91. virtual void processAllOverlappingPairs(btOverlapCallback*,btDispatcher* dispatcher);
  92. virtual btBroadphasePair* getOverlappingPairArrayPtr()
  93. {
  94. return &m_overlappingPairArray[0];
  95. }
  96. const btBroadphasePair* getOverlappingPairArrayPtr() const
  97. {
  98. return &m_overlappingPairArray[0];
  99. }
  100. btBroadphasePairArray& getOverlappingPairArray()
  101. {
  102. return m_overlappingPairArray;
  103. }
  104. const btBroadphasePairArray& getOverlappingPairArray() const
  105. {
  106. return m_overlappingPairArray;
  107. }
  108. void cleanOverlappingPair(btBroadphasePair& pair,btDispatcher* dispatcher);
  109. btBroadphasePair* findPair(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1);
  110. int GetCount() const { return m_overlappingPairArray.size(); }
  111. // btBroadphasePair* GetPairs() { return m_pairs; }
  112. btOverlapFilterCallback* getOverlapFilterCallback()
  113. {
  114. return m_overlapFilterCallback;
  115. }
  116. void setOverlapFilterCallback(btOverlapFilterCallback* callback)
  117. {
  118. m_overlapFilterCallback = callback;
  119. }
  120. int getNumOverlappingPairs() const
  121. {
  122. return m_overlappingPairArray.size();
  123. }
  124. private:
  125. btBroadphasePair* internalAddPair(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1);
  126. void growTables();
  127. SIMD_FORCE_INLINE bool equalsPair(const btBroadphasePair& pair, int proxyId1, int proxyId2)
  128. {
  129. return pair.m_pProxy0->getUid() == proxyId1 && pair.m_pProxy1->getUid() == proxyId2;
  130. }
  131. /*
  132. // Thomas Wang's hash, see: http://www.concentric.net/~Ttwang/tech/inthash.htm
  133. // This assumes proxyId1 and proxyId2 are 16-bit.
  134. SIMD_FORCE_INLINE int getHash(int proxyId1, int proxyId2)
  135. {
  136. int key = (proxyId2 << 16) | proxyId1;
  137. key = ~key + (key << 15);
  138. key = key ^ (key >> 12);
  139. key = key + (key << 2);
  140. key = key ^ (key >> 4);
  141. key = key * 2057;
  142. key = key ^ (key >> 16);
  143. return key;
  144. }
  145. */
  146. SIMD_FORCE_INLINE unsigned int getHash(unsigned int proxyId1, unsigned int proxyId2)
  147. {
  148. int key = static_cast<int>(((unsigned int)proxyId1) | (((unsigned int)proxyId2) <<16));
  149. // Thomas Wang's hash
  150. key += ~(key << 15);
  151. key ^= (key >> 10);
  152. key += (key << 3);
  153. key ^= (key >> 6);
  154. key += ~(key << 11);
  155. key ^= (key >> 16);
  156. return static_cast<unsigned int>(key);
  157. }
  158. SIMD_FORCE_INLINE btBroadphasePair* internalFindPair(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1, int hash)
  159. {
  160. int proxyId1 = proxy0->getUid();
  161. int proxyId2 = proxy1->getUid();
  162. #if 0 // wrong, 'equalsPair' use unsorted uids, copy-past devil striked again. Nat.
  163. if (proxyId1 > proxyId2)
  164. btSwap(proxyId1, proxyId2);
  165. #endif
  166. int index = m_hashTable[hash];
  167. while( index != BT_NULL_PAIR && equalsPair(m_overlappingPairArray[index], proxyId1, proxyId2) == false)
  168. {
  169. index = m_next[index];
  170. }
  171. if ( index == BT_NULL_PAIR )
  172. {
  173. return NULL;
  174. }
  175. btAssert(index < m_overlappingPairArray.size());
  176. return &m_overlappingPairArray[index];
  177. }
  178. virtual bool hasDeferredRemoval()
  179. {
  180. return false;
  181. }
  182. virtual void setInternalGhostPairCallback(btOverlappingPairCallback* ghostPairCallback)
  183. {
  184. m_ghostPairCallback = ghostPairCallback;
  185. }
  186. virtual void sortOverlappingPairs(btDispatcher* dispatcher);
  187. };
  188. ///btSortedOverlappingPairCache maintains the objects with overlapping AABB
  189. ///Typically managed by the Broadphase, Axis3Sweep or btSimpleBroadphase
  190. class btSortedOverlappingPairCache : public btOverlappingPairCache
  191. {
  192. protected:
  193. //avoid brute-force finding all the time
  194. btBroadphasePairArray m_overlappingPairArray;
  195. //during the dispatch, check that user doesn't destroy/create proxy
  196. bool m_blockedForChanges;
  197. ///by default, do the removal during the pair traversal
  198. bool m_hasDeferredRemoval;
  199. //if set, use the callback instead of the built in filter in needBroadphaseCollision
  200. btOverlapFilterCallback* m_overlapFilterCallback;
  201. btOverlappingPairCallback* m_ghostPairCallback;
  202. public:
  203. btSortedOverlappingPairCache();
  204. virtual ~btSortedOverlappingPairCache();
  205. virtual void processAllOverlappingPairs(btOverlapCallback*,btDispatcher* dispatcher);
  206. void* removeOverlappingPair(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1,btDispatcher* dispatcher);
  207. void cleanOverlappingPair(btBroadphasePair& pair,btDispatcher* dispatcher);
  208. btBroadphasePair* addOverlappingPair(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1);
  209. btBroadphasePair* findPair(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1);
  210. void cleanProxyFromPairs(btBroadphaseProxy* proxy,btDispatcher* dispatcher);
  211. void removeOverlappingPairsContainingProxy(btBroadphaseProxy* proxy,btDispatcher* dispatcher);
  212. inline bool needsBroadphaseCollision(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1) const
  213. {
  214. if (m_overlapFilterCallback)
  215. return m_overlapFilterCallback->needBroadphaseCollision(proxy0,proxy1);
  216. bool collides = (proxy0->m_collisionFilterGroup & proxy1->m_collisionFilterMask) != 0;
  217. collides = collides && (proxy1->m_collisionFilterGroup & proxy0->m_collisionFilterMask);
  218. return collides;
  219. }
  220. btBroadphasePairArray& getOverlappingPairArray()
  221. {
  222. return m_overlappingPairArray;
  223. }
  224. const btBroadphasePairArray& getOverlappingPairArray() const
  225. {
  226. return m_overlappingPairArray;
  227. }
  228. btBroadphasePair* getOverlappingPairArrayPtr()
  229. {
  230. return &m_overlappingPairArray[0];
  231. }
  232. const btBroadphasePair* getOverlappingPairArrayPtr() const
  233. {
  234. return &m_overlappingPairArray[0];
  235. }
  236. int getNumOverlappingPairs() const
  237. {
  238. return m_overlappingPairArray.size();
  239. }
  240. btOverlapFilterCallback* getOverlapFilterCallback()
  241. {
  242. return m_overlapFilterCallback;
  243. }
  244. void setOverlapFilterCallback(btOverlapFilterCallback* callback)
  245. {
  246. m_overlapFilterCallback = callback;
  247. }
  248. virtual bool hasDeferredRemoval()
  249. {
  250. return m_hasDeferredRemoval;
  251. }
  252. virtual void setInternalGhostPairCallback(btOverlappingPairCallback* ghostPairCallback)
  253. {
  254. m_ghostPairCallback = ghostPairCallback;
  255. }
  256. virtual void sortOverlappingPairs(btDispatcher* dispatcher);
  257. };
  258. ///btNullPairCache skips add/removal of overlapping pairs. Userful for benchmarking and unit testing.
  259. class btNullPairCache : public btOverlappingPairCache
  260. {
  261. btBroadphasePairArray m_overlappingPairArray;
  262. public:
  263. virtual btBroadphasePair* getOverlappingPairArrayPtr()
  264. {
  265. return &m_overlappingPairArray[0];
  266. }
  267. const btBroadphasePair* getOverlappingPairArrayPtr() const
  268. {
  269. return &m_overlappingPairArray[0];
  270. }
  271. btBroadphasePairArray& getOverlappingPairArray()
  272. {
  273. return m_overlappingPairArray;
  274. }
  275. virtual void cleanOverlappingPair(btBroadphasePair& /*pair*/,btDispatcher* /*dispatcher*/)
  276. {
  277. }
  278. virtual int getNumOverlappingPairs() const
  279. {
  280. return 0;
  281. }
  282. virtual void cleanProxyFromPairs(btBroadphaseProxy* /*proxy*/,btDispatcher* /*dispatcher*/)
  283. {
  284. }
  285. virtual void setOverlapFilterCallback(btOverlapFilterCallback* /*callback*/)
  286. {
  287. }
  288. virtual void processAllOverlappingPairs(btOverlapCallback*,btDispatcher* /*dispatcher*/)
  289. {
  290. }
  291. virtual btBroadphasePair* findPair(btBroadphaseProxy* /*proxy0*/, btBroadphaseProxy* /*proxy1*/)
  292. {
  293. return 0;
  294. }
  295. virtual bool hasDeferredRemoval()
  296. {
  297. return true;
  298. }
  299. virtual void setInternalGhostPairCallback(btOverlappingPairCallback* /* ghostPairCallback */)
  300. {
  301. }
  302. virtual btBroadphasePair* addOverlappingPair(btBroadphaseProxy* /*proxy0*/,btBroadphaseProxy* /*proxy1*/)
  303. {
  304. return 0;
  305. }
  306. virtual void* removeOverlappingPair(btBroadphaseProxy* /*proxy0*/,btBroadphaseProxy* /*proxy1*/,btDispatcher* /*dispatcher*/)
  307. {
  308. return 0;
  309. }
  310. virtual void removeOverlappingPairsContainingProxy(btBroadphaseProxy* /*proxy0*/,btDispatcher* /*dispatcher*/)
  311. {
  312. }
  313. virtual void sortOverlappingPairs(btDispatcher* dispatcher)
  314. {
  315. (void) dispatcher;
  316. }
  317. };
  318. #endif //BT_OVERLAPPING_PAIR_CACHE_H