poly2tri-rs 0.1.2

An idiomatic and fast Constrained Delaunay Triangulation library
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
use crate::advancing_front::{AdvancingFront, NodeId, NodeRef};
use crate::points::{Points, PointsBuilder};
use crate::triangles::TriangleId;
use crate::triangles::TriangleStore;
use crate::utils::{in_circle, in_scan_area, orient_2d, Angle, Orientation};
use crate::{shape::*, Context, PointId, Triangle};

/// Observer for sweeper, used to monitor how sweeper works, quite useful
/// for visual debugging when things goes wrong. Check example's draw.
#[allow(unused_variables)]
pub trait Observer {
    /// A point_event processed
    fn enter_point_event(&mut self, point_id: PointId, context: &Context) {}
    fn exit_point_event(&mut self, point_id: PointId, context: &Context) {}

    /// An edge event processed
    fn edge_event(&mut self, edge: Edge, context: &Context) {}

    /// Sweep process done
    fn sweep_done(&mut self, context: &Context) {}

    /// The result finalized, holes, fake points etc cleaned.
    fn finalized(&mut self, context: &Context) {}

    /// About to legalize for triangle
    #[inline]
    fn will_legalize(&mut self, triangle_id: TriangleId, context: &Context) {}

    /// A single step inside one legalization process
    #[inline]
    fn legalize_step(&mut self, triangle_id: TriangleId, context: &Context) {}

    /// A rotate happened
    #[inline]
    fn triangle_rotated(
        &mut self,
        triangle_id: TriangleId,
        opposite_triangle_id: TriangleId,
        context: &Context,
    ) {
    }

    /// The triangle legalized
    #[inline]
    fn legalized(&mut self, triangel_id: TriangleId, context: &Context) {}
}

/// Default dummy observer, blank impl, so all calls should be optimized out by compiler.
impl Observer for () {}

/// Sweeper Builder
///
/// # Example
/// ```rust
///    use poly2tri_rs::{SweeperBuilder, Point};
///
///    let builder = SweeperBuilder::new(vec![
///        Point::new(-10., -10.),
///        Point::new(810., -10.),
///        Point::new(810., 810.),
///        Point::new(-10., 810.),
///    ]).add_steiner_points(vec![
///        Point::new(50., 50.),
///    ]).add_hole(vec![
///        Point::new(400., 400.),
///        Point::new(600., 400.),
///        Point::new(600., 600.),
///        Point::new(400., 600.),
///    ]);
///    let sweeper = builder.build();
/// ```

#[derive(Clone)]
pub struct SweeperBuilder {
    points_builder: PointsBuilder,
}

impl SweeperBuilder {
    /// Create a new Builder with polyline
    /// There should be only one polyline, and multiple holes and steiner points supported
    pub fn new(polyline: Vec<Point>) -> Self {
        let mut points_builder = PointsBuilder::with_capacity(polyline.len());
        parse_polyline(polyline, &mut points_builder);

        Self { points_builder }
    }

    /// Add a single sparse `Point`, there is no edge attached to it
    /// NOTE: if the point locates outside of polyline, then it has no
    /// effect on the final result
    pub fn add_steiner_point(mut self, point: Point) -> Self {
        self.points_builder.add_steiner_point(point);
        self
    }

    /// Add multiple [`Point`], batch version for `Self::add_point`
    pub fn add_steiner_points(mut self, points: impl IntoIterator<Item = Point>) -> Self {
        let _ = self.points_builder.add_steiner_points(points);
        self
    }

    /// Add a hole defined by polyline.
    pub fn add_hole(mut self, polyline: Vec<Point>) -> Self {
        parse_polyline(polyline, &mut self.points_builder);
        self
    }

    /// Add holes
    pub fn add_holes(mut self, holes: impl IntoIterator<Item = Vec<Point>>) -> Self {
        for polyline in holes.into_iter() {
            self = self.add_hole(polyline);
        }
        self
    }

    /// build the sweeper
    pub fn build(self) -> Sweeper {
        let points = self.points_builder.build();
        Sweeper { points }
    }
}

/// Main interface, user should grab a new Sweeper by [`SweeperBuilder::build`]
#[derive(Clone)]
pub struct Sweeper {
    points: Points,
}

/// The result of triangulate
pub struct Triangles {
    /// points store, it includes all points, including ones in hole
    points: Points,
    /// including all triangles, including ones in hole
    triangles: TriangleStore,
    /// final result `TriangleId`s
    result: Vec<TriangleId>,

    /// iterator next cursor
    next: usize,
}

impl Iterator for Triangles {
    type Item = Triangle;

    fn next(&mut self) -> Option<Self::Item> {
        if self.next < self.result.len() {
            let index = self.next;
            self.next += 1;

            // safety: just checked index less than len
            let tri_id = unsafe { self.result.get_unchecked(index) };
            let triangle = tri_id.get(&self.triangles);

            return Some(Triangle {
                points: [
                    triangle.points[0].get(&self.points),
                    triangle.points[1].get(&self.points),
                    triangle.points[2].get(&self.points),
                ],
            });
        } else {
            None
        }
    }
}

impl Sweeper {
    /// Run trianglate with dummy observer
    pub fn triangulate(self) -> Triangles {
        self.triangulate_with_observer(&mut ())
    }

    /// Run triangulate with observer
    pub fn triangulate_with_observer(self, observer: &mut impl Observer) -> Triangles {
        let mut triangles = TriangleStore::with_capacity(self.points.len() * 3);

        let initial_triangle = triangles.insert(InnerTriangle::new(
            self.points.get_id_by_y(0).unwrap(),
            self.points.head,
            self.points.tail,
        ));

        // create the advancing front with initial triangle
        let mut advancing_front = AdvancingFront::new(
            triangles.get(initial_triangle).unwrap(),
            initial_triangle,
            &self.points,
        );

        let mut context = Context::new(&self.points, &mut triangles, &mut advancing_front);

        Self::sweep_points(&mut context, observer);
        observer.sweep_done(&context);

        Self::finalize_polygon(&mut context);
        observer.finalized(&context);

        // take result out of context
        let result = context.result;

        Triangles {
            points: self.points,
            triangles,
            result,

            next: 0,
        }
    }
}

impl Sweeper {
    fn sweep_points(context: &mut Context, observer: &mut impl Observer) {
        for (point_id, point, edges) in context.points.iter_point_by_y(1) {
            observer.enter_point_event(point_id, context);
            Self::point_event(point_id, point, context, observer);
            observer.exit_point_event(point_id, context);

            for p in edges {
                let edge = Edge { p, q: point_id };
                Self::edge_event(edge, point, context, observer);

                observer.edge_event(edge, context);
            }

            debug_assert!(Self::verify_triangles(context));
        }
    }

    fn finalize_polygon(context: &mut Context) -> Option<()> {
        // get an internal triangle to start with
        // the first node is head, artificial point, so skip
        let node = context.advancing_front.nth(1)?;

        let mut t = node.triangle?;

        loop {
            if let Some(tri) = context.triangles.get(t) {
                if !tri.constrained_edge_cw(node.point_id()) {
                    t = tri.neighbor_ccw(node.point_id());
                } else {
                    break;
                }
            } else {
                // no valid triangle found, just break
                break;
            }
        }

        if !t.invalid() {
            Self::clean_mesh(t, context);
        }

        Some(())
    }

    fn clean_mesh(triangle_id: TriangleId, context: &mut Context) -> Option<()> {
        // id and from, it should not trigger from again
        let mut triangles = Vec::<(TriangleId, TriangleId)>::with_capacity(context.points.len());
        triangles.push((triangle_id, TriangleId::INVALID));

        while let Some((t, from)) = triangles.pop() {
            if t.invalid() {
                continue;
            }

            let tri = context.triangles.get_mut(t).unwrap();

            if !tri.interior {
                tri.interior = true;
                context.result.push(t);

                for i in 0..3 {
                    if !tri.is_constrained(i) {
                        let new_t = tri.neighbors[i];
                        if new_t != from {
                            triangles.push((new_t, t));
                        }
                    }
                }
            }
        }

        Some(())
    }
}

// first need to visualize each step, then trouble shoot
// print detailed steps, like what changes this going to address.

/// Point event related methods
impl Sweeper {
    fn point_event(
        point_id: PointId,
        point: Point,
        context: &mut Context,
        observer: &mut impl Observer,
    ) {
        let node = context.advancing_front.locate_node(point).unwrap();
        let node_id = node.get_node_id();
        let next_node = node.next().unwrap();
        let node_point = node.point();

        let triangle = context.triangles.insert(InnerTriangle::new(
            point_id,
            node.point_id(),
            next_node.point_id(),
        ));
        let node_triangle = node.triangle.unwrap();
        context.triangles.mark_neighbor(node_triangle, triangle);
        context.advancing_front.insert(point_id, point, triangle);

        Self::legalize(triangle, context, observer);

        // in middle case, the node's x should be less than point'x
        // in left case, they are same.
        if point.x <= node_point.x + f64::EPSILON {
            Self::fill_one(node_id, context, observer);
        }

        Self::fill_advancing_front(point, context, observer);
    }

    /// helper function to check wether triangle is legal
    fn is_legalize(triangle_id: TriangleId, context: &Context) -> [TriangleId; 3] {
        let mut result = [TriangleId::INVALID; 3];
        for point_idx in 0..3 {
            let triangle = context.triangles.get_unchecked(triangle_id);
            let opposite_triangle_id = triangle.neighbors[point_idx];
            let Some(opposite_triangle) = context.triangles.get(opposite_triangle_id) else {
                continue;
            };

            let p = triangle.points[point_idx];
            let op = opposite_triangle.opposite_point(&triangle, p);
            let oi = opposite_triangle.point_index(op).unwrap();

            if opposite_triangle.is_constrained(oi) {
                continue;
            }

            let inside = unsafe {
                in_circle(
                    context.points.get_point_uncheck(p),
                    context.points.get_point_uncheck(triangle.point_ccw(p)),
                    context.points.get_point_uncheck(triangle.point_cw(p)),
                    context.points.get_point_uncheck(op),
                )
            };

            if inside {
                result[point_idx] = opposite_triangle_id;
            }
        }

        result
    }

    /// legalize the triangle
    fn legalize(triangle_id: TriangleId, context: &mut Context, observer: &mut impl Observer) {
        observer.will_legalize(triangle_id, context);

        // keeps record of all touched triangles, after legalize finished
        // need to remap all to the advancing front
        let mut legalized_triangles = std::mem::take(&mut context.legalize_remap_tids);

        // record the task and who triggered it
        let mut task_queue = std::mem::take(&mut context.legalize_task_queue);
        task_queue.push(triangle_id);
        legalized_triangles.push(triangle_id);

        while let Some(triangle_id) = task_queue.pop() {
            for point_idx in 0..3 {
                let triangle = triangle_id.get(&context.triangles);
                // skip legalize for constrained_edge
                if triangle.is_constrained(point_idx) || triangle.is_delaunay(point_idx) {
                    continue;
                }

                let opposite_triangle_id = triangle.neighbors[point_idx];
                if opposite_triangle_id.invalid() {
                    continue;
                };
                let opposite_triangle = opposite_triangle_id.get(&context.triangles);

                let p = triangle.points[point_idx];
                let op = opposite_triangle.opposite_point(&triangle, p);

                let illegal = in_circle(
                    p.get(&context.points),
                    triangle.point_ccw(p).get(&context.points),
                    triangle.point_cw(p).get(&context.points),
                    op.get(&context.points),
                );
                if illegal {
                    observer.triangle_rotated(triangle_id, opposite_triangle_id, context);
                    // rotate shared edge one vertex cw to legalize it
                    let need_remap = Self::rotate_triangle_pair(
                        triangle_id,
                        p,
                        opposite_triangle_id,
                        op,
                        context.triangles,
                    );

                    // set the delaunay flag for the edge we just fixed
                    {
                        let (t, ot) = unsafe {
                            context
                                .triangles
                                .get_mut_two(triangle_id, opposite_triangle_id)
                        };

                        let (t_idx, ot_idx) = t.common_edge_index(ot).unwrap();
                        t.set_delaunay(t_idx, true);
                        ot.set_delaunay(ot_idx, true);
                    }

                    task_queue.push(triangle_id);
                    task_queue.push(opposite_triangle_id);

                    if need_remap {
                        legalized_triangles.push(triangle_id);
                        legalized_triangles.push(opposite_triangle_id);
                    }
                    break;
                } else {
                    // though we can set delaunay edge to prevent future recalulate
                    // it turns out slower, it means the recalculation is not many
                }
            }

            observer.legalize_step(triangle_id, context);
        }

        for triangle_id in legalized_triangles.drain(..) {
            Self::map_triangle_to_nodes(triangle_id, context);
        }

        {
            // give back the task queue
            context.legalize_task_queue = task_queue;
            context.legalize_remap_tids = legalized_triangles;
        }

        observer.legalized(triangle_id, context);
    }

    /// Rotate the triangle pair, returns two flag indicate (t, ot) whether candidate for af remap
    fn rotate_triangle_pair(
        t_id: TriangleId,
        p: PointId,
        ot_id: TriangleId,
        op: PointId,
        triangles: &mut TriangleStore,
    ) -> bool {
        let (t, ot) = unsafe { triangles.get_mut_two(t_id, ot_id) };

        let n1 = t.neighbor_ccw(p);
        let n2 = t.neighbor_cw(p);
        let n3 = ot.neighbor_ccw(op);
        let n4 = ot.neighbor_cw(op);

        let ea1 = t.edge_attr_ccw(p);
        let ea2 = t.edge_attr_cw(p);
        let ea3 = ot.edge_attr_ccw(op);
        let ea4 = ot.edge_attr_cw(op);

        // rotate shared edge one vertex cw to legalize it
        t.rotate_cw(p, op);
        ot.rotate_cw(op, p);

        t.set_edge_attr_cw(p, ea2);
        t.set_edge_attr_ccw(op, ea3);
        ot.set_edge_attr_ccw(p, ea1);
        ot.set_edge_attr_cw(op, ea4);

        t.clear_neighbors();
        ot.clear_neighbors();

        TriangleStore::mark_neighbor_for_two_mut(t_id, ot_id, t, ot);

        let (t, ot, t_n1, t_n2, t_n3, t_n4) =
            unsafe { triangles.get_mut_six(t_id, ot_id, n1, n2, n3, n4) };

        if let Some(t_n2) = t_n2 {
            TriangleStore::mark_neighbor_for_two_mut(t_id, n2, t, t_n2);
        }
        if let Some(t_n3) = t_n3 {
            TriangleStore::mark_neighbor_for_two_mut(t_id, n3, t, t_n3);
        }
        if let Some(t_n1) = t_n1 {
            TriangleStore::mark_neighbor_for_two_mut(ot_id, n1, ot, t_n1);
        }
        if let Some(t_n4) = t_n4 {
            TriangleStore::mark_neighbor_for_two_mut(ot_id, n4, ot, t_n4);
        }

        n1.invalid() || n2.invalid() || n3.invalid() || n4.invalid()
    }

    /// update advancing front node's triangle
    fn map_triangle_to_nodes(triangle_id: TriangleId, context: &mut Context) {
        let triangle = triangle_id.get(&context.triangles);
        for i in 0..3 {
            if triangle.neighbors[i].invalid() {
                let point = unsafe {
                    context
                        .points
                        .get_point_uncheck(triangle.point_cw(triangle.points[i]))
                };
                context.advancing_front.update_triangle(point, triangle_id);
            }
        }
    }

    /// fill the node with one triangle.
    /// Note: The moment it filled, advancing_front is modified.
    /// if the node is covered by another triangle, then it is deleted from advancing_front.
    /// all following advancing front lookup is affected.
    /// Returns the new next's point info.
    fn fill_one(
        node: NodeId,
        context: &mut Context,
        observer: &mut impl Observer,
    ) -> Option<FillOne> {
        let node = context.advancing_front.get_node_with_id(node).unwrap();
        let prev_node = node.prev()?;
        let next_node = node.next()?;

        // After fill and legalize, next's node won't change. So we save a version here
        // why: Most of time, after fill, external code needs to query the new
        //      next/prev and check whether more work needs to do. The checking logic
        //      only requires point info.
        let fill_one_result = FillOne {
            prev: prev_node.get_node_id(),
            next: next_node.get_node_id(),
        };

        let new_triangle = context.triangles.insert(InnerTriangle::new(
            prev_node.point_id(),
            node.point_id(),
            next_node.point_id(),
        ));

        context
            .triangles
            .mark_neighbor(new_triangle, prev_node.triangle.unwrap());
        context
            .triangles
            .mark_neighbor(new_triangle, node.triangle.unwrap());

        // update prev_node's triangle to newly created and delete the node.
        // node is covered by new triangle.
        // safety: prev_node and node is valid till this point, advanceing_front can not changed
        //       under the hood, so the index is still valid
        unsafe {
            context.advancing_front.update_and_delete_by_index(
                prev_node.index(),
                prev_node.point_id(),
                new_triangle,
                node.index(),
            )
        };

        // legalize works on existing triangles, no new triangle will be created
        // that ganrentees next point won't change
        Self::legalize(new_triangle, context, observer);

        Some(fill_one_result)
    }

    fn fill_advancing_front(
        node_point: Point,
        context: &mut Context,
        observer: &mut impl Observer,
    ) {
        let node_id = context
            .advancing_front
            .get_node(node_point)
            .unwrap()
            .get_node_id();

        {
            // fill right holes
            let mut node_id = node_id.clone();
            while let Some(next_node) = context.advancing_front.locate_next_node(node_id) {
                if next_node.next().is_some() {
                    // if HoleAngle exceeds 90 degrees then break
                    if Self::should_fill(&next_node) {
                        break;
                    }
                    let next_node_id = next_node.get_node_id();

                    node_id = match Self::fill_one(next_node_id, context, observer) {
                        Some(fill_one) => fill_one.next,
                        None => next_node_id,
                    };
                } else {
                    break;
                }
            }
        }

        {
            // fill left holes
            let mut node_id = node_id.clone();

            while let Some(prev_node) = context.advancing_front.locate_prev_node(node_id) {
                if prev_node.prev().is_some() {
                    // if HoleAngle exceeds 90 degrees then break
                    if Self::should_fill(&prev_node) {
                        break;
                    }

                    node_id = prev_node.get_node_id();
                    Self::fill_one(node_id, context, observer);
                } else {
                    break;
                }
            }
        }

        // fill right basins
        if Self::basin_angle_satisfy(node_id, context) {
            Self::fill_basin(node_id, context, observer);
        }
    }

    fn should_fill(node: &NodeRef) -> bool {
        let next = node.next().unwrap();
        let prev_node = node.prev().unwrap();

        let angle = crate::utils::Angle::new(node.point(), next.point(), prev_node.point());

        if angle.is_negative() {
            // negative means next -> node -> prev is cw, then it is not a hole
            return true;
        } else if !angle.exceeds_90_degree() {
            // we only fill holes with angle less than PI/2
            // otherwise we generate many bad shaped triangles
            // that needs rotated later
            return false;
        }

        if let Some(next_next) = next.next() {
            if Angle::new(node.point(), next_next.point(), prev_node.point())
                .between_0_to_90_degree()
            {
                return false;
            }
        }

        if let Some(prev_prev) = prev_node.prev() {
            if Angle::new(node.point(), next.point(), prev_prev.point()).between_0_to_90_degree() {
                return false;
            }
        }

        true
    }
}

struct FillOne {
    prev: NodeId,
    next: NodeId,
}

#[derive(Debug)]
struct ConstrainedEdge {
    constrained_edge: Edge,
    p: Point,
    q: Point,
    /// Whether the constrained edge is "right" edge, p.x larger than q.x
    right: bool,
}

impl ConstrainedEdge {
    fn p_id(&self) -> PointId {
        self.constrained_edge.p
    }

    fn q_id(&self) -> PointId {
        self.constrained_edge.q
    }

    fn with_q(&self, q: PointId, context: &Context) -> Self {
        let q_point = q.get(&context.points);
        Self {
            constrained_edge: Edge {
                p: self.constrained_edge.p,
                q,
            },
            p: self.p,
            q: q_point,
            right: self.p.x > q_point.x,
        }
    }
}

/// EdgeEvent related methods
impl Sweeper {
    fn edge_event(edge: Edge, q: Point, context: &mut Context, observer: &mut impl Observer) {
        let p = edge.p.get(&context.points);

        let constrain_edge = ConstrainedEdge {
            constrained_edge: edge,
            p,
            q,
            right: p.x > q.x,
        };

        {
            // check and fill
            let node = context.advancing_front.get_node_with_cache(q).unwrap();

            let triangle_id = node.triangle.unwrap();
            let node_id = node.get_node_id();
            if Self::try_mark_edge_for_triangle(edge.p, edge.q, triangle_id, context) {
                // the edge is already an edge of the triangle, return
                return;
            }

            // for now we will do all needed filling
            Self::fill_edge_event(&constrain_edge, node_id, context, observer);
        }

        // node's triangle may changed, get the latest
        let triangle = context
            .advancing_front
            .get_node_with_cache(q)
            .unwrap()
            .triangle
            .unwrap();

        // this triangle crosses constraint so let's flippin start!
        let mut triangle_ids = std::mem::take(&mut context.triangle_id_queue);
        Self::edge_event_process(
            edge.p,
            edge.q,
            &constrain_edge,
            triangle,
            edge.q,
            &mut triangle_ids,
            context,
        );

        for triangle_id in triangle_ids.drain(..) {
            Self::legalize(triangle_id, context, observer);
        }
        context.triangle_id_queue = triangle_ids;
    }

    /// try mark edge for triangle if the constrained edge already is a edge
    /// returns `true` if yes, otherwise `false`
    fn try_mark_edge_for_triangle(
        p: PointId,
        q: PointId,
        t_id: TriangleId,
        context: &mut Context,
    ) -> bool {
        let triangle = context.triangles.get_mut_unchecked(t_id);
        let Some(index) = triangle.edge_index(p, q) else { return false; };

        triangle.set_constrained(index, true);
        let neighbor_t_id = triangle.neighbors[index];
        if !neighbor_t_id.invalid() {
            let ot = context.triangles.get_mut_unchecked(neighbor_t_id);
            let index = ot.neighbor_index(t_id);
            ot.set_constrained(index, true);
        }

        true
    }

    fn fill_edge_event(
        edge: &ConstrainedEdge,
        node_id: NodeId,
        context: &mut Context,
        observer: &mut impl Observer,
    ) {
        if edge.right {
            Self::fill_right_above_edge_event(edge, node_id, context, observer);
        } else {
            Self::fill_left_above_edge_event(edge, node_id, context, observer);
        }
    }

    fn fill_right_above_edge_event(
        edge: &ConstrainedEdge,
        node_id: NodeId,
        context: &mut Context,
        observer: &mut impl Observer,
    ) {
        let mut node_id = node_id.clone();
        while let Some(next_node) = context.advancing_front.locate_next_node(node_id) {
            if next_node.point().x >= edge.p.x {
                break;
            }

            // check if next node is below the edge
            if orient_2d(edge.q, next_node.point(), edge.p).is_ccw() {
                Self::fill_right_below_edge_event(edge, node_id, context, observer);
            } else {
                // try next node
                node_id = next_node.get_node_id();
            }
        }
    }

    fn fill_right_below_edge_event(
        edge: &ConstrainedEdge,
        node_id: NodeId,
        context: &mut Context,
        observer: &mut impl Observer,
    ) -> Option<()> {
        if node_id.point().x >= edge.p.x {
            return None;
        }

        let node = context.advancing_front.get_node_with_id(node_id).unwrap();

        let next_node = node.next().unwrap();
        let next_next_node = next_node.next().unwrap();

        if orient_2d(node.point(), next_node.point(), next_next_node.point()).is_ccw() {
            // concave
            Self::fill_right_concave_edge_event(edge, node_id, context, observer);
        } else {
            // convex
            Self::fill_right_convex_edge_event(edge, node_id, context, observer)?;

            // retry this one
            Self::fill_right_below_edge_event(edge, node_id, context, observer);
        }

        Some(())
    }

    /// recursively fill concave nodes
    fn fill_right_concave_edge_event(
        edge: &ConstrainedEdge,
        node_id: NodeId,
        context: &mut Context,
        observer: &mut impl Observer,
    ) {
        let next_id = {
            let next_node = context.advancing_front.locate_next_node(node_id).unwrap();
            let next_id = next_node.get_node_id();
            match Self::fill_one(next_id, context, observer) {
                None => {
                    // nothing changed
                    next_id
                }
                Some(fill_one) => fill_one.next,
            }
        };

        if next_id.point_id() != edge.p_id() {
            // next above or below edge?
            if orient_2d(edge.q, next_id.point(), edge.p).is_ccw() {
                let next_next_node = context.advancing_front.locate_next_node(next_id).unwrap();

                //  below
                if orient_2d(node_id.point(), next_id.point(), next_next_node.point()).is_ccw() {
                    // next is concave
                    Self::fill_right_concave_edge_event(edge, node_id, context, observer);
                } else {
                    // next is convex
                }
            }
        }
    }

    // if nothing changed, returns None
    #[must_use]
    fn fill_right_convex_edge_event(
        edge: &ConstrainedEdge,
        node_id: NodeId,
        context: &mut Context,
        observer: &mut impl Observer,
    ) -> Option<()> {
        let next_node = context.advancing_front.locate_next_node(node_id).unwrap();
        let next_next_node = next_node.next().unwrap();
        let next_next_next_node = next_next_node.next()?;
        // next concave or convex?
        if orient_2d(
            next_node.point(),
            next_next_node.point(),
            next_next_next_node.point(),
        )
        .is_ccw()
        {
            // concave
            Self::fill_right_concave_edge_event(edge, node_id, context, observer);
        } else {
            // convex
            // next above or below edge?
            if orient_2d(edge.q, next_next_node.point(), edge.p).is_ccw() {
                // Below
                Self::fill_right_convex_edge_event(
                    edge,
                    next_node.get_node_id(),
                    context,
                    observer,
                )?;
            } else {
                // Above
            }
        }

        Some(())
    }

    fn fill_left_above_edge_event(
        edge: &ConstrainedEdge,
        node_id: NodeId,
        context: &mut Context,
        observer: &mut impl Observer,
    ) {
        let mut node_id = node_id.clone();
        while let Some(prev_node) = context.advancing_front.locate_prev_node(node_id) {
            // check if next node is below the edge
            if prev_node.point().x <= edge.p.x {
                break;
            }

            if orient_2d(edge.q, prev_node.point(), edge.p).is_cw() {
                Self::fill_left_below_edge_event(edge, node_id, context, observer);
            } else {
                node_id = prev_node.get_node_id();
            }
        }
    }

    fn fill_left_below_edge_event(
        edge: &ConstrainedEdge,
        node_id: NodeId,
        context: &mut Context,
        observer: &mut impl Observer,
    ) -> Option<()> {
        if node_id.point().x > edge.p.x {
            let prev_node = context.advancing_front.locate_prev_node(node_id).unwrap();
            let prev_prev_node = prev_node.prev().unwrap();
            if orient_2d(node_id.point(), prev_node.point(), prev_prev_node.point()).is_cw() {
                Self::fill_left_concave_edge_event(edge, node_id, context, observer);
            } else {
                // convex
                Self::fill_left_convex_edge_event(edge, node_id, context, observer)?;

                // retry this one
                Self::fill_left_below_edge_event(edge, node_id, context, observer);
            }
            Some(())
        } else {
            None
        }
    }

    // if nothing changed, returns None
    #[must_use]
    fn fill_left_convex_edge_event(
        edge: &ConstrainedEdge,
        node_id: NodeId,
        context: &mut Context,
        observer: &mut impl Observer,
    ) -> Option<()> {
        // next concave or convex?
        let prev_node = context.advancing_front.locate_prev_node(node_id).unwrap();
        let prev_prev_node = prev_node.prev().unwrap();
        let prev_prev_prev_node = prev_prev_node.prev()?;

        if orient_2d(
            prev_node.point(),
            prev_prev_node.point(),
            prev_prev_prev_node.point(),
        )
        .is_cw()
        {
            // concave
            Self::fill_left_concave_edge_event(edge, prev_node.get_node_id(), context, observer);
        } else {
            // convex
            // next above or below edge?
            if orient_2d(edge.q, prev_prev_node.point(), edge.p).is_cw() {
                // below
                Self::fill_left_convex_edge_event(
                    edge,
                    prev_node.get_node_id(),
                    context,
                    observer,
                )?;
            } else {
                // above
            }
        }
        Some(())
    }

    fn fill_left_concave_edge_event(
        edge: &ConstrainedEdge,
        node_id: NodeId,
        context: &mut Context,
        observer: &mut impl Observer,
    ) {
        let prev_node = context.advancing_front.locate_prev_node(node_id).unwrap();

        let prev_node_id = prev_node.get_node_id();

        let prev_node_id = match Self::fill_one(prev_node_id, context, observer) {
            Some(fill_one) => fill_one.prev,
            None => prev_node_id,
        };

        if prev_node_id.point_id() != edge.p_id() {
            // next above or below edge?
            if orient_2d(edge.q, prev_node_id.point(), edge.p).is_cw() {
                let prev_node = context
                    .advancing_front
                    .get_node_with_id(prev_node_id)
                    .unwrap();
                // below
                let prev_prev_node = prev_node.prev().unwrap();
                if orient_2d(node_id.point(), prev_node.point(), prev_prev_node.point()).is_cw() {
                    // next is concave
                    Self::fill_left_concave_edge_event(edge, node_id, context, observer);
                } else {
                    // next is convex
                }
            }
        }
    }

    fn edge_event_process(
        ep: PointId,
        eq: PointId,
        constrain_edge: &ConstrainedEdge,
        triangle_id: TriangleId,
        p: PointId,
        triangle_ids: &mut Vec<TriangleId>,
        context: &mut Context,
    ) {
        assert!(!triangle_id.invalid());

        if Self::try_mark_edge_for_triangle(ep, eq, triangle_id, context) {
            return;
        }

        let triangle = context.triangles.get_mut_unchecked(triangle_id);
        let p1 = triangle.point_ccw(p);
        let o1 = orient_2d(
            eq.get(&context.points),
            p1.get(&context.points),
            ep.get(&context.points),
        );

        if o1.is_collinear() {
            if let Some(edge_index) = triangle.edge_index(eq, p1) {
                triangle.set_constrained(edge_index, true);

                let neighbor_across_t = triangle.neighbor_across(p);
                Self::edge_event_process(
                    ep,
                    p1,
                    &constrain_edge.with_q(p1, context),
                    neighbor_across_t,
                    p1,
                    triangle_ids,
                    context,
                );
                return;
            } else {
                panic!("EdgeEvent - collinear points not supported")
            }
        }

        let p2 = triangle.point_cw(p);
        let o2 = orient_2d(
            eq.get(&context.points),
            p2.get(&context.points),
            ep.get(&context.points),
        );
        if o2.is_collinear() {
            if let Some(edge_index) = triangle.edge_index(eq, p2) {
                triangle.set_constrained(edge_index, true);

                let neighbor_across_t = triangle.neighbor_across(p);
                Self::edge_event_process(
                    ep,
                    p2,
                    &constrain_edge.with_q(p2, context),
                    neighbor_across_t,
                    p2,
                    triangle_ids,
                    context,
                );

                return;
            } else {
                panic!("collinear points not supported");
            }
        }

        if o1 == o2 {
            // need to decide if we are rotating cw or ccw to get to a triangle
            // that will cross edge
            let triangle_id = if o1.is_cw() {
                triangle.neighbor_ccw(p)
            } else {
                triangle.neighbor_cw(p)
            };

            Self::edge_event_process(
                ep,
                eq,
                constrain_edge,
                triangle_id,
                p,
                triangle_ids,
                context,
            );
        } else {
            Self::flip_edge_event(
                ep,
                eq,
                constrain_edge,
                triangle_id,
                p,
                triangle_ids,
                context,
            );
        }
    }
}

/// flip edge related methods
impl Sweeper {
    fn flip_edge_event(
        ep: PointId,
        eq: PointId,
        edge: &ConstrainedEdge,
        triangle_id: TriangleId,
        p: PointId,
        legalize_queue: &mut Vec<TriangleId>,
        context: &mut Context,
    ) {
        let t = triangle_id.get(&context.triangles);
        let ot_id = t.neighbor_across(p);
        let ot = ot_id.get(&context.triangles);
        let op = ot.opposite_point(t, p);

        if in_scan_area(
            p.get(&context.points),
            t.point_ccw(p).get(&context.points),
            t.point_cw(p).get(&context.points),
            op.get(&context.points),
        ) {
            // lets rotate shared edge one vertex cw
            if Self::rotate_triangle_pair(triangle_id, p, ot_id, op, &mut context.triangles) {
                Self::map_triangle_to_nodes(triangle_id, context);
                Self::map_triangle_to_nodes(ot_id, context);
            }
            // legalize later
            legalize_queue.extend([triangle_id, ot_id]);

            if p == eq && op == ep {
                if eq == edge.q_id() && ep == edge.p_id() {
                    context
                        .triangles
                        .get_mut_unchecked(triangle_id)
                        .set_constrained_for_edge(ep, eq);

                    context
                        .triangles
                        .get_mut_unchecked(ot_id)
                        .set_constrained_for_edge(ep, eq);
                }
            } else {
                let o = orient_2d(
                    eq.get(&context.points),
                    op.get(&context.points),
                    ep.get(&context.points),
                );

                let t = Self::next_flip_triangle(o, triangle_id, ot_id, legalize_queue);
                Self::flip_edge_event(ep, eq, edge, t, p, legalize_queue, context);
            }
        } else {
            let new_p = Self::next_flip_point(ep, eq, ot_id, op, context);
            Self::flip_scan_edge_event(
                ep,
                eq,
                edge,
                triangle_id,
                ot_id,
                new_p,
                legalize_queue,
                context,
            );
            Self::edge_event_process(ep, eq, edge, triangle_id, p, legalize_queue, context);
        }
    }

    fn next_flip_triangle(
        o: Orientation,
        t: TriangleId,
        ot: TriangleId,
        triangle_ids: &mut Vec<TriangleId>,
    ) -> TriangleId {
        if o.is_ccw() {
            // ot is not crossing edge after flip
            triangle_ids.push(ot);
            t
        } else {
            // t is not crossing edge after flip
            triangle_ids.push(t);
            ot
        }
    }

    fn next_flip_point(
        ep: PointId,
        eq: PointId,
        ot: TriangleId,
        op: PointId,
        context: &mut Context,
    ) -> PointId {
        let o2d = orient_2d(
            eq.get(&context.points),
            op.get(&context.points),
            ep.get(&context.points),
        );

        let ot = context.triangles.get_unchecked(ot);
        match o2d {
            Orientation::CW => {
                // right
                ot.point_ccw(op)
            }
            Orientation::CCW => {
                // left
                ot.point_cw(op)
            }
            Orientation::Collinear => {
                panic!("Opposing point on constrained edge");
            }
        }
    }

    fn flip_scan_edge_event(
        ep: PointId,
        eq: PointId,
        edge: &ConstrainedEdge,
        flip_triangle_id: TriangleId,
        t_id: TriangleId,
        p: PointId,
        triangle_ids: &mut Vec<TriangleId>,
        context: &mut Context,
    ) {
        let t = t_id.get(&context.triangles);
        let ot = t.neighbor_across(p);
        if ot.invalid() {
            panic!("flip_scan_edge_event - null neighbor across");
        }

        let op = ot.get(&context.triangles).opposite_point(t, p);
        let flip_triangle = flip_triangle_id.get(&context.triangles);
        let p1 = flip_triangle.point_ccw(eq);
        let p2 = flip_triangle.point_cw(eq);

        if in_scan_area(
            eq.get(&context.points),
            p1.get(&context.points),
            p2.get(&context.points),
            op.get(&context.points),
        ) {
            // flip with new edge op -> eq
            Self::flip_edge_event(eq, op, edge, ot, op, triangle_ids, context);

            // original comment:
            // TODO: Actually I just figured out that it should be possible to
            //       improve this by getting the next ot and op before the the above
            //       flip and continue the flipScanEdgeEvent here
            // set new ot and op here and loop back to inScanArea test
            // also need to set a new flip_triangle first
            // Turns out at first glance that this is somewhat complicated
            // so it will have to wait.
        } else {
            let new_p = Self::next_flip_point(ep, eq, ot, op, context);
            Self::flip_scan_edge_event(
                ep,
                eq,
                edge,
                flip_triangle_id,
                ot,
                new_p,
                triangle_ids,
                context,
            );
        }
    }
}

#[derive(Debug)]
struct Basin {
    left: Point,
    right: Point,
    width: f64,
    left_higher: bool,
}

impl Basin {
    pub fn is_shallow(&self, point: Point) -> bool {
        let height = if self.left_higher {
            self.left.y - point.y
        } else {
            self.right.y - point.y
        };

        self.width > height
    }

    pub fn completed(&self, point: Point) -> bool {
        if point.x >= self.right.x || point.x <= self.left.x {
            return true;
        }

        self.is_shallow(point)
    }
}

/// Basin related methods
impl Sweeper {
    fn basin_angle_satisfy(node_id: NodeId, context: &Context) -> bool {
        const TAN_3_4_PI: f64 = -1.;
        let Some(next) = context.advancing_front.locate_next_node(node_id) else { return false };
        let Some(next_next) = next.next() else { return false };

        let ax = node_id.point().x - next_next.point().x;
        let ay = node_id.point().y - next_next.point().y;
        // the basin angle is (1/2pi, pi), so as long as tan value is less than 3/4 pi's, then its angle is less than 3/4 pi

        // ay / ax < tan(3/4 * PI)
        if ax > 0. {
            ay < TAN_3_4_PI * ax
        } else {
            ay > TAN_3_4_PI * ax
        }
    }

    /// basin is like a bowl, we first identify it's left, bottom, right node.
    /// then fill it
    fn fill_basin(
        node_point: NodeId,
        context: &mut Context,
        observer: &mut impl Observer,
    ) -> Option<()> {
        let next_node = context.advancing_front.locate_next_node(node_point)?;
        let next_next_node = next_node.next()?;

        // find the left
        let left: NodeRef<'_>;
        if orient_2d(
            node_point.point(),
            next_node.point(),
            next_next_node.point(),
        )
        .is_ccw()
        {
            left = next_next_node;
        } else {
            left = next_node;
        }

        // find the bottom
        let mut bottom = left.clone();
        while let Some(next_node) = bottom.next() {
            if bottom.point().y >= next_node.point().y {
                bottom = next_node;
            } else {
                break;
            }
        }

        // no valid basin
        if bottom.point_id().eq(&left.point_id()) {
            return None;
        }

        // find the right
        let mut right: NodeRef = bottom.clone();
        while let Some(next_node) = right.next() {
            if right.point().y < next_node.point().y {
                right = next_node;
            } else {
                break;
            }
        }
        if right.point_id() == bottom.point_id() {
            // no valid basin
            return None;
        }

        let width = right.point().x - left.point().x;
        let left_higher: bool = left.point().y > right.point().y;

        Self::fill_basin_req(
            bottom.get_node_id(),
            &Basin {
                left: left.point(),
                right: right.point(),
                width,
                left_higher,
            },
            context,
            observer,
        );

        Some(())
    }

    fn fill_basin_req(
        node: NodeId,
        basin: &Basin,
        context: &mut Context,
        observer: &mut impl Observer,
    ) -> Option<()> {
        if basin.completed(node.point()) {
            return None;
        }

        let fill_one = Self::fill_one(node, context, observer).expect("already in basin");
        let prev = fill_one.prev;
        let next = fill_one.next;

        if prev.point().eq(&basin.left) && next.point().eq(&basin.right) {
            return Some(());
        }

        let new_node = if prev.point().eq(&basin.left) {
            let next = context.advancing_front.get_node_with_id(next).unwrap();
            let next_next = next.next().unwrap();
            if orient_2d(node.point(), next.point(), next_next.point()).is_cw() {
                return None;
            }

            next.get_node_id()
        } else if next.point().eq(&basin.right) {
            let prev = context.advancing_front.get_node_with_id(prev).unwrap();
            let prev_prev = prev.prev()?;
            if orient_2d(node.point(), prev.point(), prev_prev.point()).is_ccw() {
                return None;
            }

            prev.get_node_id()
        } else {
            // continue with the neighbor node with lowest Y value
            if prev.point().y < next.point().y {
                prev
            } else {
                next
            }
        };

        Self::fill_basin_req(new_node, basin, context, observer)
    }
}

impl Sweeper {
    pub fn verify_triangles(context: &Context) -> bool {
        Self::illegal_triangles(context).is_empty()
    }

    /// verify all triangles stored in context are legal
    #[allow(unused)]
    pub fn illegal_triangles(context: &Context) -> Vec<(TriangleId, TriangleId)> {
        let triangle_ids = context
            .triangles
            .iter()
            .map(|(t_id, _)| t_id)
            .collect::<Vec<_>>();

        let mut result = Vec::<(TriangleId, TriangleId)>::new();

        for t_id in triangle_ids {
            for illegal_neighbor in &Self::is_legalize(t_id, context) {
                if !illegal_neighbor.invalid() {
                    result.push((t_id, *illegal_neighbor));
                }
            }
        }

        result
    }
}

fn parse_polyline(polyline: Vec<Point>, points: &mut PointsBuilder) {
    // here we need to set points' edges
    let mut point_iter = polyline
        .iter()
        .map(|p| (points.add_steiner_point(*p), p))
        .collect::<Vec<_>>()
        .into_iter();

    if let Some(first_point) = point_iter.next() {
        let mut last_point = first_point;
        loop {
            match point_iter.next() {
                Some(p2) => {
                    let edge = Edge::new(last_point, p2);
                    points.get_point_mut(edge.q).unwrap().edges.push(edge.p);
                    last_point = p2;
                }
                None => {
                    let edge = Edge::new(last_point, first_point);
                    points.get_point_mut(edge.q).unwrap().edges.push(edge.p);
                    break;
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::io::{Read, Write};

    use rand::Rng;

    use super::*;

    #[derive(Default)]
    struct CacheHitOb {
        hit_count: u64,
        mis_count: u64,
        rotate_count: u64,
        legalize_step_count: u64,
        legalize_count: u64,
    }

    impl CacheHitOb {
        pub fn hit_rate(&self) -> f64 {
            self.hit_count as f64 / (self.hit_count + self.mis_count) as f64
        }
    }

    impl Observer for CacheHitOb {
        fn legalized(&mut self, _triangel_id: TriangleId, _context: &Context) {
            self.legalize_count += 1;
        }

        fn legalize_step(&mut self, _triangle_id: TriangleId, _context: &Context) {
            self.legalize_step_count += 1;
        }

        fn triangle_rotated(
            &mut self,
            _triangle_id: TriangleId,
            _opposite_triangle_id: TriangleId,
            _context: &Context,
        ) {
            self.rotate_count += 1;
        }

        fn finalized(&mut self, context: &Context) {
            let hit = context
                .advancing_front
                .hit_count
                .load(std::sync::atomic::Ordering::Relaxed);
            let miss = context
                .advancing_front
                .miss_count
                .load(std::sync::atomic::Ordering::Relaxed);
            println!(
                "af cache hit: {}/{} rate: {:.2}%",
                hit,
                hit + miss,
                hit as f64 / (hit + miss) as f64 * 100.
            );

            println!(
                "points: {} triangle: {}",
                context.points.len(),
                context.triangles.len()
            );
            println!(
                "legalize: {} steps: {} rotate: {}",
                self.legalize_count, self.legalize_step_count, self.rotate_count
            );
            self.hit_count = hit;
            self.mis_count = miss;
        }
    }

    #[test]
    fn test_bird() {
        let file_path = "test_data/bird.dat";
        let points = try_load_from_file(file_path).unwrap();

        let mut cache_hit = CacheHitOb::default();
        let sweeper = SweeperBuilder::new(points).build();
        let triangles = sweeper
            .triangulate_with_observer(&mut cache_hit)
            .collect::<Vec<_>>();
        assert_eq!(triangles.len(), 273);
        assert!(cache_hit.hit_rate() > 0.63);
        assert!(cache_hit.rotate_count == 272);
    }

    #[test]
    fn test_nazca_heron() {
        let file_path = "test_data/nazca_heron.dat";
        let points = try_load_from_file(file_path).unwrap();

        let sweeper = SweeperBuilder::new(points).build();
        let mut cache_hit = CacheHitOb::default();
        let triangles = sweeper
            .triangulate_with_observer(&mut cache_hit)
            .collect::<Vec<_>>();
        assert_eq!(triangles.len(), 1034);
        assert!(cache_hit.hit_rate() > 0.71);
        assert!(cache_hit.rotate_count == 671);
    }

    #[test]
    fn test_rand() {
        let test_path = "test_data/latest_test_data";
        let points = match try_load_from_file(test_path) {
            None => {
                let mut points = Vec::<Point>::new();
                for _ in 0..100 {
                    let x: f64 = rand::thread_rng().gen_range(0.0..800.);
                    let y: f64 = rand::thread_rng().gen_range(0.0..800.);
                    points.push(Point::new(x, y));
                }
                save_to_file(&points, test_path);
                points
            }
            Some(points) => points,
        };

        let sweeper = SweeperBuilder::new(vec![
            Point::new(-10., -10.),
            Point::new(810., -10.),
            Point::new(810., 810.),
            Point::new(-10., 810.),
        ])
        .add_steiner_points(points)
        .add_hole(vec![
            Point::new(400., 400.),
            Point::new(600., 400.),
            Point::new(600., 600.),
            Point::new(400., 600.),
        ])
        .build();
        let _ = sweeper.triangulate();

        delete_file(test_path);
    }

    fn try_load_from_file(path: &str) -> Option<Vec<Point>> {
        let mut f = std::fs::File::options().read(true).open(path).ok()?;
        let mut value = "".to_string();
        f.read_to_string(&mut value).unwrap();
        let mut points = vec![];
        for line in value.lines() {
            let mut iter = line.split_whitespace();
            let x = iter.next().unwrap();
            let y = iter.next().unwrap();

            let x = x.parse::<f64>().unwrap();
            let y = y.parse::<f64>().unwrap();
            points.push(Point::new(x, y));
        }

        Some(points)
    }

    fn save_to_file(points: &[Point], path: &str) {
        use std::fmt::Write;

        let mut f = std::fs::File::options()
            .write(true)
            .create_new(true)
            .open(path)
            .unwrap();

        let mut value = "".to_string();
        for p in points {
            writeln!(value, "{} {}", p.x, p.y).unwrap();
        }

        f.write_all(value.as_bytes()).unwrap();
    }

    fn delete_file(path: &str) {
        std::fs::remove_file(path).unwrap();
    }
}