def test_mat_rows_are_vecs(self): mat = Mat( [1, 2, 3], [4, 5, 6], [7, 8, 9], ) self.assertEqual(mat.row(0), Vec(1, 2, 3))
def test_eq(self): mat1 = Mat(3) mat2 = Mat.new_identity(3) self.assertNotEqual(mat1, mat2) mat3 = Mat.new_identity(3) self.assertEqual(mat2, mat3)
def test_change_vec_changes_mat(self): correct = Mat( [1, 123, 0], [0, 1, 0], [0, 0, 1], ) mat = Mat.new_identity(3) vec = mat[1] vec[0] = 123 self.assertEqual(mat, correct) mat = Mat.new_identity(3) vec = mat.col(1) vec[0] = 123 self.assertEqual(mat, correct) mat = Mat.new_identity(3) vec = mat.row(0) vec[1] = 123 self.assertEqual(mat, correct) mat = Mat.new_identity(3) vec = mat[1] vec.x = 123 self.assertEqual(mat, correct)
def test_mat_cols_are_vecs(self): mat = Mat( [1, 2, 3], [4, 5, 6], [7, 8, 9], ) self.assertEqual(mat[0], Vec(1, 4, 7)) self.assertEqual(mat.col(0), Vec(1, 4, 7))
def test_copy_is_unlinked(self): m = Mat.new_identity(3) v = m[1] v = v.copy() v[0] = 123 self.assertEqual(v, Vec(123, 1, 0)) self.assertEqual(m, Mat.new_identity(3))
def test_copy_to(self): m1 = Mat( [1, 2, 3], [4, 5, 6], [7, 8, 9], ) m2 = Mat.new_identity(3) m1.copy_to(m2) self.assertEqual(m1, m2)
def test_popped(self): mat = Mat( [1, 2, 3], [4, 5, 6], [7, 8, 9], ) correct = Mat( [1, 2, 3], [4, 5, 6] ) self.assertEqual(mat.popped(), correct)
def test_rot_mat(self): rot = Mat.new_rotation_3d() ident = Mat.new_identity(4) # a rotation matrix with no rotations is just the basis vectors self.assertEqual(rot, ident) vec = Vec(1, 0, 0, 1) self.assertEqual(rot * vec, Vec(1, 0, 0, 1)) rot = Mat.new_rotation_3d(0, -pi/2, 0) self.assertEqual(rot * vec, Vec(0, 0, 1, 1))
def test_appended_col(self): mat = Mat( [1, 2, 3], [4, 5, 6], [7, 8, 9] ) mat = mat.appended_col(Vec(1, 2, 3)) correct = Mat( [1, 2, 3, 1], [4, 5, 6, 2], [7, 8, 9, 3], ) self.assertEqual(mat, correct)
def test_appended(self): mat = Mat( [1, 2, 3], [4, 5, 6], [7, 8, 9] ) mat = mat.appended(Vec(1, 2, 3)) correct = Mat( [1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 2, 3] ) self.assertEqual(mat, correct)
def test_normalize_row(self): mat = Mat( [1, 2, 3], [0, 2, 0], [1, 0, 0], ) correct = Mat( [1, 2, 3], [0, 1, 0], [1, 0, 0], ) vec = mat.row(1) vec.normalize_in_place() self.assertEqual(mat, correct)
def test_scale_mat(self): mat = Mat.new_scale_3d(0.5, 2.0, 1) correct = Mat( [0.5, 0, 0, 0], [0, 2.0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], ) self.assertEqual(mat, correct) vec = Vec(4, 1, 3, 1) self.assertEqual(mat * vec, Vec(2, 2, 3, 1))
def test_trans_mat(self): mat = Mat.new_translation_3d(1, 2, 3) correct = Mat( [1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3], [0, 0, 0, 1] ) self.assertEqual(mat, correct) vec = Vec(1, 0, 0, 1) self.assertEqual(mat * vec, Vec(2, 2, 3, 1))
def test_identity(self): mat = Mat.new_identity(2) self.assertEqual(mat[0][0], 1) self.assertEqual(mat[1][0], 0) self.assertEqual(mat[0][1], 0) self.assertEqual(mat[1][1], 1) mat = Mat.new_identity(3) self.assertEqual(mat[0][0], 1) self.assertEqual(mat[1][0], 0) self.assertEqual(mat[2][0], 0) self.assertEqual(mat[0][1], 0) self.assertEqual(mat[1][1], 1) self.assertEqual(mat[2][1], 0) self.assertEqual(mat[0][2], 0) self.assertEqual(mat[1][2], 0) self.assertEqual(mat[2][2], 1)
def test_print(self): mat = Mat([ [1.234, 9, 39], [3, 165, 0.1], ]) self.assertEqual(str(mat), "| 1.23 9 39 |\n| 3 165 0.10 |") mat = Mat.new_identity(4) self.assertEqual(str(mat), "| 1 0 0 0 |\n| 0 1 0 0 |\n| 0 0 1 0 |\n| 0\ 0 0 1 |") mat[1][1] = 348290482.23984 self.assertEqual(str(mat), "| 1 0 0 \ 0 |\n| 0 348290482.24 0 0 |\n| \ 0 0 1 0 |\n| 0 0\ 0 1 |") # try some fancy character renderings mat = Mat( [pi, pi/2, pi/4], ) self.assertEqual(str(mat.friendly()), "| \xcf\x80 \xcf\x80/2 \xcf\x80/4 |") mat = Mat.new_rotation_3d(radians(45), radians(30), radians(270)) self.assertEqual(str(mat.friendly()), "| 0 cos(\xcf\x80/6) \ sin(\xcf\x80/6) 0 |\n| sin(-\xcf\x80/4) 0.35 -0.61 0 \ |\n| sin(-\xcf\x80/4) -0.35 0.61 0 |\n| 0 0 \ 0 1 |") self.assertEqual(str(mat), "| -0.00 0.87 0.50 0.00 |\n| -0.71 \ 0.35 -0.61 0.00 |\n| -0.71 -0.35 0.61 0.00 |\n| 0.00 0.00 0.00 1.00 |")
def test_transpose(self): mat = Mat( [1, 2] ) correct = Mat( [1], [2], ) self.assertEqual(mat.transpose(), correct) mat = Mat( [1, 2], [3, 4], ) correct = Mat( [1, 3], [2, 4] ) self.assertEqual(mat.transpose(), correct) mat = Mat( [1, 2], [3, 4], [5, 6], ) correct = Mat( [1, 3, 5], [2, 4, 6] ) self.assertEqual(mat.transpose(), correct)
def test_to_dict(self): self.assertEqual(Vec(1, 19, 3).to_dict(), {"x": 1, "y": 19, "z": 3}) self.assertEqual(Mat.new_identity(3).row(0).to_dict(), {"x": 1, "y": 0, "z": 0})
def test_len_list(self): self.assertEqual(len(Vec(1, 2, 3)), 3) self.assertEqual(len(Vec(4, 2)), 2) self.assertEqual(len(Vec(1, 2, 3, 4)), 4) self.assertEqual(len(Mat.new_identity(3).row(0)), 3)
def test_set_mat(self): mat = Mat.new_identity(3) mat[0][1] = 123 self.assertEqual(mat[0][1], 123)
def test_change_mat_changes_vec(self): mat = Mat.new_identity(3) vec = mat[0] mat[0][1] = 123 self.assertEqual(vec[1], 123)