-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.java
More file actions
185 lines (154 loc) · 5.68 KB
/
Tree.java
File metadata and controls
185 lines (154 loc) · 5.68 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
import java.util.ArrayList;
class Tree {
public Node root;
public int size;
double[] from;
double[] to;
Tree() {
size = 0;
}
public void add(double[] coord) {
if (coord == null) {
System.out.println("Coordinate null");
return;
}
root = add(root, coord, 0);
}
public Node add(Node current, double[] coord, int split) {
// add leaf.
if (current == null) {
size++;
return new Node(coord, current, split);
}
// int splitBy = current.parent.splitBy;
switch (split) {
case 0:
if (coord[0] < current.point[0]) {
// bound[0][1] = current.point[0];
current.left = add(current.left, coord, 1);
} else if (coord[0] > current.point[0]) {
// bound[0][0] = current.point[0];
current.right = add(current.right, coord, 1);
}
break;
case 1:
if (coord[1] < current.point[1]) {
//bound[1][1] = current.point[1];
current.left = add(current.left, coord, 2);
} else if (coord[1] > current.point[1]) {
//bound[1][0] = current.point[1];
current.right = add(current.right, coord, 2);
}
break;
case 2:
if (coord[2] < current.point[2]) {
//bound[2][1] = current.point[2];
current.left = add(current.left, coord, 0);
} else if (coord[2] > current.point[2]) {
//bound[2][0] = current.point[2];
current.right = add(current.right, coord, 0);
}
break;
}
return current;
}
/**
* Caculate distance between two nodes
*/
double calcDistance(double[] from, double[] to) {
double dist = 0;
for (int i = 0; i < 3; i++) {
dist += (from[i] - to[i]) * (from[i] - to[i]);
}
return dist;
}
/**
* Print tree using preorder traversal
*/
public void printTree() {
printTree(root);
}
private void printTree(Node node) {
if (node != null) {
System.out.print(node.toString());
printTree(node.left);
printTree(node.right);
}
}
public ArrayList<Node> searchBetween(double[] from, double[] to) {
this.from = from;
this.to = to;
double[][] rangeBound = new double[][] { { from[0], to[0] }, { from[1], to[1] }, { from[2], to[2] } };
double[][] nodeBound = new double[][] { { 0.00, 1000.00},{ 0.00, 1000.00},{ 0.00, 1000.00} };
ArrayList<Node> arrNode = new ArrayList<Node>();
searchBetween(root, rangeBound,nodeBound, arrNode);
return arrNode;
}
private void searchBetween( Node node, double[][] rangeBound,double[][] nodeBound, ArrayList<Node> arrNode){
if (node == null){
return;
}
//No need to search in tree if not intersection
if(!intersects(rangeBound, nodeBound)){
return;
}
double[][] nodeBoundL = new double[][]{{nodeBound[0][0],nodeBound[0][1]},{nodeBound[1][0],nodeBound[1][1]},{nodeBound[2][0],nodeBound[2][1]}};
double[][] nodeBoundR = new double[][]{{nodeBound[0][0],nodeBound[0][1]},{nodeBound[1][0],nodeBound[1][1]},{nodeBound[2][0],nodeBound[2][1]}};
if(node.inRange(from, to)){
//System.out.print("(" + node.point[0] + ", " + node.point[1] + ", "+ node.point[2] + ") ");
arrNode.add(node);
}
nodeBoundL[node.splitBy][1] = node.point[node.splitBy];
searchBetween(node.left, nodeBoundL, rangeBound, arrNode);
nodeBoundR[node.splitBy][0] = node.point[node.splitBy];
searchBetween(node.right, nodeBoundR, rangeBound, arrNode);
}
public double[] findNearest(double[] coord){
return findNearest(root, coord, root.point);
}
/**
* Recursively traverse tree. If the closest point discovered so far is closer than
* the distance between the query point and the cube corresponding to a node,
* there is no need to explore that node (or its subtrees). Only search a node if
* i may contain a point that is closer
*/
private double[] findNearest(Node node, double[] coord, double[] nearest){
if(node == null){
return nearest;
}
if(node.equal(coord)){
return coord;
}
double distance = calcDistance(nearest, coord);
if(calcDistance(node.point, coord) < calcDistance(nearest, coord)){
nearest = node.point;
}
if(coord[node.splitBy] < node.point[node.splitBy]){
nearest = findNearest(node.left, coord, nearest);
if(calcDistance(nearest, coord) >= distance){
nearest = findNearest(node.right, coord, nearest);
}
}else{
nearest = findNearest(node.right, coord, nearest);
if(calcDistance(nearest, coord) >= distance){
nearest = findNearest(node.left, coord, nearest);
}
}
return nearest;
}
/**
* Very if the bounding cube intersects
*/
private boolean intersects(double[][] bnd1, double[][] bnd2){
boolean intersct = false;
if(bnd1[0][1] >= bnd2[0][0]
&& bnd1[1][1] >= bnd2[1][0]
&& bnd1[2][1] >= bnd2[2][0]
&& bnd2[0][1] >= bnd1[0][0]
&& bnd2[1][1] >= bnd1[1][0]
&& bnd2[2][1] >= bnd1[2][0]){
intersct = true;
}
return intersct;
}
}