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
#[derive(Copy, Clone, PartialEq)]
pub struct Transform {
    pub translation_x: f32,
    pub translation_y: f32,
    pub scale_x: f32,
    pub scale_y: f32,
    pub skew_x: f32,
    pub skew_y: f32,
}

impl Default for Transform {
    fn default() -> Self {
        Self {
            translation_x: 0.,
            translation_y: 0.,
            scale_x: 1.,
            scale_y: 1.,
            skew_x: 0.,
            skew_y: 0.,
        }
    }
}

impl Transform {
    pub fn rotation<R: Into<super::Rad>>(phi: R) -> Self {
        let phi = phi.into();
        let (sin, cos) = phi.0.sin_cos();
        Self {
            scale_x: cos,
            skew_y: -sin,
            skew_x: sin,
            scale_y: cos,
            ..Default::default()
        }
    }

    pub fn scale(scale: f32) -> Self {
        Self {
            scale_x: scale,
            scale_y: scale,
            ..Default::default()
        }
    }

    pub fn translation(x: f32, y: f32) -> Self {
        Self {
            translation_x: x,
            translation_y: y,
            ..Default::default()
        }
    }

    pub fn transform_point(&self, x: f32, y: f32) -> (f32, f32) {
        (
            x * self.scale_x + y * self.skew_x + self.translation_x,
            x * self.skew_y + y * self.scale_y + self.translation_y,
        )
    }
}

impl std::ops::Mul for Transform {
    type Output = Self;

    fn mul(mut self, rhs: Self) -> Self::Output {
        let lhs = self;
        self.scale_x = lhs.scale_x * rhs.scale_x + lhs.skew_x * rhs.skew_y;
        self.skew_y = lhs.skew_y * rhs.scale_x + lhs.scale_y * rhs.skew_y;
        self.skew_x = lhs.scale_x * rhs.skew_x + lhs.skew_x * rhs.scale_y;
        self.scale_y = lhs.skew_y * rhs.skew_x + lhs.scale_y * rhs.scale_y;
        self.translation_x =
            lhs.scale_x * rhs.translation_x + lhs.skew_x * rhs.translation_y + lhs.translation_x;
        self.translation_y =
            lhs.skew_y * rhs.translation_x + lhs.scale_y * rhs.translation_y + lhs.translation_y;
        self
    }
}

impl std::ops::MulAssign for Transform {
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

impl Into<mint::ColumnMatrix3<f32>> for Transform {
    fn into(self) -> mint::ColumnMatrix3<f32> {
        mint::ColumnMatrix3 {
            x: mint::Vector3 {
                x: self.scale_x,
                y: self.skew_y,
                z: 0.,
            },
            y: mint::Vector3 {
                x: self.skew_x,
                y: self.scale_y,
                z: 0.,
            },
            z: mint::Vector3 {
                x: self.translation_x,
                y: self.translation_y,
                z: 1.,
            },
        }
    }
}

impl Into<mint::ColumnMatrix4<f32>> for Transform {
    fn into(self) -> mint::ColumnMatrix4<f32> {
        mint::ColumnMatrix4 {
            x: mint::Vector4 {
                x: self.scale_x,
                y: self.skew_y,
                z: 0.,
                w: 0.,
            },
            y: mint::Vector4 {
                x: self.scale_y,
                y: self.skew_x,
                z: 0.,
                w: 0.,
            },
            z: mint::Vector4 {
                x: 0.,
                y: 0.,
                z: 1.,
                w: 0.,
            },
            w: mint::Vector4 {
                x: self.translation_x,
                y: self.translation_y,
                z: 0.,
                w: 1.,
            },
        }
    }
}

#[derive(Default)]
pub struct Transforms {
    base: Transform,
    stack: Vec<Transform>,
}

impl Transforms {
    pub fn current(&self) -> &Transform {
        self.stack.last().unwrap_or(&self.base)
    }

    pub fn current_mut(&mut self) -> &mut Transform {
        self.stack.last_mut().unwrap_or(&mut self.base)
    }

    pub fn push(&mut self) -> &mut Transform {
        self.stack.push(*self.current());
        self.current_mut()
    }

    pub fn pop(&mut self) -> Option<Transform> {
        self.stack.pop()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    pub fn transform_point_identity() {
        let identity = Transform::default();

        let (px, py) = (0., 0.);
        assert_eq!(identity.transform_point(px, py), (px, py));

        let (px, py) = (100., 0.);
        assert_eq!(identity.transform_point(px, py), (px, py));

        let (px, py) = (-213., 123.);
        assert_eq!(identity.transform_point(px, py), (px, py));
    }

    #[test]
    pub fn transform_point_translation() {
        let identity = Transform {
            translation_x: 2.,
            translation_y: 1.5,
            ..Transform::default()
        };

        let (px, py) = (0., 0.);
        assert_eq!(identity.transform_point(px, py), (2., 1.5));
    }

    #[test]
    pub fn transform_point_rotation() {
        use approx::*;

        let transform = Transform::rotation(crate::Deg(90.));
        assert_abs_diff_eq!(transform.scale_x, 0.);
        assert_abs_diff_eq!(transform.scale_y, 0.);
        assert_abs_diff_eq!(transform.skew_x, 1.);
        assert_abs_diff_eq!(transform.skew_y, -1.);

        let (px, py) = (0., 0.);
        assert_eq!(transform.transform_point(px, py), (px, py));

        let (px, py) = (1., 0.);
        let (tx, ty) = transform.transform_point(px, py);
        assert_abs_diff_eq!(tx, 0.);
        assert_abs_diff_eq!(ty, -1.);

        let (tx, ty) = transform.transform_point(tx, ty);
        assert_abs_diff_eq!(tx, -px);
        assert_abs_diff_eq!(ty, -py);

        let (px, py) = (2., 2.);
        let (tx, ty) = transform.transform_point(px, py);
        assert_abs_diff_eq!(tx, 2., epsilon = 0.001);
        assert_abs_diff_eq!(ty, -2., epsilon = 0.001);

        let (tx, ty) = transform.transform_point(tx, ty);
        assert_abs_diff_eq!(tx, -2., epsilon = 0.001);
        assert_abs_diff_eq!(ty, -2., epsilon = 0.001);

        let (tx, ty) = transform.transform_point(tx, ty);
        assert_abs_diff_eq!(tx, -2., epsilon = 0.001);
        assert_abs_diff_eq!(ty, 2., epsilon = 0.001);

        let (tx, ty) = transform.transform_point(tx, ty);
        assert_abs_diff_eq!(tx, 2., epsilon = 0.001);
        assert_abs_diff_eq!(ty, 2., epsilon = 0.001);
    }

    #[test]
    pub fn transform_point_scale() {
        let identity = Transform {
            scale_x: 2.,
            scale_y: 1.5,
            ..Transform::default()
        };

        let (px, py) = (0., 0.);
        assert_eq!(identity.transform_point(px, py), (px, py));

        let (px, py) = (100., 0.);
        assert_eq!(identity.transform_point(px, py), (200., 0.));

        let (px, py) = (-213., 123.);
        assert_eq!(identity.transform_point(px, py), (px * 2., py * 1.5));
    }

    #[test]
    pub fn transform_point() {
        let identity = Transform {
            translation_x: 100.,
            translation_y: 200.,
            scale_x: 2.,
            scale_y: 1.5,
            ..Transform::default()
        };

        let (px, py) = (0., 0.);
        assert_eq!(identity.transform_point(px, py), (100., 200.));

        let (px, py) = (1., 2.);
        assert_eq!(identity.transform_point(px, py), (102., 203.));
    }
}