From 9f39bf67b26c3e1d6203a914a141d1005d379613 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sat, 9 Nov 2013 12:15:51 +1100 Subject: [PATCH] Formatting cleanups --- src/cgmath/plane.rs | 9 ++++---- src/cgmath/point.rs | 7 +++---- src/cgmath/rotation.rs | 16 +++++++------- src/cgmath/sphere.rs | 3 +-- src/cgmath/transform.rs | 46 ++++++++++++++++++++--------------------- src/cgmath/vector.rs | 16 +++++++------- src/tests/sphere.rs | 8 +++---- 7 files changed, 52 insertions(+), 53 deletions(-) diff --git a/src/cgmath/plane.rs b/src/cgmath/plane.rs index 4e691d3..25d31c5 100644 --- a/src/cgmath/plane.rs +++ b/src/cgmath/plane.rs @@ -89,11 +89,10 @@ 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))) } + (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))) } } } } diff --git a/src/cgmath/point.rs b/src/cgmath/point.rs index 199687c..65b2adf 100644 --- a/src/cgmath/point.rs +++ b/src/cgmath/point.rs @@ -56,14 +56,13 @@ impl Point3 { impl Point3 { #[inline] - pub fn from_homogeneous(v: &Vec4) -> Point3 { - let _1 :S = one(); - let e = v.truncate().mul_s( _1 / v.w ); + pub fn from_homogeneous(v: &Vec4) -> Point3 { + let e = v.truncate().mul_s(one::() / v.w); Point3::new(e.x.clone(), e.y.clone(), e.z.clone()) //FIXME } #[inline] - pub fn to_homogeneous(&self) -> Vec4 { + pub fn to_homogeneous(&self) -> Vec4 { Vec4::new(self.x.clone(), self.y.clone(), self.z.clone(), one()) } } diff --git a/src/cgmath/rotation.rs b/src/cgmath/rotation.rs index 91fdb47..7baed0c 100644 --- a/src/cgmath/rotation.rs +++ b/src/cgmath/rotation.rs @@ -38,12 +38,12 @@ pub trait Rotation fn rotate_vec(&self, vec: &V) -> V; #[inline] - fn rotate_point(&self, point: &P) -> P { + fn rotate_point(&self, point: &P) -> P { Point::from_vec( &self.rotate_vec( &point.to_vec() ) ) } #[inline] - fn rotate_ray(&self, ray: &Ray) -> Ray { + fn rotate_ray(&self, ray: &Ray) -> Ray { Ray::new( //FIXME: use clone derived from Array Array::build(|i| ray.origin.i(i).clone()), self.rotate_vec(&ray.direction) ) @@ -58,7 +58,7 @@ pub trait Rotation } #[inline] - fn invert_self(&mut self) { + fn invert_self(&mut self) { *self = self.invert(); } } @@ -117,7 +117,7 @@ impl ToMat2 for Basis2 { impl Rotation, Point2> for Basis2 { #[inline] - fn identity() -> Basis2 { Basis2{ mat: Mat2::identity() } } + fn identity() -> Basis2 { Basis2{ mat: Mat2::identity() } } #[inline] fn rotate_vec(&self, vec: &Vec2) -> Vec2 { self.mat.mul_v(vec) } @@ -157,7 +157,7 @@ impl ApproxEq for Basis2 { } } -impl Rotation2 for Basis2 {} +impl Rotation2 for Basis2 {} /// A three-dimensional rotation matrix. /// @@ -232,7 +232,7 @@ impl ToQuat for Basis3 { impl Rotation, Point3> for Basis3 { #[inline] - fn identity() -> Basis3 { Basis3{ mat: Mat3::identity() } } + fn identity() -> Basis3 { Basis3{ mat: Mat3::identity() } } #[inline] fn rotate_vec(&self, vec: &Vec3) -> Vec3 { self.mat.mul_v(vec) } @@ -272,7 +272,7 @@ impl ApproxEq for Basis3 { } } -impl Rotation3 for Basis3 {} +impl Rotation3 for Basis3 {} // Quaternion Rotation impls @@ -288,7 +288,7 @@ impl ToQuat for Quat { impl Rotation, Point3> for Quat { #[inline] - fn identity() -> Quat { Quat::identity() } + fn identity() -> Quat { Quat::identity() } #[inline] fn rotate_vec(&self, vec: &Vec3) -> Vec3 { self.mul_v(vec) } diff --git a/src/cgmath/sphere.rs b/src/cgmath/sphere.rs index 2be7bd9..b2f6643 100644 --- a/src/cgmath/sphere.rs +++ b/src/cgmath/sphere.rs @@ -36,8 +36,7 @@ pub struct Sphere { impl Intersect>> for (Sphere, Ray3) { fn intersection(&self) -> Option> { 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; } diff --git a/src/cgmath/transform.rs b/src/cgmath/transform.rs index c6bf6b1..8e6fcef 100644 --- a/src/cgmath/transform.rs +++ b/src/cgmath/transform.rs @@ -37,12 +37,12 @@ pub trait Transform fn transform_point(&self, point: &P) -> P; #[inline] - fn transform_ray(&self, ray: &Ray) -> Ray { + fn transform_ray(&self, ray: &Ray) -> Ray { Ray::new( self.transform_point(&ray.origin), self.transform_vec(&ray.direction) ) } #[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() } @@ -55,7 +55,7 @@ pub trait Transform } #[inline] - fn invert_self(&mut self)-> bool { + fn invert_self(&mut self)-> bool { match self.invert() { Some(t) => {*self = t; true}, None => false, @@ -65,7 +65,7 @@ pub trait Transform /// A generic transformation consisting of a rotation, /// displacement vector and scale amount. -pub struct Decomposed { +pub struct Decomposed { scale: S, rot: R, disp: V, @@ -79,9 +79,9 @@ impl P: Point, R: Rotation > -Transform for Decomposed { +Transform for Decomposed { #[inline] - fn identity() -> Decomposed { + fn identity() -> Decomposed { Decomposed { scale: num::one(), rot: Rotation::identity(), @@ -90,16 +90,16 @@ Transform for Decomposed { } #[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() )) } #[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 ) } - fn concat(&self, other: &Decomposed) -> Decomposed { + fn concat(&self, other: &Decomposed) -> Decomposed { Decomposed { scale: self.scale * other.scale, rot: self.rot.concat( &other.rot ), @@ -107,10 +107,10 @@ Transform for Decomposed { } } - fn invert(&self) -> Option> { - if self.scale.approx_eq( &num::zero() ) { + fn invert(&self) -> Option> { + if self.scale.approx_eq( &num::zero() ) { None - }else { + }else { let _1 : S = num::one(); let s = _1 / self.scale; let r = self.rot.invert(); @@ -131,7 +131,7 @@ pub trait Transform3 impl> ToMat4 for Decomposed, R> { - fn to_mat4(&self) -> Mat4 { + fn to_mat4(&self) -> Mat4 { let mut m = self.rot.to_mat3().mul_s( self.scale.clone() ).to_mat4(); m.w = self.disp.extend( num::one() ); m @@ -139,7 +139,7 @@ ToMat4 for Decomposed, R> { } impl> -Transform3 for Decomposed,R> {} +Transform3 for Decomposed,R> {} impl> ToStr for Decomposed,R> { @@ -156,40 +156,40 @@ pub struct AffineMatrix3 { } impl -Transform, Point3> for AffineMatrix3 { +Transform, Point3> for AffineMatrix3 { #[inline] - fn identity() -> AffineMatrix3 { + fn identity() -> AffineMatrix3 { AffineMatrix3 { mat: Mat4::identity() } } #[inline] - fn transform_vec(&self, vec: &Vec3) -> Vec3 { + fn transform_vec(&self, vec: &Vec3) -> Vec3 { self.mat.mul_v( &vec.extend(num::zero()) ).truncate() } #[inline] - fn transform_point(&self, point: &Point3) -> Point3 { + fn transform_point(&self, point: &Point3) -> Point3 { Point3::from_homogeneous( &self.mat.mul_v( &point.to_homogeneous() )) } #[inline] - fn concat(&self, other: &AffineMatrix3) -> AffineMatrix3 { + fn concat(&self, other: &AffineMatrix3) -> AffineMatrix3 { AffineMatrix3 { mat: self.mat.mul_m( &other.mat ) } } #[inline] - fn invert(&self) -> Option> { + fn invert(&self) -> Option> { self.mat.invert().map(|m| AffineMatrix3{ mat: m }) } } impl -ToMat4 for AffineMatrix3 { - #[inline] fn to_mat4(&self) -> Mat4 { self.mat.clone() } +ToMat4 for AffineMatrix3 { + #[inline] fn to_mat4(&self) -> Mat4 { self.mat.clone() } } impl -Transform3 for AffineMatrix3 {} +Transform3 for AffineMatrix3 {} /// A transformation in three dimensions consisting of a rotation, diff --git a/src/cgmath/vector.rs b/src/cgmath/vector.rs index 1ecd665..dec7a07 100644 --- a/src/cgmath/vector.rs +++ b/src/cgmath/vector.rs @@ -70,9 +70,9 @@ impl Vec2 { #[inline] pub fn unit_x() -> Vec2 { Vec2::new(one(), zero()) } #[inline] pub fn unit_y() -> Vec2 { Vec2::new(zero(), one()) } } -impl Vec2 { +impl Vec2 { #[inline] - pub fn extend(&self, z: S)-> Vec3 { + pub fn extend(&self, z: S)-> Vec3 { Vec3::new(self.x.clone(), self.y.clone(), z) } } @@ -82,13 +82,14 @@ impl Vec3 { #[inline] pub fn unit_y() -> Vec3 { Vec3::new(zero(), one(), zero()) } #[inline] pub fn unit_z() -> Vec3 { Vec3::new(zero(), zero(), one()) } } -impl Vec3 { +impl Vec3 { #[inline] - pub fn extend(&self, w: S)-> Vec4 { + pub fn extend(&self, w: S)-> Vec4 { Vec4::new(self.x.clone(), self.y.clone(), self.z.clone(), w) } + #[inline] - pub fn truncate(&self)-> Vec2 { + pub fn truncate(&self)-> Vec2 { Vec2::new(self.x.clone(), self.y.clone()) //ignore Z } } @@ -99,9 +100,10 @@ impl Vec4 { #[inline] pub fn unit_z() -> Vec4 { Vec4::new(zero(), zero(), one(), zero()) } #[inline] pub fn unit_w() -> Vec4 { Vec4::new(zero(), zero(), zero(), one()) } } -impl Vec4 { + +impl Vec4 { #[inline] - pub fn truncate(&self)-> Vec3 { + pub fn truncate(&self)-> Vec3 { Vec3::new(self.x.clone(), self.y.clone(), self.z.clone()) //ignore W } } diff --git a/src/tests/sphere.rs b/src/tests/sphere.rs index 0fc5055..2b76be2 100644 --- a/src/tests/sphere.rs +++ b/src/tests/sphere.rs @@ -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 = Ray::new(Point3::new(0f64, 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize()); - let r1: Ray3 = Ray::new(Point3::new(num::cos(1f64), 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize()); - let r2: Ray3 = Ray::new(Point3::new(1f64, 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize()); - let r3: Ray3 = 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)));