Formatting cleanups

This commit is contained in:
Brendan Zabarauskas 2013-11-09 12:15:51 +11:00
parent c916b65c6f
commit 9f39bf67b2
7 changed files with 52 additions and 53 deletions

View file

@ -89,8 +89,7 @@ impl<S: Float> Plane<S> {
impl<S: Float> Intersect<Option<Point3<S>>> for (Plane<S>, Ray3<S>) {
fn intersection(&self) -> Option<Point3<S>> {
match *self {
(ref p, ref r) =>
{
(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))) }

View file

@ -57,8 +57,7 @@ impl<S: Num> Point3<S> {
impl<S: Clone + Num + Primitive> Point3<S> {
#[inline]
pub fn from_homogeneous(v: &Vec4<S>) -> Point3<S> {
let _1 :S = one();
let e = v.truncate().mul_s( _1 / v.w );
let e = v.truncate().mul_s(one::<S>() / v.w);
Point3::new(e.x.clone(), e.y.clone(), e.z.clone()) //FIXME
}

View file

@ -36,8 +36,7 @@ pub struct Sphere<S> {
impl<S: Float> Intersect<Option<Point3<S>>> for (Sphere<S>, Ray3<S>) {
fn intersection(&self) -> Option<Point3<S>> {
match *self {
(ref s, ref r) =>
{
(ref s, ref r) => {
let l = s.center.sub_p(&r.origin);
let tca = l.dot(&r.direction);
if tca < cast(0.0) { return None; }

View file

@ -87,6 +87,7 @@ impl<S: Primitive + Clone> Vec3<S> {
pub fn extend(&self, w: S)-> Vec4<S> {
Vec4::new(self.x.clone(), self.y.clone(), self.z.clone(), w)
}
#[inline]
pub fn truncate(&self)-> Vec2<S> {
Vec2::new(self.x.clone(), self.y.clone()) //ignore Z
@ -99,6 +100,7 @@ impl<S: Primitive> Vec4<S> {
#[inline] pub fn unit_z() -> Vec4<S> { Vec4::new(zero(), zero(), one(), zero()) }
#[inline] pub fn unit_w() -> Vec4<S> { Vec4::new(zero(), zero(), zero(), one()) }
}
impl<S: Primitive + Clone> Vec4<S> {
#[inline]
pub fn truncate(&self)-> Vec3<S> {

View file

@ -8,10 +8,10 @@ use std::num;
#[test]
fn test_intersection() {
let sphere = Sphere {center: Point3::new(0f64,0f64,0f64), radius: 1f64};
let r0: Ray3<f64> = Ray::new(Point3::new(0f64, 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize());
let r1: Ray3<f64> = Ray::new(Point3::new(num::cos(1f64), 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize());
let r2: Ray3<f64> = Ray::new(Point3::new(1f64, 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize());
let r3: Ray3<f64> = Ray::new(Point3::new(2f64, 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize());
let r0 = Ray::new(Point3::new(0f64, 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize());
let r1 = Ray::new(Point3::new(num::cos(1f64), 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize());
let r2 = Ray::new(Point3::new(1f64, 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize());
let r3 = Ray::new(Point3::new(2f64, 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize());
assert_eq!((sphere,r0).intersection(), Some(Point3::new(0f64, 0f64, 1f64)));
assert_approx_eq!((sphere,r1).intersection().unwrap(), Point3::new(num::cos(1f64), 0f64, num::sin(1f64)));
assert_eq!((sphere,r2).intersection(), Some(Point3::new(1f64, 0f64, 0f64)));