Remove Mat3x3 from math3d lib.

It was an extraneous abstraction.
This commit is contained in:
Nathan Vegdahl 2022-07-14 00:33:38 -07:00
parent 658e4746ca
commit 732dee958e
5 changed files with 61 additions and 101 deletions

View File

@ -1,6 +1,5 @@
#![allow(dead_code)] #![allow(dead_code)]
pub mod mat3x3;
pub mod normal; pub mod normal;
pub mod point; pub mod point;
pub mod transform; pub mod transform;

View File

@ -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<f32> 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<f32> 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])
}
}

View File

@ -4,7 +4,6 @@ use std::ops::{Add, Mul};
use approx::relative_eq; use approx::relative_eq;
use crate::mat3x3::Mat3x3;
use crate::point::Point; use crate::point::Point;
use crate::transform_dual::TransformDual; use crate::transform_dual::TransformDual;
use crate::wide4::f32x4; use crate::wide4::f32x4;
@ -13,8 +12,8 @@ use crate::wide4::f32x4;
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
#[repr(C)] #[repr(C)]
pub struct Transform { pub struct Transform {
pub(crate) m: Mat3x3, // Scale, rotation, and shear. pub(crate) m: [f32x4; 3], // Linear matrix.
pub(crate) t: f32x4, // Translation. pub(crate) t: f32x4, // Translation.
} }
impl Transform { impl Transform {
@ -45,11 +44,11 @@ impl Transform {
l: f32, l: f32,
) -> Self { ) -> Self {
Self { Self {
m: Mat3x3::new( m: [
f32x4::new(a, b, c, 0.0), f32x4::new(a, b, c, 0.0),
f32x4::new(d, e, f, 0.0), f32x4::new(d, e, f, 0.0),
f32x4::new(g, h, i, 0.0), f32x4::new(g, h, i, 0.0),
), ],
t: f32x4::new(j, k, l, 0.0), t: f32x4::new(j, k, l, 0.0),
} }
} }
@ -58,7 +57,11 @@ impl Transform {
#[inline] #[inline]
pub fn identity() -> Self { pub fn identity() -> Self {
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), t: f32x4::splat(0.0),
} }
} }
@ -66,7 +69,11 @@ impl Transform {
#[inline] #[inline]
pub fn from_location(loc: Point) -> Transform { pub fn from_location(loc: Point) -> Transform {
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: loc.0, t: loc.0,
} }
} }
@ -79,10 +86,9 @@ impl Transform {
let mut eq = true; let mut eq = true;
for (t1, t2) in self for (t1, t2) in self
.m .m
.0
.iter() .iter()
.chain(&[self.t]) .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.a(), t2.a(), epsilon = epsilon);
eq &= relative_eq!(t1.b(), t2.b(), epsilon = epsilon); eq &= relative_eq!(t1.b(), t2.b(), epsilon = epsilon);
@ -91,12 +97,22 @@ impl Transform {
eq eq
} }
/// Returns the inverse of the Matrix /// Returns the dual transform, which can do inverse transforms.
#[inline] #[inline]
pub fn compute_dual(self) -> TransformDual { pub fn compute_dual(self) -> TransformDual {
TransformDual { TransformDual {
m: self.m, 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, t: self.t,
} }
} }
@ -125,7 +141,7 @@ impl Mul<f32> for Transform {
#[inline] #[inline]
fn mul(self, rhs: f32) -> Self { fn mul(self, rhs: f32) -> Self {
Self { Self {
m: self.m * rhs, m: [self.m[0] * rhs, self.m[1] * rhs, self.m[2] * rhs],
t: self.t * rhs, t: self.t * rhs,
} }
} }
@ -138,7 +154,11 @@ impl Add for Transform {
#[inline] #[inline]
fn add(self, rhs: Self) -> Self { fn add(self, rhs: Self) -> 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, t: self.t + rhs.t,
} }
} }

View File

@ -1,4 +1,3 @@
use crate::mat3x3::Mat3x3;
use crate::wide4::f32x4; use crate::wide4::f32x4;
/// An affine transform with precomputed data for performing reverse /// An affine transform with precomputed data for performing reverse
@ -6,7 +5,7 @@ use crate::wide4::f32x4;
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
#[repr(C)] #[repr(C)]
pub struct TransformDual { pub struct TransformDual {
pub(crate) m: Mat3x3, // Scale, rotation, and shear. pub(crate) m: [f32x4; 3], // Linear matrix.
pub(crate) m_inv: Mat3x3, // Inverse scale, rotation, and shear. pub(crate) m_inv: [f32x4; 3], // Inverse of linear matrix.
pub(crate) t: f32x4, // Forward translation. pub(crate) t: f32x4, // Forward translation.
} }

View File

@ -81,6 +81,31 @@ mod fallback {
// a.max(b) // 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. // Individual components.