forked from Pike1z/SETSM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid_triangulation.cpp
1378 lines (1249 loc) · 38.6 KB
/
grid_triangulation.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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************
* The triangulation algorithm in the code was adapted from the methods in these
* two papers:
* [1] Wenzhou Wu, Yikang Rui, Fenzhen Su, Liang Cheng & Jiechen Wang (2014)
* Novel parallel algorithm for constructing Delaunay triangulation based on a
* twofold-divide-and-conquer scheme, GIScience & Remote Sensing, 51:5, 537-554, DOI:
* 10.1080/15481603.2014.946666
* [2] Leonidas Guibas & Jorge Stolfi (1985) Primitives for the Manipulation of
* General Subdivisions and the Computation of Voronoi Diagrams, ACM Transactions on
* Graphics, 51:2, 74-123.
* Retriangulation uses low degree optimizations from this paper
* [3] Olivier Devillers. Vertex Removal in Two Dimensional Delaunay Triangulation:
* Speed-up by Low Degrees Optimization. Computational Geometry, Elsevier, 2011, 44,
* pp.169-177
***************************************/
#include <iostream>
#include <algorithm>
#include <vector>
#include <omp.h>
#include "grid_triangulation.hpp"
//////////////////////////////////////
// HELPERS ///////////////////////////
//////////////////////////////////////
/* Positive if a, b, c counter-clockwise
* Negative if a, b, c clockwise
* Zero if a, b, c collinear
*/
inline INT64 Orientation(const GridPoint &a, const GridPoint &b, const GridPoint &c)
{
INT64 d_11 = a.col - c.col;
INT64 d_21 = b.col - c.col;
INT64 d_12 = a.row - c.row;
INT64 d_22 = b.row - c.row;
return d_11 * d_22 - d_12 *d_21;
}
/* Assumes a, b, c in counter-clockwise order
* Then positive if d in circle,
* negative if d outside circle,
* zero if d on circle
*/
inline INT128 InCircle(const GridPoint &a, const GridPoint &b, const GridPoint &c, const GridPoint &d)
{
INT64 d_11 = a.col - d.col;
INT64 d_12 = a.row - d.row;
INT64 d_13 = d_11 * d_11 + d_12 * d_12;
INT64 d_21 = b.col - d.col;
INT64 d_22 = b.row - d.row;
INT64 d_23 = d_21 * d_21 + d_22 * d_22;
INT64 d_31 = c.col - d.col;
INT64 d_32 = c.row - d.row;
INT64 d_33 = d_31 * d_31 + d_32 * d_32;
INT128 bc = d_21 * d_32 - d_31 * d_22;
INT128 ac = d_11 * d_32 - d_31 * d_12;
INT128 ab = d_11 * d_22 - d_21 * d_12;
return d_13 * bc - d_23 * ac + d_33 * ab;
}
inline bool LessThanPtrXY(GridPoint *a, GridPoint *b)
{
if (a && b) return LessThanXY(*a, *b);
else return false;
}
inline bool LessThanPtrYX(GridPoint *a, GridPoint *b)
{
if (a && b) return LessThanYX(*a, *b);
else return false;
}
//////////////////////////////////////
// GridTriangulation Implementation //
//////////////////////////////////////
template <typename GridType, typename IterType>
GridTriangulation<GridType, IterType>::GridTriangulation(INDEX width, INDEX height)
{
this->width = width;
this->height = height;
this->grid = 0;
this->edge_list = 0;
}
template <typename GridType, typename IterType>
GridTriangulation<GridType, IterType>::~GridTriangulation()
{
delete this->grid;
delete this->edge_list;
}
template <typename GridType, typename IterType>
Grid<GridType, IterType> *GridTriangulation<GridType, IterType>::getGrid()
{
return this->grid;
}
template <typename GridType, typename IterType>
Edge *GridTriangulation<GridType, IterType>::AddEdgeAndTwin(const GridPoint &orig, const GridPoint &dest)
{
// Get edges from edge list
Edge *e;
Edge *et;
e = this->edge_list->GetNewEdge();
et = this->edge_list->GetNewEdge();
// Set next/prev appropriately
e->twin = (e->oprev = (e->dnext = et));
et->twin = (et->oprev = (et->dnext = e));
// Tie to orig/dest
e->orig = orig;
et->orig = dest;
return e;
}
template <typename GridType, typename IterType>
void GridTriangulation<GridType, IterType>::RemoveEdgeAndTwin(Edge &edge)
{
// Get edge, its twin, and orig/dest
Edge *e = &edge;
Edge *et = e->twin;
GridPoint &orig = e->orig;
GridPoint &dest = et->orig;
// Update next/prev edges
e->oprev->dnext = et->dnext;
e->dnext->oprev = et->oprev;
et->oprev->dnext = e->dnext;
et->dnext->oprev = e->oprev;
// Free edge and twin from edge_list
// so memory can be used for new edges
this->edge_list->RemoveEdge(*e);
this->edge_list->RemoveEdge(*et);
}
template <typename GridType, typename IterType>
void GridTriangulation<GridType, IterType>::Weld(Edge &in, Edge &out)
{
// Set edges before and after
// in (counterclockwise)
Edge *prev = out.oprev;
Edge *curr = ∈
Edge *next = &out;
// Update next/prev edges
next->oprev = curr;
curr->dnext = next;
curr->twin->oprev = prev;
prev->dnext = curr->twin;
}
template <typename GridType, typename IterType>
Edge *GridTriangulation<GridType, IterType>::Bridge(Edge &start, Edge &end)
{
Edge *in = &start;
Edge *out = &end;
GridPoint &orig = in->twin->orig;
GridPoint &dest = out->orig;
// Create bridging edge and its twin
Edge *e = this->AddEdgeAndTwin(orig, dest);
Edge *et = e->twin;
// Make appropriate welds to connect the
// new edges to the rest of the graph
this->Weld(*et, *(in->dnext));
this->Weld(*e, *out);
return e;
}
template <typename GridType, typename IterType>
void GridTriangulation<GridType, IterType>::Triangulate(GridPoint *points[], size_t num_points)
{
// Initialize memory for edges by creating an EdgeList
// and run the triangulation routine
// Note: ex contains some of the edges of the convex hull,
// not usefull for result of simple triangulation, so it is
// discarded
#pragma omp parallel
{
#pragma omp single
{
//double b = omp_get_wtime();
this->edge_list = new EdgeList(num_points);
ExtremeEdges *ex = this->TriangulateHorizontalThreaded(points, num_points);
delete ex;
//double e = omp_get_wtime();
//fprintf(stderr, "\tTriangulation took %f\n", e - b);
}
}
//double b = omp_get_wtime();
this->grid = new Grid<GridType, IterType>(this->width, this->height, points, num_points);
this->edge_list->SetGrid(this->grid);
//double e = omp_get_wtime();
//fprintf(stderr, "\tgrid setting took %f\n", e - b);
}
template <typename GridType, typename IterType>
ExtremeEdges *GridTriangulation<GridType, IterType>::TriangulateHorizontalThreaded(GridPoint *points[], size_t num_points)
{
// Base Cases /////////////////////////////////
if (num_points < THREADED_CUTOFF)
{
return this->TriangulateHorizontalSerial(points, num_points);
}
else if (num_points < 2)
{
return 0;
}
else if (num_points == 2)
{
return this->Triangulate2(points);
}
else if (num_points == 3)
{
return this->Triangulate3(points);
}
// Recursive Case /////////////////////////////
// Partition lexicographically (compare x/col, then y/row)
// on median of the list
size_t median = num_points / 2;
std::sort(points, points + num_points, LessThanPtrXY);
// Perform recursive triangulations of list halves
// Note alternating direction of cuts for better expected runtime
GridTriangulation *g_left = new GridTriangulation(this->width, this->height);
GridTriangulation *g_right = new GridTriangulation(this->width, this->height);
this->edge_list->SplitEdgeList(g_left->edge_list, g_right->edge_list, median);
ExtremeEdges *left_ex;
ExtremeEdges *right_ex;
#pragma omp task shared(left_ex)
{
left_ex = g_left->TriangulateVerticalThreaded(points, median);
}
#pragma omp task shared(right_ex)
{
right_ex = g_right->TriangulateVerticalThreaded(points + median, num_points - median);
}
#pragma omp taskwait
this->edge_list->MergeEdgeLists(*(g_left->edge_list), *(g_right->edge_list));
delete g_left;
delete g_right;
ExtremeEdges *ex = new ExtremeEdges();
// Use extreme edges from recursive triangulations to determine
// the lower common tangent (lct) of combined convex hull
Edge *left_edge = left_ex->right_edge_cw;
Edge *right_edge = right_ex->left_edge_ccw;
while (1)
{
if (Orientation(left_edge->orig, left_edge->twin->orig, right_edge->orig) > 0) left_edge = left_edge->twin->oprev->twin;
else if (Orientation(right_edge->orig, right_edge->twin->orig, left_edge->orig) < 0) right_edge = right_edge->dnext;
else break;
}
Edge *lct = this->Bridge(*(right_edge->oprev), *(left_edge->twin->dnext));
// Check if lct 'covers' extreme edges from either sub-triangulation
if (left_edge->orig == left_ex->left_edge_ccw->orig) left_ex->left_edge_ccw = lct->twin;
if (right_edge->orig == right_ex->right_edge_cw->orig) right_ex->right_edge_cw = lct;
// Perform merge of two sub-triangulations, ending with the
// creation of the upper common tangent (uct) of the combined
// convex hull
Edge *new_base = lct;
Edge *uct = 0;
while (new_base != 0)
{
uct = new_base;
new_base = this->NextCrossEdge(uct);
}
// Set the left/right extreme edges of the combined
// convex hull using the results from the sub-triangulations
ex->left_edge_ccw = left_ex->left_edge_ccw;
ex->right_edge_cw = right_ex->right_edge_cw;
// Set the bottom extreme edge of the combined
// convex hull using the lct as a starting guess
Edge *temp = lct->twin;
while (LessThanYX(temp->orig, temp->twin->orig)) temp = temp->oprev;
temp = temp->dnext;
while (LessThanYX(temp->twin->orig, temp->orig)) temp = temp->dnext;
ex->bottom_edge_ccw = temp;
// Set the top extreme edge of the combined
// convex hull using the uct as a starting guess
temp = uct->twin;
while (LessThanYX(temp->twin->orig, temp->orig)) temp = temp->twin->dnext->twin;
temp = temp->twin->oprev->twin;
while (LessThanYX(temp->orig, temp->twin->orig)) temp = temp->twin->oprev->twin;
ex->top_edge_cw = temp;
delete left_ex;
delete right_ex;
return ex;
}
template <typename GridType, typename IterType>
ExtremeEdges *GridTriangulation<GridType, IterType>::TriangulateVerticalThreaded(GridPoint *points[], size_t num_points)
{
// Base Cases /////////////////////////////////
if (num_points < THREADED_CUTOFF)
{
return this->TriangulateVerticalSerial(points, num_points);
}
else if (num_points < 2)
{
return 0;
}
else if (num_points == 2)
{
return this->Triangulate2(points);
}
else if (num_points == 3)
{
return this->Triangulate3(points);
}
// Recursive Case /////////////////////////////
// Partition (compare y/row, then -x/col) points on
// median of the list
size_t median = num_points / 2;
std::sort(points, points + num_points, LessThanPtrYX);
// Perform recursive triangulations of list halves
// Note alternating direction of cuts for better expected runtime
GridTriangulation *g_bottom = new GridTriangulation(this->width, this->height);
GridTriangulation *g_top = new GridTriangulation(this->width, this->height);
this->edge_list->SplitEdgeList(g_bottom->edge_list, g_top->edge_list, median);
ExtremeEdges *bottom_ex;
ExtremeEdges *top_ex;
#pragma omp task shared(bottom_ex)
{
bottom_ex = g_bottom->TriangulateHorizontalThreaded(points, median);
}
#pragma omp task shared(top_ex)
{
top_ex = g_top->TriangulateHorizontalThreaded(points + median, num_points - median);
}
#pragma omp taskwait
this->edge_list->MergeEdgeLists(*(g_bottom->edge_list), *(g_top->edge_list));
delete g_bottom;
delete g_top;
ExtremeEdges *ex = new ExtremeEdges();
// Use extreme edges from recursive triangulations to determine
// the right common tangent (rct) of combined convex hull
Edge *bottom_edge = bottom_ex->top_edge_cw;
Edge *top_edge = top_ex->bottom_edge_ccw;
while (1)
{
if (Orientation(bottom_edge->orig, bottom_edge->twin->orig, top_edge->orig) > 0) bottom_edge = bottom_edge->twin->oprev->twin;
else if (Orientation(top_edge->orig, top_edge->twin->orig, bottom_edge->orig) < 0) top_edge = top_edge->dnext;
else break;
}
Edge *rct = this->Bridge(*(top_edge->oprev), *(bottom_edge->twin->dnext));
// Check if rct 'covers' extreme edges from either sub-triangulation
if (bottom_edge->orig == bottom_ex->bottom_edge_ccw->orig) bottom_ex->bottom_edge_ccw = rct->twin;
if (top_edge->orig == top_ex->top_edge_cw->orig) top_ex->top_edge_cw = rct;
// Perform merge of two sub-triangulations, ending with the
// creation of the left common tangent (lct) of the combined
// convex hull
Edge *new_base = rct;
Edge *lct = 0;
while (new_base != 0)
{
lct = new_base;
new_base = this->NextCrossEdge(lct);
}
// Set the bottom/top extreme edges of the combined
// convex hull using the results from the sub-triangulations
ex->bottom_edge_ccw = bottom_ex->bottom_edge_ccw;
ex->top_edge_cw = top_ex->top_edge_cw;
// Set the right extreme edge of the combined
// convex hull using the rct as a starting guess
Edge *temp = rct;
while (LessThanXY(temp->twin->orig, temp->orig)) temp = temp->twin->dnext->twin;
temp = temp->twin->oprev->twin;
while (LessThanXY(temp->orig, temp->twin->orig)) temp = temp->twin->oprev->twin;
ex->right_edge_cw = temp;
// Set the left extreme edge of the combined
// convex hull using the lct as a starting guess
temp = lct;
while (LessThanXY(temp->orig, temp->twin->orig)) temp = temp->oprev;
temp = temp->dnext;
while (LessThanXY(temp->twin->orig, temp->orig)) temp = temp->dnext;
ex->left_edge_ccw = temp;
delete bottom_ex;
delete top_ex;
return ex;
}
template <typename GridType, typename IterType>
ExtremeEdges *GridTriangulation<GridType, IterType>::TriangulateHorizontalSerial(GridPoint *points[], size_t num_points)
{
// Base Cases /////////////////////////////////
if (num_points < 2)
{
return 0;
}
else if (num_points == 2)
{
return this->Triangulate2(points);
}
else if (num_points == 3)
{
return this->Triangulate3(points);
}
// Recursive Case /////////////////////////////
// Partition lexicographically (compare x/col, then y/row)
// on median of the list
size_t median = num_points / 2;
std::sort(points, points + num_points, LessThanPtrXY);
// Perform recursive triangulations of list halves
// Note alternating direction of cuts for better expected runtime
ExtremeEdges *left_ex = this->TriangulateVerticalSerial(points, median);
ExtremeEdges *right_ex = this->TriangulateVerticalSerial(points + median, num_points - median);
ExtremeEdges *ex = new ExtremeEdges();
// Use extreme edges from recursive triangulations to determine
// the lower common tangent (lct) of combined convex hull
Edge *left_edge = left_ex->right_edge_cw;
Edge *right_edge = right_ex->left_edge_ccw;
while (1)
{
if (Orientation(left_edge->orig, left_edge->twin->orig, right_edge->orig) > 0) left_edge = left_edge->twin->oprev->twin;
else if (Orientation(right_edge->orig, right_edge->twin->orig, left_edge->orig) < 0) right_edge = right_edge->dnext;
else break;
}
Edge *lct = this->Bridge(*(right_edge->oprev), *(left_edge->twin->dnext));
// Check if lct 'covers' extreme edges from either sub-triangulation
if (left_edge->orig == left_ex->left_edge_ccw->orig) left_ex->left_edge_ccw = lct->twin;
if (right_edge->orig == right_ex->right_edge_cw->orig) right_ex->right_edge_cw = lct;
// Perform merge of two sub-triangulations, ending with the
// creation of the upper common tangent (uct) of the combined
// convex hull
Edge *new_base = lct;
Edge *uct = 0;
while (new_base != 0)
{
uct = new_base;
new_base = this->NextCrossEdge(uct);
}
// Set the left/right extreme edges of the combined
// convex hull using the results from the sub-triangulations
ex->left_edge_ccw = left_ex->left_edge_ccw;
ex->right_edge_cw = right_ex->right_edge_cw;
// Set the bottom extreme edge of the combined
// convex hull using the lct as a starting guess
Edge *temp = lct->twin;
while (LessThanYX(temp->orig, temp->twin->orig)) temp = temp->oprev;
temp = temp->dnext;
while (LessThanYX(temp->twin->orig, temp->orig)) temp = temp->dnext;
ex->bottom_edge_ccw = temp;
// Set the top extreme edge of the combined
// convex hull using the uct as a starting guess
temp = uct->twin;
while (LessThanYX(temp->twin->orig, temp->orig)) temp = temp->twin->dnext->twin;
temp = temp->twin->oprev->twin;
while (LessThanYX(temp->orig, temp->twin->orig)) temp = temp->twin->oprev->twin;
ex->top_edge_cw = temp;
delete left_ex;
delete right_ex;
return ex;
}
template <typename GridType, typename IterType>
ExtremeEdges *GridTriangulation<GridType, IterType>::TriangulateVerticalSerial(GridPoint *points[], size_t num_points)
{
// Base Cases /////////////////////////////////
if (num_points < 2)
{
return 0;
}
else if (num_points == 2)
{
return this->Triangulate2(points);
}
else if (num_points == 3)
{
return this->Triangulate3(points);
}
// Recursive Case /////////////////////////////
// Partition (compare y/row, then -x/col) points on
// median of the list
size_t median = num_points / 2;
std::sort(points, points + num_points, LessThanPtrYX);
// Perform recursive triangulations of list halves
// Note alternating direction of cuts for better expected runtime
ExtremeEdges *bottom_ex = this->TriangulateHorizontalSerial(points, median);
ExtremeEdges *top_ex = this->TriangulateHorizontalSerial(points + median, num_points - median);
ExtremeEdges *ex = new ExtremeEdges();
// Use extreme edges from recursive triangulations to determine
// the right common tangent (rct) of combined convex hull
Edge *bottom_edge = bottom_ex->top_edge_cw;
Edge *top_edge = top_ex->bottom_edge_ccw;
while (1)
{
if (Orientation(bottom_edge->orig, bottom_edge->twin->orig, top_edge->orig) > 0) bottom_edge = bottom_edge->twin->oprev->twin;
else if (Orientation(top_edge->orig, top_edge->twin->orig, bottom_edge->orig) < 0) top_edge = top_edge->dnext;
else break;
}
Edge *rct = this->Bridge(*(top_edge->oprev), *(bottom_edge->twin->dnext));
// Check if rct 'covers' extreme edges from either sub-triangulation
if (bottom_edge->orig == bottom_ex->bottom_edge_ccw->orig) bottom_ex->bottom_edge_ccw = rct->twin;
if (top_edge->orig == top_ex->top_edge_cw->orig) top_ex->top_edge_cw = rct;
// Perform merge of two sub-triangulations, ending with the
// creation of the left common tangent (lct) of the combined
// convex hull
Edge *new_base = rct;
Edge *lct = 0;
while (new_base != 0)
{
lct = new_base;
new_base = this->NextCrossEdge(lct);
}
// Set the bottom/top extreme edges of the combined
// convex hull using the results from the sub-triangulations
ex->bottom_edge_ccw = bottom_ex->bottom_edge_ccw;
ex->top_edge_cw = top_ex->top_edge_cw;
// Set the right extreme edge of the combined
// convex hull using the rct as a starting guess
Edge *temp = rct;
while (LessThanXY(temp->twin->orig, temp->orig)) temp = temp->twin->dnext->twin;
temp = temp->twin->oprev->twin;
while (LessThanXY(temp->orig, temp->twin->orig)) temp = temp->twin->oprev->twin;
ex->right_edge_cw = temp;
// Set the left extreme edge of the combined
// convex hull using the lct as a starting guess
temp = lct;
while (LessThanXY(temp->orig, temp->twin->orig)) temp = temp->oprev;
temp = temp->dnext;
while (LessThanXY(temp->twin->orig, temp->orig)) temp = temp->dnext;
ex->left_edge_ccw = temp;
delete bottom_ex;
delete top_ex;
return ex;
}
template <typename GridType, typename IterType>
ExtremeEdges *GridTriangulation<GridType, IterType>::Triangulate2(GridPoint *points[])
{
ExtremeEdges *ex = new ExtremeEdges();
// Create edge and twin between two points
GridPoint a = *(points[0]);
GridPoint b = *(points[1]);
Edge *e = this->AddEdgeAndTwin(a, b);
// Check lexicographic order (compare x/col, then y/row)
// of two points to determine left/right extreme edges
if (LessThanXY(a, b))
{
ex->left_edge_ccw = e;
ex->right_edge_cw = e->twin;
}
else
{
ex->left_edge_ccw = e->twin;
ex->right_edge_cw = e;
}
// Check lexicographic order (compare y/row, then -x/col)
// of two points to determine top/bottom extreme edges
if (LessThanYX(a, b))
{
ex->bottom_edge_ccw = e;
ex->top_edge_cw = e->twin;
}
else
{
ex->bottom_edge_ccw = e->twin;
ex->top_edge_cw = e;
}
return ex;
}
template <typename GridType, typename IterType>
ExtremeEdges *GridTriangulation<GridType, IterType>::Triangulate3(GridPoint *points[])
{
ExtremeEdges *ex = new ExtremeEdges();
// Sort points lexicographically (compare x/col, then y/row)
std::sort(points, points + 3, LessThanPtrXY);
GridPoint a = *(points[0]);
GridPoint b = *(points[1]);
GridPoint c = *(points[2]);
// Add edges/twins between a,b and b,c
Edge *e1 = this->AddEdgeAndTwin(a, b);
Edge *e2 = this->AddEdgeAndTwin(b, c);
this->Weld(*e1, *e2);
// Check sign of area of triangle a,b,c
// to:
// a) see if third edge is necessary
// b) determine right/left extreme edges
Edge *e3;
INT64 signed_area = Orientation(a, b, c);
if (signed_area > 0) // a, b, c is a counterclockwise-oriented triangle
{
e3 = this->Bridge(*e2, *e1);
ex->left_edge_ccw = e1;
ex->right_edge_cw = e2->twin;
}
else if (signed_area < 0) // a, b, c is a clockwise-oriented triangle
{
e3 = this->Bridge(*e2, *e1);
ex->left_edge_ccw = e3->twin;
ex->right_edge_cw = e3;
}
else // a, b, c are collinear
{
ex->left_edge_ccw = e1;
ex->right_edge_cw = e2->twin;
}
// Sort points (compare y/row, -x/col)
std::sort(points, points + 3, LessThanPtrYX);
a = *(points[0]);
b = *(points[1]);
c = *(points[2]);
// Collect edges previously created so that
// e1 is the edge from a to b
// and e2, e3 follow in order
//Edge *e = this->GetEdgeOut(a);
Edge *e = (e1->orig == a) ? e1 : (e2->orig == a ? e2 : e2->twin);
e1 = (e->twin->orig == b) ? e : e->twin->dnext;
e2 = e1->dnext;
e3 = e2->dnext;
// Check sign of area of triangle a,b,c
// to determine top/bottom extreme edges
signed_area = Orientation(a, b, c);
if (signed_area >= 0)
{
ex->bottom_edge_ccw = e1;
ex->top_edge_cw = e2->twin;
}
else
{
ex->bottom_edge_ccw = e3->twin;
ex->top_edge_cw = e3;
}
return ex;
}
//Main retriangulation method, begins recursive divide and conquer
template <typename GridType, typename IterType>
void GridTriangulation<GridType, IterType>::Retriangulate(GridPoint *blunders[], size_t num_blunders)
{
#pragma omp parallel
{
#pragma omp single
{
std::vector<GridPoint*> blunder_vector(blunders, blunders+num_blunders);
this->RetriangulateHorizontal(blunder_vector,false);
}
}
this->grid->ReduceNumOfElems(num_blunders);
}
template <typename GridType, typename IterType>
void GridTriangulation<GridType, IterType>::RetriangulateVertical(std::vector<GridPoint*> blunders, bool nosplit)
{
if(blunders.size() > RETRI_CUTOFF)
{
//Split blunders in half. Will be sorted into three sets: top and bottom portion (Unlinkeds), and middle (Linked).
int med = blunders.size()/2;
std::sort(blunders.begin(), blunders.end(), LessThanPtrYX);
int len1 = 0;
int len2 = 0;
int linked_len = 0;
std::vector<GridPoint*> Unlinked1;
//There should never be more than half of the total blunders in either half given we split by # of blunders and some are in the middle, so reserve med
Unlinked1.reserve(med);
std::vector<GridPoint*> Unlinked2;
Unlinked2.reserve(med);
std::vector<GridPoint*> Linked;
for(auto it = blunders.begin(); it!=blunders.end(); it++)
{
GridPoint *p = *it;
Edge *e = this->GetEdgeOut(*p);
if(e!=0)
{
Edge *f = e;
GridPoint mid = **(blunders.begin()+med);
bool unlink = true;
do
{
//If edge spans boundary, point is linked.
if(LessThanYX(mid, f->twin->orig) != LessThanYX(mid, *p))
{
unlink = false;
break;
}
}while((f=f->twin->dnext)!=e);
if(unlink)
{
if((it-blunders.begin())<med)
{
Unlinked1.push_back(p);
}else
{
Unlinked2.push_back(p);
}
}else
{
Linked.push_back(p);
}
}
}
bool center_only = Unlinked1.size()==0 && Unlinked2.size()==0;
if (!center_only)
{
//Spawn two tasks to remove unlinked blunders on each side
GridTriangulation *g_bottom = new GridTriangulation(this->width, this->height);
GridTriangulation *g_top = new GridTriangulation(this->width, this->height);
g_bottom->edge_list = this->edge_list->MakeLocalCopy();
g_top->edge_list = this->edge_list->MakeLocalCopy();
g_bottom->grid = this->grid;
g_top->grid = this->grid;
#pragma omp task
{
g_bottom->RetriangulateHorizontal(Unlinked1,false);
}
#pragma omp task
{
g_top->RetriangulateHorizontal(Unlinked2,false);
}
//Wait for both sides to finish before eliminating linked blunders. We can Recurse on the linked blunders as well but this is unlikely to provide much of an increase in performance
#pragma omp taskwait
// No need to maintain unused edge list during retriangulation
//this->edge_list->MergeUnused(*(g_bottom->edge_list));
//this->edge_list->MergeUnused(*(g_top->edge_list));
g_bottom->grid = 0;
g_top->grid = 0;
delete g_bottom;
delete g_top;
}
// Handle center strip specially to avoid infinite recursion
if (!(center_only&&nosplit))
{
this->RetriangulateHorizontal(Linked,center_only);
}
else
{
for(auto it = blunders.begin(); it!=blunders.end(); it++)
{
this->RemovePointAndRetriangulate(**it);
}
}
}else
{
//Remove blunders and retriangulate. This is base case
for(auto it = blunders.begin(); it!=blunders.end(); it++)
{
this->RemovePointAndRetriangulate(**it);
}
}
}
template <typename GridType, typename IterType>
void GridTriangulation<GridType, IterType>::RetriangulateHorizontal(std::vector<GridPoint*> blunders, bool nosplit)
{
if(blunders.size() > RETRI_CUTOFF)
{
//Same as vertical split except now there are left and right portions (Unlinked) as well as a middle portion (Linked).
int med = blunders.size()/2;
std::sort(blunders.begin(), blunders.end(), LessThanPtrXY);
std::vector<GridPoint*> Unlinked1;
Unlinked1.reserve(med);
std::vector<GridPoint*> Unlinked2;
Unlinked2.reserve(med);
std::vector<GridPoint*> Linked;
for(auto it = blunders.begin(); it!=blunders.end(); it++)
{
GridPoint *p = *it;
Edge *e = this->GetEdgeOut(*p);
if(e!=0)
{
Edge *f = e;
GridPoint mid = **(blunders.begin()+med);
bool unlink = true;
do
{
if(LessThanXY(mid, f->twin->orig)!=LessThanXY(mid, *p))
{
unlink = false;
break;
}
}while((f=f->twin->dnext)!=e);
if(unlink)
{
if((it-blunders.begin())<med)
{
Unlinked1.push_back(p);
}else
{
Unlinked2.push_back(p);
}
}else
{
Linked.push_back(p);
}
}
}
bool center_only = Unlinked1.size()==0 && Unlinked2.size()==0;
if (!center_only)
{
GridTriangulation *g_left = new GridTriangulation(this->width, this->height);
GridTriangulation *g_right = new GridTriangulation(this->width, this->height);
g_left->edge_list = this->edge_list->MakeLocalCopy();
g_right->edge_list = this->edge_list->MakeLocalCopy();
g_left->grid = this->grid;
g_right->grid = this->grid;
#pragma omp task
{
g_left->RetriangulateVertical(Unlinked1,false);
}
#pragma omp task
{
g_right->RetriangulateVertical(Unlinked2,false);
}
#pragma omp taskwait
// No need to maintain unused edge list during retriangulation
//this->edge_list->MergeUnused(*(g_left->edge_list));
//this->edge_list->MergeUnused(*(g_right->edge_list));
g_left->grid = 0;
g_right->grid = 0;
delete g_left;
delete g_right;
}
// Handle center strip specially to avoid infinite recursion
if (!(center_only&&nosplit))
{
this->RetriangulateVertical(Linked,center_only);
}
else
{
for(auto it = blunders.begin(); it!=blunders.end(); it++)
{
this->RemovePointAndRetriangulate(**it);
}
}
}else
{
for(auto it = blunders.begin(); it!=blunders.end(); it++)
{
this->RemovePointAndRetriangulate(**it);
}
}
}
template <typename GridType, typename IterType>
Edge *GridTriangulation<GridType, IterType>::NextCrossEdge(Edge *base)
{
// Determine first valid candidate for next left-to-right
// bridge in merge operation, setting valid_l to false
// if no such candidates exist
Edge *l_cand = base->dnext;
bool valid_l = Orientation(base->orig, l_cand->orig, l_cand->twin->orig) < 0;
if (valid_l)
{
Edge *next_cand = l_cand->twin->dnext;
while (InCircle(l_cand->orig, base->orig, l_cand->twin->orig, next_cand->twin->orig) > 0)
{
this->RemoveEdgeAndTwin(*l_cand);
l_cand = next_cand;
next_cand = l_cand->twin->dnext;
}
}
// Determine first valid candidate for next right-to-left
// bridge in merge operation, setting valid_r to false
// if no such candidates exist
Edge *r_cand = base->oprev->twin;
bool valid_r = Orientation(base->twin->orig, base->orig, r_cand->twin->orig) > 0;
if (valid_r)
{
Edge *next_cand = r_cand->oprev->twin;
while (InCircle(base->twin->orig, base->orig, r_cand->twin->orig, next_cand->twin->orig) > 0)
{
this->RemoveEdgeAndTwin(*r_cand);
r_cand = next_cand;
next_cand = r_cand->oprev->twin;
}
}
// Return null if no candidates were found,
// otherwise pick valid candidate.
// Existence of delaunay triangulation guarentees
// candidate, and in for general position points,
// it is unique. In special case where both
// left-to-right and right-to-left candidates
// are valid, the code arbitrarily select the
// left-to-right candidate
if (!valid_l && !valid_r) return 0;
else if (!valid_l || (valid_r && (InCircle(l_cand->twin->orig, l_cand->orig, r_cand->orig, r_cand->twin->orig)) > 0)) return (this->Bridge(*base, *(r_cand->twin)))->twin;
else return (this->Bridge(*l_cand, *base))->twin;
}
/* Assumes point is included in full
* triangulation.
* Returns 0 if point is not on
* convex hull, returns the clockwise
* edge out of p on the convex hull
* otherwise.
*/
template <typename GridType, typename IterType>
Edge *GridTriangulation<GridType, IterType>::OnConvexHull(const GridPoint &p)
{
// Check that point is actually part of triangulation,
// that is, has an edge out
Edge *e = this->GetEdgeOut(p);
if (e == 0) return 0;
// Iterate over adjacent edges out of p
// and check for reflex angles
Edge *f = e;
do
{
GridPoint &a = f->twin->orig;
GridPoint &b = f->twin->dnext->twin->orig;
GridPoint &c = f->orig;
if (Orientation(a, b, c) <= 0) return f;
} while ((f = f->twin->dnext) != e);
return 0;
}
template <typename GridType, typename IterType>
void GridTriangulation<GridType, IterType>::TriangulateEmptyPolygon(Edge &edge)
{
Edge *e = &edge;
int n = 1;
while ((e = e->dnext) != &edge) n++;
// Base Case
if (n <= 3) return;
//Extended base cases from [3]
if( n == 4 )
{