diff --git a/src/aabb.rs b/src/aabb.rs index b0d1846..a869946 100644 --- a/src/aabb.rs +++ b/src/aabb.rs @@ -201,43 +201,40 @@ impl fmt::Debug for Aabb3 { impl Intersect>> for (Ray2, Aabb2) { fn intersection(&self) -> Option> { - match *self { - (ref ray, ref aabb) => { + let (ref ray, ref aabb) = *self; - let mut tmin: S = Float::neg_infinity(); - let mut tmax: S = Float::infinity(); + let mut tmin: S = Float::neg_infinity(); + let mut tmax: S = Float::infinity(); - if ray.direction.x != zero() { - let tx1 = (aabb.min.x - ray.origin.x) / ray.direction.x; - let tx2 = (aabb.max.x - ray.origin.x) / ray.direction.x; - tmin = tmin.max(tx1.min(tx2)); - tmax = tmax.min(tx1.max(tx2)); - } + if ray.direction.x != zero() { + let tx1 = (aabb.min.x - ray.origin.x) / ray.direction.x; + let tx2 = (aabb.max.x - ray.origin.x) / ray.direction.x; + tmin = tmin.max(tx1.min(tx2)); + tmax = tmax.min(tx1.max(tx2)); + } - if ray.direction.y != zero() { - let ty1 = (aabb.min.y - ray.origin.y) / ray.direction.y; - let ty2 = (aabb.max.y - ray.origin.y) / ray.direction.y; - tmin = tmin.max(ty1.min(ty2)); - tmax = tmax.min(ty1.max(ty2)); - } + if ray.direction.y != zero() { + let ty1 = (aabb.min.y - ray.origin.y) / ray.direction.y; + let ty2 = (aabb.max.y - ray.origin.y) / ray.direction.y; + tmin = tmin.max(ty1.min(ty2)); + tmax = tmax.min(ty1.max(ty2)); + } - if tmin < zero() && tmax < zero() { - None - } - else if tmax >= tmin { - if tmin >= zero() { - Some(Point2::new(ray.origin.x + ray.direction.x * tmin, - ray.origin.y + ray.direction.y * tmin)) - } - else { - Some(Point2::new(ray.origin.x + ray.direction.x * tmax, - ray.origin.y + ray.direction.y * tmax)) - } - } - else { - None - } + if tmin < zero() && tmax < zero() { + None + } + else if tmax >= tmin { + if tmin >= zero() { + Some(Point2::new(ray.origin.x + ray.direction.x * tmin, + ray.origin.y + ray.direction.y * tmin)) } + else { + Some(Point2::new(ray.origin.x + ray.direction.x * tmax, + ray.origin.y + ray.direction.y * tmax)) + } + } + else { + None } } } diff --git a/src/line.rs b/src/line.rs index f225071..872f960 100644 --- a/src/line.rs +++ b/src/line.rs @@ -41,53 +41,51 @@ pub type Line3 = Line>; /// Determines if an intersection between a ray and a line segments is found. impl Intersect>> for (Ray2, Line2) { fn intersection(&self) -> Option> { - match *self { - (ref ray, ref line) => { - let p = ray.origin; - let q = line.origin; - let r = ray.direction; - let s = Vector2::new(line.dest.x - line.origin.x, line.dest.y - line.origin.y); - let zero: S = Zero::zero(); + let (ref ray, ref line) = *self; - let cross_1 = r.perp_dot(&s); - let qmp = Vector2::new(q.x - p.x, q.y - p.y); - let cross_2 = qmp.perp_dot(&r); + let p = ray.origin; + let q = line.origin; + let r = ray.direction; + let s = Vector2::new(line.dest.x - line.origin.x, line.dest.y - line.origin.y); + let zero: S = Zero::zero(); - if cross_1 == zero { - if cross_2 != zero { - // parallel - return None; - } - - // collinear - let q2mp = Vector2::new(line.dest.x - p.x, line.dest.y - p.y); - let dot_1 = qmp.dot(&r); - let dot_2 = q2mp.dot(&r); - if (dot_1 <= zero && dot_2 >= zero) || (dot_1 >= zero && dot_2 <= zero) { - return Some(p); - } - else if dot_1 >= zero && dot_2 >= zero { - if dot_1 <= dot_2 { - return Some(q); - } - else { - return Some(line.dest); - } - } - - // no overlap exists - return None; - } - - let t = qmp.perp_dot(&s) / cross_1; - let u = cross_2 / cross_1; - - if zero <= t && u >= zero && u <= One::one() { - return Some(Point2::new(p.x + t*r.x, p.y + t*r.y)); - } + let cross_1 = r.perp_dot(&s); + let qmp = Vector2::new(q.x - p.x, q.y - p.y); + let cross_2 = qmp.perp_dot(&r); + if cross_1 == zero { + if cross_2 != zero { + // parallel return None; } + + // collinear + let q2mp = Vector2::new(line.dest.x - p.x, line.dest.y - p.y); + let dot_1 = qmp.dot(&r); + let dot_2 = q2mp.dot(&r); + if (dot_1 <= zero && dot_2 >= zero) || (dot_1 >= zero && dot_2 <= zero) { + return Some(p); + } + else if dot_1 >= zero && dot_2 >= zero { + if dot_1 <= dot_2 { + return Some(q); + } + else { + return Some(line.dest); + } + } + + // no overlap exists + return None; } + + let t = qmp.perp_dot(&s) / cross_1; + let u = cross_2 / cross_1; + + if zero <= t && u >= zero && u <= One::one() { + return Some(Point2::new(p.x + t*r.x, p.y + t*r.y)); + } + + return None; } } diff --git a/src/plane.rs b/src/plane.rs index 909045e..636c7a6 100644 --- a/src/plane.rs +++ b/src/plane.rs @@ -111,13 +111,11 @@ impl Plane { impl Intersect>> for (Plane, Ray3) { fn intersection(&self) -> Option> { - match *self { - (ref p, ref r) => { - let t = -(p.d + r.origin.dot(&p.n)) / r.direction.dot(&p.n); - if t < Zero::zero() { None } - else { Some(r.origin.add_v(&r.direction.mul_s(t))) } - } - } + let (ref p, ref r) = *self; + + let t = -(p.d + r.origin.dot(&p.n)) / r.direction.dot(&p.n); + if t < Zero::zero() { None } + else { Some(r.origin.add_v(&r.direction.mul_s(t))) } } } diff --git a/src/sphere.rs b/src/sphere.rs index 61565ac..2f07ca4 100644 --- a/src/sphere.rs +++ b/src/sphere.rs @@ -31,17 +31,15 @@ pub struct Sphere { impl Intersect>> for (Sphere, Ray3) { fn intersection(&self) -> Option> { - match *self { - (ref s, ref r) => { - let l = s.center.sub_p(&r.origin); - let tca = l.dot(&r.direction); - if tca < zero() { return None; } - let d2 = l.dot(&l) - tca*tca; - if d2 > s.radius*s.radius { return None; } - let thc = (s.radius*s.radius - d2).sqrt(); - Some(r.origin.add_v(&r.direction.mul_s(tca - thc))) - } - } + let (ref s, ref r) = *self; + + let l = s.center.sub_p(&r.origin); + let tca = l.dot(&r.direction); + if tca < zero() { return None; } + let d2 = l.dot(&l) - tca*tca; + if d2 > s.radius*s.radius { return None; } + let thc = (s.radius*s.radius - d2).sqrt(); + Some(r.origin.add_v(&r.direction.mul_s(tca - thc))) } }