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

View file

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

View file

@ -38,12 +38,12 @@ pub trait Rotation
fn rotate_vec(&self, vec: &V) -> V; fn rotate_vec(&self, vec: &V) -> V;
#[inline] #[inline]
fn rotate_point(&self, point: &P) -> P { fn rotate_point(&self, point: &P) -> P {
Point::from_vec( &self.rotate_vec( &point.to_vec() ) ) Point::from_vec( &self.rotate_vec( &point.to_vec() ) )
} }
#[inline] #[inline]
fn rotate_ray(&self, ray: &Ray<P,V>) -> Ray<P,V> { fn rotate_ray(&self, ray: &Ray<P,V>) -> Ray<P,V> {
Ray::new( //FIXME: use clone derived from Array Ray::new( //FIXME: use clone derived from Array
Array::build(|i| ray.origin.i(i).clone()), Array::build(|i| ray.origin.i(i).clone()),
self.rotate_vec(&ray.direction) ) self.rotate_vec(&ray.direction) )
@ -58,7 +58,7 @@ pub trait Rotation
} }
#[inline] #[inline]
fn invert_self(&mut self) { fn invert_self(&mut self) {
*self = self.invert(); *self = self.invert();
} }
} }
@ -117,7 +117,7 @@ impl<S: Float> ToMat2<S> for Basis2<S> {
impl<S: Float> Rotation<S, [S, ..2], Vec2<S>, Point2<S>> for Basis2<S> { impl<S: Float> Rotation<S, [S, ..2], Vec2<S>, Point2<S>> for Basis2<S> {
#[inline] #[inline]
fn identity() -> Basis2<S> { Basis2{ mat: Mat2::identity() } } fn identity() -> Basis2<S> { Basis2{ mat: Mat2::identity() } }
#[inline] #[inline]
fn rotate_vec(&self, vec: &Vec2<S>) -> Vec2<S> { self.mat.mul_v(vec) } fn rotate_vec(&self, vec: &Vec2<S>) -> Vec2<S> { self.mat.mul_v(vec) }
@ -157,7 +157,7 @@ impl<S: Float> ApproxEq<S> for Basis2<S> {
} }
} }
impl<S: Float> Rotation2<S> for Basis2<S> {} impl<S: Float> Rotation2<S> for Basis2<S> {}
/// A three-dimensional rotation matrix. /// A three-dimensional rotation matrix.
/// ///
@ -232,7 +232,7 @@ impl<S: Float> ToQuat<S> for Basis3<S> {
impl<S: Float> Rotation<S, [S, ..3], Vec3<S>, Point3<S>> for Basis3<S> { impl<S: Float> Rotation<S, [S, ..3], Vec3<S>, Point3<S>> for Basis3<S> {
#[inline] #[inline]
fn identity() -> Basis3<S> { Basis3{ mat: Mat3::identity() } } fn identity() -> Basis3<S> { Basis3{ mat: Mat3::identity() } }
#[inline] #[inline]
fn rotate_vec(&self, vec: &Vec3<S>) -> Vec3<S> { self.mat.mul_v(vec) } fn rotate_vec(&self, vec: &Vec3<S>) -> Vec3<S> { self.mat.mul_v(vec) }
@ -272,7 +272,7 @@ impl<S: Float> ApproxEq<S> for Basis3<S> {
} }
} }
impl<S: Float> Rotation3<S> for Basis3<S> {} impl<S: Float> Rotation3<S> for Basis3<S> {}
// Quaternion Rotation impls // Quaternion Rotation impls
@ -288,7 +288,7 @@ impl<S: Float> ToQuat<S> for Quat<S> {
impl<S: Float> Rotation<S, [S, ..3], Vec3<S>, Point3<S>> for Quat<S> { impl<S: Float> Rotation<S, [S, ..3], Vec3<S>, Point3<S>> for Quat<S> {
#[inline] #[inline]
fn identity() -> Quat<S> { Quat::identity() } fn identity() -> Quat<S> { Quat::identity() }
#[inline] #[inline]
fn rotate_vec(&self, vec: &Vec3<S>) -> Vec3<S> { self.mul_v(vec) } fn rotate_vec(&self, vec: &Vec3<S>) -> Vec3<S> { self.mul_v(vec) }

View file

@ -36,8 +36,7 @@ pub struct Sphere<S> {
impl<S: Float> Intersect<Option<Point3<S>>> for (Sphere<S>, Ray3<S>) { impl<S: Float> Intersect<Option<Point3<S>>> for (Sphere<S>, Ray3<S>) {
fn intersection(&self) -> Option<Point3<S>> { fn intersection(&self) -> Option<Point3<S>> {
match *self { match *self {
(ref s, ref r) => (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 < cast(0.0) { return None; } if tca < cast(0.0) { return None; }

View file

@ -37,12 +37,12 @@ pub trait Transform
fn transform_point(&self, point: &P) -> P; fn transform_point(&self, point: &P) -> P;
#[inline] #[inline]
fn transform_ray(&self, ray: &Ray<P,V>) -> Ray<P,V> { fn transform_ray(&self, ray: &Ray<P,V>) -> Ray<P,V> {
Ray::new( self.transform_point(&ray.origin), self.transform_vec(&ray.direction) ) Ray::new( self.transform_point(&ray.origin), self.transform_vec(&ray.direction) )
} }
#[inline] #[inline]
fn transform_as_point(&self, vec: &V)-> V { fn transform_as_point(&self, vec: &V)-> V {
self.transform_point( &Point::from_vec(vec) ).to_vec() self.transform_point( &Point::from_vec(vec) ).to_vec()
} }
@ -55,7 +55,7 @@ pub trait Transform
} }
#[inline] #[inline]
fn invert_self(&mut self)-> bool { fn invert_self(&mut self)-> bool {
match self.invert() { match self.invert() {
Some(t) => {*self = t; true}, Some(t) => {*self = t; true},
None => false, None => false,
@ -65,7 +65,7 @@ pub trait Transform
/// A generic transformation consisting of a rotation, /// A generic transformation consisting of a rotation,
/// displacement vector and scale amount. /// displacement vector and scale amount.
pub struct Decomposed<S,V,R> { pub struct Decomposed<S,V,R> {
scale: S, scale: S,
rot: R, rot: R,
disp: V, disp: V,
@ -79,9 +79,9 @@ impl
P: Point<S, V, Slice>, P: Point<S, V, Slice>,
R: Rotation<S, Slice, V, P> R: Rotation<S, Slice, V, P>
> >
Transform<S, Slice, V, P> for Decomposed<S,V,R> { Transform<S, Slice, V, P> for Decomposed<S,V,R> {
#[inline] #[inline]
fn identity() -> Decomposed<S,V,R> { fn identity() -> Decomposed<S,V,R> {
Decomposed { Decomposed {
scale: num::one(), scale: num::one(),
rot: Rotation::identity(), rot: Rotation::identity(),
@ -90,16 +90,16 @@ Transform<S, Slice, V, P> for Decomposed<S,V,R> {
} }
#[inline] #[inline]
fn transform_vec(&self, vec: &V) -> V { fn transform_vec(&self, vec: &V) -> V {
self.rot.rotate_vec( &vec.mul_s( self.scale.clone() )) self.rot.rotate_vec( &vec.mul_s( self.scale.clone() ))
} }
#[inline] #[inline]
fn transform_point(&self, point: &P) -> P { fn transform_point(&self, point: &P) -> P {
self.rot.rotate_point( &point.mul_s( self.scale.clone() )).add_v( &self.disp ) self.rot.rotate_point( &point.mul_s( self.scale.clone() )).add_v( &self.disp )
} }
fn concat(&self, other: &Decomposed<S,V,R>) -> Decomposed<S,V,R> { fn concat(&self, other: &Decomposed<S,V,R>) -> Decomposed<S,V,R> {
Decomposed { Decomposed {
scale: self.scale * other.scale, scale: self.scale * other.scale,
rot: self.rot.concat( &other.rot ), rot: self.rot.concat( &other.rot ),
@ -107,10 +107,10 @@ Transform<S, Slice, V, P> for Decomposed<S,V,R> {
} }
} }
fn invert(&self) -> Option<Decomposed<S,V,R>> { fn invert(&self) -> Option<Decomposed<S,V,R>> {
if self.scale.approx_eq( &num::zero() ) { if self.scale.approx_eq( &num::zero() ) {
None None
}else { }else {
let _1 : S = num::one(); let _1 : S = num::one();
let s = _1 / self.scale; let s = _1 / self.scale;
let r = self.rot.invert(); let r = self.rot.invert();
@ -131,7 +131,7 @@ pub trait Transform3<S>
impl<S: Float + Clone, R: Rotation3<S>> impl<S: Float + Clone, R: Rotation3<S>>
ToMat4<S> for Decomposed<S, Vec3<S>, R> { ToMat4<S> for Decomposed<S, Vec3<S>, R> {
fn to_mat4(&self) -> Mat4<S> { fn to_mat4(&self) -> Mat4<S> {
let mut m = self.rot.to_mat3().mul_s( self.scale.clone() ).to_mat4(); let mut m = self.rot.to_mat3().mul_s( self.scale.clone() ).to_mat4();
m.w = self.disp.extend( num::one() ); m.w = self.disp.extend( num::one() );
m m
@ -139,7 +139,7 @@ ToMat4<S> for Decomposed<S, Vec3<S>, R> {
} }
impl<S: Float, R: Rotation3<S>> impl<S: Float, R: Rotation3<S>>
Transform3<S> for Decomposed<S,Vec3<S>,R> {} Transform3<S> for Decomposed<S,Vec3<S>,R> {}
impl<S: fmt::Default + Float, R: ToStr + Rotation3<S>> impl<S: fmt::Default + Float, R: ToStr + Rotation3<S>>
ToStr for Decomposed<S,Vec3<S>,R> { ToStr for Decomposed<S,Vec3<S>,R> {
@ -156,40 +156,40 @@ pub struct AffineMatrix3<S> {
} }
impl<S : Clone + Float> impl<S : Clone + Float>
Transform<S, [S, ..3], Vec3<S>, Point3<S>> for AffineMatrix3<S> { Transform<S, [S, ..3], Vec3<S>, Point3<S>> for AffineMatrix3<S> {
#[inline] #[inline]
fn identity() -> AffineMatrix3<S> { fn identity() -> AffineMatrix3<S> {
AffineMatrix3 { mat: Mat4::identity() } AffineMatrix3 { mat: Mat4::identity() }
} }
#[inline] #[inline]
fn transform_vec(&self, vec: &Vec3<S>) -> Vec3<S> { fn transform_vec(&self, vec: &Vec3<S>) -> Vec3<S> {
self.mat.mul_v( &vec.extend(num::zero()) ).truncate() self.mat.mul_v( &vec.extend(num::zero()) ).truncate()
} }
#[inline] #[inline]
fn transform_point(&self, point: &Point3<S>) -> Point3<S> { fn transform_point(&self, point: &Point3<S>) -> Point3<S> {
Point3::from_homogeneous( &self.mat.mul_v( &point.to_homogeneous() )) Point3::from_homogeneous( &self.mat.mul_v( &point.to_homogeneous() ))
} }
#[inline] #[inline]
fn concat(&self, other: &AffineMatrix3<S>) -> AffineMatrix3<S> { fn concat(&self, other: &AffineMatrix3<S>) -> AffineMatrix3<S> {
AffineMatrix3 { mat: self.mat.mul_m( &other.mat ) } AffineMatrix3 { mat: self.mat.mul_m( &other.mat ) }
} }
#[inline] #[inline]
fn invert(&self) -> Option<AffineMatrix3<S>> { fn invert(&self) -> Option<AffineMatrix3<S>> {
self.mat.invert().map(|m| AffineMatrix3{ mat: m }) self.mat.invert().map(|m| AffineMatrix3{ mat: m })
} }
} }
impl<S: Clone + Primitive> impl<S: Clone + Primitive>
ToMat4<S> for AffineMatrix3<S> { ToMat4<S> for AffineMatrix3<S> {
#[inline] fn to_mat4(&self) -> Mat4<S> { self.mat.clone() } #[inline] fn to_mat4(&self) -> Mat4<S> { self.mat.clone() }
} }
impl<S: Float> impl<S: Float>
Transform3<S> for AffineMatrix3<S> {} Transform3<S> for AffineMatrix3<S> {}
/// A transformation in three dimensions consisting of a rotation, /// A transformation in three dimensions consisting of a rotation,

View file

@ -70,9 +70,9 @@ impl<S: Primitive> Vec2<S> {
#[inline] pub fn unit_x() -> Vec2<S> { Vec2::new(one(), zero()) } #[inline] pub fn unit_x() -> Vec2<S> { Vec2::new(one(), zero()) }
#[inline] pub fn unit_y() -> Vec2<S> { Vec2::new(zero(), one()) } #[inline] pub fn unit_y() -> Vec2<S> { Vec2::new(zero(), one()) }
} }
impl<S: Primitive + Clone> Vec2<S> { impl<S: Primitive + Clone> Vec2<S> {
#[inline] #[inline]
pub fn extend(&self, z: S)-> Vec3<S> { pub fn extend(&self, z: S)-> Vec3<S> {
Vec3::new(self.x.clone(), self.y.clone(), z) Vec3::new(self.x.clone(), self.y.clone(), z)
} }
} }
@ -82,13 +82,14 @@ impl<S: Primitive> Vec3<S> {
#[inline] pub fn unit_y() -> Vec3<S> { Vec3::new(zero(), one(), zero()) } #[inline] pub fn unit_y() -> Vec3<S> { Vec3::new(zero(), one(), zero()) }
#[inline] pub fn unit_z() -> Vec3<S> { Vec3::new(zero(), zero(), one()) } #[inline] pub fn unit_z() -> Vec3<S> { Vec3::new(zero(), zero(), one()) }
} }
impl<S: Primitive + Clone> Vec3<S> { impl<S: Primitive + Clone> Vec3<S> {
#[inline] #[inline]
pub fn extend(&self, w: S)-> Vec4<S> { pub fn extend(&self, w: S)-> Vec4<S> {
Vec4::new(self.x.clone(), self.y.clone(), self.z.clone(), w) Vec4::new(self.x.clone(), self.y.clone(), self.z.clone(), w)
} }
#[inline] #[inline]
pub fn truncate(&self)-> Vec2<S> { pub fn truncate(&self)-> Vec2<S> {
Vec2::new(self.x.clone(), self.y.clone()) //ignore Z Vec2::new(self.x.clone(), self.y.clone()) //ignore Z
} }
} }
@ -99,9 +100,10 @@ impl<S: Primitive> Vec4<S> {
#[inline] pub fn unit_z() -> Vec4<S> { Vec4::new(zero(), zero(), one(), zero()) } #[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()) } #[inline] pub fn unit_w() -> Vec4<S> { Vec4::new(zero(), zero(), zero(), one()) }
} }
impl<S: Primitive + Clone> Vec4<S> {
impl<S: Primitive + Clone> Vec4<S> {
#[inline] #[inline]
pub fn truncate(&self)-> Vec3<S> { pub fn truncate(&self)-> Vec3<S> {
Vec3::new(self.x.clone(), self.y.clone(), self.z.clone()) //ignore W Vec3::new(self.x.clone(), self.y.clone(), self.z.clone()) //ignore W
} }
} }

View file

@ -8,10 +8,10 @@ use std::num;
#[test] #[test]
fn test_intersection() { fn test_intersection() {
let sphere = Sphere {center: Point3::new(0f64,0f64,0f64), radius: 1f64}; 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 r0 = 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 r1 = 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 r2 = 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 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_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_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))); assert_eq!((sphere,r2).intersection(), Some(Point3::new(1f64, 0f64, 0f64)));