-
Notifications
You must be signed in to change notification settings - Fork 0
/
PartialVoronoi.java
222 lines (194 loc) · 7.95 KB
/
PartialVoronoi.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
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
package mass;
import edu.uw.bothell.css.dsl.MASS.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;
import java.util.List;
import java.awt.geom.Point2D.Double;
/**
*
* @author seane
*/
public class PartialVoronoi extends Place {
//this class contains:
// point_set: set of points in this partial voronoi diagram
// edge_list: set of voronoi edges in this PVD
private class EdgeList {
private ArrayList<Edge> list;
public EdgeList() {
list= new ArrayList<Edge>();
}
public void addToList(Edge e) {
list.add(e);
}
}
private ArrayList<Double> point_set;
private List<Double> convex_hull;
private EdgeList voronoi_edges;
Vector<int[]> neighbors;
public static final int COMPUTE_VORONOI= 0;
public static final int MERGE_VORONOI= 1;
public static final int GET_HOSTNAME= 2;
public static final int FILL_NEIGHBOR_BUFFER= 3;
public PartialVoronoi(Object o) {
point_set= new ArrayList<Double>();
voronoi_edges= new EdgeList();
init_neighbors();
Object[] temp= point_set.toArray();
convex_hull= ConvexHull.convexHull((Double[]) temp, point_set.size());
}
private PartialVoronoi(ArrayList<Double> points, EdgeList e) {
point_set= points;
voronoi_edges= e;
init_neighbors();
Object[] temp= point_set.toArray();
convex_hull= ConvexHull.convexHull((Double[]) temp, point_set.size());
}
//Places instantiatiion
public PartialVoronoi(ArrayList<ArrayList<Double>> points) {
//retrieve corresponding arraylist of points
point_set= points.get(getIndex()[0]);
init_neighbors(); //specigfy before exchagne all
Object[] temp= point_set.toArray();
convex_hull= ConvexHull.convexHull((Double[]) temp, point_set.size());
}
private void init_neighbors() {
double idx= Math.ceil(Math.log10(getIndex()[0]) / Math.log10(2));
if(idx != Math.floor(idx)) return; //idx not a power of 2: has no neighbors
neighbors= new Vector<int[]>();
int[] nbr= new int[1];
for(int i= getIndex()[0]+1; i < main.NUM_PLACES; i*=2) {
nbr[0]= i;
neighbors.add(nbr);
}
}
public Object callMethod(int method, Object o) {
switch(method) {
case COMPUTE_VORONOI:
return compute_voronoi(o);
case MERGE_VORONOI:
return merge_voronoi_wrapper(o);
case GET_HOSTNAME:
return null;
case FILL_NEIGHBOR_BUFFER:
return getNeighborPartialVoronoi();
default:
return new String("Error in callMethod");
}
}
private ArrayList<ArrayList<Double>> cut_in_half(ArrayList<Double> p) {
ArrayList<ArrayList<Double>> ret= new ArrayList<ArrayList<Double>>();
int half= p.size() / 2;
Iterator<Double> it= p.iterator();
ret.add(new ArrayList<Double>());
ret.add(new ArrayList<Double>());
for(int i= 0; i < half; ++i) ret.get(0).add(p.get(i));
for(int i= half; i < p.size(); ++i) ret.get(1).add(p.get(i));
return ret;
}
private PartialVoronoi bisect(ArrayList<Double> p) {
double x1= p.get(0).x;
double y1= p.get(0).y;
double x2= p.get(1).x;
double y2= p.get(1).y;
EdgeList edge_list= new EdgeList();
edge_list.addToList(new Edge(-1/(y2-y1)/(x2-x1), new Double((x2-x1)/2, (y2-y1)/2)));
return new PartialVoronoi(p, edge_list);
}
private Edge bisect(Edge line) {
Double left= line.getLeftPoint();
Double right= line.getRightPoint();
return new Edge(-1/(right.getY()-left.getY())/(right.getX()-left.getX()),
new Double(right.getX()-left.getX()/2, (right.getY()-left.getY())/2));
}
//compute voronio map for a given set of points
//given a PointSet with n points, this function returns an EdgeList object holding n-1 edges
private PartialVoronoi compute_voronoi(Object pointset) {
ArrayList<Double> ps= (ArrayList<Double>) pointset;
if(ps.size() == 2 || ps.size() == 1) return bisect(ps);
if(ps.size() < 1) return null;
ArrayList<ArrayList<Double>> splitPoints= cut_in_half(ps);
ArrayList<Double> s_left= splitPoints.get(0);
ArrayList<Double> s_right= splitPoints.get(1);
PartialVoronoi v_left= compute_voronoi(s_left);
PartialVoronoi v_right= compute_voronoi(s_right);
return merge_voronoi(v_left, v_right);
}
private List<Double> get_convex_hull() {
return convex_hull;
}
private Edge compute_support_line(List<Double> hull1, List<Double> hull2) {
int l_idx= hull1.size()-1;
int r_idx= 0;
Double u= hull1.get(l_idx);
Double v= hull2.get(r_idx);
Edge ret= new Edge(u,v);
boolean found_left= false, found_right= false;
while(!found_left && !found_right) {
Double test_left= hull1.get(l_idx--);
Double test_right= hull2.get(r_idx++);
if(test_left.getY() < ret.getLeftPoint().getY() || test_left.getY() < ret.getRightPoint().getY()) {
ret.setPoints(test_left, v);
found_left= false;
}
else found_left= true;
if(test_right.getY() < ret.getLeftPoint().getY() || test_right.getY() < ret.getRightPoint().getY()) {
ret.setPoints(u, test_right);
found_right= false;
}
else found_right = true;
}
return ret;
}
private Double intersection(Edge bisector) {
//point of intersection between this voronoi edges and bisector
return null;
}
//merge 2 voronoi maps
private PartialVoronoi merge_voronoi(PartialVoronoi v1, PartialVoronoi v2) {
List<Double> cvh_lhs= v1.get_convex_hull();
List<Double> cvh_rhs= v2.get_convex_hull();
Edge csl= compute_support_line(cvh_lhs, cvh_rhs);
Double p= csl.getLeftPoint();
Double q= csl.getRightPoint();
Edge bisector= new Edge();
EdgeList stichLine= new EdgeList();
while(csl != null) {
bisector= bisect(csl);
Double sl_intersection= v1.intersection(bisector); //bisector intersection with vl
Double sr_intersection= v2.intersection(bisector); //bisector intersection with v2
if(sl_intersection.y < sr_intersection.y) { //intersected with v1
p= sl_intersection;
}
else { //intersected with v2
q= sr_intersection;
}
stichLine.addToList(bisector); //todo: cut off bisector at intersection point
csl.setPoints(p,q);
}
return trim(v1, v2, stichLine);
}
private PartialVoronoi merge_voronoi_wrapper(Object global_step) {
int step= (int) global_step;
PartialVoronoi rhs= null;
PartialVoronoi[] neighbor= (PartialVoronoi[]) getInMessages();
if(neighbor != null) {
if(neighbor[0] != null) { //retrieve next PartialVoronoi to merge
rhs= neighbor[0];
}
}
else return null; //node is exhausted (no remaining neighbors)
return merge_voronoi(this, rhs);
}
private PartialVoronoi getNeighborPartialVoronoi() {
return this;
}
private PartialVoronoi trim(PartialVoronoi v1, PartialVoronoi v2, EdgeList stichLine) {
return this;
}
}
//trim
//intersection