Skip to main content

valo_geometry/
matrix.rs

1use crate::{Point, Rect};
2
3/// `Matrix` is a full 4×4 column-major transform.
4///
5/// Valo maps two-dimensional input as `(x, y, 0, 1)`, including perspective
6/// division. The renderer ignores transformed z for draw ordering.
7#[derive(Clone, Copy, Debug, PartialEq)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9#[cfg_attr(feature = "serde", serde(transparent))]
10pub struct Matrix(glam::Mat4);
11
12/// `MatrixKind` classifies the behavior relevant to two-dimensional rendering.
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14pub enum MatrixKind {
15    /// `AxisAligned` contains only positive scale and translation.
16    AxisAligned,
17    /// `Affine` includes rotation, shear, or reflection without perspective.
18    Affine,
19    /// `General` includes perspective.
20    General,
21}
22
23/// `W_EPSILON` keeps points at the eye plane from producing unbounded values.
24const W_EPSILON: f32 = 1e-6;
25
26impl Matrix {
27    /// `IDENTITY` leaves coordinates unchanged.
28    pub const IDENTITY: Matrix = Matrix(glam::Mat4::IDENTITY);
29
30    /// `translation` creates a two-dimensional translation.
31    pub fn translation(tx: f32, ty: f32) -> Self {
32        Matrix(glam::Mat4::from_translation(glam::Vec3::new(tx, ty, 0.0)))
33    }
34
35    /// `scale` creates a two-dimensional scale.
36    pub fn scale(sx: f32, sy: f32) -> Self {
37        Matrix(glam::Mat4::from_scale(glam::Vec3::new(sx, sy, 1.0)))
38    }
39
40    /// `rotation` creates a rotation around the origin.
41    ///
42    /// Positive angles rotate clockwise in Valo's y-down coordinate system.
43    pub fn rotation(radians: f32) -> Self {
44        Matrix(glam::Mat4::from_rotation_z(radians))
45    }
46
47    /// `from_affine` creates a matrix from `[a, b, c, d, tx, ty]`.
48    ///
49    /// The linear columns are `(a, b)` and `(c, d)`.
50    pub fn from_affine(a: f32, b: f32, c: f32, d: f32, tx: f32, ty: f32) -> Self {
51        Matrix(glam::Mat4::from_cols_array(&[
52            a, b, 0.0, 0.0, //
53            c, d, 0.0, 0.0, //
54            0.0, 0.0, 1.0, 0.0, //
55            tx, ty, 0.0, 1.0,
56        ]))
57    }
58
59    /// `from_flutter_array` creates a matrix from 16 column-major Flutter values.
60    pub fn from_flutter_array(values: &[f32; 16]) -> Self {
61        Matrix(glam::Mat4::from_cols_array(values))
62    }
63
64    /// `to_flutter_array` returns 16 column-major Flutter values.
65    pub fn to_flutter_array(&self) -> [f32; 16] {
66        self.0.to_cols_array()
67    }
68
69    /// `to_mat4` returns the backing glam matrix.
70    pub fn to_mat4(self) -> glam::Mat4 {
71        self.0
72    }
73
74    /// `then` composes this matrix with `other`.
75    ///
76    /// The result applies `other` first and this matrix second.
77    pub fn then(&self, other: &Matrix) -> Matrix {
78        Matrix(self.0 * other.0)
79    }
80
81    /// `is_affine` reports whether two-dimensional input has no perspective.
82    pub fn is_affine(&self) -> bool {
83        let m = &self.0;
84        m.x_axis.w == 0.0 && m.y_axis.w == 0.0 && m.w_axis.w == 1.0
85    }
86
87    /// `kind` classifies this matrix for two-dimensional rendering.
88    pub fn kind(&self) -> MatrixKind {
89        if !self.is_affine() {
90            return MatrixKind::General;
91        }
92        let m = &self.0;
93        let axis_aligned =
94            m.x_axis.y == 0.0 && m.y_axis.x == 0.0 && m.x_axis.x > 0.0 && m.y_axis.y > 0.0;
95        if axis_aligned {
96            MatrixKind::AxisAligned
97        } else {
98            MatrixKind::Affine
99        }
100    }
101
102    /// `rect_stays_rect` reports whether this matrix maps every rectangle
103    /// onto a rectangle: Skia's `SkMatrix::rectStaysRect`, no perspective
104    /// and a two-dimensional block that scales each axis by a nonzero amount,
105    /// mirrored or not, or turns it a quarter onto the other.
106    pub(crate) fn rect_stays_rect(&self) -> bool {
107        if !self.is_affine() {
108            return false;
109        }
110        let [a, b, c, d, ..] = self.to_affine();
111        let scales = b == 0.0 && c == 0.0 && a != 0.0 && d != 0.0;
112        let turns = a == 0.0 && d == 0.0 && b != 0.0 && c != 0.0;
113        scales || turns
114    }
115
116    /// `map_point` transforms a point and applies perspective division.
117    ///
118    /// Points at or behind the eye plane use a small positive divisor.
119    pub fn map_point(&self, p: Point) -> Point {
120        let v = self.0 * glam::Vec4::new(p.x, p.y, 0.0, 1.0);
121        let w = if v.w > W_EPSILON { v.w } else { W_EPSILON };
122        Point::new(v.x / w, v.y / w)
123    }
124
125    /// `map_rect` returns axis-aligned bounds around a transformed rectangle.
126    ///
127    /// It returns [`Rect::EVERYTHING`] when a corner reaches or crosses the
128    /// eye plane and finite conservative bounds cannot be proven.
129    pub fn map_rect(&self, r: &Rect) -> Rect {
130        let (mut left, mut top) = (f32::MAX, f32::MAX);
131        let (mut right, mut bottom) = (f32::MIN, f32::MIN);
132        for corner in r.corners() {
133            let v = self.0 * glam::Vec4::new(corner.x, corner.y, 0.0, 1.0);
134            if v.w <= W_EPSILON {
135                return Rect::EVERYTHING;
136            }
137            let (x, y) = (v.x / v.w, v.y / v.w);
138            left = left.min(x);
139            top = top.min(y);
140            right = right.max(x);
141            bottom = bottom.max(y);
142        }
143        Rect::from_ltrb(left, top, right, bottom)
144    }
145
146    /// `max_scale` returns the larger length of the transformed x and y basis vectors.
147    ///
148    /// It ignores perspective and is therefore approximate for general matrices.
149    pub fn max_scale(&self) -> f32 {
150        let m = &self.0;
151        let sx = (m.x_axis.x * m.x_axis.x + m.x_axis.y * m.x_axis.y).sqrt();
152        let sy = (m.y_axis.x * m.y_axis.x + m.y_axis.y * m.y_axis.y).sqrt();
153        sx.max(sy)
154    }
155
156    /// `to_affine` returns `[a, b, c, d, tx, ty]` from the two-dimensional block.
157    ///
158    /// Perspective components are omitted; check [`Self::is_affine`] when
159    /// exact conversion is required.
160    pub fn to_affine(&self) -> [f32; 6] {
161        let m = &self.0;
162        [
163            m.x_axis.x, m.x_axis.y, m.y_axis.x, m.y_axis.y, m.w_axis.x, m.w_axis.y,
164        ]
165    }
166
167    /// `determinant` returns the signed area scale of the two-dimensional block.
168    pub fn determinant(&self) -> f32 {
169        let m = &self.0;
170        m.x_axis.x * m.y_axis.y - m.x_axis.y * m.y_axis.x
171    }
172
173    /// `invert` returns the inverse matrix or `None` when no finite inverse exists.
174    pub fn invert(&self) -> Option<Matrix> {
175        let det = self.0.determinant();
176        if det == 0.0 || !det.is_finite() {
177            return None;
178        }
179        let inverse = self.0.inverse();
180        inverse.is_finite().then_some(Matrix(inverse))
181    }
182}
183
184impl Default for Matrix {
185    fn default() -> Self {
186        Self::IDENTITY
187    }
188}
189
190#[cfg(test)]
191mod tests {
192    use super::*;
193
194    fn close(a: Point, b: Point) -> bool {
195        (a.x - b.x).abs() < 1e-4 && (a.y - b.y).abs() < 1e-4
196    }
197
198    #[test]
199    fn near_singular_matrices_invert_to_none() {
200        assert!(Matrix::scale(1e-20, 1e-20).invert().is_none());
201        assert!(Matrix::scale(0.0, 1.0).invert().is_none());
202    }
203
204    #[test]
205    fn then_applies_local_first() {
206        // translate then scale: point scales, THEN translates (current ∘ local).
207        let t = Matrix::translation(10.0, 0.0).then(&Matrix::scale(2.0, 2.0));
208        assert!(close(
209            t.map_point(Point::new(1.0, 1.0)),
210            Point::new(12.0, 2.0)
211        ));
212    }
213
214    #[test]
215    fn rotation_quarter_turn() {
216        let t = Matrix::rotation(std::f32::consts::FRAC_PI_2);
217        // y-down: (1,0) rotates clockwise to (0,1).
218        assert!(close(
219            t.map_point(Point::new(1.0, 0.0)),
220            Point::new(0.0, 1.0)
221        ));
222    }
223
224    #[test]
225    fn invert_roundtrip() {
226        let t = Matrix::translation(5.0, -3.0)
227            .then(&Matrix::rotation(0.7))
228            .then(&Matrix::scale(2.0, 0.5));
229        let inv = t.invert().unwrap();
230        let p = Point::new(3.0, 4.0);
231        assert!(close(inv.map_point(t.map_point(p)), p));
232    }
233
234    #[test]
235    fn map_rect_rotation_is_conservative_bounds() {
236        let t = Matrix::rotation(std::f32::consts::FRAC_PI_4);
237        let r = t.map_rect(&Rect::new(-1.0, -1.0, 2.0, 2.0));
238        let d = 2.0_f32.sqrt();
239        assert!((r.width - 2.0 * d).abs() < 1e-4 && (r.height - 2.0 * d).abs() < 1e-4);
240    }
241
242    #[test]
243    fn perspective_divides_by_w() {
244        // Flutter's classic card tilt: entry[3][2] bends z into w — for 2D
245        // content that only matters through concatenation (below); a raw
246        // w-row on x makes near points larger than far ones.
247        let mut values = Matrix::IDENTITY.to_flutter_array();
248        values[3] = 0.001; // w += 0.001 · x
249        let t = Matrix::from_flutter_array(&values);
250        assert!(close(
251            t.map_point(Point::new(100.0, 100.0)),
252            Point::new(100.0 / 1.1, 100.0 / 1.1)
253        ));
254        assert_eq!(t.kind(), MatrixKind::General);
255    }
256
257    #[test]
258    fn concatenation_stays_four_by_four() {
259        // tilt ∘ translate ∘ tilt: the sequence Flutter's Transform widgets
260        // produce. Slicing each factor to its 2D action BEFORE multiplying
261        // loses the z column the middle translation feeds into the outer
262        // tilt's w row — full 4×4 concatenation keeps it.
263        let mut tilt_values = Matrix::IDENTITY.to_flutter_array();
264        tilt_values[11] = 0.001; // w += 0.001 · z (the Flutter entry(3,2))
265        let tilt = Matrix::from_flutter_array(&tilt_values);
266        let mut rotate_x = Matrix::IDENTITY.to_flutter_array();
267        // rotateX(0.5): y/z plane rotation — feeds y into z.
268        let (sin, cos) = 0.5_f32.sin_cos();
269        rotate_x[5] = cos;
270        rotate_x[6] = sin;
271        rotate_x[9] = -sin;
272        rotate_x[10] = cos;
273        let full = tilt.then(&Matrix::from_flutter_array(&rotate_x));
274        // The composed matrix must carry perspective from y (via z).
275        assert_eq!(full.kind(), MatrixKind::General);
276        let p = full.map_point(Point::new(0.0, 100.0));
277        // y rotated toward the viewer shrinks: 100·cos / (1 + 0.001·100·sin).
278        let expected_y = 100.0 * cos / (1.0 + 0.001 * 100.0 * sin);
279        assert!((p.y - expected_y).abs() < 1e-2, "{} vs {expected_y}", p.y);
280    }
281
282    #[test]
283    fn eye_plane_bounds_are_everything() {
284        let mut values = Matrix::IDENTITY.to_flutter_array();
285        values[3] = -0.1; // w = 1 - 0.1 · x → w ≤ 0 from x = 10 on
286        let t = Matrix::from_flutter_array(&values);
287        assert_eq!(
288            t.map_rect(&Rect::new(0.0, 0.0, 100.0, 10.0)),
289            Rect::EVERYTHING
290        );
291    }
292}