Blender V2.61 - r43446

bl_pyapi_mathutils.py

Go to the documentation of this file.
00001 import unittest
00002 from test import support
00003 from mathutils import Matrix, Vector
00004 
00005 
00006 class MatrixTesting(unittest.TestCase):
00007     def test_matrix_column_access(self):
00008         #mat =
00009         #[ 1  2  3  4 ]
00010         #[ 1  2  3  4 ]
00011         #[ 1  2  3  4 ]
00012         mat = Matrix(((1, 11, 111),
00013                       (2, 22, 222),
00014                       (3, 33, 333),
00015                       (4, 44, 444)))
00016 
00017         self.assertEqual(mat[0], Vector((1, 11, 111)))
00018         self.assertEqual(mat[1], Vector((2, 22, 222)))
00019         self.assertEqual(mat[2], Vector((3, 33, 333)))
00020         self.assertEqual(mat[3], Vector((4, 44, 444)))
00021 
00022     def test_item_access(self):
00023         args = ((1, 4, 0, -1),
00024                 (2, -1, 2, -2),
00025                 (0, 3, 8, 3),
00026                 (-2, 9, 1, 0))
00027 
00028         mat = Matrix(args)
00029 
00030         for row in range(4):
00031             for col in range(4):
00032                 self.assertEqual(mat[row][col], args[row][col])
00033 
00034         self.assertEqual(mat[0][2], 0)
00035         self.assertEqual(mat[3][1], 9)
00036         self.assertEqual(mat[2][3], 3)
00037         self.assertEqual(mat[0][0], 1)
00038         self.assertEqual(mat[3][3], 0)
00039 
00040     def test_item_assignment(self):
00041         mat = Matrix() - Matrix()
00042         indices = (0, 0), (1, 3), (2, 0), (3, 2), (3, 1)
00043         checked_indices = []
00044         for row, col in indices:
00045             mat[row][col] = 1
00046 
00047         for row in range(4):
00048             for col in range(4):
00049                 if mat[row][col]:
00050                     checked_indices.append((row, col))
00051 
00052         for item in checked_indices:
00053             self.assertIn(item, indices)
00054 
00055     def test_matrix_to_3x3(self):
00056         #mat =
00057         #[ 1  2  3  4  ]
00058         #[ 2  4  6  8  ]
00059         #[ 3  6  9  12 ]
00060         #[ 4  8  12 16 ]
00061         mat = Matrix(tuple((i, 2 * i, 3 * i, 4 * i) for i in range(1, 5)))
00062         mat_correct = Matrix(((1, 2, 3), (2, 4, 6), (3, 6, 9)))
00063         self.assertEqual(mat.to_3x3(), mat_correct)
00064 
00065     def test_matrix_to_translation(self):
00066         mat = Matrix()
00067         mat[0][3] = 1
00068         mat[1][3] = 2
00069         mat[2][3] = 3
00070         self.assertEqual(mat.to_translation(), Vector((1, 2, 3)))
00071 
00072     def test_matrix_translation(self):
00073         mat = Matrix()
00074         mat.translation = Vector((1, 2, 3))
00075         self.assertEqual(mat[0][3], 1)
00076         self.assertEqual(mat[1][3], 2)
00077         self.assertEqual(mat[2][3], 3)
00078 
00079     def test_non_square_mult(self):
00080         mat1 = Matrix(((1, 2, 3),
00081                        (4, 5, 6)))
00082         mat2 = Matrix(((1, 2),
00083                        (3, 4),
00084                        (5, 6)))
00085 
00086         prod_mat1 = Matrix(((22, 28),
00087                             (49, 64)))
00088         prod_mat2 = Matrix(((9, 12, 15),
00089                             (19, 26, 33),
00090                             (29, 40, 51)))
00091 
00092         self.assertEqual(mat1*mat2, prod_mat1)
00093         self.assertEqual(mat2 * mat1, prod_mat2)
00094 
00095     def test_mat4x4_vec3D_mult(self):
00096         mat = Matrix(((1, 0, 2, 0),
00097                       (0, 6, 0, 0),
00098                       (0, 0, 1, 1),
00099                       (0, 0, 0, 1)))
00100 
00101         vec = Vector((1, 2, 3))
00102         
00103         prod_mat_vec = Vector((7, 12, 4))
00104         prod_vec_mat = Vector((1, 12, 5))
00105 
00106         self.assertEqual(mat * vec, prod_mat_vec)
00107         self.assertEqual(vec * mat, prod_vec_mat)
00108 
00109     def test_mat_vec_mult(self):
00110         mat1 = Matrix()
00111 
00112         vec = Vector((1, 2))
00113 
00114         self.assertRaises(TypeError, mat1.__mul__, vec)
00115         self.assertRaises(ValueError, vec.__mul__, mat1)  # Why are these different?!
00116 
00117         mat2 = Matrix(((1, 2),
00118                        (-2, 3)))
00119 
00120         prod = Vector((5, 4))
00121 
00122         self.assertEqual(mat2 * vec, prod)
00123 
00124     def test_matrix_inverse(self):
00125         mat = Matrix(((1, 4, 0, -1),
00126                       (2, -1, 2, -2),
00127                       (0, 3, 8, 3),
00128                       (-2, 9, 1, 0)))
00129 
00130         inv_mat = (1 / 285) * Matrix(((195, -57, 27, -102),
00131                                       (50, -19, 4, 6),
00132                                       (-60, 57, 18, 27),
00133                                       (110, -133, 43, -78)))
00134 
00135         self.assertEqual(mat.inverted(), inv_mat)
00136 
00137     def test_matrix_mult(self):
00138         mat = Matrix(((1, 4, 0, -1),
00139                       (2, -1, 2, -2),
00140                       (0, 3, 8, 3),
00141                       (-2, 9, 1, 0)))
00142 
00143         prod_mat = Matrix(((11, -9, 7, -9),
00144                            (4, -3, 12, 6),
00145                            (0, 48, 73, 18),
00146                            (16, -14, 26, -13)))
00147 
00148         self.assertEqual(mat * mat, prod_mat)
00149 
00150 
00151 def test_main():
00152     try:
00153         support.run_unittest(MatrixTesting)
00154     except:
00155         import traceback
00156         traceback.print_exc()
00157 
00158         # alert CTest we failed
00159         import sys
00160         sys.exit(1)
00161 
00162 if __name__ == '__main__':
00163     test_main()