diff --git a/src/bin/flat/main.rs b/src/bin/flat/main.rs index a9f8119..06e4e2e 100644 --- a/src/bin/flat/main.rs +++ b/src/bin/flat/main.rs @@ -166,13 +166,13 @@ fn draw_ray_2(gc: &mut Vec, space: &Space, camera: Location, dir: Vec3) { gc.new_path(); gc.move_to(pos.x, pos.y); for pt in &path.points[1..] { - gc.line_to(pt.x, pt.y); + gc.line_to(pt.pos.x, pt.pos.y); } let end_pos = *path .points .last() .expect("the starting point is always in the path"); - let dir_pos = end_pos + 1000.0 * path.end_dir; + let dir_pos = end_pos.forward(1000. / DT).pos; gc.line_to(dir_pos.x, dir_pos.y); gc.stroke(); } diff --git a/src/bin/wireframe/scene.rs b/src/bin/wireframe/scene.rs index 9c67ed3..56cdc32 100644 --- a/src/bin/wireframe/scene.rs +++ b/src/bin/wireframe/scene.rs @@ -1,5 +1,5 @@ use glam::*; -use itertools::{chain, iproduct}; +use itertools::{chain, iproduct, Itertools}; use refraction::ifaces::{DebugTraceable, Traceable}; use refraction::tube::metric::Tube; @@ -107,9 +107,9 @@ fn draw_ray_2(gc: &mut Vec, space: &Space, camera: Location, dir: Vec3) { let end_pos = *pts .last() .expect("the starting point is always in the path"); - let dir_pos = end_pos + 10000.0 * path.end_dir; + let dir_pos = end_pos.forward(10000.0); pts.push(dir_pos); - gc.push(Line::Strip(pts)); + gc.push(Line::Strip(pts.into_iter().map(|r| r.pos).collect())); } fn draw_fan_2(space: &Space, camera: Location, spread: f32) -> Vec { diff --git a/src/ifaces.rs b/src/ifaces.rs index d8d3e4c..a293f0c 100644 --- a/src/ifaces.rs +++ b/src/ifaces.rs @@ -1,5 +1,4 @@ use crate::types::{Hit, Location, Ray}; -use glam::Vec3; pub trait Traceable { /// Traces a ray from a given starting point. `ray` is relative to the camera. @@ -19,8 +18,7 @@ pub trait OptimizedTraceable: Traceable { } pub struct RayPath { - pub points: Vec, - pub end_dir: Vec3, + pub points: Vec, } pub trait DebugTraceable: Traceable { diff --git a/src/tube/mod.rs b/src/tube/mod.rs index e7e31af..797e493 100644 --- a/src/tube/mod.rs +++ b/src/tube/mod.rs @@ -146,16 +146,25 @@ impl Space { .collect() } - pub fn line(&self, a: Vec3, b: Vec3, step: f32) -> Vec { + pub fn line(&self, a: Vec3, b: Vec3, step: f32) -> Vec { match self.which_subspace(a) { - Outer => vec![b], + Outer => vec![Ray { + pos: b, + dir: (b - a).normalize(), + }], Inner => { let cs = InnerCS(self.tube); let n = ((b - a).length() / step) as usize + 1; let a = cs.global_to_flat(a); let b = cs.global_to_flat(b); + let dir = (b - a).normalize(); (1..=n) - .map(|k| cs.flat_to_global(a.lerp(b, k as f32 / n as f32))) + .map(|k| { + cs.flat_to_global(Ray { + pos: a.lerp(b, k as f32 / n as f32), + dir, + }) + }) .collect() } Boundary => panic!("Can't draw a line here!"), @@ -210,9 +219,9 @@ impl DebugTraceable for Space { let mut hits = vec![]; let mut ray = self.camera_ray_to_abs(camera, ray); - let trace_to_flat = |points: &mut Vec, ray| { + let trace_to_flat = |points: &mut Vec, ray| { for ray in self.trace_iter(ray).skip(1) { - points.push(ray.pos); + points.push(ray); if let Some(hitter) = self.obj_hitter(ray.pos) { return (ray, hitter(self, ray)); } @@ -220,18 +229,12 @@ impl DebugTraceable for Space { unreachable!("Space::trace_iter terminated!") }; - points.push(ray.pos); + points.push(ray); for _ in 0..100 { let (ray_into_flat, ret) = trace_to_flat(&mut points, ray); hits.extend(ret.objects); // TODO fix distance let Some(ray_outta_flat) = ret.end else { - return ( - hits, - RayPath { - points, - end_dir: ray_into_flat.dir.normalize(), - }, - ); + return (hits, RayPath { points }); }; points.extend(self.line(ray_into_flat.pos, ray_outta_flat.pos, 10.0)); ray = ray_outta_flat;