1pub mod base;
2pub mod shape;
3pub mod prelude;
4pub mod traits;
5pub mod transform;
6pub mod repetition;
7
8use num_traits::{Num, NumCast};
9use shape::{CoordType, Edge, Path, Point, Polygon, Polyline, Rect, Text};
10use traits::{Cast, MapPoint, TryBoundingBox};
11use transform::Transform;
12
13#[derive(PartialEq, Eq, Clone, Debug, Hash)]
14pub enum Geometry<T> {
15 Point(Point<T>),
16 Edge(Edge<T>),
17 Rect(Rect<T>),
18 Polyline(Polyline<T>),
19 Polygon(Polygon<T>),
20 Path(Path<T>),
21 Text(Text<T>),
22}
23
24macro_rules! geometry_from {
25 ( $t:tt ) => {
26 impl<T> From<$t<T>> for Geometry<T> {
27 fn from(x: $t<T>) -> Geometry<T> {
28 Geometry::$t(x)
29 }
30 }
31 };
32}
33
34geometry_from!(Point);
35geometry_from!(Edge);
36geometry_from!(Rect);
37geometry_from!(Polyline);
38geometry_from!(Polygon);
39geometry_from!(Path);
40geometry_from!(Text);
41
42impl<T: CoordType> Geometry<T> {
43 pub fn transformed(&self, transform: &Transform<T>) -> Self {
44 let trans = |p| transform.transform_point(p);
45 match self {
46 Geometry::Point(p) => transform.transform_point(*p).into(),
47 Geometry::Edge(g) => g.transform(trans).into(),
48 Geometry::Rect(g) => g.transform(trans).into(),
49 Geometry::Polyline(g) => g.transform(trans).into(),
50 Geometry::Polygon(g) => g.transform(trans).into(),
51 Geometry::Path(p) => p.transform(transform).into(),
52 Geometry::Text(g) => g.transform(trans).into(),
53 }
54 }
55}
56
57impl<T: Copy + PartialOrd + Num> TryBoundingBox<T> for Geometry<T> {
58 fn try_bounding_box(&self) -> Option<Rect<T>> {
59 match self {
60 Geometry::Point(p) => p.try_bounding_box(),
61 Geometry::Edge(e) => e.try_bounding_box(),
62 Geometry::Rect(e) => e.try_bounding_box(),
63 Geometry::Polyline(e) => e.try_bounding_box(),
64 Geometry::Polygon(e) => e.try_bounding_box(),
65 Geometry::Path(p) => p.try_bounding_box(),
66 Geometry::Text(t) => t.try_bounding_box(),
67 }
68 }
69}
70
71impl<T: CoordType + NumCast, Dst: CoordType + NumCast> Cast<T, Dst>
72 for Geometry<T>
73{
74 type Output = Geometry<Dst>;
75
76 fn try_cast(&self) -> Option<Self::Output> {
77 match self {
78 Geometry::Point(p) => p.try_cast().map(|s| s.into()),
79 Geometry::Edge(e) => e.try_cast().map(|s| s.into()),
80 Geometry::Rect(r) => r.try_cast().map(|s| s.into()),
81 Geometry::Polyline(p) => p.try_cast().map(|s| s.into()),
82 Geometry::Polygon(p) => p.try_cast().map(|s| s.into()),
83 Geometry::Path(p) => p.try_cast().map(|s| s.into()),
84 Geometry::Text(t) => t.try_cast().map(|s| s.into()),
85 }
86 }
87}