-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkd_tree.h
More file actions
158 lines (120 loc) · 4.42 KB
/
kd_tree.h
File metadata and controls
158 lines (120 loc) · 4.42 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
#pragma once
namespace Tmpl8
{
class KDTree final
{
public:
KDTree() noexcept = default;
KDTree(std::vector<Tank*> values, int begin, int end) noexcept;
KDTree(const KDTree& other) noexcept = delete;
KDTree(KDTree&& other) noexcept = default;
KDTree& operator=(const KDTree& tree) noexcept = delete;
KDTree& operator=(KDTree&& tree) noexcept = default;
void rebuild() noexcept;
Tank* findNearestNeighbour(vec2 point) noexcept;
Tank* findNearestNeighbour_norecursion(vec2 point) noexcept;
~KDTree() noexcept = default;
private:
enum Axis
{
X = 0, Y = 1
};
struct Node
{
Tank* value = nullptr;
Axis axis = Axis::X;
Node* left = nullptr;
Node* right = nullptr;
};
std::vector<Node> nodes;
Node* root;
Node* build(int begin, int end, int depth = 0) noexcept;
void findNearestNeighbour(const vec2& point, Node* node, Tank*& closest_tank, float& closest_distance) noexcept;
};
inline KDTree::KDTree(std::vector<Tank*> values, int begin, int end) noexcept
{
this->nodes.reserve(end-begin);
for (int i = begin; i < end; i++) {
nodes.push_back(Node{values[i]});
}
this->root = build(0, nodes.size());
}
inline void KDTree::rebuild() noexcept
{
this->root = build(0, nodes.size());
}
inline KDTree::Node* KDTree::build(int begin, int end, int depth) noexcept
{
if (begin >= end) return nullptr;
Axis axis = Axis(depth % 2);
const auto middle = begin + (end - begin) / 2;
{ // Partially sort 'nodes' such that the object pointed to by middle_it appears at the same index as if 'nodes' had been fully sorted.
const auto begin_it = this->nodes.begin() + begin;
const auto middle_it = this->nodes.begin() + middle;
const auto end_it = this->nodes.begin() + end;
const auto comparator = [=](const Node& a, const Node& b) noexcept { return a.value->position.cell[axis] < b.value->position.cell[axis]; };
std::nth_element(begin_it, middle_it, end_it, comparator);
} // Done sorting
Node& current_node = this->nodes[middle];
current_node.axis = axis;
current_node.left = build(begin, middle, depth + 1);
current_node.right = build(middle + 1, end, depth + 1);
return ¤t_node;
}
inline Tank* KDTree::findNearestNeighbour(vec2 point) noexcept
{
Tank* closest_tank = nullptr;
float closest_distance = std::numeric_limits<float>::infinity();
findNearestNeighbour(point, this->root, closest_tank, closest_distance);
return closest_tank;
}
inline void KDTree::findNearestNeighbour(const vec2& point, Node* node, Tank*& closest_tank, float& closest_distance) noexcept
{
if (node == nullptr) return;
if (node->value->active)
{
const auto distance = (node->value->position - point).sqrLength();
if (distance < closest_distance)
{
closest_distance = distance;
closest_tank = node->value;
}
}
if (closest_distance == 0)
return;
float dx = node->value->position.cell[node->axis] - point.cell[node->axis];
findNearestNeighbour(point, dx > 0 ? node->left : node->right, closest_tank, closest_distance);
if (dx * dx >= closest_distance)
return;
findNearestNeighbour(point, dx > 0 ? node->right : node->left, closest_tank, closest_distance);
}
inline Tank* KDTree::findNearestNeighbour_norecursion(vec2 point) noexcept
{
Tank* closest_tank = nullptr;
float closest_distance = std::numeric_limits<float>::infinity();
std::deque<Node*> nodes;
nodes.push_back(this->root);
while (!nodes.empty())
{
Node* current_node = nodes.front();
nodes.pop_front();
if (current_node == nullptr) continue;
if (current_node->value->active)
{
const auto distance = (current_node->value->position - point).sqrLength();
if (distance < closest_distance)
{
closest_distance = distance;
closest_tank = current_node->value;
}
}
if (closest_distance == 0)
break;
float dx = current_node->value->position[current_node->axis] - point[current_node->axis];
nodes.push_back(dx > 0 ? current_node->left : current_node->right);
if (dx * dx >= closest_distance) continue;
nodes.push_back(dx > 0 ? current_node->right : current_node->left);
}
return closest_tank;
}
} // namespace Tmpl8