1 /* 2 Bullet Continuous Collision Detection and Physics Library 3 Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org 4 5 This software is provided 'as-is', without any express or implied warranty. 6 In no event will the authors be held liable for any damages arising from the use of this software. 7 Permission is granted to anyone to use this software for any purpose, 8 including commercial applications, and to alter it and redistribute it freely, 9 subject to the following restrictions: 10 11 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. 12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 13 3. This notice may not be removed or altered from any source distribution. 14 */ 15 16 #ifndef BT_HASHED_SIMPLE_PAIR_CACHE_H 17 #define BT_HASHED_SIMPLE_PAIR_CACHE_H 18 19 #include "LinearMath/btAlignedObjectArray.h" 20 21 const int BT_SIMPLE_NULL_PAIR = 0xffffffff; 22 23 struct btSimplePair 24 { btSimplePairbtSimplePair25 btSimplePair(int indexA, int indexB) 26 : m_indexA(indexA), 27 m_indexB(indexB), 28 m_userPointer(0) 29 { 30 } 31 32 int m_indexA; 33 int m_indexB; 34 union { 35 void* m_userPointer; 36 int m_userValue; 37 }; 38 }; 39 40 typedef btAlignedObjectArray<btSimplePair> btSimplePairArray; 41 42 #ifdef BT_DEBUG_COLLISION_PAIRS 43 extern int gOverlappingSimplePairs; 44 extern int gRemoveSimplePairs; 45 extern int gAddedSimplePairs; 46 extern int gFindSimplePairs; 47 #endif //BT_DEBUG_COLLISION_PAIRS 48 49 class btHashedSimplePairCache 50 { 51 btSimplePairArray m_overlappingPairArray; 52 53 protected: 54 btAlignedObjectArray<int> m_hashTable; 55 btAlignedObjectArray<int> m_next; 56 57 public: 58 btHashedSimplePairCache(); 59 virtual ~btHashedSimplePairCache(); 60 61 void removeAllPairs(); 62 63 virtual void* removeOverlappingPair(int indexA, int indexB); 64 65 // Add a pair and return the new pair. If the pair already exists, 66 // no new pair is created and the old one is returned. addOverlappingPair(int indexA,int indexB)67 virtual btSimplePair* addOverlappingPair(int indexA, int indexB) 68 { 69 #ifdef BT_DEBUG_COLLISION_PAIRS 70 gAddedSimplePairs++; 71 #endif 72 73 return internalAddPair(indexA, indexB); 74 } 75 getOverlappingPairArrayPtr()76 virtual btSimplePair* getOverlappingPairArrayPtr() 77 { 78 return &m_overlappingPairArray[0]; 79 } 80 getOverlappingPairArrayPtr()81 const btSimplePair* getOverlappingPairArrayPtr() const 82 { 83 return &m_overlappingPairArray[0]; 84 } 85 getOverlappingPairArray()86 btSimplePairArray& getOverlappingPairArray() 87 { 88 return m_overlappingPairArray; 89 } 90 getOverlappingPairArray()91 const btSimplePairArray& getOverlappingPairArray() const 92 { 93 return m_overlappingPairArray; 94 } 95 96 btSimplePair* findPair(int indexA, int indexB); 97 GetCount()98 int GetCount() const { return m_overlappingPairArray.size(); } 99 getNumOverlappingPairs()100 int getNumOverlappingPairs() const 101 { 102 return m_overlappingPairArray.size(); 103 } 104 105 private: 106 btSimplePair* internalAddPair(int indexA, int indexB); 107 108 void growTables(); 109 equalsPair(const btSimplePair & pair,int indexA,int indexB)110 SIMD_FORCE_INLINE bool equalsPair(const btSimplePair& pair, int indexA, int indexB) 111 { 112 return pair.m_indexA == indexA && pair.m_indexB == indexB; 113 } 114 getHash(unsigned int indexA,unsigned int indexB)115 SIMD_FORCE_INLINE unsigned int getHash(unsigned int indexA, unsigned int indexB) 116 { 117 unsigned int key = indexA | (indexB << 16); 118 // Thomas Wang's hash 119 120 key += ~(key << 15); 121 key ^= (key >> 10); 122 key += (key << 3); 123 key ^= (key >> 6); 124 key += ~(key << 11); 125 key ^= (key >> 16); 126 return key; 127 } 128 internalFindPair(int proxyIdA,int proxyIdB,int hash)129 SIMD_FORCE_INLINE btSimplePair* internalFindPair(int proxyIdA, int proxyIdB, int hash) 130 { 131 int index = m_hashTable[hash]; 132 133 while (index != BT_SIMPLE_NULL_PAIR && equalsPair(m_overlappingPairArray[index], proxyIdA, proxyIdB) == false) 134 { 135 index = m_next[index]; 136 } 137 138 if (index == BT_SIMPLE_NULL_PAIR) 139 { 140 return NULL; 141 } 142 143 btAssert(index < m_overlappingPairArray.size()); 144 145 return &m_overlappingPairArray[index]; 146 } 147 }; 148 149 #endif //BT_HASHED_SIMPLE_PAIR_CACHE_H 150