b3OverlappingPairCache.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. #include "b3OverlappingPairCache.h"
  14. //#include "b3Dispatcher.h"
  15. //#include "b3CollisionAlgorithm.h"
  16. #include "Bullet3Geometry/b3AabbUtil.h"
  17. #include <stdio.h>
  18. int b3g_overlappingPairs = 0;
  19. int b3g_removePairs = 0;
  20. int b3g_addedPairs = 0;
  21. int b3g_findPairs = 0;
  22. b3HashedOverlappingPairCache::b3HashedOverlappingPairCache() : m_overlapFilterCallback(0)
  23. //, m_blockedForChanges(false)
  24. {
  25. int initialAllocatedSize = 2;
  26. m_overlappingPairArray.reserve(initialAllocatedSize);
  27. growTables();
  28. }
  29. b3HashedOverlappingPairCache::~b3HashedOverlappingPairCache()
  30. {
  31. }
  32. void b3HashedOverlappingPairCache::cleanOverlappingPair(b3BroadphasePair& pair, b3Dispatcher* dispatcher)
  33. {
  34. /* if (pair.m_algorithm)
  35. {
  36. {
  37. pair.m_algorithm->~b3CollisionAlgorithm();
  38. dispatcher->freeCollisionAlgorithm(pair.m_algorithm);
  39. pair.m_algorithm=0;
  40. }
  41. }
  42. */
  43. }
  44. void b3HashedOverlappingPairCache::cleanProxyFromPairs(int proxy, b3Dispatcher* dispatcher)
  45. {
  46. class CleanPairCallback : public b3OverlapCallback
  47. {
  48. int m_cleanProxy;
  49. b3OverlappingPairCache* m_pairCache;
  50. b3Dispatcher* m_dispatcher;
  51. public:
  52. CleanPairCallback(int cleanProxy, b3OverlappingPairCache* pairCache, b3Dispatcher* dispatcher)
  53. : m_cleanProxy(cleanProxy),
  54. m_pairCache(pairCache),
  55. m_dispatcher(dispatcher)
  56. {
  57. }
  58. virtual bool processOverlap(b3BroadphasePair& pair)
  59. {
  60. if ((pair.x == m_cleanProxy) ||
  61. (pair.y == m_cleanProxy))
  62. {
  63. m_pairCache->cleanOverlappingPair(pair, m_dispatcher);
  64. }
  65. return false;
  66. }
  67. };
  68. CleanPairCallback cleanPairs(proxy, this, dispatcher);
  69. processAllOverlappingPairs(&cleanPairs, dispatcher);
  70. }
  71. void b3HashedOverlappingPairCache::removeOverlappingPairsContainingProxy(int proxy, b3Dispatcher* dispatcher)
  72. {
  73. class RemovePairCallback : public b3OverlapCallback
  74. {
  75. int m_obsoleteProxy;
  76. public:
  77. RemovePairCallback(int obsoleteProxy)
  78. : m_obsoleteProxy(obsoleteProxy)
  79. {
  80. }
  81. virtual bool processOverlap(b3BroadphasePair& pair)
  82. {
  83. return ((pair.x == m_obsoleteProxy) ||
  84. (pair.y == m_obsoleteProxy));
  85. }
  86. };
  87. RemovePairCallback removeCallback(proxy);
  88. processAllOverlappingPairs(&removeCallback, dispatcher);
  89. }
  90. b3BroadphasePair* b3HashedOverlappingPairCache::findPair(int proxy0, int proxy1)
  91. {
  92. b3g_findPairs++;
  93. if (proxy0 > proxy1)
  94. b3Swap(proxy0, proxy1);
  95. int proxyId1 = proxy0;
  96. int proxyId2 = proxy1;
  97. /*if (proxyId1 > proxyId2)
  98. b3Swap(proxyId1, proxyId2);*/
  99. int hash = static_cast<int>(getHash(static_cast<unsigned int>(proxyId1), static_cast<unsigned int>(proxyId2)) & (m_overlappingPairArray.capacity() - 1));
  100. if (hash >= m_hashTable.size())
  101. {
  102. return NULL;
  103. }
  104. int index = m_hashTable[hash];
  105. while (index != B3_NULL_PAIR && equalsPair(m_overlappingPairArray[index], proxyId1, proxyId2) == false)
  106. {
  107. index = m_next[index];
  108. }
  109. if (index == B3_NULL_PAIR)
  110. {
  111. return NULL;
  112. }
  113. b3Assert(index < m_overlappingPairArray.size());
  114. return &m_overlappingPairArray[index];
  115. }
  116. //#include <stdio.h>
  117. void b3HashedOverlappingPairCache::growTables()
  118. {
  119. int newCapacity = m_overlappingPairArray.capacity();
  120. if (m_hashTable.size() < newCapacity)
  121. {
  122. //grow hashtable and next table
  123. int curHashtableSize = m_hashTable.size();
  124. m_hashTable.resize(newCapacity);
  125. m_next.resize(newCapacity);
  126. int i;
  127. for (i = 0; i < newCapacity; ++i)
  128. {
  129. m_hashTable[i] = B3_NULL_PAIR;
  130. }
  131. for (i = 0; i < newCapacity; ++i)
  132. {
  133. m_next[i] = B3_NULL_PAIR;
  134. }
  135. for (i = 0; i < curHashtableSize; i++)
  136. {
  137. const b3BroadphasePair& pair = m_overlappingPairArray[i];
  138. int proxyId1 = pair.x;
  139. int proxyId2 = pair.y;
  140. /*if (proxyId1 > proxyId2)
  141. b3Swap(proxyId1, proxyId2);*/
  142. int hashValue = static_cast<int>(getHash(static_cast<unsigned int>(proxyId1), static_cast<unsigned int>(proxyId2)) & (m_overlappingPairArray.capacity() - 1)); // New hash value with new mask
  143. m_next[i] = m_hashTable[hashValue];
  144. m_hashTable[hashValue] = i;
  145. }
  146. }
  147. }
  148. b3BroadphasePair* b3HashedOverlappingPairCache::internalAddPair(int proxy0, int proxy1)
  149. {
  150. if (proxy0 > proxy1)
  151. b3Swap(proxy0, proxy1);
  152. int proxyId1 = proxy0;
  153. int proxyId2 = proxy1;
  154. /*if (proxyId1 > proxyId2)
  155. b3Swap(proxyId1, proxyId2);*/
  156. int hash = static_cast<int>(getHash(static_cast<unsigned int>(proxyId1), static_cast<unsigned int>(proxyId2)) & (m_overlappingPairArray.capacity() - 1)); // New hash value with new mask
  157. b3BroadphasePair* pair = internalFindPair(proxy0, proxy1, hash);
  158. if (pair != NULL)
  159. {
  160. return pair;
  161. }
  162. /*for(int i=0;i<m_overlappingPairArray.size();++i)
  163. {
  164. if( (m_overlappingPairArray[i].m_pProxy0==proxy0)&&
  165. (m_overlappingPairArray[i].m_pProxy1==proxy1))
  166. {
  167. printf("Adding duplicated %u<>%u\r\n",proxyId1,proxyId2);
  168. internalFindPair(proxy0, proxy1, hash);
  169. }
  170. }*/
  171. int count = m_overlappingPairArray.size();
  172. int oldCapacity = m_overlappingPairArray.capacity();
  173. pair = &m_overlappingPairArray.expandNonInitializing();
  174. //this is where we add an actual pair, so also call the 'ghost'
  175. // if (m_ghostPairCallback)
  176. // m_ghostPairCallback->addOverlappingPair(proxy0,proxy1);
  177. int newCapacity = m_overlappingPairArray.capacity();
  178. if (oldCapacity < newCapacity)
  179. {
  180. growTables();
  181. //hash with new capacity
  182. hash = static_cast<int>(getHash(static_cast<unsigned int>(proxyId1), static_cast<unsigned int>(proxyId2)) & (m_overlappingPairArray.capacity() - 1));
  183. }
  184. *pair = b3MakeBroadphasePair(proxy0, proxy1);
  185. // pair->m_pProxy0 = proxy0;
  186. // pair->m_pProxy1 = proxy1;
  187. //pair->m_algorithm = 0;
  188. //pair->m_internalTmpValue = 0;
  189. m_next[count] = m_hashTable[hash];
  190. m_hashTable[hash] = count;
  191. return pair;
  192. }
  193. void* b3HashedOverlappingPairCache::removeOverlappingPair(int proxy0, int proxy1, b3Dispatcher* dispatcher)
  194. {
  195. b3g_removePairs++;
  196. if (proxy0 > proxy1)
  197. b3Swap(proxy0, proxy1);
  198. int proxyId1 = proxy0;
  199. int proxyId2 = proxy1;
  200. /*if (proxyId1 > proxyId2)
  201. b3Swap(proxyId1, proxyId2);*/
  202. int hash = static_cast<int>(getHash(static_cast<unsigned int>(proxyId1), static_cast<unsigned int>(proxyId2)) & (m_overlappingPairArray.capacity() - 1));
  203. b3BroadphasePair* pair = internalFindPair(proxy0, proxy1, hash);
  204. if (pair == NULL)
  205. {
  206. return 0;
  207. }
  208. cleanOverlappingPair(*pair, dispatcher);
  209. int pairIndex = int(pair - &m_overlappingPairArray[0]);
  210. b3Assert(pairIndex < m_overlappingPairArray.size());
  211. // Remove the pair from the hash table.
  212. int index = m_hashTable[hash];
  213. b3Assert(index != B3_NULL_PAIR);
  214. int previous = B3_NULL_PAIR;
  215. while (index != pairIndex)
  216. {
  217. previous = index;
  218. index = m_next[index];
  219. }
  220. if (previous != B3_NULL_PAIR)
  221. {
  222. b3Assert(m_next[previous] == pairIndex);
  223. m_next[previous] = m_next[pairIndex];
  224. }
  225. else
  226. {
  227. m_hashTable[hash] = m_next[pairIndex];
  228. }
  229. // We now move the last pair into spot of the
  230. // pair being removed. We need to fix the hash
  231. // table indices to support the move.
  232. int lastPairIndex = m_overlappingPairArray.size() - 1;
  233. //if (m_ghostPairCallback)
  234. // m_ghostPairCallback->removeOverlappingPair(proxy0, proxy1,dispatcher);
  235. // If the removed pair is the last pair, we are done.
  236. if (lastPairIndex == pairIndex)
  237. {
  238. m_overlappingPairArray.pop_back();
  239. return 0;
  240. }
  241. // Remove the last pair from the hash table.
  242. const b3BroadphasePair* last = &m_overlappingPairArray[lastPairIndex];
  243. /* missing swap here too, Nat. */
  244. int lastHash = static_cast<int>(getHash(static_cast<unsigned int>(last->x), static_cast<unsigned int>(last->y)) & (m_overlappingPairArray.capacity() - 1));
  245. index = m_hashTable[lastHash];
  246. b3Assert(index != B3_NULL_PAIR);
  247. previous = B3_NULL_PAIR;
  248. while (index != lastPairIndex)
  249. {
  250. previous = index;
  251. index = m_next[index];
  252. }
  253. if (previous != B3_NULL_PAIR)
  254. {
  255. b3Assert(m_next[previous] == lastPairIndex);
  256. m_next[previous] = m_next[lastPairIndex];
  257. }
  258. else
  259. {
  260. m_hashTable[lastHash] = m_next[lastPairIndex];
  261. }
  262. // Copy the last pair into the remove pair's spot.
  263. m_overlappingPairArray[pairIndex] = m_overlappingPairArray[lastPairIndex];
  264. // Insert the last pair into the hash table
  265. m_next[pairIndex] = m_hashTable[lastHash];
  266. m_hashTable[lastHash] = pairIndex;
  267. m_overlappingPairArray.pop_back();
  268. return 0;
  269. }
  270. //#include <stdio.h>
  271. void b3HashedOverlappingPairCache::processAllOverlappingPairs(b3OverlapCallback* callback, b3Dispatcher* dispatcher)
  272. {
  273. int i;
  274. // printf("m_overlappingPairArray.size()=%d\n",m_overlappingPairArray.size());
  275. for (i = 0; i < m_overlappingPairArray.size();)
  276. {
  277. b3BroadphasePair* pair = &m_overlappingPairArray[i];
  278. if (callback->processOverlap(*pair))
  279. {
  280. removeOverlappingPair(pair->x, pair->y, dispatcher);
  281. b3g_overlappingPairs--;
  282. }
  283. else
  284. {
  285. i++;
  286. }
  287. }
  288. }
  289. void b3HashedOverlappingPairCache::sortOverlappingPairs(b3Dispatcher* dispatcher)
  290. {
  291. ///need to keep hashmap in sync with pair address, so rebuild all
  292. b3BroadphasePairArray tmpPairs;
  293. int i;
  294. for (i = 0; i < m_overlappingPairArray.size(); i++)
  295. {
  296. tmpPairs.push_back(m_overlappingPairArray[i]);
  297. }
  298. for (i = 0; i < tmpPairs.size(); i++)
  299. {
  300. removeOverlappingPair(tmpPairs[i].x, tmpPairs[i].y, dispatcher);
  301. }
  302. for (i = 0; i < m_next.size(); i++)
  303. {
  304. m_next[i] = B3_NULL_PAIR;
  305. }
  306. tmpPairs.quickSort(b3BroadphasePairSortPredicate());
  307. for (i = 0; i < tmpPairs.size(); i++)
  308. {
  309. addOverlappingPair(tmpPairs[i].x, tmpPairs[i].y);
  310. }
  311. }
  312. void* b3SortedOverlappingPairCache::removeOverlappingPair(int proxy0, int proxy1, b3Dispatcher* dispatcher)
  313. {
  314. if (!hasDeferredRemoval())
  315. {
  316. b3BroadphasePair findPair = b3MakeBroadphasePair(proxy0, proxy1);
  317. int findIndex = m_overlappingPairArray.findLinearSearch(findPair);
  318. if (findIndex < m_overlappingPairArray.size())
  319. {
  320. b3g_overlappingPairs--;
  321. b3BroadphasePair& pair = m_overlappingPairArray[findIndex];
  322. cleanOverlappingPair(pair, dispatcher);
  323. //if (m_ghostPairCallback)
  324. // m_ghostPairCallback->removeOverlappingPair(proxy0, proxy1,dispatcher);
  325. m_overlappingPairArray.swap(findIndex, m_overlappingPairArray.capacity() - 1);
  326. m_overlappingPairArray.pop_back();
  327. return 0;
  328. }
  329. }
  330. return 0;
  331. }
  332. b3BroadphasePair* b3SortedOverlappingPairCache::addOverlappingPair(int proxy0, int proxy1)
  333. {
  334. //don't add overlap with own
  335. b3Assert(proxy0 != proxy1);
  336. if (!needsBroadphaseCollision(proxy0, proxy1))
  337. return 0;
  338. b3BroadphasePair* pair = &m_overlappingPairArray.expandNonInitializing();
  339. *pair = b3MakeBroadphasePair(proxy0, proxy1);
  340. b3g_overlappingPairs++;
  341. b3g_addedPairs++;
  342. // if (m_ghostPairCallback)
  343. // m_ghostPairCallback->addOverlappingPair(proxy0, proxy1);
  344. return pair;
  345. }
  346. ///this findPair becomes really slow. Either sort the list to speedup the query, or
  347. ///use a different solution. It is mainly used for Removing overlapping pairs. Removal could be delayed.
  348. ///we could keep a linked list in each proxy, and store pair in one of the proxies (with lowest memory address)
  349. ///Also we can use a 2D bitmap, which can be useful for a future GPU implementation
  350. b3BroadphasePair* b3SortedOverlappingPairCache::findPair(int proxy0, int proxy1)
  351. {
  352. if (!needsBroadphaseCollision(proxy0, proxy1))
  353. return 0;
  354. b3BroadphasePair tmpPair = b3MakeBroadphasePair(proxy0, proxy1);
  355. int findIndex = m_overlappingPairArray.findLinearSearch(tmpPair);
  356. if (findIndex < m_overlappingPairArray.size())
  357. {
  358. //b3Assert(it != m_overlappingPairSet.end());
  359. b3BroadphasePair* pair = &m_overlappingPairArray[findIndex];
  360. return pair;
  361. }
  362. return 0;
  363. }
  364. //#include <stdio.h>
  365. void b3SortedOverlappingPairCache::processAllOverlappingPairs(b3OverlapCallback* callback, b3Dispatcher* dispatcher)
  366. {
  367. int i;
  368. for (i = 0; i < m_overlappingPairArray.size();)
  369. {
  370. b3BroadphasePair* pair = &m_overlappingPairArray[i];
  371. if (callback->processOverlap(*pair))
  372. {
  373. cleanOverlappingPair(*pair, dispatcher);
  374. pair->x = -1;
  375. pair->y = -1;
  376. m_overlappingPairArray.swap(i, m_overlappingPairArray.size() - 1);
  377. m_overlappingPairArray.pop_back();
  378. b3g_overlappingPairs--;
  379. }
  380. else
  381. {
  382. i++;
  383. }
  384. }
  385. }
  386. b3SortedOverlappingPairCache::b3SortedOverlappingPairCache() : m_blockedForChanges(false),
  387. m_hasDeferredRemoval(true),
  388. m_overlapFilterCallback(0)
  389. {
  390. int initialAllocatedSize = 2;
  391. m_overlappingPairArray.reserve(initialAllocatedSize);
  392. }
  393. b3SortedOverlappingPairCache::~b3SortedOverlappingPairCache()
  394. {
  395. }
  396. void b3SortedOverlappingPairCache::cleanOverlappingPair(b3BroadphasePair& pair, b3Dispatcher* dispatcher)
  397. {
  398. /* if (pair.m_algorithm)
  399. {
  400. {
  401. pair.m_algorithm->~b3CollisionAlgorithm();
  402. dispatcher->freeCollisionAlgorithm(pair.m_algorithm);
  403. pair.m_algorithm=0;
  404. b3g_removePairs--;
  405. }
  406. }
  407. */
  408. }
  409. void b3SortedOverlappingPairCache::cleanProxyFromPairs(int proxy, b3Dispatcher* dispatcher)
  410. {
  411. class CleanPairCallback : public b3OverlapCallback
  412. {
  413. int m_cleanProxy;
  414. b3OverlappingPairCache* m_pairCache;
  415. b3Dispatcher* m_dispatcher;
  416. public:
  417. CleanPairCallback(int cleanProxy, b3OverlappingPairCache* pairCache, b3Dispatcher* dispatcher)
  418. : m_cleanProxy(cleanProxy),
  419. m_pairCache(pairCache),
  420. m_dispatcher(dispatcher)
  421. {
  422. }
  423. virtual bool processOverlap(b3BroadphasePair& pair)
  424. {
  425. if ((pair.x == m_cleanProxy) ||
  426. (pair.y == m_cleanProxy))
  427. {
  428. m_pairCache->cleanOverlappingPair(pair, m_dispatcher);
  429. }
  430. return false;
  431. }
  432. };
  433. CleanPairCallback cleanPairs(proxy, this, dispatcher);
  434. processAllOverlappingPairs(&cleanPairs, dispatcher);
  435. }
  436. void b3SortedOverlappingPairCache::removeOverlappingPairsContainingProxy(int proxy, b3Dispatcher* dispatcher)
  437. {
  438. class RemovePairCallback : public b3OverlapCallback
  439. {
  440. int m_obsoleteProxy;
  441. public:
  442. RemovePairCallback(int obsoleteProxy)
  443. : m_obsoleteProxy(obsoleteProxy)
  444. {
  445. }
  446. virtual bool processOverlap(b3BroadphasePair& pair)
  447. {
  448. return ((pair.x == m_obsoleteProxy) ||
  449. (pair.y == m_obsoleteProxy));
  450. }
  451. };
  452. RemovePairCallback removeCallback(proxy);
  453. processAllOverlappingPairs(&removeCallback, dispatcher);
  454. }
  455. void b3SortedOverlappingPairCache::sortOverlappingPairs(b3Dispatcher* dispatcher)
  456. {
  457. //should already be sorted
  458. }