Bullet Collision Detection & Physics Library: btHashedSimplePairCache.h Source File

Bullet Collision Detection & Physics Library

btHashedSimplePairCache.h
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
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 {
25 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.
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
76 virtual btSimplePair* getOverlappingPairArrayPtr()
77 {
78 return &m_overlappingPairArray[0];
79 }
80
81 const btSimplePair* getOverlappingPairArrayPtr() const
82 {
83 return &m_overlappingPairArray[0];
84 }
85
86 btSimplePairArray& getOverlappingPairArray()
87 {
88 return m_overlappingPairArray;
89 }
90
91 const btSimplePairArray& getOverlappingPairArray() const
92 {
93 return m_overlappingPairArray;
94 }
95
96 btSimplePair* findPair(int indexA, int indexB);
97
98 int GetCount() const { return m_overlappingPairArray.size(); }
99
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
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
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
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
btAlignedObjectArray.h
BT_SIMPLE_NULL_PAIR
const int BT_SIMPLE_NULL_PAIR
Definition: btHashedSimplePairCache.h:21
btSimplePairArray
btAlignedObjectArray< btSimplePair > btSimplePairArray
Definition: btHashedSimplePairCache.h:40
SIMD_FORCE_INLINE
#define SIMD_FORCE_INLINE
Definition: btScalar.h:98
btAssert
#define btAssert(x)
Definition: btScalar.h:153
btAlignedObjectArray< btSimplePair >
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:142
btHashedSimplePairCache
Definition: btHashedSimplePairCache.h:50
btHashedSimplePairCache::~btHashedSimplePairCache
virtual ~btHashedSimplePairCache()
Definition: btHashedSimplePairCache.cpp:34
btHashedSimplePairCache::getOverlappingPairArray
const btSimplePairArray & getOverlappingPairArray() const
Definition: btHashedSimplePairCache.h:91
btHashedSimplePairCache::getHash
unsigned int getHash(unsigned int indexA, unsigned int indexB)
Definition: btHashedSimplePairCache.h:115
btHashedSimplePairCache::GetCount
int GetCount() const
Definition: btHashedSimplePairCache.h:98
btHashedSimplePairCache::internalAddPair
btSimplePair * internalAddPair(int indexA, int indexB)
Definition: btHashedSimplePairCache.cpp:119
btHashedSimplePairCache::m_hashTable
btAlignedObjectArray< int > m_hashTable
Definition: btHashedSimplePairCache.h:54
btHashedSimplePairCache::internalFindPair
btSimplePair * internalFindPair(int proxyIdA, int proxyIdB, int hash)
Definition: btHashedSimplePairCache.h:129
btHashedSimplePairCache::getNumOverlappingPairs
int getNumOverlappingPairs() const
Definition: btHashedSimplePairCache.h:100
btHashedSimplePairCache::m_next
btAlignedObjectArray< int > m_next
Definition: btHashedSimplePairCache.h:55
btHashedSimplePairCache::m_overlappingPairArray
btSimplePairArray m_overlappingPairArray
Definition: btHashedSimplePairCache.h:51
btHashedSimplePairCache::growTables
void growTables()
Definition: btHashedSimplePairCache.cpp:83
btHashedSimplePairCache::btHashedSimplePairCache
btHashedSimplePairCache()
Definition: btHashedSimplePairCache.cpp:27
btHashedSimplePairCache::getOverlappingPairArrayPtr
virtual btSimplePair * getOverlappingPairArrayPtr()
Definition: btHashedSimplePairCache.h:76
btHashedSimplePairCache::getOverlappingPairArray
btSimplePairArray & getOverlappingPairArray()
Definition: btHashedSimplePairCache.h:86
btHashedSimplePairCache::findPair
btSimplePair * findPair(int indexA, int indexB)
Definition: btHashedSimplePairCache.cpp:49
btHashedSimplePairCache::addOverlappingPair
virtual btSimplePair * addOverlappingPair(int indexA, int indexB)
Definition: btHashedSimplePairCache.h:67
btHashedSimplePairCache::equalsPair
bool equalsPair(const btSimplePair &pair, int indexA, int indexB)
Definition: btHashedSimplePairCache.h:110
btHashedSimplePairCache::removeOverlappingPair
virtual void * removeOverlappingPair(int indexA, int indexB)
Definition: btHashedSimplePairCache.cpp:152
btHashedSimplePairCache::getOverlappingPairArrayPtr
const btSimplePair * getOverlappingPairArrayPtr() const
Definition: btHashedSimplePairCache.h:81
btHashedSimplePairCache::removeAllPairs
void removeAllPairs()
Definition: btHashedSimplePairCache.cpp:38
btSimplePair
Definition: btHashedSimplePairCache.h:24
btSimplePair::m_userPointer
void * m_userPointer
Definition: btHashedSimplePairCache.h:35
btSimplePair::m_indexA
int m_indexA
Definition: btHashedSimplePairCache.h:32
btSimplePair::btSimplePair
btSimplePair(int indexA, int indexB)
Definition: btHashedSimplePairCache.h:25
btSimplePair::m_indexB
int m_indexB
Definition: btHashedSimplePairCache.h:33
btSimplePair::m_userValue
int m_userValue
Definition: btHashedSimplePairCache.h:36