-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
208 lines (173 loc) · 4.46 KB
/
utils.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
#include <cassert>
#include <iostream>
#include <queue>
#include "utils.h"
using namespace std;
unsigned long
ApplyOffset(unsigned long target,
unsigned long offset,
bool neg) {
if (neg) {
if (offset > target) {
throw out_of_range("Offset causes overflow");
}
return target - offset;
} else {
if (target > (ULONG_MAX - offset)) {
throw out_of_range("Offset causes overflow");
}
return target + offset;
}
}
unsigned long
CalculateOffset(unsigned long from,
unsigned long to,
bool *neg) {
if (from > to) {
*neg = true;
return from - to;
} else {
*neg = false;
return to - from;
}
}
bool
BoundingBox::Contains(unsigned long x,
unsigned long y) const {
return x > _x && x <= (_x + _width) &&
y >= _y && y < (_y + _height);
}
bool
BoundingBox::ContainsGreedy(unsigned long x,
unsigned long y) const {
return x >= _x && x <= (_x + _width) &&
y >= _y && y <= (_y + _height);
}
bool
BoundingBox::Intersects(const BoundingBox& box) const {
return !((box._x < (ULONG_MAX - box._width) &&
_x > (box._x + box._width)) ||
(_x < (ULONG_MAX - _width) &&
(_x + _width) < box._x) ||
(box._y < (ULONG_MAX - box._height) &&
_y > (box._y + box._height)) ||
(_y < (ULONG_MAX - _height) &&
(_y + _height) < box._y));
}
QuadTree::~QuadTree() {
Clear();
}
void
QuadTree::Clear() {
_cells.clear();
if (_upperLeft != NULL) {
delete _upperLeft;
_upperLeft = NULL;
}
if (_upperRight != NULL) {
delete _upperRight;
_upperRight = NULL;
}
if (_lowerLeft != NULL) {
delete _lowerLeft;
_lowerLeft = NULL;
}
if (_lowerRight != NULL) {
delete _lowerRight;
_lowerRight = NULL;
}
assert(_cells.empty());
}
bool
CellQueue::Push(const Cell& cell) {
if (_elements.insert(cell).second) {
_queue.push(cell);
return true;
} else {
return false;
}
}
Cell&
CellQueue::Front() {
return _queue.front();
}
bool
CellQueue::Empty() const {
return _queue.empty();
}
void
CellQueue::Pop() {
_queue.pop();
}
void
QuadTree::Divide() {
// Should only be called once
if (_upperLeft != NULL) {
return;
}
assert(!_cells.empty());
unsigned long leftWidth = _boundary._width / 2;
unsigned long rightWidth = leftWidth + _boundary._width % 2;
unsigned long topHeight = _boundary._height / 2;
unsigned long bottomHeight = topHeight + _boundary._height %2;
_upperLeft = new QuadTree(BoundingBox(_boundary._x, _boundary._y,
leftWidth, topHeight));
_upperRight = new QuadTree(BoundingBox(_boundary._x + leftWidth,
_boundary._y, rightWidth, topHeight));
_lowerLeft = new QuadTree(BoundingBox(_boundary._x, _boundary._y + topHeight,
leftWidth, bottomHeight));
_lowerRight = new QuadTree(BoundingBox(_boundary._x + leftWidth,
_boundary._y + topHeight,
rightWidth, bottomHeight));
Cell cell = *_cells.begin();
_cells.pop_back();
Insert(cell);
assert(_cells.empty());
}
bool
QuadTree::Insert(const Cell& cell) {
if (_boundary.Contains(cell.x, cell.y)) {
if (_cells.empty() && _upperLeft == NULL) {
_cells.push_back(cell);
return true;
} else {
Divide();
assert(_cells.empty());
if (_upperLeft->Insert(cell)) {
return true;
} else if (_upperRight->Insert(cell)) {
return true;
} else if (_lowerLeft->Insert(cell)) {
return true;
} else if (_lowerRight->Insert(cell)) {
return true;
} else {
return false;
}
}
} else {
return false;
}
}
void
QuadTree::FindPoints(const BoundingBox& bound,
CellSet& out) const {
if (_boundary.Intersects(bound)) {
if (_upperLeft == NULL) {
// This is a leaf node. Check it!
for (vector<Cell>::const_iterator it = _cells.begin();
it != _cells.end(); ++it) {
if (bound.ContainsGreedy(it->x, it->y)) {
out.insert(*it);
}
}
} else {
// This is a parent node.
assert(_cells.empty());
_upperLeft->FindPoints(bound, out);
_upperRight->FindPoints(bound, out);
_lowerLeft->FindPoints(bound, out);
_lowerRight->FindPoints(bound, out);
}
}
}