From 490997c93f70443ca20d249b2228e86831e5ff26 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Tue, 3 Nov 2015 15:50:07 +1100 Subject: [PATCH] Add an alias to Vector::Scalar in Point --- src/point.rs | 49 +++++++++++++++++++++++++++--------------------- src/rotation.rs | 4 ++-- src/transform.rs | 17 +++++++++-------- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/point.rs b/src/point.rs index 2b21109..c2d0568 100644 --- a/src/point.rs +++ b/src/point.rs @@ -68,7 +68,7 @@ impl Point3 { /// Specifies the numeric operations for point types. pub trait Point: Clone where // FIXME: Ugly type signatures - blocked by rust-lang/rust#24092 - Self: Array1::Vector as Vector>::Scalar>, + Self: Array1::Scalar>, // FIXME: blocked by rust-lang/rust#20671 // // for<'a, 'b> &'a Self: Add<&'b V, Output = Self>, @@ -78,8 +78,13 @@ pub trait Point: Clone where // for<'a> &'a Self: Div, // for<'a> &'a Self: Rem, { + /// The associated scalar. + /// + /// Due to the equality constraints demanded by `Self::Vector`, this is effectively just an + /// alias to `Self::Vector::Scalar`. + type Scalar: BaseNum; /// The associated displacement vector. - type Vector: Vector; + type Vector: Vector; /// Create a point at the origin. fn origin() -> Self; @@ -91,13 +96,13 @@ pub trait Point: Clone where /// Multiply each component by a scalar, returning the new point. #[must_use] - fn mul_s(&self, scalar: <::Vector as Vector>::Scalar) -> Self; + fn mul_s(&self, scalar: Self::Scalar) -> Self; /// Divide each component by a scalar, returning the new point. #[must_use] - fn div_s(&self, scalar: <::Vector as Vector>::Scalar) -> Self; + fn div_s(&self, scalar: Self::Scalar) -> Self; /// Subtract a scalar from each component, returning the new point. #[must_use] - fn rem_s(&self, scalar: <::Vector as Vector>::Scalar) -> Self; + fn rem_s(&self, scalar: Self::Scalar) -> Self; /// Add a vector to this point, returning the new point. #[must_use] @@ -106,17 +111,17 @@ pub trait Point: Clone where fn sub_p(&self, p: &Self) -> Self::Vector; /// Multiply each component by a scalar, in-place. - fn mul_self_s(&mut self, scalar: <::Vector as Vector>::Scalar); + fn mul_self_s(&mut self, scalar: Self::Scalar); /// Divide each component by a scalar, in-place. - fn div_self_s(&mut self, scalar: <::Vector as Vector>::Scalar); + fn div_self_s(&mut self, scalar: Self::Scalar); /// Take the remainder of each component by a scalar, in-place. - fn rem_self_s(&mut self, scalar: <::Vector as Vector>::Scalar); + fn rem_self_s(&mut self, scalar: Self::Scalar); /// Add a vector to this point, in-place. fn add_self_v(&mut self, v: &Self::Vector); /// This is a weird one, but its useful for plane calculations. - fn dot(&self, v: &Self::Vector) -> <::Vector as Vector>::Scalar; + fn dot(&self, v: &Self::Vector) -> Self::Scalar; #[must_use] fn min(&self, p: &Self) -> Self; @@ -130,6 +135,7 @@ impl Array1 for Point2 { } impl Point for Point2 { + type Scalar = S; type Vector = Vector2; #[inline] @@ -147,26 +153,26 @@ impl Point for Point2 { Vector2::new(self.x, self.y) } - #[inline] fn mul_s(&self, scalar: <::Vector as Vector>::Scalar) -> Point2 { self * scalar } - #[inline] fn div_s(&self, scalar: <::Vector as Vector>::Scalar) -> Point2 { self / scalar } - #[inline] fn rem_s(&self, scalar: <::Vector as Vector>::Scalar) -> Point2 { self % scalar } + #[inline] fn mul_s(&self, scalar: S) -> Point2 { self * scalar } + #[inline] fn div_s(&self, scalar: S) -> Point2 { self / scalar } + #[inline] fn rem_s(&self, scalar: S) -> Point2 { self % scalar } #[inline] fn add_v(&self, v: &Vector2) -> Point2 { self + v } #[inline] fn sub_p(&self, p: &Point2) -> Vector2 { self - p } #[inline] - fn mul_self_s(&mut self, scalar: <::Vector as Vector>::Scalar) { + fn mul_self_s(&mut self, scalar: S) { self.x = self.x * scalar; self.y = self.y * scalar; } #[inline] - fn div_self_s(&mut self, scalar: <::Vector as Vector>::Scalar) { + fn div_self_s(&mut self, scalar: S) { self.x = self.x / scalar; self.y = self.y / scalar; } #[inline] - fn rem_self_s(&mut self, scalar: <::Vector as Vector>::Scalar) { + fn rem_self_s(&mut self, scalar: S) { self.x = self.x % scalar; self.y = self.y % scalar; } @@ -209,6 +215,7 @@ impl Array1 for Point3 { } impl Point for Point3 { + type Scalar = S; type Vector = Vector3; #[inline] @@ -226,28 +233,28 @@ impl Point for Point3 { Vector3::new(self.x, self.y, self.z) } - #[inline] fn mul_s(&self, scalar: <::Vector as Vector>::Scalar) -> Point3 { self * scalar } - #[inline] fn div_s(&self, scalar: <::Vector as Vector>::Scalar) -> Point3 { self / scalar } - #[inline] fn rem_s(&self, scalar: <::Vector as Vector>::Scalar) -> Point3 { self % scalar } + #[inline] fn mul_s(&self, scalar: S) -> Point3 { self * scalar } + #[inline] fn div_s(&self, scalar: S) -> Point3 { self / scalar } + #[inline] fn rem_s(&self, scalar: S) -> Point3 { self % scalar } #[inline] fn add_v(&self, v: &Vector3) -> Point3 { self + v } #[inline] fn sub_p(&self, p: &Point3) -> Vector3 { self - p } #[inline] - fn mul_self_s(&mut self, scalar: <::Vector as Vector>::Scalar) { + fn mul_self_s(&mut self, scalar: S) { self.x = self.x * scalar; self.y = self.y * scalar; self.z = self.z * scalar; } #[inline] - fn div_self_s(&mut self, scalar: <::Vector as Vector>::Scalar) { + fn div_self_s(&mut self, scalar: S) { self.x = self.x / scalar; self.y = self.y / scalar; self.z = self.z / scalar; } #[inline] - fn rem_self_s(&mut self, scalar: <::Vector as Vector>::Scalar) { + fn rem_self_s(&mut self, scalar: S) { self.x = self.x % scalar; self.y = self.y % scalar; self.z = self.z % scalar; diff --git a/src/rotation.rs b/src/rotation.rs index 7e1fed6..068bfcc 100644 --- a/src/rotation.rs +++ b/src/rotation.rs @@ -27,8 +27,8 @@ use vector::{Vector, Vector2, Vector3}; /// creates a circular motion, and preserves at least one point in the space. pub trait Rotation: PartialEq + Sized where // FIXME: Ugly type signatures - blocked by rust-lang/rust#24092 - Self: ApproxEq::Vector as Vector>::Scalar>, - <

::Vector as Vector>::Scalar: BaseFloat, + Self: ApproxEq::Scalar>, +

::Scalar: BaseFloat, { /// Create the identity transform (causes no transformation). fn one() -> Self; diff --git a/src/transform.rs b/src/transform.rs index 5a0e2fb..a1583ab 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -80,12 +80,12 @@ pub struct Decomposed { impl> Transform

for Decomposed where // FIXME: Ugly type signatures - blocked by rust-lang/rust#24092 - <

::Vector as Vector>::Scalar: BaseFloat, +

::Scalar: BaseFloat, { #[inline] fn one() -> Decomposed { Decomposed { - scale: <

::Vector as Vector>::Scalar::one(), + scale:

::Scalar::one(), rot: R::one(), disp: P::Vector::zero(), } @@ -96,7 +96,7 @@ impl> Transform

for Decomposed where let rot = R::look_at(¢er.sub_p(eye), up); let disp = rot.rotate_vector(&P::origin().sub_p(eye)); Decomposed { - scale: <

::Vector as Vector>::Scalar::one(), + scale:

::Scalar::one(), rot: rot, disp: disp, } @@ -121,10 +121,10 @@ impl> Transform

for Decomposed where } fn invert(&self) -> Option> { - if self.scale.approx_eq(&<

::Vector as Vector>::Scalar::zero()) { + if self.scale.approx_eq(&

::Scalar::zero()) { None } else { - let s = <

::Vector as Vector>::Scalar::one() / self.scale; + let s =

::Scalar::one() / self.scale; let r = self.rot.invert(); let d = r.rotate_vector(&self.disp).mul_s(-s); Some(Decomposed { @@ -215,7 +215,8 @@ impl Transform3 for AffineMatrix3 {} /// A trait that allows extracting components (rotation, translation, scale) /// from an arbitrary transformations pub trait ToComponents> where - <

::Vector as Vector>::Scalar: BaseFloat, + // FIXME: Ugly type signatures - blocked by rust-lang/rust#24092 +

::Scalar: BaseFloat, { /// Extract the (scale, rotation, translation) triple fn decompose(&self) -> (P::Vector, R, P::Vector); @@ -226,7 +227,7 @@ pub trait ToComponents3>: ToComponents, pub trait CompositeTransform>: Transform

+ ToComponents where // FIXME: Ugly type signatures - blocked by rust-lang/rust#24092 - <

::Vector as Vector>::Scalar: BaseFloat, +

::Scalar: BaseFloat, {} pub trait CompositeTransform2>: Transform2 + ToComponents2 {} @@ -234,7 +235,7 @@ pub trait CompositeTransform3>: Transform3 + To impl + Clone> ToComponents for Decomposed where // FIXME: Ugly type signatures - blocked by rust-lang/rust#24092 - <

::Vector as Vector>::Scalar: BaseFloat, +

::Scalar: BaseFloat, { fn decompose(&self) -> (P::Vector, R, P::Vector) { (P::Vector::one().mul_s(self.scale), self.rot.clone(), self.disp.clone())