spright/transform.rs
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
/// A 3x2 transformation matrix representing an affine transform.
///
/// In other words, it is a 2x2 transformation matrix with a translation component, or a 3x3 homogenous transform matrix.
#[derive(Clone, Copy, Debug)]
pub struct Transform([f32; 6]);
impl Default for Transform {
fn default() -> Self {
Self::IDENTITY
}
}
impl Transform {
// Identity matrix.
pub const IDENTITY: Self = Self([
1.0, 0.0, //
0.0, 1.0, //
0.0, 0.0,
]);
/// Creates a matrix from each individual element.
///
/// The matrix is in column-major order, that is:
///
/// $$
/// \begin{bmatrix}
/// \texttt{m00} & \texttt{m10} & \texttt{tx} \\\\
/// \texttt{m01} & \texttt{m11} & \texttt{ty} \\\\
/// 0 & 0 & 1
/// \end{bmatrix}
/// $$
pub const fn new(m00: f32, m01: f32, m10: f32, m11: f32, tx: f32, ty: f32) -> Self {
Self([
m00, m01, //
m10, m11, //
tx, ty,
])
}
/// Creates a transform that performs a translation.
///
/// $$
/// \begin{bmatrix}
/// 1 & 0 & \texttt{tx} \\\\
/// 0 & 1 & \texttt{ty} \\\\
/// 0 & 0 & 1
/// \end{bmatrix}
/// $$
pub const fn translation(tx: f32, ty: f32) -> Self {
Self([
1.0, 0.0, //
0.0, 1.0, //
tx, ty,
])
}
/// Creates a transform that performs scaling.
///
/// $$
/// \begin{bmatrix}
/// \texttt{sx} & 0 & 0 \\\\
/// 0 & \texttt{sy} & 0 \\\\
/// 0 & 0 & 1
/// \end{bmatrix}
/// $$
pub const fn scaling(sx: f32, sy: f32) -> Self {
Self([
sx, 0.0, //
0.0, sy, //
0.0, 0.0,
])
}
/// Creates a transform that performs a rotation.
///
/// $$
/// \begin{bmatrix}
/// \text{cos}\ \theta & -\text{sin}\ \theta & 0 \\\\
/// \text{sin}\ \theta & \text{cos}\ \theta & 0 \\\\
/// 0 & 0 & 1
/// \end{bmatrix}
/// $$
pub fn rotation(theta: f32) -> Self {
let c = theta.cos();
let s = theta.sin();
Self([
c, s, //
-s, c, //
0.0, 0.0,
])
}
/// Computes the determinant of the matrix.
pub const fn determinant(&self) -> f32 {
self.0[0] * self.0[3] - self.0[1] * self.0[2]
}
/// Computes the inverse of the matrix.
///
/// If the matrix is degenerate (that is, the determinant is zero), returns [`None`].
pub const fn inverse(&self) -> Option<Self> {
let det = self.determinant();
if det == 0.0 {
return None;
}
Some(Self([
self.0[3] / det,
-self.0[1] / det,
-self.0[2] / det,
self.0[0] / det,
(self.0[1] * self.0[5] - self.0[3] * self.0[4]) / det,
(self.0[4] * self.0[2] - self.0[0] * self.0[5]) / det,
]))
}
/// Transforms a point by the matrix.
pub const fn transform(&self, x: f32, y: f32) -> (f32, f32) {
(
x * self.0[0] + y * self.0[2] + self.0[4],
x * self.0[1] + y * self.0[3] + self.0[5],
)
}
}
impl std::ops::MulAssign<Transform> for Transform {
fn mul_assign(&mut self, rhs: Transform) {
self.0 = [
self.0[0] * rhs.0[0] + self.0[1] * rhs.0[2],
self.0[0] * rhs.0[1] + self.0[1] * rhs.0[3],
self.0[2] * rhs.0[0] + self.0[3] * rhs.0[2],
self.0[2] * rhs.0[1] + self.0[3] * rhs.0[3],
self.0[4] * rhs.0[0] + self.0[5] * rhs.0[2] + rhs.0[4],
self.0[4] * rhs.0[1] + self.0[5] * rhs.0[3] + rhs.0[5],
];
}
}
impl std::ops::Mul<Transform> for Transform {
type Output = Transform;
fn mul(mut self, rhs: Transform) -> Self::Output {
self *= rhs;
self
}
}