-
Notifications
You must be signed in to change notification settings - Fork 1
/
BeachLineBST.cpp
787 lines (630 loc) · 20.2 KB
/
BeachLineBST.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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
#include "BeachLineBST.h"
#define COUNT 10
BeachLineBST::BeachLineBST()
{
}
void BeachLineBST::InsertArc(Vertex * s, EventPQ * eventPQ)
{
float directrixY = s->pos.y;
//Inserting First Item in Tree
if (!root)
{
firstEventY = s->pos.y;
root = CreateArcItem(s);
return;
}
//Sites in close proximity to first when the first one is degerate line
if(firstEventY - s->pos.y < 1.0f)
{
BeachLineBSTNode* newArc = CreateArcItem(s);
BeachLineBSTNode* arcAbove = (root->isLeaf) ? root : GetArcByX2(s->pos.x, directrixY);
HalfEdge ray;
ray.orig = { (newArc->site->pos.x + arcAbove->site->pos.x) / 2, 1000.0f };
ray.dir = { 0.0f, -1.0f };
ray.isMaxY = true;
if (arcAbove->GetParent() == nullptr)
{
//Directly Root will be affected
//Compare x
if (arcAbove->site->pos.x <= newArc->site->pos.x)
{
BeachLineBSTNode* newBreakPt = CreateBreakPointItem(arcAbove->site, newArc->site, ray);
root = newBreakPt;
newBreakPt->SetLeftChild(arcAbove);
newBreakPt->SetRightChild(newArc);
}
else
{
BeachLineBSTNode* newBreakPt = CreateBreakPointItem(newArc->site, arcAbove->site, ray);
root = newBreakPt;
newBreakPt->SetLeftChild(newArc);
newBreakPt->SetRightChild(arcAbove);
}
}
else
{
if (arcAbove->site->pos.x <= newArc->site->pos.x)
{
BeachLineBSTNode* newBreakPt = CreateBreakPointItem(arcAbove->site, newArc->site, ray);
bool left = (arcAbove->GetParent()->GetLeftChild() == arcAbove) ? true : false;
newBreakPt->SetParent(arcAbove->GetParent(), left);
newBreakPt->SetLeftChild(arcAbove);
newBreakPt->SetRightChild(newArc);
}
else
{
BeachLineBSTNode* newBreakPt = CreateBreakPointItem(newArc->site, arcAbove->site, ray);
bool left = (arcAbove->GetParent()->GetLeftChild() == arcAbove) ? true : false;
newBreakPt->SetParent(arcAbove->GetParent(), left);
newBreakPt->SetLeftChild(newArc);
newBreakPt->SetRightChild(arcAbove);
}
}
return;
}
position2D t = s->pos;
//BeachLineBSTNode* arcToReplace = GetArcByX(t.x, directrixY);
BeachLineBSTNode* arcToReplace = GetArcByX2(t.x, directrixY);
if (arcToReplace->cEvent)
{
Event *temp = arcToReplace->cEvent;
eventPQ->deleteEvent(temp);
delete temp;
arcToReplace->cEvent = nullptr;
}
BeachLineBSTNode* arcLeft = CreateArcItem(arcToReplace->site);
BeachLineBSTNode* arcRight = CreateArcItem(arcToReplace->site);
BeachLineBSTNode* newArc = CreateArcItem(s);
float intersectionY = GetArcYForXCoord(arcToReplace->site, t.x, directrixY);
//Making 2 new edges for 2 new breakpoints
position2D start = { t.x, intersectionY };
HalfEdge edgeLeft;
HalfEdge edgeRight;
edgeLeft.orig = start;
edgeRight.orig = start;
//TODO: Dual Fix
position2D dir = { arcToReplace->site->pos.x - newArc->site->pos.x,
arcToReplace->site->pos.y - newArc->site->pos.y };
position2D bisectorDir = normalize({ -dir.y, dir.x });
edgeLeft.dir = { bisectorDir.x, bisectorDir.y };
edgeRight.dir = { -bisectorDir.x, -bisectorDir.y };
BeachLineBSTNode *breakPtLeftEdge = CreateBreakPointItem(arcToReplace->site, newArc->site, edgeLeft);
BeachLineBSTNode *breakPtRightEdge = CreateBreakPointItem(newArc->site, arcToReplace->site, edgeRight);
if (arcToReplace->GetParent() == nullptr)
{
//Directly Root will be affected
root = breakPtLeftEdge;
breakPtLeftEdge->SetLeftChild(arcLeft);
breakPtLeftEdge->SetRightChild(breakPtRightEdge);
breakPtRightEdge->SetLeftChild(newArc);
breakPtRightEdge->SetRightChild(arcRight);
}
else
{
bool left = (arcToReplace->GetParent()->GetLeftChild() == arcToReplace) ? true : false;
breakPtLeftEdge->SetParent(arcToReplace->GetParent(), left);
breakPtLeftEdge->SetLeftChild(arcLeft);
breakPtLeftEdge->SetRightChild(breakPtRightEdge);
breakPtRightEdge->SetLeftChild(newArc);
breakPtRightEdge->SetRightChild(arcRight);
}
delete arcToReplace;
//Check For Circle Events
CheckAndAddCircleEvent(arcLeft, eventPQ, directrixY);
CheckAndAddCircleEvent(arcRight, eventPQ, directrixY);
#ifdef DEBUG
print2D(directrixY);
#endif
}
void BeachLineBST::RemoveArc(Event * cEvent, EventPQ * eventPQ, std::vector<Edge*>& edges, std::vector<Vertex*>& vertices)
{
float directrixY = cEvent->point->pos.y;
BeachLineBSTNode* toDeleteArc = cEvent->arc;
//Find the left and right breakpoint
//If there is a circle event then there will be left and right breakpoints Assertion
BeachLineBSTNode* leftBreakPt = nullptr;
BeachLineBSTNode* rightBreakPt = nullptr;
GetLeftAndRightBreakPoints(toDeleteArc, leftBreakPt, rightBreakPt);
#ifdef DEBUG
assert(leftBreakPt != rightBreakPt);
#endif // DEBUG
BeachLineBSTNode* leftArc = GetLeftArc(toDeleteArc);
BeachLineBSTNode* rightArc = GetRightArc(toDeleteArc);
#ifdef DEBUG
std::cout << leftArc << " " << rightArc << std::endl;
assert(leftArc != rightArc);
#endif // DEBUG
//Delete the Circle Events of the predecessor and successor arcs
if (leftArc && leftArc->cEvent)
{
Event *temp = leftArc->cEvent;
eventPQ->deleteEvent(temp);
delete temp;
leftArc->cEvent = nullptr;
}
if (rightArc && rightArc->cEvent)
{
Event *temp = rightArc->cEvent;
eventPQ->deleteEvent(temp);
delete temp;
rightArc->cEvent = nullptr;
}
Vertex *vertex = new Vertex(cEvent->circleCenter);
vertices.push_back(vertex);
//From breakpoints we also get rays
//These rays will now be converted to line segments/final edges, and also vertex records will be added in Voronoi Diags
//The vertex will be center of the circle of the circle Event
//This vertex will also be orig of the new edge of new breakpoint
Edge *leftEdge = new Edge();
leftEdge->start = new Vertex(leftBreakPt->ray.orig);
untrackedVertices.push_back(leftEdge->start);
leftEdge->end = vertex;
//position2D dirTest = normalize({leftBreakPt->ray.orig.x - vertex->pos.x, leftBreakPt->ray.orig.y - vertex->pos.y});
//assert(dirTest.x == leftBreakPt->ray.dir.x && dirTest.y == leftBreakPt->ray.dir.y);
Edge *rightEdge = new Edge();
rightEdge->start = new Vertex(rightBreakPt->ray.orig);
untrackedVertices.push_back(rightEdge->start);
rightEdge->end = vertex;
if (leftBreakPt->ray.isMaxY)
{
leftEdge->start->pos.y = 1000.0f;
}
if (rightBreakPt->ray.isMaxY)
{
rightEdge->start->pos.y = 1000.0f;
}
//TODO::Fixing Dual
edges.push_back(leftEdge);
edges.push_back(rightEdge);
HalfEdge newRay;
newRay.orig = vertex->pos;
//Finding new Edge direction
position2D dir = { leftArc->site->pos.x - rightArc->site->pos.x,
leftArc->site->pos.y - rightArc->site->pos.y };
position2D bisectorDir = normalize({ -dir.y, dir.x });
newRay.dir = { bisectorDir.x, bisectorDir.y };
//Fix the BeachLineBST
//Becoz we need to delete 2 breakpoints and add one breakpoint with new edge
//TODO: Do this
/*
Now because of the way we insert items, both edges/breakpoints must be a (grand)parent of the arc that we want to remove and in particular, one of them must be that arc’s immediate parent.
Before we remove the arc’s parent, we need to first do something with it’s sibling. So we’ll put it in it’s parent’s place as one of it’s parent’s parent’s children. We can then safely remove the arc and parent edge from the tree, but there is another parent somewhere higher up the tree. This higher edge simply gets substituted with the new replacement edge, inheriting the same parent and children.
*/
//Finding out which breakpoint is an immediate parent
BeachLineBSTNode *immediateParent = (toDeleteArc->GetParent() == leftBreakPt) ? leftBreakPt: rightBreakPt;
BeachLineBSTNode *ancestorParent = (toDeleteArc->GetParent() != leftBreakPt) ? leftBreakPt : rightBreakPt; //Way up the hierarchy
//Is Item we trying to delete is root then fix root(Here ancestor parent may be a root)
//Fix Immediate Parent
BeachLineBSTNode *sibling = (immediateParent->GetLeftChild() == toDeleteArc) ? immediateParent->GetRightChild(): immediateParent->GetLeftChild();
if (immediateParent->GetParent()->GetLeftChild() == immediateParent)
{
immediateParent->GetParent()->SetLeftChild(sibling);
}
else
{
immediateParent->GetParent()->SetRightChild(sibling);
}
delete immediateParent;
delete toDeleteArc;
//Fix Ancestor Parent
BeachLineBSTNode *newBreakPt = CreateBreakPointItem(leftArc->site, rightArc->site, newRay);
if (ancestorParent->GetParent() == nullptr)
{
//ancesotor Parent is root
root = newBreakPt;
}
else
{
bool left = (ancestorParent->GetParent()->GetLeftChild() == ancestorParent) ? true : false;
newBreakPt->SetParent(ancestorParent->GetParent(), left);
}
newBreakPt->SetLeftChild(ancestorParent->GetLeftChild());
newBreakPt->SetRightChild(ancestorParent->GetRightChild());
delete ancestorParent;
//Tree is Fixed Now
//Check circle events again
CheckAndAddCircleEvent(leftArc, eventPQ, directrixY);
CheckAndAddCircleEvent(rightArc, eventPQ, directrixY);
#ifdef DEBUG
print2D(directrixY);
#endif
}
bool GetParabolaRayIntersection(HalfEdge &ray, Vertex *site, float directrixY, position2D &intersectionPt)
{
//Input Ray is the ray from BreakPt
//Input arc is one of the arcs from the tuple of Breakpt
//Case: Edge is a vertical Line
if (ray.dir.x == 0.0f)
{
//Arc is also vertical line(When sweep line is on directrix)
if (directrixY == site->pos.y)
{
if (ray.orig.x == site->pos.x)
{
intersectionPt = site->pos;
return true;
}
else
{
return false;
}
}
//Edge is vertical line
//Just put it's x value in Parabola Eqn
float arcY = GetArcYForXCoord(site, ray.orig.x, directrixY);
intersectionPt = { ray.orig.x, arcY };
return true;
}
//Line Equation => Point-Slope Form to Slope-Intercept Form
//Ray is in Point-Slope Form
//Slope Intercept Form
//y = mx + c
float m1 = ray.dir.y / ray.dir.x;
float c1 = ray.orig.y - m1 * ray.orig.x;
//Case: Arc is currently a vertical line(directrixY == arc.focus.y)
if (site->pos.y == directrixY)
{
//Check if the intersection is in the same direction as edge
if ((site->pos.x - ray.orig.x) * ray.dir.x < 0)
{
return false;
}
intersectionPt.x = site->pos.x;
intersectionPt.y = m1 * site->pos.x + c1;
return true;
}
//Equate Line and Parabola equation at intersection point
// For the edge; y = m1 x + c1 where m is gradient, c is y intercept
//For the parabola; y = a2 x ^ 2 + b2 x + c2
//Find the parabola first
float yf_d = 2.0f * (site->pos.y - directrixY);
float a2 = 1.0f / yf_d;
float b2 = -2.0f * site->pos.x / yf_d;
float c2 = site->pos.x * site->pos.x / yf_d + (site->pos.y + directrixY) * 0.5f;
//At Intersection y = a2 x^2 + (b2 - m1)x + (c2 - c1)
float a = a2;
float b = b2 - m1;
float c = c2 - c1;
float discriminant = b * b - 4.0f * a * c;
if (discriminant < 0)
return false;
float sqDisc = std::sqrt(discriminant);
float x1 = (-b + sqDisc) / (2.0f * a);
float x2 = (-b - sqDisc) / (2.0f * a);
//This gives us two locations where the line crosses the parabola. Remember we are not dealing with lines, but technically with rays, they have a starting point and a direction. In that case we only want one of these x values. The one I select depends on the direction of the edge, if it's pointing right we select the highest value, if it's pointing left we select the minimum value:
float x;
float x1Offset = x1 - ray.orig.x;
float x2Offset = x2 - ray.orig.x;
float x1Dot = x1Offset * ray.dir.x;
float x2Dot = x2Offset * ray.dir.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(site, x, directrixY);
intersectionPt = { x, y };
return true;
}
//Solved by Equating Parabola equation on both sides and making a quadratic equation
float BeachLineBST::GetXOfBreakPoint(BeachLineBSTNode * breakpoint, float directrixY)
{
float breakPtX = 0.0f;
position2D l = breakpoint->lSite->pos;
position2D r = breakpoint->rSite->pos;
//Parabola equation y = ax^2 + bx + c
//First find both parabola equation coefficients
float yf_d = 2.0f * (l.y - directrixY);
float a1 = 1.0f / yf_d;
float b1 = -2.0f * l.x / yf_d;
float c1 = l.x * l.x / yf_d + (l.y + directrixY) * 0.5f;
//Find 2nd parabola equation coefficients
yf_d = 2.0f * (r.y - directrixY);
float a2 = 1.0f / yf_d;
float b2 = -2.0f * r.x / yf_d;
float c2 = r.x * r.x / yf_d + (r.y + directrixY) * 0.5f;
float a = a1 - a2;
float b = b1 - b2;
float c = c1 - c2;
//2 parabolas of same orientation (like in out case upwards) can intersect at 2 points
float discriminant = b * b - 4 * a * c;
float x1 = (-b + std::sqrt(discriminant)) / (2*a);
float x2 = (-b - std::sqrt(discriminant)) / (2*a);
if (l.y < r.y)
{
breakPtX = std::max(x1, x2);
}
else
{
breakPtX = std::min(x1, x2);
}
return breakPtX;
}
void BeachLineBST::CheckAndAddCircleEvent(BeachLineBSTNode * item, EventPQ * eventPQ, float directrixY)
{
BeachLineBSTNode *leftBreakPt = nullptr;
BeachLineBSTNode *rightBreakPt = nullptr;
GetLeftAndRightBreakPoints(item, leftBreakPt, rightBreakPt);
assert(leftBreakPt != rightBreakPt);
//Check 0
if (!leftBreakPt || !rightBreakPt)
return;
//Assert leftBreakPt->rSite == rightBreakPt->lSite
//Check 1
if (leftBreakPt->lSite == rightBreakPt->rSite)
return;
//Check 2
position2D intersectionPt;
if (CheckRayIntersection(leftBreakPt->ray, rightBreakPt->ray, intersectionPt))
{
//Intersection Point Found
//Circle Event Detected
//TODO: Discard Circle Events outside bounding box
float radius = magnitude({ intersectionPt.x - item->site->pos.x, intersectionPt.y - item->site->pos.y });
float circleEventY = intersectionPt.y - radius;
////New Code
//if(circleEventY > directrixY)
// return;
////New
////New 2
//if (item->cEvent != nullptr)
//{
// if (item->cEvent->point->pos.y >= circleEventY)
// {
// return;
// }
//}
////New 2
assert(isfinite(circleEventY));
Event* cEvent = new Event(new Vertex({ intersectionPt.x, circleEventY }), item, intersectionPt);
eventPQ->push(cEvent);
//Assert(item->cEvent is NULL)
item->cEvent = cEvent;
}
}
BeachLineBSTNode * BeachLineBST::GetArcByX(float x, float directrixY)
{
BeachLineBSTNode* curr = root;
while (!curr->isLeaf)
{
float breakptX = GetXOfBreakPoint(curr, directrixY);
if (x >= breakptX) //Biased towards right
{
curr = curr->GetRightChild();
}
else
{
curr = curr->GetLeftChild();
}
}
//Assert curr is leaf
return curr;
}
BeachLineBSTNode * BeachLineBST::GetArcByX2(float x, float directrixY)
{
BeachLineBSTNode* curr = root;
while (!curr->isLeaf)
{
position2D breakpt;
bool didLeftIntersect = GetParabolaRayIntersection(curr->ray, curr->lSite, directrixY, breakpt);
if(!didLeftIntersect)
bool didRightIntersect = GetParabolaRayIntersection(curr->ray, curr->rSite, directrixY, breakpt);
if (x >= breakpt.x) //Biased towards right
{
curr = curr->GetRightChild();
}
else
{
curr = curr->GetLeftChild();
}
}
//Assert curr is leaf
return curr;
}
void BeachLineBST::print2DUtil(BeachLineBSTNode * curr, int space, float directrixY)
{
// Base case
if (curr == nullptr)
return;
// Increase distance between levels
space += COUNT;
// Process right child first
print2DUtil(curr->GetRightChild(), space, directrixY);
// Print current node after space
// count
std::cout << std::endl;
for (int i = COUNT; i < space; i++)
std::cout << " ";
if (!curr->isLeaf)
{
position2D breakpt;
bool didLeftIntersect = GetParabolaRayIntersection(curr->ray, curr->lSite, directrixY, breakpt);
if (!didLeftIntersect)
bool didRightIntersect = GetParabolaRayIntersection(curr->ray, curr->rSite, directrixY, breakpt);
std::cout << "(" << breakpt.x << ")" << curr->lSite->pos << curr->rSite->pos << std::endl;
}
else
{
std::cout << "Arc: " << curr->site->siteID << std::endl;
}
// Process left child
print2DUtil(curr->GetLeftChild(), space, directrixY);
}
void BeachLineBST::print2D(float directrixY)
{
print2DUtil(root, 0, directrixY);
std::cout << std::endl;
for(int i = 0; i < 3; i++)
std::cout << "====================================================================================" << std::endl;
std::cout << std::endl;
}
BeachLineBST::~BeachLineBST()
{
for (auto &v : untrackedVertices)
{
#ifdef DEBUG
std::cout << v->pos << std::endl;
#endif
delete v;
}
}
BeachLineBSTNode* GetLeftArc(BeachLineBSTNode* curr)
{
BeachLineBSTNode* leftArc = nullptr;
//curr should be leaf/arc
//Immediately Above Breakpt
if (curr->GetParent() != nullptr && curr->GetParent()->GetRightChild() == curr)
{
leftArc = curr->GetParent()->GetLeftChild();
while (!leftArc->isLeaf)
{
leftArc = leftArc->GetRightChild();
}
#ifdef DEBUG
assert(leftArc->isLeaf);
#endif
return leftArc;
}
//Go Up and find first parent who has the child as right
while (curr->GetParent() != nullptr && curr->GetParent()->GetLeftChild() == curr)
{
curr = curr->GetParent();
}
curr = curr->GetParent();
if (curr != nullptr)
{
if (curr->GetLeftChild() != nullptr)
{
//Go Right Till Leaf
curr = curr->GetLeftChild();
while (!curr->isLeaf)
{
curr = curr->GetRightChild();
}
leftArc = curr;
}
}
#ifdef DEBUG
assert(leftArc->isLeaf);
#endif
return leftArc;
}
BeachLineBSTNode* GetRightArc(BeachLineBSTNode* curr)
{
BeachLineBSTNode* rightArc = nullptr;
//curr should be leaf/arc
//Immediately Above Breakpt
if (curr->GetParent() != nullptr && curr->GetParent()->GetLeftChild() == curr)
{
rightArc = curr->GetParent()->GetRightChild();
while (!rightArc->isLeaf)
{
rightArc = rightArc->GetLeftChild();
}
#ifdef DEBUG
assert(rightArc->isLeaf);
#endif
return rightArc;
}
//Go Up and find first parent who has the child as left
while (curr->GetParent() != nullptr && curr->GetParent()->GetRightChild() == curr)
{
curr = curr->GetParent();
}
curr = curr->GetParent();
if (curr != nullptr)
{
if (curr->GetRightChild() != nullptr)
{
//Go Right Till Leaf
curr = curr->GetRightChild();
while (!curr->isLeaf)
{
curr = curr->GetLeftChild();
}
rightArc = curr;
}
}
#ifdef DEBUG
assert(rightArc->isLeaf);
#endif
return rightArc;
}
void GetLeftAndRightBreakPoints(BeachLineBSTNode* curr, BeachLineBSTNode* &leftBreakPt, BeachLineBSTNode* &rightBreakPt)
{
BeachLineBSTNode* temp = curr;
leftBreakPt = nullptr;
rightBreakPt = nullptr;
//Assert Curr will be input as leaf
if (curr->GetParent() != nullptr)
{
if (curr->GetParent()->GetLeftChild() == curr)
{
//Then the parent is the rightBreakPt
rightBreakPt = curr->GetParent();
curr = curr->GetParent();
//Assert both of the breakpoints should not be a leaf
//Now find the leftBreakPt //First right turn while climbing up the tree
while (curr->GetParent() != nullptr && curr->GetParent()->GetLeftChild() == curr)
{
curr = curr->GetParent();
}
leftBreakPt = curr->GetParent();
}
else
{
//Then the parent is the leftBreakPt
leftBreakPt = curr->GetParent();
curr = curr->GetParent();
//Assert Both of the break points should not be a leaf
//Now find the rightBreakPt //First left turn while climbing up the tree
while (curr->GetParent() != nullptr && curr->GetParent()->GetRightChild() == curr)
{
curr = curr->GetParent();
}
rightBreakPt = curr->GetParent();
}
}
#ifdef DEBUG
if (leftBreakPt)
assert(leftBreakPt->rSite == temp->site);
if (rightBreakPt)
assert(rightBreakPt->lSite == temp->site);
assert(leftBreakPt != rightBreakPt);
#endif
}
float GetArcYForXCoord(Vertex* site, float x, float directrixY)
{
float a = 1.0f / (2.0f*(site->pos.y - directrixY));
float c = (site->pos.y + directrixY)*0.5f;
float w = x - site->pos.x;
return a * w*w + c;
}
bool CheckRayIntersection(HalfEdge &rayA, HalfEdge &rayB, position2D &pos)
{
position2D as = rayA.orig;
position2D ad = { rayA.dir.x, rayA.dir.y };
position2D bs = rayB.orig;
position2D bd = { rayB.dir.x, rayB.dir.y };
float dx = bs.x - as.x;
float dy = bs.y - as.y;
float det = bd.x * ad.y - bd.y * ad.x;
//if (det == 0) //Rays do not intersect at all
// return false;
float u = (dy * bd.x - dx * bd.y) / det;
float v = (dy * ad.x - dx * ad.y) / det;
if (u < 0 && !rayA.isMaxY)
return false;
if (v < 0 && !rayB.isMaxY)
return false;
if ((u == 0.0f) && (v == 0.0f) && !rayA.isMaxY && !rayB.isMaxY) return false;
pos = { as.x + u * ad.x, as.y + u * ad.y };
return true;
}