@@ -553,15 +553,70 @@ impl<T: Copy + fmt::Debug, Src, Dst> fmt::Debug for TypedMatrix4D<T, Src, Dst> {
553553
554554#[ cfg( test) ]
555555mod tests {
556- use point:: Point2D ;
556+ use point:: { Point2D , Point3D } ;
557+ use matrix2d:: Matrix2D ;
557558 use Radians ;
558559 use super :: * ;
560+ use approxeq:: ApproxEq ;
561+
562+ use std:: f32:: consts:: FRAC_PI_2 ;
559563
560564 type Mf32 = Matrix4D < f32 > ;
561565
562566 // For convenience.
563567 fn rad ( v : f32 ) -> Radians < f32 > { Radians :: new ( v) }
564568
569+ #[ test]
570+ pub fn test_translation ( ) {
571+ let t1 = Mf32 :: create_translation ( 1.0 , 2.0 , 3.0 ) ;
572+ let t2 = Mf32 :: identity ( ) . pre_translated ( 1.0 , 2.0 , 3.0 ) ;
573+ let t3 = Mf32 :: identity ( ) . post_translated ( 1.0 , 2.0 , 3.0 ) ;
574+ assert_eq ! ( t1, t2) ;
575+ assert_eq ! ( t1, t3) ;
576+
577+ assert_eq ! ( t1. transform_point3d( & Point3D :: new( 1.0 , 1.0 , 1.0 ) ) , Point3D :: new( 2.0 , 3.0 , 4.0 ) ) ;
578+ assert_eq ! ( t1. transform_point( & Point2D :: new( 1.0 , 1.0 ) ) , Point2D :: new( 2.0 , 3.0 ) ) ;
579+
580+ assert_eq ! ( t1. post_mul( & t1) , Mf32 :: create_translation( 2.0 , 4.0 , 6.0 ) ) ;
581+
582+ assert ! ( !t1. is_2d( ) ) ;
583+ assert_eq ! ( Mf32 :: create_translation( 1.0 , 2.0 , 3.0 ) . to_2d( ) , Matrix2D :: create_translation( 1.0 , 2.0 ) ) ;
584+ }
585+
586+ #[ test]
587+ pub fn test_rotation ( ) {
588+ let r1 = Mf32 :: create_rotation ( 0.0 , 0.0 , 1.0 , rad ( FRAC_PI_2 ) ) ;
589+ let r2 = Mf32 :: identity ( ) . pre_rotated ( 0.0 , 0.0 , 1.0 , rad ( FRAC_PI_2 ) ) ;
590+ let r3 = Mf32 :: identity ( ) . post_rotated ( 0.0 , 0.0 , 1.0 , rad ( FRAC_PI_2 ) ) ;
591+ assert_eq ! ( r1, r2) ;
592+ assert_eq ! ( r1, r3) ;
593+
594+ assert ! ( r1. transform_point3d( & Point3D :: new( 1.0 , 2.0 , 3.0 ) ) . approx_eq( & Point3D :: new( 2.0 , -1.0 , 3.0 ) ) ) ;
595+ assert ! ( r1. transform_point( & Point2D :: new( 1.0 , 2.0 ) ) . approx_eq( & Point2D :: new( 2.0 , -1.0 ) ) ) ;
596+
597+ assert ! ( r1. post_mul( & r1) . approx_eq( & Mf32 :: create_rotation( 0.0 , 0.0 , 1.0 , rad( FRAC_PI_2 * 2.0 ) ) ) ) ;
598+
599+ assert ! ( r1. is_2d( ) ) ;
600+ assert ! ( r1. to_2d( ) . approx_eq( & Matrix2D :: create_rotation( rad( FRAC_PI_2 ) ) ) ) ;
601+ }
602+
603+ #[ test]
604+ pub fn test_scale ( ) {
605+ let s1 = Mf32 :: create_scale ( 2.0 , 3.0 , 4.0 ) ;
606+ let s2 = Mf32 :: identity ( ) . pre_scaled ( 2.0 , 3.0 , 4.0 ) ;
607+ let s3 = Mf32 :: identity ( ) . post_scaled ( 2.0 , 3.0 , 4.0 ) ;
608+ assert_eq ! ( s1, s2) ;
609+ assert_eq ! ( s1, s3) ;
610+
611+ assert ! ( s1. transform_point3d( & Point3D :: new( 2.0 , 2.0 , 2.0 ) ) . approx_eq( & Point3D :: new( 4.0 , 6.0 , 8.0 ) ) ) ;
612+ assert ! ( s1. transform_point( & Point2D :: new( 2.0 , 2.0 ) ) . approx_eq( & Point2D :: new( 4.0 , 6.0 ) ) ) ;
613+
614+ assert_eq ! ( s1. post_mul( & s1) , Mf32 :: create_scale( 4.0 , 9.0 , 16.0 ) ) ;
615+
616+ assert ! ( !s1. is_2d( ) ) ;
617+ assert_eq ! ( Mf32 :: create_scale( 2.0 , 3.0 , 0.0 ) . to_2d( ) , Matrix2D :: create_scale( 2.0 , 3.0 ) ) ;
618+ }
619+
565620 #[ test]
566621 pub fn test_ortho ( ) {
567622 let ( left, right, bottom, top) = ( 0.0f32 , 1.0f32 , 0.1f32 , 1.0f32 ) ;
@@ -596,6 +651,24 @@ mod tests {
596651 assert_eq ! ( m1, m2) ;
597652 }
598653
654+ #[ test]
655+ fn test_column_major ( ) {
656+ assert_eq ! (
657+ Mf32 :: row_major(
658+ 1.0 , 2.0 , 3.0 , 4.0 ,
659+ 5.0 , 6.0 , 7.0 , 8.0 ,
660+ 9.0 , 10.0 , 11.0 , 12.0 ,
661+ 13.0 , 14.0 , 15.0 , 16.0 ,
662+ ) ,
663+ Mf32 :: column_major(
664+ 1.0 , 5.0 , 9.0 , 13.0 ,
665+ 2.0 , 6.0 , 10.0 , 14.0 ,
666+ 3.0 , 7.0 , 11.0 , 15.0 ,
667+ 4.0 , 8.0 , 12.0 , 16.0 ,
668+ )
669+ ) ;
670+ }
671+
599672 #[ test]
600673 pub fn test_inverse_simple ( ) {
601674 let m1 = Mf32 :: identity ( ) ;
@@ -638,10 +711,36 @@ mod tests {
638711 assert ! ( p3. eq( & p1) ) ;
639712 }
640713
714+ #[ test]
715+ fn test_inverse_none ( ) {
716+ assert ! ( Mf32 :: create_scale( 2.0 , 0.0 , 2.0 ) . inverse( ) . is_none( ) ) ;
717+ assert ! ( Mf32 :: create_scale( 2.0 , 2.0 , 2.0 ) . inverse( ) . is_some( ) ) ;
718+ }
719+
641720 #[ test]
642721 pub fn test_pre_post ( ) {
643722 let m1 = Matrix4D :: identity ( ) . post_scaled ( 1.0 , 2.0 , 3.0 ) . post_translated ( 1.0 , 2.0 , 3.0 ) ;
644723 let m2 = Matrix4D :: identity ( ) . pre_translated ( 1.0 , 2.0 , 3.0 ) . pre_scaled ( 1.0 , 2.0 , 3.0 ) ;
645724 assert ! ( m1. approx_eq( & m2) ) ;
725+
726+ let r = Mf32 :: create_rotation ( 0.0 , 0.0 , 1.0 , rad ( FRAC_PI_2 ) ) ;
727+ let t = Mf32 :: create_translation ( 2.0 , 3.0 , 0.0 ) ;
728+
729+ let a = Point3D :: new ( 1.0 , 1.0 , 1.0 ) ;
730+
731+ assert ! ( r. post_mul( & t) . transform_point3d( & a) . approx_eq( & Point3D :: new( 3.0 , 2.0 , 1.0 ) ) ) ;
732+ assert ! ( t. post_mul( & r) . transform_point3d( & a) . approx_eq( & Point3D :: new( 4.0 , -3.0 , 1.0 ) ) ) ;
733+ assert ! ( t. post_mul( & r) . transform_point3d( & a) . approx_eq( & r. transform_point3d( & t. transform_point3d( & a) ) ) ) ;
734+
735+ assert ! ( r. pre_mul( & t) . transform_point3d( & a) . approx_eq( & Point3D :: new( 4.0 , -3.0 , 1.0 ) ) ) ;
736+ assert ! ( t. pre_mul( & r) . transform_point3d( & a) . approx_eq( & Point3D :: new( 3.0 , 2.0 , 1.0 ) ) ) ;
737+ assert ! ( t. pre_mul( & r) . transform_point3d( & a) . approx_eq( & t. transform_point3d( & r. transform_point3d( & a) ) ) ) ;
738+ }
739+
740+ #[ test]
741+ fn test_size_of ( ) {
742+ use std:: mem:: size_of;
743+ assert_eq ! ( size_of:: <Matrix4D <f32 >>( ) , 16 * size_of:: <f32 >( ) ) ;
744+ assert_eq ! ( size_of:: <Matrix4D <f64 >>( ) , 16 * size_of:: <f64 >( ) ) ;
646745 }
647746}
0 commit comments