btOverlappingPairCache.h 13 KB

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