-
Notifications
You must be signed in to change notification settings - Fork 5
/
voronoi.cpp
669 lines (590 loc) · 20.7 KB
/
voronoi.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#include <assert.h>
#include <float.h>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <vector>
enum class BeachlineItemType
{
None,
Arc,
Edge
};
struct Edge
{
Vector2 start;
Vector2 direction;
bool extendsUpwardsForever;
};
struct CompleteEdge
{
Vector2 endpointA;
Vector2 endpointB;
};
struct SweepEvent;
struct Arc
{
Vector2 focus;
SweepEvent* squeezeEvent;
};
struct BeachlineItem
{
BeachlineItemType type;
union
{
Arc arc;
Edge edge;
};
BeachlineItem* parent;
BeachlineItem* left;
BeachlineItem* right;
BeachlineItem() : parent(nullptr), left(nullptr), right(nullptr) {}
void SetLeft(BeachlineItem* newLeft)
{
assert(type == BeachlineItemType::Edge);
assert(newLeft != nullptr);
left = newLeft;
newLeft->parent = this;
}
void SetRight(BeachlineItem* newRight)
{
assert(type == BeachlineItemType::Edge);
assert(newRight != nullptr);
right = newRight;
newRight->parent = this;
}
void SetParentFromItem(BeachlineItem* item)
{
assert(item != nullptr);
if(item->parent == nullptr)
{
parent = nullptr;
return;
}
if(item->parent->left == item)
{
item->parent->SetLeft(this);
}
else
{
assert(item->parent->right == item);
item->parent->SetRight(this);
}
}
};
enum class SweepEventType
{
None,
NewPoint,
EdgeIntersection
};
struct NewPointEvent
{
Vector2 point;
};
struct EdgeIntersectionEvent
{
Vector2 intersectionPoint;
BeachlineItem* squeezedArc;
bool isValid;
};
struct SweepEvent
{
float yCoord;
SweepEventType type;
union
{
NewPointEvent newPoint;
EdgeIntersectionEvent edgeIntersect;
};
};
#include "vtree.cpp"
struct FortuneState
{
float sweepY;
std::vector<CompleteEdge*> edges;
std::vector<SweepEvent*> unencounteredEvents;
BeachlineItem* beachlineRoot;
};
struct EventComparison : std::binary_function<SweepEvent, SweepEvent, bool>
{
bool operator()(const SweepEvent* lhs, const SweepEvent* rhs) const
{
assert((lhs != nullptr) && (rhs != nullptr));
return lhs->yCoord < rhs->yCoord;
}
};
float GetArcYForXCoord(Arc& arc, float x, float directrixY)
{
// NOTE: In the interest of keeping the formula simple when moving away from the origin,
// we'll use the substitution from (x,y) -> (w,y) = (x-focusX,y).
// In particular this substitution means that the formula always has the form:
// y = aw^2 + c, the linear term's coefficient is always 0.
float a = 1.0f/(2.0f*(arc.focus.y - directrixY));
float c = (arc.focus.y + directrixY)*0.5f;
float w = x - arc.focus.x;
return a*w*w + c;
}
bool GetEdgeArcIntersectionPoint(Edge& edge, Arc& arc, float directrixY, Vector2& intersectionPt)
{
// Special case 1: Edge is a vertical line.
if(edge.direction.x == 0.0f)
{
if(directrixY == arc.focus.y)
{
// Special case A of special case 1: The arc's focus is on the directrix line, so the arc is essentially a vertical line
if(edge.start.x == arc.focus.x)
{
// TODO: What is the correct Y-value to use here?
intersectionPt = arc.focus;
return true;
}
else
{
return false;
}
}
float arcY = GetArcYForXCoord(arc, edge.start.x, directrixY);
intersectionPt = {edge.start.x, arcY};
return true;
}
// y = px + q
float p = edge.direction.y/edge.direction.x;
float q = edge.start.y - p*edge.start.x;
// Special case 2: Arc is currently a vertical line (directrixY == arc.focus.y)
if(arc.focus.y == directrixY)
{
float intersectionXOffset = arc.focus.x - edge.start.x;
// Check if the intersection is in the direction that the edge is going. If not then no intersect
if(intersectionXOffset * edge.direction.x < 0)
{
return false;
}
intersectionPt.x = arc.focus.x;
intersectionPt.y = p*arc.focus.x + q;
return true;
}
// y = a_0 + a_1x + a_2x^2
float a2 = 1.0f/(2.0f*(arc.focus.y - directrixY));
float a1 = -p - 2.0f*a2*arc.focus.x;
float a0 = a2*arc.focus.x*arc.focus.x + (arc.focus.y + directrixY)*0.5f - q;
float discriminant = a1*a1 - 4.0f*a2*a0;
if(discriminant < 0)
{
return false;
}
float rootDisc = sqrtf(discriminant);
float x1 = (-a1 + rootDisc)/(2.0f*a2);
float x2 = (-a1 - rootDisc)/(2.0f*a2);
float x1Offset = x1 - edge.start.x;
float x2Offset = x2 - edge.start.x;
float x1Dot = x1Offset * edge.direction.x;
float x2Dot = x2Offset * edge.direction.x;
float x;
if((x1Dot >= 0.0f) && (x2Dot < 0.0f)) x = x1;
else if((x1Dot < 0.0f) && (x2Dot >= 0.0f)) x = x2;
else if((x1Dot >= 0.0f) && (x2Dot >= 0.0f))
{
if(x1Dot < x2Dot) x = x1;
else x = x2;
}
else // (x1Dot < 0.0f) && (x2Dot < 0.0f)
{
if(x1Dot < x2Dot) x = x2;
else x = x1;
}
float y = GetArcYForXCoord(arc, x, directrixY);
assert(isfinite(y));
intersectionPt = {x,y};
return true;
}
BeachlineItem* GetActiveArcForXCoord(BeachlineItem* root, float x, float directrixY)
{
BeachlineItem* currentItem = root;
while(currentItem->type != BeachlineItemType::Arc)
{
assert(currentItem->type == BeachlineItemType::Edge);
BeachlineItem* left = GetFirstLeafOnTheLeft(currentItem);
BeachlineItem* right = GetFirstLeafOnTheRight(currentItem);
assert((left != nullptr) && (left->type == BeachlineItemType::Arc));
assert((right != nullptr) && (right->type == BeachlineItemType::Arc));
BeachlineItem* fromLeft = GetFirstParentOnTheRight(left);
BeachlineItem* fromRight = GetFirstParentOnTheLeft(right);
assert((fromLeft != nullptr) && (fromLeft == fromRight));
assert(fromLeft->type == BeachlineItemType::Edge);
Edge& separatingEdge = fromLeft->edge;
Vector2 leftIntersect;
Vector2 rightIntersect;
bool didLeftIntersect = GetEdgeArcIntersectionPoint(separatingEdge, left->arc, directrixY, leftIntersect);
bool didRightIntersect = GetEdgeArcIntersectionPoint(separatingEdge, right->arc, directrixY, rightIntersect);
#if 0
// TODO: These should all pass as far as I can tell, but precision issues cause that not to be the case.
assert(didLeftIntersect && didRightIntersect);
assert(abs(leftIntersect.x - rightIntersect.x) < 1.0f);
assert(didLeftIntersect || didRightIntersect);
#endif
float intersectionX = leftIntersect.x;
if(!didLeftIntersect && didRightIntersect)
{
intersectionX = rightIntersect.x;
}
if(x < intersectionX)
{
currentItem = currentItem->left;
}
else
{
currentItem = currentItem->right;
}
}
assert(currentItem->type == BeachlineItemType::Arc);
return currentItem;
}
static BeachlineItem* CreateArc(Vector2 focus)
{
BeachlineItem* result = new BeachlineItem();
result->type = BeachlineItemType::Arc;
result->arc.focus = focus;
result->arc.squeezeEvent = nullptr;
return result;
}
static BeachlineItem* CreateEdge(Vector2 start, Vector2 dir)
{
BeachlineItem* result = new BeachlineItem();
result->type = BeachlineItemType::Edge;
result->edge.start = start;
result->edge.direction = dir;
result->edge.extendsUpwardsForever = false;
return result;
}
bool TryGetEdgeIntersectionPoint(Edge& e1, Edge& e2, Vector2& intersectionPt)
{
float dx = e2.start.x - e1.start.x;
float dy = e2.start.y - e1.start.y;
float det = e2.direction.x*e1.direction.y - e2.direction.y*e1.direction.x;
float u = (dy*e2.direction.x - dx*e2.direction.y)/det;
float v = (dy*e1.direction.x - dx*e1.direction.y)/det;
if((u < 0.0f) && !e1.extendsUpwardsForever) return false;
if((v < 0.0f) && !e2.extendsUpwardsForever) return false;
if((u == 0.0f) && (v == 0.0f) && !e1.extendsUpwardsForever && !e2.extendsUpwardsForever) return false;
intersectionPt = {e1.start.x + e1.direction.x*u, e1.start.y + e1.direction.y*u};
return true;
}
void AddArcSqueezeEvent(
std::priority_queue<SweepEvent*, std::vector<SweepEvent*>, EventComparison>& eventQueue,
BeachlineItem* arc)
{
BeachlineItem* leftEdge = GetFirstParentOnTheLeft(arc);
BeachlineItem* rightEdge = GetFirstParentOnTheRight(arc);
if((leftEdge == nullptr) || (rightEdge == nullptr))
{
return;
}
Vector2 circleEventPoint;
bool edgesIntersect = TryGetEdgeIntersectionPoint(leftEdge->edge, rightEdge->edge, circleEventPoint);
if(!edgesIntersect)
{
return;
}
Vector2 circleCentreOffset = {arc->arc.focus.x - circleEventPoint.x,
arc->arc.focus.y - circleEventPoint.y};
float circleRadius = Magnitude(circleCentreOffset);
float circleEventY = circleEventPoint.y - circleRadius;
assert(arc->type == BeachlineItemType::Arc);
// NOTE: If we already have an intersection event that we'll encounter sooner than this one, then
// just don't add this one (because otherwise it'll reference a deleted arc when it gets processed)
if(arc->arc.squeezeEvent != nullptr)
{
if(arc->arc.squeezeEvent->yCoord >= circleEventY)
{
return;
}
else
{
assert(arc->arc.squeezeEvent->type == SweepEventType::EdgeIntersection);
arc->arc.squeezeEvent->edgeIntersect.isValid = false;
}
}
//printf("Add circle event at y=%f\n", circleEventY);
SweepEvent* newEvt = new SweepEvent();
newEvt->type = SweepEventType::EdgeIntersection;
newEvt->yCoord = circleEventY;
newEvt->edgeIntersect.squeezedArc = arc;
newEvt->edgeIntersect.intersectionPoint = circleEventPoint;
newEvt->edgeIntersect.isValid = true;
eventQueue.push(newEvt);
arc->arc.squeezeEvent = newEvt;
}
BeachlineItem* AddArcToBeachline(std::priority_queue<SweepEvent*, std::vector<SweepEvent*>, EventComparison>& eventQueue,
BeachlineItem* root, SweepEvent& evt, float sweepLineY)
{
//printf("Add arc @ (%f, %f) to the beachline\n", evt.newPoint.point.x, evt.newPoint.point.y);
Vector2 newPoint = evt.newPoint.point;
BeachlineItem* replacedArc = GetActiveArcForXCoord(root, newPoint.x, sweepLineY);
assert((replacedArc != nullptr) && (replacedArc->type == BeachlineItemType::Arc));
BeachlineItem* splitArcLeft = CreateArc(replacedArc->arc.focus);
BeachlineItem* splitArcRight = CreateArc(replacedArc->arc.focus);
BeachlineItem* newArc = CreateArc(newPoint);
float intersectionY = GetArcYForXCoord(replacedArc->arc, newPoint.x, sweepLineY);
assert(isfinite(intersectionY));
Vector2 edgeStart = {newPoint.x, intersectionY};
Vector2 focusOffset = {newArc->arc.focus.x - replacedArc->arc.focus.x,
newArc->arc.focus.y - replacedArc->arc.focus.y};
Vector2 edgeDir = normalize({focusOffset.y, -focusOffset.x});
BeachlineItem* edgeLeft = CreateEdge(edgeStart, edgeDir);
BeachlineItem* edgeRight = CreateEdge(edgeStart, {-edgeDir.x, -edgeDir.y});
assert(replacedArc->left == nullptr);
assert(replacedArc->right == nullptr);
edgeLeft->SetParentFromItem(replacedArc);
edgeLeft->SetLeft(splitArcLeft);
edgeLeft->SetRight(edgeRight);
edgeRight->SetLeft(newArc);
edgeRight->SetRight(splitArcRight);
BeachlineItem* newRoot = root;
if(root == replacedArc)
{
newRoot = edgeLeft;
}
if(replacedArc->arc.squeezeEvent != nullptr)
{
assert(replacedArc->arc.squeezeEvent->type == SweepEventType::EdgeIntersection);
assert(replacedArc->arc.squeezeEvent->edgeIntersect.isValid);
replacedArc->arc.squeezeEvent->edgeIntersect.isValid = false;
}
VerifyThatThereAreNoReferencesToItem(newRoot, replacedArc);
assert((replacedArc->arc.squeezeEvent == nullptr) || (replacedArc->arc.squeezeEvent->edgeIntersect.isValid == false));
delete replacedArc;
AddArcSqueezeEvent(eventQueue, splitArcLeft);
AddArcSqueezeEvent(eventQueue, splitArcRight);
return newRoot;
}
BeachlineItem* RemoveArcFromBeachline(
std::priority_queue<SweepEvent*, std::vector<SweepEvent*>, EventComparison>& eventQueue,
BeachlineItem* root,
std::vector<CompleteEdge*>& outputEdges,
SweepEvent& evt)
{
BeachlineItem* squeezedArc = evt.edgeIntersect.squeezedArc;
assert(evt.type == SweepEventType::EdgeIntersection);
assert(evt.edgeIntersect.isValid);
assert(squeezedArc->arc.squeezeEvent == &evt);
//printf("Remove arc @ (%f, %f) from the beachline because we reached y=%f\n", squeezedArc->arc.focus.x, squeezedArc->arc.focus.y, evt.yCoord);
BeachlineItem* leftEdge = GetFirstParentOnTheLeft(squeezedArc);
BeachlineItem* rightEdge = GetFirstParentOnTheRight(squeezedArc);
assert((leftEdge != nullptr) && (rightEdge != nullptr));
BeachlineItem* leftArc = GetFirstLeafOnTheLeft(leftEdge);
BeachlineItem* rightArc = GetFirstLeafOnTheRight(rightEdge);
assert((leftArc != nullptr) && (rightArc != nullptr));
assert(leftArc != rightArc);
Vector2 circleCentre = evt.edgeIntersect.intersectionPoint;
CompleteEdge* edgeA = new CompleteEdge();
edgeA->endpointA = leftEdge->edge.start;
edgeA->endpointB = circleCentre;
CompleteEdge* edgeB = new CompleteEdge();
edgeB->endpointA = circleCentre;
edgeB->endpointB = rightEdge->edge.start;
if(leftEdge->edge.extendsUpwardsForever)
{
edgeA->endpointA.y = FLT_MAX;
}
if(rightEdge->edge.extendsUpwardsForever)
{
edgeB->endpointA.y = FLT_MAX;
}
outputEdges.emplace_back(edgeA);
outputEdges.emplace_back(edgeB);
Vector2 adjacentArcOffset = {};
adjacentArcOffset.x = rightArc->arc.focus.x - leftArc->arc.focus.x;
adjacentArcOffset.y = rightArc->arc.focus.y - leftArc->arc.focus.y;
Vector2 newEdgeDirection = {adjacentArcOffset.y, -adjacentArcOffset.x};
newEdgeDirection = normalize(newEdgeDirection);
BeachlineItem* newItem = CreateEdge(circleCentre, newEdgeDirection);
BeachlineItem* higherEdge = nullptr;
BeachlineItem* tempItem = squeezedArc;
while(tempItem->parent != nullptr)
{
tempItem = tempItem->parent;
if(tempItem == leftEdge) higherEdge = leftEdge;
if(tempItem == rightEdge) higherEdge = rightEdge;
}
assert((higherEdge != nullptr) && (higherEdge->type == BeachlineItemType::Edge));
newItem->SetParentFromItem(higherEdge);
newItem->SetLeft(higherEdge->left);
newItem->SetRight(higherEdge->right);
assert((squeezedArc->parent == nullptr) || (squeezedArc->parent->type == BeachlineItemType::Edge));
BeachlineItem* remainingItem = nullptr;
BeachlineItem* parent = squeezedArc->parent;
if(parent->left == squeezedArc)
{
remainingItem = parent->right;
}
else
{
assert(parent->right == squeezedArc);
remainingItem = parent->left;
}
assert((parent == leftEdge) || (parent == rightEdge));
assert(parent != higherEdge);
remainingItem->SetParentFromItem(parent);
BeachlineItem* newRoot = root;
if((root == leftEdge) || (root == rightEdge))
{
newRoot = newItem;
}
VerifyThatThereAreNoReferencesToItem(newRoot, leftEdge);
VerifyThatThereAreNoReferencesToItem(newRoot, squeezedArc);
VerifyThatThereAreNoReferencesToItem(newRoot, rightEdge);
assert(squeezedArc->type == BeachlineItemType::Arc);
if(squeezedArc->arc.squeezeEvent != nullptr)
{
assert(squeezedArc->arc.squeezeEvent->type == SweepEventType::EdgeIntersection);
assert(squeezedArc->arc.squeezeEvent->edgeIntersect.isValid);
squeezedArc->arc.squeezeEvent->edgeIntersect.isValid = false;
}
delete leftEdge;
delete squeezedArc;
delete rightEdge;
AddArcSqueezeEvent(eventQueue, leftArc);
AddArcSqueezeEvent(eventQueue, rightArc);
return newRoot;
}
void FinishEdge(BeachlineItem* item, std::vector<CompleteEdge*>& edges)
{
if(item == nullptr)
{
return;
}
if (item->type == BeachlineItemType::Edge)
{
float length = 10000;
Vector2 edgeEnd = item->edge.start;
edgeEnd.x += length * item->edge.direction.x;
edgeEnd.y += length * item->edge.direction.y;
CompleteEdge* edge = new CompleteEdge();
edge->endpointA = item->edge.start;
edge->endpointB = edgeEnd;
edges.emplace_back(edge);
FinishEdge(item->left, edges);
FinishEdge(item->right, edges);
}
delete item;
}
FortuneState FortunesAlgorithm(std::vector<Vector2>& sites, float cutoffY)
{
std::vector<CompleteEdge*> edges;
std::priority_queue<SweepEvent*, std::vector<SweepEvent*>, EventComparison> eventQueue;
for(Vector2 pt : sites)
{
SweepEvent* evt = new SweepEvent();
evt->type = SweepEventType::NewPoint;
evt->newPoint.point = pt;
evt->yCoord = pt.y;
eventQueue.push(evt);
}
// NOTE: We start out by taking the first event and handling it manually, because it lets
// us avoid the "is there an arc here" check that would otherwise need to run very often
SweepEvent* firstEvent = eventQueue.top();
assert(firstEvent->type == SweepEventType::NewPoint);
if(firstEvent->yCoord < cutoffY)
{
FortuneState result = {};
result.sweepY = cutoffY;
while(!eventQueue.empty())
{
result.unencounteredEvents.emplace_back(eventQueue.top());
eventQueue.pop();
}
return result;
}
eventQueue.pop();
BeachlineItem* firstArc = new BeachlineItem();
firstArc->type = BeachlineItemType::Arc;
firstArc->arc.focus = firstEvent->newPoint.point;
firstArc->arc.squeezeEvent = nullptr;
delete firstEvent;
BeachlineItem* root = firstArc;
float startupSpecialCaseEndY = firstArc->arc.focus.y - 1.0f;
while(!eventQueue.empty() && (eventQueue.top()->yCoord > startupSpecialCaseEndY))
{
SweepEvent* evt = eventQueue.top();
if(evt->yCoord < cutoffY)
break;
eventQueue.pop();
assert(evt->type == SweepEventType::NewPoint);
Vector2 newFocus = evt->newPoint.point;
BeachlineItem* newArc = CreateArc(newFocus);
BeachlineItem* activeArc = GetActiveArcForXCoord(root, newFocus.x, newFocus.y);
assert(activeArc->type == BeachlineItemType::Arc);
Vector2 edgeStart = {(newFocus.x+activeArc->arc.focus.x)/2.0f, /*FLT_MAX*//*1000.0f*/newFocus.y+100.0f};
Vector2 edgeDir = {0.0f, -1.0f};
BeachlineItem* newEdge = CreateEdge(edgeStart, edgeDir);
newEdge->edge.extendsUpwardsForever = true;
if(activeArc->parent != nullptr)
{
if(activeArc == activeArc->parent->left)
{
activeArc->parent->SetLeft(newEdge);
}
else
{
assert(activeArc == activeArc->parent->right);
activeArc->parent->SetRight(newEdge);
}
}
else
{
root = newEdge;
}
if(newFocus.x < activeArc->arc.focus.x)
{
newEdge->SetLeft(newArc);
newEdge->SetRight(activeArc);
}
else
{
newEdge->SetLeft(activeArc);
newEdge->SetRight(newArc);
}
delete evt;
}
while(!eventQueue.empty())
{
SweepEvent* nextEvent = eventQueue.top();
// NOTE: For the purposes of interactive demonstration, we add an artificial cutoff.
if(nextEvent->yCoord < cutoffY)
break;
eventQueue.pop();
float sweepY = nextEvent->yCoord;
if(nextEvent->type == SweepEventType::NewPoint)
{
root = AddArcToBeachline(eventQueue, root, *nextEvent, sweepY);
}
else if(nextEvent->type == SweepEventType::EdgeIntersection)
{
if(nextEvent->edgeIntersect.isValid)
{
root = RemoveArcFromBeachline(eventQueue, root, edges, *nextEvent);
}
}
else
{
printf("Unrecognized queue item type: %d\n", nextEvent->type);
}
delete nextEvent;
}
if(eventQueue.empty() || (cutoffY < -200.0f))
{
FinishEdge(root, edges);
root = nullptr;
}
FortuneState result;
result.sweepY = 0.0f;
result.beachlineRoot = root;
result.edges = edges;
while(!eventQueue.empty())
{
result.unencounteredEvents.emplace_back(eventQueue.top());
eventQueue.pop();
}
return result;
}