-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
156 lines (112 loc) · 2.52 KB
/
utils.h
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
#ifndef __QUADTREE_H__
#define __QUADTREE_H__
#include <vector>
#include <queue>
#include "cell.h"
/*
* Throws out_of_range if adding/subtracting offset causes overflow.
*/
unsigned long
ApplyOffset(unsigned long target,
unsigned long offset,
bool neg);
unsigned long
CalculateOffset(unsigned long from,
unsigned long to,
bool *neg);
/**
* Rectangle for collision detection
*/
class BoundingBox {
public:
// Left hand coord
unsigned long _x;
// Top coord
unsigned long _y;
unsigned long _width;
unsigned long _height;
BoundingBox()
: _x(0), _y(0), _width(0), _height(0) {}
BoundingBox(unsigned long x,
unsigned long y,
unsigned long width,
unsigned long height)
: _x(x), _y(y), _width(width), _height(height) {}
/*
* Only points along top and right edges count
* This is so that each point is in only one
* leaf node of the quadtree.
*/
bool
Contains(unsigned long x,
unsigned long y) const;
// All points along the edge count.
bool
ContainsGreedy(unsigned long x,
unsigned long y) const;
bool
Intersects(const BoundingBox& box) const;
};
/**
* Utility class - queue that rejects duplicates.
*
* We can only insert an element into this queue once.
* Cannot insert an equal element even if we pop the
* original one.
*
* TODO: (low priority) make template
*/
class CellQueue {
private:
std::queue<Cell> _queue;
CellSet _elements;
public:
CellQueue() {}
CellQueue(const CellSet& cells)
: _elements(cells) {
for (CellSet::const_iterator it = cells.begin();
it != cells.end(); ++it) {
_queue.push(*it);
}
}
bool
Push(const Cell& cell);
Cell&
Front();
bool
Empty() const;
void
Pop();
};
/*
* Simple region quadtree implementation, with the limit
* per region at 1. Stores Cell objects.
*
* TODO: (not important) make template
*
* See: http://en.wikipedia.org/wiki/Quadtree
*/
class QuadTree {
private:
std::vector<Cell> _cells;
BoundingBox _boundary;
QuadTree *_upperLeft;
QuadTree *_upperRight;
QuadTree *_lowerLeft;
QuadTree *_lowerRight;
void
Divide();
public:
QuadTree(const BoundingBox& boundary)
: _boundary(boundary), _upperLeft(NULL), _upperRight(NULL),
_lowerLeft(NULL), _lowerRight(NULL) {}
~QuadTree();
void
Clear();
bool
Insert(const Cell& point);
void
FindPoints(const BoundingBox& bound,
CellSet& out) const;
};
#endif