-
Notifications
You must be signed in to change notification settings - Fork 0
/
RangeSearch.java
147 lines (111 loc) · 3.68 KB
/
RangeSearch.java
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
package prj3;
import java.util.Arrays;
import java.util.Comparator;
/**
* The Range Search data structure
*
* @author enter your names here
*
*/
public class RangeSearch {
/**
* [x_1 y_1 z_1]; row(1) = p_1
* [x_2 y_2 z_2]; row(2) = p_2
* [ ... ]
* [ ... ]
* [ ... ]
* [x_n y_n z_n] row(n) = p_n
*/
private int[][] points;
public Point[] Px;
public Point[] Py;
public Point[] Pz;
BST x_tree;
public Node root;
private int n;
public KDTree kdtree1;
/**
* The constructor of the class
*
* @param points
* a 2 dimensional array, each row has the format [x,y,z], so the
* i-th point has x coordinate points[i-1][0], y coordinate
* points[i-1][1], and z coordinate points[i-1][2]
*/
public RangeSearch(int[][] points) {
this.points = points;
this.n = this.points.length;
Px = new Point[n]; // Array of Points. We will sort this by the x cooridnates. All the points will have the same entries but in different orders
Py = new Point[n]; // Array of Points. We will sort this by the y cooridnates.
Pz = new Point[n]; // Array of Points. We will sort this by the z cooridnates.
//Sorts by the x coordinates.
points = sortPointsBy(points, n, "x");
for(int i = 0; i < n; i++) { //Adds "Point" elements to Px.
Px[i] = new Point(points[i][0], points[i][1],points[i][2]);
}
//Sort Y Array
points = sortPointsBy(points, n, "y");
for(int i = 0; i < n; i++) {
Py[i] = new Point(points[i][0], points[i][1],points[i][2]);
}
//Sorts Z Array
points = sortPointsBy(points, n, "z");
for(int i = 0; i < n; i++) {
Pz[i] = new Point(points[i][0], points[i][1],points[i][2]);
}
//1-D Trees
x_tree = new BST(); //Sorted by x coordinates
x_tree.root = x_tree.buildTree(Pz); //Builds a 1-d Binary search tree sorted by the X coordinates
kdtree1 = new KDTree(Px);
}
/**
* Given the description of a rectangular prism in the 3 dimensional space,
* this method returns the number points that lie inside or on the boundary
* of the query range
*
* @param xMin
* queries[
* @param xMax
* @param yMin
* @param yMax
* @param zMin
* @param zMax
*
* @return
*/
public int query(
int xMin,
int xMax,
int yMin,
int yMax,
int zMin,
int zMax) {
Point plow = new Point(xMin, yMin, zMin);
Point phigh = new Point(xMax, yMax, zMax);
Prism queryRange = new Prism(plow, phigh);
// how to get this
return kdtree1.searchKdTree(kdtree1.root, queryRange);
}
/**
* Sorts the points Matrix based on "0" = "x".
* "1" = "Y"
* "2" = "Z"
*
* @param coords
* @param n
* @param sortBy
* @return
*/
public int[][] sortPointsBy(int[][] coords, int n, String sortBy) {
if(sortBy == "x") {
Arrays.sort(coords, Comparator.comparingDouble(o -> o[0]));
} else if(sortBy == "y") {
Arrays.sort(coords, Comparator.comparingDouble(o -> o[1]));
} else if(sortBy == "z") {
Arrays.sort(coords, Comparator.comparingDouble(o -> o[2]));
} else {
return null; // The String sortBy must be "x" "y" or "z"
}
return coords;
}
}