-
Notifications
You must be signed in to change notification settings - Fork 0
/
kdtree.h
189 lines (162 loc) · 6.05 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
//
// Created by janos on 16.06.20.
//
#ifndef KDTREE_KDTREE_H
#define KDTREE_KDTREE_H
#include <Magnum/Magnum.h>
#include <Magnum/Math/Vector3.h>
#include <Magnum/Math/Distance.h>
#include <Magnum/Math/Range.h>
#include <Magnum/Math/Functions.h>
#include <Magnum/Math/FunctionsBatch.h>
#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/StridedArrayView.h>
#include <algorithm>
#include <string>
using namespace Magnum;
using namespace Corrade;
enum class Side : UnsignedByte {
Left,
Right
};
template<Side side, UnsignedInt Size, class T>
Math::Range<Size, T> trim(
Math::Range<Size, T> range,
UnsignedInt dim,
typename Math::Range<Size,T>::VectorType const& p)
{
if constexpr(side == Side::Right) range.min()[dim] = p[dim];
else range.max()[dim] = p[dim];
return range;
}
template<class Vector>
typename Vector::Type bbDistanceSq(Vector const& p, Math::Range<Vector::Size, typename Vector::Type> const& range){
using Type = typename Vector::Type;
Type dsq{0};
for (int j = 0; j < Vector::Size; ++j){
auto lower = range.min()[j];
auto upper = range.max()[j];
if(p[j] < lower)
dsq += Math::pow(lower - p[j], Type{2.});
else if(p[j] > upper)
dsq += Math::pow(p[j] - upper, Type{2.});
}
return dsq;
}
template<class Vector>
class KDTree {
public:
struct Node {
constexpr static int32_t Invalid = -1;
int32_t leftChild;
int32_t rightChild;
int32_t pointIndex;
};
enum {
Size = Vector::Size
};
using Type = typename Vector::Type;
using Range = Math::Range<Size, Type>;
using NodeHandle = int32_t;
KDTree() = default;
explicit KDTree(Containers::StridedArrayView1D<const Vector> const& points) :
m_nodes(Containers::NoInit, points.size()),
m_points(points),
m_bb(Math::minmax(points))
{
Containers::Array<UnsignedInt> nodeIds(Containers::NoInit, points.size());
for (UnsignedInt i = 0; i < nodeIds.size(); ++i)
nodeIds[i] = i;
uint32_t size = 0;
m_root = constructTree(nodeIds.begin(), nodeIds.end(), 0, size);
}
auto& point(NodeHandle handle) const { return m_points[m_nodes[handle].pointIndex]; }
auto leftChild(NodeHandle handle) const { return m_nodes[handle].leftChild; }
auto rightChild(NodeHandle handle) const { return m_nodes[handle].rightChild; }
auto root() const { return m_root; }
auto bb() const { return m_bb; }
struct NNResult{
int pointIndex;
Type distanceSquared = Math::Constants<Type>::inf();
};
/**
* @param queryPoint the point for which to perform
* the nearest neighbor query.
* @return struct containing index corresponding to nearst neighbor and
* squared distance
*/
NNResult nearestNeighbor(Vector const& queryPoint){
NNResult result;
recurse(queryPoint, m_root, 0, m_bb, result);
return result;
}
private:
uint32_t constructTree(uint32_t* begin, uint32_t* end, uint32_t depth, uint32_t& size){
if(end <= begin) return Node::Invalid;
auto cd = depth % Size;
auto n = begin + (end - begin)/2;
std::nth_element(begin, n, end,
[&](uint32_t id1, uint32_t id2){ return m_points[id1][cd] < m_points[id2][cd]; });
auto handle = size++;
auto& node = m_nodes[handle];
node.pointIndex = *n;
node.leftChild = constructTree(begin, n, depth + 1, size);
node.rightChild = constructTree(n + 1, end, depth + 1, size);
return handle;
}
void recurse(Vector const& q, int nodeId, int cd, Range const& bb, NNResult& result){
if(nodeId == Node::Invalid) return;
auto const& node = m_nodes[nodeId];
auto const& p = m_points[node.pointIndex];
/* prune by computing distance to bounding box, @todo does this actually speed things up */
if(bbDistanceSq(q, bb) > result.distanceSquared) return;
auto distSq = (p - q).dot();
if(distSq < result.distanceSquared){
result.distanceSquared = distSq;
result.pointIndex = node.pointIndex;
}
auto nextCd = (cd+1) % Size;
if(q[cd] < p[cd]){ /* q is closer to left child */
recurse(q, node.leftChild, nextCd, trim<Side::Left>(bb, cd, p), result);
/* prune by computing distance to splitting plane */
if(p[cd] - q[cd] < result.distanceSquared)
recurse(q, node.rightChild, nextCd, trim<Side::Right>(bb, cd, p), result);
} else { /* q is closer to right child */
recurse(q, node.rightChild, nextCd, trim<Side::Right>(bb, cd, p), result);
if(q[cd] - p[cd] < result.distanceSquared)
recurse(q, node.leftChild, nextCd, trim<Side::Left>(bb, cd, p), result);
}
}
int m_root;
Range m_bb;
Containers::Array<Node> m_nodes;
Containers::StridedArrayView1D<const Vector> m_points;
};
/* deduction guide */
template<class Vector>
KDTree(Containers::Array<Vector> const& points) -> KDTree<Vector>;
template<class T>
void formatTree(
Magnum::Debug& debug,
std::string const& prefix,
KDTree<T> const& tree,
typename KDTree<T>::NodeHandle handle,
bool isLeft,
const char* arrow)
{
if(handle != KDTree<T>::Node::Invalid){
debug << prefix.c_str() << arrow << tree.point(handle) << "\n";
// enter the next tree level - left and right branch
const char* leftChildArrow = "├──";
if(tree.rightChild(handle) == KDTree<T>::Node::Invalid)
leftChildArrow = "└──";
formatTree(debug, prefix + (isLeft ? " │ " : " "), tree, tree.leftChild(handle), true, leftChildArrow);
formatTree(debug, prefix + (isLeft ? " │ " : " "), tree, tree.rightChild(handle), false, "└──");
}
}
template<class T>
Magnum::Debug& operator<<(Magnum::Debug& debug, KDTree<T> const& tree){
formatTree(debug, "", tree, tree.root(), false, "└──");
return debug;
}
#endif //KDTREE_KDTREE_H