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
use ::std::convert::From;
use ::std::ops::Add;

use ::num_traits::sign::Signed;

use crate::geom::Line;
use crate::geom::Point;
use crate::geom::Size;
use crate::num::Num;
use crate::num::NumIdentity;

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Transform<N: Num = f32> {
    scale: Size<N>,
    position: Point<N>,
    rotation: f32,
}

impl<N: Num> Transform<N> {
    #[must_use]
    pub fn new() -> Self {
        Self {
            position: Point::new_zero_value(),
            scale: Size::new_one_value(),
            rotation: 0.0,
        }
    }

    #[must_use]
    pub fn set_position(mut self, position: Point<N>) -> Self {
        self.position = position;
        self
    }

    pub fn position(&self) -> Point<N> {
        self.position
    }

    #[must_use]
    pub fn set_rotation(mut self, rotation: f32) -> Self {
        self.rotation = rotation;
        self
    }

    pub fn rotation(&self) -> f32 {
        self.rotation
    }

    #[must_use]
    fn set_scale_width(mut self, scale_width: N) -> Self {
        self.scale.set_width(scale_width);
        self
    }

    #[must_use]
    fn set_scale_height(mut self, scale_height: N) -> Self {
        self.scale.set_height(scale_height);
        self
    }

    #[must_use]
    pub fn set_scale(mut self, scale: Size<N>) -> Self {
        self.scale = scale;
        self
    }

    pub fn scale(self) -> Size<N> {
        self.scale
    }
}

impl<N> Transform<N>
where
    N: Num + Signed,
{
    pub fn set_flip_x_from_dir_num(self, dir: f32) -> Self {
        self.set_flip_x_from_bool(dir < 0.0)
    }

    pub fn set_flip_y_from_dir_num(self, dir: f32) -> Self {
        self.set_flip_y_from_bool(dir < 0.0)
    }

    pub fn set_flip_x_from_bool(self, is_flipped: bool) -> Self {
        if is_flipped {
            self.set_scale_width(-<N as NumIdentity>::one())
        } else {
            self.set_scale_width(<N as NumIdentity>::one())
        }
    }

    pub fn set_flip_y_from_bool(self, is_flipped: bool) -> Self {
        if is_flipped {
            self.set_scale_height(-<N as NumIdentity>::one())
        } else {
            self.set_scale_height(<N as NumIdentity>::one())
        }
    }
}

impl<N> Add<Line<N>> for Transform<N>
where
    N: Num,
{
    type Output = Line<N>;

    fn add(self, line: Self::Output) -> Self::Output {
        line.rotate_around_zero(self.rotation()) * self.scale() + self.position()
    }
}

impl<N> Add<Point<N>> for Transform<N>
where
    N: Num,
{
    type Output = Point<N>;

    fn add(self, point: Self::Output) -> Self::Output {
        point.rotate_around_zero(self.rotation()) * self.scale() + self.position()
    }
}

impl<N> From<Point<N>> for Transform<N>
where
    N: Num,
{
    fn from(p: Point<N>) -> Self {
        Self::new().set_position(p)
    }
}

impl<N> From<Size<N>> for Transform<N>
where
    N: Num,
{
    fn from(s: Size<N>) -> Self {
        Self::new().set_scale(s)
    }
}