From 9ce8da5f380ac550691766b105749f7f028cc35d Mon Sep 17 00:00:00 2001 From: numzero Date: Sun, 28 Apr 2024 23:37:05 +0300 Subject: [PATCH] Traity! --- src/bin/flat.rs | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/bin/flat.rs b/src/bin/flat.rs index 6777c3d..5818007 100644 --- a/src/bin/flat.rs +++ b/src/bin/flat.rs @@ -86,19 +86,39 @@ mod riemann { pub diag: Vec2, } + impl Decomp2 { + fn square(&self) -> Self { + Self { + ortho: self.ortho, + diag: pow2(self.diag), + } + } + + fn inverse(&self) -> Self { + Self { + ortho: self.ortho, + diag: Vec2::from_s(1.0) / self.diag, + } + } + } + + impl From for Mat2 { + fn from(value: Decomp2) -> Self { + transpose(&value.ortho) * diagonal(value.diag) * value.ortho + } + } + type Tens2 = [Mat2; 2]; pub trait Metric { fn halfmetric(&self, pos: Vec2) -> Decomp2; fn metric(&self, pos: Vec2) -> Mat2 { - let h = self.halfmetric(pos); - transpose(&h.ortho) * diagonal(pow2(h.diag)) * h.ortho + self.halfmetric(pos).square().into() } fn invmetric(&self, pos: Vec2) -> Mat2 { - let h = self.halfmetric(pos); - transpose(&h.ortho) * diagonal(Vec2::from_s(1.0) / pow2(h.diag)) * h.ortho + self.halfmetric(pos).square().inverse().into() } fn dmetric(&self, pos: Vec2) -> Tens2 { @@ -114,8 +134,7 @@ mod riemann { } fn globalize(&self, at: Vec2, v: Vec2) -> Vec2 { - let h = self.halfmetric(at); - transpose(&h.ortho) * diagonal(Vec2::from_s(1.0) / h.diag) * h.ortho * v + Mat2::from(self.halfmetric(at).inverse()) * v } }