first commit
[blok] / Box2D / Source / Collision / b2PairManager.h
1 /*
2 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty.  In no event will the authors be held liable for any damages
6 * 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
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18
19 // The pair manager is used by the broad-phase to quickly add/remove/find pairs
20 // of overlapping proxies. It is based closely on code provided by Pierre Terdiman.
21 // http://www.codercorner.com/IncrementalSAP.txt
22
23 #ifndef B2_PAIR_MANAGER_H
24 #define B2_PAIR_MANAGER_H
25
26 #include "../Common/b2Settings.h"
27 #include "../Common/b2Math.h"
28
29 #include <climits>
30
31 class b2BroadPhase;
32 struct b2Proxy;
33
34 const uint16 b2_nullPair = USHRT_MAX;
35 const uint16 b2_nullProxy = USHRT_MAX;
36 const int32 b2_tableCapacity = b2_maxPairs;     // must be a power of two
37 const int32 b2_tableMask = b2_tableCapacity - 1;
38
39 struct b2Pair
40 {
41         enum
42         {
43                 e_pairBuffered  = 0x0001,
44                 e_pairRemoved   = 0x0002,
45                 e_pairFinal             = 0x0004,
46         };
47
48         void SetBuffered()              { status |= e_pairBuffered; }
49         void ClearBuffered()    { status &= ~e_pairBuffered; }
50         bool IsBuffered()               { return (status & e_pairBuffered) == e_pairBuffered; }
51
52         void SetRemoved()               { status |= e_pairRemoved; }
53         void ClearRemoved()             { status &= ~e_pairRemoved; }
54         bool IsRemoved()                { return (status & e_pairRemoved) == e_pairRemoved; }
55
56         void SetFinal()         { status |= e_pairFinal; }
57         bool IsFinal()          { return (status & e_pairFinal) == e_pairFinal; }
58
59         void* userData;
60         uint16 proxyId1;
61         uint16 proxyId2;
62         uint16 next;
63         uint16 status;
64 };
65
66 struct b2BufferedPair
67 {
68         uint16 proxyId1;
69         uint16 proxyId2;
70 };
71
72 class b2PairCallback
73 {
74 public:
75         virtual ~b2PairCallback() {}
76
77         // This should return the new pair user data. It is ok if the
78         // user data is null.
79         virtual void* PairAdded(void* proxyUserData1, void* proxyUserData2) = 0;
80
81         // This should free the pair's user data. In extreme circumstances, it is possible
82         // this will be called with null pairUserData because the pair never existed.
83         virtual void PairRemoved(void* proxyUserData1, void* proxyUserData2, void* pairUserData) = 0;
84 };
85
86 class b2PairManager
87 {
88 public:
89         b2PairManager();
90
91         void Initialize(b2BroadPhase* broadPhase, b2PairCallback* callback);
92
93         void AddBufferedPair(int32 proxyId1, int32 proxyId2);
94         void RemoveBufferedPair(int32 proxyId1, int32 proxyId2);
95
96         void Commit();
97
98 private:
99         b2Pair* Find(int32 proxyId1, int32 proxyId2);
100         b2Pair* Find(int32 proxyId1, int32 proxyId2, uint32 hashValue);
101
102         b2Pair* AddPair(int32 proxyId1, int32 proxyId2);
103         void* RemovePair(int32 proxyId1, int32 proxyId2);
104
105         void ValidateBuffer();
106         void ValidateTable();
107
108 public:
109         b2BroadPhase *m_broadPhase;
110         b2PairCallback *m_callback;
111         b2Pair m_pairs[b2_maxPairs];
112         uint16 m_freePair;
113         int32 m_pairCount;
114
115         b2BufferedPair m_pairBuffer[b2_maxPairs];
116         int32 m_pairBufferCount;
117
118         uint16 m_hashTable[b2_tableCapacity];
119 };
120
121 #endif