-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathkdtree.h
209 lines (195 loc) · 7.37 KB
/
kdtree.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#ifndef KDTREE_H_
#define KDTREE_H_
#include <memory>
#include <limits>
#include <queue>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
namespace spatial_index {
namespace util {
// Some compile time recursion in order to get a dimension dynamically
template <typename Point, std::size_t Dimension, std::size_t Count>
struct dimension_extractor {
static inline typename boost::geometry::default_distance_result<Point>::type subtract(const Point &p1, const Point &p2, std::size_t dim) {
if (Dimension == dim) {
return boost::geometry::get<Dimension>(p1) - boost::geometry::get<Dimension>(p2);
}
return dimension_extractor<Point, Dimension + 1, Count>::subtract(p1, p2, dim);
}
};
// end recursion
template <typename Point, std::size_t Count>
struct dimension_extractor<Point, Count, Count> {
static inline typename boost::geometry::default_distance_result<Point>::type subtract(const Point &p1, const Point &p2, std::size_t dim) {
}
};
template <typename Point>
typename boost::geometry::default_distance_result<Point>::type subtract(const Point &p1, const Point &p2, std::size_t dim) {
return dimension_extractor<Point, 0, boost::geometry::dimension<Point>::type::value>::subtract(p1, p2, dim);
}
} // namespace util
template <typename Data, typename Point = boost::geometry::model::d2::point_xy<double>>
class kdtree {
public:
kdtree() {}
virtual ~kdtree() {}
void add(const Point *point, const Data *data) {
typename kdnode::ptr node = std::make_shared<kdnode>(point, data);
m_nodes.push_back(node);
}
void build() {
if (m_nodes.empty()) {
return;
}
m_root = build(m_nodes, 0);
}
void clear() {
m_root.reset();
m_nodes.clear();
}
const Data *nearest_recursive(const Point &query) const {
if (!m_root) {
return NULL;
}
best_match best(m_root, std::numeric_limits<double>::max());
nearest(query, m_root, best);
return best.node->data;
}
void knearest(const Point &query, size_t k, std::vector<const Data*> &result) const {
if (!m_root || k < 1) {
return;
}
MaxPriorityQueue tmp;
knearest(query, m_root, k, tmp);
size_t size = tmp.size();
result.resize(size);
for (size_t i = 0; i < size; i++) {
// Reverse order
result[size - i - 1] = tmp.top().second->data;
tmp.pop();
}
}
const Data *nearest_iterative(const Point &query) const {
if (!m_root) {
return NULL;
}
MinPriorityQueue pq;
best_match best(m_root, std::numeric_limits<double>::max());
pq.push(DistanceTuple(0, m_root));
while (!pq.empty()) {
const auto current = pq.top();
if (current.first >= best.distance) {
return best.node->data;
}
pq.pop();
auto currentNode = current.second;
double d = boost::geometry::comparable_distance(query, *currentNode->split); // no sqrt
double dx = util::subtract(query, *currentNode->split, currentNode->axis);
if (d < best.distance) {
best.node = currentNode;
best.distance = d;
}
node_ptr near = dx <= 0 ? currentNode->left : currentNode->right;
node_ptr far = dx <= 0 ? currentNode->right : currentNode->left;
if (far) pq.push(DistanceTuple(dx * dx, far));
if (near) pq.push(DistanceTuple(0, near));
}
return best.node->data;
}
private:
struct kdnode {
typedef std::shared_ptr<kdnode> ptr;
ptr left;
ptr right;
int axis;
const Point *split;
const Data *data;
kdnode(const Point *g, const Data *d) : axis(0), split(g), data(d) {}
};
typedef typename kdnode::ptr node_ptr; // get rid of annoying typename
typedef std::vector<node_ptr> Nodes;
typedef std::pair<double, node_ptr> DistanceTuple;
struct SmallestOnTop {
bool operator()(const DistanceTuple &a, const DistanceTuple &b) const {
return a.first > b.first;
}
};
struct LargestOnTop {
bool operator()(const DistanceTuple &a, const DistanceTuple &b) const {
return a.first < b.first;
}
};
typedef std::priority_queue<DistanceTuple, std::vector<DistanceTuple>, SmallestOnTop> MinPriorityQueue;
typedef std::priority_queue<DistanceTuple, std::vector<DistanceTuple>, LargestOnTop> MaxPriorityQueue;
Nodes m_nodes;
node_ptr m_root;
template<typename NODE_TYPE>
struct Sort : std::binary_function<NODE_TYPE, NODE_TYPE, bool> {
Sort(std::size_t dim) : m_dimension(dim) {}
bool operator()(const NODE_TYPE &lhs, const NODE_TYPE &rhs) const {
return util::subtract(*lhs->split, *rhs->split, m_dimension) < 0;
}
std::size_t m_dimension;
};
struct best_match {
node_ptr node;
double distance;
best_match(const node_ptr &n, double d) : node(n), distance(d) {}
};
node_ptr build(Nodes &nodes, int depth) {
if (nodes.empty()) {
return node_ptr();
}
int axis = depth % boost::geometry::dimension<Point>();
size_t median = nodes.size() / 2;
std::nth_element(nodes.begin(), nodes.begin() + median, nodes.end(), Sort<node_ptr>(axis));
node_ptr node = nodes.at(median);
node->axis = axis;
Nodes left(nodes.begin(), nodes.begin() + median);
Nodes right(nodes.begin() + median + 1, nodes.end());
node->left = build(left, depth + 1);
node->right = build(right, depth + 1);
return node;
}
static void nearest(const Point &query, const node_ptr ¤tNode, best_match &best) {
if (!currentNode) {
return;
}
double d = boost::geometry::comparable_distance(query, *currentNode->split); // no sqrt
double dx = util::subtract(query, *currentNode->split, currentNode->axis);
if (d < best.distance) {
best.node = currentNode;
best.distance = d;
}
node_ptr near = dx <= 0 ? currentNode->left : currentNode->right;
node_ptr far = dx <= 0 ? currentNode->right : currentNode->left;
nearest(query, near, best);
if ((dx * dx) >= best.distance) {
return;
}
nearest(query, far, best);
}
template <typename PriorityQueue>
static void knearest(const Point &query, const node_ptr ¤tNode, size_t k, PriorityQueue &result) {
if (!currentNode) {
return;
}
double d = boost::geometry::comparable_distance(query, *currentNode->split); // no sqrt
double dx = util::subtract(query, *currentNode->split, currentNode->axis);
if (result.size() < k or d <= result.top().first) {
result.push(DistanceTuple(d, currentNode));
if (result.size() > k) {
result.pop();
}
}
node_ptr near = dx <= 0 ? currentNode->left : currentNode->right;
node_ptr far = dx <= 0 ? currentNode->right : currentNode->left;
knearest(query, near, k, result);
if ((dx * dx) >= result.top().first) {
return;
}
knearest(query, far, k, result);
}
};
} // namespace spatial_index
#endif /* KDTREE_H_ */