-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathQuadTree.h
60 lines (51 loc) · 1.54 KB
/
QuadTree.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
#pragma once
#include <any>
#include <vector>
#include <algorithm>
struct Rect {
double x, y, width, height;
bool contains(const Rect &other) const noexcept;
bool intersects(const Rect &other) const noexcept;
double getLeft() const noexcept;
double getTop() const noexcept;
double getRight() const noexcept;
double getBottom() const noexcept;
Rect(const Rect&);
Rect(double _x = 0, double _y = 0, double _width = 0, double _height = 0);
};
struct Collidable {
friend class QuadTree;
public:
Rect bound;
std::any data;
Collidable(const Rect &_bounds = {}, std::any _data = {});
private:
QuadTree *qt = nullptr;
Collidable(const Collidable&) = delete;
};
class QuadTree {
public:
QuadTree(const Rect &_bound, unsigned _capacity, unsigned _maxLevel);
QuadTree(const QuadTree&);
QuadTree();
bool insert(Collidable *obj);
bool remove(Collidable *obj);
bool update(Collidable *obj);
std::vector<Collidable*> &getObjectsInBound(const Rect &bound);
unsigned totalChildren() const noexcept;
unsigned totalObjects() const noexcept;
void clear() noexcept;
~QuadTree();
private:
bool isLeaf = true;
unsigned level = 0;
unsigned capacity;
unsigned maxLevel;
Rect bounds;
QuadTree* parent = nullptr;
QuadTree* children[4] = { nullptr, nullptr, nullptr, nullptr };
std::vector<Collidable*> objects, foundObjects;
void subdivide();
void discardEmptyBuckets();
inline QuadTree *getChild(const Rect &bound) const noexcept;
};