-
Notifications
You must be signed in to change notification settings - Fork 0
/
PointKDTree.h
212 lines (181 loc) · 4.66 KB
/
PointKDTree.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
210
211
212
#pragma once
#include "nvVector.h"
#include <vector>
#include <limits>
typedef float Real;
typedef nv::vec3f Vector3;
#define INF std::numeric_limits<float>::infinity()
struct KdNode
{
Real splitPos;
uint32_t splitAxis : 2;
uint32_t hasLeftChild : 1 , rightChild : 29;
void init(Real pos , uint32_t a)
{
splitPos = pos;
splitAxis = a;
rightChild = (1 << 29) - 1;
hasLeftChild = 0;
}
void initLeaf()
{
splitAxis = -1;
rightChild = (1 << 29) - 1;
hasLeftChild = 0;
}
};
template<typename NodeData>
class PointKDTree
{
public:
KdNode *nodes;
NodeData *nodeData;
uint32_t totNodes , nextFreeNode;
PointKDTree(const std::vector<NodeData>& data);
~PointKDTree()
{
delete[] nodes;
delete[] nodeData;
}
void buildTree(uint32_t nodeNum , int st , int ed ,
const NodeData **buildNodes);
template<typename Query>
void searchInRadius(uint32_t nodeNum , const Vector3& pos ,
Real radius , Query& query);
template<typename Query>
void searchKnn(uint32_t nodeNum , const Vector3& pos ,
Query& query);
};
template<typename NodeData>
PointKDTree<NodeData>::PointKDTree(const std::vector<NodeData>& data)
{
totNodes = data.size();
nextFreeNode = 1;
nodes = new KdNode[totNodes];
nodeData = new NodeData[totNodes];
std::vector<const NodeData*> buildNodes(totNodes , NULL);
for (uint32_t i = 0; i < totNodes; i++)
{
buildNodes[i] = &data[i];
}
buildTree(0 , 0 , totNodes , &buildNodes[0]);
}
template<typename NodeData>
struct CompareNode
{
int axis;
CompareNode(int axis) : axis(axis) {}
bool operator()(const NodeData *d1 , const NodeData *d2) const
{
return (d1->pos[axis] == d2->pos[axis]) ? (d1 < d2) :
(d1->pos[axis] < d2->pos[axis]);
}
};
template<typename NodeData>
void PointKDTree<NodeData>::buildTree(uint32_t nodeNum ,
int st , int ed , const NodeData **buildNodes)
{
if (st + 1 == ed)
{
nodes[nodeNum].initLeaf();
nodeData[nodeNum] = *buildNodes[st];
return;
}
Vector3 l(INF) , r(-INF);
for (int i = st; i < ed; i++)
{
l.x = std::min(l.x , buildNodes[i]->pos.x);
l.y = std::min(l.y , buildNodes[i]->pos.y);
l.z = std::min(l.z , buildNodes[i]->pos.z);
r.x = std::max(r.x , buildNodes[i]->pos.x);
r.y = std::max(r.y , buildNodes[i]->pos.y);
r.z = std::max(r.z , buildNodes[i]->pos.z);
}
Vector3 diag = r - l;
Real tmp = -INF;
int splitAxis = -1;
for (int i = 0; i <= 2; i++)
{
if (tmp < diag[i])
{
tmp = diag[i];
splitAxis = i;
}
}
int splitPos = (st + ed) / 2;
std::nth_element(&buildNodes[st] , &buildNodes[splitPos] ,
&buildNodes[ed] , CompareNode<NodeData>(splitAxis));
nodes[nodeNum].init(buildNodes[splitPos]->pos[splitAxis] , splitAxis);
nodeData[nodeNum] = *buildNodes[splitPos];
if (st < splitPos)
{
nodes[nodeNum].hasLeftChild = 1;
uint32_t childNum = nextFreeNode++;
buildTree(childNum , st , splitPos , buildNodes);
}
if (splitPos + 1 < ed)
{
nodes[nodeNum].rightChild = nextFreeNode++;
buildTree(nodes[nodeNum].rightChild , splitPos + 1 , ed , buildNodes);
}
}
template<typename NodeData>
template<typename Query>
void PointKDTree<NodeData>::searchInRadius(uint32_t nodeNum , const Vector3& pos ,
Real radius , Query& query)
{
KdNode *node = &nodes[nodeNum];
int axis = node->splitAxis;
Real delta = std::abs(pos[axis] - node->splitPos);
if (axis != -1)
{
if (pos[axis] <= node->splitPos)
{
if (node->hasLeftChild)
searchInRadius(nodeNum + 1 , pos , radius , query);
if (delta < radius && node->rightChild < totNodes)
searchInRadius(node->rightChild , pos , radius , query);
}
else
{
if (node->rightChild < totNodes)
searchInRadius(node->rightChild , pos , radius , query);
if (delta < radius && node->hasLeftChild)
searchInRadius(nodeNum + 1 , pos , radius , query);
}
}
Vector3 d = pos - nodeData[nodeNum].pos;
Real dis = d.length();
if (dis < radius)
query.process(nodeData[nodeNum]);
}
template<typename NodeData>
template<typename Query>
void PointKDTree<NodeData>::searchKnn(uint32_t nodeNum , const Vector3& pos ,
Query& query)
{
KdNode *node = &nodes[nodeNum];
int axis = node->splitAxis;
Real delta2 = powf(pos[axis] - node->splitPos, 2);
if (axis != -1)
{
if (pos[axis] <= node->splitPos)
{
if (node->hasLeftChild)
searchKnn(nodeNum + 1 , pos , query);
if (delta2 < query.maxSqrDis && node->rightChild < totNodes)
searchKnn(node->rightChild , pos , query);
}
else
{
if (node->rightChild < totNodes)
searchKnn(node->rightChild , pos , query);
if (delta2 < query.maxSqrDis && node->hasLeftChild)
searchKnn(nodeNum + 1 , pos , query);
}
}
Vector3 d = pos - nodeData[nodeNum].pos;
Real dist2 = powf(d.length(), 2);
if (dist2 < query.maxSqrDis)
query.process(&nodeData[nodeNum] , dist2);
}