From 8f3ee253772da5ff12cedf0bbc70da57ef7da002 Mon Sep 17 00:00:00 2001 From: numzero Date: Fri, 10 May 2024 16:06:26 +0300 Subject: [PATCH] Fast boundary finding Unbounded cells start to cause trouble... --- src/bin/flat.rs | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/src/bin/flat.rs b/src/bin/flat.rs index aad0287..687cced 100644 --- a/src/bin/flat.rs +++ b/src/bin/flat.rs @@ -119,13 +119,15 @@ fn draw_ray_2(gc: &mut Vec, space: &impl Metric, grid: &Grid, base: Vec2, gc.dash_length(6.0); gc.new_path(); gc.move_to(p.x, p.y); - let mut ray = cell.ray_to_local(Ray { pos: p, dir: v }); - while cell.is_inside(ray.pos) && !ray.pos.abs().cmpgt(Vec2::splat(1000.0)).any() { - ray = Ray { pos: ray.pos + ray.dir * dt, dir: ray.dir }; - p = cell.pos_to_global(ray.pos); - gc.line_to(p.x, p.y); + let ray = cell.ray_to_local(Ray { pos: p, dir: v }); + if let Some(ray) = cell.to_boundary(ray) { + Ray { pos: p, dir: v } = cell.ray_to_global(ray); + } else { + gc.stroke(); + gc.new_dash_pattern(); + return; } - Ray { pos: p, dir: v } = cell.ray_to_global(ray); + gc.line_to(p.x, p.y); gc.stroke(); gc.new_dash_pattern(); gc.new_path(); @@ -244,6 +246,30 @@ trait FlatCell: std::fmt::Debug { pos.cmpge(bnd.0).all() && pos.cmple(bnd.1).all() } fn local_bounds(&self) -> (Vec2, Vec2); + + fn to_boundary(&self, ray: Ray) -> Option { + assert!(self.is_inside(ray.pos)); + let sgn = ray.dir.signum(); + let p = ray.pos * sgn; + let v = ray.dir * sgn; + let mut bnd = self.local_bounds(); + if sgn.x < 0.0 { + (bnd.0.x, bnd.1.x) = (-bnd.1.x, -bnd.0.x); + } + if sgn.y < 0.0 { + (bnd.0.y, bnd.1.y) = (-bnd.1.y, -bnd.0.y); + } + let t = if (bnd.1.x - p.x) * v.y <= (bnd.1.y - p.y) * v.x { + (bnd.1.x - p.x) / v.x + } else { + (bnd.1.y - p.y) / v.y + }; + if t <= 1000.0 { + Some(Ray { pos: sgn * (p + v * t), dir: sgn * v }) + } else { + None + } + } } #[derive(Debug)]