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

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
20
21 #include "LinearMath/btAlignedObjectArray.h"
22
23 const int BT_SIMPLE_NULL_PAIR=0xffffffff;
24
25 struct btSimplePair
26 {
27 btSimplePair(int indexA,int indexB)
28 :m_indexA(indexA),
29 m_indexB(indexB),
30 m_userPointer(0)
31 {
32 }
33
34 int m_indexA;
35 int m_indexB;
36 union
37 {
38 void* m_userPointer;
39 int m_userValue;
40 };
41 };
42
43 typedef btAlignedObjectArray<btSimplePair> btSimplePairArray;
44
45
46
47 extern int gOverlappingSimplePairs;
48 extern int gRemoveSimplePairs;
49 extern int gAddedSimplePairs;
50 extern int gFindSimplePairs;
51
52
53
54
55 class btHashedSimplePairCache
56 {
57 btSimplePairArray m_overlappingPairArray;
58
59
60 protected:
61
62 btAlignedObjectArray<int> m_hashTable;
63 btAlignedObjectArray<int> m_next;
64
65
66 public:
67 btHashedSimplePairCache();
68 virtual ~btHashedSimplePairCache();
69
70 void removeAllPairs();
71
72 virtual void* removeOverlappingPair(int indexA,int indexB);
73
74 // Add a pair and return the new pair. If the pair already exists,
75 // no new pair is created and the old one is returned.
76 virtual btSimplePair* addOverlappingPair(int indexA,int indexB)
77 {
78 gAddedSimplePairs++;
79
80 return internalAddPair(indexA,indexB);
81 }
82
83
84 virtual btSimplePair* getOverlappingPairArrayPtr()
85 {
86 return &m_overlappingPairArray[0];
87 }
88
89 const btSimplePair* getOverlappingPairArrayPtr() const
90 {
91 return &m_overlappingPairArray[0];
92 }
93
94 btSimplePairArray& getOverlappingPairArray()
95 {
96 return m_overlappingPairArray;
97 }
98
99 const btSimplePairArray& getOverlappingPairArray() const
100 {
101 return m_overlappingPairArray;
102 }
103
104
105 btSimplePair* findPair(int indexA,int indexB);
106
107 int GetCount() const { return m_overlappingPairArray.size(); }
108
109 int getNumOverlappingPairs() const
110 {
111 return m_overlappingPairArray.size();
112 }
113 private:
114
115 btSimplePair* internalAddPair(int indexA, int indexB);
116
117 void growTables();
118
119 SIMD_FORCE_INLINE bool equalsPair(const btSimplePair& pair, int indexA, int indexB)
120 {
121 return pair.m_indexA == indexA && pair.m_indexB == indexB;
122 }
123
124
125
126 SIMD_FORCE_INLINE unsigned int getHash(unsigned int indexA, unsigned int indexB)
127 {
128 int key = static_cast<int>(((unsigned int)indexA) | (((unsigned int)indexB) <<16));
129 // Thomas Wang's hash
130
131 key += ~(key << 15);
132 key ^= (key >> 10);
133 key += (key << 3);
134 key ^= (key >> 6);
135 key += ~(key << 11);
136 key ^= (key >> 16);
137 return static_cast<unsigned int>(key);
138 }
139
140
141
142
143
144 SIMD_FORCE_INLINE btSimplePair* internalFindPair(int proxyIdA , int proxyIdB, int hash)
145 {
146
147 int index = m_hashTable[hash];
148
149 while( index != BT_SIMPLE_NULL_PAIR && equalsPair(m_overlappingPairArray[index], proxyIdA, proxyIdB) == false)
150 {
151 index = m_next[index];
152 }
153
154 if ( index == BT_SIMPLE_NULL_PAIR )
155 {
156 return NULL;
157 }
158
159 btAssert(index < m_overlappingPairArray.size());
160
161 return &m_overlappingPairArray[index];
162 }
163
164
165 };
166
167
168
169
170 #endif //BT_HASHED_SIMPLE_PAIR_CACHE_H
171
172
btSimplePair::m_indexA
int m_indexA
Definition: btHashedSimplePairCache.h:34
btHashedSimplePairCache::getOverlappingPairArray
const btSimplePairArray & getOverlappingPairArray() const
Definition: btHashedSimplePairCache.h:99
btHashedSimplePairCache::addOverlappingPair
virtual btSimplePair * addOverlappingPair(int indexA, int indexB)
Definition: btHashedSimplePairCache.h:76
btAlignedObjectArray< btSimplePair >
btHashedSimplePairCache::getHash
unsigned int getHash(unsigned int indexA, unsigned int indexB)
Definition: btHashedSimplePairCache.h:126
btHashedSimplePairCache::internalFindPair
btSimplePair * internalFindPair(int proxyIdA, int proxyIdB, int hash)
Definition: btHashedSimplePairCache.h:144
btHashedSimplePairCache
Definition: btHashedSimplePairCache.h:55
gFindSimplePairs
int gFindSimplePairs
Definition: btHashedSimplePairCache.cpp:26
btAssert
#define btAssert(x)
Definition: btScalar.h:113
gOverlappingSimplePairs
int gOverlappingSimplePairs
Definition: btHashedSimplePairCache.cpp:23
SIMD_FORCE_INLINE
#define SIMD_FORCE_INLINE
Definition: btScalar.h:63
btHashedSimplePairCache::getOverlappingPairArray
btSimplePairArray & getOverlappingPairArray()
Definition: btHashedSimplePairCache.h:94
btHashedSimplePairCache::getOverlappingPairArrayPtr
virtual btSimplePair * getOverlappingPairArrayPtr()
Definition: btHashedSimplePairCache.h:84
btHashedSimplePairCache::getOverlappingPairArrayPtr
const btSimplePair * getOverlappingPairArrayPtr() const
Definition: btHashedSimplePairCache.h:89
gRemoveSimplePairs
int gRemoveSimplePairs
Definition: btHashedSimplePairCache.cpp:24
BT_SIMPLE_NULL_PAIR
const int BT_SIMPLE_NULL_PAIR
Definition: btHashedSimplePairCache.h:23
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:155
gAddedSimplePairs
int gAddedSimplePairs
Definition: btHashedSimplePairCache.cpp:25
btSimplePair
Definition: btHashedSimplePairCache.h:25
btSimplePair::m_userPointer
void * m_userPointer
Definition: btHashedSimplePairCache.h:38
btHashedSimplePairCache::GetCount
int GetCount() const
Definition: btHashedSimplePairCache.h:107
btHashedSimplePairCache::m_hashTable
btAlignedObjectArray< int > m_hashTable
Definition: btHashedSimplePairCache.h:62
btSimplePairArray
btAlignedObjectArray< btSimplePair > btSimplePairArray
Definition: btHashedSimplePairCache.h:43
btSimplePair::m_indexB
int m_indexB
Definition: btHashedSimplePairCache.h:35
btSimplePair::m_userValue
int m_userValue
Definition: btHashedSimplePairCache.h:39
btHashedSimplePairCache::equalsPair
bool equalsPair(const btSimplePair &pair, int indexA, int indexB)
Definition: btHashedSimplePairCache.h:119
btHashedSimplePairCache::getNumOverlappingPairs
int getNumOverlappingPairs() const
Definition: btHashedSimplePairCache.h:109
btAlignedObjectArray.h
btHashedSimplePairCache::m_next
btAlignedObjectArray< int > m_next
Definition: btHashedSimplePairCache.h:63
btHashedSimplePairCache::m_overlappingPairArray
btSimplePairArray m_overlappingPairArray
Definition: btHashedSimplePairCache.h:57
btSimplePair::btSimplePair
btSimplePair(int indexA, int indexB)
Definition: btHashedSimplePairCache.h:27