-
Notifications
You must be signed in to change notification settings - Fork 2
/
Edge.cs
324 lines (273 loc) · 7.81 KB
/
Edge.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
using System.Collections.Generic;
namespace VisualVoronoi
{
public class Edge
{
private static Stack<Edge> pool = new Stack<Edge>();
public static Edge DELETED = new Edge();
private Vertex leftVertex;
private Vertex rightVertex;
public double a;
public double b;
public double c;
private static int numEdges = 0;
private Dictionary<LR, Point> clippedVertices;
private Dictionary<LR, Site> sites;
private int index;
public static Edge CreateBisectingEdge(Site site0, Site site1)
{
double dX;
double dY;
double absdX;
double absdY;
double a;
double b;
double c;
dX = site1.coord.x - site0.coord.x;
dY = site1.coord.y - site0.coord.y;
absdX = dX > 0 ? dX : -dX;
absdY = dY > 0 ? dY : -dY;
c = (site0.coord.x * dX) + (site0.coord.y * dY) + (((dX * dX) + (dY * dY)) * 0.5);
if (absdX > absdY)
{
a = 1.0;
b = dY / dX;
c /= dX;
}
else
{
b = 1.0;
a = dX / dY;
c /= dY;
}
Edge e = Edge.Create();
e.SetLeftSite(site0);
e.SetRightSite(site1);
site0.AddEdge(e);
site1.AddEdge(e);
e.leftVertex = null;
e.rightVertex = null;
e.a = a;
e.b = b;
e.c = c;
return e;
}
private static Edge Create()
{
Edge e;
if (pool.Count > 0)
{
e = pool.Pop();
e.Initialize();
}
else
e = new Edge();
return e;
}
private Edge()
{
index = numEdges++;
Initialize();
}
private void Initialize()
{
sites = new Dictionary<LR, Site>();
}
public LineSegment DelaunayLine()
{
return new LineSegment(GetLeftSite().coord, GetRightSite().coord);
}
public LineSegment VoronoiEdge()
{
if (!GetVisible())
return new LineSegment(null, null);
return new LineSegment(clippedVertices[LR.LEFT], clippedVertices[LR.RIGHT]);
}
public Vertex GetLeftVertex()
{
return leftVertex;
}
public Vertex GetRightVertex()
{
return rightVertex;
}
public Vertex GetVertex(LR lr)
{
return (lr == LR.LEFT) ? leftVertex : rightVertex;
}
public void SetVertex(LR lr, Vertex v)
{
if (lr == LR.LEFT)
leftVertex = v;
else
rightVertex = v;
}
public bool IsPartOfConvexHull()
{
return (leftVertex == null || rightVertex == null);
}
public Dictionary<LR, Point> GetClippedEnds()
{
return clippedVertices;
}
public bool GetVisible()
{
return clippedVertices != null;
}
public double SitesDistance()
{
return Point.Distance(GetLeftSite().coord, GetRightSite().coord);
}
public static double CompareSitesDistances_MAX(Edge e0, Edge e1)
{
double l0 = e0.SitesDistance();
double l1 = e1.SitesDistance();
if (l0 < l1)
return 1;
if (l0 > l1)
return -1;
return 0;
}
public static double CompareSitesDistances(Edge e0, Edge e1)
{
return -CompareSitesDistances_MAX(e0, e1);
}
public Site GetLeftSite()
{
return sites[LR.LEFT];
}
public Site GetRightSite()
{
return sites[LR.RIGHT];
}
public void SetLeftSite(Site s)
{
sites[LR.LEFT] = s;
}
public void SetRightSite(Site s)
{
sites[LR.RIGHT] = s;
}
public Site Site(LR lr)
{
return sites[lr];
}
public void Dispose()
{
leftVertex = null;
rightVertex = null;
if (clippedVertices != null)
{
clippedVertices.Clear();
clippedVertices = null;
}
sites.Clear();
sites = null;
pool.Push(this);
}
public void ClipVertices(Rectangle bounds)
{
double xMin = bounds.x;
double yMin = bounds.y;
double xMax = bounds.right;
double yMax = bounds.bottom;
Vertex v0, v1;
double x0, x1, y0, y1;
if (a == 1.0 && b >= 0.0)
{
v0 = rightVertex;
v1 = leftVertex;
}
else
{
v0 = leftVertex;
v1 = rightVertex;
}
if (a == 1.0)
{
y0 = yMin;
if ((v0 != null) && (v0.GetCoord().y > yMin))
y0 = v0.GetCoord().y;
if (y0 > yMax)
return;
x0 = c - b * y0;
y1 = yMax;
if ((v1 != null) && (v1.GetCoord().y < yMax))
y1 = v1.GetCoord().y;
if (y1 < yMin)
return;
x1 = c - (b * y1);
if (((x0 > xMax) && (x1 > xMax)) || ((x0 < xMin) && (x1 < xMin)))
return;
if (x0 > xMax)
{
x0 = xMax;
y0 = (c - x0) / b;
}
else if (x0 < xMin)
{
x0 = xMin;
y0 = (c - x0) / b;
}
if (x1 > xMax)
{
x1 = xMax;
y1 = (c - x1) / b;
}
else if (x1 < xMin)
{
x1 = xMin;
y1 = (c - x1) / b;
}
}
else
{
x0 = xMin;
if (v0 != null && v0.GetCoord().x > xMin)
x0 = v0.GetCoord().x;
if (x0 > xMax)
return;
y0 = c - (a * x0);
x1 = xMax;
if ((v1 != null) && (v1.GetCoord().x < xMax))
x1 = v1.GetCoord().x;
if (x1 < xMin)
return;
y1 = c - (a * x1);
if (((y0 > yMax) && (y1 > yMax)) || ((y0 < yMin) && (y1 < yMin)))
return;
if (y0 > yMax)
{
y0 = yMax;
x0 = (c - y0) / a;
}
else if (y0 < yMin)
{
y0 = yMin;
x0 = (c - y0) / a;
}
if (y1 > yMax)
{
y1 = yMax;
x1 = (c - y1) / a;
}
else if (y1 < yMin)
{
y1 = yMin;
x1 = (c - y1) / a;
}
}
clippedVertices = new Dictionary<LR, Point>();
if (v0 == leftVertex)
{
clippedVertices[LR.LEFT] = new Point(x0, y0);
clippedVertices[LR.RIGHT] = new Point(x1, y1);
}
else
{
clippedVertices[LR.RIGHT] = new Point(x0, y0);
clippedVertices[LR.LEFT] = new Point(x1, y1);
}
}
}
}