From f57ef1c14158a8296e732d6d2953960ffd8083e5 Mon Sep 17 00:00:00 2001 From: numzero Date: Sun, 15 Sep 2024 10:03:38 +0300 Subject: [PATCH] Extract Decomp2 to mathx --- src/bin/flat/riemann.rs | 40 +----------------------------------- src/bin/flat/tube/metric.rs | 6 ++++-- src/mathx.rs | 41 ++++++++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 42 deletions(-) diff --git a/src/bin/flat/riemann.rs b/src/bin/flat/riemann.rs index f8b232c..a6e3d39 100644 --- a/src/bin/flat/riemann.rs +++ b/src/bin/flat/riemann.rs @@ -1,43 +1,5 @@ use glam::*; - -#[derive(Copy, Clone, Debug, PartialEq)] -pub struct Decomp2 { - pub ortho: Mat2, - pub diag: Vec2, -} - -impl Decomp2 { - fn square(&self) -> Self { - Self { - ortho: self.ortho, - diag: self.diag * self.diag, - } - } - - pub fn inverse(&self) -> Self { - Self { - ortho: self.ortho, - diag: Vec2::splat(1.0) / self.diag, - } - } -} - -impl std::ops::Mul for Decomp2 -where - Mat2: std::ops::Mul, -{ - type Output = >::Output; - - fn mul(self, rhs: T) -> Self::Output { - Mat2::from(self) * rhs - } -} - -impl From for Mat2 { - fn from(value: Decomp2) -> Self { - value.ortho.transpose() * Mat2::from_diagonal(value.diag) * value.ortho - } -} +use refraction::mathx::Decomp2; pub type Tens2 = [Mat2; 2]; diff --git a/src/bin/flat/tube/metric.rs b/src/bin/flat/tube/metric.rs index f88b69b..b4d4c60 100644 --- a/src/bin/flat/tube/metric.rs +++ b/src/bin/flat/tube/metric.rs @@ -1,7 +1,8 @@ use glam::{f32, vec2, Mat2, Vec2}; +use refraction::mathx::Decomp2; use crate::fns::{self, Limiter}; -use crate::riemann::{Decomp2, Metric, Tens2}; +use crate::riemann::{Metric, Tens2}; #[derive(Copy, Clone, Debug)] pub struct Tube { @@ -74,9 +75,10 @@ mod test { use glam::{vec2, Vec2}; use itertools_num::linspace; - use crate::riemann::{Decomp2, Metric}; + use crate::riemann::Metric; use crate::tube::Space; use crate::types::Ray; + use refraction::mathx::Decomp2; use super::Tube; diff --git a/src/mathx.rs b/src/mathx.rs index 93136ce..fdf9165 100644 --- a/src/mathx.rs +++ b/src/mathx.rs @@ -1,4 +1,4 @@ -use glam::{FloatExt, Mat2, Mat3}; +use glam::{FloatExt, Mat2, Mat3, Vec2}; mod bounds { pub trait Pair {} @@ -44,6 +44,45 @@ impl MatExt for Mat3 { } } +#[derive(Copy, Clone, Debug, PartialEq)] +pub struct Decomp2 { + pub ortho: Mat2, + pub diag: Vec2, +} + +impl Decomp2 { + pub fn square(&self) -> Self { + Self { + ortho: self.ortho, + diag: self.diag * self.diag, + } + } + + pub fn inverse(&self) -> Self { + Self { + ortho: self.ortho, + diag: Vec2::splat(1.0) / self.diag, + } + } +} + +impl From for Mat2 { + fn from(value: Decomp2) -> Self { + value.ortho.transpose() * Mat2::from_diagonal(value.diag) * value.ortho + } +} + +impl std::ops::Mul for Decomp2 +where + Mat2: std::ops::Mul, +{ + type Output = >::Output; + + fn mul(self, rhs: T) -> Self::Output { + Mat2::from(self) * rhs + } +} + #[cfg(test)] mod tests { use super::*;