planar_geo 0.4.0

A Rust library for 2D geometry: geometric objects, algorithms and visualization
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
/*!
Defines the [`Polysegment`] type, a foundational [`Composite`] used throughout
this crate.

Segment polysegments serve as the basis for higher-level geometric types such as
[`Contour`](crate::contour::Contour) and [`Shape`](crate::shape::Shape). They
represent connected sequences of segments and provide the core functionality
required by composite geometries.

Most users should interact with this module through the [`Polysegment`] type
itself; see its documentation for details on construction, invariants, and
usage.
*/

use std::collections::VecDeque;

use crate::primitive::Primitive;
use crate::segment::{ArcSegment, LineSegment, Segment, SegmentRef};
use crate::{DEFAULT_EPSILON, DEFAULT_MAX_ULPS};
use crate::{Transformation, composite::*};
use approx::ulps_eq;
use bounding_box::{BoundingBox, ToBoundingBox};
use rayon::prelude::*;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/**
A sequence of [`Segment`]s where each segment is "connected" to its successor
(end / stop point of a segment is approximately equal to the start point of the
successor).

*/
#[doc = ""]
#[cfg_attr(
    feature = "doc-images",
    doc = "![Polysegment example][example_polysegment]"
)]
#[cfg_attr(
    feature = "doc-images",
    embed_doc_image::embed_doc_image("example_polysegment", "docs/img/example_polysegment.svg")
)]
#[cfg_attr(
    not(feature = "doc-images"),
    doc = "**Doc images not enabled**. Compile docs with `cargo doc --features 'doc-images'` and Rust version >= 1.54."
)]
/**

The approximate equality of start and end point is defined by
[`DEFAULT_EPSILON`] and [`DEFAULT_MAX_ULPS`]:

```ignore
approx::ulps_eq!(polysegment[i].stop(), polysegment[i+1].start(), epsilon = DEFAULT_EPSILON, max_ulps = DEFAULT_MAX_ULPS)
```

A polysegment can intersect itself (i.e. at least two of its segments
intersect each other). This can be tested using its [`Composite`] trait
implementation:

```
use planar_geo::prelude::*;

let polysegment = Polysegment::from_points(&[[0.0, 0.0], [2.0, 2.0], [0.0, 2.0], [2.0, 0.0]]);
assert_eq!(polysegment.intersections_polysegment(&polysegment, 0.0, 0).count(), 1);
```

# Constructing a polysegment

A polysegment is essentially a [newtype](https://doc.rust-lang.org/rust-by-example/generics/new_types.html)
wrapper around a [`VecDeque<Segment>`] and therefore exposes many of the same
methods. For example, a polysegment can be built via
[`push_front`](Polysegment::push_front) and
[`push_back`](Polysegment::push_back) just like a [`VecDeque`]. The
[`extend_front`](Polysegment::extend_front) and
[`extend_back`](Polysegment::extend_back) methods can be used to implicitly
add [`LineSegment`]s.

There are multiple constructors available:
- [`new`](Polysegment::new): A new, empty polysegment with no segment.
- [`with_capacity`](Polysegment::with_capacity): A new, empty polysegment
with a defined space for segments preallocated with no segment.
- [`from_points`](Polysegment::from_points): A polysegment consisting of
[`LineSegment`]s which connect the given points.
- [`from_iter`](Polysegment::from_iter): A polysegment consisting of the
segments given by an iterator. In case two subsequent segments are not
connected, a "filler" [`LineSegment`] is introduced between them.

# Modifying a polysegment

To uphold the "connection" property, it is not possible to manipulate arbitrary
segments, which is why methods like [`VecDeque::get_mut`] are missing.
It is however possible to manipulate the whole polysegment via the
[`Transformation`] implementation. Additionally, individual segments can be
removed from the ends of the polysegment with [`pop_front`](Polysegment::pop_front)
and [`pop_back`](Polysegment::pop_back).

# Access of individual segments

Accessing individual segments is possible via indexing (which panics when
out-of-bounds) and the [`get`](Polysegment::get) method (which returns `None`
when out-of-bounds). As in the underlying deque, the segments are not
necessarily contiguous in memory and can generally only be accessed via two
slices with the [`as_slices`](Polysegment::as_slices) method. [`Polysegment`]
does however expose the [`make_contiguous`](Polysegment::make_contiguous) to
store the segments contiguous in memory.

# Serialization and deserialization

When the `serde` feature is enabled, a polysegment can be serialized and
deserialized using the [`serde`] crate. It uses the same serialized
representation as a [`VecDeque`].
 */
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Polysegment(VecDeque<Segment>);

impl Polysegment {
    /**
    Creates an empty [`Polysegment`].

    # Examples

    ```
    use planar_geo::prelude::Polysegment;

    let polysegment = Polysegment::new();
    ```
     */
    pub fn new() -> Self {
        return Self(VecDeque::new());
    }

    /**
    Creates an empty [`Polysegment`] with space for at least `capacity`
    [`Segment`].

    # Examples

    ```
    use planar_geo::prelude::Polysegment;

    let polysegment = Polysegment::with_capacity(10);
    ```
     */
    pub fn with_capacity(capacity: usize) -> Self {
        return Self(VecDeque::with_capacity(capacity));
    }

    /**
    Creates an [`Polysegment`] of [`LineSegment`]s from the given points.
    If two consecutive points are equal (within the tolerances defined by
    [`DEFAULT_EPSILON`] and [`DEFAULT_MAX_ULPS`]), they are treated as a single
    vertex.

    # Examples

    ```
    use planar_geo::prelude::Polysegment;

    // Three points -> Two line segments
    let polysegment = Polysegment::from_points(&[[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]);
    assert_eq!(polysegment.len(), 2);

    // Two consecutive points are equal
    let polysegment = Polysegment::from_points(&[[0.0, 0.0], [0.0, 0.0], [0.0, 1.0]]);
    assert_eq!(polysegment.len(), 1);
    ```
     */
    pub fn from_points(points: &[[f64; 2]]) -> Self {
        let mut segments: VecDeque<Segment> = VecDeque::with_capacity(points.len());
        for window in points.windows(2) {
            let start = window[0];
            let stop = window[1];
            match segments.back() {
                Some(last_segment) => {
                    if last_segment.stop() == stop {
                        continue;
                    }
                }
                None => (),
            }

            if let Ok(ls) = LineSegment::new(start, stop, DEFAULT_EPSILON, DEFAULT_MAX_ULPS) {
                segments.push_back(ls.into());
            }
        }
        return Self(segments);
    }

    /**
    "Closes" the [`Polysegment`] by connecting the start point of the first /
    "front" segment with the stop point of the last / "back" segment with a
    [`LineSegment`] (if the two points aren't already equal).

    # Examples

    ```
    use planar_geo::prelude::Polysegment;

    // Three points -> Two line segments
    let mut polysegment = Polysegment::from_points(&[[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]);
    assert_eq!(polysegment.len(), 2);

    // Close the polysegment
    polysegment.close();
    assert_eq!(polysegment.len(), 3);
    ```
     */
    pub fn close(&mut self) -> () {
        if let Some(start) = self.0.front() {
            if let Some(stop) = self.0.back() {
                if let Ok(ls) = LineSegment::new(
                    stop.stop(),
                    start.start(),
                    DEFAULT_EPSILON,
                    DEFAULT_MAX_ULPS,
                ) {
                    self.0.push_back(ls.into())
                }
            }
        }
    }

    /**
    Returns a reference to the underlying [`VecDeque`].

    This allows using all methods of [`VecDeque`] which use a shared reference,
    e.g. its iterators, accessor methods etc.
     */
    pub fn vec_deque(&self) -> &VecDeque<Segment> {
        return &self.0;
    }

    /**
    Calls the [`VecDeque::as_slices`] method of the underlying deque. See its
    docstring for more.
     */
    pub fn as_slices(&self) -> (&[Segment], &[Segment]) {
        return self.0.as_slices();
    }

    /**
    Calls the [`VecDeque::make_contiguous`] method of the underlying deque. See
    its docstring for more.
     */
    pub fn make_contiguous(&mut self) -> &[Segment] {
        return self.0.make_contiguous();
    }

    /**
    Appends a [`Segment`] to the back of the [`Polysegment`].

    If the polysegment already has a "back" segment ([`Polysegment::back`] returns
    [`Some`]), the start point of `segment` is compared to the stop point of
    the back segment. If they aren't equal within the tolerances defined by
    [`DEFAULT_EPSILON`] and [`DEFAULT_MAX_ULPS`], a filler line segment is
    inserted between the two.

    # Examples

    ```
    use planar_geo::prelude::*;

    let mut polysegment = Polysegment::new();
    assert_eq!(polysegment.len(), 0);

    polysegment.push_back(LineSegment::new([0.0, 0.0], [1.0, 0.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap().into());
    assert_eq!(polysegment.len(), 1);

    // The start point of this segment matches the stop point of the already
    // inserted segment
    polysegment.push_back(LineSegment::new([1.0, 0.0], [1.0, 1.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap().into());
    assert_eq!(polysegment.len(), 2);

    // Start and stop point don't match -> A filler segment is introduced
    polysegment.push_back(LineSegment::new([2.0, 1.0], [2.0, 0.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap().into());
    assert_eq!(polysegment.len(), 4);
    ```
     */
    pub fn push_back(&mut self, segment: Segment) {
        let opt_stop = self.back().map(Segment::stop);
        if let Some(stop) = opt_stop {
            if let Ok(ls) =
                LineSegment::new(stop, segment.start(), DEFAULT_EPSILON, DEFAULT_MAX_ULPS)
            {
                self.0.push_back(ls.into());
            }
        }
        self.0.push_back(segment);
    }

    /**
    Prepends a [`Segment`] to the front of the [`Polysegment`].

    If the polysegment already has a "front" segment ([`Polysegment::front`] returns
    [`Some`]), the stop point of `segment` is compared to the start point of
    the front segment. If they aren't equal within the tolerances defined by
    [`DEFAULT_EPSILON`] and [`DEFAULT_MAX_ULPS`], a filler line segment is
    inserted between the two.

    # Examples

    ```
    use planar_geo::prelude::*;

    let mut polysegment = Polysegment::new();
    assert_eq!(polysegment.len(), 0);

    polysegment.push_front(LineSegment::new([0.0, 0.0], [1.0, 0.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap().into());
    assert_eq!(polysegment.len(), 1);

    // The stop point of this segment matches the start point of the already
    // inserted segment
    polysegment.push_front(LineSegment::new([0.0, 1.0], [0.0, 0.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap().into());
    assert_eq!(polysegment.len(), 2);

    // Start and stop point don't match -> A filler segment is introduced
    polysegment.push_front(LineSegment::new([2.0, 1.0], [2.0, 0.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap().into());
    assert_eq!(polysegment.len(), 4);
    ```
     */
    pub fn push_front(&mut self, segment: Segment) {
        let opt_start = self.front().map(Segment::start);
        if let Some(start) = opt_start {
            if let Ok(ls) =
                LineSegment::new(segment.stop(), start, DEFAULT_EPSILON, DEFAULT_MAX_ULPS)
            {
                self.0.push_front(ls.into());
            }
        }
        self.0.push_front(segment);
    }

    /**
    Removes the last / back element from the polysegment and returns it, or [`None`]
    if it is empty.

    # Examples

    ```
    use planar_geo::prelude::*;

    let mut polysegment = Polysegment::new();

    let ls: Segment = LineSegment::new([0.0, 0.0], [1.0, 0.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap().into();

    polysegment.push_back(ls.clone());
    assert_eq!(polysegment.len(), 1);

    assert_eq!(polysegment.pop_back(), Some(ls));
    assert_eq!(polysegment.pop_back(), None);
     */
    pub fn pop_back(&mut self) -> Option<Segment> {
        return self.0.pop_back();
    }

    /**
    Removes the first / front element from the polysegment and returns it, or [`None`]
    if it is empty.

    # Examples

    ```
    use planar_geo::prelude::*;

    let mut polysegment = Polysegment::new();

    let ls: Segment = LineSegment::new([0.0, 0.0], [1.0, 0.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap().into();

    polysegment.push_front(ls.clone());
    assert_eq!(polysegment.len(), 1);

    assert_eq!(polysegment.pop_front(), Some(ls));
    assert_eq!(polysegment.pop_front(), None);
     */
    pub fn pop_front(&mut self) -> Option<Segment> {
        return self.0.pop_front();
    }

    /**
    Provides a reference to the back element, or [`None`] if the polysegment is empty.

    # Examples

    ```
    use planar_geo::prelude::*;

    let polysegment = Polysegment::from_points(&[[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]);
    let ls: Segment = LineSegment::new([1.0, 0.0], [0.0, 1.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap().into();

    assert_eq!(polysegment.back(), Some(&ls));
    ```
     */
    pub fn back(&self) -> Option<&Segment> {
        return self.0.back();
    }

    /**
    Provides a reference to the front element, or [`None`] if the polysegment is empty.

    # Examples

    ```
    use planar_geo::prelude::*;

    let polysegment = Polysegment::from_points(&[[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]);
    let ls: Segment = LineSegment::new([0.0, 0.0], [1.0, 0.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap().into();

    assert_eq!(polysegment.front(), Some(&ls));
     */
    pub fn front(&self) -> Option<&Segment> {
        return self.0.front();
    }

    /**
    Adds a [`LineSegment`] to the front of `self` which stops at `point` and
    starts at the current stop point of `self` - i.e., the `stop` point of the
    [`Segment`] returned from [`Polysegment::back`]. If `self` is empty or
    `point` is equal to `stop`, this is a no-op.

    # Examples

    ```
    use planar_geo::prelude::*;

    let mut polysegment = Polysegment::new();
    assert_eq!(polysegment.len(), 0);

    // No-op, since the polysegment is empty
    polysegment.extend_back([0.0, 0.0]);
    assert_eq!(polysegment.len(), 0);

    // Now add a line
    polysegment.push_back(LineSegment::new([1.0, 0.0], [1.0, 1.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap().into());
    assert_eq!(polysegment.len(), 1);

    // Now adding the point works
    polysegment.extend_back([0.0, 0.0]);
    assert_eq!(polysegment.len(), 2);
    ```
     */
    pub fn extend_back(&mut self, point: [f64; 2]) {
        let endpoint = self.back().map(|s| s.stop());
        if let Some(endpoint) = endpoint {
            if let Ok(ls) = LineSegment::new(endpoint, point, DEFAULT_EPSILON, DEFAULT_MAX_ULPS) {
                self.push_back(ls.into());
            }
        }
    }

    /**
    Adds a [`LineSegment`] to the front of `self` which starts at `point` and
    stops at the current start point of `self` - i.e., the `start` point of the
    [`Segment`] returned from [`Polysegment::front`]. If `self` is empty or
    `point` is equal to `start`, this is a no-op.

    # Examples

    ```
    use planar_geo::prelude::*;

    let mut polysegment = Polysegment::new();
    assert_eq!(polysegment.len(), 0);

    // No-op, since the polysegment is empty
    polysegment.extend_front([0.0, 0.0]);
    assert_eq!(polysegment.len(), 0);

    // Now add a line
    polysegment.push_front(LineSegment::new([1.0, 0.0], [1.0, 1.0], DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap().into());
    assert_eq!(polysegment.len(), 1);

    // Now adding the point works
    polysegment.extend_front([0.0, 0.0]);
    assert_eq!(polysegment.len(), 2);
    ```
     */
    pub fn extend_front(&mut self, point: [f64; 2]) {
        let endpoint = self.front().map(|s| s.start());
        if let Some(endpoint) = endpoint {
            if let Ok(ls) = LineSegment::new(point, endpoint, DEFAULT_EPSILON, DEFAULT_MAX_ULPS) {
                self.push_front(ls.into());
            }
        }
    }

    /**
    Returns whether the polysegment / the underlying [`VecDeque`] is empty or not.

    # Examples

    ```
     use planar_geo::prelude::*;

    let mut polysegment = Polysegment::new();
    assert!(polysegment.is_empty());

    polysegment.push_back(LineSegment::new([0.0, 0.0], [1.0, 0.0], 0.0, 0).unwrap().into());
    assert!(!polysegment.is_empty());
    ```
     */
    pub fn is_empty(&self) -> bool {
        return self.0.is_empty();
    }

    /**
    Provides a reference to the [`Segment`] at the given index.

    The segment at index 0 is the front of the polysegment.

    # Examples

    ```
     use planar_geo::prelude::*;

    let polysegment = Polysegment::from_points(&[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]]);
    assert!(polysegment.get(0).is_some());
    assert!(polysegment.get(3).is_none());
    ```
     */
    pub fn get(&self, index: usize) -> Option<&Segment> {
        return self.0.get(index);
    }

    /**
    Returns the number of [`Segment`]s in `self`.

    # Examples

    ```
    use planar_geo::prelude::*;

    let polysegment = Polysegment::from_points(&[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]]);
    assert_eq!(polysegment.len(), 2);
    ```
     */
    pub fn len(&self) -> usize {
        return self.0.len();
    }

    /**
    Returns a front-to-back iterator over all [`Segment`]s of `self`.

    # Examples

    ```
    use planar_geo::prelude::*;

    let polysegment = Polysegment::from_points(&[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]]);
    let mut iter = polysegment.segments();
    assert!(iter.next().is_some());
    assert!(iter.next().is_some());
    assert!(iter.next().is_none());
    ```
     */
    pub fn segments(&self) -> std::collections::vec_deque::Iter<'_, Segment> {
        return self.0.iter();
    }

    /**
    Returns a parallel front-to-back iterator over all [`Segment`]s of `self`.

    # Examples

    ```
    use rayon::prelude::*;
    use planar_geo::prelude::*;

    let polysegment = Polysegment::from_points(&[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]]);
    let vec: Vec<_> = polysegment.segments_par().collect();
    assert_eq!(vec.len(), 2);
    ```
     */
    pub fn segments_par(&self) -> rayon::collections::vec_deque::Iter<'_, Segment> {
        return self.0.par_iter();
    }

    /**
    Moves all [`Segment`]s of `other` into `self`, leaving `other` empty.

    If the first point of `other` is not equal to the last point of `self`, a
    filler line segment is introduced first

    # Panics

    Panics if the new number of elements in `self` overflows an [`usize`].

    # Examples

    ```
    use planar_geo::prelude::*;

    let mut polysegment1 = Polysegment::from_points(&[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]]);
    let mut polysegment2 = Polysegment::from_points(&[[1.0, 1.0], [0.0, 1.0]]);
    let mut polysegment3 = Polysegment::from_points(&[[0.0, 2.0], [0.0, 3.0]]);

    assert_eq!(polysegment1.len(), 2);
    assert_eq!(polysegment2.len(), 1);
    assert_eq!(polysegment3.len(), 1);

    polysegment1.append(&mut polysegment2);
    assert_eq!(polysegment1.len(), 3);

    polysegment1.append(&mut polysegment3);
    assert_eq!(polysegment1.len(), 5);
    ```
     */
    pub fn append(&mut self, other: &mut Polysegment) -> () {
        if let Some(s) = other.0.front() {
            let start = s.start();
            if let Some(s) = self.0.back() {
                let stop = s.stop();
                if let Ok(ls) = LineSegment::new(stop, start, DEFAULT_EPSILON, DEFAULT_MAX_ULPS) {
                    self.0.push_back(ls.into())
                }
            }
        }

        // Append the segments
        self.0.append(&mut other.0);
    }

    /**
    Returns an iterator over all points of the polysegment.

    # Examples

    ```
    use planar_geo::prelude::*;

    let polysegment = Polysegment::from_points(&[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]]);

    let mut iter = polysegment.points();
    assert_eq!(iter.next(), Some([0.0, 0.0]));
    assert_eq!(iter.next(), Some([1.0, 0.0]));
    assert_eq!(iter.next(), Some([1.0, 1.0]));
    assert_eq!(iter.next(), None);
    ```
     */
    pub fn points(&self) -> PointIterator<'_> {
        return PointIterator::new(self, false, Polygonizer::default());
    }

    /**
    Returns the points of a polygon polysegment which approximates `self`. The
    individual segments are "polygonized" via [`Segment::polygonize`] and
    an [`SegmentPolygonizer`](crate::segment::SegmentPolygonizer) specified
    within [`Polygonizer`]. See the docstring of the latter fore more.
     */
    pub fn polygonize(&self, polygonizer: Polygonizer) -> PointIterator<'_> {
        return PointIterator::new(self, false, polygonizer);
    }

    /**
    Returns the combined length of all segments of `self`.

    ```
    use planar_geo::prelude::*;
    use std::f64::consts::{PI, TAU, SQRT_2};

    // Square with a side length of 1
    let points = &[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]];
    let mut polysegment = Polysegment::from_points(points);
    polysegment.close();
    assert_eq!(polysegment.length(), 4.0);

    // Circle with a radius of 1
    let segment: Segment = ArcSegment::from_center_radius_start_offset_angle([0.0, 0.0], 1.0, 0.0, TAU, DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap().into();
    let polysegment = Polysegment::from(segment);
    assert_eq!(polysegment.length(), TAU);

    // Half-circle segment
    let segment: Segment = ArcSegment::from_center_radius_start_offset_angle([0.0, 0.0], 1.0, 0.0, PI, DEFAULT_EPSILON, DEFAULT_MAX_ULPS).unwrap().into();
    let mut polysegment = Polysegment::from(segment);
    polysegment.close();
    assert_eq!(polysegment.length(), PI + 2.0);

    // Triangle
    let points = &[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]];
    let mut polysegment = Polysegment::from_points(points);
    polysegment.close();
    assert_eq!(polysegment.length(), 2.0 + SQRT_2);
    ```
     */
    pub fn length(&self) -> f64 {
        return self.segments_par().map(|segment| segment.length()).sum();
    }

    /**
    Cuts `self` into multiple polysegments by intersecting it with `other` and returns
    those them.

    The specified tolerances `epsilon` and `max_ulps` are used to determine
    intersections, see documentation of
    [`PrimitiveIntersections`](crate::primitive::PrimitiveIntersections).

    # Examples

    ```
    use planar_geo::prelude::*;

    let points = &[[0.0, 0.0], [2.0, 0.0], [2.0, 2.0], [0.0, 2.0]];
    let line = Polysegment::from_points(points);
    let cut = Polysegment::from_points(&[[-1.0, 1.0], [3.0, 1.0]]);

    let separated_lines = line.intersection_cut(&cut, DEFAULT_EPSILON, DEFAULT_MAX_ULPS);

    // This cut results in two separate polysegments
    assert_eq!(separated_lines.len(), 2);
    ```
     */
    pub fn intersection_cut(
        &self,
        other: &Polysegment,
        epsilon: f64,
        max_ulps: u32,
    ) -> Vec<Polysegment> {
        // Inner helper function
        fn create_cut_segment(
            segment: &Segment,
            start: [f64; 2],
            stop: [f64; 2],
            center: [f64; 2],
            epsilon: f64,
            max_ulps: u32,
        ) -> Option<Segment> {
            // Check to avoid degenerated segments, when start equals stop
            if !ulps_eq!(start, stop, epsilon = epsilon, max_ulps = max_ulps) {
                match segment {
                    Segment::LineSegment(_) => {
                        if let Ok(ls) = LineSegment::new(start, stop, epsilon, max_ulps) {
                            return Some(ls.into());
                        } else {
                            return None;
                        }
                    }
                    Segment::ArcSegment(_) => {
                        let normalized_stop = [stop[0] - center[0], stop[1] - center[1]];
                        let normalized_start = [start[0] - center[0], start[1] - center[1]];
                        let stop_angle = normalized_stop[1].atan2(normalized_stop[0]);
                        let start_angle = normalized_start[1].atan2(normalized_start[0]);
                        let offset_angle = stop_angle - start_angle;

                        if let Ok(arc) = ArcSegment::from_start_center_angle(
                            start,
                            center,
                            offset_angle,
                            epsilon,
                            max_ulps,
                        ) {
                            return Some(arc.into());
                        } else {
                            return None;
                        }
                    }
                };
            } else {
                return None;
            }
        }

        let mut lines: Vec<Polysegment> = Vec::new();
        let mut segments_of_current_line: Vec<Segment> = Vec::new();

        // Temporary variables. The values themselves are dummy values to satisfy the
        // borrow checker.
        let mut center: [f64; 2] = [0.0, 0.0];

        // Storage for intersections
        let mut intersections: Vec<[f64; 2]> = Vec::new();

        for segment_self in self.0.iter() {
            // Preparations for the current segment
            match segment_self {
                Segment::LineSegment(_) => (),
                Segment::ArcSegment(arc) => {
                    center = arc.center();
                }
            }

            // Reset the intersections vector
            intersections.clear();

            for segment_other in other.0.iter() {
                let intersections_segment_other =
                    segment_self.intersections_primitive(segment_other, epsilon, max_ulps);
                intersections.extend(
                    intersections_segment_other
                        .into_iter()
                        .map::<[f64; 2], _>(From::from),
                );
            }

            if intersections.is_empty() {
                // No intersections could be found -> add segment_self to the list of segments
                // forming the current line
                segments_of_current_line.push(segment_self.clone())
            } else {
                // Sort the intersections depending on their distance from the start of
                // segment_self (smalles first)
                intersections.sort_unstable_by(|a, b| {
                    let start = segment_self.start();

                    // Calculate the distance of a from segment_self.start and compare it to the
                    // distance of b from segment_self. Since we are only
                    // interested in the order, it is not necessary to normalize the distance.
                    // Do the same for b.
                    let dist_a = (start[0] - a[0]).powi(2) + (start[1] - a[1]).powi(2);
                    let dist_b = (start[0] - b[0]).powi(2) + (start[1] - b[1]).powi(2);

                    // When non-comparable values are used (e.g. NaN), the user
                    // has provided nonsensical values for the segment points
                    // and the result of the intersection cut will be
                    // meaningless anyway -> We can return any ordering
                    dist_a
                        .partial_cmp(&dist_b)
                        .unwrap_or(std::cmp::Ordering::Equal)
                });

                for (idx, intersection) in intersections.iter().enumerate() {
                    if idx == 0 {
                        let start = segment_self.start();
                        let stop = *intersection;

                        if let Some(segment) = create_cut_segment(
                            &segment_self,
                            start,
                            stop,
                            center,
                            epsilon,
                            max_ulps,
                        ) {
                            if segments_of_current_line.is_empty() {
                                lines.push(Polysegment::from(segment));
                            } else {
                                let mut polysegment =
                                    Polysegment::with_capacity(segments_of_current_line.len());
                                for s in segments_of_current_line.into_iter() {
                                    polysegment.push_back(s);
                                }
                                polysegment.push_back(segment);
                                lines.push(polysegment);
                            }
                        } else {
                            if !segments_of_current_line.is_empty() {
                                let mut polysegment =
                                    Polysegment::with_capacity(segments_of_current_line.len());
                                for s in segments_of_current_line.into_iter() {
                                    polysegment.push_back(s);
                                }
                                lines.push(polysegment);
                            }
                        }

                        // Reinstatiate the vector for the next segment_self
                        segments_of_current_line = Vec::new();
                    } else {
                        // SAFETY: An out-of-bound index is catched by the if idx == 0 above
                        let start = unsafe { *intersections.get_unchecked(idx - 1) };
                        let stop = *intersection;

                        // Found some intersections => finish the current line and create a new one
                        if let Some(segment) = create_cut_segment(
                            &segment_self,
                            start,
                            stop,
                            center,
                            epsilon,
                            max_ulps,
                        ) {
                            lines.push(Polysegment::from(segment));
                        }
                    };
                }

                // Populate the "tail end" (the last cut)
                if let Some(start) = intersections.last() {
                    if let Some(segment) = create_cut_segment(
                        &segment_self,
                        start.clone(),
                        segment_self.stop(),
                        center,
                        epsilon,
                        max_ulps,
                    ) {
                        segments_of_current_line.push(segment);
                    }
                }
            };
        }

        // Store the last segments as a line into the lines vector
        let mut polysegment = Polysegment::with_capacity(segments_of_current_line.len());
        for s in segments_of_current_line.into_iter() {
            polysegment.push_back(s);
        }
        lines.push(polysegment);

        return lines;
    }

    /**
    Reverses all [`Segment`]s of the polysegment by switching their start and stop
    points (see [`Segment::reverse`]). To ensure the "connected" property holds
    true, the ordering of the segments itself is exchanged as well.

    This method calls [`make_contiguous`](Polysegment::make_contiguous) so all
    segments are in the first slice before reversing it.

    # Examples

    ```
    use planar_geo::prelude::*;

    let mut polysegment = Polysegment::from_points(&[[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]);
    assert_eq!(polysegment.front().unwrap().start(), [0.0, 0.0]);
    assert_eq!(polysegment.back().unwrap().stop(), [0.0, 1.0]);

    polysegment.reverse();
    assert_eq!(polysegment.front().unwrap().start(), [0.0, 1.0]);
    assert_eq!(polysegment.back().unwrap().stop(), [0.0, 0.0]);
    ```
     */
    pub fn reverse(&mut self) -> () {
        self.make_contiguous();

        // After making the deque contiguous, the second slice is empty
        let (slice, _) = self.0.as_mut_slices();
        for segment in slice.iter_mut() {
            segment.reverse();
        }
        slice.reverse();
    }

    /**
    Creates a rotated pattern from `self`. For each one of the specified
    `repetitions`, the individual segments of `self` are cloned, rotated around
    `center` and then pushed to the back of `self`. The rotation angle is
    `angle` times the index of the current repetition plus one.

    # Examples

    ```
    use planar_geo::prelude::*;
    use std::f64::consts::FRAC_PI_2;

    let mut polysegment = Polysegment::from_points(&[[0.0, 0.0], [1.0, 0.0]]);

    // 0 repetitions => The original polysegment is left unchanged
    polysegment.rotational_pattern([1.0, 1.0], FRAC_PI_2, 0);
    let points: Vec<[f64; 2]> = polysegment.points().collect();
    assert_eq!(points.len(), 2);
    approx::assert_abs_diff_eq!(points[0], [0.0, 0.0], epsilon = 1e-10);
    approx::assert_abs_diff_eq!(points[1], [1.0, 0.0], epsilon = 1e-10);

    // Two repetitions: The points are rotated by 90° and 180° respectively.
    // The polysegment has 6 points after the extension
    polysegment.rotational_pattern([1.0, 1.0], FRAC_PI_2, 2);
    let points: Vec<[f64; 2]> = polysegment.points().collect();
    assert_eq!(points.len(), 6);
    approx::assert_abs_diff_eq!(points[0], [0.0, 0.0], epsilon = 1e-10);
    approx::assert_abs_diff_eq!(points[1], [1.0, 0.0], epsilon = 1e-10);
    approx::assert_abs_diff_eq!(points[2], [2.0, 0.0], epsilon = 1e-10);
    approx::assert_abs_diff_eq!(points[3], [2.0, 1.0], epsilon = 1e-10);
    approx::assert_abs_diff_eq!(points[4], [2.0, 2.0], epsilon = 1e-10);
    approx::assert_abs_diff_eq!(points[5], [1.0, 2.0], epsilon = 1e-10);
    ```
    */
    pub fn rotational_pattern(&mut self, center: [f64; 2], angle: f64, repetitions: usize) -> () {
        let num_segments = self.num_segments();
        for rep in 0..repetitions {
            for seg_idx in 0..num_segments {
                // Index is always in bounds, because its maximum value is
                // the number of segments at the start of the outer loop
                // and no segments are removed from self.
                let mut segment = self[seg_idx].clone();
                segment.rotate(center, angle * (rep as f64 + 1.0));
                self.push_back(segment);
            }
        }
    }

    /**
    Creates a translated pattern from `self`. For each one of the specified
    `repetitions`, the individual segments of `self` are cloned, shifted and
    then pushed to the back of `self`. The shift vector is `shift` times the
    index of the current repetition plus one.

    # Examples

    ```
    use planar_geo::prelude::*;
    use std::f64::consts::FRAC_PI_2;

    let mut polysegment = Polysegment::from_points(&[[0.0, 0.0], [1.0, 0.0]]);

    // 0 repetitions => The original polysegment is left unchanged
    polysegment.translational_pattern([1.0, 1.0], 0);
    let points: Vec<[f64; 2]> = polysegment.points().collect();
    assert_eq!(points.len(), 2);
    approx::assert_abs_diff_eq!(points[0], [0.0, 0.0], epsilon = 1e-10);
    approx::assert_abs_diff_eq!(points[1], [1.0, 0.0], epsilon = 1e-10);

    // Two repetitions: A "stair" polysegment is created
    polysegment.translational_pattern([1.0, 1.0], 2);
    let points: Vec<[f64; 2]> = polysegment.points().collect();
    assert_eq!(points.len(), 6);
    approx::assert_abs_diff_eq!(points[0], [0.0, 0.0], epsilon = 1e-10);
    approx::assert_abs_diff_eq!(points[1], [1.0, 0.0], epsilon = 1e-10);
    approx::assert_abs_diff_eq!(points[2], [1.0, 1.0], epsilon = 1e-10);
    approx::assert_abs_diff_eq!(points[3], [2.0, 1.0], epsilon = 1e-10);
    approx::assert_abs_diff_eq!(points[4], [2.0, 2.0], epsilon = 1e-10);
    approx::assert_abs_diff_eq!(points[5], [3.0, 2.0], epsilon = 1e-10);
    ```
    */
    pub fn translational_pattern(&mut self, shift: [f64; 2], repetitions: usize) -> () {
        let num_segments = self.num_segments();
        for rep in 0..repetitions {
            let f = rep as f64 + 1.0; // f for "factor"
            for seg_idx in 0..num_segments {
                // Index is always in bounds, because its maximum value is
                // the number of segments at the start of the outer loop
                // and no segments are removed from self.
                let mut segment = self[seg_idx].clone();
                segment.translate([shift[0] * f, shift[1] * f]);
                self.push_back(segment);
            }
        }
    }
}

impl FromIterator<Segment> for Polysegment {
    fn from_iter<T: IntoIterator<Item = Segment>>(iter: T) -> Self {
        let mut polysegment = Polysegment::new();
        for segment in iter {
            polysegment.push_back(segment);
        }
        return polysegment;
    }
}

impl crate::composite::private::Sealed for Polysegment {}

impl Composite for Polysegment {
    fn segment(&self, key: SegmentKey) -> Option<&crate::segment::Segment> {
        return self.get(key.segment_idx);
    }

    fn num_segments(&self) -> usize {
        return self.len();
    }

    fn centroid(&self) -> [f64; 2] {
        return crate::CentroidData::from(self).into();
    }

    fn iter<'a>(&'a self) -> impl Iterator<Item = (SegmentKey, &'a crate::segment::Segment)> {
        return self
            .segments()
            .enumerate()
            .map(|(i, s)| (SegmentKey::from_segment_idx(i), s));
    }

    fn par_iter<'a>(
        &'a self,
    ) -> impl ParallelIterator<Item = (SegmentKey, &'a crate::segment::Segment)> {
        return self
            .segments_par()
            .enumerate()
            .map(|(i, s)| (SegmentKey::from_segment_idx(i), s));
    }

    fn intersections_polysegment<'a>(
        &'a self,
        other: &'a Self,
        epsilon: f64,
        max_ulps: u32,
    ) -> impl Iterator<Item = Intersection> + 'a {
        let same_polysegment_len = if std::ptr::eq(self, other) {
            Some(self.num_segments())
        } else {
            None
        };

        // Naive implementation: Loop over all segments of segments_self. Check each
        // segment of self for an intersection with all segments of
        // segments_other.
        return other
            .0
            .iter()
            .enumerate()
            .flat_map(move |(right_idx, right_seg)| {
                intersections_between_polysegment_and_segment_priv(
                    self.0.iter(),
                    right_idx,
                    right_seg,
                    same_polysegment_len,
                    epsilon,
                    max_ulps,
                )
            });
    }

    fn intersections_polysegment_par<'a>(
        &'a self,
        other: &'a Self,
        epsilon: f64,
        max_ulps: u32,
    ) -> impl ParallelIterator<Item = Intersection> + 'a {
        let same_polysegment_len = if std::ptr::eq(self, other) {
            Some(self.num_segments())
        } else {
            None
        };

        // Naive implementation: Loop over all segments of segments_self. Check each
        // segment of self for an intersection with all segments of
        // segments_other. The outer iteration is done in parallel,
        // therefore the intersections are out of order.
        return other
            .0
            .par_iter()
            .enumerate()
            .flat_map(move |(right_idx, right_seg)| {
                intersections_between_polysegment_and_segment_priv_par(
                    self.0.par_iter(),
                    right_idx,
                    right_seg,
                    same_polysegment_len,
                    epsilon,
                    max_ulps,
                )
            });
    }

    fn intersections_shape<'a>(
        &'a self,
        shape: &'a crate::prelude::Shape,
        epsilon: f64,
        max_ulps: u32,
    ) -> impl Iterator<Item = Intersection> {
        shape
            .intersections_polysegment(self, epsilon, max_ulps)
            .map(Intersection::switch)
    }

    fn intersections_shape_par<'a>(
        &'a self,
        shape: &'a crate::prelude::Shape,
        epsilon: f64,
        max_ulps: u32,
    ) -> impl ParallelIterator<Item = Intersection> {
        shape
            .intersections_polysegment_par(self, epsilon, max_ulps)
            .map(Intersection::switch)
    }

    fn intersections_composite<'a, T: Composite>(
        &'a self,
        other: &'a T,
        epsilon: f64,
        max_ulps: u32,
    ) -> impl Iterator<Item = Intersection> + 'a
    where
        Self: Sized,
    {
        return other
            .intersections_polysegment(self, epsilon, max_ulps)
            .map(Intersection::switch);
    }

    fn intersections_composite_par<'a, T: Composite>(
        &'a self,
        other: &'a T,
        epsilon: f64,
        max_ulps: u32,
    ) -> impl ParallelIterator<Item = Intersection> + 'a
    where
        Self: Sized,
    {
        return other
            .intersections_polysegment_par(self, epsilon, max_ulps)
            .map(Intersection::switch);
    }

    fn covers_point(&self, point: [f64; 2], epsilon: f64, max_ulps: u32) -> bool {
        return self
            .intersections_primitive(&point, epsilon, max_ulps)
            .next()
            .is_some();
    }

    fn covers_segment<'a, T: Into<SegmentRef<'a>>>(
        &self,
        segment: T,
        epsilon: f64,
        max_ulps: u32,
    ) -> bool {
        let segment: SegmentRef = segment.into();
        return covers_segment(self.0.iter(), segment, epsilon, max_ulps);
    }

    fn contains_point(&self, _: [f64; 2], _: f64, _: u32) -> bool {
        // A polysegment has no surface area and therefore cannot contain a point.
        return false;
    }

    fn contains_segment<'a, T: Into<SegmentRef<'a>>>(&self, _: T, _: f64, _: u32) -> bool {
        return false;
    }

    fn covers_composite<'a, T: Composite>(
        &'a self,
        other: &'a T,
        epsilon: f64,
        max_ulps: u32,
    ) -> bool {
        return other.covers_polysegment(self, epsilon, max_ulps);
    }

    fn contains_composite<'a, T: Composite>(
        &'a self,
        other: &'a T,
        epsilon: f64,
        max_ulps: u32,
    ) -> bool {
        return other.contains_polysegment(self, epsilon, max_ulps);
    }

    fn overlaps_segment<'a, T: Into<SegmentRef<'a>>>(
        &self,
        _segment: T,
        _epsilon: f64,
        _max_ulps: u32,
    ) -> bool {
        return false;
    }

    fn overlaps_contour(
        &self,
        _contour: &crate::prelude::Contour,
        _epsilon: f64,
        _max_ulps: u32,
    ) -> bool {
        return false;
    }

    fn overlaps_shape(
        &self,
        _shape: &crate::prelude::Shape,
        _epsilon: f64,
        _max_ulps: u32,
    ) -> bool {
        return false;
    }

    fn overlaps_composite<'a, T: Composite>(
        &'a self,
        _other: &'a T,
        _epsilon: f64,
        _max_ulps: u32,
    ) -> bool {
        return false;
    }
}

impl std::ops::Index<usize> for Polysegment {
    type Output = Segment;

    fn index(&self, index: usize) -> &Self::Output {
        &self.0[index]
    }
}

impl From<Segment> for Polysegment {
    fn from(value: Segment) -> Self {
        let mut polysegment = Polysegment::new();
        polysegment.push_back(value);
        return polysegment;
    }
}

impl From<LineSegment> for Polysegment {
    fn from(value: LineSegment) -> Self {
        return Polysegment::from(Segment::LineSegment(value));
    }
}

impl From<ArcSegment> for Polysegment {
    fn from(value: ArcSegment) -> Self {
        return Polysegment::from(Segment::ArcSegment(value));
    }
}

impl crate::Transformation for Polysegment {
    fn rotate(&mut self, center: [f64; 2], angle: f64) -> () {
        self.0.par_iter_mut().for_each(|segment| {
            segment.rotate(center, angle);
        })
    }

    fn translate(&mut self, shift: [f64; 2]) -> () {
        self.0.par_iter_mut().for_each(|segment| {
            segment.translate(shift);
        })
    }

    fn scale(&mut self, factor: f64) -> () {
        self.0.par_iter_mut().for_each(|segment| {
            segment.scale(factor);
        })
    }

    fn line_reflection(&mut self, start: [f64; 2], stop: [f64; 2]) -> () {
        self.0.par_iter_mut().for_each(|segment| {
            segment.line_reflection(start, stop);
        })
    }
}

impl ToBoundingBox for Polysegment {
    fn bounding_box(&self) -> BoundingBox {
        // Use the bounding box of the first segment as a starting point
        return self
            .segments_par()
            .skip(1)
            .map(|segment| BoundingBox::from(segment))
            .reduce(
                || {
                    if let Some(s) = self.get(0) {
                        BoundingBox::from(s)
                    } else {
                        BoundingBox::new(0.0, 0.0, 0.0, 0.0)
                    }
                },
                |prev, curr| prev.union(&curr),
            );
    }
}

impl IntoIterator for Polysegment {
    type Item = Segment;
    type IntoIter = std::collections::vec_deque::IntoIter<Segment>;

    fn into_iter(self) -> Self::IntoIter {
        return self.0.into_iter();
    }
}

impl From<Polysegment> for VecDeque<Segment> {
    fn from(line: Polysegment) -> Self {
        return line.0;
    }
}

impl From<BoundingBox> for Polysegment {
    fn from(bounding_box: BoundingBox) -> Self {
        return Self::from(&bounding_box);
    }
}

impl From<&BoundingBox> for Polysegment {
    fn from(bounding_box: &BoundingBox) -> Self {
        return Polysegment::from_points(&[
            [bounding_box.xmin(), bounding_box.ymin()],
            [bounding_box.xmax(), bounding_box.ymin()],
            [bounding_box.xmax(), bounding_box.ymax()],
            [bounding_box.xmin(), bounding_box.ymax()],
            [bounding_box.xmin(), bounding_box.ymin()],
        ]);
    }
}

impl From<&Polysegment> for crate::CentroidData {
    fn from(value: &Polysegment) -> Self {
        return value
            .segments_par()
            .map(|segment| match segment {
                Segment::LineSegment(line) => Self::from(line),
                Segment::ArcSegment(arc) => Self::from(arc),
            })
            .reduce(
                || Self {
                    area: 0.0,
                    x: 0.0,
                    y: 0.0,
                },
                |prev, curr| prev.union(&curr),
            );
    }
}

/**
Calculates the area of a polygon defined by the points given by an iterator.
If the polygon orientation is mathematically positive (points are given
counterclockwise), then the result is also positive. Otherwise, it is negative.

This implementation uses the "Shoelace formula", as e.g. described here:
<https://en.wikipedia.org/wiki/Shoelace_formula>.

```
use planar_geo::polysegment::area_signed;

let pt1 = [0.0, 0.0];
let pt2 = [1.0, 0.0];
let pt3 = [1.0, 1.0];
assert_eq!(
    area_signed([pt1, pt2, pt3].into_iter()),
    0.5
);
assert_eq!(
    area_signed([pt2, pt1, pt3].into_iter()),
    -0.5
);
```
 */
pub fn area_signed<'a, I>(mut points: I) -> f64
where
    I: Iterator<Item = [f64; 2]>,
{
    // Keep the first vertex
    let first_vertex = match points.next() {
        Some(vertex) => vertex,
        None => return 0.0,
    };
    let mut previous_vertex = first_vertex.clone();

    let mut area = 0.0;

    // The polysegmented element covers the end value x_n*y_0 - x_0*y_n, where n
    // equals the last iterator element
    for current_vertex in points.chain(std::iter::once(first_vertex)) {
        area += (previous_vertex[1] + current_vertex[1]) * (previous_vertex[0] - current_vertex[0]);
        previous_vertex = current_vertex.clone();
    }

    return 0.5 * area; // Correction factor of 0.5 comes from the area formel of a triangle.
}

pub(crate) fn covers_segment<'a, 'b, I>(
    iterator: I,
    segment: SegmentRef<'b>,
    epsilon: f64,
    max_ulps: u32,
) -> bool
where
    I: Iterator<Item = &'a Segment>,
{
    match segment {
        SegmentRef::LineSegment(line_segment) => {
            // Multiple subsequent line segments are combined into a single
            // one if they form a single line segment (i.e. their angles are
            // identical)
            let mut combined: Option<LineSegment> = None;
            for seg in iterator {
                if let Segment::LineSegment(sl) = seg {
                    combined = match combined {
                        Some(c) => {
                            if ulps_eq!(
                                c.angle(),
                                sl.angle(),
                                epsilon = epsilon,
                                max_ulps = max_ulps
                            ) {
                                // Add the new line segment, if it has the same angle as
                                // "combined"
                                if let Ok(new_combined) =
                                    LineSegment::new(c.start(), sl.stop(), epsilon, max_ulps)
                                {
                                    if new_combined.covers(line_segment, epsilon, max_ulps) {
                                        return true;
                                    }
                                    Some(new_combined)
                                } else {
                                    None
                                }
                            } else {
                                // Replace the old combined segment with the new line segment
                                if sl.covers(line_segment, epsilon, max_ulps) {
                                    return true;
                                }
                                Some(sl.clone())
                            }
                        }
                        None => {
                            if sl.covers(line_segment, epsilon, max_ulps) {
                                return true;
                            }
                            Some(sl.clone())
                        }
                    };
                } else {
                    // Arc segment breaks the chain
                    combined = None;
                }
            }
            return false;
        }
        SegmentRef::ArcSegment(arc_segment) => {
            // Multiple subsequent arc segments are combined into a single
            // one if they form a single arc segment
            // (i.e. their radius and center are identical).
            let mut combined: Option<ArcSegment> = None;
            for seg in iterator {
                if let Segment::ArcSegment(sa) = seg {
                    combined = match combined {
                        Some(c) => {
                            if ulps_eq!(
                                c.center(),
                                sa.center(),
                                epsilon = epsilon,
                                max_ulps = max_ulps
                            ) && ulps_eq!(
                                c.radius(),
                                sa.radius(),
                                epsilon = epsilon,
                                max_ulps = max_ulps
                            ) {
                                // Add the new line segment, if it has the same angle as
                                // "combined"
                                if let Ok(new_combined) =
                                    ArcSegment::from_center_radius_start_offset_angle(
                                        c.center(),
                                        c.radius(),
                                        c.start_angle(),
                                        c.offset_angle() + sa.offset_angle(),
                                        epsilon,
                                        max_ulps,
                                    )
                                {
                                    if new_combined.covers(arc_segment, epsilon, max_ulps) {
                                        return true;
                                    }
                                    Some(new_combined)
                                } else {
                                    None
                                }
                            } else {
                                // Replace the old combined segment with the new line segment
                                if sa.covers(arc_segment, epsilon, max_ulps) {
                                    return true;
                                }
                                Some(sa.clone())
                            }
                        }
                        None => {
                            if sa.covers(arc_segment, epsilon, max_ulps) {
                                return true;
                            }
                            Some(sa.clone())
                        }
                    };
                } else {
                    // Line segment breaks the chain
                    combined = None;
                }
            }
            return false;
        }
    }
}

fn intersections_between_polysegment_and_segment_priv<'a, L>(
    left: L,
    right_idx: usize,
    right_seg: &'a Segment,
    same_polysegment_len: Option<usize>,
    epsilon: f64,
    max_ulps: u32,
) -> impl Iterator<Item = Intersection> + 'a
where
    L: Iterator<Item = &'a Segment> + 'a,
{
    left.enumerate()
        .filter_map(move |(left_idx, left_seg)| {
            segment_intersections(
                left_seg,
                left_idx,
                right_seg,
                right_idx,
                same_polysegment_len,
                epsilon,
                max_ulps,
            )
        })
        .flatten()
}

fn intersections_between_polysegment_and_segment_priv_par<'a, L>(
    left: L,
    right_idx: usize,
    right_seg: &'a Segment,
    same_polysegment_len: Option<usize>,
    epsilon: f64,
    max_ulps: u32,
) -> impl ParallelIterator<Item = Intersection> + 'a
where
    L: IndexedParallelIterator<Item = &'a Segment> + 'a,
{
    left.enumerate()
        .filter_map(move |(left_idx, left_seg)| {
            segment_intersections(
                left_seg,
                left_idx,
                right_seg,
                right_idx,
                same_polysegment_len,
                epsilon,
                max_ulps,
            )
            .map(|iter| iter.par_bridge())
        })
        .flatten()
}

fn segment_intersections<'a>(
    left_seg: &'a Segment,
    left_idx: usize,
    right_seg: &'a Segment,
    right_idx: usize,
    same_polysegment_len: Option<usize>,
    epsilon: f64,
    max_ulps: u32,
) -> Option<impl Iterator<Item = Intersection> + 'a> {
    /*
    If the two segments are identical, they have no intersection by definition.

    If slices_are_identical, an additional optimization is possible:
    It is sufficient to only check segments where left_idx < right_idx.
    This is possible because a naive loop implementation visits every segment pair twice:
    a) Left segment is in the outer loop, right segment is in the inner loop
    b) Left segment is in the outer loop, right segment is in the inner loop
     */
    if same_polysegment_len.is_some() {
        if left_idx == right_idx {
            return None;
        }
        if left_idx >= right_idx {
            return None;
        }
    } else {
        if left_seg == right_seg {
            return None;
        }
    }

    let intersection_iter = left_seg
        .intersections_primitive(right_seg, epsilon, max_ulps)
        .into_iter()
        .filter_map(move |point| {
            let point: [f64; 2] = point.into();

            /*
            If the two slices are identical, check whether left_seg is a
            successor / predecessor of right_seg. If yes, filter out the
            connection points.
             */

            if let Some(len) = same_polysegment_len {
                /*
                This check evaluates whether the left segment is a predecessor
                of the right segment. If that is the case, filter out
                "intersections" which are actually just the connection point
                between the two segments.
                 */
                if left_idx + 1 == right_idx || (left_idx == 0 && right_idx == len - 1) {
                    if approx::ulps_eq!(
                        point,
                        left_seg.stop(),
                        epsilon = epsilon,
                        max_ulps = max_ulps
                    ) {
                        return None;
                    }
                }
            }

            Some(Intersection {
                point,
                left: SegmentKey::from_segment_idx(left_idx),
                right: SegmentKey::from_segment_idx(right_idx),
            })
        });

    return Some(intersection_iter);
}