-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuadTree.inl
245 lines (206 loc) · 6.23 KB
/
QuadTree.inl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
template<class T> void QuadTree::intersectRecursive(const AABox2d& p, T& visitor) const
{
struct TT {
static void Visit(QuadIterator it, const AABox2d& p, T& visitor)
{
const Node * me = it.current();
if ( (uint32&)(*me) == DBG_WORD )
return;
visitor(it);
if (!it.moveNext()) // last node has no childs, no sense test for intersection
return;
QuadrantFlags res = me->intersectionQuadrants(p);
if (res & (1 << LeftUpper))
Visit(it.nearby(LeftUpper), p, visitor);
if (res & (1 << RightUpper))
Visit(it.nearby(RightUpper), p, visitor);
if (res & (1 << LeftLower))
Visit(it.nearby(LeftLower), p, visitor);
if (res & (1 << RightLower))
Visit(it.nearby(RightLower), p, visitor);
}
};
TT::Visit(QuadIterator::create(this), p, visitor);
}
void QuadTree::initNodes(const Point& center, length_type sideSize, uint32 nodesAmount)
{
struct TT {
static void Visit(QuadIterator it, Point myCenter, length_type mySideSize)
{
Node * me = it.current();
if ( (uint32&)(*me) != DBG_WORD )
return;
me->xDiv = myCenter.x;
me->yDiv = myCenter.y;
if (!it.moveNext()) // last node has no childs
return;
Point child_center(myCenter);
length_type child_SideSize = mySideSize / 2;
length_type offset = mySideSize / 4;
child_center.x -= offset;
child_center.y += offset;
Visit(it.nearby(LeftUpper), child_center, child_SideSize); // LeftUpper
child_center = myCenter;
child_center.x += offset;
child_center.y += offset;
Visit(it.nearby(RightUpper), child_center, child_SideSize); // RightUpper
child_center = myCenter;
child_center.x -= offset;
child_center.y -= offset;
Visit(it.nearby(LeftLower), child_center, child_SideSize); // LeftLower
child_center = myCenter;
child_center.x += offset;
child_center.y -= offset;
Visit(it.nearby(RightLower), child_center, child_SideSize); // RightLower
}
};
memset(nodes, DBG_WORD, nodesAmount * sizeof Node);
TT::Visit(QuadIterator::create(this), center, sideSize);
}
QuadIterator QuadTree::deepestContaining(const AABox2d& p) const
{
QuadIterator it(QuadIterator::create(this));
for(;;)
{
Node * me = it.current();
if ( (uint32&)(*me) == DBG_WORD )
break;
if (!it.moveNext()) // last node has no childs
break;
SpaceDivision::IntersectionResult res = me->intersection(p);
switch (res)
{
case SpaceDivision::Mask_LeftUpper:
it.moveTo(LeftUpper);
continue;
case SpaceDivision::Mask_RightUpper:
it.moveTo(RightUpper);
continue;
case SpaceDivision::Mask_LeftLower:
it.moveTo(LeftLower);
continue;
case SpaceDivision::Mask_RightLower:
it.moveTo(RightLower);
continue;
default:
break;
}
it.movePrev();
break;
}
return it;
}
template<class T> void QuadTree::Deprecate_intersect(const AABox2d& p, T& visitor) const
{
enum{
StackLast = 30,
};
QuadIterator it(QuadIterator::create(this));
uint8 stackArray[StackLast+1];
uint8 * stack = stackArray;
for(;;)
{
const Node * me = it.current();
if ( (uint32&)(*me) == DBG_WORD )
return;
visitor(it);
if (it.moveNext())
{
if ((stack - stackArray) == StackLast)
return; // overflow
*(++stack) = (uint8)me->intersectionQuadrants(p);
}
else
{
for(;;)
{
if (stack == stackArray)
return;
if (*stack)
break;
--stack;
it.movePrev();
}
}
uint8& res = *stack;
// couple of 'if' statements are faster than commented code below
if (res & NorthWest){
res &= ~NorthWest;
it.moveTo(LeftUpper);
}
else if (res & NorthEast){
res &= ~NorthEast;
it.moveTo(RightUpper);
}
else if (res & SouthWest){
res &= ~SouthWest;
it.moveTo(LeftLower);
}
else if (res & SouthEast){
res &= ~SouthEast;
it.moveTo(RightLower);
}
/*for (uint8 i = 0; i < 4; ++i)
{
if (res & (1 << i))
{
res &= ~(1 << i);
it.moveTo((ChildOffset)i);
break;
}
}*/
}
}
template<class T> void QuadTree::intersect(const AABox2d& p, T& visitor) const
{
enum{
StackLast = 30,
};
uint8 stackArray[StackLast+1];
uint8 * stack = stackArray;
QuadIterator it(QuadIterator::create(this));
visitor(it);
for(;;)
{
const Node * me = it.current();
if ( (uint32&)(*me) == DBG_WORD )
return;
if (it.moveNext())
{
if ((stack - stackArray) == StackLast)
return; // overflow
uint8 res = (uint8)me->intersectionQuadrants(p);
*(++stack) = res;
for (uint8 i = 0; i < 4; ++i)
{
if (res & (1 << i))
{
it.moveTo((ChildOffset)i);
visitor(it);
}
}
}
else
{
for(;;)
{
if (stack == stackArray)
return;
if (*stack)
break;
--stack;
it.movePrev();
}
}
uint8& res = *stack;
for (uint8 i = 0; i < 4; ++i)
{
if (res & (1 << i))
{
res &= ~(1 << i);
it.moveTo((ChildOffset)i);
break;
}
}
}
}