Skip to content

Commit 490997c

Browse files
committed
Add an alias to Vector::Scalar in Point
1 parent 89e656b commit 490997c

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

src/point.rs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl<S: BaseNum> Point3<S> {
6868
/// Specifies the numeric operations for point types.
6969
pub trait Point: Clone where
7070
// FIXME: Ugly type signatures - blocked by rust-lang/rust#24092
71-
Self: Array1<Element = <<Self as Point>::Vector as Vector>::Scalar>,
71+
Self: Array1<Element = <Self as Point>::Scalar>,
7272
// FIXME: blocked by rust-lang/rust#20671
7373
//
7474
// for<'a, 'b> &'a Self: Add<&'b V, Output = Self>,
@@ -78,8 +78,13 @@ pub trait Point: Clone where
7878
// for<'a> &'a Self: Div<S, Output = Self>,
7979
// for<'a> &'a Self: Rem<S, Output = Self>,
8080
{
81+
/// The associated scalar.
82+
///
83+
/// Due to the equality constraints demanded by `Self::Vector`, this is effectively just an
84+
/// alias to `Self::Vector::Scalar`.
85+
type Scalar: BaseNum;
8186
/// The associated displacement vector.
82-
type Vector: Vector;
87+
type Vector: Vector<Scalar = Self::Scalar>;
8388

8489
/// Create a point at the origin.
8590
fn origin() -> Self;
@@ -91,13 +96,13 @@ pub trait Point: Clone where
9196

9297
/// Multiply each component by a scalar, returning the new point.
9398
#[must_use]
94-
fn mul_s(&self, scalar: <<Self as Point>::Vector as Vector>::Scalar) -> Self;
99+
fn mul_s(&self, scalar: Self::Scalar) -> Self;
95100
/// Divide each component by a scalar, returning the new point.
96101
#[must_use]
97-
fn div_s(&self, scalar: <<Self as Point>::Vector as Vector>::Scalar) -> Self;
102+
fn div_s(&self, scalar: Self::Scalar) -> Self;
98103
/// Subtract a scalar from each component, returning the new point.
99104
#[must_use]
100-
fn rem_s(&self, scalar: <<Self as Point>::Vector as Vector>::Scalar) -> Self;
105+
fn rem_s(&self, scalar: Self::Scalar) -> Self;
101106

102107
/// Add a vector to this point, returning the new point.
103108
#[must_use]
@@ -106,17 +111,17 @@ pub trait Point: Clone where
106111
fn sub_p(&self, p: &Self) -> Self::Vector;
107112

108113
/// Multiply each component by a scalar, in-place.
109-
fn mul_self_s(&mut self, scalar: <<Self as Point>::Vector as Vector>::Scalar);
114+
fn mul_self_s(&mut self, scalar: Self::Scalar);
110115
/// Divide each component by a scalar, in-place.
111-
fn div_self_s(&mut self, scalar: <<Self as Point>::Vector as Vector>::Scalar);
116+
fn div_self_s(&mut self, scalar: Self::Scalar);
112117
/// Take the remainder of each component by a scalar, in-place.
113-
fn rem_self_s(&mut self, scalar: <<Self as Point>::Vector as Vector>::Scalar);
118+
fn rem_self_s(&mut self, scalar: Self::Scalar);
114119

115120
/// Add a vector to this point, in-place.
116121
fn add_self_v(&mut self, v: &Self::Vector);
117122

118123
/// This is a weird one, but its useful for plane calculations.
119-
fn dot(&self, v: &Self::Vector) -> <<Self as Point>::Vector as Vector>::Scalar;
124+
fn dot(&self, v: &Self::Vector) -> Self::Scalar;
120125

121126
#[must_use]
122127
fn min(&self, p: &Self) -> Self;
@@ -130,6 +135,7 @@ impl<S: BaseNum> Array1 for Point2<S> {
130135
}
131136

132137
impl<S: BaseNum> Point for Point2<S> {
138+
type Scalar = S;
133139
type Vector = Vector2<S>;
134140

135141
#[inline]
@@ -147,26 +153,26 @@ impl<S: BaseNum> Point for Point2<S> {
147153
Vector2::new(self.x, self.y)
148154
}
149155

150-
#[inline] fn mul_s(&self, scalar: <<Self as Point>::Vector as Vector>::Scalar) -> Point2<S> { self * scalar }
151-
#[inline] fn div_s(&self, scalar: <<Self as Point>::Vector as Vector>::Scalar) -> Point2<S> { self / scalar }
152-
#[inline] fn rem_s(&self, scalar: <<Self as Point>::Vector as Vector>::Scalar) -> Point2<S> { self % scalar }
156+
#[inline] fn mul_s(&self, scalar: S) -> Point2<S> { self * scalar }
157+
#[inline] fn div_s(&self, scalar: S) -> Point2<S> { self / scalar }
158+
#[inline] fn rem_s(&self, scalar: S) -> Point2<S> { self % scalar }
153159
#[inline] fn add_v(&self, v: &Vector2<S>) -> Point2<S> { self + v }
154160
#[inline] fn sub_p(&self, p: &Point2<S>) -> Vector2<S> { self - p }
155161

156162
#[inline]
157-
fn mul_self_s(&mut self, scalar: <<Self as Point>::Vector as Vector>::Scalar) {
163+
fn mul_self_s(&mut self, scalar: S) {
158164
self.x = self.x * scalar;
159165
self.y = self.y * scalar;
160166
}
161167

162168
#[inline]
163-
fn div_self_s(&mut self, scalar: <<Self as Point>::Vector as Vector>::Scalar) {
169+
fn div_self_s(&mut self, scalar: S) {
164170
self.x = self.x / scalar;
165171
self.y = self.y / scalar;
166172
}
167173

168174
#[inline]
169-
fn rem_self_s(&mut self, scalar: <<Self as Point>::Vector as Vector>::Scalar) {
175+
fn rem_self_s(&mut self, scalar: S) {
170176
self.x = self.x % scalar;
171177
self.y = self.y % scalar;
172178
}
@@ -209,6 +215,7 @@ impl<S: BaseNum> Array1 for Point3<S> {
209215
}
210216

211217
impl<S: BaseNum> Point for Point3<S> {
218+
type Scalar = S;
212219
type Vector = Vector3<S>;
213220

214221
#[inline]
@@ -226,28 +233,28 @@ impl<S: BaseNum> Point for Point3<S> {
226233
Vector3::new(self.x, self.y, self.z)
227234
}
228235

229-
#[inline] fn mul_s(&self, scalar: <<Self as Point>::Vector as Vector>::Scalar) -> Point3<S> { self * scalar }
230-
#[inline] fn div_s(&self, scalar: <<Self as Point>::Vector as Vector>::Scalar) -> Point3<S> { self / scalar }
231-
#[inline] fn rem_s(&self, scalar: <<Self as Point>::Vector as Vector>::Scalar) -> Point3<S> { self % scalar }
236+
#[inline] fn mul_s(&self, scalar: S) -> Point3<S> { self * scalar }
237+
#[inline] fn div_s(&self, scalar: S) -> Point3<S> { self / scalar }
238+
#[inline] fn rem_s(&self, scalar: S) -> Point3<S> { self % scalar }
232239
#[inline] fn add_v(&self, v: &Vector3<S>) -> Point3<S> { self + v }
233240
#[inline] fn sub_p(&self, p: &Point3<S>) -> Vector3<S> { self - p }
234241

235242
#[inline]
236-
fn mul_self_s(&mut self, scalar: <<Self as Point>::Vector as Vector>::Scalar) {
243+
fn mul_self_s(&mut self, scalar: S) {
237244
self.x = self.x * scalar;
238245
self.y = self.y * scalar;
239246
self.z = self.z * scalar;
240247
}
241248

242249
#[inline]
243-
fn div_self_s(&mut self, scalar: <<Self as Point>::Vector as Vector>::Scalar) {
250+
fn div_self_s(&mut self, scalar: S) {
244251
self.x = self.x / scalar;
245252
self.y = self.y / scalar;
246253
self.z = self.z / scalar;
247254
}
248255

249256
#[inline]
250-
fn rem_self_s(&mut self, scalar: <<Self as Point>::Vector as Vector>::Scalar) {
257+
fn rem_self_s(&mut self, scalar: S) {
251258
self.x = self.x % scalar;
252259
self.y = self.y % scalar;
253260
self.z = self.z % scalar;

src/rotation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use vector::{Vector, Vector2, Vector3};
2727
/// creates a circular motion, and preserves at least one point in the space.
2828
pub trait Rotation<P: Point>: PartialEq + Sized where
2929
// FIXME: Ugly type signatures - blocked by rust-lang/rust#24092
30-
Self: ApproxEq<Epsilon = <<P as Point>::Vector as Vector>::Scalar>,
31-
<<P as Point>::Vector as Vector>::Scalar: BaseFloat,
30+
Self: ApproxEq<Epsilon = <P as Point>::Scalar>,
31+
<P as Point>::Scalar: BaseFloat,
3232
{
3333
/// Create the identity transform (causes no transformation).
3434
fn one() -> Self;

src/transform.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ pub struct Decomposed<V: Vector, R> {
8080

8181
impl<P: Point, R: Rotation<P>> Transform<P> for Decomposed<P::Vector, R> where
8282
// FIXME: Ugly type signatures - blocked by rust-lang/rust#24092
83-
<<P as Point>::Vector as Vector>::Scalar: BaseFloat,
83+
<P as Point>::Scalar: BaseFloat,
8484
{
8585
#[inline]
8686
fn one() -> Decomposed<P::Vector, R> {
8787
Decomposed {
88-
scale: <<P as Point>::Vector as Vector>::Scalar::one(),
88+
scale: <P as Point>::Scalar::one(),
8989
rot: R::one(),
9090
disp: P::Vector::zero(),
9191
}
@@ -96,7 +96,7 @@ impl<P: Point, R: Rotation<P>> Transform<P> for Decomposed<P::Vector, R> where
9696
let rot = R::look_at(&center.sub_p(eye), up);
9797
let disp = rot.rotate_vector(&P::origin().sub_p(eye));
9898
Decomposed {
99-
scale: <<P as Point>::Vector as Vector>::Scalar::one(),
99+
scale: <P as Point>::Scalar::one(),
100100
rot: rot,
101101
disp: disp,
102102
}
@@ -121,10 +121,10 @@ impl<P: Point, R: Rotation<P>> Transform<P> for Decomposed<P::Vector, R> where
121121
}
122122

123123
fn invert(&self) -> Option<Decomposed<P::Vector, R>> {
124-
if self.scale.approx_eq(&<<P as Point>::Vector as Vector>::Scalar::zero()) {
124+
if self.scale.approx_eq(&<P as Point>::Scalar::zero()) {
125125
None
126126
} else {
127-
let s = <<P as Point>::Vector as Vector>::Scalar::one() / self.scale;
127+
let s = <P as Point>::Scalar::one() / self.scale;
128128
let r = self.rot.invert();
129129
let d = r.rotate_vector(&self.disp).mul_s(-s);
130130
Some(Decomposed {
@@ -215,7 +215,8 @@ impl<S: BaseFloat> Transform3<S> for AffineMatrix3<S> {}
215215
/// A trait that allows extracting components (rotation, translation, scale)
216216
/// from an arbitrary transformations
217217
pub trait ToComponents<P: Point, R: Rotation<P>> where
218-
<<P as Point>::Vector as Vector>::Scalar: BaseFloat,
218+
// FIXME: Ugly type signatures - blocked by rust-lang/rust#24092
219+
<P as Point>::Scalar: BaseFloat,
219220
{
220221
/// Extract the (scale, rotation, translation) triple
221222
fn decompose(&self) -> (P::Vector, R, P::Vector);
@@ -226,15 +227,15 @@ pub trait ToComponents3<S: BaseFloat, R: Rotation3<S>>: ToComponents<Point3<S>,
226227

227228
pub trait CompositeTransform<P: Point, R: Rotation<P>>: Transform<P> + ToComponents<P, R> where
228229
// FIXME: Ugly type signatures - blocked by rust-lang/rust#24092
229-
<<P as Point>::Vector as Vector>::Scalar: BaseFloat,
230+
<P as Point>::Scalar: BaseFloat,
230231
{}
231232

232233
pub trait CompositeTransform2<S: BaseFloat, R: Rotation2<S>>: Transform2<S> + ToComponents2<S, R> {}
233234
pub trait CompositeTransform3<S: BaseFloat, R: Rotation3<S>>: Transform3<S> + ToComponents3<S, R> {}
234235

235236
impl<P: Point, R: Rotation<P> + Clone> ToComponents<P, R> for Decomposed<P::Vector, R> where
236237
// FIXME: Ugly type signatures - blocked by rust-lang/rust#24092
237-
<<P as Point>::Vector as Vector>::Scalar: BaseFloat,
238+
<P as Point>::Scalar: BaseFloat,
238239
{
239240
fn decompose(&self) -> (P::Vector, R, P::Vector) {
240241
(P::Vector::one().mul_s(self.scale), self.rot.clone(), self.disp.clone())

0 commit comments

Comments
 (0)