btHashedSimplePairCache.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_HASHED_SIMPLE_PAIR_CACHE_H
  14. #define BT_HASHED_SIMPLE_PAIR_CACHE_H
  15. #include "bullet/LinearMath/btAlignedObjectArray.h"
  16. const int BT_SIMPLE_NULL_PAIR=0xffffffff;
  17. struct btSimplePair
  18. {
  19. btSimplePair(int indexA,int indexB)
  20. :m_indexA(indexA),
  21. m_indexB(indexB),
  22. m_userPointer(0)
  23. {
  24. }
  25. int m_indexA;
  26. int m_indexB;
  27. union
  28. {
  29. void* m_userPointer;
  30. int m_userValue;
  31. };
  32. };
  33. typedef btAlignedObjectArray<btSimplePair> btSimplePairArray;
  34. extern int gOverlappingSimplePairs;
  35. extern int gRemoveSimplePairs;
  36. extern int gAddedSimplePairs;
  37. extern int gFindSimplePairs;
  38. class btHashedSimplePairCache
  39. {
  40. btSimplePairArray m_overlappingPairArray;
  41. bool m_blockedForChanges;
  42. protected:
  43. btAlignedObjectArray<int> m_hashTable;
  44. btAlignedObjectArray<int> m_next;
  45. public:
  46. btHashedSimplePairCache();
  47. virtual ~btHashedSimplePairCache();
  48. void removeAllPairs();
  49. virtual void* removeOverlappingPair(int indexA,int indexB);
  50. // Add a pair and return the new pair. If the pair already exists,
  51. // no new pair is created and the old one is returned.
  52. virtual btSimplePair* addOverlappingPair(int indexA,int indexB)
  53. {
  54. gAddedSimplePairs++;
  55. return internalAddPair(indexA,indexB);
  56. }
  57. virtual btSimplePair* getOverlappingPairArrayPtr()
  58. {
  59. return &m_overlappingPairArray[0];
  60. }
  61. const btSimplePair* getOverlappingPairArrayPtr() const
  62. {
  63. return &m_overlappingPairArray[0];
  64. }
  65. btSimplePairArray& getOverlappingPairArray()
  66. {
  67. return m_overlappingPairArray;
  68. }
  69. const btSimplePairArray& getOverlappingPairArray() const
  70. {
  71. return m_overlappingPairArray;
  72. }
  73. btSimplePair* findPair(int indexA,int indexB);
  74. int GetCount() const { return m_overlappingPairArray.size(); }
  75. int getNumOverlappingPairs() const
  76. {
  77. return m_overlappingPairArray.size();
  78. }
  79. private:
  80. btSimplePair* internalAddPair(int indexA, int indexB);
  81. void growTables();
  82. SIMD_FORCE_INLINE bool equalsPair(const btSimplePair& pair, int indexA, int indexB)
  83. {
  84. return pair.m_indexA == indexA && pair.m_indexB == indexB;
  85. }
  86. SIMD_FORCE_INLINE unsigned int getHash(unsigned int indexA, unsigned int indexB)
  87. {
  88. int key = static_cast<int>(((unsigned int)indexA) | (((unsigned int)indexB) <<16));
  89. // Thomas Wang's hash
  90. key += ~(key << 15);
  91. key ^= (key >> 10);
  92. key += (key << 3);
  93. key ^= (key >> 6);
  94. key += ~(key << 11);
  95. key ^= (key >> 16);
  96. return static_cast<unsigned int>(key);
  97. }
  98. SIMD_FORCE_INLINE btSimplePair* internalFindPair(int proxyIdA , int proxyIdB, int hash)
  99. {
  100. int index = m_hashTable[hash];
  101. while( index != BT_SIMPLE_NULL_PAIR && equalsPair(m_overlappingPairArray[index], proxyIdA, proxyIdB) == false)
  102. {
  103. index = m_next[index];
  104. }
  105. if ( index == BT_SIMPLE_NULL_PAIR )
  106. {
  107. return NULL;
  108. }
  109. btAssert(index < m_overlappingPairArray.size());
  110. return &m_overlappingPairArray[index];
  111. }
  112. };
  113. #endif //BT_HASHED_SIMPLE_PAIR_CACHE_H