def test_intersect_from_above(self): p = Plane() r = Ray(Point(0, -1, 0), Vector(0, 1, 0)) xs = p.local_intersect(r) self.assertEqual(len(xs), 1) self.assertEqual(xs[0].t, 1) self.assertEqual(xs[0].object, p)
def test_ray_from_below(): p = Plane() r = Ray(Point(0, -1, 0), Vector(0, 1, 0)) xs = p.local_intersect(r) assert len(xs) == 1 assert xs[0].t == 1 assert xs[0].object == p
def test_constant_normal(): p = Plane() n1 = p.local_normal_at(Point(0, 0, 0)) n2 = p.local_normal_at(Point(10, 0, -10)) n3 = p.local_normal_at(Point(-5, 0, 150)) assert n1 == Vector(0, 1, 0) assert n2 == Vector(0, 1, 0) assert n3 == Vector(0, 1, 0)
def test_normal_of_plane_is_constant(self): p = Plane() n1 = p.object_normal(Point(0, 0, 0)) n2 = p.object_normal(Point(10, 0, -10)) n3 = p.object_normal(Point(-5, 0, 150)) self.assertEqual(n1, Vector(0, 1, 0)) self.assertEqual(n2, Vector(0, 1, 0)) self.assertEqual(n3, Vector(0, 1, 0))
def test_reflected_color_at_max_recursive_depth(self, default_world): w = default_world shape = Plane() shape.material.reflective = 0.5 shape.transformation = translation(0, -1, 0) w.add(shape) r = Ray(Point(0, 0, -3), Vector(0, -sqrt(2) / 2, sqrt(2) / 2)) i = Intersection(sqrt(2), shape) comps = i.prepare_computations(r) color = w.reflected_color(comps, 0) assert color == Color.black()
def test_shade_hit_with_reflective_material(self, default_world): w = default_world shape = Plane() shape.material.reflective = 0.5 shape.transformation = translation(0, -1, 0) w.add(shape) r = Ray(Point(0, 0, -3), Vector(0, -sqrt(2) / 2, sqrt(2) / 2)) i = Intersection(sqrt(2), shape) comps = i.prepare_computations(r) color = w.shade_hit(comps) assert color == Color(0.87676, 0.92434, 0.82917)
def test_reflected_color_for_reflective_material(self, default_world): w = default_world shape = Plane() shape.material.reflective = 0.5 shape.transformation = translation(0, -1, 0) w.add(shape) r = Ray(Point(0, 0, -3), Vector(0, -sqrt(2) / 2, sqrt(2) / 2)) i = Intersection(sqrt(2), shape) comps = i.prepare_computations(r) color = w.reflected_color(comps) assert color == Color(0.19033, 0.23791, 0.14274)
def test_shade_hit_reflective(): w = World().default() s = Plane() s.material.reflective = 0.5 s.set_transform(Translation(0, -1, 0)) w.objects.append(s) r = Ray(Point(0, 0, -3), Vector(0, -math.sqrt(2) / 2, math.sqrt(2) / 2)) i = Intersection(math.sqrt(2), s) comps = i.prepare_computation(r) c = w.shade_hit(comps) assert c == Color(0.87677, 0.92436, 0.82918)
def test_maximum_recursion_depth_color_at(): w = World().default() s = Plane() s.material.reflective = 0.5 s.set_transform(Translation(0, -1, 0)) w.objects.append(s) r = Ray(Point(0, 0, -3), Vector(0, -math.sqrt(2) / 2, math.sqrt(2) / 2)) i = Intersection(math.sqrt(2), s) comps = i.prepare_computation(r) color = w.reflected_color(comps, 0) assert color == Color(0, 0, 0)
def test_reflected_color(): w = World().default() s = Plane() s.material.reflective = 0.5 s.set_transform(Translation(0, -1, 0)) w.objects.append(s) r = Ray(Point(0, 0, -3), Vector(0, -math.sqrt(2) / 2, math.sqrt(2) / 2)) i = Intersection(math.sqrt(2), s) comps = i.prepare_computation(r) c = w.reflected_color(comps) print(c) assert c == Color(0.19032, 0.2379, 0.14274)
def test_shade_hit_with_transparent_material(self, default_world): w = default_world floor = Plane() floor.transformation = translation(0, -1, 0) floor.material.transparency = 0.5 floor.material.refractive_index = 1.5 ball = Sphere() ball.material.color = Color(1, 0, 0) ball.material.ambient = 0.5 ball.transformation = translation(0, -3.5, -0.5) w.add(floor, ball) r = Ray(Point(0, 0, -3), Vector(0, -sqrt(2) / 2, sqrt(2) / 2)) xs = Intersections(Intersection(sqrt(2), floor)) comps = xs[0].prepare_computations(r, xs) color = w.shade_hit(comps, 5) assert color == Color(0.93642, 0.68642, 0.68642)
def shade_hit_transparent(): w = World().default() floor = Plane() floor.set_transform(Translation(0, -1, 0)) floor.material.transparency = 0.5 floor.material.refractive_index = 1.5 w.objects.append(floor) ball = Sphere() ball.material.color = Color(1, 0, 0) ball.material.ambient = 0.5 ball.set_transform(Translation(0, -3.5, -0.5)) w.objects.append(ball) r = Ray(Point(0, 0, -3), Vector(0, -math.sqrt(2) / 2, math.sqrt(2) / 2)) xs = Intersections(Intersection(math.sqrt(2), floor)) comps = xs[0].prepare_computations(r, xs) color = w.shade_hit(comps, 5) assert color == Color(0.93642, 0.68642, 0.68642)
def test_mutually_reflective_color_at(): w = World() w.light = PointLight(Point(0, 0, 0), Color(1, 1, 1)) lower = Plane() lower.material.reflective = 1 lower.set_transform(Translation(0, -1, 0)) w.objects.append(lower) upper = Plane() upper.material.reflective = 1 upper.set_transform(Translation(0, 1, 0)) w.objects.append(upper) r = Ray(Point(0, 0, 0), Vector(0, 1, 0)) assert w.color_at(r) is not None
def test_color_at_with_mutually_reflective_surfaces(self): w = World() w.light_source = PointLight(Point(0, 0, 0), Color.white()) lower = Plane() lower.material.reflective = 1 lower.transformation = translation(0, -1, 0) upper = Plane() upper.material.reflective = 1 upper.transformation = translation(0, 1, 0) w.add(lower, upper) r = Ray(Point(0, 0, 0), Vector(0, 1, 0)) # Avoid Infinite Recursion w.color_at(r) assert True
def test_coplanar_ray(): p = Plane() r = Ray(Point(0, 0, 0), Vector(0, 0, 1)) xs = p.local_intersect(r) assert len(xs) == 0
def test_reflection_vector(): s = Plane() r = Ray(Point(0, 1, -1), Vector(0, -math.sqrt(2) / 2, math.sqrt(2) / 2)) i = Intersection(math.sqrt(2), s) comps = i.prepare_computation(r) assert comps.reflectv == Vector(0, math.sqrt(2) / 2, math.sqrt(2) / 2)
# # ====================================================== # # a white backdrop for the scene # # ====================================================== w = World() w.light = light # - add: plane # material: # color: [ 1, 1, 1 ] # ambient: 1 # diffuse: 0 # specular: 0 # transform: # - [ rotate-x, 1.5707963267948966 ] # pi/2 # - [ translate, 0, 0, 500 ] p = Plane() p.material.color = Color(1, 1, 1) p.material.ambient = 1 p.material.diffuse = 0 p.material.specular = 0 p.set_transform(Translation(0, 0, 500) * RotationX(math.pi / 2)) w.objects.append(p) # # ====================================================== # # describe the elements of the scene # # ====================================================== # - add: sphere # material: # color: [ 0.373, 0.404, 0.550 ] # diffuse: 0.2 # ambient: 0.0
from math import pi from os import sep from raytracer.camera import Camera from raytracer.canvas import write_ppm_to_file from raytracer.lights import PointLight from raytracer.patterns import * from raytracer.matrices import scaling, rotation_x, rotation_y, rotation_z, translation, view_transform from raytracer.scene import World from raytracer.shapes import Cylinder, Plane from raytracer.tuples import Color, Point, Vector if __name__ == '__main__': floor = Plane() floor.material.color = Color(.8, .8, .8) floor.material.reflective = .2 cylinder = Cylinder(closed=True) cylinder.maximum = 0.25 cylinder.material.color = Color(.8, 0, 0) cylinder.material.reflective = .6 cylinder.transformation = translation(0, 1, 0) world = World() world.add(floor, cylinder) world.light_source = PointLight(Point(-10, 10, -10), Color.white()) camera = Camera(160, 120, pi / 3) camera.transformation = view_transform(Point(0, 1.5, -5), Point( 0, 1, 0), Vector(0, 1, 0)) * rotation_y(-pi / 4) * rotation_x(-pi / 6) canvas = camera.render(world)
from math import pi from os import sep from raytracer.camera import Camera from raytracer.canvas import write_ppm_to_file from raytracer.lights import PointLight from raytracer.materials import Material from raytracer.matrices import scaling, rotation_y, rotation_x, translation, view_transform from raytracer.scene import World from raytracer.shapes import Sphere, Plane from raytracer.tuples import Color, Point, Vector if __name__ == '__main__': floor = Plane() floor.material = Material() floor.material.color = Color(1, 0.9, 0.9) floor.material.specular = 0 middle = Sphere() middle.transformation = translation(-0.5, 1, 0.5) middle.material = Material() middle.material.color = Color(0.5, 0, 0) middle.material.diffuse = 0.7 middle.material.specular = 0.3 middle.material.reflective = 1 right = Sphere() right.transformation = translation(1.5, 0.5, -0.5) * scaling(0.5, 0.5, 0.5) right.material = Material() right.material.color = Color(0.2, 0.2, 0.8) right.material.diffuse = 0.7
def test_intersect_with_coplanar(self): p = Plane() r = Ray(Point(0, 0, 0), Vector(0, 0, 1)) xs = p.local_intersect(r) self.assertEqual(len(xs), 0)
from math import pi from os import sep from raytracer.camera import Camera from raytracer.canvas import write_ppm_to_file from raytracer.lights import PointLight from raytracer.patterns import * from raytracer.matrices import scaling, rotation_y, rotation_x, translation, view_transform from raytracer.scene import World from raytracer.shapes import Sphere, Plane from raytracer.tuples import Color, Point, Vector if __name__ == '__main__': floor = Plane() floor.material.color = Color(.1, 1, .1) floor.material.reflective = 0.9 floor.material.transparency = 1 floor.material.refractive_index = 1.333 under = Sphere() under.transformation = translation(0, -.5, 0) * scaling(.25, .25, .25) under.material.color = Color(0, .8, 0) under.material.refractive_index = 1.5 under.material.ambient = .8 above = Sphere() above.transformation = translation(1, .5, 0) * scaling(.5, .5, .5) above.material.color = Color(.2, 0, 0) above.material.diffuse = .8 above.material.ambient = .5
def test_precompute_reflection_vector(self): shape = Plane() r = Ray(Point(0, 1, -1), Vector(0, -sqrt(2) / 2, sqrt(2) / 2)) i = Intersection(sqrt(2), shape) comps = i.prepare_computations(r) assert comps.reflectv == Vector(0, sqrt(2) / 2, sqrt(2) / 2)