-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quadtree.cpp
180 lines (146 loc) · 4.66 KB
/
Quadtree.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
#include "Quadtree.h"
#include "debug_draw/debugdraw.h"
#include "Globals.h"
#include "GameObject.h"
#include "Leaks.h"
QuadtreeNode::QuadtreeNode(AABB boundingBox) : boundingBox(boundingBox), subdivided(false) {
}
QuadtreeNode::~QuadtreeNode() {
}
void QuadtreeNode::InsertGameObject(GameObject* gameObject) {
if (!subdivided) { //It's not subdivided
gameObjects.push_back(gameObject);
if (gameObjects.size() > NODE_MAX_CAPACITY) {
Subdivide();
Organize();
}
} else {
// If intersects in other QuadTree insert gameObject
AABB gameObjectAABB = gameObject->GetAABB().MinimalEnclosingAABB();
for (std::vector<QuadtreeNode>::iterator it = childNodes.begin(); it != childNodes.end(); ++it) {
if ((*it).boundingBox.Intersects(gameObjectAABB)) {
(*it).InsertGameObject(gameObject);
}
}
}
}
void QuadtreeNode::EraseGameObject(GameObject* gameObject) {
std::vector<GameObject*>::iterator it = std::find(gameObjects.begin(), gameObjects.end(), gameObject);
if (it != gameObjects.end()) { // Game Object found in node
gameObjects.erase(it);
}
if (subdivided) {
for (int i = 0; i < childNodes.size(); ++i) {
childNodes[i].EraseGameObject(gameObject);
}
}
//This could be optimized by collecting the AMOUNT of gameobjects contained within children, if children contain less than the MAX_CAPACITY, then their gameObjects would
//Come back to the parent and they would be deleted
bool allEmpty = true;
for (int i = 0; i < childNodes.size() && allEmpty; ++i) {
allEmpty = (childNodes[i].IsEmpty());
}
if (allEmpty) {
childNodes.clear();
subdivided = false;
}
}
bool QuadtreeNode::IsEmpty() const {
bool allEmpty = gameObjects.size() == 0;
if (subdivided) {
for (std::vector<QuadtreeNode>::const_iterator it = childNodes.begin(); it != childNodes.end() && allEmpty; ++it) {
allEmpty = (*it).IsEmpty();
}
}
return allEmpty;
}
void QuadtreeNode::Draw() const {
dd::aabb(boundingBox.minPoint, boundingBox.maxPoint, float3::one);
if (subdivided) { // If subdivided, draw all children
for (std::vector<QuadtreeNode>::const_iterator it = childNodes.begin(); it != childNodes.end(); ++it) {
(*it).Draw();
}
}
}
void QuadtreeNode::Subdivide() {
float3 centerPoint = boundingBox.CenterPoint();
float3 childSize = boundingBox.HalfSize();
childSize.y *= 2;
childNodes.reserve(CHILD_AMOUNT);
AABB aaBB;
float3 childCenter;
//// North - West
childCenter.x = centerPoint.x - childSize.x / 2;
childCenter.y = centerPoint.y;
childCenter.z = centerPoint.z + childSize.z / 2;
aaBB.SetFromCenterAndSize(childCenter, childSize);
childNodes.push_back(QuadtreeNode(aaBB));
// North - East
childCenter.x = centerPoint.x + childSize.x / 2;
childCenter.y = centerPoint.y;
childCenter.z = centerPoint.z + childSize.z / 2;
aaBB.SetFromCenterAndSize(childCenter, childSize);
childNodes.push_back(QuadtreeNode(aaBB));
// South - West
childCenter.x = centerPoint.x - childSize.x / 2;
childCenter.y = centerPoint.y;
childCenter.z = centerPoint.z - childSize.z / 2;
aaBB.SetFromCenterAndSize(childCenter, childSize);
childNodes.push_back(QuadtreeNode(aaBB));
// South - East
childCenter.x = centerPoint.x + childSize.x / 2;
childCenter.y = centerPoint.y;
childCenter.z = centerPoint.z - childSize.z / 2;
aaBB.SetFromCenterAndSize(childCenter, childSize);
childNodes.push_back(QuadtreeNode(aaBB));
for (int i = 0; i < CHILD_AMOUNT; i++) {
childNodes[i].parent = this;
}
subdivided = true;
}
/// <summary>
/// We split contained GameObjects among all child nodes
/// </summary>
void QuadtreeNode::Organize() {
for (std::vector<GameObject*>::iterator it = gameObjects.begin(); it != gameObjects.end();) {
GameObject* currentGO = *it;
bool intersections[CHILD_AMOUNT];
unsigned intersectionsFound = 0;
AABB gameObjectAABB(currentGO->GetAABB().MinimalEnclosingAABB());
for (int i = 0; i < CHILD_AMOUNT; i++) {
intersections[i] = childNodes[i].boundingBox.Intersects(gameObjectAABB);
intersectionsFound = intersections[0] ? intersectionsFound + 1 : intersectionsFound;
}
if (intersectionsFound > 1) {
++it;
} else {
it = gameObjects.erase(it);
for (int i = 0; i < CHILD_AMOUNT; i++) {
if (intersections[i]) {
childNodes[i].InsertGameObject(currentGO);
}
}
}
}
}
Quadtree::Quadtree(const AABB& boundingBox) {
root = new QuadtreeNode(boundingBox);
}
Quadtree::~Quadtree() {
RELEASE(root);
}
void Quadtree::InsertGameObject(GameObject* gameObject) {
if (root) {
if (gameObject->GetAABB().Intersects(root->boundingBox)) {
root->InsertGameObject(gameObject);
}
}
}
void Quadtree::EraseGameObject(GameObject* gameObject) {
if (root) {
root->EraseGameObject(gameObject);
}
}
void Quadtree::Draw() {
root->Draw();
}