line.rs: destructure self with let, save indentation
This commit is contained in:
parent
c80f0889be
commit
556c5f2375
4 changed files with 83 additions and 92 deletions
61
src/aabb.rs
61
src/aabb.rs
|
@ -201,43 +201,40 @@ impl<S: BaseNum> fmt::Debug for Aabb3<S> {
|
|||
|
||||
impl<S: BaseFloat> Intersect<Option<Point2<S>>> for (Ray2<S>, Aabb2<S>) {
|
||||
fn intersection(&self) -> Option<Point2<S>> {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
82
src/line.rs
82
src/line.rs
|
@ -41,53 +41,51 @@ pub type Line3<S> = Line<Point3<S>>;
|
|||
/// Determines if an intersection between a ray and a line segments is found.
|
||||
impl<S: BaseFloat> Intersect<Option<Point2<S>>> for (Ray2<S>, Line2<S>) {
|
||||
fn intersection(&self) -> Option<Point2<S>> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
12
src/plane.rs
12
src/plane.rs
|
@ -111,13 +111,11 @@ impl<S: BaseFloat> Plane<S> {
|
|||
|
||||
impl<S: BaseFloat> Intersect<Option<Point3<S>>> for (Plane<S>, Ray3<S>) {
|
||||
fn intersection(&self) -> Option<Point3<S>> {
|
||||
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))) }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -31,17 +31,15 @@ pub struct Sphere<S> {
|
|||
|
||||
impl<S: BaseFloat> Intersect<Option<Point3<S>>> for (Sphere<S>, Ray3<S>) {
|
||||
fn intersection(&self) -> Option<Point3<S>> {
|
||||
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)))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue