From a31a950eca34c18f4d6792cbb3ce437db8fde275 Mon Sep 17 00:00:00 2001 From: numzero Date: Tue, 25 Jun 2024 19:55:45 +0300 Subject: [PATCH] Unify coordinate mapping --- src/bin/flat/tube/mod.rs | 76 +++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 40 deletions(-) diff --git a/src/bin/flat/tube/mod.rs b/src/bin/flat/tube/mod.rs index 07c8955..ff7a7f3 100644 --- a/src/bin/flat/tube/mod.rs +++ b/src/bin/flat/tube/mod.rs @@ -3,6 +3,7 @@ use crate::riemann; use crate::riemann::Metric; use Subspace::{Boundary, Inner, Outer}; use metric::Tube; +use coords::Mapper; use crate::types::{FlatTraceResult, Hit, Location, Object, Ray}; pub mod metric; @@ -63,13 +64,14 @@ impl Space { pub fn trace_inner(&self, ray: Ray) -> FlatTraceResult { assert_eq!(self.which_subspace(ray.pos), Inner); - let cell = TubeInside { tube: self.tube }; - let ray = cell.ray_to_local(ray); + let size = vec2(self.tube.inner_radius, self.tube.internal_halflength); + let ray = self.tube.outer_to_inner(ray); + assert!(ray.pos.abs().cmple(size).all()); let objs = self.list_objects_inner(); - let dist = cell.to_boundary(ray).expect("Can't get outta here!"); + let dist = Rect { size }.trace_out_of(ray).expect("Can't get outta here!"); FlatTraceResult { - end: Some(cell.ray_to_global(ray.forward(dist))), - objects: Self::hit_objects(objs.as_slice(), ray, dist, |pos| cell.pos_to_global(pos)), + end: Some(self.tube.inner_to_outer(ray.forward(dist))), + objects: Self::hit_objects(objs.as_slice(), ray, dist, |pos| self.tube.inner_to_outer(pos)), } } @@ -151,11 +153,10 @@ impl Space { match self.which_subspace(a) { Outer => vec![b], Inner => { - let cell = TubeInside { tube: self.tube }; let n = ((b - a).length() / step) as usize + 1; - let a = cell.pos_to_local(a); - let b = cell.pos_to_local(b); - (1..=n).map(|k| cell.pos_to_global(a.lerp(b, k as f32 / n as f32))).collect() + let a = self.tube.outer_to_inner(a); + let b = self.tube.outer_to_inner(b); + (1..=n).map(|k| self.tube.inner_to_outer(a.lerp(b, k as f32 / n as f32))).collect() } Boundary => panic!("Can't draw a line here!"), } @@ -223,44 +224,39 @@ fn test_rect() { assert_eq!(r.trace_out_of(Ray { pos: vec2(2.0, 3.0), dir: vec2(1.0, 1.0) }), Some(0.0)); } -#[derive(Debug)] -struct TubeInside { - tube: Tube, -} +mod coords { + use glam::{Vec2, vec2}; + use crate::types::Ray; + use super::Tube; -impl TubeInside { - fn is_inside(&self, pos: Vec2) -> bool { - pos.abs().cmple(self.size()).all() + pub trait Mapper { + fn inner_to_outer(self, v: T) -> T; + fn outer_to_inner(self, v: T) -> T; } - fn to_boundary(&self, ray: Ray) -> Option { - assert!(self.is_inside(ray.pos)); - Rect { size: self.size() }.trace_out_of(ray) - } + impl Mapper for Tube { + fn inner_to_outer(self, pos: Vec2) -> Vec2 { + vec2(pos.x, self.y(pos.y)) + } - fn pos_to_global(&self, pos: Vec2) -> Vec2 { - vec2(pos.x, self.tube.y(pos.y)) - } - - fn pos_to_local(&self, pos: Vec2) -> Vec2 { - vec2(pos.x, self.tube.v(pos.y)) - } - - fn ray_to_global(&self, ray: Ray) -> Ray { - Ray { - pos: self.pos_to_global(ray.pos), - dir: vec2(ray.dir.x, self.tube.dy(ray.pos.y) * ray.dir.y), + fn outer_to_inner(self, pos: Vec2) -> Vec2 { + vec2(pos.x, self.v(pos.y)) } } - fn ray_to_local(&self, ray: Ray) -> Ray { - Ray { - pos: self.pos_to_local(ray.pos), - dir: vec2(ray.dir.x, self.tube.dv(ray.pos.y) * ray.dir.y), + impl Mapper for Tube { + fn inner_to_outer(self, ray: Ray) -> Ray { + Ray { + pos: self.inner_to_outer(ray.pos), + dir: vec2(ray.dir.x, self.dy(ray.pos.y) * ray.dir.y), + } + } + + fn outer_to_inner(self, ray: Ray) -> Ray { + Ray { + pos: self.outer_to_inner(ray.pos), + dir: vec2(ray.dir.x, self.dv(ray.pos.y) * ray.dir.y), + } } } - - fn size(&self) -> Vec2 { - vec2(self.tube.inner_radius, self.tube.internal_halflength) - } }