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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// pathfinder/geometry/src/basic/transform2d.rs
//
// Copyright © 2019 The Pathfinder Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! 2D affine transforms.

use crate::line_segment::LineSegment2F;
use crate::rect::RectF;
use crate::transform3d::Transform4F;
use crate::unit_vector::UnitVector;
use crate::vector::{IntoVector2F, Vector2F, vec2f};
use pathfinder_simd::default::F32x4;
use std::ops::{Mul, MulAssign, Sub};

/// A 2x2 matrix, optimized with SIMD, in column-major order.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Matrix2x2F(pub F32x4);

impl Default for Matrix2x2F {
    #[inline]
    fn default() -> Matrix2x2F {
        Self::from_scale(1.0)
    }
}

impl Matrix2x2F {
    #[inline]
    pub fn from_scale<S>(scale: S) -> Matrix2x2F where S: IntoVector2F {
        let scale = scale.into_vector_2f();
        Matrix2x2F(F32x4::new(scale.x(), 0.0, 0.0, scale.y()))
    }

    #[inline]
    pub fn from_rotation(theta: f32) -> Matrix2x2F {
        Matrix2x2F::from_rotation_vector(UnitVector::from_angle(theta))
    }

    #[inline]
    pub fn from_rotation_vector(vector: UnitVector) -> Matrix2x2F {
        Matrix2x2F((vector.0).0.to_f32x4().xyyx() * F32x4::new(1.0, 1.0, -1.0, 1.0))
    }

    #[inline]
    pub fn row_major(m00: f32, m01: f32, m10: f32, m11: f32) -> Matrix2x2F {
        Matrix2x2F(F32x4::new(m00, m10, m01, m11))
    }

    #[inline]
    pub fn entrywise_mul(&self, other: &Matrix2x2F) -> Matrix2x2F {
        Matrix2x2F(self.0 * other.0)
    }

    #[inline]
    pub fn adjugate(&self) -> Matrix2x2F {
        Matrix2x2F(self.0.wyzx() * F32x4::new(1.0, -1.0, -1.0, 1.0))
    }

    #[inline]
    pub fn det(&self) -> f32 {
        self.0[0] * self.0[3] - self.0[2] * self.0[1]
    }

    #[inline]
    pub fn inverse(&self) -> Matrix2x2F {
        Matrix2x2F(F32x4::splat(1.0 / self.det()) * self.adjugate().0)
    }

    #[inline]
    pub fn scale(&self, factor: f32) -> Matrix2x2F {
        Matrix2x2F(self.0 * F32x4::splat(factor))
    }

    /// Extracts the scale from this matrix.
    #[inline]
    pub fn extract_scale(&self) -> Vector2F {
        let squared = self.0 * self.0;
        Vector2F((squared.xy() + squared.zw()).sqrt())
    }

    #[inline]
    pub fn m11(&self) -> f32 {
        self.0[0]
    }

    #[inline]
    pub fn m21(&self) -> f32 {
        self.0[1]
    }

    #[inline]
    pub fn m12(&self) -> f32 {
        self.0[2]
    }

    #[inline]
    pub fn m22(&self) -> f32 {
        self.0[3]
    }
}

impl Sub<Matrix2x2F> for Matrix2x2F {
    type Output = Matrix2x2F;
    #[inline]
    fn sub(self, other: Matrix2x2F) -> Matrix2x2F {
        Matrix2x2F(self.0 - other.0)
    }
}

impl Mul<Matrix2x2F> for Matrix2x2F {
    type Output = Matrix2x2F;
    #[inline]
    fn mul(self, other: Matrix2x2F) -> Matrix2x2F {
        Matrix2x2F(self.0.xyxy() * other.0.xxzz() + self.0.zwzw() * other.0.yyww())
    }
}

impl Mul<Vector2F> for Matrix2x2F {
    type Output = Vector2F;
    #[inline]
    fn mul(self, vector: Vector2F) -> Vector2F {
        let halves = self.0 * vector.0.to_f32x4().xxyy();
        Vector2F(halves.xy() + halves.zw())
    }
}

/// An affine transform, optimized with SIMD.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Transform2F {
    pub matrix: Matrix2x2F,
    pub vector: Vector2F,
}

impl Default for Transform2F {
    #[inline]
    fn default() -> Transform2F {
        Self::from_scale(vec2f(1.0, 1.0))
    }
}

impl Transform2F {
    #[inline]
    pub fn from_scale<S>(scale: S) -> Transform2F where S: IntoVector2F {
        let scale = scale.into_vector_2f();
        Transform2F {
            matrix: Matrix2x2F::from_scale(scale),
            vector: Vector2F::zero(),
        }
    }

    #[inline]
    pub fn from_rotation(theta: f32) -> Transform2F {
        Transform2F {
            matrix: Matrix2x2F::from_rotation(theta),
            vector: Vector2F::zero(),
        }
    }

    #[inline]
    pub fn from_rotation_vector(vector: UnitVector) -> Transform2F {
        Transform2F {
            matrix: Matrix2x2F::from_rotation_vector(vector),
            vector: Vector2F::zero(),
        }
    }

    #[inline]
    pub fn from_translation(vector: Vector2F) -> Transform2F {
        Transform2F { matrix: Matrix2x2F::default(), vector }
    }

    #[inline]
    pub fn from_scale_rotation_translation<S>(scale: S, theta: f32, translation: Vector2F)
                                              -> Transform2F where S: IntoVector2F {
        let scale = scale.into_vector_2f();
        let rotation = Transform2F::from_rotation(theta);
        let translation = Transform2F::from_translation(translation);
        Transform2F::from_scale(scale) * rotation * translation
    }

    #[inline]
    pub fn row_major(m11: f32, m12: f32, m21: f32, m22: f32, m31: f32, m32: f32) -> Transform2F {
        Transform2F {
            matrix: Matrix2x2F::row_major(m11, m12, m21, m22),
            vector: Vector2F::new(m31, m32),
        }
    }

    // TODO(pcwalton): Optimize better with SIMD.
    #[inline]
    pub fn to_3d(&self) -> Transform4F {
        Transform4F::row_major(
            self.matrix.0[0],
            self.matrix.0[1],
            0.0,
            self.vector.x(),
            self.matrix.0[2],
            self.matrix.0[3],
            0.0,
            self.vector.y(),
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            1.0,
        )
    }

    #[inline]
    pub fn is_identity(&self) -> bool {
        *self == Transform2F::default()
    }

    /// Extracts the scale from this matrix.
    #[inline]
    pub fn extract_scale(&self) -> Vector2F {
        self.matrix.extract_scale()
    }

    #[inline]
    pub fn m11(&self) -> f32 {
        self.matrix.m11()
    }
    #[inline]
    pub fn m21(&self) -> f32 {
        self.matrix.m21()
    }
    #[inline]
    pub fn m12(&self) -> f32 {
        self.matrix.m12()
    }
    #[inline]
    pub fn m22(&self) -> f32 {
        self.matrix.m22()
    }
    #[inline]
    pub fn m31(&self) -> f32 {
        self.vector.x()
    }
    #[inline]
    pub fn m32(&self) -> f32 {
        self.vector.y()
    }

    #[inline]
    pub fn translate(&self, vector: Vector2F) -> Transform2F {
        Transform2F::from_translation(vector) * *self
    }

    #[inline]
    pub fn rotate(&self, theta: f32) -> Transform2F {
        Transform2F::from_rotation(theta) * *self
    }

    #[inline]
    pub fn scale<S>(&self, scale: S) -> Transform2F where S: IntoVector2F {
        let scale = scale.into_vector_2f();
        Transform2F::from_scale(scale) * *self
    }

    /// Returns the translation part of this matrix.
    ///
    /// This decomposition assumes that scale, rotation, and translation are applied in that order.
    #[inline]
    pub fn translation(&self) -> Vector2F {
        self.vector
    }

    /// Returns the rotation angle of this matrix.
    ///
    /// This decomposition assumes that scale, rotation, and translation are applied in that order.
    #[inline]
    pub fn rotation(&self) -> f32 {
        f32::atan2(self.m21(), self.m11())
    }

    /// Returns the scale factor of this matrix.
    ///
    /// This decomposition assumes that scale, rotation, and translation are applied in that order.
    #[inline]
    pub fn scale_factor(&self) -> f32 {
        Vector2F(self.matrix.0.zw()).length()
    }

    #[inline]
    pub fn inverse(&self) -> Transform2F {
        let matrix_inv = self.matrix.inverse();
        let vector_inv = -(matrix_inv * self.vector);
        Transform2F { matrix: matrix_inv, vector: vector_inv }
    }
}

impl Mul<Transform2F> for Transform2F {
    type Output = Transform2F;
    #[inline]
    fn mul(self, other: Transform2F) -> Transform2F {
        Transform2F {
            matrix: self.matrix * other.matrix,
            vector: self * other.vector,
        }
    }
}

impl Mul<Vector2F> for Transform2F {
    type Output = Vector2F;
    #[inline]
    fn mul(self, vector: Vector2F) -> Vector2F {
        self.matrix * vector + self.vector
    }
}

impl Mul<LineSegment2F> for Transform2F {
    type Output = LineSegment2F;
    #[inline]
    fn mul(self, line_segment: LineSegment2F) -> LineSegment2F {
        LineSegment2F::new(self * line_segment.from(), self * line_segment.to())
    }
}

impl Mul<RectF> for Transform2F {
    type Output = RectF;
    #[inline]
    fn mul(self, rect: RectF) -> RectF {
        let (upper_left, upper_right) = (self * rect.origin(),     self * rect.upper_right());
        let (lower_left, lower_right) = (self * rect.lower_left(), self * rect.lower_right());
        let min_point = upper_left.min(upper_right).min(lower_left).min(lower_right);
        let max_point = upper_left.max(upper_right).max(lower_left).max(lower_right);
        RectF::from_points(min_point, max_point)
    }
}

impl MulAssign for Transform2F {
    #[inline]
    fn mul_assign(&mut self, other: Transform2F) {
        *self = *self * other
    }
}