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
/*!
Defines the [`Shape`] type, a [`Composite`] representing the external boundaries
of an object in 2D space.

A [`Shape`] is made up of multiple [`Contour`]s, where one contour describes the
object extents ("outer contour"), whereas all other contours define "holes"
within the object.

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

use crate::CentroidData;
use crate::composite::{Composite, Intersection, SegmentKey};
use crate::error::ShapeConstructorError;
use crate::segment::SegmentRef;
use crate::{DEFAULT_EPSILON, DEFAULT_MAX_ULPS};
use crate::{Transformation, contour::Contour};
use bounding_box::{BoundingBox, ToBoundingBox};
use rayon::prelude::*;

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

/**
A collection of [`Contour`]s which describe the boundaries / extents of a
geometric body in 2D space.

*/
#[doc = ""]
#[cfg_attr(feature = "doc-images", doc = "![Shape example][example_shape]")]
#[cfg_attr(
    feature = "doc-images",
    embed_doc_image::embed_doc_image("example_shape", "docs/img/example_shape.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."
)]
/**

A [`Shape`] always has at least one [`Contour`] which describes the outer
extents of the body represented by it. Any additional contours are interpreted
as "holes" within the "outer" contour. These contours need to fulfill the
following invariants:
- The outer contour contains all holes (see [`Contour::contains`]).
- No hole may contain another hole
- No contour intersects with itself or any other contour.
- No contour might be empty (i.e. contain no
[`Segment`](crate::segment::Segment)).

# Constructing and modifying a shape

The standard constructor which checks all the aforementioned invariants is
[`new`](Shape::new), which takes a [`Vec<Contour`](Contour), interprets the first
element as the outer contour and all other elements as holes. Alternatively, a
shape can be constructed from its outer contour via
[`from_outer`](Shape::from_outer) and holes can be added incrementally with
[`add_hole`](Shape::add_hole). Holes can also be removed with
[`remove_hole`](Shape::remove_hole).

# Access of individual contours and segments

Individual segments can be retrieved with [`Composite::segment`]. The outer
contour is available with [`contour`](Shape::contour) method, while a slice
of all holes can be accessed with [`holes`](Shape::holes). The
[`contours`](Shape::contours) method returns a slice where the first element is
the outer contour and all other elements are the holes. A shape can also be
converted into the underlying [`Vec<Contour>`](Contour) with its [`From`]
implementation.

# Serialization and deserialization

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

impl Shape {
    /**
    Creates a new [`Shape`] out of the given `contours`. The first element is
    interpreted as the outer contour of the shape, the other elements are
    interpreted as holes.

    The given vector of [`Contour`]s must fulfill the following conditions:
    - It must not be empty.
    - None of its elements (contours) must be empty.
    - None of the contours must intersect itself or each other.
    - The first ("outer") contour must contain all other contours / "holes".
    - No hole is contained within another hole.

    # Examples

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

    // Shape fulfills all conditions
    let vertices = &[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]];
    let c1 = Contour::new(Polysegment::from_points(vertices));
    let vertices = &[[0.1, 0.1], [0.9, 0.1], [0.9, 0.9], [0.1, 0.9]];
    let c2 = Contour::new(Polysegment::from_points(vertices));
    assert!(Shape::new(vec![c1, c2]).is_ok());

    // Given vector is empty
    assert!(Shape::new(Vec::new()).is_err());

    // One of the contours is empty
    let vertices = &[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]];
    let c1 = Contour::new(Polysegment::from_points(vertices));
    let c2 = Contour::new(Polysegment::new());
    assert!(Shape::new(vec![c1, c2]).is_err());

    // The contours intersect
    let vertices = &[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]];
    let c1 = Contour::new(Polysegment::from_points(vertices));
    let vertices = &[[0.1, 0.1], [1.1, 0.1], [1.1, 0.9], [0.1, 0.9]];
    let c2 = Contour::new(Polysegment::from_points(vertices));
    assert!(Shape::new(vec![c1, c2]).is_err());

    // Second contour is not inside the first contour
    let vertices = &[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]];
    let c1 = Contour::new(Polysegment::from_points(vertices));
    let vertices = &[[1.0, 0.1], [2.0, 0.1], [2.0, 0.9], [1.0, 0.9]];
    let c2 = Contour::new(Polysegment::from_points(vertices));
    assert!(Shape::new(vec![c1, c2]).is_err());
    ```
     */
    pub fn new(contours: Vec<Contour>) -> Result<Self, ShapeConstructorError<Vec<Contour>>> {
        if contours.len() == 0 {
            return Err(ShapeConstructorError::EmptyVec);
        }
        for (idx, contour) in contours.iter().enumerate() {
            if contour.is_empty() {
                return Err(ShapeConstructorError::EmptyContour {
                    input: contours,
                    idx,
                });
            }
        }
        let this = Shape(contours);

        let outer = this.contour();
        for (first_hole_idx, first_hole) in this.holes().iter().enumerate() {
            if !outer.contains_contour(&first_hole, DEFAULT_EPSILON, DEFAULT_MAX_ULPS) {
                return Err(ShapeConstructorError::HoleOutsideContour {
                    input: this.0,
                    idx: first_hole_idx + 1, // First element of this.0 is the outer contour.
                });
            }

            for (second_hole_idx, second_hole) in
                this.holes()[(first_hole_idx + 1)..].iter().enumerate()
            {
                if first_hole.contains_contour(second_hole, DEFAULT_EPSILON, DEFAULT_MAX_ULPS) {
                    return Err(ShapeConstructorError::HoleInsideHole {
                        input: this.0,
                        outer_hole_idx: first_hole_idx,
                        inner_hole_idx: second_hole_idx,
                    });
                }
                if second_hole.contains_contour(first_hole, DEFAULT_EPSILON, DEFAULT_MAX_ULPS) {
                    return Err(ShapeConstructorError::HoleInsideHole {
                        input: this.0,
                        outer_hole_idx: second_hole_idx,
                        inner_hole_idx: first_hole_idx,
                    });
                }
            }
        }

        // Check if the shape intersects itself
        if let Some(intersection) = this
            .intersections_shape_par(&this, DEFAULT_EPSILON, DEFAULT_MAX_ULPS)
            .find_map_any(|v| Some(v))
        {
            return Err(ShapeConstructorError::Intersection {
                input: this.0,
                intersection,
            });
        }

        return Ok(this);
    }

    /**
    Creates a contour with no holes from its outer contour.

    The contour must not be empty and must not intersect itself.

    # Examples

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

    // Contour fulfill al criteria
    let vertices = &[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]];
    let c = Contour::new(Polysegment::from_points(vertices));
    assert!(Shape::from_outer(c).is_ok());

    // Contour is empty
    let c = Contour::new(Polysegment::new());
    assert!(Shape::from_outer(c).is_err());

    // Contour intersects itself
    let vertices = &[[0.0, 0.0], [1.0, 1.0], [1.0, 0.0], [0.0, 1.0]];
    let c = Contour::new(Polysegment::from_points(vertices));
    assert!(Shape::from_outer(c).is_err());
    ```
     */
    pub fn from_outer(outer: Contour) -> Result<Self, ShapeConstructorError<Contour>> {
        if outer.is_empty() {
            return Err(ShapeConstructorError::EmptyContour {
                input: outer,
                idx: 0,
            });
        }
        if let Some(i) = outer
            .intersections_contour_par(&outer, DEFAULT_EPSILON, DEFAULT_MAX_ULPS)
            .find_map_any(|v| Some(v))
        {
            return Err(ShapeConstructorError::Intersection {
                input: outer,
                intersection: Intersection {
                    point: i.point,
                    left: i.left,
                    right: i.right,
                },
            });
        }

        return Ok(Shape(vec![outer]));
    }

    /**
    Returns a reference to all [`Contour`]s defining `self`. The first contour
    is the "outer" contour (see [`Shape::contour`]), all other contours are
    interpreted as "holes" (see [`Shape::holes`]).
     */
    pub fn contours(&self) -> &[Contour] {
        return &self.0;
    }

    /**
    Returns the "outer" [`Contour`] of `self`. This is the first element of the
    underlying [`Vec<Contour>`].
     */
    pub fn contour<'a>(&'a self) -> &'a Contour {
        /*
        Safety: This function is safe since it is guaranteed during the Shape
        element construction that it contains at least the contour (e.g. the
        field "lines" has a length of at least 1).
        */
        return unsafe { self.0.get_unchecked(0) };
    }

    /**
    Returns all "holes" of `self`. If the [`Shape`] has no holes, the returned
    slice is empty.

    # Examples

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

    let vertices = &[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]];
    let c1 = Contour::new(Polysegment::from_points(vertices));

    let vertices = &[[0.1, 0.1], [0.4, 0.1], [0.4, 0.9], [0.1, 0.9]];
    let c2 = Contour::new(Polysegment::from_points(vertices));

    let vertices = &[[0.6, 0.1], [0.9, 0.1], [0.9, 0.9], [0.6, 0.9]];
    let c3 = Contour::new(Polysegment::from_points(vertices));

    let shape = Shape::new(vec![c1, c2, c3]).expect("valid input");
    assert_eq!(shape.holes().len(), 2);
    assert_eq!(shape.holes().len() + 1, shape.contours().len());
    ```
     */
    pub fn holes(&self) -> &[Contour] {
        return &self.0[1..self.0.len()]; // Skip first element since it is the contour of the shape
    }

    /**
    Returns the surface area of `self`.

    This function calculates the surface area of all contours (via
    [`Contour::area`]) and then subtracts the areas of all holes from that of
    the outer contour.

    # Examples

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

    let vertices = &[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]];
    let contour = Contour::new(Polysegment::from_points(vertices));

    let vertices = &[[0.1, 0.1], [0.9, 0.1], [0.9, 0.9], [0.1, 0.9]];
    let hole = Contour::new(Polysegment::from_points(vertices));

    let shape = Shape::new(vec![contour.clone(), hole.clone()]).expect("valid input");
    assert_eq!(shape.area(), contour.area() - hole.area());
    ```
     */
    pub fn area(&self) -> f64 {
        let contour_area = self.contour().area();
        let holes: f64 = self
            .holes()
            .par_iter()
            .map(|polysegment| return polysegment.area())
            .sum();
        return contour_area - holes;
    }

    /**
    Tries to add a hole to `self`. The given contour must fulfill the following
    conditions:
    - It must be within the outer contour of `self`.
    - It must not be empty.
    - It must not intersect any of the contours making up the shape.
    - It must not be within another hole.

    If any of these conditions are not met, an error is returned which contains
    the input `hole`.

    # Examples

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

    // Shaoe without an hole
    let vertices = &[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]];
    let mut shape = Shape::new(vec![Contour::new(Polysegment::from_points(vertices))]).expect("valid input");
    assert_eq!(shape.holes().len(), 0);

    // Add a hole to the shape - this works
    let vertices = &[[0.1, 0.1], [0.9, 0.1], [0.9, 0.9], [0.1, 0.9]];
    let hole = Contour::new(Polysegment::from_points(vertices));
    assert!(shape.add_hole(hole).is_ok());
    assert_eq!(shape.holes().len(), 1);

    // Adding this hole does not work because it intersects with the outer contour
    let vertices = &[[1.0, 0.1], [1.0, 0.1], [2.0, 0.9], [2.0, 0.9]];
    let hole = Contour::new(Polysegment::from_points(vertices));
    assert!(shape.add_hole(hole).is_err());
    assert_eq!(shape.holes().len(), 1);
    ```
     */
    pub fn add_hole(&mut self, hole: Contour) -> Result<(), ShapeConstructorError<Contour>> {
        if hole.is_empty() {
            return Err(ShapeConstructorError::EmptyContour {
                input: hole,
                idx: 0,
            });
        }

        let outer = self.contour();
        if !outer.contains_contour(&hole, DEFAULT_EPSILON, DEFAULT_MAX_ULPS) {
            return Err(ShapeConstructorError::HoleOutsideContour {
                input: hole,
                idx: 0, // Dummy value
            });
        }

        for (shape_hole_idx, shape_hole) in self.holes().iter().enumerate() {
            if hole.contains_contour(shape_hole, DEFAULT_EPSILON, DEFAULT_MAX_ULPS) {
                return Err(ShapeConstructorError::HoleInsideHole {
                    input: hole,
                    outer_hole_idx: shape_hole_idx,
                    inner_hole_idx: 0,
                });
            }
            if shape_hole.contains_contour(&hole, DEFAULT_EPSILON, DEFAULT_MAX_ULPS) {
                return Err(ShapeConstructorError::HoleInsideHole {
                    input: hole,
                    outer_hole_idx: 0,
                    inner_hole_idx: shape_hole_idx,
                });
            }
        }

        if let Some(intersection) = self
            .intersections_contour_par(&hole, DEFAULT_EPSILON, DEFAULT_MAX_ULPS)
            .find_map_any(|v| Some(v))
        {
            return Err(ShapeConstructorError::Intersection {
                input: hole,
                intersection,
            });
        }

        self.0.push(hole);

        return Ok(());
    }

    /**
    Removes the hole with the given `index` from `self` and returns the
    associated contour. If no hole exists for the given `index`, `None` is
    returned instead.

    This operation performs a [`Vec::swap_remove`] on the underlying vector.
    This means that the order of holes can change (but the first element of
    the vector which represents the contour will not change position).

    # Examples

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

    let vertices = &[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]];
    let c1 = Contour::new(Polysegment::from_points(vertices));

    let vertices = &[[0.1, 0.1], [0.4, 0.1], [0.4, 0.9], [0.1, 0.9]];
    let c2 = Contour::new(Polysegment::from_points(vertices));

    let vertices = &[[0.6, 0.1], [0.9, 0.1], [0.9, 0.9], [0.6, 0.9]];
    let c3 = Contour::new(Polysegment::from_points(vertices));

    let mut shape = Shape::new(vec![c1, c2, c3]).unwrap();

    assert_eq!(shape.holes().len(), 2);
    assert!(shape.remove_hole(2).is_none());

    assert!(shape.remove_hole(1).is_some());
    assert_eq!(shape.holes().len(), 1);

    assert!(shape.remove_hole(0).is_some());
    assert_eq!(shape.holes().len(), 0);

    assert!(shape.remove_hole(0).is_none());
    assert_eq!(shape.holes().len(), 0);
    ```
     */
    pub fn remove_hole(&mut self, index: usize) -> Option<Contour> {
        if self.holes().len() > index {
            return Some(self.0.swap_remove(index + 1));
        }
        return None;
    }
}

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

impl Composite for Shape {
    fn segment(&self, key: SegmentKey) -> Option<&crate::segment::Segment> {
        let contour = self.0.get(key.contour_idx)?;
        return contour.segment(key);
    }

    fn num_segments(&self) -> usize {
        return self.0.iter().map(Composite::num_segments).sum();
    }

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

    fn iter<'a>(&'a self) -> impl Iterator<Item = (SegmentKey, &'a crate::segment::Segment)> {
        return self.0.iter().enumerate().flat_map(|(i, c)| {
            c.iter().map(move |(mut k, s)| {
                k.contour_idx = i;
                return (k, s);
            })
        });
    }

    fn par_iter<'a>(
        &'a self,
    ) -> impl ParallelIterator<Item = (SegmentKey, &'a crate::segment::Segment)> {
        return self.0.par_iter().enumerate().flat_map(|(i, c)| {
            c.par_iter().map(move |(mut k, s)| {
                k.contour_idx = i;
                return (k, s);
            })
        });
    }

    fn intersections_polysegment<'a>(
        &'a self,
        polysegment: &'a crate::prelude::Polysegment,
        epsilon: f64,
        max_ulps: u32,
    ) -> impl Iterator<Item = Intersection> + 'a {
        self.contours()
            .iter()
            .enumerate()
            .flat_map(move |(c1, contour_1)| {
                contour_1
                    .polysegment()
                    .intersections_polysegment(polysegment, epsilon, max_ulps)
                    .map(move |i| Intersection {
                        point: i.point,
                        left: SegmentKey {
                            contour_idx: c1,
                            segment_idx: i.left.segment_idx,
                        },
                        right: i.right,
                    })
            })
    }

    fn intersections_polysegment_par<'a>(
        &'a self,
        polysegment: &'a crate::prelude::Polysegment,
        epsilon: f64,
        max_ulps: u32,
    ) -> impl ParallelIterator<Item = Intersection> + 'a {
        self.contours()
            .par_iter()
            .enumerate()
            .flat_map(move |(c1, contour_1)| {
                contour_1
                    .polysegment()
                    .intersections_polysegment_par(polysegment, epsilon, max_ulps)
                    .map(move |i| Intersection {
                        point: i.point,
                        left: SegmentKey {
                            contour_idx: c1,
                            segment_idx: i.left.segment_idx,
                        },
                        right: i.right,
                    })
            })
    }

    fn intersections_shape<'a>(
        &'a self,
        shape: &'a Shape,
        epsilon: f64,
        max_ulps: u32,
    ) -> impl Iterator<Item = Intersection> + 'a {
        self.contours()
            .iter()
            .enumerate()
            .flat_map(move |(idx_1, contour_1)| {
                shape
                    .contours()
                    .iter()
                    .enumerate()
                    .map(move |(idx_2, contour_2)| {
                        contour_1
                            .intersections_contour(contour_2, epsilon, max_ulps)
                            .map(move |i| Intersection {
                                point: i.point,
                                left: SegmentKey {
                                    contour_idx: idx_1,
                                    segment_idx: i.left.segment_idx,
                                },
                                right: SegmentKey {
                                    contour_idx: idx_2,
                                    segment_idx: i.right.segment_idx,
                                },
                            })
                    })
                    .flatten()
            })
    }

    fn intersections_shape_par<'a>(
        &'a self,
        shape: &'a Shape,
        epsilon: f64,
        max_ulps: u32,
    ) -> impl ParallelIterator<Item = Intersection> + 'a {
        self.contours()
            .par_iter()
            .enumerate()
            .flat_map(move |(idx_1, contour_1)| {
                shape
                    .contours()
                    .par_iter()
                    .enumerate()
                    .map(move |(idx_2, contour_2)| {
                        contour_1
                            .intersections_contour_par(contour_2, epsilon, max_ulps)
                            .map(move |i| Intersection {
                                point: i.point,
                                left: SegmentKey {
                                    contour_idx: idx_1,
                                    segment_idx: i.left.segment_idx,
                                },
                                right: SegmentKey {
                                    contour_idx: idx_2,
                                    segment_idx: i.right.segment_idx,
                                },
                            })
                    })
                    .flatten()
            })
    }

    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_shape(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_shape_par(self, epsilon, max_ulps)
            .map(Intersection::switch);
    }

    fn covers_point(&self, point: [f64; 2], epsilon: f64, max_ulps: u32) -> bool {
        self.contours().par_iter().enumerate().all(|(i, c)| {
            if i == 0 {
                // Special handling of outer contour
                c.covers_point(point, epsilon, max_ulps)
            } else {
                !c.contains_point(point, epsilon, max_ulps)
            }
        })
    }

    fn covers_segment<'a, T: Into<SegmentRef<'a>>>(
        &self,
        segment: T,
        epsilon: f64,
        max_ulps: u32,
    ) -> bool {
        let segment: SegmentRef = segment.into();
        self.contours().par_iter().enumerate().all(|(i, c)| {
            if i == 0 {
                // Special handling of outer contour
                c.covers_segment(segment.clone(), epsilon, max_ulps)
            } else {
                !c.contains_segment(segment.clone(), epsilon, max_ulps)
            }
        })
    }

    fn contains_point(&self, point: [f64; 2], epsilon: f64, max_ulps: u32) -> bool {
        self.contours().par_iter().enumerate().all(|(i, c)| {
            if i == 0 {
                // Special handling of outer contour
                c.contains_point(point, epsilon, max_ulps)
            } else {
                !c.covers_point(point, epsilon, max_ulps)
            }
        })
    }

    fn contains_segment<'a, T: Into<crate::prelude::SegmentRef<'a>>>(
        &self,
        segment: T,
        epsilon: f64,
        max_ulps: u32,
    ) -> bool {
        let segment: SegmentRef = segment.into();
        self.contours().par_iter().enumerate().all(|(i, c)| {
            if i == 0 {
                // Special handling of outer contour
                c.contains_segment(segment.clone(), epsilon, max_ulps)
            } else {
                !c.covers_segment(segment.clone(), epsilon, max_ulps)
            }
        })
    }

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

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

    fn overlaps_segment<'a, T: Into<SegmentRef<'a>>>(
        &self,
        segment: T,
        epsilon: f64,
        max_ulps: u32,
    ) -> bool {
        let segment: SegmentRef = segment.into();
        /*
        The segment overlaps the shape if it overlaps the outer contour and is
        not contained in one of the shape's holes
         */
        if !self
            .contour()
            .overlaps_segment(segment.clone(), epsilon, max_ulps)
        {
            return false;
        }
        for hole in self.holes() {
            if hole.covers_segment(segment.clone(), epsilon, max_ulps) {
                return false;
            }
        }
        return true;
    }

    fn overlaps_contour(&self, contour: &Contour, epsilon: f64, max_ulps: u32) -> bool {
        // If other either not overlaps the outer contour of the shape or if it
        // is completely covered by one of the holes, then there is no overlap

        if !(self.contour().overlaps_contour(contour, epsilon, max_ulps)) {
            return false;
        }

        for hole in self.holes() {
            if hole.covers_contour(contour, epsilon, max_ulps) {
                return false;
            }
        }
        return true;
    }

    fn overlaps_shape(&self, other: &Self, epsilon: f64, max_ulps: u32) -> bool {
        return std::ptr::eq(self, other)
            || (self.overlaps_contour(other.contour(), epsilon, max_ulps)
                && other.overlaps_contour(self.contour(), epsilon, max_ulps));
    }

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

impl TryFrom<Contour> for Shape {
    type Error = ShapeConstructorError<Vec<Contour>>;

    fn try_from(value: Contour) -> Result<Self, Self::Error> {
        return Shape::new(vec![value]);
    }
}

impl Transformation for Shape {
    fn translate(&mut self, shift: [f64; 2]) -> () {
        self.0
            .as_mut_slice()
            .par_iter_mut()
            .for_each(|polygon| polygon.translate(shift));
    }

    fn rotate(&mut self, center: [f64; 2], angle: f64) -> () {
        self.0
            .as_mut_slice()
            .par_iter_mut()
            .for_each(|polygon| polygon.rotate(center, angle));
    }

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

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

impl ToBoundingBox for Shape {
    fn bounding_box(&self) -> BoundingBox {
        return self.contour().bounding_box();
    }
}

impl From<&Shape> for CentroidData {
    fn from(value: &Shape) -> Self {
        let contour_centroid = CentroidData::from(value.contour());
        return value
            .holes()
            .iter()
            .map(|contour| CentroidData::from(contour))
            .fold(contour_centroid, |prev, curr| prev.subtract(&curr));
    }
}

impl From<Shape> for Vec<Contour> {
    fn from(value: Shape) -> Self {
        return value.0;
    }
}