def test_rotate_point_x(self): p = Point(0, 1, 0) half_quarter = Transformations.rotation_x(math.pi / 4) full_quarter = Transformations.rotation_x(math.pi / 2) self.assertEqual(Matrix.multiply_tuple(half_quarter, p), Point(0, math.sqrt(2) / 2, math.sqrt(2) / 2)) self.assertEqual(Matrix.multiply_tuple(full_quarter, p), Point(0, 0, 1))
def test_rotate_point_x_inverse(self): p = Point(0, 1, 0) half_quarter = Transformations.rotation_x(math.pi / 4) inv = Matrix.inverse(half_quarter) self.assertEqual(Matrix.multiply_tuple(inv, p), Point(0, math.sqrt(2) / 2, -math.sqrt(2) / 2))
def test_chained_transformations(self): p = Point(1, 0, 1) a = Transformations.rotation_x(math.pi / 2) b = Transformations.scaling(5, 5, 5) c = Transformations.translation(10, 5, 7) t = c.dot(b.dot(a)) self.assertEqual(Matrix.multiply_tuple(t, p), Point(15, 0, 7))
def test_tranforming_bounding_box(self): box = Bounds(Point(-1, -1, -1), Point(1, 1, 1)) matrix = Transformations.rotation_x(math.pi / 4).dot( Transformations.rotation_y(math.pi / 4)) box2 = box.transform(matrix) self.assertEqual(box2.min, Point(-1.4142, -1.7071, -1.7071)) self.assertEqual(box2.max, Point(1.4142, 1.7071, 1.7071))
def test_individual_transformations(self): p = Point(1, 0, 1) a = Transformations.rotation_x(math.pi / 2) b = Transformations.scaling(5, 5, 5) c = Transformations.translation(10, 5, 7) # rotate first p2 = Matrix.multiply_tuple(a, p) self.assertEqual(p2, Point(1, -1, 0)) # apply scaling p3 = Matrix.multiply_tuple(b, p2) self.assertEqual(p3, Point(5, -5, 0)) # apply translation p4 = Matrix.multiply_tuple(c, p3) self.assertEqual(p4, Point(15, 0, 7))
if __name__ == '__main__': # The floor is an extremely flattened sphere with a matte texture floor = Sphere() floor.transform = Transformations.scaling(10, 0.1, 10) floor.material = Material() floor.material.color = Color(1, 0.9, 0.9) floor.material.specular = 0 # The wall on the left has the same scale and color as the floor, but is also rotated and translated into place left_wall = Sphere() left_wall.transform = Transformations.translation(0, 0, 5) left_wall.transform = left_wall.transform.dot( Transformations.rotation_y(-math.pi / 4)) left_wall.transform = left_wall.transform.dot( Transformations.rotation_x(math.pi / 2)) left_wall.transform = left_wall.transform.dot( Transformations.scaling(10, 0.1, 10)) left_wall.material = floor.material # The wall on the right is identical to the left wall, but is rotated the opposite direction in y right_wall = Sphere() right_wall.transform = Transformations.translation(0, 0, 5) right_wall.transform = right_wall.transform.dot( Transformations.rotation_y(math.pi / 4)) right_wall.transform = right_wall.transform.dot( Transformations.rotation_x(math.pi / 2)) right_wall.transform = right_wall.transform.dot( Transformations.scaling(10, 0.1, 10)) right_wall.material = floor.material