-
Notifications
You must be signed in to change notification settings - Fork 0
/
Voronoi.cs
395 lines (339 loc) · 10.9 KB
/
Voronoi.cs
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
using System;
using System.Collections;
using System.Collections.Generic;
namespace WSMath.Delaunay {
public class Voronoi {
private SiteList sites;
private List<Triangle> triangles;
private List<Edge> edges;
public List<Edge> Edges {get{return edges;}}
// TODO generalize this so it doesn't have to be a rectangle;
// then we can make the fractal voronois-within-voronois
private Rectf plotBounds;
public Rectf PlotBounds {get{return plotBounds;}}
private Dictionary<Vector2f,Site> sitesIndexedByLocation;
public Dictionary<Vector2f,Site> SitesIndexedByLocation {get{return sitesIndexedByLocation;}}
private Random weigthDistributor;
public void Dispose() {
sites.Dispose();
sites = null;
foreach (Triangle t in triangles) {
t.Dispose();
}
triangles.Clear();
foreach (Edge e in edges) {
e.Dispose();
}
edges.Clear();
plotBounds = Rectf.zero;
sitesIndexedByLocation.Clear();
sitesIndexedByLocation = null;
}
public Voronoi(List<Vector2f> points, Rectf plotBounds) {
weigthDistributor = new Random();
Init(points,plotBounds);
}
public Voronoi(List<Vector2f> points, Rectf plotBounds, int lloydIterations) {
weigthDistributor = new Random();
Init(points,plotBounds);
LloydRelaxation(lloydIterations);
}
private void Init(List<Vector2f> points, Rectf plotBounds) {
sites = new SiteList();
sitesIndexedByLocation = new Dictionary<Vector2f, Site>();
AddSites(points);
this.plotBounds = plotBounds;
triangles = new List<Triangle>();
edges = new List<Edge>();
FortunesAlgorithm();
}
private void AddSites(List<Vector2f> points) {
for (int i = 0; i < points.Count; i++) {
AddSite(points[i], i);
}
}
private void AddSite(Vector2f p, int index) {
float weigth = (float)weigthDistributor.NextDouble() * 100;
Site site = Site.Create(p, index, weigth);
sites.Add(site);
sitesIndexedByLocation[p] = site;
}
public List<Vector2f> Region (Vector2f p) {
Site site;
if (sitesIndexedByLocation.TryGetValue(p, out site)) {
return site.Region(plotBounds);
} else {
return new List<Vector2f>();
}
}
public List<Vector2f> NeighborSitesForSite(Vector2f coord) {
List<Vector2f> points = new List<Vector2f>();
Site site;
if (sitesIndexedByLocation.TryGetValue(coord, out site)) {
List<Site> sites = site.NeighborSites();
foreach (Site neighbor in sites) {
points.Add(neighbor.Coord);
}
}
return points;
}
public List<WSMath.Geometry.Circle> Circles() {
return sites.Circles();
}
/*
public List<LineSegment> VoronoiBoundaryForSite(Vector2f coord) {
return LineSegment.VisibleLineSegments(Edge.SelectEdgesForSitePoint(coord, edges));
}
*/
/*
public List<LineSegment> DelaunayLinesForSite(Vector2f coord) {
return DelaunayLinesForEdges(Edge.SelectEdgesForSitePoint(coord, edges));
}*/
/*
public List<LineSegment> VoronoiDiagram() {
return LineSegment.VisibleLineSegments(edges);
}
*/
/*
public List<LineSegment> Hull() {
return DelaunayLinesForEdges(HullEdges());
}*/
public List<Edge> HullEdges() {
return edges.FindAll(edge=>edge.IsPartOfConvexHull());
}
public List<Vector2f> HullPointsInOrder() {
List<Edge> hullEdges = HullEdges();
List<Vector2f> points = new List<Vector2f>();
if (hullEdges.Count == 0) {
return points;
}
EdgeReorderer reorderer = new EdgeReorderer(hullEdges, typeof(Site));
hullEdges = reorderer.Edges;
List<LR> orientations = reorderer.EdgeOrientations;
reorderer.Dispose();
LR orientation;
for (int i = 0; i < hullEdges.Count; i++) {
Edge edge = hullEdges[i];
orientation = orientations[i];
points.Add(edge.Site(orientation).Coord);
}
return points;
}
public List<List<Vector2f>> Regions() {
return sites.Regions(plotBounds);
}
public List<Vector2f> SiteCoords() {
return sites.SiteCoords();
}
private void FortunesAlgorithm() {
Site newSite, bottomSite, topSite, tempSite;
Vertex v, vertex;
Vector2f newIntStar = Vector2f.zero;
LR leftRight;
Halfedge lbnd, rbnd, llbnd, rrbnd, bisector;
Edge edge;
Rectf dataBounds = sites.GetSitesBounds();
int sqrtSitesNb = (int)Math.Sqrt(sites.Count() + 4);
HalfedgePriorityQueue heap = new HalfedgePriorityQueue(dataBounds.y, dataBounds.height, sqrtSitesNb);
EdgeList edgeList = new EdgeList(dataBounds.x, dataBounds.width, sqrtSitesNb);
List<Halfedge> halfEdges = new List<Halfedge>();
List<Vertex> vertices = new List<Vertex>();
Site bottomMostSite = sites.Next();
newSite = sites.Next();
while (true) {
if (!heap.Empty()) {
newIntStar = heap.Min();
}
if (newSite != null &&
(heap.Empty() || CompareByYThenX(newSite, newIntStar) < 0)) {
// New site is smallest
//Debug.Log("smallest: new site " + newSite);
// Step 8:
lbnd = edgeList.EdgeListLeftNeighbor(newSite.Coord); // The halfedge just to the left of newSite
//UnityEngine.Debug.Log("lbnd: " + lbnd);
rbnd = lbnd.edgeListRightNeighbor; // The halfedge just to the right
//UnityEngine.Debug.Log("rbnd: " + rbnd);
bottomSite = RightRegion(lbnd, bottomMostSite); // This is the same as leftRegion(rbnd)
// This Site determines the region containing the new site
//UnityEngine.Debug.Log("new Site is in region of existing site: " + bottomSite);
// Step 9
edge = Edge.CreateBisectingEdge(bottomSite, newSite);
//UnityEngine.Debug.Log("new edge: " + edge);
edges.Add(edge);
bisector = Halfedge.Create(edge, LR.LEFT);
halfEdges.Add(bisector);
// Inserting two halfedges into edgelist constitutes Step 10:
// Insert bisector to the right of lbnd:
edgeList.Insert(lbnd, bisector);
// First half of Step 11:
if ((vertex = Vertex.Intersect(lbnd, bisector)) != null) {
vertices.Add(vertex);
heap.Remove(lbnd);
lbnd.vertex = vertex;
lbnd.ystar = vertex.y + newSite.Dist(vertex);
heap.Insert(lbnd);
}
lbnd = bisector;
bisector = Halfedge.Create(edge, LR.RIGHT);
halfEdges.Add(bisector);
// Second halfedge for Step 10::
// Insert bisector to the right of lbnd:
edgeList.Insert(lbnd, bisector);
// Second half of Step 11:
if ((vertex = Vertex.Intersect(bisector, rbnd)) != null) {
vertices.Add(vertex);
bisector.vertex = vertex;
bisector.ystar = vertex.y + newSite.Dist(vertex);
heap.Insert(bisector);
}
newSite = sites.Next();
} else if (!heap.Empty()) {
// Intersection is smallest
lbnd = heap.ExtractMin();
llbnd = lbnd.edgeListLeftNeighbor;
rbnd = lbnd.edgeListRightNeighbor;
rrbnd = rbnd.edgeListRightNeighbor;
bottomSite = LeftRegion(lbnd, bottomMostSite);
topSite = RightRegion(rbnd, bottomMostSite);
// These three sites define a Delaunay triangle
// (not actually using these for anything...)
// triangles.Add(new Triangle(bottomSite, topSite, RightRegion(lbnd, bottomMostSite)));
v = lbnd.vertex;
v.SetIndex();
lbnd.edge.SetVertex(lbnd.leftRight, v);
rbnd.edge.SetVertex(rbnd.leftRight, v);
edgeList.Remove(lbnd);
heap.Remove(rbnd);
edgeList.Remove(rbnd);
leftRight = LR.LEFT;
if (bottomSite.y > topSite.y) {
tempSite = bottomSite;
bottomSite = topSite;
topSite = tempSite;
leftRight = LR.RIGHT;
}
edge = Edge.CreateBisectingEdge(bottomSite, topSite);
edges.Add(edge);
bisector = Halfedge.Create(edge, leftRight);
halfEdges.Add(bisector);
edgeList.Insert(llbnd, bisector);
edge.SetVertex(LR.Other(leftRight), v);
if ((vertex = Vertex.Intersect(llbnd, bisector)) != null) {
vertices.Add(vertex);
heap.Remove(llbnd);
llbnd.vertex = vertex;
llbnd.ystar = vertex.y + bottomSite.Dist(vertex);
heap.Insert(llbnd);
}
if ((vertex = Vertex.Intersect(bisector, rrbnd)) != null) {
vertices.Add(vertex);
bisector.vertex = vertex;
bisector.ystar = vertex.y + bottomSite.Dist(vertex);
heap.Insert(bisector);
}
} else {
break;
}
}
// Heap should be empty now
heap.Dispose();
edgeList.Dispose();
foreach (Halfedge halfedge in halfEdges) {
halfedge.ReallyDispose();
}
halfEdges.Clear();
// we need the vertices to clip the edges
foreach (Edge e in edges) {
e.ClipVertices(plotBounds);
}
// But we don't actually ever use them again!
foreach (Vertex ve in vertices) {
ve.Dispose();
}
vertices.Clear();
}
public void LloydRelaxation(int nbIterations) {
// Reapeat the whole process for the number of iterations asked
for (int i = 0; i < nbIterations; i++) {
List<Vector2f> newPoints = new List<Vector2f>();
// Go thourgh all sites
sites.ResetListIndex();
Site site = sites.Next();
while (site != null) {
// Loop all corners of the site to calculate the centroid
List<Vector2f> region = site.Region(plotBounds);
if (region.Count < 1) {
site = sites.Next();
continue;
}
Vector2f centroid = Vector2f.zero;
float signedArea = 0;
float x0 = 0;
float y0 = 0;
float x1 = 0;
float y1 = 0;
float a = 0;
// For all vertices except last
for (int j = 0; j < region.Count-1; j++) {
x0 = region[j].x;
y0 = region[j].y;
x1 = region[j+1].x;
y1 = region[j+1].y;
a = x0*y1 - x1*y0;
signedArea += a;
centroid.x += (x0 + x1)*a;
centroid.y += (y0 + y1)*a;
}
// Do last vertex
x0 = region[region.Count-1].x;
y0 = region[region.Count-1].y;
x1 = region[0].x;
y1 = region[0].y;
a = x0*y1 - x1*y0;
signedArea += a;
centroid.x += (x0 + x1)*a;
centroid.y += (y0 + y1)*a;
signedArea *= 0.5f;
centroid.x /= (6*signedArea);
centroid.y /= (6*signedArea);
// Move site to the centroid of its Voronoi cell
newPoints.Add(centroid);
site = sites.Next();
}
// Between each replacement of the cendroid of the cell,
// we need to recompute Voronoi diagram:
Rectf origPlotBounds = this.plotBounds;
Dispose();
Init(newPoints,origPlotBounds);
}
}
private Site LeftRegion(Halfedge he, Site bottomMostSite) {
Edge edge = he.edge;
if (edge == null) {
return bottomMostSite;
}
return edge.Site(he.leftRight);
}
private Site RightRegion(Halfedge he, Site bottomMostSite) {
Edge edge = he.edge;
if (edge == null) {
return bottomMostSite;
}
return edge.Site(LR.Other(he.leftRight));
}
public static int CompareByYThenX(Site s1, Site s2) {
if (s1.y < s2.y) return -1;
if (s1.y > s2.y) return 1;
if (s1.x < s2.x) return -1;
if (s1.x > s2.x) return 1;
return 0;
}
public static int CompareByYThenX(Site s1, Vector2f s2) {
if (s1.y < s2.y) return -1;
if (s1.y > s2.y) return 1;
if (s1.x < s2.x) return -1;
if (s1.x > s2.x) return 1;
return 0;
}
}
}