@@ -29,7 +29,7 @@ use approx::ApproxEq;
29
29
use array:: { Array1 , Array2 , FixedArray } ;
30
30
use num:: { BaseFloat , BaseNum } ;
31
31
use point:: { Point , Point3 } ;
32
- use quaternion:: { Quaternion , ToQuaternion } ;
32
+ use quaternion:: Quaternion ;
33
33
use vector:: { Vector , EuclideanVector } ;
34
34
use vector:: { Vector2 , Vector3 , Vector4 } ;
35
35
@@ -1326,94 +1326,76 @@ impl<S: BaseFloat> ApproxEq<S> for Matrix4<S> {
1326
1326
1327
1327
// Conversion traits
1328
1328
1329
- /// Represents types which can be converted to a Matrix2
1330
- pub trait ToMatrix2 < S : BaseNum > {
1331
- /// Convert this value to a Matrix2
1332
- fn to_matrix2 ( & self ) -> Matrix2 < S > ;
1333
- }
1334
-
1335
- /// Represents types which can be converted to a Matrix3
1336
- pub trait ToMatrix3 < S : BaseNum > {
1337
- /// Convert this value to a Matrix3
1338
- fn to_matrix3 ( & self ) -> Matrix3 < S > ;
1339
- }
1340
-
1341
- /// Represents types which can be converted to a Matrix4
1342
- pub trait ToMatrix4 < S : BaseNum > {
1343
- /// Convert this value to a Matrix4
1344
- fn to_matrix4 ( & self ) -> Matrix4 < S > ;
1345
- }
1346
-
1347
- impl < S : BaseFloat > ToMatrix3 < S > for Matrix2 < S > {
1329
+ impl < S : BaseFloat > From < Matrix2 < S > > for Matrix3 < S > {
1348
1330
/// Clone the elements of a 2-dimensional matrix into the top-left corner
1349
1331
/// of a 3-dimensional identity matrix.
1350
- fn to_matrix3 ( & self ) -> Matrix3 < S > {
1351
- Matrix3 :: new ( self [ 0 ] [ 0 ] , self [ 0 ] [ 1 ] , zero ( ) ,
1352
- self [ 1 ] [ 0 ] , self [ 1 ] [ 1 ] , zero ( ) ,
1353
- zero ( ) , zero ( ) , one ( ) )
1332
+ fn from ( m : Matrix2 < S > ) -> Matrix3 < S > {
1333
+ Matrix3 :: new ( m [ 0 ] [ 0 ] , m [ 0 ] [ 1 ] , zero ( ) ,
1334
+ m [ 1 ] [ 0 ] , m [ 1 ] [ 1 ] , zero ( ) ,
1335
+ zero ( ) , zero ( ) , one ( ) )
1354
1336
}
1355
1337
}
1356
1338
1357
- impl < S : BaseFloat > ToMatrix4 < S > for Matrix2 < S > {
1339
+ impl < S : BaseFloat > From < Matrix2 < S > > for Matrix4 < S > {
1358
1340
/// Clone the elements of a 2-dimensional matrix into the top-left corner
1359
1341
/// of a 4-dimensional identity matrix.
1360
- fn to_matrix4 ( & self ) -> Matrix4 < S > {
1361
- Matrix4 :: new ( self [ 0 ] [ 0 ] , self [ 0 ] [ 1 ] , zero ( ) , zero ( ) ,
1362
- self [ 1 ] [ 0 ] , self [ 1 ] [ 1 ] , zero ( ) , zero ( ) ,
1363
- zero ( ) , zero ( ) , one ( ) , zero ( ) ,
1364
- zero ( ) , zero ( ) , zero ( ) , one ( ) )
1342
+ fn from ( m : Matrix2 < S > ) -> Matrix4 < S > {
1343
+ Matrix4 :: new ( m [ 0 ] [ 0 ] , m [ 0 ] [ 1 ] , zero ( ) , zero ( ) ,
1344
+ m [ 1 ] [ 0 ] , m [ 1 ] [ 1 ] , zero ( ) , zero ( ) ,
1345
+ zero ( ) , zero ( ) , one ( ) , zero ( ) ,
1346
+ zero ( ) , zero ( ) , zero ( ) , one ( ) )
1365
1347
}
1366
1348
}
1367
1349
1368
- impl < S : BaseFloat > ToMatrix4 < S > for Matrix3 < S > {
1350
+ impl < S : BaseFloat > From < Matrix3 < S > > for Matrix4 < S > {
1369
1351
/// Clone the elements of a 3-dimensional matrix into the top-left corner
1370
1352
/// of a 4-dimensional identity matrix.
1371
- fn to_matrix4 ( & self ) -> Matrix4 < S > {
1372
- Matrix4 :: new ( self [ 0 ] [ 0 ] , self [ 0 ] [ 1 ] , self [ 0 ] [ 2 ] , zero ( ) ,
1373
- self [ 1 ] [ 0 ] , self [ 1 ] [ 1 ] , self [ 1 ] [ 2 ] , zero ( ) ,
1374
- self [ 2 ] [ 0 ] , self [ 2 ] [ 1 ] , self [ 2 ] [ 2 ] , zero ( ) ,
1375
- zero ( ) , zero ( ) , zero ( ) , one ( ) )
1353
+ fn from ( m : Matrix3 < S > ) -> Matrix4 < S > {
1354
+ Matrix4 :: new ( m [ 0 ] [ 0 ] , m [ 0 ] [ 1 ] , m [ 0 ] [ 2 ] , zero ( ) ,
1355
+ m [ 1 ] [ 0 ] , m [ 1 ] [ 1 ] , m [ 1 ] [ 2 ] , zero ( ) ,
1356
+ m [ 2 ] [ 0 ] , m [ 2 ] [ 1 ] , m [ 2 ] [ 2 ] , zero ( ) ,
1357
+ zero ( ) , zero ( ) , zero ( ) , one ( ) )
1376
1358
}
1377
1359
}
1378
1360
1379
- impl < S : BaseFloat > ToQuaternion < S > for Matrix3 < S > {
1361
+ impl < S : BaseFloat > From < Matrix3 < S > > for Quaternion < S > {
1380
1362
/// Convert the matrix to a quaternion
1381
- fn to_quaternion ( & self ) -> Quaternion < S > {
1363
+ fn from ( mat : Matrix3 < S > ) -> Quaternion < S > {
1382
1364
// http://www.cs.ucr.edu/~vbz/resources/quatut.pdf
1383
- let trace = self . trace ( ) ;
1365
+ let trace = mat . trace ( ) ;
1384
1366
let half: S = cast ( 0.5f64 ) . unwrap ( ) ;
1385
1367
1386
1368
if trace >= zero :: < S > ( ) {
1387
1369
let s = ( one :: < S > ( ) + trace) . sqrt ( ) ;
1388
1370
let w = half * s;
1389
1371
let s = half / s;
1390
- let x = ( self [ 1 ] [ 2 ] - self [ 2 ] [ 1 ] ) * s;
1391
- let y = ( self [ 2 ] [ 0 ] - self [ 0 ] [ 2 ] ) * s;
1392
- let z = ( self [ 0 ] [ 1 ] - self [ 1 ] [ 0 ] ) * s;
1372
+ let x = ( mat [ 1 ] [ 2 ] - mat [ 2 ] [ 1 ] ) * s;
1373
+ let y = ( mat [ 2 ] [ 0 ] - mat [ 0 ] [ 2 ] ) * s;
1374
+ let z = ( mat [ 0 ] [ 1 ] - mat [ 1 ] [ 0 ] ) * s;
1393
1375
Quaternion :: new ( w, x, y, z)
1394
- } else if ( self [ 0 ] [ 0 ] > self [ 1 ] [ 1 ] ) && ( self [ 0 ] [ 0 ] > self [ 2 ] [ 2 ] ) {
1395
- let s = ( half + ( self [ 0 ] [ 0 ] - self [ 1 ] [ 1 ] - self [ 2 ] [ 2 ] ) ) . sqrt ( ) ;
1376
+ } else if ( mat [ 0 ] [ 0 ] > mat [ 1 ] [ 1 ] ) && ( mat [ 0 ] [ 0 ] > mat [ 2 ] [ 2 ] ) {
1377
+ let s = ( half + ( mat [ 0 ] [ 0 ] - mat [ 1 ] [ 1 ] - mat [ 2 ] [ 2 ] ) ) . sqrt ( ) ;
1396
1378
let w = half * s;
1397
1379
let s = half / s;
1398
- let x = ( self [ 0 ] [ 1 ] - self [ 1 ] [ 0 ] ) * s;
1399
- let y = ( self [ 2 ] [ 0 ] - self [ 0 ] [ 2 ] ) * s;
1400
- let z = ( self [ 1 ] [ 2 ] - self [ 2 ] [ 1 ] ) * s;
1380
+ let x = ( mat [ 0 ] [ 1 ] - mat [ 1 ] [ 0 ] ) * s;
1381
+ let y = ( mat [ 2 ] [ 0 ] - mat [ 0 ] [ 2 ] ) * s;
1382
+ let z = ( mat [ 1 ] [ 2 ] - mat [ 2 ] [ 1 ] ) * s;
1401
1383
Quaternion :: new ( w, x, y, z)
1402
- } else if self [ 1 ] [ 1 ] > self [ 2 ] [ 2 ] {
1403
- let s = ( half + ( self [ 1 ] [ 1 ] - self [ 0 ] [ 0 ] - self [ 2 ] [ 2 ] ) ) . sqrt ( ) ;
1384
+ } else if mat [ 1 ] [ 1 ] > mat [ 2 ] [ 2 ] {
1385
+ let s = ( half + ( mat [ 1 ] [ 1 ] - mat [ 0 ] [ 0 ] - mat [ 2 ] [ 2 ] ) ) . sqrt ( ) ;
1404
1386
let w = half * s;
1405
1387
let s = half / s;
1406
- let x = ( self [ 0 ] [ 1 ] - self [ 1 ] [ 0 ] ) * s;
1407
- let y = ( self [ 1 ] [ 2 ] - self [ 2 ] [ 1 ] ) * s;
1408
- let z = ( self [ 2 ] [ 0 ] - self [ 0 ] [ 2 ] ) * s;
1388
+ let x = ( mat [ 0 ] [ 1 ] - mat [ 1 ] [ 0 ] ) * s;
1389
+ let y = ( mat [ 1 ] [ 2 ] - mat [ 2 ] [ 1 ] ) * s;
1390
+ let z = ( mat [ 2 ] [ 0 ] - mat [ 0 ] [ 2 ] ) * s;
1409
1391
Quaternion :: new ( w, x, y, z)
1410
1392
} else {
1411
- let s = ( half + ( self [ 2 ] [ 2 ] - self [ 0 ] [ 0 ] - self [ 1 ] [ 1 ] ) ) . sqrt ( ) ;
1393
+ let s = ( half + ( mat [ 2 ] [ 2 ] - mat [ 0 ] [ 0 ] - mat [ 1 ] [ 1 ] ) ) . sqrt ( ) ;
1412
1394
let w = half * s;
1413
1395
let s = half / s;
1414
- let x = ( self [ 2 ] [ 0 ] - self [ 0 ] [ 2 ] ) * s;
1415
- let y = ( self [ 1 ] [ 2 ] - self [ 2 ] [ 1 ] ) * s;
1416
- let z = ( self [ 0 ] [ 1 ] - self [ 1 ] [ 0 ] ) * s;
1396
+ let x = ( mat [ 2 ] [ 0 ] - mat [ 0 ] [ 2 ] ) * s;
1397
+ let y = ( mat [ 1 ] [ 2 ] - mat [ 2 ] [ 1 ] ) * s;
1398
+ let z = ( mat [ 0 ] [ 1 ] - mat [ 1 ] [ 0 ] ) * s;
1417
1399
Quaternion :: new ( w, x, y, z)
1418
1400
}
1419
1401
}
0 commit comments