forked from jjkorth/VisualVoronoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Site.cs
382 lines (341 loc) · 11.8 KB
/
Site.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
using System;
using System.Collections.Generic;
namespace VisualVoronoi
{
public class Site : ICoord
{
private static Stack<Site> pool = new Stack<Site>();
public Point coord;
public double weight;
private int index;
public List<Edge> edges;
private List<Point> reg;
private List<LR> edgeOrientations;
private static double EPSILON = 0.005;
public Site(Point p, int index, double weight)
{
Initialize(p, index, weight);
}
public static Site Create(Point p, int index, double weight)
{
if (pool.Count > 0)
return pool.Pop().Initialize(p, index, weight);
return new Site(p, index, weight);
}
public static void SortSites(List<Site> sites)
{
sites.Sort(new SiteComparer());
}
private static Boolean CloseEnough(Point p1, Point p2)
{
return Point.Distance(p1, p2) < EPSILON;
}
public static double Compare(Site s1, Site s2)
{
int rValue = CompareYThenX(s1, s2);
int tempI;
if (rValue == -1)
{
if (s1.index > s2.index)
{
tempI = s1.index;
s1.index = s2.index;
s2.index = tempI;
}
}
else if (rValue == 1)
{
if (s1.index < s2.index)
{
tempI = s2.index;
s2.index = s1.index;
s1.index = tempI;
}
}
return rValue;
}
private static int CompareYThenX(Site s1, Site s2)
{
if (s1.coord.y < s2.coord.y)
return -1;
if (s1.coord.y > s2.coord.y)
return 1;
if (s1.coord.x < s2.coord.x)
return -1;
if (s1.coord.x > s2.coord.x)
return 1;
return 0;
}
private Site Initialize(Point p, int index, double weight)
{
this.coord = p;
this.index = index;
this.weight = weight;
edges = new List<Edge>();
reg = null;
return this;
}
private void Move(Point p)
{
Clear();
coord = p;
}
private void Clear()
{
if (edges != null)
{
edges.Clear();
edges = null;
}
if (edgeOrientations != null)
{
edgeOrientations.Clear();
edgeOrientations = null;
}
if (reg != null)
{
reg.Clear();
reg = null;
}
}
public void AddEdge(Edge e)
{
edges.Add(e);
}
public Edge NearestEdge()
{
edges.Sort(new EdgeComparer());
return edges[0];
}
public List<Site> NeighborSites()
{
if (edges == null || edges.Count < 1)
return new List<Site>();
if (edgeOrientations == null)
ReorderEdges();
List<Site> list = new List<Site>();
foreach (Edge e in edges)
list.Add(NeighborSite(e));
return list;
}
private Site NeighborSite(Edge e)
{
if (this == e.GetLeftSite())
return e.GetRightSite();
if (this == e.GetRightSite())
return e.GetLeftSite();
return null;
}
public List<Point> Region(Rectangle bounds)
{
if (edges == null || edges.Count < 1)
return new List<Point>();
if (edgeOrientations == null)
{
ReorderEdges();
reg = ClipToBounds(bounds);
if ((new Polygon(reg)).GetWinding() == Winding.CLOCKWISE)
reg.Reverse();
}
return reg;
}
private void ReorderEdges()
{
EdgeReorderer er = new EdgeReorderer(edges, "v");
edges = er.GetEdges();
edgeOrientations = er.GetEdgeOrientations();
er.Dispose();
}
public void Dispose()
{
coord = null;
Clear();
pool.Push(this);
}
private List<Point> ClipToBounds(Rectangle bounds)
{
List<Point> points = new List<Point>();
int n = edges.Count;
int i = 0;
Edge e;
while (i < n && (edges[i].GetVisible() == false))
i++;
if (i == n)
return new List<Point>();
e = edges[i];
LR orient = edgeOrientations[i];
points.Add(e.GetClippedEnds()[orient]);
points.Add(e.GetClippedEnds()[LR.Other(orient)]);
for (int j = i + 1; j < n; j++)
{
e = edges[j];
if (e.GetVisible() == false)
continue;
Connect(points, j, bounds, false);
}
Connect(points, i, bounds, true);
return points;
}
private void Connect(List<Point> points, int j, Rectangle bounds, bool Closing)
{
Point rightPoint = points[points.Count - 1];
Edge nEdge = edges[j];
LR nOrient = edgeOrientations[j];
Point nPoint = nEdge.GetClippedEnds()[nOrient];
if (CloseEnough(rightPoint, nPoint))
{
if ((rightPoint.x != nPoint.x) && (rightPoint.y != nPoint.y))
{
int rightCheck = BoundsCheck.Check(rightPoint, bounds);
int nCheck = BoundsCheck.Check(nPoint, bounds);
double pX;
double pY;
if ((rightCheck & BoundsCheck.RIGHT) != 0)
{
pX = bounds.right;
if ((nCheck & BoundsCheck.BOTTOM) != 0)
{
pY = bounds.bottom;
points.Add(new Point(pX, pY));
}
else if ((nCheck & BoundsCheck.TOP) != 0)
{
pY = bounds.top;
points.Add(new Point(pX, pY));
}
else if ((nCheck & BoundsCheck.LEFT) != 0)
{
if (((rightPoint.y - bounds.y + nPoint.y) - bounds.y) < bounds.height)
{
pY = bounds.top;
}
else
{
pY = bounds.bottom;
}
points.Add(new Point(pX, pY));
points.Add(new Point(bounds.left, pY));
}
}
else if ((rightCheck & BoundsCheck.LEFT) != 0)
{
pX = bounds.left;
if ((nCheck & BoundsCheck.BOTTOM) != 0)
{
pY = bounds.bottom;
points.Add(new Point(pX, pY));
}
else if ((nCheck & BoundsCheck.TOP) != 0)
{
pY = bounds.top;
points.Add(new Point(pX, pY));
}
else if ((nCheck & BoundsCheck.RIGHT) != 0)
{
if (((rightPoint.y - bounds.y + nPoint.y) - bounds.y) < bounds.height)
pY = bounds.top;
else
pY = bounds.bottom;
points.Add(new Point(pX, pY));
points.Add(new Point(bounds.right, pY));
}
}
else if ((rightCheck & BoundsCheck.TOP) != 0)
{
pY = bounds.top;
if ((nCheck & BoundsCheck.RIGHT) != 0)
{
pX = bounds.right;
points.Add(new Point(pX, pY));
}
else if ((nCheck & BoundsCheck.LEFT) != 0)
{
pX = bounds.left;
points.Add(new Point(pX, pY));
}
else if ((nCheck & BoundsCheck.BOTTOM) != 0)
{
if (((rightPoint.x - bounds.x + nPoint.x) - bounds.x) < bounds.width)
pX = bounds.left;
else
pX = bounds.right;
points.Add(new Point(pX, pY));
points.Add(new Point(pX, bounds.bottom));
}
}
else if ((rightCheck & BoundsCheck.BOTTOM) != 0)
{
pY = bounds.bottom;
if ((nCheck & BoundsCheck.RIGHT) != 0)
{
pX = bounds.right;
points.Add(new Point(pX, pY));
}
else if ((nCheck & BoundsCheck.LEFT) != 0)
{
pX = bounds.left;
points.Add(new Point(pX, pY));
}
else if ((nCheck & BoundsCheck.TOP) != 0)
{
if (((rightPoint.x - bounds.x + nPoint.x) - bounds.x) < bounds.width)
pX = bounds.left;
else
pX = bounds.right;
points.Add(new Point(pX, pY));
points.Add(new Point(pX, bounds.top));
}
}
}
if (Closing)
return;
points.Add(nPoint);
}
Point nRightPoint = nEdge.GetClippedEnds()[LR.Other(nOrient)];
if (!CloseEnough(points[0], nRightPoint))
points.Add(nRightPoint);
}
public double Dist(ICoord p)
{
return Point.Distance(p.GetCoord(), this.coord);
}
public Point GetCoord()
{
return this.coord;
}
public class BoundsCheck
{
public const int TOP = 1;
public const int BOTTOM = 2;
public const int LEFT = 4;
public const int RIGHT = 8;
public static int Check(Point p, Rectangle b)
{
int value = 0;
if (p.x == b.left)
value |= LEFT;
if (p.x == b.right)
value |= RIGHT;
if (p.x == b.top)
value |= TOP;
if (p.x == b.bottom)
value |= BOTTOM;
return value;
}
}
private class SiteComparer : Comparer<Site>
{
public override int Compare(Site x, Site y)
{
return (int)Site.Compare(x, y);
}
}
private class EdgeComparer : Comparer<Edge>
{
public override int Compare(Edge x, Edge y)
{
return (int)Edge.CompareSitesDistances(x, y);
}
}
}
}