-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkd.hpp
82 lines (73 loc) · 1.7 KB
/
kd.hpp
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
#ifndef KD_HPP
#define KD_HPP
#include <memory>
#include "mesh.hpp"
using std::shared_ptr;
using std::make_shared;
enum class KDAxis {KDX, KDY, KDZ};
class KDTreeBase {
public:
virtual void
radiusSearch(const Vector3f &ref, real r, std::vector<Point *> &ret) = 0;
virtual ~KDTreeBase() = default;
};
class KDTree {
shared_ptr<KDTreeBase> t;
public:
KDTree(shared_ptr<KDTreeBase> t) : t(t) {}
void radiusSearch(const Vector3f &ref, real r, std::vector<Point *> &ret) {
t->radiusSearch(ref, r, ret);
}
};
class KDTreeLeaf : public KDTreeBase {
Point *p;
public:
KDTreeLeaf(Point *p) : p(p) {}
virtual void
radiusSearch(const Vector3f &ref, real r, std::vector<Point *> &ret) override {
if (distance(ref, *p) <= r) {
ret.push_back(p);
}
}
~KDTreeLeaf() = default;
};
class KDTreeNode : public KDTreeBase {
// <= and >
KDTree low;
KDTree hig;
KDAxis axis;
real coord;
public:
KDTreeNode(KDTree low, KDTree hig, KDAxis axis, real coord)
: low(low), hig(hig), axis(axis), coord(coord) {}
virtual void
radiusSearch(const Vector3f &ref, real r, std::vector<Point *> &ret) override {
real refc;
switch (axis) {
case KDAxis::KDX:
refc = ref.x;
break;
case KDAxis::KDY:
refc = ref.y;
break;
case KDAxis::KDZ:
refc = ref.z;
break;
}
if (refc <= coord) {
low.radiusSearch(ref, r, ret);
if (coord - refc < r) {
hig.radiusSearch(ref, r, ret);
}
} else {
hig.radiusSearch(ref, r, ret);
if (refc - coord <= r) {
low.radiusSearch(ref, r, ret);
}
}
}
~KDTreeNode() = default;
};
// pts can be modified
KDTree buildKDTree(std::vector<Point *> &pts);
#endif