Fast boundary finding

Unbounded cells start to cause trouble...
This commit is contained in:
numzero 2024-05-10 16:06:26 +03:00
parent 6f77a31727
commit 8f3ee25377

View File

@ -119,13 +119,15 @@ fn draw_ray_2(gc: &mut Vec<Draw>, space: &impl Metric, grid: &Grid, base: Vec2,
gc.dash_length(6.0); gc.dash_length(6.0);
gc.new_path(); gc.new_path();
gc.move_to(p.x, p.y); gc.move_to(p.x, p.y);
let mut ray = cell.ray_to_local(Ray { pos: p, dir: v }); let 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() { if let Some(ray) = cell.to_boundary(ray) {
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);
}
Ray { pos: p, dir: v } = cell.ray_to_global(ray); Ray { pos: p, dir: v } = cell.ray_to_global(ray);
} else {
gc.stroke();
gc.new_dash_pattern();
return;
}
gc.line_to(p.x, p.y);
gc.stroke(); gc.stroke();
gc.new_dash_pattern(); gc.new_dash_pattern();
gc.new_path(); gc.new_path();
@ -244,6 +246,30 @@ trait FlatCell: std::fmt::Debug {
pos.cmpge(bnd.0).all() && pos.cmple(bnd.1).all() pos.cmpge(bnd.0).all() && pos.cmple(bnd.1).all()
} }
fn local_bounds(&self) -> (Vec2, Vec2); fn local_bounds(&self) -> (Vec2, Vec2);
fn to_boundary(&self, ray: Ray) -> Option<Ray> {
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)] #[derive(Debug)]