def test_rotate_point_y(self): p = Point(0, 0, 1) half_quarter = Transformations.rotation_y(math.pi / 4) full_quarter = Transformations.rotation_y(math.pi / 2) self.assertEqual(Matrix.multiply_tuple(half_quarter, p), Point(math.sqrt(2) / 2, 0, math.sqrt(2) / 2)) self.assertEqual(Matrix.multiply_tuple(full_quarter, p), Point(1, 0, 0))
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_ray_camera_transformed(self): c = Camera(201, 101, math.pi / 2) c.transform = Transformations.rotation_y(math.pi / 4).dot( Transformations.translation(0, -2, 5)) r = Camera.ray_for_pixel(c, 100, 50) self.assertEqual(r.origin, Point(0, 2, -5)) self.assertEqual(r.direction, Vector(math.sqrt(2) / 2, 0, -math.sqrt(2) / 2))
def test_converting_normal_from_object_to_world_space(self): g1 = Group() g1.transform = Transformations.rotation_y(math.pi / 2) g2 = Group() g2.transform = Transformations.scaling(1, 2, 3) g1.add_child(g2) s = Sphere() s.transform = Transformations.translation(5, 0, 0) g2.add_child(s) n = s.normal_to_world(Vector(math.sqrt(3) /3, math.sqrt(3) / 3, math.sqrt(3) / 3)) self.assertEqual(n, Vector(0.2857, 0.4286, -0.8571))
def test_converting_point_from_world_to_object_space(self): g1 = Group() g1.transform = Transformations.rotation_y(math.pi / 2) g2 = Group() g2.transform = Transformations.scaling(2, 2, 2) g1.add_child(g2) s = Sphere() s.transform = Transformations.translation(5, 0, 0) g2.add_child(s) p = s.world_to_object(Point(-2, 0, -10)) self.assertEqual(p, Point(0, 0, -1))
def test_finding_normal_on_child_object(self): g1 = Group() g1.transform = Transformations.rotation_y(math.pi / 2) g2 = Group() g2.transform = Transformations.scaling(1, 2, 3) g1.add_child(g2) s = Sphere() s.transform = Transformations.translation(5, 0, 0) g2.add_child(s) n = s.normal_at(Point(1.7321, 1.1547, -5.5774)) self.assertEqual(n, Vector(0.2857, 0.4286, -0.8571))
from tuple import Point, Vector from world import World 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))