-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKDTree.java
More file actions
249 lines (196 loc) · 5.63 KB
/
KDTree.java
File metadata and controls
249 lines (196 loc) · 5.63 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
class KDTree{
KDNode Root;
int TimeStart, TimeFinish;
int CounterFreq;
double d_min;
KDNode nearest_neighbour;
int KD_id;
int nList;
KDNode CheckedNodes[];
int checked_nodes;
KDNode List[];
double pt_min[], pt_max[];
boolean max_boundary[], min_boundary[];
int n_boundary;
double[] from;
double[] to;
/**
* Constructor
* @param i
*/
public KDTree(int i){
Root = null;
KD_id = 1;
nList = 0;
List = new KDNode[i];
CheckedNodes = new KDNode[i];
max_boundary = new boolean[3];
min_boundary = new boolean[3];
pt_min = new double[3];
pt_max = new double[3];
}
/**
* Add a new node
*/
public boolean add(double[] x){
if (nList >= 2000000 - 1)
return false; // can't add more points
if (Root == null) {
Root = new KDNode(x, 0);
Root.id = KD_id++;
List[nList++] = Root;
}
else{
KDNode pNode;
if ((pNode = Root.Insert(x)) != null){
pNode.id = KD_id++;
List[nList++] = pNode;
}
}
return true;
}
/**
* Caculate distance between two nodes
*/
double calcDistance(double[] pt1, double[] pt2){
double d = 0;
for (int i = 0; i < 3; i++)
d += (pt1[i] - pt2[i]) * (pt1[i] - pt2[i]);
return d;
}
/**
* Given a node, find the nearest neighbour
* @param x
* @return
*/
public KDNode find_nearest(double[] x){
if (Root == null)
return null;
checked_nodes = 0;
KDNode parent = Root.FindParent(x);
nearest_neighbour = parent;
d_min = calcDistance(x, parent.point);
if (parent.equal(x, parent.point) == true)
return nearest_neighbour;
search_parent(parent, x);
uncheck();
return nearest_neighbour;
}
public void check_subtree(KDNode node, double[] x){
if ((node == null) || node.checked)
return;
CheckedNodes[checked_nodes++] = node;
node.checked = true;
set_bounding_cube(node, x);
int dim = node.axis;
double d = node.point[dim] - x[dim];
if (d * d > d_min){
if (node.point[dim] > x[dim])
check_subtree(node.Left, x);
else
check_subtree(node.Right, x);
}
else{
check_subtree(node.Left, x);
check_subtree(node.Right, x);
}
}
public void set_bounding_cube(KDNode node, double[] x){
if (node == null){
return;
}
int d = 0;
double dx;
for (int k = 0; k < 3; k++){
dx = node.point[k] - x[k];
if (dx > 0){
dx *= dx;
if (!max_boundary[k]){
if (dx > pt_max[k])
pt_max[k] = dx;
if (pt_max[k] > d_min){
max_boundary[k] = true;
n_boundary++;
}
}
}
else{
dx *= dx;
if (!min_boundary[k]){
if (dx > pt_min[k])
pt_min[k] = dx;
if (pt_min[k] > d_min){
min_boundary[k] = true;
n_boundary++;
}
}
}
d += dx;
if (d > d_min)
return;
}
if (d < d_min){
d_min = d;
nearest_neighbour = node;
}
}
public KDNode search_parent(KDNode parent, double[] pt){
for (int i = 0; i < 3; i++){
pt_min[i] = pt_max[i] = 0;
max_boundary[i] = min_boundary[i] = false; //
}
n_boundary = 0;
KDNode search_root = parent;
while (parent != null && (n_boundary != 3 * 3)){
check_subtree(parent, pt);
search_root = parent;
parent = parent.Parent;
}
return search_root;
}
public void uncheck(){
for (int n = 0; n < checked_nodes; n++)
CheckedNodes[n].checked = false;
}
/**
* Print tree using preorder traversal
*/
public void printTree(){
printTree(Root);
}
private void printTree(KDNode node){
if (node != null){
System.out.print("(" + node.point[0] + ", " + node.point[1] + ", "+ node.point[2] + ") ");
printTree(node.Left);
printTree(node.Right);
}
}
/**
* Search for a node
*/
public void search(double[] pt){
search(Root, pt);
}
private void search(KDNode root, double[] pt){
if (root != null){
search(root.Left, pt);
if (pt[0] == root.point[0] && pt[1] == root.point[1] && pt[2] == root.point[2])
System.out.print("True (" + root.point[0] + ", " + root.point[1] + ", "+ root.point[2] + ") ");
search(root.Right, pt);
}
}
public void searchBetween(double[] from, double[] to){
this.from = from;
this.to = to;
searchBetween(Root);
}
private void searchBetween(KDNode node){
if (node != null){
if(node.inRange(from, to)){
System.out.print("(" + node.point[0] + ", " + node.point[1] + ", "+ node.point[2] + ") ");
}
searchBetween(node.Left);
searchBetween(node.Right);
}
}
}