Simplify code

This commit is contained in:
numzero 2024-06-25 13:31:39 +03:00
parent 4caa260a34
commit 2515c0a0da

View File

@ -194,10 +194,6 @@ impl Rect {
let t = ts.min_element(); let t = ts.min_element();
Some(t) Some(t)
} }
fn visualise(&self) -> Vec<Vec2> {
vec![vec2(-self.size.x, -self.size.y), vec2(self.size.x, -self.size.y), vec2(self.size.x, self.size.y), vec2(-self.size.x, self.size.y)]
}
} }
#[test] #[test]
@ -234,32 +230,12 @@ struct TubeInside {
impl TubeInside { impl TubeInside {
fn is_inside(&self, pos: Vec2) -> bool { fn is_inside(&self, pos: Vec2) -> bool {
let bnd = self.local_bounds(); pos.abs().cmple(self.size()).all()
pos.cmpge(bnd.0).all() && pos.cmple(bnd.1).all()
} }
fn to_boundary(&self, ray: Ray) -> Option<f32> { fn to_boundary(&self, ray: Ray) -> Option<f32> {
assert!(self.is_inside(ray.pos)); assert!(self.is_inside(ray.pos));
let sgn = ray.dir.signum(); Rect { size: self.size() }.trace_out_of(ray)
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 <= 100000.0 {
Some(t)
} else {
None
}
} }
fn pos_to_global(&self, pos: Vec2) -> Vec2 { fn pos_to_global(&self, pos: Vec2) -> Vec2 {
@ -284,7 +260,7 @@ impl TubeInside {
} }
} }
fn local_bounds(&self) -> (Vec2, Vec2) { fn size(&self) -> Vec2 {
(vec2(-self.tube.inner_radius, -self.tube.internal_halflength), vec2(self.tube.inner_radius, self.tube.internal_halflength)) vec2(self.tube.inner_radius, self.tube.internal_halflength)
} }
} }