Unify Ray and Location transforms

This commit is contained in:
numzero 2024-06-30 12:43:16 +03:00
parent 150f81f03b
commit e3d068579c

View File

@ -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<f32> { None }
}
trait MetricCS {
trait MetricCS: FlatCoordinateSystem<Vec2> {
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<T: FlatCoordinateSystem<Vec2> + MetricCS> FlatCoordinateSystem<Ray> 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<T: FlatCoordinateSystem<Vec2> + MetricCS> FlatCoordinateSystem<Location> 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,
}
}
}