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>) {
|
impl<S: BaseFloat> Intersect<Option<Point2<S>>> for (Ray2<S>, Aabb2<S>) {
|
||||||
fn intersection(&self) -> Option<Point2<S>> {
|
fn intersection(&self) -> Option<Point2<S>> {
|
||||||
match *self {
|
let (ref ray, ref aabb) = *self;
|
||||||
(ref ray, ref aabb) => {
|
|
||||||
|
|
||||||
let mut tmin: S = Float::neg_infinity();
|
let mut tmin: S = Float::neg_infinity();
|
||||||
let mut tmax: S = Float::infinity();
|
let mut tmax: S = Float::infinity();
|
||||||
|
|
||||||
if ray.direction.x != zero() {
|
if ray.direction.x != zero() {
|
||||||
let tx1 = (aabb.min.x - ray.origin.x) / ray.direction.x;
|
let tx1 = (aabb.min.x - ray.origin.x) / ray.direction.x;
|
||||||
let tx2 = (aabb.max.x - ray.origin.x) / ray.direction.x;
|
let tx2 = (aabb.max.x - ray.origin.x) / ray.direction.x;
|
||||||
tmin = tmin.max(tx1.min(tx2));
|
tmin = tmin.max(tx1.min(tx2));
|
||||||
tmax = tmax.min(tx1.max(tx2));
|
tmax = tmax.min(tx1.max(tx2));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ray.direction.y != zero() {
|
if ray.direction.y != zero() {
|
||||||
let ty1 = (aabb.min.y - ray.origin.y) / ray.direction.y;
|
let ty1 = (aabb.min.y - ray.origin.y) / ray.direction.y;
|
||||||
let ty2 = (aabb.max.y - ray.origin.y) / ray.direction.y;
|
let ty2 = (aabb.max.y - ray.origin.y) / ray.direction.y;
|
||||||
tmin = tmin.max(ty1.min(ty2));
|
tmin = tmin.max(ty1.min(ty2));
|
||||||
tmax = tmax.min(ty1.max(ty2));
|
tmax = tmax.min(ty1.max(ty2));
|
||||||
}
|
}
|
||||||
|
|
||||||
if tmin < zero() && tmax < zero() {
|
if tmin < zero() && tmax < zero() {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
else if tmax >= tmin {
|
else if tmax >= tmin {
|
||||||
if tmin >= zero() {
|
if tmin >= zero() {
|
||||||
Some(Point2::new(ray.origin.x + ray.direction.x * tmin,
|
Some(Point2::new(ray.origin.x + ray.direction.x * tmin,
|
||||||
ray.origin.y + ray.direction.y * 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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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.
|
/// 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>) {
|
impl<S: BaseFloat> Intersect<Option<Point2<S>>> for (Ray2<S>, Line2<S>) {
|
||||||
fn intersection(&self) -> Option<Point2<S>> {
|
fn intersection(&self) -> Option<Point2<S>> {
|
||||||
match *self {
|
let (ref ray, ref line) = *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 cross_1 = r.perp_dot(&s);
|
let p = ray.origin;
|
||||||
let qmp = Vector2::new(q.x - p.x, q.y - p.y);
|
let q = line.origin;
|
||||||
let cross_2 = qmp.perp_dot(&r);
|
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 {
|
let cross_1 = r.perp_dot(&s);
|
||||||
if cross_2 != zero {
|
let qmp = Vector2::new(q.x - p.x, q.y - p.y);
|
||||||
// parallel
|
let cross_2 = qmp.perp_dot(&r);
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if cross_1 == zero {
|
||||||
|
if cross_2 != zero {
|
||||||
|
// parallel
|
||||||
return None;
|
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>) {
|
impl<S: BaseFloat> Intersect<Option<Point3<S>>> for (Plane<S>, Ray3<S>) {
|
||||||
fn intersection(&self) -> Option<Point3<S>> {
|
fn intersection(&self) -> Option<Point3<S>> {
|
||||||
match *self {
|
let (ref p, ref r) = *self;
|
||||||
(ref p, ref r) => {
|
|
||||||
let t = -(p.d + r.origin.dot(&p.n)) / r.direction.dot(&p.n);
|
let t = -(p.d + r.origin.dot(&p.n)) / r.direction.dot(&p.n);
|
||||||
if t < Zero::zero() { None }
|
if t < Zero::zero() { None }
|
||||||
else { Some(r.origin.add_v(&r.direction.mul_s(t))) }
|
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>) {
|
impl<S: BaseFloat> Intersect<Option<Point3<S>>> for (Sphere<S>, Ray3<S>) {
|
||||||
fn intersection(&self) -> Option<Point3<S>> {
|
fn intersection(&self) -> Option<Point3<S>> {
|
||||||
match *self {
|
let (ref s, ref r) = *self;
|
||||||
(ref s, ref r) => {
|
|
||||||
let l = s.center.sub_p(&r.origin);
|
let l = s.center.sub_p(&r.origin);
|
||||||
let tca = l.dot(&r.direction);
|
let tca = l.dot(&r.direction);
|
||||||
if tca < zero() { return None; }
|
if tca < zero() { return None; }
|
||||||
let d2 = l.dot(&l) - tca*tca;
|
let d2 = l.dot(&l) - tca*tca;
|
||||||
if d2 > s.radius*s.radius { return None; }
|
if d2 > s.radius*s.radius { return None; }
|
||||||
let thc = (s.radius*s.radius - d2).sqrt();
|
let thc = (s.radius*s.radius - d2).sqrt();
|
||||||
Some(r.origin.add_v(&r.direction.mul_s(tca - thc)))
|
Some(r.origin.add_v(&r.direction.mul_s(tca - thc)))
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue