Some shuffling of the math sub-crate's organization.

This commit is contained in:
Nathan Vegdahl 2022-07-14 12:31:32 -07:00
parent 732dee958e
commit 8a695a7694
11 changed files with 99 additions and 101 deletions

16
Cargo.lock generated
View File

@ -242,13 +242,6 @@ version = "0.2.94"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e" checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e"
[[package]]
name = "math3d"
version = "0.1.0"
dependencies = [
"approx",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.4.0" version = "2.4.0"
@ -360,11 +353,11 @@ dependencies = [
"halton", "halton",
"kioku", "kioku",
"lazy_static", "lazy_static",
"math3d",
"nom", "nom",
"num_cpus", "num_cpus",
"openexr", "openexr",
"png_encode_mini", "png_encode_mini",
"rmath",
"rustc-serialize", "rustc-serialize",
"scoped_threadpool", "scoped_threadpool",
"sobol_burley", "sobol_burley",
@ -570,6 +563,13 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "rmath"
version = "0.1.0"
dependencies = [
"approx",
]
[[package]] [[package]]
name = "rustc-serialize" name = "rustc-serialize"
version = "0.3.24" version = "0.3.24"

View File

@ -4,7 +4,7 @@ members = [
"sub_crates/color", "sub_crates/color",
"sub_crates/compact", "sub_crates/compact",
"sub_crates/halton", "sub_crates/halton",
"sub_crates/math3d", "sub_crates/rmath",
"sub_crates/spectral_upsampling", "sub_crates/spectral_upsampling",
] ]
@ -51,8 +51,8 @@ path = "sub_crates/compact"
path = "sub_crates/halton" path = "sub_crates/halton"
[dependencies.math3d] [dependencies.rmath]
path = "sub_crates/math3d" path = "sub_crates/rmath"
[dependencies.spectral_upsampling] [dependencies.spectral_upsampling]
path = "sub_crates/spectral_upsampling" path = "sub_crates/spectral_upsampling"

View File

@ -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.
}

View File

@ -1,12 +1,12 @@
[package] [package]
name = "math3d" name = "rmath"
version = "0.1.0" version = "0.1.0"
authors = ["Nathan Vegdahl <cessen@cessen.com>"] authors = ["Nathan Vegdahl <cessen@cessen.com>"]
edition = "2018" edition = "2018"
license = "MIT, Apache 2.0" license = "MIT, Apache 2.0"
[lib] [lib]
name = "math3d" name = "rmath"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]

View File

@ -1,16 +1,14 @@
//! RMath: a math library for building CPU-based renderers.
#![allow(dead_code)] #![allow(dead_code)]
pub mod normal; mod normal;
pub mod point; mod point;
pub mod transform; mod vector;
pub mod transform_dual;
pub mod vector;
pub mod wide4; pub mod wide4;
mod xform;
pub use self::{ pub use self::{normal::Normal, point::Point, vector::Vector, xform::Xform, xform::XformFull};
normal::Normal, point::Point, transform::Transform, transform_dual::TransformDual,
vector::Vector,
};
// /// Trait for calculating dot products. // /// Trait for calculating dot products.
// pub trait DotProduct { // pub trait DotProduct {

View File

@ -2,19 +2,19 @@
use std::ops::{Add, Div, Mul, Neg, Sub}; use std::ops::{Add, Div, Mul, Neg, Sub};
use crate::wide4::f32x4; use crate::wide4::Float4;
use crate::Vector; use crate::Vector;
/// A surface normal in 3D space. /// A surface normal in 3D space.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
#[repr(transparent)] #[repr(transparent)]
pub struct Normal(pub(crate) f32x4); pub struct Normal(pub(crate) Float4);
impl Normal { impl Normal {
#[inline(always)] #[inline(always)]
pub fn new(x: f32, y: f32, z: f32) -> Self { 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)] #[inline(always)]

View File

@ -2,17 +2,17 @@
use std::ops::{Add, Sub}; use std::ops::{Add, Sub};
use crate::vector::Vector; use crate::vector::Vector;
use crate::wide4::f32x4; use crate::wide4::Float4;
/// A position in 3D space. /// A position in 3D space.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
#[repr(transparent)] #[repr(transparent)]
pub struct Point(pub(crate) f32x4); pub struct Point(pub(crate) Float4);
impl Point { impl Point {
#[inline(always)] #[inline(always)]
pub fn new(x: f32, y: f32, z: f32) -> Self { 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)] #[inline(always)]

View File

@ -4,17 +4,17 @@ use std::ops::{Add, Div, Mul, Neg, Sub};
use crate::normal::Normal; use crate::normal::Normal;
use crate::point::Point; use crate::point::Point;
use crate::wide4::f32x4; use crate::wide4::Float4;
/// A direction vector in 3D space. /// A direction vector in 3D space.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
#[repr(transparent)] #[repr(transparent)]
pub struct Vector(pub(crate) f32x4); pub struct Vector(pub(crate) Float4);
impl Vector { impl Vector {
#[inline(always)] #[inline(always)]
pub fn new(x: f32, y: f32, z: f32) -> Self { 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)] #[inline(always)]

View File

@ -1,24 +1,24 @@
use std::ops::{AddAssign, DivAssign, MulAssign, SubAssign}; use std::ops::{AddAssign, DivAssign, MulAssign, SubAssign};
pub use fallback::f32x4; pub use fallback::Float4;
mod fallback { mod fallback {
use std::ops::{Add, Div, Mul, Neg, Sub}; use std::ops::{Add, Div, Mul, Neg, Sub};
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
#[repr(C, align(16))] #[repr(C, align(16))]
pub struct f32x4 { pub struct Float4 {
n: [f32; 4], n: [f32; 4],
} }
impl f32x4 { impl Float4 {
/// Create a new `f32x4` with the given components. /// Create a new `Float4` with the given components.
#[inline(always)] #[inline(always)]
pub fn new(a: f32, b: f32, c: f32, d: f32) -> Self { pub fn new(a: f32, b: f32, c: f32, d: f32) -> Self {
Self { n: [a, b, c, d] } 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)] #[inline(always)]
pub fn splat(n: f32) -> Self { pub fn splat(n: f32) -> Self {
Self { n: [n, n, n, n] } Self { n: [n, n, n, n] }
@ -189,7 +189,7 @@ mod fallback {
} }
} }
impl Add for f32x4 { impl Add for Float4 {
type Output = Self; type Output = Self;
#[inline(always)] #[inline(always)]
@ -205,7 +205,7 @@ mod fallback {
} }
} }
impl Sub for f32x4 { impl Sub for Float4 {
type Output = Self; type Output = Self;
#[inline(always)] #[inline(always)]
@ -221,7 +221,7 @@ mod fallback {
} }
} }
impl Mul for f32x4 { impl Mul for Float4 {
type Output = Self; type Output = Self;
#[inline(always)] #[inline(always)]
@ -237,7 +237,7 @@ mod fallback {
} }
} }
impl Mul<f32> for f32x4 { impl Mul<f32> for Float4 {
type Output = Self; type Output = Self;
#[inline(always)] #[inline(always)]
@ -253,7 +253,7 @@ mod fallback {
} }
} }
impl Div for f32x4 { impl Div for Float4 {
type Output = Self; type Output = Self;
#[inline(always)] #[inline(always)]
@ -269,7 +269,7 @@ mod fallback {
} }
} }
impl Div<f32> for f32x4 { impl Div<f32> for Float4 {
type Output = Self; type Output = Self;
#[inline(always)] #[inline(always)]
@ -285,7 +285,7 @@ mod fallback {
} }
} }
impl Neg for f32x4 { impl Neg for Float4 {
type Output = Self; type Output = Self;
#[inline(always)] #[inline(always)]
@ -299,42 +299,42 @@ mod fallback {
//------------------------------------------------------------- //-------------------------------------------------------------
impl AddAssign for f32x4 { impl AddAssign for Float4 {
#[inline(always)] #[inline(always)]
fn add_assign(&mut self, rhs: Self) { fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs; *self = *self + rhs;
} }
} }
impl SubAssign for f32x4 { impl SubAssign for Float4 {
#[inline(always)] #[inline(always)]
fn sub_assign(&mut self, rhs: Self) { fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs; *self = *self - rhs;
} }
} }
impl MulAssign for f32x4 { impl MulAssign for Float4 {
#[inline(always)] #[inline(always)]
fn mul_assign(&mut self, rhs: Self) { fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs; *self = *self * rhs;
} }
} }
impl MulAssign<f32> for f32x4 { impl MulAssign<f32> for Float4 {
#[inline(always)] #[inline(always)]
fn mul_assign(&mut self, rhs: f32) { fn mul_assign(&mut self, rhs: f32) {
*self = *self * rhs; *self = *self * rhs;
} }
} }
impl DivAssign for f32x4 { impl DivAssign for Float4 {
#[inline(always)] #[inline(always)]
fn div_assign(&mut self, rhs: Self) { fn div_assign(&mut self, rhs: Self) {
*self = *self / rhs; *self = *self / rhs;
} }
} }
impl DivAssign<f32> for f32x4 { impl DivAssign<f32> for Float4 {
#[inline(always)] #[inline(always)]
fn div_assign(&mut self, rhs: f32) { fn div_assign(&mut self, rhs: f32) {
*self = *self / rhs; *self = *self / rhs;

View File

@ -5,18 +5,17 @@ use std::ops::{Add, Mul};
use approx::relative_eq; use approx::relative_eq;
use crate::point::Point; use crate::point::Point;
use crate::transform_dual::TransformDual; use crate::wide4::Float4;
use crate::wide4::f32x4;
/// An affine transform. /// An affine transform.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
#[repr(C)] #[repr(C)]
pub struct Transform { pub struct Xform {
pub(crate) m: [f32x4; 3], // Linear matrix. pub(crate) m: [Float4; 3], // Linear matrix.
pub(crate) t: f32x4, // Translation. pub(crate) t: Float4, // Translation.
} }
impl Transform { impl Xform {
/// Creates a new affine transform the specified values: /// Creates a new affine transform the specified values:
/// ///
/// ``` /// ```
@ -45,11 +44,11 @@ impl Transform {
) -> Self { ) -> Self {
Self { Self {
m: [ m: [
f32x4::new(a, b, c, 0.0), Float4::new(a, b, c, 0.0),
f32x4::new(d, e, f, 0.0), Float4::new(d, e, f, 0.0),
f32x4::new(g, h, i, 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 { pub fn identity() -> Self {
Self { Self {
m: [ m: [
f32x4::new(1.0, 0.0, 0.0, 0.0), Float4::new(1.0, 0.0, 0.0, 0.0),
f32x4::new(0.0, 1.0, 0.0, 0.0), Float4::new(0.0, 1.0, 0.0, 0.0),
f32x4::new(0.0, 0.0, 1.0, 0.0), Float4::new(0.0, 0.0, 1.0, 0.0),
], ],
t: f32x4::splat(0.0), t: Float4::splat(0.0),
} }
} }
#[inline] #[inline]
pub fn from_location(loc: Point) -> Transform { pub fn from_location(loc: Point) -> Xform {
Self { Self {
m: [ m: [
f32x4::new(1.0, 0.0, 0.0, 0.0), Float4::new(1.0, 0.0, 0.0, 0.0),
f32x4::new(0.0, 1.0, 0.0, 0.0), Float4::new(0.0, 1.0, 0.0, 0.0),
f32x4::new(0.0, 0.0, 1.0, 0.0), Float4::new(0.0, 0.0, 1.0, 0.0),
], ],
t: loc.0, t: loc.0,
} }
@ -82,7 +81,7 @@ impl Transform {
/// Each corresponding element in the matrices cannot have a relative /// Each corresponding element in the matrices cannot have a relative
/// error exceeding epsilon. /// error exceeding epsilon.
#[inline] #[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; let mut eq = true;
for (t1, t2) in self for (t1, t2) in self
.m .m
@ -99,33 +98,33 @@ impl Transform {
/// Returns the dual transform, which can do inverse transforms. /// Returns the dual transform, which can do inverse transforms.
#[inline] #[inline]
pub fn compute_dual(self) -> TransformDual { pub fn compute_dual(self) -> XformFull {
TransformDual { XformFull {
m: self.m, m: self.m,
m_inv: f32x4::invert_3x3(self.m), m_inv: Float4::invert_3x3(self.m),
t: self.t, t: self.t,
} }
} }
/// Slower but precise version of `compute_dual()`. /// Slower but precise version of `compute_dual()`.
#[inline] #[inline]
pub fn compute_dual_precise(self) -> TransformDual { pub fn compute_dual_precise(self) -> XformFull {
TransformDual { XformFull {
m: self.m, m: self.m,
m_inv: f32x4::invert_3x3_precise(self.m), m_inv: Float4::invert_3x3_precise(self.m),
t: self.t, t: self.t,
} }
} }
} }
impl Default for Transform { impl Default for Xform {
fn default() -> Self { fn default() -> Self {
Self::identity() Self::identity()
} }
} }
// /// Multiply two matrices together // /// Multiply two matrices together
// impl Mul for Transform { // impl Mul for Xform {
// type Output = Self; // type Output = Self;
// #[inline] // #[inline]
@ -135,7 +134,7 @@ impl Default for Transform {
// } // }
/// Multiply a matrix by a f32 /// Multiply a matrix by a f32
impl Mul<f32> for Transform { impl Mul<f32> for Xform {
type Output = Self; type Output = Self;
#[inline] #[inline]
@ -148,7 +147,7 @@ impl Mul<f32> for Transform {
} }
/// Add two matrices together /// Add two matrices together
impl Add for Transform { impl Add for Xform {
type Output = Self; type Output = Self;
#[inline] #[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)] // #[cfg(test)]
// mod tests { // mod tests {
// use super::*; // use super::*;
// #[test] // #[test]
// fn equality_test() { // fn equality_test() {
// let a = Transform::new(); // let a = Xform::new();
// let b = Transform::new(); // let b = Xform::new();
// let c = // 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_eq!(a, b);
// assert!(a != c); // assert!(a != c);
@ -183,14 +194,14 @@ impl Add for Transform {
// #[test] // #[test]
// fn approximate_equality_test() { // fn approximate_equality_test() {
// let a = Transform::new(); // let a = Xform::new();
// let b = Transform::new_from_values( // 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, // 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, // 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, // -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] // #[test]
// fn multiply_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, // 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, // 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, // 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] // #[test]
// fn inverse_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, // 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 b = a.inverse();
// let c = Transform::new(); // let c = Xform::new();
// assert!((dbg!(a * b)).aprx_eq(dbg!(c), 0.0000001)); // assert!((dbg!(a * b)).aprx_eq(dbg!(c), 0.0000001));
// } // }