-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquadtreenode.cpp
More file actions
195 lines (176 loc) · 4.48 KB
/
quadtreenode.cpp
File metadata and controls
195 lines (176 loc) · 4.48 KB
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
#include "quadtreenode.h"
#include "core/os/memory.h"
#include "core/object/class_db.h"
#include "core/string/print_string.h"
#include "editor/editor_log.h"
void QuadTreeNode::init_node(const Rect2& bounds, const int state)
{
this->bounds = Rect2i(bounds);
this->state = state;
}
QuadTreeNode::QuadTreeNode()
{
state = 0;
}
QuadTreeNode::~QuadTreeNode() {}
void QuadTreeNode::split()
{
if (!child_nw.is_valid()) {
child_nw.instantiate();
}
if (!child_ne.is_valid()) {
child_ne.instantiate();
}
if (!child_sw.is_valid()) {
child_sw.instantiate();
}
if (!child_se.is_valid()) {
child_se.instantiate();
}
child_nw->parent = this;
child_ne->parent = this;
child_sw->parent = this;
child_se->parent = this;
child_nw->init_node(Rect2(
bounds.position.x,
bounds.position.y,
bounds.size.width/2,
bounds.size.height/2),
state);
child_ne->init_node(Rect2(
bounds.position.x + bounds.size.x/2,
bounds.position.y,
bounds.size.width/2,
bounds.size.height/2),
state);
child_sw->init_node(Rect2(
bounds.position.x,
bounds.position.y + bounds.size.y/2,
bounds.size.width/2,
bounds.size.height/2),
state);
child_se->init_node(Rect2(
bounds.position.x + bounds.size.x/2,
bounds.position.y + bounds.size.y/2,
bounds.size.width/2,
bounds.size.height/2),
state);
state = -1;
}
void QuadTreeNode::close(const int state)
{
this->state = state;
if (child_ne.is_valid()) {
child_ne->close(state);
child_ne.unref();
}
if (child_nw.is_valid()) {
child_nw->close(state);
child_nw.unref();
}
if (child_se.is_valid()) {
child_se->close(state);
child_se.unref();
}
if (child_sw.is_valid()) {
child_sw->close(state);
child_sw.unref();
}
}
void QuadTreeNode::insert_rect(const Rect2& target, const int value)
{
Rect2i target_i = Rect2i(target);
if (target_i.encloses(bounds))
{
close(value);
}
else if (target.intersects(bounds))
{
if (state != -1) split();
child_nw->insert_rect(target_i, value);
child_ne->insert_rect(target_i, value);
child_sw->insert_rect(target_i, value);
child_se->insert_rect(target_i, value);
if (child_ne->state != -1 &&
child_ne->state == child_nw->state &&
child_ne->state == child_sw->state &&
child_ne->state == child_se->state)
{
close(child_ne->state);
}
}
}
int QuadTreeNode::state_at_point(const Vector2& point)
{
if (state != -1)
{
return state;
}
else if (child_nw->bounds.has_point(point))
{
return child_nw->state_at_point(point);
}
else if (child_ne->bounds.has_point(point))
{
return child_ne->state_at_point(point);
}
else if (child_sw->bounds.has_point(point))
{
return child_sw->state_at_point(point);
}
else if (child_se->bounds.has_point(point))
{
return child_se->state_at_point(point);
}
return -1;
}
int QuadTreeNode::get_state()
{
return state;
}
void QuadTreeNode::set_state(const int state)
{
this->state = state;
}
Rect2 QuadTreeNode::get_bounds()
{
return Rect2(bounds);
}
void QuadTreeNode::set_bounds(const Rect2& bounds)
{
this->bounds = bounds;
}
Ref<QuadTreeNode> QuadTreeNode::get_nw()
{
return child_nw;
}
Ref<QuadTreeNode> QuadTreeNode::get_ne()
{
return child_ne;
}
Ref<QuadTreeNode> QuadTreeNode::get_sw()
{
return child_sw;
}
Ref<QuadTreeNode> QuadTreeNode::get_se()
{
return child_se;
}
void QuadTreeNode::_bind_methods()
{
ClassDB::bind_method(D_METHOD("init_node", "bounds", "state"), &QuadTreeNode::init_node);
ClassDB::bind_method(D_METHOD("split"), &QuadTreeNode::split);
ClassDB::bind_method(D_METHOD("close", "state"), &QuadTreeNode::close);
ClassDB::bind_method(D_METHOD("insert_rect", "target", "value"), &QuadTreeNode::insert_rect);
ClassDB::bind_method(D_METHOD("state_at_point", "point"), &QuadTreeNode::state_at_point);
ClassDB::bind_method(D_METHOD("get_state"), &QuadTreeNode::get_state);
ClassDB::bind_method(D_METHOD("get_bounds"), &QuadTreeNode::get_bounds);
ClassDB::bind_method(D_METHOD("set_state", "state"), &QuadTreeNode::set_state);
ClassDB::bind_method(D_METHOD("set_bounds", "bounds"), &QuadTreeNode::set_bounds);
ClassDB::bind_method(D_METHOD("get_nw"), &QuadTreeNode::get_nw);
ClassDB::bind_method(D_METHOD("get_ne"), &QuadTreeNode::get_ne);
ClassDB::bind_method(D_METHOD("get_sw"), &QuadTreeNode::get_sw);
ClassDB::bind_method(D_METHOD("get_se"), &QuadTreeNode::get_se);
ADD_PROPERTY(PropertyInfo(Variant::INT, "state", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_state", "get_state");
ADD_PROPERTY(PropertyInfo(Variant::RECT2, "bounds", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_bounds", "get_bounds");
}