#![cfg_attr(test, allow(clippy::unwrap_used, clippy::expect_used))]
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Point {
pub x: f64,
pub y: f64,
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Rect {
pub x0: f64,
pub y0: f64,
pub x1: f64,
pub y1: f64,
}
impl Rect {
pub fn width(&self) -> f64 {
self.x1 - self.x0
}
pub fn height(&self) -> f64 {
self.y1 - self.y0
}
pub fn normalized(self) -> Rect {
Rect {
x0: self.x0.min(self.x1),
y0: self.y0.min(self.y1),
x1: self.x0.max(self.x1),
y1: self.y0.max(self.y1),
}
}
pub fn union(self, other: Rect) -> Rect {
Rect {
x0: self.x0.min(other.x0),
y0: self.y0.min(other.y0),
x1: self.x1.max(other.x1),
y1: self.y1.max(other.y1),
}
}
pub fn intersect(self, other: Rect) -> Option<Rect> {
let r = Rect {
x0: self.x0.max(other.x0),
y0: self.y0.max(other.y0),
x1: self.x1.min(other.x1),
y1: self.y1.min(other.y1),
};
(r.x0 <= r.x1 && r.y0 <= r.y1).then_some(r)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Matrix {
pub a: f64,
pub b: f64,
pub c: f64,
pub d: f64,
pub e: f64,
pub f: f64,
}
impl Matrix {
pub const IDENTITY: Matrix = Matrix {
a: 1.0,
b: 0.0,
c: 0.0,
d: 1.0,
e: 0.0,
f: 0.0,
};
pub fn translate(tx: f64, ty: f64) -> Matrix {
Matrix {
e: tx,
f: ty,
..Matrix::IDENTITY
}
}
pub fn scale(sx: f64, sy: f64) -> Matrix {
Matrix {
a: sx,
d: sy,
..Matrix::IDENTITY
}
}
pub fn then(self, other: Matrix) -> Matrix {
Matrix {
a: self.a * other.a + self.b * other.c,
b: self.a * other.b + self.b * other.d,
c: self.c * other.a + self.d * other.c,
d: self.c * other.b + self.d * other.d,
e: self.e * other.a + self.f * other.c + other.e,
f: self.e * other.b + self.f * other.d + other.f,
}
}
pub fn apply(&self, p: Point) -> Point {
Point {
x: self.a * p.x + self.c * p.y + self.e,
y: self.b * p.x + self.d * p.y + self.f,
}
}
pub fn transform_rect(&self, rect: Rect) -> Rect {
let corners = [
Point {
x: rect.x0,
y: rect.y0,
},
Point {
x: rect.x1,
y: rect.y0,
},
Point {
x: rect.x1,
y: rect.y1,
},
Point {
x: rect.x0,
y: rect.y1,
},
]
.map(|corner| self.apply(corner));
let mut result = Rect {
x0: corners[0].x,
y0: corners[0].y,
x1: corners[0].x,
y1: corners[0].y,
};
for point in &corners[1..] {
result.x0 = result.x0.min(point.x);
result.y0 = result.y0.min(point.y);
result.x1 = result.x1.max(point.x);
result.y1 = result.y1.max(point.y);
}
result
}
pub fn determinant(&self) -> f64 {
self.a * self.d - self.b * self.c
}
pub fn is_finite(&self) -> bool {
self.a.is_finite()
&& self.b.is_finite()
&& self.c.is_finite()
&& self.d.is_finite()
&& self.e.is_finite()
&& self.f.is_finite()
}
pub fn is_stably_invertible(&self) -> bool {
if !self.is_finite() {
return false;
}
let scale = self
.a
.abs()
.max(self.b.abs())
.max(self.c.abs())
.max(self.d.abs());
if scale == 0.0 || !scale.is_finite() {
return false;
}
self.determinant().abs() > 64.0 * f64::EPSILON * scale * scale
}
pub fn invert(&self) -> Option<Matrix> {
if !self.is_finite() {
return None;
}
let det = self.determinant();
if det == 0.0 || !det.is_finite() {
return None;
}
let inv = 1.0 / det;
let a = self.d * inv;
let b = -self.b * inv;
let c = -self.c * inv;
let d = self.a * inv;
Some(Matrix {
a,
b,
c,
d,
e: -(self.e * a + self.f * c),
f: -(self.e * b + self.f * d),
})
}
}
impl Default for Matrix {
fn default() -> Self {
Matrix::IDENTITY
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DeviceSize {
pub width: u32,
pub height: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DeviceRect {
pub x: i32,
pub y: i32,
pub width: u32,
pub height: u32,
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
#[test]
fn matrix_compose_and_apply() {
let m = Matrix::scale(2.0, 3.0).then(Matrix::translate(10.0, 20.0));
let p = m.apply(Point { x: 1.0, y: 1.0 });
assert_eq!(p, Point { x: 12.0, y: 23.0 });
assert_eq!(m.determinant(), 6.0);
}
#[test]
fn transform_rect_bounds_rotated_and_flipped_corners() {
let rect = Rect {
x0: 0.0,
y0: 0.0,
x1: 2.0,
y1: 1.0,
};
let rotate = Matrix {
a: 0.0,
b: 1.0,
c: -1.0,
d: 0.0,
e: 5.0,
f: 0.0,
};
assert_eq!(
rotate.transform_rect(rect),
Rect {
x0: 4.0,
y0: 0.0,
x1: 5.0,
y1: 2.0,
}
);
assert_eq!(
Matrix::scale(1.0, -1.0).transform_rect(rect),
Rect {
x0: 0.0,
y0: -1.0,
x1: 2.0,
y1: 0.0,
}
);
}
#[test]
fn matrix_invert_roundtrips() {
let m = Matrix {
a: 2.0,
b: 0.5,
c: -1.0,
d: 3.0,
e: 10.0,
f: -4.0,
};
let inv = m.invert().unwrap();
let p = Point { x: 7.0, y: -2.0 };
let back = inv.apply(m.apply(p));
assert!((back.x - p.x).abs() < 1e-9 && (back.y - p.y).abs() < 1e-9);
assert!(
Matrix {
a: 1.0,
b: 2.0,
c: 2.0,
d: 4.0,
e: 0.0,
f: 0.0
}
.invert()
.is_none()
);
}
#[test]
fn small_but_valid_matrices_stay_invertible() {
let m = Matrix::scale(0.001, 0.001).then(Matrix::scale(0.001, 0.001));
assert_eq!(m.determinant(), 1e-12);
let inv = m.invert().expect("a pure scale is always invertible");
let p = inv.apply(m.apply(Point { x: 3.0, y: -5.0 }));
assert!((p.x - 3.0).abs() < 1e-9 && (p.y + 5.0).abs() < 1e-9);
assert!(
m.is_stably_invertible(),
"a pure scale is well-conditioned at any size"
);
}
#[test]
fn stability_is_scale_relative_not_absolute() {
let ill = Matrix {
a: 1e8,
b: 1e8,
c: 1e8,
d: 1e8 * (1.0 + 1e-15),
e: 0.0,
f: 0.0,
};
assert!(
ill.determinant().abs() > 1e-12,
"sails past a fixed epsilon"
);
assert!(
!ill.is_stably_invertible(),
"but must be rejected as ill-conditioned"
);
let big = Matrix {
a: 1e8,
b: 0.0,
c: 0.0,
d: 1e8,
e: 0.0,
f: 0.0,
};
assert!(
big.is_stably_invertible(),
"a big pure scale is well-conditioned"
);
}
#[test]
fn non_finite_matrices_are_rejected() {
for bad in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
let m = Matrix {
a: bad,
..Matrix::IDENTITY
};
assert!(!m.is_finite());
assert!(m.invert().is_none(), "{bad} matrix must not invert");
assert!(!m.is_stably_invertible());
}
let nan_det = Matrix {
a: f64::NAN,
d: f64::NAN,
..Matrix::IDENTITY
};
let old_guard_would_fire = nan_det.determinant().abs() < 1e-12;
assert!(
!old_guard_would_fire,
"every NaN comparison is false, so the old guard passed it"
);
assert!(
nan_det.invert().is_none(),
"the finiteness check catches what magnitude cannot"
);
}
#[test]
fn singular_matrices_have_no_inverse() {
assert!(Matrix::scale(0.0, 1.0).invert().is_none());
assert!(
Matrix {
a: 1.0,
b: 2.0,
c: 2.0,
d: 4.0,
e: 0.0,
f: 0.0
}
.invert()
.is_none()
);
}
#[test]
fn rect_ops() {
let r = Rect {
x0: 10.0,
y0: 10.0,
x1: 0.0,
y1: 0.0,
}
.normalized();
assert_eq!(r.width(), 10.0);
let other = Rect {
x0: 5.0,
y0: 5.0,
x1: 20.0,
y1: 20.0,
};
assert_eq!(
r.intersect(other),
Some(Rect {
x0: 5.0,
y0: 5.0,
x1: 10.0,
y1: 10.0
})
);
assert!(
r.intersect(Rect {
x0: 11.0,
y0: 11.0,
x1: 12.0,
y1: 12.0
})
.is_none()
);
}
}