Beispiel #1
0
 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)
Beispiel #2
0
 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)
Beispiel #3
0
 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))
Beispiel #4
0
    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 |")
Beispiel #5
0
 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)
Beispiel #6
0
 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))
Beispiel #7
0
 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)
Beispiel #8
0
 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})
Beispiel #9
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)
Beispiel #10
0
 def test_change_mat_changes_vec(self):
     mat = Mat.new_identity(3)
     vec = mat[0]
     
     mat[0][1] = 123
     self.assertEqual(vec[1], 123)
Beispiel #11
0
 def test_set_mat(self):
     mat = Mat.new_identity(3)
     mat[0][1] = 123
     self.assertEqual(mat[0][1], 123)