From 732dee958e9708e9e09be4a75790d18f6f6f6a72 Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Thu, 14 Jul 2022 00:33:38 -0700 Subject: [PATCH] Remove Mat3x3 from math3d lib. It was an extraneous abstraction. --- sub_crates/math3d/src/lib.rs | 1 - sub_crates/math3d/src/mat3x3.rs | 83 ------------------------- sub_crates/math3d/src/transform.rs | 46 ++++++++++---- sub_crates/math3d/src/transform_dual.rs | 7 +-- sub_crates/math3d/src/wide4.rs | 25 ++++++++ 5 files changed, 61 insertions(+), 101 deletions(-) delete mode 100644 sub_crates/math3d/src/mat3x3.rs diff --git a/sub_crates/math3d/src/lib.rs b/sub_crates/math3d/src/lib.rs index 53ba427..6b8a785 100644 --- a/sub_crates/math3d/src/lib.rs +++ b/sub_crates/math3d/src/lib.rs @@ -1,6 +1,5 @@ #![allow(dead_code)] -pub mod mat3x3; pub mod normal; pub mod point; pub mod transform; diff --git a/sub_crates/math3d/src/mat3x3.rs b/sub_crates/math3d/src/mat3x3.rs deleted file mode 100644 index 0be112e..0000000 --- a/sub_crates/math3d/src/mat3x3.rs +++ /dev/null @@ -1,83 +0,0 @@ -use std::ops::{Add, Div, Mul}; - -use crate::wide4::f32x4; - -/// A 3x3 matrix. -/// -/// Internally this is actually 4x3 to take advantage of SIMD. -#[derive(Debug, Copy, Clone)] -#[repr(C)] -pub(crate) struct Mat3x3(pub(crate) [f32x4; 3]); - -impl Mat3x3 { - #[inline(always)] - pub fn new(a: f32x4, b: f32x4, c: f32x4) -> Self { - Self([a, b, c]) - } - - pub fn identity() -> Self { - Self([ - f32x4::new(1.0, 0.0, 0.0, 0.0), - f32x4::new(0.0, 1.0, 0.0, 0.0), - f32x4::new(0.0, 0.0, 1.0, 0.0), - ]) - } - - #[must_use] - #[inline] - pub fn inverse(self) -> Self { - todo!() - } - - #[must_use] - #[inline] - pub fn inverse_precise(self) -> Self { - todo!() - } - - #[must_use] - #[inline] - pub fn transpose(self) -> Self { - todo!() - } -} - -impl Add for Mat3x3 { - type Output = Self; - - #[inline(always)] - fn add(self, rhs: Self) -> Self { - Self([ - self.0[0] + rhs.0[0], - self.0[1] + rhs.0[1], - self.0[2] + rhs.0[2], - ]) - } -} - -impl Mul for Mat3x3 { - type Output = Self; - - #[inline] - fn mul(self, _rhs: Self) -> Self { - todo!() - } -} - -impl Mul for Mat3x3 { - type Output = Self; - - #[inline(always)] - fn mul(self, rhs: f32) -> Self { - Self([self.0[0] * rhs, self.0[1] * rhs, self.0[2] * rhs]) - } -} - -impl Div for Mat3x3 { - type Output = Self; - - #[inline(always)] - fn div(self, rhs: f32) -> Self { - Self([self.0[0] / rhs, self.0[1] / rhs, self.0[2] / rhs]) - } -} diff --git a/sub_crates/math3d/src/transform.rs b/sub_crates/math3d/src/transform.rs index 67a88e5..16056a1 100644 --- a/sub_crates/math3d/src/transform.rs +++ b/sub_crates/math3d/src/transform.rs @@ -4,7 +4,6 @@ use std::ops::{Add, Mul}; use approx::relative_eq; -use crate::mat3x3::Mat3x3; use crate::point::Point; use crate::transform_dual::TransformDual; use crate::wide4::f32x4; @@ -13,8 +12,8 @@ use crate::wide4::f32x4; #[derive(Debug, Copy, Clone)] #[repr(C)] pub struct Transform { - pub(crate) m: Mat3x3, // Scale, rotation, and shear. - pub(crate) t: f32x4, // Translation. + pub(crate) m: [f32x4; 3], // Linear matrix. + pub(crate) t: f32x4, // Translation. } impl Transform { @@ -45,11 +44,11 @@ impl Transform { l: f32, ) -> Self { Self { - m: Mat3x3::new( + m: [ f32x4::new(a, b, c, 0.0), f32x4::new(d, e, f, 0.0), f32x4::new(g, h, i, 0.0), - ), + ], t: f32x4::new(j, k, l, 0.0), } } @@ -58,7 +57,11 @@ impl Transform { #[inline] pub fn identity() -> Self { Self { - m: Mat3x3::identity(), + m: [ + f32x4::new(1.0, 0.0, 0.0, 0.0), + f32x4::new(0.0, 1.0, 0.0, 0.0), + f32x4::new(0.0, 0.0, 1.0, 0.0), + ], t: f32x4::splat(0.0), } } @@ -66,7 +69,11 @@ impl Transform { #[inline] pub fn from_location(loc: Point) -> Transform { Self { - m: Mat3x3::identity(), + m: [ + f32x4::new(1.0, 0.0, 0.0, 0.0), + f32x4::new(0.0, 1.0, 0.0, 0.0), + f32x4::new(0.0, 0.0, 1.0, 0.0), + ], t: loc.0, } } @@ -79,10 +86,9 @@ impl Transform { let mut eq = true; for (t1, t2) in self .m - .0 .iter() .chain(&[self.t]) - .zip(other.m.0.iter().chain(&[other.t])) + .zip(other.m.iter().chain(&[other.t])) { eq &= relative_eq!(t1.a(), t2.a(), epsilon = epsilon); eq &= relative_eq!(t1.b(), t2.b(), epsilon = epsilon); @@ -91,12 +97,22 @@ impl Transform { eq } - /// Returns the inverse of the Matrix + /// Returns the dual transform, which can do inverse transforms. #[inline] pub fn compute_dual(self) -> TransformDual { TransformDual { m: self.m, - m_inv: self.m.inverse(), + m_inv: f32x4::invert_3x3(self.m), + t: self.t, + } + } + + /// Slower but precise version of `compute_dual()`. + #[inline] + pub fn compute_dual_precise(self) -> TransformDual { + TransformDual { + m: self.m, + m_inv: f32x4::invert_3x3_precise(self.m), t: self.t, } } @@ -125,7 +141,7 @@ impl Mul for Transform { #[inline] fn mul(self, rhs: f32) -> Self { Self { - m: self.m * rhs, + m: [self.m[0] * rhs, self.m[1] * rhs, self.m[2] * rhs], t: self.t * rhs, } } @@ -138,7 +154,11 @@ impl Add for Transform { #[inline] fn add(self, rhs: Self) -> Self { Self { - m: self.m + rhs.m, + m: [ + self.m[0] + rhs.m[0], + self.m[1] + rhs.m[1], + self.m[2] + rhs.m[2], + ], t: self.t + rhs.t, } } diff --git a/sub_crates/math3d/src/transform_dual.rs b/sub_crates/math3d/src/transform_dual.rs index d0f191e..102061a 100644 --- a/sub_crates/math3d/src/transform_dual.rs +++ b/sub_crates/math3d/src/transform_dual.rs @@ -1,4 +1,3 @@ -use crate::mat3x3::Mat3x3; use crate::wide4::f32x4; /// An affine transform with precomputed data for performing reverse @@ -6,7 +5,7 @@ use crate::wide4::f32x4; #[derive(Debug, Copy, Clone)] #[repr(C)] pub struct TransformDual { - pub(crate) m: Mat3x3, // Scale, rotation, and shear. - pub(crate) m_inv: Mat3x3, // Inverse scale, rotation, and shear. - pub(crate) t: f32x4, // Forward translation. + pub(crate) m: [f32x4; 3], // Linear matrix. + pub(crate) m_inv: [f32x4; 3], // Inverse of linear matrix. + pub(crate) t: f32x4, // Forward translation. } diff --git a/sub_crates/math3d/src/wide4.rs b/sub_crates/math3d/src/wide4.rs index 3001e2f..8dcf29e 100644 --- a/sub_crates/math3d/src/wide4.rs +++ b/sub_crates/math3d/src/wide4.rs @@ -81,6 +81,31 @@ mod fallback { // a.max(b) // } + //----------------------------------------------------- + // For matrix stuff. + + #[inline(always)] + pub fn transpose_3x3(m: [Self; 3]) -> [Self; 3] { + [ + // The fourth component in each row below is arbitrary, + // but in this case chosen so that it matches the + // behavior of the SSE version of transpose_3x3. + Self::new(m[0].a(), m[1].a(), m[2].a(), m[2].d()), + Self::new(m[0].b(), m[1].b(), m[2].b(), m[2].d()), + Self::new(m[0].c(), m[1].c(), m[2].c(), m[2].d()), + ] + } + + #[inline] + pub fn invert_3x3(_m: [Self; 3]) -> [Self; 3] { + todo!() + } + + #[inline] + pub fn invert_3x3_precise(_m: [Self; 3]) -> [Self; 3] { + todo!() + } + //----------------------------------------------------- // Individual components.