22
22
//! vector are also provided:
23
23
//!
24
24
//! ```rust
25
- //! use cgmath::{Vector, Vector2, Vector3, Vector4, zero, vec2, vec3};
25
+ //! use cgmath::{Vector, Vector2, Vector3, Vector4, vec2, vec3};
26
26
//!
27
27
//! assert_eq!(Vector2::new(1.0f64, 0.0f64), Vector2::unit_x());
28
- //! assert_eq!(vec3(0.0f64, 0.0f64, 0.0f64), zero());
28
+ //! assert_eq!(vec3(0.0f64, 0.0f64, 0.0f64), Vector3:: zero());
29
29
//! assert_eq!(Vector2::from_value(1.0f64), vec2(1.0, 1.0));
30
30
//! ```
31
31
//!
32
32
//! Vectors can be manipulated with typical mathematical operations (addition,
33
33
//! subtraction, element-wise multiplication, element-wise division, negation)
34
- //! using the built-in operators. The additive and multiplicative neutral
35
- //! elements (zero and one) are also provided by this library
34
+ //! using the built-in operators.
36
35
//!
37
36
//! ```rust
38
- //! use cgmath::{Vector2, Vector3, Vector4, one, zero };
37
+ //! use cgmath::{Vector, Vector2, Vector3, Vector4};
39
38
//!
40
39
//! let a: Vector2<f64> = Vector2::new(3.0, 4.0);
41
40
//! let b: Vector2<f64> = Vector2::new(-3.0, -4.0);
42
41
//!
43
- //! assert_eq!(a + b, zero());
44
- //! assert_eq!(-(a * b), Vector2::new(9.0f64, 16.0f64));
45
- //! assert_eq!(a / one (), a);
42
+ //! assert_eq!(& a + & b, Vector2:: zero());
43
+ //! assert_eq!(-(& a * & b), Vector2::new(9.0f64, 16.0f64));
44
+ //! assert_eq!(& a / &Vector2::identity (), a);
46
45
//!
47
46
//! // As with Rust's `int` and `f32` types, Vectors of different types cannot
48
47
//! // be added and so on with impunity. The following will fail to compile:
49
48
//! // let c = a + Vector3::new(1.0, 0.0, 2.0);
50
49
//!
51
50
//! // Instead, we need to convert the Vector2 to a Vector3 by "extending" it
52
51
//! // with the value for the last coordinate:
53
- //! let c: Vector3<f64> = a.extend(0.0) + Vector3::new(1.0, 0.0, 2.0);
52
+ //! let c: Vector3<f64> = & a.extend(0.0) + & Vector3::new(1.0, 0.0, 2.0);
54
53
//!
55
54
//! // Similarly, we can "truncate" a Vector4 down to a Vector3:
56
- //! let d: Vector3<f64> = c + Vector4::unit_x().truncate();
55
+ //! let d: Vector3<f64> = & c + & Vector4::unit_x().truncate();
57
56
//!
58
57
//! assert_eq!(d, Vector3::new(5.0f64, 4.0f64, 2.0f64));
59
58
//! ```
64
63
//! and [cross products](http://en.wikipedia.org/wiki/Cross_product).
65
64
//!
66
65
//! ```rust
67
- //! use cgmath::{Vector, Vector2, Vector3, Vector4, dot, zero };
66
+ //! use cgmath::{Vector, Vector2, Vector3, Vector4, dot};
68
67
//!
69
68
//! // All vectors implement the dot product as a method:
70
69
//! let a: Vector2<f64> = Vector2::new(3.0, 6.0);
71
70
//! let b: Vector2<f64> = Vector2::new(-2.0, 1.0);
72
- //! assert_eq!(a.dot(&b), zero() );
71
+ //! assert_eq!(a.dot(&b), 0.0 );
73
72
//!
74
73
//! // But there is also a top-level function:
75
74
//! assert_eq!(a.dot(&b), dot(a, b));
@@ -112,9 +111,32 @@ use num::{BaseNum, BaseFloat};
112
111
/// A trait that specifies a range of numeric operations for vectors. Not all
113
112
/// of these make sense from a linear algebra point of view, but are included
114
113
/// for pragmatic reasons.
115
- pub trait Vector < S : BaseNum > : Array1 < S > + Zero + One {
114
+ pub trait Vector < S : BaseNum > : Array1 < S > + Clone // where
115
+ // FIXME: blocked by rust-lang/rust#20671
116
+ //
117
+ // for<'a, 'b> &'a Self: Add<&'b Self, Output = Self>,
118
+ // for<'a, 'b> &'a Self: Sub<&'b Self, Output = Self>,
119
+ // for<'a, 'b> &'a Self: Mul<&'b Self, Output = Self>,
120
+ // for<'a, 'b> &'a Self: Div<&'b Self, Output = Self>,
121
+ // for<'a, 'b> &'a Self: Rem<&'b Self, Output = Self>,
122
+ // for<'a, 'b> &'a Self: Sub<&'b Self, Output = Self>,
123
+ //
124
+ // for<'a> &'a Self: Add<S, Output = Self>,
125
+ // for<'a> &'a Self: Sub<S, Output = Self>,
126
+ // for<'a> &'a Self: Mul<S, Output = Self>,
127
+ // for<'a> &'a Self: Div<S, Output = Self>,
128
+ // for<'a> &'a Self: Rem<S, Output = Self>,
129
+ {
116
130
/// Construct a vector from a single value, replicating it.
117
131
fn from_value ( s : S ) -> Self ;
132
+
133
+ /// The zero vector (with all components set to zero)
134
+ #[ inline]
135
+ fn zero ( ) -> Self { Self :: from_value ( S :: zero ( ) ) }
136
+ /// The identity vector (with all components set to one)
137
+ #[ inline]
138
+ fn identity ( ) -> Self { Self :: from_value ( S :: one ( ) ) }
139
+
118
140
/// Add a scalar to this vector, returning a new vector.
119
141
#[ must_use]
120
142
fn add_s ( & self , s : S ) -> Self ;
@@ -215,19 +237,6 @@ macro_rules! vec {
215
237
$Self_:: new( $( $field) ,+)
216
238
}
217
239
218
- impl <$S: Zero + BaseNum > Zero for $Self_<$S> {
219
- #[ inline]
220
- fn zero( ) -> $Self_<S > { $Self_ { $( $field: $S:: zero( ) ) ,+ } }
221
-
222
- #[ inline]
223
- fn is_zero( & self ) -> bool { $( ( self . $field. is_zero( ) ) ) &&+ }
224
- }
225
-
226
- impl <$S: One + BaseNum > One for $Self_<$S> {
227
- #[ inline]
228
- fn one( ) -> $Self_<$S> { $Self_ { $( $field: S :: one( ) ) ,+ } }
229
- }
230
-
231
240
impl <$S: NumCast + Copy > $Self_<$S> {
232
241
/// Component-wise casting to another type
233
242
#[ inline]
@@ -240,94 +249,98 @@ macro_rules! vec {
240
249
241
250
impl <S : BaseNum > Vector <S > for $Self_<S > {
242
251
#[ inline] fn from_value( s: S ) -> $Self_<S > { $Self_ { $( $field: s) ,+ } }
243
- #[ inline] fn add_s( & self , s: S ) -> $Self_<S > { $Self_:: new( $( self . $field + s) ,+) }
244
- #[ inline] fn sub_s( & self , s: S ) -> $Self_<S > { $Self_:: new( $( self . $field - s) ,+) }
245
- #[ inline] fn mul_s( & self , s: S ) -> $Self_<S > { $Self_:: new( $( self . $field * s) ,+) }
246
- #[ inline] fn div_s( & self , s: S ) -> $Self_<S > { $Self_:: new( $( self . $field / s) ,+) }
247
- #[ inline] fn rem_s( & self , s: S ) -> $Self_<S > { $Self_:: new( $( self . $field % s) ,+) }
248
-
249
- #[ inline] fn add_v( & self , v: & $Self_<S >) -> $Self_<S > { $Self_:: new( $( self . $field + v. $field) ,+) }
250
- #[ inline] fn sub_v( & self , v: & $Self_<S >) -> $Self_<S > { $Self_:: new( $( self . $field - v. $field) ,+) }
251
- #[ inline] fn mul_v( & self , v: & $Self_<S >) -> $Self_<S > { $Self_:: new( $( self . $field * v. $field) ,+) }
252
- #[ inline] fn div_v( & self , v: & $Self_<S >) -> $Self_<S > { $Self_:: new( $( self . $field / v. $field) ,+) }
253
- #[ inline] fn rem_v( & self , v: & $Self_<S >) -> $Self_<S > { $Self_:: new( $( self . $field % v. $field) ,+) }
254
-
255
- #[ inline] fn add_self_s( & mut self , s: S ) { $( self . $field = self . $field + s; ) + }
256
- #[ inline] fn sub_self_s( & mut self , s: S ) { $( self . $field = self . $field - s; ) + }
257
- #[ inline] fn mul_self_s( & mut self , s: S ) { $( self . $field = self . $field * s; ) + }
258
- #[ inline] fn div_self_s( & mut self , s: S ) { $( self . $field = self . $field / s; ) + }
259
- #[ inline] fn rem_self_s( & mut self , s: S ) { $( self . $field = self . $field % s; ) + }
260
-
261
- #[ inline] fn add_self_v( & mut self , v: & $Self_<S >) { $( self . $field = self . $field + v. $field; ) + }
262
- #[ inline] fn sub_self_v( & mut self , v: & $Self_<S >) { $( self . $field = self . $field - v. $field; ) + }
263
- #[ inline] fn mul_self_v( & mut self , v: & $Self_<S >) { $( self . $field = self . $field * v. $field; ) + }
264
- #[ inline] fn div_self_v( & mut self , v: & $Self_<S >) { $( self . $field = self . $field / v. $field; ) + }
265
- #[ inline] fn rem_self_v( & mut self , v: & $Self_<S >) { $( self . $field = self . $field % v. $field; ) + }
252
+
253
+ #[ inline] fn add_s( & self , s: S ) -> $Self_<S > { self + s }
254
+ #[ inline] fn sub_s( & self , s: S ) -> $Self_<S > { self - s }
255
+ #[ inline] fn mul_s( & self , s: S ) -> $Self_<S > { self * s }
256
+ #[ inline] fn div_s( & self , s: S ) -> $Self_<S > { self / s }
257
+ #[ inline] fn rem_s( & self , s: S ) -> $Self_<S > { self % s }
258
+
259
+ #[ inline] fn add_v( & self , v: & $Self_<S >) -> $Self_<S > { self + v }
260
+ #[ inline] fn sub_v( & self , v: & $Self_<S >) -> $Self_<S > { self - v }
261
+ #[ inline] fn mul_v( & self , v: & $Self_<S >) -> $Self_<S > { self * v }
262
+ #[ inline] fn div_v( & self , v: & $Self_<S >) -> $Self_<S > { self / v }
263
+ #[ inline] fn rem_v( & self , v: & $Self_<S >) -> $Self_<S > { self % v }
264
+
265
+ #[ inline] fn add_self_s( & mut self , s: S ) { * self = & * self + s; }
266
+ #[ inline] fn sub_self_s( & mut self , s: S ) { * self = & * self - s; }
267
+ #[ inline] fn mul_self_s( & mut self , s: S ) { * self = & * self * s; }
268
+ #[ inline] fn div_self_s( & mut self , s: S ) { * self = & * self / s; }
269
+ #[ inline] fn rem_self_s( & mut self , s: S ) { * self = & * self % s; }
270
+
271
+ #[ inline] fn add_self_v( & mut self , v: & $Self_<S >) { * self = & * self + v; }
272
+ #[ inline] fn sub_self_v( & mut self , v: & $Self_<S >) { * self = & * self - v; }
273
+ #[ inline] fn mul_self_v( & mut self , v: & $Self_<S >) { * self = & * self * v; }
274
+ #[ inline] fn div_self_v( & mut self , v: & $Self_<S >) { * self = & * self / v; }
275
+ #[ inline] fn rem_self_v( & mut self , v: & $Self_<S >) { * self = & * self % v; }
266
276
267
277
#[ inline] fn comp_add( & self ) -> S { fold!( add, { $( self . $field) ,+ } ) }
268
278
#[ inline] fn comp_mul( & self ) -> S { fold!( mul, { $( self . $field) ,+ } ) }
269
279
#[ inline] fn comp_min( & self ) -> S { fold!( partial_min, { $( self . $field) ,+ } ) }
270
280
#[ inline] fn comp_max( & self ) -> S { fold!( partial_max, { $( self . $field) ,+ } ) }
271
281
}
272
282
273
- impl <S : BaseNum > Add for $Self_<S > {
274
- type Output = $Self_<S >;
275
-
276
- #[ inline]
277
- fn add( self , v: $Self_<S >) -> $Self_<S > { self . add_v( & v) }
278
- }
279
-
280
- impl <S : BaseNum > Sub for $Self_<S > {
281
- type Output = $Self_<S >;
282
-
283
- #[ inline]
284
- fn sub( self , v: $Self_<S >) -> $Self_<S > { self . sub_v( & v) }
285
- }
286
-
287
283
impl <S : Neg <Output = S >> Neg for $Self_<S > {
288
284
type Output = $Self_<S >;
289
285
290
286
#[ inline]
291
287
fn neg( self ) -> $Self_<S > { $Self_:: new( $( -self . $field) ,+) }
292
288
}
293
289
294
- impl <S : BaseNum > Mul for $Self_<S > {
295
- type Output = $Self_<S >;
296
-
290
+ impl <S : BaseFloat > ApproxEq <S > for $Self_<S > {
297
291
#[ inline]
298
- fn mul( self , v: $Self_<S >) -> $Self_<S > { self . mul_v( & v) }
292
+ fn approx_eq_eps( & self , other: & $Self_<S >, epsilon: & S ) -> bool {
293
+ $( self . $field. approx_eq_eps( & other. $field, epsilon) ) &&+
294
+ }
299
295
}
300
296
301
- impl <S : BaseNum > Div for $Self_<S > {
302
- type Output = $Self_<S >;
303
-
297
+ impl <S : BaseFloat + Rand > Rand for $Self_<S > {
304
298
#[ inline]
305
- fn div( self , v: $Self_<S >) -> $Self_<S > { self . div_v( & v) }
299
+ fn rand<R : Rng >( rng: & mut R ) -> $Self_<S > {
300
+ $Self_ { $( $field: rng. gen ( ) ) ,+ }
301
+ }
306
302
}
303
+ }
304
+ }
307
305
308
- impl <S : BaseNum > Rem for $Self_<S > {
309
- type Output = $Self_<S >;
310
-
311
- #[ inline]
312
- fn rem( self , v: $Self_<S >) -> $Self_<S > { self . rem_v( & v) }
313
- }
306
+ macro_rules! impl_binary_operator {
307
+ ( $Binop: ident :: $binop: ident, $VectorN: ident { $( $field: ident) ,+ } ) => {
308
+ impl <' a, S : BaseNum > $Binop<S > for & ' a $VectorN<S > {
309
+ type Output = $VectorN<S >;
314
310
315
- impl <S : BaseFloat > ApproxEq <S > for $Self_<S > {
316
311
#[ inline]
317
- fn approx_eq_eps ( & self , other : & $Self_< S > , epsilon : & S ) -> bool {
318
- $( self . $field. approx_eq_eps ( & other . $field , epsilon ) ) &&+
312
+ fn $binop ( self , s : S ) -> $VectorN< S > {
313
+ $VectorN :: new ( $ ( self . $field. $binop ( s ) ) ,+ )
319
314
}
320
315
}
321
316
322
- impl <S : BaseFloat + Rand > Rand for $Self_<S > {
317
+ impl <' a, ' b, S : BaseNum > $Binop<& ' a $VectorN<S >> for & ' b $VectorN<S > {
318
+ type Output = $VectorN<S >;
319
+
323
320
#[ inline]
324
- fn rand< R : Rng > ( rng : & mut R ) -> $Self_ <S > {
325
- $Self_ { $ ( $field: rng . gen ( ) ) ,+ }
321
+ fn $binop ( self , other : & ' a $VectorN< S > ) -> $VectorN <S > {
322
+ $VectorN :: new ( $ ( self . $field. $binop ( other . $field ) ) ,+)
326
323
}
327
324
}
328
325
}
329
326
}
330
327
328
+ impl_binary_operator ! ( Add :: add, Vector2 { x, y } ) ;
329
+ impl_binary_operator ! ( Add :: add, Vector3 { x, y, z } ) ;
330
+ impl_binary_operator ! ( Add :: add, Vector4 { x, y, z, w } ) ;
331
+ impl_binary_operator ! ( Sub :: sub, Vector2 { x, y } ) ;
332
+ impl_binary_operator ! ( Sub :: sub, Vector3 { x, y, z } ) ;
333
+ impl_binary_operator ! ( Sub :: sub, Vector4 { x, y, z, w } ) ;
334
+ impl_binary_operator ! ( Mul :: mul, Vector2 { x, y } ) ;
335
+ impl_binary_operator ! ( Mul :: mul, Vector3 { x, y, z } ) ;
336
+ impl_binary_operator ! ( Mul :: mul, Vector4 { x, y, z, w } ) ;
337
+ impl_binary_operator ! ( Div :: div, Vector2 { x, y } ) ;
338
+ impl_binary_operator ! ( Div :: div, Vector3 { x, y, z } ) ;
339
+ impl_binary_operator ! ( Div :: div, Vector4 { x, y, z, w } ) ;
340
+ impl_binary_operator ! ( Rem :: rem, Vector2 { x, y } ) ;
341
+ impl_binary_operator ! ( Rem :: rem, Vector3 { x, y, z } ) ;
342
+ impl_binary_operator ! ( Rem :: rem, Vector4 { x, y, z, w } ) ;
343
+
331
344
macro_rules! fold {
332
345
( & $method: ident, { $x: expr, $y: expr } ) => { $x. $method( & $y) } ;
333
346
( & $method: ident, { $x: expr, $y: expr, $z: expr } ) => { $x. $method( & $y) . $method( & $z) } ;
0 commit comments