reda_geometry/
transform.rs

1use std::ops::Mul;
2use num_traits::{One, Zero};
3use crate::{base::{Angle, Vec2}, shape::{CoordType, Point}, traits::{Mirror, Rotate, Scale, Translate}};
4
5pub struct Transform<T> {
6    pub mirror: bool,
7    pub rotation: Angle,
8    pub magnification: T,
9    pub displacement: Vec2<T>,
10}
11
12impl<T> Transform<T> {
13    pub fn new(mirror: bool, rotation: Angle, magnification: T, displacement: Vec2<T>) -> Self {
14        Self { 
15            mirror,
16            rotation,
17            magnification,
18            displacement, 
19        }
20    }
21}
22
23impl<T: Zero + One> Transform<T> {
24    pub fn identity() -> Self {
25        Self::translate(Vec2::zero())
26    }
27
28    pub fn translate<V: Into<Vec2<T>>>(v: V) -> Self {
29        let t = v.into();
30        Self::new(false, Angle::R0, T::one(), t)
31    }
32
33    pub fn rotate(angle: Angle) -> Self {
34        Self::new(false, angle, T::one(), Vec2::zero())
35    }
36
37    pub fn rotate_ccw90() -> Self {
38        Self::rotate(Angle::R90)
39    }
40
41    pub fn rotate_cw90() -> Self {
42        Self::rotate(Angle::R270)
43    }
44
45    pub fn mirror_x() -> Self {
46        Self::new(true, Angle::R0, T::one(), Vec2::zero())
47    }
48
49    pub fn mirror_y() -> Self {
50        Self::new(true, Angle::R180, T::one(), Vec2::zero())
51    }
52
53    pub fn scale(factor: T) -> Self {
54        Self::new(false, Angle::R0, factor, Vec2::zero())
55    }
56}
57
58impl<T> Transform<T>
59where
60    T: Copy + Mul<Output = T>,
61{
62    pub fn transform_distance(&self, d: T) -> T {
63        d * self.magnification
64    }
65}
66
67impl<T: CoordType> Transform<T> {
68    pub fn rotate_around(angle: Angle, rotation_center: Point<T>) -> Self {
69        Self::translate(Point::zero() - rotation_center)
70            .then(&Self::rotate(angle))
71            .then(&Self::translate(rotation_center))
72    }
73
74    pub fn transform_point(&self, p: Point<T>) -> Point<T> {
75        if self.mirror { p.mirror_x() } else { p }
76            .rotate(self.rotation)
77            .scale(self.magnification)
78            .translate(self.displacement)
79    }
80
81    /// Return a new transformation that is equal to applying
82    pub fn then(&self, t: &Self) -> Self {
83        let d = t.transform_point(self.displacement.into());
84        let r = if t.mirror {
85            -self.rotation
86        } else {
87            self.rotation
88        };
89        Self {
90            mirror: self.mirror ^ t.mirror,
91            rotation: r + t.rotation,
92            magnification: self.magnification * t.magnification,
93            displacement: d.vec2()
94        }
95    }   
96}
97
98impl<T: CoordType> Default for Transform<T> {
99    fn default() -> Self {
100        Self::identity()
101    }
102}
103
104#[cfg(test)]
105mod test {
106    use crate::{base::Angle, shape::Point, transform::Transform};
107
108    #[test]
109    fn test_simple_transform_combine() {
110        let t1 = Transform::new(false, Angle::R90, 1, (1, 2).into());
111        let t2 = Transform::new(true, Angle::R90, 1, (3, 4).into());
112    
113        let p = Point::new(10, 11);
114        assert_eq!(
115            t2.transform_point(t1.transform_point(p)),
116            t1.then(&t2).transform_point(p)
117        );
118        assert_eq!(
119            t1.transform_point(t2.transform_point(p)),
120            t2.then(&t1).transform_point(p)
121        );
122    }
123}