def test_rotate(self): self.assertEqual(rotate(40, Vector(1.0, 0.0, 0.0)), rotate_x(40)) self.assertEqual(rotate(20, Vector(0.0, 1.0, 0.0)), rotate_y(20)) self.assertEqual(rotate(70, Vector(0.0, 0.0, 1.0)), rotate_z(70))
def test_decompose(self): vector_translate = Vector(10.0, 20.0, 30.0) matrix_translate = translate(vector_translate) matrix_rotation = rotate(35.0, Vector(1.0, 2.0, 3.0)) vector_scale = Vector(1.2, 3.4, 3.2) matrix_scale = scale(vector_scale.x, vector_scale.y, vector_scale.z) transform = matrix_translate * matrix_rotation * matrix_scale vector_, quaternion_, scale_ = decompose(transform.m) self.assertEqual(vector_translate, vector_) self.assertEqual(matrix_rotation, quaternion_.to_transform()) self.assertEqual(matrix_scale.m, scale_)
def test_inverse(self): m1 = scale(2.0, 3.0, 4.0) m2 = scale(1.0/2.0, 1.0/3.0, 1.0/4.0) self.assertEqual(m1.inverse(), m2) self.assertEqual(m1.m_inv, m2.m) self.assertEqual(m2.m_inv, m1.m) m3 = translate(Point(5, 6, 7)) * scale(2, -3 , 4) * rotate(17, Vector(-1, 4, -2)) m4 = m3.inverse() identity = Transform() self.assertTrue((m3*m4).is_identity()) self.assertTrue((m4*m3).is_identity())
def test_transform(self): p = Point(1, 2, 3) p2 = translate(Point(10, 20, 30))(p) self.assertTrue(isinstance(p2, Point)) self.assertEqual(p2, Point(11, 22, 33)) v = Vector(1, 2, 3) v2 = translate(Point(10, 20, 30))(v) self.assertTrue(isinstance(v2, Vector)) self.assertEqual(v2, Vector(1, 2, 3)) self.assertEqual(scale(2, 3, 4)(Point(1, 2, 3)), Point(2, 6, 12)) self.assertEqual(scale(2, 3, 4)(Vector(1, 2, 3)), Vector(2, 6, 12)) self.assertEqual(rotate(90, Vector(0, 1, 0))(Normal(1, 0, 0)), Normal(0, 0, -1))
def test_transform_handedness(self): m1 = translate(Point(-17, 2, 31)) * scale(0.5, 6 , 1.4) * rotate(35, Vector(-15, 20, 0.2)) self.assertFalse(m1.swap_handedness()) m2 = translate(Point(5, 6, 7)) * scale(2, -3 , 4) * rotate(17, Vector(-1, 4, -2)) self.assertTrue(m2.swap_handedness())