-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuadTreeNode.cpp
299 lines (231 loc) · 6.6 KB
/
QuadTreeNode.cpp
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "QuadTreeNode.h"
#include "QuadTree.h"
#include <assert.h>
namespace qdt
{
// Defaults
int QuadTreeNode::minNumOccupants = 3;
int QuadTreeNode::maxNumOccupants = 6;
int QuadTreeNode::maxNumLevels = 20;
float QuadTreeNode::m_oversizeMultiplier = 1.2f;
QuadTreeNode::QuadTreeNode()
: m_hasChildren(false), m_numOccupantsBelow(0)
{
}
QuadTreeNode::QuadTreeNode(const AABB ®ion, int level, QuadTreeNode* pParent, QuadTree* pQuadTree)
: m_region(region), m_level(level), m_pParent(pParent), m_pQuadTree(pQuadTree),
m_hasChildren(false), m_numOccupantsBelow(0)
{
}
QuadTreeNode::~QuadTreeNode()
{
if(m_hasChildren)
DestroyChildren();
}
void QuadTreeNode::Create(const AABB ®ion, int level, QuadTreeNode* pParent, QuadTree* pQuadTree)
{
m_region = region;
m_level = level;
m_pParent = pParent;
m_pQuadTree = pQuadTree;
}
inline QuadTreeNode* QuadTreeNode::GetChild(const Point2i position)
{
return &(*m_children)[position.x][position.y];
}
void QuadTreeNode::GetPossibleOccupantPosition(QuadTreeOccupant* pOc, Point2i &point)
{
// Compare the center of the AABB of the occupant to that of this node to determine
// which child it may (possibly, not certainly) fit in
const Vec2f &occupantCenter(pOc->m_aabb.GetCenter());
const Vec2f &nodeRegionCenter(m_region.GetCenter());
if(occupantCenter.x > nodeRegionCenter.x)
point.x = 1;
else
point.x = 0;
if(occupantCenter.y > nodeRegionCenter.y)
point.y = 1;
else
point.y = 0;
}
void QuadTreeNode::AddToThisLevel(QuadTreeOccupant* pOc)
{
pOc->m_pQuadTreeNode = this;
m_pOccupants.insert(pOc);
}
bool QuadTreeNode::AddToChildren(QuadTreeOccupant* pOc)
{
assert(m_hasChildren);
Point2i position;
GetPossibleOccupantPosition(pOc, position);
QuadTreeNode* pChild = GetChild(position);
// See if the occupant fits in the child at the selected position
if(pChild->m_region.Contains(pOc->m_aabb))
{
// Fits, so can add to the child and finish
pChild->Add(pOc);
return true;
}
return false;
}
void QuadTreeNode::Partition()
{
assert(!m_hasChildren);
const Vec2f &halfRegionDims(m_region.GetHalfDims());
const Vec2f ®ionLowerBound(m_region.GetLowerBound());
const Vec2f ®ionCenter(m_region.GetCenter());
int nextLowerLevel = m_level - 1;
m_children = new std::vector<std::vector<QuadTreeNode>>();
// Create the children nodes
(*m_children).resize(2);
for(int x = 0; x < 2; x++)
{
(*m_children)[x].resize(2);
for(int y = 0; y < 2; y++)
{
Vec2f offset(x * halfRegionDims.x, y * halfRegionDims.y);
AABB childAABB(regionLowerBound + offset, regionCenter + offset);
childAABB.SetHalfDims(childAABB.GetHalfDims() * m_oversizeMultiplier);
// Scale up AABB by the oversize multiplier
(*m_children)[x][y].Create(childAABB, nextLowerLevel, this, m_pQuadTree);
}
}
m_hasChildren = true;
}
void QuadTreeNode::DestroyChildren()
{
assert(m_hasChildren);
delete m_children;
m_hasChildren = false;
}
void QuadTreeNode::Merge()
{
if(m_hasChildren)
{
// Place all occupants at lower levels into this node
GetOccupants(m_pOccupants);
DestroyChildren();
}
}
void QuadTreeNode::GetOccupants(std::unordered_set<QuadTreeOccupant*> &occupants)
{
// Iteratively parse subnodes in order to collect all occupants below this node
std::list<QuadTreeNode*> open;
open.push_back(this);
while(!open.empty())
{
// Depth-first (results in less memory usage), remove objects from open list
QuadTreeNode* pCurrent = open.back();
open.pop_back();
// Get occupants
for(std::unordered_set<QuadTreeOccupant*>::iterator it = pCurrent->m_pOccupants.begin(); it != pCurrent->m_pOccupants.end(); it++)
{
// Assign new node
(*it)->m_pQuadTreeNode = this;
// Add to this node
occupants.insert(*it);
}
// If the node has children, add them to the open list
if(pCurrent->m_hasChildren)
{
for(int x = 0; x < 2; x++)
for(int y = 0; y < 2; y++)
open.push_back(&(*pCurrent->m_children)[x][y]);
}
}
}
void QuadTreeNode::GetAllOccupantsBelow(std::vector<QuadTreeOccupant*> &occupants)
{
// Iteratively parse subnodes in order to collect all occupants below this node
std::list<QuadTreeNode*> open;
open.push_back(this);
while(!open.empty())
{
// Depth-first (results in less memory usage), remove objects from open list
QuadTreeNode* pCurrent = open.back();
open.pop_back();
// Get occupants
for(std::unordered_set<QuadTreeOccupant*>::iterator it = pCurrent->m_pOccupants.begin(); it != pCurrent->m_pOccupants.end(); it++)
// Add to this node
occupants.push_back(*it);
// If the node has children, add them to the open list
if(pCurrent->m_hasChildren)
{
for(int x = 0; x < 2; x++)
for(int y = 0; y < 2; y++)
open.push_back(&(*pCurrent->m_children)[x][y]);
}
}
}
void QuadTreeNode::Update(QuadTreeOccupant* pOc)
{
// Remove, may be re-added to this node later
m_pOccupants.erase(pOc);
// Propogate upwards, looking for a node that has room (the current one may still have room)
QuadTreeNode* pNode = this;
while(pNode != NULL)
{
pNode->m_numOccupantsBelow--;
// If has room for 1 more, found a spot
if(pNode->m_region.Contains(pOc->m_aabb))
break;
pNode = pNode->m_pParent;
}
// If no node that could contain the occupant was found, add to outside root set
if(pNode == NULL)
{
m_pQuadTree->m_outsideRoot.insert(pOc);
pOc->m_pQuadTreeNode = NULL;
}
else // Add to the selected node
pNode->Add(pOc);
}
void QuadTreeNode::Remove(QuadTreeOccupant* pOc)
{
assert(!m_pOccupants.empty());
// Remove from node
m_pOccupants.erase(pOc);
// Propogate upwards, merging if there are enough occupants in the node
QuadTreeNode* pNode = this;
while(pNode != NULL)
{
pNode->m_numOccupantsBelow--;
if(pNode->m_numOccupantsBelow >= minNumOccupants)
{
pNode->Merge();
break;
}
pNode = pNode->m_pParent;
}
}
void QuadTreeNode::Add(QuadTreeOccupant* pOc)
{
m_numOccupantsBelow++;
// See if the occupant fits into any children (if there are any)
if(m_hasChildren)
{
if(AddToChildren(pOc))
return; // Fit, can stop
}
else
{
// Check if we need a new partition
if(static_cast<signed>(m_pOccupants.size()) >= maxNumOccupants && m_level < maxNumLevels)
{
Partition();
if(AddToChildren(pOc))
return;
}
}
// Did not fit in anywhere, add to this level, even if it goes over the maximum size
AddToThisLevel(pOc);
}
QuadTree* QuadTreeNode::GetTree()
{
return m_pQuadTree;
}
const AABB &QuadTreeNode::GetRegion()
{
return m_region;
}
}