Changeset View
Changeset View
Standalone View
Standalone View
tests/python/bl_pyapi_mathutils.py
| Show First 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | class MatrixTesting(unittest.TestCase): | ||||
| def test_matrix_translation(self): | def test_matrix_translation(self): | ||||
| mat = Matrix() | mat = Matrix() | ||||
| mat.translation = Vector((1, 2, 3)) | mat.translation = Vector((1, 2, 3)) | ||||
| self.assertEqual(mat[0][3], 1) | self.assertEqual(mat[0][3], 1) | ||||
| self.assertEqual(mat[1][3], 2) | self.assertEqual(mat[1][3], 2) | ||||
| self.assertEqual(mat[2][3], 3) | self.assertEqual(mat[2][3], 3) | ||||
| def test_non_square_mult(self): | def test_matrix_non_square_matmul(self): | ||||
| mat1 = Matrix(((1, 2, 3), | mat1 = Matrix(((1, 2, 3), | ||||
| (4, 5, 6))) | (4, 5, 6))) | ||||
| mat2 = Matrix(((1, 2), | mat2 = Matrix(((1, 2), | ||||
| (3, 4), | (3, 4), | ||||
| (5, 6))) | (5, 6))) | ||||
| prod_mat1 = Matrix(((22, 28), | prod_mat1 = Matrix(((22, 28), | ||||
| (49, 64))) | (49, 64))) | ||||
| prod_mat2 = Matrix(((9, 12, 15), | prod_mat2 = Matrix(((9, 12, 15), | ||||
| (19, 26, 33), | (19, 26, 33), | ||||
| (29, 40, 51))) | (29, 40, 51))) | ||||
| self.assertEqual(mat1 * mat2, prod_mat1) | self.assertEqual(mat1 @ mat2, prod_mat1) | ||||
| self.assertEqual(mat2 * mat1, prod_mat2) | self.assertEqual(mat2 @ mat1, prod_mat2) | ||||
| def test_mat4x4_vec3D_mult(self): | def test_mat4x4_vec3D_matmul(self): | ||||
| mat = Matrix(((1, 0, 2, 0), | mat = Matrix(((1, 0, 2, 0), | ||||
| (0, 6, 0, 0), | (0, 6, 0, 0), | ||||
| (0, 0, 1, 1), | (0, 0, 1, 1), | ||||
| (0, 0, 0, 1))) | (0, 0, 0, 1))) | ||||
| vec = Vector((1, 2, 3)) | vec = Vector((1, 2, 3)) | ||||
| prod_mat_vec = Vector((7, 12, 4)) | prod_mat_vec = Vector((7, 12, 4)) | ||||
| prod_vec_mat = Vector((1, 12, 5)) | prod_vec_mat = Vector((1, 12, 5)) | ||||
| self.assertEqual(mat * vec, prod_mat_vec) | self.assertEqual(mat @ vec, prod_mat_vec) | ||||
| self.assertEqual(vec * mat, prod_vec_mat) | self.assertEqual(vec @ mat, prod_vec_mat) | ||||
| def test_mat_vec_mult(self): | def test_mat_vec_matmul(self): | ||||
| mat1 = Matrix() | mat1 = Matrix() | ||||
| vec = Vector((1, 2)) | vec = Vector((1, 2)) | ||||
| self.assertRaises(ValueError, mat1.__mul__, vec) | self.assertRaises(ValueError, mat1.__matmul__, vec) | ||||
| self.assertRaises(ValueError, vec.__mul__, mat1) | self.assertRaises(ValueError, vec.__matmul__, mat1) | ||||
| mat2 = Matrix(((1, 2), | mat2 = Matrix(((1, 2), | ||||
| (-2, 3))) | (-2, 3))) | ||||
| prod = Vector((5, 4)) | prod = Vector((5, 4)) | ||||
| self.assertEqual(mat2 * vec, prod) | self.assertEqual(mat2 @ vec, prod) | ||||
| def test_matrix_square_matmul(self): | |||||
| mat1 = Matrix(((1, 0), | |||||
| (1, 2))) | |||||
| mat2 = Matrix(((1, 2), | |||||
| (-2, 3))) | |||||
| prod1 = Matrix(((1, 2), | |||||
| (-3, 8))) | |||||
| prod2 = Matrix(((3, 4), | |||||
| (1, 6))) | |||||
| self.assertEqual(mat1 @ mat2, prod1) | |||||
| self.assertEqual(mat2 @ mat1, prod2) | |||||
| """ | |||||
| # tests for element-wise multiplication | |||||
| def test_matrix_mul(self): | |||||
| mat1 = Matrix(((1, 0), | |||||
| (1, 2))) | |||||
| mat2 = Matrix(((1, 2), | |||||
| (-2, 3))) | |||||
| mat3 = Matrix(((1, 0, 2, 0), | |||||
| (0, 6, 0, 0), | |||||
| (0, 0, 1, 1), | |||||
| (0, 0, 0, 1))) | |||||
| prod = Matrix(((1, 0), | |||||
| (-2, 6))) | |||||
| self.assertEqual(mat1 * mat2, prod) | |||||
| self.assertEqual(mat2 * mat1, prod) | |||||
| self.assertRaises(ValueError, mat1.__mul__, mat3) | |||||
| """ | |||||
| def test_matrix_inverse(self): | def test_matrix_inverse(self): | ||||
| mat = Matrix(((1, 4, 0, -1), | mat = Matrix(((1, 4, 0, -1), | ||||
| (2, -1, 2, -2), | (2, -1, 2, -2), | ||||
| (0, 3, 8, 3), | (0, 3, 8, 3), | ||||
| (-2, 9, 1, 0))) | (-2, 9, 1, 0))) | ||||
| inv_mat = (1 / 285) * Matrix(((195, -57, 27, -102), | inv_mat = (1 / 285) * Matrix(((195, -57, 27, -102), | ||||
| Show All 21 Lines | def test_matrix_inverse_safe(self): | ||||
| inv_mat_safe = Matrix(((1.0, -0.5, 0.0, -0.5), | inv_mat_safe = Matrix(((1.0, -0.5, 0.0, -0.5), | ||||
| (0.222222, -0.111111, -0.0, 0.0), | (0.222222, -0.111111, -0.0, 0.0), | ||||
| (-333333344.0, 316666656.0, 100000000.0, 150000000.0), | (-333333344.0, 316666656.0, 100000000.0, 150000000.0), | ||||
| (0.888888, -0.9444444, 0.0, -0.5))) | (0.888888, -0.9444444, 0.0, -0.5))) | ||||
| ''' | ''' | ||||
| self.assertEqual(mat.inverted_safe(), inv_mat_safe) | self.assertEqual(mat.inverted_safe(), inv_mat_safe) | ||||
| def test_matrix_mult(self): | def test_matrix_matmult(self): | ||||
| mat = Matrix(((1, 4, 0, -1), | mat = Matrix(((1, 4, 0, -1), | ||||
| (2, -1, 2, -2), | (2, -1, 2, -2), | ||||
| (0, 3, 8, 3), | (0, 3, 8, 3), | ||||
| (-2, 9, 1, 0))) | (-2, 9, 1, 0))) | ||||
| prod_mat = Matrix(((11, -9, 7, -9), | prod_mat = Matrix(((11, -9, 7, -9), | ||||
| (4, -3, 12, 6), | (4, -3, 12, 6), | ||||
| (0, 48, 73, 18), | (0, 48, 73, 18), | ||||
| (16, -14, 26, -13))) | (16, -14, 26, -13))) | ||||
| self.assertEqual(mat * mat, prod_mat) | self.assertEqual(mat @ mat, prod_mat) | ||||
| class VectorTesting(unittest.TestCase): | class VectorTesting(unittest.TestCase): | ||||
| def test_orthogonal(self): | def test_orthogonal(self): | ||||
| angle_90d = math.pi / 2.0 | angle_90d = math.pi / 2.0 | ||||
| for v in vector_data: | for v in vector_data: | ||||
| v = Vector(v) | v = Vector(v) | ||||
| if v.length_squared != 0.0: | if v.length_squared != 0.0: | ||||
| self.assertAlmostEqual(v.angle(v.orthogonal()), angle_90d) | self.assertAlmostEqual(v.angle(v.orthogonal()), angle_90d) | ||||
| def test_vector_matmul(self): | |||||
| # produces dot product for vectors | |||||
| vec1 = Vector((1, 3, 5)) | |||||
| vec2 = Vector((1, 2)) | |||||
| self.assertRaises(ValueError, vec1.__matmul__, vec2) | |||||
| self.assertEqual(vec1 @ vec1, 35) | |||||
| self.assertEqual(vec2 @ vec2, 5) | |||||
| def test_vector_imatmul(self): | |||||
| vec = Vector((1, 3, 5)) | |||||
| with self.assertRaises(TypeError): | |||||
| vec @= vec | |||||
| """ | |||||
| # tests for element-wise multiplication | |||||
| def test_vector_mul(self): | |||||
| # element-wise multiplication | |||||
| vec1 = Vector((1, 3, 5)) | |||||
| vec2 = Vector((1, 2)) | |||||
| prod1 = Vector((1, 9, 25)) | |||||
| prod2 = Vector((2, 6, 10)) | |||||
| self.assertRaises(ValueError, vec1.__mul__, vec2) | |||||
| self.assertEqual(vec1 * vec1, prod1) | |||||
| self.assertEqual(2 * vec1, prod2) | |||||
| def test_vector_imul(self): | |||||
| # inplace element-wise multiplication | |||||
| vec = Vector((1, 3, 5)) | |||||
| prod1 = Vector((1, 9, 25)) | |||||
| prod2 = Vector((2, 18, 50)) | |||||
| vec *= vec | |||||
| self.assertEqual(vec, prod1) | |||||
| vec *= 2 | |||||
| self.assertEqual(vec, prod2) | |||||
| """ | |||||
| class QuaternionTesting(unittest.TestCase): | class QuaternionTesting(unittest.TestCase): | ||||
| def test_to_expmap(self): | def test_to_expmap(self): | ||||
| q = Quaternion((0, 0, 1), math.radians(90)) | q = Quaternion((0, 0, 1), math.radians(90)) | ||||
| e = q.to_exponential_map() | e = q.to_exponential_map() | ||||
| self.assertAlmostEqual(e.x, 0) | self.assertAlmostEqual(e.x, 0) | ||||
| ▲ Show 20 Lines • Show All 197 Lines • Show Last 20 Lines | |||||