Some shuffling of the math sub-crate's organization.
This commit is contained in:
parent
732dee958e
commit
8a695a7694
16
Cargo.lock
generated
16
Cargo.lock
generated
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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.
|
||||
}
|
|
@ -1,12 +1,12 @@
|
|||
[package]
|
||||
name = "math3d"
|
||||
name = "rmath"
|
||||
version = "0.1.0"
|
||||
authors = ["Nathan Vegdahl <cessen@cessen.com>"]
|
||||
edition = "2018"
|
||||
license = "MIT, Apache 2.0"
|
||||
|
||||
[lib]
|
||||
name = "math3d"
|
||||
name = "rmath"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
|
@ -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 {
|
|
@ -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)]
|
|
@ -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)]
|
|
@ -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)]
|
|
@ -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<f32> for f32x4 {
|
||||
impl Mul<f32> 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<f32> for f32x4 {
|
||||
impl Div<f32> 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<f32> for f32x4 {
|
||||
impl MulAssign<f32> 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<f32> for f32x4 {
|
||||
impl DivAssign<f32> for Float4 {
|
||||
#[inline(always)]
|
||||
fn div_assign(&mut self, rhs: f32) {
|
||||
*self = *self / rhs;
|
|
@ -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<f32> for Transform {
|
||||
impl Mul<f32> for Xform {
|
||||
type Output = Self;
|
||||
|
||||
#[inline]
|
||||
|
@ -148,7 +147,7 @@ impl Mul<f32> 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));
|
||||
// }
|
Loading…
Reference in New Issue
Block a user