geometry/
shape.rs

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
//! An enumeration of geometric shapes and their properties.

use serde::{Deserialize, Serialize};

use crate::{
    bbox::Bbox,
    contains::{Containment, Contains},
    point::Point,
    polygon::Polygon,
    rect::Rect,
    transform::{TransformMut, TransformRef, Transformation, TranslateMut, TranslateRef},
    union::BoundingUnion,
};

/// An enumeration of geometric shapes.
#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub enum Shape {
    /// A rectangle.
    Rect(Rect),
    /// A polygon.
    Polygon(Polygon),
}

impl Shape {
    /// If this shape is a rectangle, returns the contained rectangle.
    /// Otherwise, returns [`None`].
    pub fn rect(&self) -> Option<Rect> {
        match self {
            Self::Rect(r) => Some(*r),
            _ => None,
        }
    }

    /// If this shape is a polygon, returns the contained polygon.
    /// Otherwise, returns [`None`].
    pub fn polygon(&self) -> Option<&Polygon> {
        match self {
            Self::Polygon(p) => Some(p),
            _ => None,
        }
    }
}

impl TranslateRef for Shape {
    #[inline]
    fn translate_ref(&self, p: Point) -> Self {
        match self {
            Shape::Rect(rect) => Shape::Rect(rect.translate_ref(p)),
            Shape::Polygon(polygon) => Shape::Polygon(polygon.translate_ref(p)),
        }
    }
}

impl TranslateMut for Shape {
    #[inline]
    fn translate_mut(&mut self, p: Point) {
        match self {
            Shape::Rect(rect) => rect.translate_mut(p),
            Shape::Polygon(polygon) => polygon.translate_mut(p),
        };
    }
}

impl TransformRef for Shape {
    #[inline]
    fn transform_ref(&self, trans: Transformation) -> Self {
        match self {
            Shape::Rect(rect) => Shape::Rect(rect.transform_ref(trans)),
            Shape::Polygon(polygon) => Shape::Polygon(polygon.transform_ref(trans)),
        }
    }
}

impl TransformMut for Shape {
    #[inline]
    fn transform_mut(&mut self, trans: crate::prelude::Transformation) {
        match self {
            Shape::Rect(rect) => rect.transform_mut(trans),
            Shape::Polygon(polygon) => polygon.transform_mut(trans),
        }
    }
}

impl Bbox for Shape {
    fn bbox(&self) -> Option<Rect> {
        match self {
            Shape::Rect(rect) => rect.bbox(),
            Shape::Polygon(polygon) => polygon.bbox(),
        }
    }
}

impl From<Rect> for Shape {
    #[inline]
    fn from(value: Rect) -> Self {
        Self::Rect(value)
    }
}

impl From<Polygon> for Shape {
    #[inline]
    fn from(value: Polygon) -> Self {
        Self::Polygon(value)
    }
}

impl<T: Bbox> BoundingUnion<T> for Shape {
    type Output = Option<Rect>;

    fn bounding_union(&self, other: &T) -> Self::Output {
        self.bbox().bounding_union(&other.bbox())
    }
}

impl Contains<crate::point::Point> for Shape {
    fn contains(&self, p: &crate::point::Point) -> Containment {
        match self {
            Shape::Rect(rect) => rect.contains(p),
            Shape::Polygon(polygon) => polygon.contains(p),
        }
    }
}