b3OverlappingPairCache.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2013 Erwin Coumans http://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 B3_OVERLAPPING_PAIR_CACHE_H
  14. #define B3_OVERLAPPING_PAIR_CACHE_H
  15. #include "Bullet3Common/shared/b3Int2.h"
  16. #include "Bullet3Common/b3AlignedObjectArray.h"
  17. class b3Dispatcher;
  18. #include "b3OverlappingPair.h"
  19. typedef b3AlignedObjectArray<b3BroadphasePair> b3BroadphasePairArray;
  20. struct b3OverlapCallback
  21. {
  22. virtual ~b3OverlapCallback()
  23. {
  24. }
  25. //return true for deletion of the pair
  26. virtual bool processOverlap(b3BroadphasePair& pair) = 0;
  27. };
  28. struct b3OverlapFilterCallback
  29. {
  30. virtual ~b3OverlapFilterCallback()
  31. {
  32. }
  33. // return true when pairs need collision
  34. virtual bool needBroadphaseCollision(int proxy0, int proxy1) const = 0;
  35. };
  36. extern int b3g_removePairs;
  37. extern int b3g_addedPairs;
  38. extern int b3g_findPairs;
  39. const int B3_NULL_PAIR = 0xffffffff;
  40. ///The b3OverlappingPairCache provides an interface for overlapping pair management (add, remove, storage), used by the b3BroadphaseInterface broadphases.
  41. ///The b3HashedOverlappingPairCache and b3SortedOverlappingPairCache classes are two implementations.
  42. class b3OverlappingPairCache
  43. {
  44. public:
  45. virtual ~b3OverlappingPairCache() {} // this is needed so we can get to the derived class destructor
  46. virtual b3BroadphasePair* getOverlappingPairArrayPtr() = 0;
  47. virtual const b3BroadphasePair* getOverlappingPairArrayPtr() const = 0;
  48. virtual b3BroadphasePairArray& getOverlappingPairArray() = 0;
  49. virtual void cleanOverlappingPair(b3BroadphasePair& pair, b3Dispatcher* dispatcher) = 0;
  50. virtual int getNumOverlappingPairs() const = 0;
  51. virtual void cleanProxyFromPairs(int proxy, b3Dispatcher* dispatcher) = 0;
  52. virtual void setOverlapFilterCallback(b3OverlapFilterCallback* callback) = 0;
  53. virtual void processAllOverlappingPairs(b3OverlapCallback*, b3Dispatcher* dispatcher) = 0;
  54. virtual b3BroadphasePair* findPair(int proxy0, int proxy1) = 0;
  55. virtual bool hasDeferredRemoval() = 0;
  56. //virtual void setInternalGhostPairCallback(b3OverlappingPairCallback* ghostPairCallback)=0;
  57. virtual b3BroadphasePair* addOverlappingPair(int proxy0, int proxy1) = 0;
  58. virtual void* removeOverlappingPair(int proxy0, int proxy1, b3Dispatcher* dispatcher) = 0;
  59. virtual void removeOverlappingPairsContainingProxy(int /*proxy0*/, b3Dispatcher* /*dispatcher*/) = 0;
  60. virtual void sortOverlappingPairs(b3Dispatcher* dispatcher) = 0;
  61. };
  62. /// Hash-space based Pair Cache, thanks to Erin Catto, Box2D, http://www.box2d.org, and Pierre Terdiman, Codercorner, http://codercorner.com
  63. class b3HashedOverlappingPairCache : public b3OverlappingPairCache
  64. {
  65. b3BroadphasePairArray m_overlappingPairArray;
  66. b3OverlapFilterCallback* m_overlapFilterCallback;
  67. // bool m_blockedForChanges;
  68. public:
  69. b3HashedOverlappingPairCache();
  70. virtual ~b3HashedOverlappingPairCache();
  71. virtual void removeOverlappingPairsContainingProxy(int proxy, b3Dispatcher* dispatcher);
  72. virtual void* removeOverlappingPair(int proxy0, int proxy1, b3Dispatcher* dispatcher);
  73. B3_FORCE_INLINE bool needsBroadphaseCollision(int proxy0, int proxy1) const
  74. {
  75. if (m_overlapFilterCallback)
  76. return m_overlapFilterCallback->needBroadphaseCollision(proxy0, proxy1);
  77. bool collides = true; //(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 b3BroadphasePair* addOverlappingPair(int proxy0, int proxy1)
  84. {
  85. b3g_addedPairs++;
  86. if (!needsBroadphaseCollision(proxy0, proxy1))
  87. return 0;
  88. return internalAddPair(proxy0, proxy1);
  89. }
  90. void cleanProxyFromPairs(int proxy, b3Dispatcher* dispatcher);
  91. virtual void processAllOverlappingPairs(b3OverlapCallback*, b3Dispatcher* dispatcher);
  92. virtual b3BroadphasePair* getOverlappingPairArrayPtr()
  93. {
  94. return &m_overlappingPairArray[0];
  95. }
  96. const b3BroadphasePair* getOverlappingPairArrayPtr() const
  97. {
  98. return &m_overlappingPairArray[0];
  99. }
  100. b3BroadphasePairArray& getOverlappingPairArray()
  101. {
  102. return m_overlappingPairArray;
  103. }
  104. const b3BroadphasePairArray& getOverlappingPairArray() const
  105. {
  106. return m_overlappingPairArray;
  107. }
  108. void cleanOverlappingPair(b3BroadphasePair& pair, b3Dispatcher* dispatcher);
  109. b3BroadphasePair* findPair(int proxy0, int proxy1);
  110. int GetCount() const { return m_overlappingPairArray.size(); }
  111. // b3BroadphasePair* GetPairs() { return m_pairs; }
  112. b3OverlapFilterCallback* getOverlapFilterCallback()
  113. {
  114. return m_overlapFilterCallback;
  115. }
  116. void setOverlapFilterCallback(b3OverlapFilterCallback* callback)
  117. {
  118. m_overlapFilterCallback = callback;
  119. }
  120. int getNumOverlappingPairs() const
  121. {
  122. return m_overlappingPairArray.size();
  123. }
  124. private:
  125. b3BroadphasePair* internalAddPair(int proxy0, int proxy1);
  126. void growTables();
  127. B3_FORCE_INLINE bool equalsPair(const b3BroadphasePair& pair, int proxyId1, int proxyId2)
  128. {
  129. return pair.x == proxyId1 && pair.y == 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. B3_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. B3_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. B3_FORCE_INLINE b3BroadphasePair* internalFindPair(int proxy0, int proxy1, int hash)
  159. {
  160. int proxyId1 = proxy0;
  161. int proxyId2 = proxy1;
  162. #if 0 // wrong, 'equalsPair' use unsorted uids, copy-past devil striked again. Nat.
  163. if (proxyId1 > proxyId2)
  164. b3Swap(proxyId1, proxyId2);
  165. #endif
  166. int index = m_hashTable[hash];
  167. while (index != B3_NULL_PAIR && equalsPair(m_overlappingPairArray[index], proxyId1, proxyId2) == false)
  168. {
  169. index = m_next[index];
  170. }
  171. if (index == B3_NULL_PAIR)
  172. {
  173. return NULL;
  174. }
  175. b3Assert(index < m_overlappingPairArray.size());
  176. return &m_overlappingPairArray[index];
  177. }
  178. virtual bool hasDeferredRemoval()
  179. {
  180. return false;
  181. }
  182. /* virtual void setInternalGhostPairCallback(b3OverlappingPairCallback* ghostPairCallback)
  183. {
  184. m_ghostPairCallback = ghostPairCallback;
  185. }
  186. */
  187. virtual void sortOverlappingPairs(b3Dispatcher* dispatcher);
  188. protected:
  189. b3AlignedObjectArray<int> m_hashTable;
  190. b3AlignedObjectArray<int> m_next;
  191. // b3OverlappingPairCallback* m_ghostPairCallback;
  192. };
  193. ///b3SortedOverlappingPairCache maintains the objects with overlapping AABB
  194. ///Typically managed by the Broadphase, Axis3Sweep or b3SimpleBroadphase
  195. class b3SortedOverlappingPairCache : public b3OverlappingPairCache
  196. {
  197. protected:
  198. //avoid brute-force finding all the time
  199. b3BroadphasePairArray m_overlappingPairArray;
  200. //during the dispatch, check that user doesn't destroy/create proxy
  201. bool m_blockedForChanges;
  202. ///by default, do the removal during the pair traversal
  203. bool m_hasDeferredRemoval;
  204. //if set, use the callback instead of the built in filter in needBroadphaseCollision
  205. b3OverlapFilterCallback* m_overlapFilterCallback;
  206. // b3OverlappingPairCallback* m_ghostPairCallback;
  207. public:
  208. b3SortedOverlappingPairCache();
  209. virtual ~b3SortedOverlappingPairCache();
  210. virtual void processAllOverlappingPairs(b3OverlapCallback*, b3Dispatcher* dispatcher);
  211. void* removeOverlappingPair(int proxy0, int proxy1, b3Dispatcher* dispatcher);
  212. void cleanOverlappingPair(b3BroadphasePair& pair, b3Dispatcher* dispatcher);
  213. b3BroadphasePair* addOverlappingPair(int proxy0, int proxy1);
  214. b3BroadphasePair* findPair(int proxy0, int proxy1);
  215. void cleanProxyFromPairs(int proxy, b3Dispatcher* dispatcher);
  216. virtual void removeOverlappingPairsContainingProxy(int proxy, b3Dispatcher* dispatcher);
  217. inline bool needsBroadphaseCollision(int proxy0, int proxy1) const
  218. {
  219. if (m_overlapFilterCallback)
  220. return m_overlapFilterCallback->needBroadphaseCollision(proxy0, proxy1);
  221. bool collides = true; //(proxy0->m_collisionFilterGroup & proxy1->m_collisionFilterMask) != 0;
  222. //collides = collides && (proxy1->m_collisionFilterGroup & proxy0->m_collisionFilterMask);
  223. return collides;
  224. }
  225. b3BroadphasePairArray& getOverlappingPairArray()
  226. {
  227. return m_overlappingPairArray;
  228. }
  229. const b3BroadphasePairArray& getOverlappingPairArray() const
  230. {
  231. return m_overlappingPairArray;
  232. }
  233. b3BroadphasePair* getOverlappingPairArrayPtr()
  234. {
  235. return &m_overlappingPairArray[0];
  236. }
  237. const b3BroadphasePair* getOverlappingPairArrayPtr() const
  238. {
  239. return &m_overlappingPairArray[0];
  240. }
  241. int getNumOverlappingPairs() const
  242. {
  243. return m_overlappingPairArray.size();
  244. }
  245. b3OverlapFilterCallback* getOverlapFilterCallback()
  246. {
  247. return m_overlapFilterCallback;
  248. }
  249. void setOverlapFilterCallback(b3OverlapFilterCallback* callback)
  250. {
  251. m_overlapFilterCallback = callback;
  252. }
  253. virtual bool hasDeferredRemoval()
  254. {
  255. return m_hasDeferredRemoval;
  256. }
  257. /* virtual void setInternalGhostPairCallback(b3OverlappingPairCallback* ghostPairCallback)
  258. {
  259. m_ghostPairCallback = ghostPairCallback;
  260. }
  261. */
  262. virtual void sortOverlappingPairs(b3Dispatcher* dispatcher);
  263. };
  264. ///b3NullPairCache skips add/removal of overlapping pairs. Userful for benchmarking and unit testing.
  265. class b3NullPairCache : public b3OverlappingPairCache
  266. {
  267. b3BroadphasePairArray m_overlappingPairArray;
  268. public:
  269. virtual b3BroadphasePair* getOverlappingPairArrayPtr()
  270. {
  271. return &m_overlappingPairArray[0];
  272. }
  273. const b3BroadphasePair* getOverlappingPairArrayPtr() const
  274. {
  275. return &m_overlappingPairArray[0];
  276. }
  277. b3BroadphasePairArray& getOverlappingPairArray()
  278. {
  279. return m_overlappingPairArray;
  280. }
  281. virtual void cleanOverlappingPair(b3BroadphasePair& /*pair*/, b3Dispatcher* /*dispatcher*/)
  282. {
  283. }
  284. virtual int getNumOverlappingPairs() const
  285. {
  286. return 0;
  287. }
  288. virtual void cleanProxyFromPairs(int /*proxy*/, b3Dispatcher* /*dispatcher*/)
  289. {
  290. }
  291. virtual void setOverlapFilterCallback(b3OverlapFilterCallback* /*callback*/)
  292. {
  293. }
  294. virtual void processAllOverlappingPairs(b3OverlapCallback*, b3Dispatcher* /*dispatcher*/)
  295. {
  296. }
  297. virtual b3BroadphasePair* findPair(int /*proxy0*/, int /*proxy1*/)
  298. {
  299. return 0;
  300. }
  301. virtual bool hasDeferredRemoval()
  302. {
  303. return true;
  304. }
  305. // virtual void setInternalGhostPairCallback(b3OverlappingPairCallback* /* ghostPairCallback */)
  306. // {
  307. //
  308. // }
  309. virtual b3BroadphasePair* addOverlappingPair(int /*proxy0*/, int /*proxy1*/)
  310. {
  311. return 0;
  312. }
  313. virtual void* removeOverlappingPair(int /*proxy0*/, int /*proxy1*/, b3Dispatcher* /*dispatcher*/)
  314. {
  315. return 0;
  316. }
  317. virtual void removeOverlappingPairsContainingProxy(int /*proxy0*/, b3Dispatcher* /*dispatcher*/)
  318. {
  319. }
  320. virtual void sortOverlappingPairs(b3Dispatcher* dispatcher)
  321. {
  322. (void)dispatcher;
  323. }
  324. };
  325. #endif //B3_OVERLAPPING_PAIR_CACHE_H