From 8a695a76943a3d0a4b71cc56ecf400be9c994b8a Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Thu, 14 Jul 2022 12:31:32 -0700 Subject: [PATCH] Some shuffling of the math sub-crate's organization. --- Cargo.lock | 16 ++-- Cargo.toml | 6 +- sub_crates/math3d/src/transform_dual.rs | 11 --- sub_crates/{math3d => rmath}/Cargo.toml | 4 +- sub_crates/{math3d => rmath}/LICENSE.md | 0 sub_crates/{math3d => rmath}/src/lib.rs | 16 ++-- sub_crates/{math3d => rmath}/src/normal.rs | 6 +- sub_crates/{math3d => rmath}/src/point.rs | 6 +- sub_crates/{math3d => rmath}/src/vector.rs | 6 +- sub_crates/{math3d => rmath}/src/wide4.rs | 36 +++---- .../src/transform.rs => rmath/src/xform.rs} | 93 +++++++++++-------- 11 files changed, 99 insertions(+), 101 deletions(-) delete mode 100644 sub_crates/math3d/src/transform_dual.rs rename sub_crates/{math3d => rmath}/Cargo.toml (84%) rename sub_crates/{math3d => rmath}/LICENSE.md (100%) rename sub_crates/{math3d => rmath}/src/lib.rs (67%) rename sub_crates/{math3d => rmath}/src/normal.rs (98%) rename sub_crates/{math3d => rmath}/src/point.rs (97%) rename sub_crates/{math3d => rmath}/src/vector.rs (98%) rename sub_crates/{math3d => rmath}/src/wide4.rs (93%) rename sub_crates/{math3d/src/transform.rs => rmath/src/xform.rs} (66%) diff --git a/Cargo.lock b/Cargo.lock index 15243e4..9f52b24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -242,13 +242,6 @@ version = "0.2.94" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e" -[[package]] -name = "math3d" -version = "0.1.0" -dependencies = [ - "approx", -] - [[package]] name = "memchr" version = "2.4.0" @@ -360,11 +353,11 @@ dependencies = [ "halton", "kioku", "lazy_static", - "math3d", "nom", "num_cpus", "openexr", "png_encode_mini", + "rmath", "rustc-serialize", "scoped_threadpool", "sobol_burley", @@ -570,6 +563,13 @@ dependencies = [ "winapi", ] +[[package]] +name = "rmath" +version = "0.1.0" +dependencies = [ + "approx", +] + [[package]] name = "rustc-serialize" version = "0.3.24" diff --git a/Cargo.toml b/Cargo.toml index 79fd0db..cfafd46 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ members = [ "sub_crates/color", "sub_crates/compact", "sub_crates/halton", - "sub_crates/math3d", + "sub_crates/rmath", "sub_crates/spectral_upsampling", ] @@ -51,8 +51,8 @@ path = "sub_crates/compact" path = "sub_crates/halton" -[dependencies.math3d] -path = "sub_crates/math3d" +[dependencies.rmath] +path = "sub_crates/rmath" [dependencies.spectral_upsampling] path = "sub_crates/spectral_upsampling" diff --git a/sub_crates/math3d/src/transform_dual.rs b/sub_crates/math3d/src/transform_dual.rs deleted file mode 100644 index 102061a..0000000 --- a/sub_crates/math3d/src/transform_dual.rs +++ /dev/null @@ -1,11 +0,0 @@ -use crate::wide4::f32x4; - -/// An affine transform with precomputed data for performing reverse -/// transforms, among other things. -#[derive(Debug, Copy, Clone)] -#[repr(C)] -pub struct TransformDual { - 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/Cargo.toml b/sub_crates/rmath/Cargo.toml similarity index 84% rename from sub_crates/math3d/Cargo.toml rename to sub_crates/rmath/Cargo.toml index 18fd257..963a83c 100644 --- a/sub_crates/math3d/Cargo.toml +++ b/sub_crates/rmath/Cargo.toml @@ -1,12 +1,12 @@ [package] -name = "math3d" +name = "rmath" version = "0.1.0" authors = ["Nathan Vegdahl "] edition = "2018" license = "MIT, Apache 2.0" [lib] -name = "math3d" +name = "rmath" path = "src/lib.rs" [dependencies] diff --git a/sub_crates/math3d/LICENSE.md b/sub_crates/rmath/LICENSE.md similarity index 100% rename from sub_crates/math3d/LICENSE.md rename to sub_crates/rmath/LICENSE.md diff --git a/sub_crates/math3d/src/lib.rs b/sub_crates/rmath/src/lib.rs similarity index 67% rename from sub_crates/math3d/src/lib.rs rename to sub_crates/rmath/src/lib.rs index 6b8a785..25e343c 100644 --- a/sub_crates/math3d/src/lib.rs +++ b/sub_crates/rmath/src/lib.rs @@ -1,16 +1,14 @@ +//! RMath: a math library for building CPU-based renderers. + #![allow(dead_code)] -pub mod normal; -pub mod point; -pub mod transform; -pub mod transform_dual; -pub mod vector; +mod normal; +mod point; +mod vector; pub mod wide4; +mod xform; -pub use self::{ - normal::Normal, point::Point, transform::Transform, transform_dual::TransformDual, - vector::Vector, -}; +pub use self::{normal::Normal, point::Point, vector::Vector, xform::Xform, xform::XformFull}; // /// Trait for calculating dot products. // pub trait DotProduct { diff --git a/sub_crates/math3d/src/normal.rs b/sub_crates/rmath/src/normal.rs similarity index 98% rename from sub_crates/math3d/src/normal.rs rename to sub_crates/rmath/src/normal.rs index b0cd469..64c1bcd 100644 --- a/sub_crates/math3d/src/normal.rs +++ b/sub_crates/rmath/src/normal.rs @@ -2,19 +2,19 @@ use std::ops::{Add, Div, Mul, Neg, Sub}; -use crate::wide4::f32x4; +use crate::wide4::Float4; use crate::Vector; /// A surface normal in 3D space. #[derive(Debug, Copy, Clone)] #[repr(transparent)] -pub struct Normal(pub(crate) f32x4); +pub struct Normal(pub(crate) Float4); impl Normal { #[inline(always)] pub fn new(x: f32, y: f32, z: f32) -> Self { - Self(f32x4::new(x, y, z, 0.0)) + Self(Float4::new(x, y, z, 0.0)) } #[inline(always)] diff --git a/sub_crates/math3d/src/point.rs b/sub_crates/rmath/src/point.rs similarity index 97% rename from sub_crates/math3d/src/point.rs rename to sub_crates/rmath/src/point.rs index a70d11e..0af5900 100644 --- a/sub_crates/math3d/src/point.rs +++ b/sub_crates/rmath/src/point.rs @@ -2,17 +2,17 @@ use std::ops::{Add, Sub}; use crate::vector::Vector; -use crate::wide4::f32x4; +use crate::wide4::Float4; /// A position in 3D space. #[derive(Debug, Copy, Clone)] #[repr(transparent)] -pub struct Point(pub(crate) f32x4); +pub struct Point(pub(crate) Float4); impl Point { #[inline(always)] pub fn new(x: f32, y: f32, z: f32) -> Self { - Self(f32x4::new(x, y, z, 0.0)) + Self(Float4::new(x, y, z, 0.0)) } #[inline(always)] diff --git a/sub_crates/math3d/src/vector.rs b/sub_crates/rmath/src/vector.rs similarity index 98% rename from sub_crates/math3d/src/vector.rs rename to sub_crates/rmath/src/vector.rs index cecb4b6..111cd87 100644 --- a/sub_crates/math3d/src/vector.rs +++ b/sub_crates/rmath/src/vector.rs @@ -4,17 +4,17 @@ use std::ops::{Add, Div, Mul, Neg, Sub}; use crate::normal::Normal; use crate::point::Point; -use crate::wide4::f32x4; +use crate::wide4::Float4; /// A direction vector in 3D space. #[derive(Debug, Copy, Clone)] #[repr(transparent)] -pub struct Vector(pub(crate) f32x4); +pub struct Vector(pub(crate) Float4); impl Vector { #[inline(always)] pub fn new(x: f32, y: f32, z: f32) -> Self { - Self(f32x4::new(x, y, z, 0.0)) + Self(Float4::new(x, y, z, 0.0)) } #[inline(always)] diff --git a/sub_crates/math3d/src/wide4.rs b/sub_crates/rmath/src/wide4.rs similarity index 93% rename from sub_crates/math3d/src/wide4.rs rename to sub_crates/rmath/src/wide4.rs index 8dcf29e..cdbd9ec 100644 --- a/sub_crates/math3d/src/wide4.rs +++ b/sub_crates/rmath/src/wide4.rs @@ -1,24 +1,24 @@ use std::ops::{AddAssign, DivAssign, MulAssign, SubAssign}; -pub use fallback::f32x4; +pub use fallback::Float4; mod fallback { use std::ops::{Add, Div, Mul, Neg, Sub}; #[allow(non_camel_case_types)] #[derive(Debug, Copy, Clone)] #[repr(C, align(16))] - pub struct f32x4 { + pub struct Float4 { n: [f32; 4], } - impl f32x4 { - /// Create a new `f32x4` with the given components. + impl Float4 { + /// Create a new `Float4` with the given components. #[inline(always)] pub fn new(a: f32, b: f32, c: f32, d: f32) -> Self { Self { n: [a, b, c, d] } } - /// Create a new `f32x4` with all elements set to `n`. + /// Create a new `Float4` with all elements set to `n`. #[inline(always)] pub fn splat(n: f32) -> Self { Self { n: [n, n, n, n] } @@ -189,7 +189,7 @@ mod fallback { } } - impl Add for f32x4 { + impl Add for Float4 { type Output = Self; #[inline(always)] @@ -205,7 +205,7 @@ mod fallback { } } - impl Sub for f32x4 { + impl Sub for Float4 { type Output = Self; #[inline(always)] @@ -221,7 +221,7 @@ mod fallback { } } - impl Mul for f32x4 { + impl Mul for Float4 { type Output = Self; #[inline(always)] @@ -237,7 +237,7 @@ mod fallback { } } - impl Mul for f32x4 { + impl Mul for Float4 { type Output = Self; #[inline(always)] @@ -253,7 +253,7 @@ mod fallback { } } - impl Div for f32x4 { + impl Div for Float4 { type Output = Self; #[inline(always)] @@ -269,7 +269,7 @@ mod fallback { } } - impl Div for f32x4 { + impl Div for Float4 { type Output = Self; #[inline(always)] @@ -285,7 +285,7 @@ mod fallback { } } - impl Neg for f32x4 { + impl Neg for Float4 { type Output = Self; #[inline(always)] @@ -299,42 +299,42 @@ mod fallback { //------------------------------------------------------------- -impl AddAssign for f32x4 { +impl AddAssign for Float4 { #[inline(always)] fn add_assign(&mut self, rhs: Self) { *self = *self + rhs; } } -impl SubAssign for f32x4 { +impl SubAssign for Float4 { #[inline(always)] fn sub_assign(&mut self, rhs: Self) { *self = *self - rhs; } } -impl MulAssign for f32x4 { +impl MulAssign for Float4 { #[inline(always)] fn mul_assign(&mut self, rhs: Self) { *self = *self * rhs; } } -impl MulAssign for f32x4 { +impl MulAssign for Float4 { #[inline(always)] fn mul_assign(&mut self, rhs: f32) { *self = *self * rhs; } } -impl DivAssign for f32x4 { +impl DivAssign for Float4 { #[inline(always)] fn div_assign(&mut self, rhs: Self) { *self = *self / rhs; } } -impl DivAssign for f32x4 { +impl DivAssign for Float4 { #[inline(always)] fn div_assign(&mut self, rhs: f32) { *self = *self / rhs; diff --git a/sub_crates/math3d/src/transform.rs b/sub_crates/rmath/src/xform.rs similarity index 66% rename from sub_crates/math3d/src/transform.rs rename to sub_crates/rmath/src/xform.rs index 16056a1..79f7cdb 100644 --- a/sub_crates/math3d/src/transform.rs +++ b/sub_crates/rmath/src/xform.rs @@ -5,18 +5,17 @@ use std::ops::{Add, Mul}; use approx::relative_eq; use crate::point::Point; -use crate::transform_dual::TransformDual; -use crate::wide4::f32x4; +use crate::wide4::Float4; /// An affine transform. #[derive(Debug, Copy, Clone)] #[repr(C)] -pub struct Transform { - pub(crate) m: [f32x4; 3], // Linear matrix. - pub(crate) t: f32x4, // Translation. +pub struct Xform { + pub(crate) m: [Float4; 3], // Linear matrix. + pub(crate) t: Float4, // Translation. } -impl Transform { +impl Xform { /// Creates a new affine transform the specified values: /// /// ``` @@ -45,11 +44,11 @@ impl Transform { ) -> Self { Self { m: [ - f32x4::new(a, b, c, 0.0), - f32x4::new(d, e, f, 0.0), - f32x4::new(g, h, i, 0.0), + Float4::new(a, b, c, 0.0), + Float4::new(d, e, f, 0.0), + Float4::new(g, h, i, 0.0), ], - t: f32x4::new(j, k, l, 0.0), + t: Float4::new(j, k, l, 0.0), } } @@ -58,21 +57,21 @@ impl Transform { pub fn identity() -> Self { Self { 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), + Float4::new(1.0, 0.0, 0.0, 0.0), + Float4::new(0.0, 1.0, 0.0, 0.0), + Float4::new(0.0, 0.0, 1.0, 0.0), ], - t: f32x4::splat(0.0), + t: Float4::splat(0.0), } } #[inline] - pub fn from_location(loc: Point) -> Transform { + pub fn from_location(loc: Point) -> Xform { Self { 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), + Float4::new(1.0, 0.0, 0.0, 0.0), + Float4::new(0.0, 1.0, 0.0, 0.0), + Float4::new(0.0, 0.0, 1.0, 0.0), ], t: loc.0, } @@ -82,7 +81,7 @@ impl Transform { /// Each corresponding element in the matrices cannot have a relative /// error exceeding epsilon. #[inline] - pub fn aprx_eq(&self, other: Transform, epsilon: f32) -> bool { + pub fn aprx_eq(&self, other: Xform, epsilon: f32) -> bool { let mut eq = true; for (t1, t2) in self .m @@ -99,33 +98,33 @@ impl Transform { /// Returns the dual transform, which can do inverse transforms. #[inline] - pub fn compute_dual(self) -> TransformDual { - TransformDual { + pub fn compute_dual(self) -> XformFull { + XformFull { m: self.m, - m_inv: f32x4::invert_3x3(self.m), + m_inv: Float4::invert_3x3(self.m), t: self.t, } } /// Slower but precise version of `compute_dual()`. #[inline] - pub fn compute_dual_precise(self) -> TransformDual { - TransformDual { + pub fn compute_dual_precise(self) -> XformFull { + XformFull { m: self.m, - m_inv: f32x4::invert_3x3_precise(self.m), + m_inv: Float4::invert_3x3_precise(self.m), t: self.t, } } } -impl Default for Transform { +impl Default for Xform { fn default() -> Self { Self::identity() } } // /// Multiply two matrices together -// impl Mul for Transform { +// impl Mul for Xform { // type Output = Self; // #[inline] @@ -135,7 +134,7 @@ impl Default for Transform { // } /// Multiply a matrix by a f32 -impl Mul for Transform { +impl Mul for Xform { type Output = Self; #[inline] @@ -148,7 +147,7 @@ impl Mul for Transform { } /// Add two matrices together -impl Add for Transform { +impl Add for Xform { type Output = Self; #[inline] @@ -166,16 +165,28 @@ impl Add for Transform { //------------------------------------------------------------- +/// An affine transform with precomputed data for performing reverse +/// transforms, among other things. +#[derive(Debug, Copy, Clone)] +#[repr(C)] +pub struct XformFull { + pub(crate) m: [Float4; 3], // Linear matrix. + pub(crate) m_inv: [Float4; 3], // Inverse of linear matrix. + pub(crate) t: Float4, // Forward translation. +} + +//------------------------------------------------------------- + // #[cfg(test)] // mod tests { // use super::*; // #[test] // fn equality_test() { -// let a = Transform::new(); -// let b = Transform::new(); +// let a = Xform::new(); +// let b = Xform::new(); // let c = -// Transform::new_from_values(1.1, 0.0, 0.0, 0.0, 0.0, 1.1, 0.0, 0.0, 0.0, 0.0, 1.1, 0.0); +// Xform::new_from_values(1.1, 0.0, 0.0, 0.0, 0.0, 1.1, 0.0, 0.0, 0.0, 0.0, 1.1, 0.0); // assert_eq!(a, b); // assert!(a != c); @@ -183,14 +194,14 @@ impl Add for Transform { // #[test] // fn approximate_equality_test() { -// let a = Transform::new(); -// let b = Transform::new_from_values( +// let a = Xform::new(); +// let b = Xform::new_from_values( // 1.000001, 0.0, 0.0, 0.0, 0.0, 1.000001, 0.0, 0.0, 0.0, 0.0, 1.000001, 0.0, // ); -// let c = Transform::new_from_values( +// let c = Xform::new_from_values( // 1.000003, 0.0, 0.0, 0.0, 0.0, 1.000003, 0.0, 0.0, 0.0, 0.0, 1.000003, 0.0, // ); -// let d = Transform::new_from_values( +// let d = Xform::new_from_values( // -1.000001, 0.0, 0.0, 0.0, 0.0, -1.000001, 0.0, 0.0, 0.0, 0.0, -1.000001, 0.0, // ); @@ -201,13 +212,13 @@ impl Add for Transform { // #[test] // fn multiply_test() { -// let a = Transform::new_from_values( +// let a = Xform::new_from_values( // 1.0, 2.0, 2.0, 1.5, 3.0, 6.0, 7.0, 8.0, 9.0, 2.0, 11.0, 12.0, // ); -// let b = Transform::new_from_values( +// let b = Xform::new_from_values( // 1.0, 5.0, 9.0, 13.0, 2.0, 6.0, 10.0, 14.0, 3.0, 7.0, 11.0, 15.0, // ); -// let c = Transform::new_from_values( +// let c = Xform::new_from_values( // 97.0, 50.0, 136.0, 162.5, 110.0, 60.0, 156.0, 185.0, 123.0, 70.0, 176.0, 207.5, // ); @@ -216,11 +227,11 @@ impl Add for Transform { // #[test] // fn inverse_test() { -// let a = Transform::new_from_values( +// let a = Xform::new_from_values( // 1.0, 0.33, 0.0, -2.0, 0.0, 1.0, 0.0, 0.0, 2.1, 0.7, 1.3, 0.0, // ); // let b = a.inverse(); -// let c = Transform::new(); +// let c = Xform::new(); // assert!((dbg!(a * b)).aprx_eq(dbg!(c), 0.0000001)); // }