From e3d068579cce61891d7ed05dac6874df1d7f5c99 Mon Sep 17 00:00:00 2001 From: numzero Date: Sun, 30 Jun 2024 12:43:16 +0300 Subject: [PATCH] Unify Ray and Location transforms --- src/bin/flat/tube/mod.rs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/bin/flat/tube/mod.rs b/src/bin/flat/tube/mod.rs index 649b59a..81ee9e6 100644 --- a/src/bin/flat/tube/mod.rs +++ b/src/bin/flat/tube/mod.rs @@ -185,7 +185,7 @@ fn test_rect() { mod coords { use glam::{Mat2, Vec2, vec2}; - use crate::riemann::Metric; + use crate::riemann::{Decomp2, Metric}; use crate::types::{Location, Ray}; use super::{Rect, Tube}; @@ -199,40 +199,47 @@ mod coords { fn distance_to_boundary(&self, _ray: Ray) -> Option { None } } - trait MetricCS { + trait MetricCS: FlatCoordinateSystem { fn global_metric(&self) -> &impl Metric; + fn sqrt_at_global(&self, pos: Vec2) -> Decomp2 { + self.global_metric().sqrt_at(pos) + } + fn flat_to_global_tfm_at(&self, pos: Vec2) -> Mat2 { + self.sqrt_at_global(self.flat_to_global(pos)).inverse().into() + } + fn global_to_flat_tfm_at(&self, pos: Vec2) -> Mat2 { + self.sqrt_at_global(pos).into() + } } impl + MetricCS> FlatCoordinateSystem for T { fn flat_to_global(&self, ray: Ray) -> Ray { - let pos = self.flat_to_global(ray.pos); Ray { - pos, - dir: Mat2::from(self.global_metric().sqrt_at(pos).inverse()) * ray.dir, + pos: self.flat_to_global(ray.pos), + dir: self.flat_to_global_tfm_at(ray.pos) * ray.dir, } } fn global_to_flat(&self, ray: Ray) -> Ray { Ray { pos: self.global_to_flat(ray.pos), - dir: Mat2::from(self.global_metric().sqrt_at(ray.pos)) * ray.dir, + dir: self.global_to_flat_tfm_at(ray.pos) * ray.dir, } } } impl + MetricCS> FlatCoordinateSystem for T { fn flat_to_global(&self, loc: Location) -> Location { - let pos = self.flat_to_global(loc.pos); Location { - pos, - rot: Mat2::from(self.global_metric().sqrt_at(pos).inverse()) * loc.rot, + pos: self.flat_to_global(loc.pos), + rot: self.flat_to_global_tfm_at(loc.pos) * loc.rot, } } fn global_to_flat(&self, loc: Location) -> Location { Location { pos: self.global_to_flat(loc.pos), // в плоской СК для Inner или её продолжении на Outer - rot: Mat2::from(self.global_metric().sqrt_at(loc.pos)) * loc.rot, + rot: self.global_to_flat_tfm_at(loc.pos) * loc.rot, } } }