-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
420 lines (340 loc) · 10.8 KB
/
main.cpp
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include "Voronoi.h"
#include <raylib.h>
#include <vector>
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const Color EDGE_COLOR = WHITE;
const Color RAY_COLOR = YELLOW;
const Color ARC_COLOR = YELLOW;
const Color SITE_COLOR = GetColor(0x990000FF);
const Color VERTEX_COLOR = DARKGREEN;
const Color BG_COLOR = GetColor(0x0c0c0cFF);
const Color SWEEP_COLOR = YELLOW;
float flipY(float y)
{
return SCREEN_HEIGHT - 1 - y;
}
Vector2 convertToScreenSpace(position2D pos)
{
return { pos.x, (SCREEN_HEIGHT - 1 - pos.y) };
}
void DrawEdge(Edge *e)
{
DrawLineV(convertToScreenSpace(e->start->pos), convertToScreenSpace(e->end->pos), EDGE_COLOR);
}
void DrawSite(Vertex *s)
{
float radius = 7.0f;
DrawCircleV(convertToScreenSpace(s->pos), radius, SITE_COLOR);
}
void DrawVertex(Vertex *v)
{
float radius = 6.0f;
DrawCircleV(convertToScreenSpace(v->pos), radius, VERTEX_COLOR);
}
//Draws an arc of BST between a given Monotonic X Range
void DrawParabola(Vertex *site, float directrixY, float minX, float maxX)
{
int i_minX = (int)minX;
int i_maxX = (int)maxX;
int n = i_maxX - i_minX + 1;
if (n < 1)
return;
std::vector<position2D> curvePts(n, position2D());
float x = minX;
for (int i = 0; i < n; i++)
{
curvePts[i] = { x, GetArcYForXCoord(site, x, directrixY) };
x += 1.0f;
}
for (int i = 1; i < n; i++)
{
DrawLineV(convertToScreenSpace(curvePts[i - 1]), convertToScreenSpace(curvePts[i]), ARC_COLOR);
}
}
void DrawRay(HalfEdge &ray, position2D &end)
{
DrawLineV(convertToScreenSpace(ray.orig), convertToScreenSpace(end), RAY_COLOR);
}
void DrawBeachLineBST(BeachLineBSTNode *curr, float sweepLineY)
{
if (!curr)
return;
//Arc
if (curr->isLeaf)
{
BeachLineBSTNode *leftBreakPt = nullptr;
BeachLineBSTNode *rightBreakPt = nullptr;
GetLeftAndRightBreakPoints(curr, leftBreakPt, rightBreakPt);
float minX = 0;
float maxX = SCREEN_WIDTH;
//leftBreakPt will be cached
if (leftBreakPt)
{
minX = max(leftBreakPt->cachedBreakPointPos.x, minX);
}
//RightBreakPt pos has to be calculated
if (rightBreakPt)
{
position2D intersectionPtThisFrame;
GetParabolaRayIntersection(rightBreakPt->ray, curr->site, sweepLineY, intersectionPtThisFrame);
rightBreakPt->cachedBreakPointPos = intersectionPtThisFrame;
maxX = min(intersectionPtThisFrame.x, maxX);
}
DrawParabola(curr->site, sweepLineY, minX, maxX);
return;
}
else//Edge
{
//Inorder Traversal
DrawBeachLineBST(curr->GetLeftChild(), sweepLineY);
//Use cached pt
DrawRay(curr->ray, curr->cachedBreakPointPos);
DrawBeachLineBST(curr->GetRightChild(), sweepLineY);
}
}
void RenderVoronoi(VoronoiOutput &out, BeachLineBSTNode *root, float sweepLineY)
{
//1: Draw Sites
for (int i = 0; i < out.sites.size(); ++i)
{
DrawSite(out.sites[i]);
}
//2 : Draw Completed Edges
for (int i = 0; i < out.edges.size(); ++i)
{
DrawEdge(out.edges[i]);
}
//3: Draw BeachLine BST (Arcs, Rays under construction)
DrawBeachLineBST(root, sweepLineY);
for (int i = 0; i < out.vertices.size(); ++i)
{
DrawVertex(out.vertices[i]);
}
}
void VoronoiDemo()
{
std::vector<position2D> sites;
int testCase = 0;
switch (testCase)
{
case 0:
// Example case: The points shown in the GIF of Fortune's algorithm on wikipedia
sites.push_back({ 155, 552 });
sites.push_back({ 405, 552 });
sites.push_back({ 624, 463 });
sites.push_back({ 211, 419 });
sites.push_back({ 458, 358 });
sites.push_back({ 673, 299 });
sites.push_back({ 261, 278 });
sites.push_back({ 88, 196 });
sites.push_back({ 497, 177 });
sites.push_back({ 715, 118 });
sites.push_back({ 275, 99 });
break;
case 1:
// Test case 1: Points with equal x
sites.push_back({ 300, 300 });
sites.push_back({ 300, 400 });
sites.push_back({ 400, 350 });
break;
case 2:
// Test case 2: Points with equal y (where those points are not the first points)
sites.push_back({ 300, 300 });
sites.push_back({ 200, 200 });
sites.push_back({ 400, 200 });
break;
case 3: //fix by FLT_MAX causes floating point issues
// Test case 3: Points with equal y (where those points are the first points)
// With a third point that is slightly off to one side.
// Requires a special case for the first points to prevent errors in finding the replacedarc
sites.push_back({ 320, 200 });
sites.push_back({ 200, 300 });
sites.push_back({ 400, 300 });
break;
case 31://CheckRayIntersection last case
// Test case 3a: Points with equal y (where those points are the first points).
// With a third point that exactly lines up with the edge between the first 2.
// Requires the special case for edges that intersect at both of their starting points (they should not be counted as intersecting).
sites.push_back({ 300, 200 });
sites.push_back({ 200, 300 });
sites.push_back({ 400, 300 });
break;
case 32: // Requires correct way to compare items in Event PQ
// Test case 3b: 3 points with equal y (and nothing else)
sites.push_back({ 300, 300 });
sites.push_back({ 200, 300 });
sites.push_back({ 400, 300 });
sites.push_back({ 500, 300 });
break;
case 4:
// Test case 4: A completely-surrounded site
sites.push_back({ 100, 100 });
sites.push_back({ 500, 150 });
sites.push_back({ 300, 300 });
sites.push_back({ 100, 550 });
sites.push_back({ 500, 500 });
break;
case 5:
// Test case 5: An arc gets squeezed by a later-created arc before it would be squeezed by its original edges.
// Requires handling of events that get "pre-empted" and are no longer required by the time they would execute.
sites.push_back({ 300, 500 });
sites.push_back({ 200, 450 });
sites.push_back({ 400, 450 });
sites.push_back({ 300, 400 });
break;
default:
break;
}
Voronoi *vor = new Voronoi();
VoronoiOutput out = vor->generateVoronoi(sites, 0, SCREEN_WIDTH, 0, SCREEN_HEIGHT);
#ifdef DEBUG
std::cout << "# SITES " << out.sites.size() << std::endl;
std::cout << "# VERTICES " << out.vertices.size() << std::endl;
std::cout << "# EDGES " << out.edges.size() << std::endl;
#endif
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Voronoi Diagram");
SetTargetFPS(60);
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
BeginDrawing();
ClearBackground(BG_COLOR);
for (int i = 0; i < out.edges.size(); ++i)
{
DrawEdge(out.edges[i]);
}
for (int i = 0; i < out.sites.size(); ++i)
{
DrawSite(out.sites[i]);
}
for (int i = 0; i < out.vertices.size(); ++i)
{
DrawVertex(out.vertices[i]);
}
EndDrawing();
}
//Clean Up Memory
out.MemoryCleanUp();
delete vor;
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
}
void VoronoiInteractiveDemo()
{
std::vector<position2D> sites;
int testCase = 0;
int speedPerSec = -30.0f;//#Pixels Per Sec
float sweepLineY = SCREEN_HEIGHT - 1;
switch (testCase)
{
case 0:
// Example case: The points shown in the GIF of Fortune's algorithm on wikipedia
sites.push_back({ 155, 552 });
sites.push_back({ 405, 552 });
sites.push_back({ 624, 463 });
sites.push_back({ 211, 419 });
sites.push_back({ 458, 358 });
sites.push_back({ 673, 299 });
sites.push_back({ 261, 278 });
sites.push_back({ 88, 196 });
sites.push_back({ 497, 177 });
sites.push_back({ 715, 118 });
sites.push_back({ 275, 99 });
break;
case 1:
// Test case 1: Points with equal x
sites.push_back({ 300, 300 });
sites.push_back({ 300, 400 });
sites.push_back({ 400, 350 });
break;
case 2:
// Test case 2: Points with equal y (where those points are not the first points)
sites.push_back({ 300, 300 });
sites.push_back({ 200, 200 });
sites.push_back({ 400, 200 });
break;
case 3: //fix by FLT_MAX causes floating point issues
// Test case 3: Points with equal y (where those points are the first points)
// With a third point that is slightly off to one side.
// Requires a special case for the first points to prevent errors in finding the replacedarc
sites.push_back({ 320, 200 });
sites.push_back({ 200, 300 });
sites.push_back({ 400, 300 });
break;
case 31://CheckRayIntersection last case
// Test case 3a: Points with equal y (where those points are the first points).
// With a third point that exactly lines up with the edge between the first 2.
// Requires the special case for edges that intersect at both of their starting points (they should not be counted as intersecting).
sites.push_back({ 300, 200 });
sites.push_back({ 200, 300 });
sites.push_back({ 400, 300 });
break;
case 32: // Requires correct way to compare items in Event PQ
// Test case 3b: 3 points with equal y (and nothing else)
sites.push_back({ 300, 300 });
sites.push_back({ 200, 300 });
sites.push_back({ 400, 300 });
sites.push_back({ 500, 300 });
break;
case 4:
// Test case 4: A completely-surrounded site
sites.push_back({ 100, 100 });
sites.push_back({ 500, 150 });
sites.push_back({ 300, 300 });
sites.push_back({ 100, 550 });
sites.push_back({ 500, 500 });
break;
case 5:
// Test case 5: An arc gets squeezed by a later-created arc before it would be squeezed by its original edges.
// Requires handling of events that get "pre-empted" and are no longer required by the time they would execute.
sites.push_back({ 300, 500 });
sites.push_back({ 200, 450 });
sites.push_back({ 400, 450 });
sites.push_back({ 300, 400 });
break;
default:
break;
}
Voronoi *vor = new Voronoi();
VoronoiOutput out;
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Voronoi Diagram");
SetTargetFPS(60);
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
BeginDrawing();
ClearBackground(BG_COLOR);
//Update Voronoi State
vor->VoronoiInteractive(sites, 0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, sweepLineY, out);
//Use State/ Render Voronoi State
RenderVoronoi(out, vor->beachLine->root, sweepLineY);
if (vor->state == VoronoiState::PROCESSING_EVENT_PQ)
{
sweepLineY += speedPerSec * GetFrameTime();
DrawLineV(convertToScreenSpace({ 0, sweepLineY }), convertToScreenSpace({ SCREEN_WIDTH - 1, sweepLineY }), SWEEP_COLOR);
}
EndDrawing();
}
// De-Initialization
//Clean Up Memory
out.MemoryCleanUp();
delete vor;
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
}
int main(int argc, char* argv[])
{
bool interactive = true;
if (interactive)
VoronoiInteractiveDemo();
else
VoronoiDemo();
#ifdef DEBUG
system("pause");
#endif // DEBUG
return 0;
}