btHashedSimplePairCache.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #include "btHashedSimplePairCache.h"
  14. #include <stdio.h>
  15. #ifdef BT_DEBUG_COLLISION_PAIRS
  16. int gOverlappingSimplePairs = 0;
  17. int gRemoveSimplePairs = 0;
  18. int gAddedSimplePairs = 0;
  19. int gFindSimplePairs = 0;
  20. #endif //BT_DEBUG_COLLISION_PAIRS
  21. btHashedSimplePairCache::btHashedSimplePairCache()
  22. {
  23. int initialAllocatedSize = 2;
  24. m_overlappingPairArray.reserve(initialAllocatedSize);
  25. growTables();
  26. }
  27. btHashedSimplePairCache::~btHashedSimplePairCache()
  28. {
  29. }
  30. void btHashedSimplePairCache::removeAllPairs()
  31. {
  32. m_overlappingPairArray.clear();
  33. m_hashTable.clear();
  34. m_next.clear();
  35. int initialAllocatedSize = 2;
  36. m_overlappingPairArray.reserve(initialAllocatedSize);
  37. growTables();
  38. }
  39. btSimplePair* btHashedSimplePairCache::findPair(int indexA, int indexB)
  40. {
  41. #ifdef BT_DEBUG_COLLISION_PAIRS
  42. gFindSimplePairs++;
  43. #endif
  44. /*if (indexA > indexB)
  45. btSwap(indexA, indexB);*/
  46. int hash = static_cast<int>(getHash(static_cast<unsigned int>(indexA), static_cast<unsigned int>(indexB)) & (m_overlappingPairArray.capacity() - 1));
  47. if (hash >= m_hashTable.size())
  48. {
  49. return NULL;
  50. }
  51. int index = m_hashTable[hash];
  52. while (index != BT_SIMPLE_NULL_PAIR && equalsPair(m_overlappingPairArray[index], indexA, indexB) == false)
  53. {
  54. index = m_next[index];
  55. }
  56. if (index == BT_SIMPLE_NULL_PAIR)
  57. {
  58. return NULL;
  59. }
  60. btAssert(index < m_overlappingPairArray.size());
  61. return &m_overlappingPairArray[index];
  62. }
  63. //#include <stdio.h>
  64. void btHashedSimplePairCache::growTables()
  65. {
  66. int newCapacity = m_overlappingPairArray.capacity();
  67. if (m_hashTable.size() < newCapacity)
  68. {
  69. //grow hashtable and next table
  70. int curHashtableSize = m_hashTable.size();
  71. m_hashTable.resize(newCapacity);
  72. m_next.resize(newCapacity);
  73. int i;
  74. for (i = 0; i < newCapacity; ++i)
  75. {
  76. m_hashTable[i] = BT_SIMPLE_NULL_PAIR;
  77. }
  78. for (i = 0; i < newCapacity; ++i)
  79. {
  80. m_next[i] = BT_SIMPLE_NULL_PAIR;
  81. }
  82. for (i = 0; i < curHashtableSize; i++)
  83. {
  84. const btSimplePair& pair = m_overlappingPairArray[i];
  85. int indexA = pair.m_indexA;
  86. int indexB = pair.m_indexB;
  87. int hashValue = static_cast<int>(getHash(static_cast<unsigned int>(indexA), static_cast<unsigned int>(indexB)) & (m_overlappingPairArray.capacity() - 1)); // New hash value with new mask
  88. m_next[i] = m_hashTable[hashValue];
  89. m_hashTable[hashValue] = i;
  90. }
  91. }
  92. }
  93. btSimplePair* btHashedSimplePairCache::internalAddPair(int indexA, int indexB)
  94. {
  95. int hash = static_cast<int>(getHash(static_cast<unsigned int>(indexA), static_cast<unsigned int>(indexB)) & (m_overlappingPairArray.capacity() - 1)); // New hash value with new mask
  96. btSimplePair* pair = internalFindPair(indexA, indexB, hash);
  97. if (pair != NULL)
  98. {
  99. return pair;
  100. }
  101. int count = m_overlappingPairArray.size();
  102. int oldCapacity = m_overlappingPairArray.capacity();
  103. void* mem = &m_overlappingPairArray.expandNonInitializing();
  104. int newCapacity = m_overlappingPairArray.capacity();
  105. if (oldCapacity < newCapacity)
  106. {
  107. growTables();
  108. //hash with new capacity
  109. hash = static_cast<int>(getHash(static_cast<unsigned int>(indexA), static_cast<unsigned int>(indexB)) & (m_overlappingPairArray.capacity() - 1));
  110. }
  111. pair = new (mem) btSimplePair(indexA, indexB);
  112. pair->m_userPointer = 0;
  113. m_next[count] = m_hashTable[hash];
  114. m_hashTable[hash] = count;
  115. return pair;
  116. }
  117. void* btHashedSimplePairCache::removeOverlappingPair(int indexA, int indexB)
  118. {
  119. #ifdef BT_DEBUG_COLLISION_PAIRS
  120. gRemoveSimplePairs++;
  121. #endif
  122. /*if (indexA > indexB)
  123. btSwap(indexA, indexB);*/
  124. int hash = static_cast<int>(getHash(static_cast<unsigned int>(indexA), static_cast<unsigned int>(indexB)) & (m_overlappingPairArray.capacity() - 1));
  125. btSimplePair* pair = internalFindPair(indexA, indexB, hash);
  126. if (pair == NULL)
  127. {
  128. return 0;
  129. }
  130. void* userData = pair->m_userPointer;
  131. int pairIndex = int(pair - &m_overlappingPairArray[0]);
  132. btAssert(pairIndex < m_overlappingPairArray.size());
  133. // Remove the pair from the hash table.
  134. int index = m_hashTable[hash];
  135. btAssert(index != BT_SIMPLE_NULL_PAIR);
  136. int previous = BT_SIMPLE_NULL_PAIR;
  137. while (index != pairIndex)
  138. {
  139. previous = index;
  140. index = m_next[index];
  141. }
  142. if (previous != BT_SIMPLE_NULL_PAIR)
  143. {
  144. btAssert(m_next[previous] == pairIndex);
  145. m_next[previous] = m_next[pairIndex];
  146. }
  147. else
  148. {
  149. m_hashTable[hash] = m_next[pairIndex];
  150. }
  151. // We now move the last pair into spot of the
  152. // pair being removed. We need to fix the hash
  153. // table indices to support the move.
  154. int lastPairIndex = m_overlappingPairArray.size() - 1;
  155. // If the removed pair is the last pair, we are done.
  156. if (lastPairIndex == pairIndex)
  157. {
  158. m_overlappingPairArray.pop_back();
  159. return userData;
  160. }
  161. // Remove the last pair from the hash table.
  162. const btSimplePair* last = &m_overlappingPairArray[lastPairIndex];
  163. /* missing swap here too, Nat. */
  164. int lastHash = static_cast<int>(getHash(static_cast<unsigned int>(last->m_indexA), static_cast<unsigned int>(last->m_indexB)) & (m_overlappingPairArray.capacity() - 1));
  165. index = m_hashTable[lastHash];
  166. btAssert(index != BT_SIMPLE_NULL_PAIR);
  167. previous = BT_SIMPLE_NULL_PAIR;
  168. while (index != lastPairIndex)
  169. {
  170. previous = index;
  171. index = m_next[index];
  172. }
  173. if (previous != BT_SIMPLE_NULL_PAIR)
  174. {
  175. btAssert(m_next[previous] == lastPairIndex);
  176. m_next[previous] = m_next[lastPairIndex];
  177. }
  178. else
  179. {
  180. m_hashTable[lastHash] = m_next[lastPairIndex];
  181. }
  182. // Copy the last pair into the remove pair's spot.
  183. m_overlappingPairArray[pairIndex] = m_overlappingPairArray[lastPairIndex];
  184. // Insert the last pair into the hash table
  185. m_next[pairIndex] = m_hashTable[lastHash];
  186. m_hashTable[lastHash] = pairIndex;
  187. m_overlappingPairArray.pop_back();
  188. return userData;
  189. }
  190. //#include <stdio.h>