@@ -249,7 +249,15 @@ impl<S: Copy + Neg<Output = S>> Matrix4<S> {
249
249
}
250
250
}
251
251
252
- pub trait Matrix < S : BaseFloat , V : Vector < S > + ' static > : Array2 < V , V , S > + ApproxEq < S > + Sized // where
252
+ pub trait Matrix where
253
+ // FIXME: Ugly type signatures - blocked by rust-lang/rust#24092
254
+ Self : Array2 <
255
+ Element = <<Self as Matrix >:: ColumnRow as Vector >:: Scalar ,
256
+ Column = <Self as Matrix >:: ColumnRow ,
257
+ Row = <Self as Matrix >:: ColumnRow ,
258
+ > ,
259
+ Self : ApproxEq < Epsilon = <<Self as Matrix >:: ColumnRow as Vector >:: Scalar > + Sized ,
260
+ Self :: Element : BaseFloat ,
253
261
// FIXME: blocked by rust-lang/rust#20671
254
262
//
255
263
// for<'a, 'b> &'a Self: Add<&'b Self, Output = Self>,
@@ -261,28 +269,31 @@ pub trait Matrix<S: BaseFloat, V: Vector<S> + 'static>: Array2<V, V, S> + Approx
261
269
// for<'a> &'a Self: Div<S, Output = Self>,
262
270
// for<'a> &'a Self: Rem<S, Output = Self>,
263
271
{
272
+ // FIXME: Will not be needed once equality constraints in where clauses is implemented
273
+ type ColumnRow : Vector ;
274
+
264
275
/// Create a new diagonal matrix using the supplied value.
265
- fn from_value ( value : S ) -> Self ;
276
+ fn from_value ( value : Self :: Element ) -> Self ;
266
277
/// Create a matrix from a non-uniform scale
267
- fn from_diagonal ( value : & V ) -> Self ;
278
+ fn from_diagonal ( diagonal : & Self :: Column ) -> Self ;
268
279
269
280
/// Create a matrix with all elements equal to zero.
270
281
#[ inline]
271
- fn zero ( ) -> Self { Self :: from_value ( S :: zero ( ) ) }
282
+ fn zero ( ) -> Self { Self :: from_value ( Self :: Element :: zero ( ) ) }
272
283
/// Create a matrix where the each element of the diagonal is equal to one.
273
284
#[ inline]
274
- fn one ( ) -> Self { Self :: from_value ( S :: one ( ) ) }
285
+ fn one ( ) -> Self { Self :: from_value ( Self :: Element :: one ( ) ) }
275
286
276
287
/// Multiply this matrix by a scalar, returning the new matrix.
277
288
#[ must_use]
278
- fn mul_s ( & self , s : S ) -> Self ;
289
+ fn mul_s ( & self , s : Self :: Element ) -> Self ;
279
290
/// Divide this matrix by a scalar, returning the new matrix.
280
291
#[ must_use]
281
- fn div_s ( & self , s : S ) -> Self ;
292
+ fn div_s ( & self , s : Self :: Element ) -> Self ;
282
293
/// Take the remainder of this matrix by a scalar, returning the new
283
294
/// matrix.
284
295
#[ must_use]
285
- fn rem_s ( & self , s : S ) -> Self ;
296
+ fn rem_s ( & self , s : Self :: Element ) -> Self ;
286
297
287
298
/// Add this matrix with another matrix, returning the new metrix.
288
299
#[ must_use]
@@ -292,18 +303,18 @@ pub trait Matrix<S: BaseFloat, V: Vector<S> + 'static>: Array2<V, V, S> + Approx
292
303
fn sub_m ( & self , m : & Self ) -> Self ;
293
304
294
305
/// Multiplay a vector by this matrix, returning a new vector.
295
- fn mul_v ( & self , v : & V ) -> V ;
306
+ fn mul_v ( & self , v : & Self :: Column ) -> Self :: Column ;
296
307
297
308
/// Multiply this matrix by another matrix, returning the new matrix.
298
309
#[ must_use]
299
310
fn mul_m ( & self , m : & Self ) -> Self ;
300
311
301
312
/// Multiply this matrix by a scalar, in-place.
302
- fn mul_self_s ( & mut self , s : S ) ;
313
+ fn mul_self_s ( & mut self , s : Self :: Element ) ;
303
314
/// Divide this matrix by a scalar, in-place.
304
- fn div_self_s ( & mut self , s : S ) ;
315
+ fn div_self_s ( & mut self , s : Self :: Element ) ;
305
316
/// Take the remainder of this matrix, in-place.
306
- fn rem_self_s ( & mut self , s : S ) ;
317
+ fn rem_self_s ( & mut self , s : Self :: Element ) ;
307
318
308
319
/// Add this matrix with another matrix, in-place.
309
320
fn add_self_m ( & mut self , m : & Self ) ;
@@ -320,14 +331,14 @@ pub trait Matrix<S: BaseFloat, V: Vector<S> + 'static>: Array2<V, V, S> + Approx
320
331
/// Transpose this matrix in-place.
321
332
fn transpose_self ( & mut self ) ;
322
333
/// Take the determinant of this matrix.
323
- fn determinant ( & self ) -> S ;
334
+ fn determinant ( & self ) -> Self :: Element ;
324
335
325
336
/// Return a vector containing the diagonal of this matrix.
326
- fn diagonal ( & self ) -> V ;
337
+ fn diagonal ( & self ) -> Self :: Column ;
327
338
328
339
/// Return the trace of this matrix. That is, the sum of the diagonal.
329
340
#[ inline]
330
- fn trace ( & self ) -> S { self . diagonal ( ) . sum ( ) }
341
+ fn trace ( & self ) -> Self :: Element { self . diagonal ( ) . sum ( ) }
331
342
332
343
/// Invert this matrix, returning a new matrix. `m.mul_m(m.invert())` is
333
344
/// the identity matrix. Returns `None` if this matrix is not invertible
@@ -343,7 +354,7 @@ pub trait Matrix<S: BaseFloat, V: Vector<S> + 'static>: Array2<V, V, S> + Approx
343
354
344
355
/// Test if this matrix is invertible.
345
356
#[ inline]
346
- fn is_invertible ( & self ) -> bool { !self . determinant ( ) . approx_eq ( & S :: zero ( ) ) }
357
+ fn is_invertible ( & self ) -> bool { !self . determinant ( ) . approx_eq ( & Self :: Element :: zero ( ) ) }
347
358
348
359
/// Test if this matrix is the identity matrix. That is, it is diagonal
349
360
/// and every element in the diagonal is one.
@@ -359,7 +370,11 @@ pub trait Matrix<S: BaseFloat, V: Vector<S> + 'static>: Array2<V, V, S> + Approx
359
370
fn is_symmetric ( & self ) -> bool ;
360
371
}
361
372
362
- impl < S : Copy + ' static > Array2 < Vector2 < S > , Vector2 < S > , S > for Matrix2 < S > {
373
+ impl < S : Copy > Array2 for Matrix2 < S > {
374
+ type Element = S ;
375
+ type Column = Vector2 < S > ;
376
+ type Row = Vector2 < S > ;
377
+
363
378
#[ inline]
364
379
fn row ( & self , r : usize ) -> Vector2 < S > {
365
380
Vector2 :: new ( self [ 0 ] [ r] ,
@@ -373,7 +388,11 @@ impl<S: Copy + 'static> Array2<Vector2<S>, Vector2<S>, S> for Matrix2<S> {
373
388
}
374
389
}
375
390
376
- impl < S : Copy + ' static > Array2 < Vector3 < S > , Vector3 < S > , S > for Matrix3 < S > {
391
+ impl < S : Copy > Array2 for Matrix3 < S > {
392
+ type Element = S ;
393
+ type Column = Vector3 < S > ;
394
+ type Row = Vector3 < S > ;
395
+
377
396
#[ inline]
378
397
fn row ( & self , r : usize ) -> Vector3 < S > {
379
398
Vector3 :: new ( self [ 0 ] [ r] ,
@@ -389,7 +408,11 @@ impl<S: Copy + 'static> Array2<Vector3<S>, Vector3<S>, S> for Matrix3<S> {
389
408
}
390
409
}
391
410
392
- impl < S : Copy + ' static > Array2 < Vector4 < S > , Vector4 < S > , S > for Matrix4 < S > {
411
+ impl < S : Copy > Array2 for Matrix4 < S > {
412
+ type Element = S ;
413
+ type Column = Vector4 < S > ;
414
+ type Row = Vector4 < S > ;
415
+
393
416
#[ inline]
394
417
fn row ( & self , r : usize ) -> Vector4 < S > {
395
418
Vector4 :: new ( self [ 0 ] [ r] ,
@@ -407,7 +430,9 @@ impl<S: Copy + 'static> Array2<Vector4<S>, Vector4<S>, S> for Matrix4<S> {
407
430
}
408
431
}
409
432
410
- impl < S : BaseFloat > Matrix < S , Vector2 < S > > for Matrix2 < S > {
433
+ impl < S : BaseFloat > Matrix for Matrix2 < S > {
434
+ type ColumnRow = Vector2 < S > ;
435
+
411
436
#[ inline]
412
437
fn from_value ( value : S ) -> Matrix2 < S > {
413
438
Matrix2 :: new ( value, S :: zero ( ) ,
@@ -504,7 +529,9 @@ impl<S: BaseFloat> Matrix<S, Vector2<S>> for Matrix2<S> {
504
529
}
505
530
}
506
531
507
- impl < S : BaseFloat > Matrix < S , Vector3 < S > > for Matrix3 < S > {
532
+ impl < S : BaseFloat > Matrix for Matrix3 < S > {
533
+ type ColumnRow = Vector3 < S > ;
534
+
508
535
#[ inline]
509
536
fn from_value ( value : S ) -> Matrix3 < S > {
510
537
Matrix3 :: new ( value, S :: zero ( ) , S :: zero ( ) ,
@@ -620,7 +647,9 @@ impl<S: BaseFloat> Matrix<S, Vector3<S>> for Matrix3<S> {
620
647
}
621
648
}
622
649
623
- impl < S : BaseFloat > Matrix < S , Vector4 < S > > for Matrix4 < S > {
650
+ impl < S : BaseFloat > Matrix for Matrix4 < S > {
651
+ type ColumnRow = Vector4 < S > ;
652
+
624
653
#[ inline]
625
654
fn from_value ( value : S ) -> Matrix4 < S > {
626
655
Matrix4 :: new ( value, S :: zero ( ) , S :: zero ( ) , S :: zero ( ) ,
@@ -790,15 +819,19 @@ impl<S: BaseFloat> Matrix<S, Vector4<S>> for Matrix4<S> {
790
819
}
791
820
}
792
821
793
- impl < S : BaseFloat > ApproxEq < S > for Matrix2 < S > {
822
+ impl < S : BaseFloat > ApproxEq for Matrix2 < S > {
823
+ type Epsilon = S ;
824
+
794
825
#[ inline]
795
826
fn approx_eq_eps ( & self , other : & Matrix2 < S > , epsilon : & S ) -> bool {
796
827
self [ 0 ] . approx_eq_eps ( & other[ 0 ] , epsilon) &&
797
828
self [ 1 ] . approx_eq_eps ( & other[ 1 ] , epsilon)
798
829
}
799
830
}
800
831
801
- impl < S : BaseFloat > ApproxEq < S > for Matrix3 < S > {
832
+ impl < S : BaseFloat > ApproxEq for Matrix3 < S > {
833
+ type Epsilon = S ;
834
+
802
835
#[ inline]
803
836
fn approx_eq_eps ( & self , other : & Matrix3 < S > , epsilon : & S ) -> bool {
804
837
self [ 0 ] . approx_eq_eps ( & other[ 0 ] , epsilon) &&
@@ -807,7 +840,9 @@ impl<S: BaseFloat> ApproxEq<S> for Matrix3<S> {
807
840
}
808
841
}
809
842
810
- impl < S : BaseFloat > ApproxEq < S > for Matrix4 < S > {
843
+ impl < S : BaseFloat > ApproxEq for Matrix4 < S > {
844
+ type Epsilon = S ;
845
+
811
846
#[ inline]
812
847
fn approx_eq_eps ( & self , other : & Matrix4 < S > , epsilon : & S ) -> bool {
813
848
self [ 0 ] . approx_eq_eps ( & other[ 0 ] , epsilon) &&
0 commit comments