def test_computing_normal_on_translated_sphere(self): s = Sphere() s.set_transform(translation(0, 1, 0)) n = s.normal_at(point(0, 1.70711, -0.70711)) self.assert_tuple_equals(vector(0, 0.70711, -0.70711), n, 0.001)
def test_default_world(self): light = PointLight(point(-10, 10, -10), color(1, 1, 1)) s1 = Sphere() s2 = Sphere() s1.material.color = color(0.8, 1.0, 0.6) s1.material.diffuse = 0.7 s1.material.specular = 0.2 s2.set_transform(scaling(0.5, 0.5, 0.5)) world = default_world() self.assertEqual(light.position, world.light.position) self.assertEqual(light.intensity, world.light.intensity) self.assertEqual(s1.material.color, world.objects[0].material.color) self.assertEqual(s1.material.diffuse, world.objects[0].material.diffuse) self.assertEqual(s1.material.specular, world.objects[0].material.specular) self.assertEqual(s2.material.color, world.objects[1].material.color) self.assertEqual(s2.material.diffuse, world.objects[1].material.diffuse) self.assertEqual(s2.material.specular, world.objects[1].material.specular)
def test_computing_normal_on_scaled_sphere(self): s = Sphere() s.set_transform(scaling(1, 0.5, 1)) n = s.normal_at(point(0, math.sqrt(2) / 2, -math.sqrt(2) / 2)) self.assert_tuple_equals(vector(0, 0.97014, -0.24254), n, 0.001)
def test_ray_originates_inside_sphere(self): r = Ray(point(0, 0, 0), vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) self.assertEqual(2, len(xs)) self.assertEqual(-1, xs[0].t) self.assertEqual(1, xs[1].t)
def test_sphere_behind_ray(self): r = Ray(point(0, 0, 5), vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) self.assertEqual(2, len(xs)) self.assertEqual(-6, xs[0].t) self.assertEqual(-4, xs[1].t)
def test_intersecting_translated_sphere_with_ray(self): r = Ray(point(0, 0, -5), vector(0, 0, 1)) s = Sphere() s.set_transform(translation(5, 0, 0)) xs = s.intersect(r) self.assertEqual(0, len(xs))
def test_ray_intersects_sphere_at_tangent(self): r = Ray(point(0, 1, -5), vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) self.assertEqual(2, len(xs)) self.assertEqual(5, xs[0].t) self.assertEqual(5, xs[1].t)
def test_normal_is_normalized_vector(self): s = Sphere() n = s.normal_at( point(math.sqrt(3) / 3, math.sqrt(3) / 3, math.sqrt(3) / 3)) self.assertEqual(n.normalize(), n)
def test_normal_on_sphere_at_point_on_none_axial_point(self): s = Sphere() n = s.normal_at( point(math.sqrt(3) / 3, math.sqrt(3) / 3, math.sqrt(3) / 3)) self.assertEqual(n.normalize(), n)
def test_intersecting_scaled_sphere_with_ray(self): r = Ray(point(0, 0, -5), vector(0, 0, 1)) s = Sphere() s.set_transform(scaling(2, 2, 2)) xs = s.intersect(r) self.assertEqual(2, len(xs)) self.assertEqual(3, xs[0].t) self.assertEqual(7, xs[1].t)
def default_world(): light = PointLight(point(-10, 10, -10), color(1, 1, 1)) s1 = Sphere() s2 = Sphere() s1.material.color = color(0.8, 1.0, 0.6) s1.material.diffuse = 0.7 s1.material.specular = 0.2 s2.set_transform(scaling(0.5, 0.5, 0.5)) return World(light=light, objects=[s1, s2])
def test_hit_when_some_intersections_have_negative_t(self): s = Sphere() i1 = Intersection(-1, s) i2 = Intersection(1, s) xs = intersections(i1, i2) h = hit(xs) self.assertEqual(i2, h)
def test_intersection_occurs_on_the_outside(self): ray = Ray(point(0, 0, -5), vector(0, 0, 1)) shape = Sphere() xs = Intersection(4, shape) xs.prepare_hit(ray) self.assertEqual(False, xs.inside)
def test_hit_when_all_intersections_have_negative_t(self): s = Sphere() i1 = Intersection(-2, s) i2 = Intersection(-1, s) xs = intersections(i1, i2) h = hit(xs) self.assertIsNone(h)
def test_hit_when_all_intersections_have_positive_t(self): s = Sphere() i1 = Intersection(1, s) i2 = Intersection(2, s) xs = intersections(i1, i2) h = hit(xs) self.assertEqual(i1, h)
def test_precomputing_state_of_intersection(self): ray = Ray(point(0, 0, -5), vector(0, 0, 1)) shape = Sphere() xs = Intersection(4, shape) xs.prepare_hit(ray) self.assertEqual(point(0, 0, -1), xs.point) self.assertEqual(vector(0, 0, -1), xs.eyev) self.assertEqual(vector(0, 0, -1), xs.normalv)
def test_hit_is_always__lowest_negative_intersection(self): s = Sphere() i1 = Intersection(5, s) i2 = Intersection(7, s) i3 = Intersection(-3, s) i4 = Intersection(2, s) xs = intersections(i1, i2, i3, i4) h = hit(xs) self.assertEqual(i4, h)
def test_intersection_occurs_on_the_inside(self): ray = Ray(point(0, 0, 0), vector(0, 0, 1)) shape = Sphere() xs = Intersection(1, shape) xs.prepare_hit(ray) self.assertEqual(point(0, 0, 1), xs.point) self.assertEqual(vector(0, 0, -1), xs.eyev) self.assertEqual(True, xs.inside) self.assertEqual(vector(0, 0, -1), xs.normalv)
import time from math import pi from renderer.camera import Camera, render from renderer.lights import PointLight from renderer.materials import Material from renderer.sphere import Sphere from renderer.transformations import scaling, translation, rotation_y, rotation_x, view_transform from renderer.tuples import point, color, vector from renderer.world import World floor = Sphere() floor.transform = scaling(10, 0.01, 10) floor.material.color = color(1, 0.9, 0.9) floor.material.specular = 0 left_wall = Sphere() left_wall.transform = translation(0, 0, 5) * rotation_y(-pi / 4) * rotation_x( pi / 2) * scaling(10, 0.01, 10) left_wall.material.color = color(1, 0.9, 0.9) left_wall.material.specular = 0 right_wall = Sphere() right_wall.transform = translation(0, 0, 5) * rotation_y(pi / 4) * rotation_x( pi / 2) * scaling(10, 0.01, 10) right_wall.material.color = color(1, 0.9, 0.9) right_wall.material.specular = 0 middle = Sphere() middle.transform = translation(-0.5, 1, 0.5) middle.material.color = color(0.1, 1, 0.5) middle.material.ambient = 0.1
def test_normal_on_sphere_at_point_on_y_axis(self): s = Sphere() n = s.normal_at(point(0, 1, 0)) self.assertEqual(vector(0, 1, 0), n)
def test_set_transform(self): s = Sphere() t = translation(2, 3, 4) s.set_transform(t) self.assertEqual(t, s.transform)
def test_sphere_default_transformation(self): s = Sphere() self.assertEqual(identity_matrix, s.transform)
import time from renderer.canvas import Canvas from renderer.intersections import hit from renderer.rays import Ray from renderer.sphere import Sphere from renderer.tuples import point, color s = Sphere() ray_origin = point(0, 0, -5) wall_z = 10 wall_size = 7.0 canvas_pixels = 1000 pixel_size = wall_size / canvas_pixels half = wall_size / 2 c = Canvas(canvas_pixels, canvas_pixels) col = color(1, 1, 0) start_time = time.time() for y in range(canvas_pixels): elapsed_time = time.time() - start_time if elapsed_time > 0 and y > 0: print("time_to_finish: " + str(1.0 * (canvas_pixels - y) / (1.0 * y / elapsed_time))) world_y = half - pixel_size * y for x in range(canvas_pixels): world_x = -half + pixel_size * x position = point(world_x, world_y, wall_z) ray = Ray(ray_origin, (position - ray_origin)) xs = s.intersect(ray)
def test_ray_misses_sphere(self): r = Ray(point(0, 2, -5), vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) self.assertEqual(0, len(xs))
def test_sphere_may_be_assigned_material(self): s = Sphere() m = Material(ambient=1) s.material = m self.assertEqual(m, s.material)
import time from renderer.canvas import Canvas from renderer.intersections import hit from renderer.lights import PointLight from renderer.rays import Ray from renderer.sphere import Sphere from renderer.transformations import scaling from renderer.tuples import point, color s = Sphere() s.set_transform(scaling(0.5, 1, 1)) s.material.color = color(1, 0.2, 1) light_position = point(-10, 10, -10) light_color = color(1, 1, 1) light = PointLight(light_position, light_color) ray_origin = point(0, 0, -5) wall_z = 10 wall_size = 7.0 canvas_pixels = 1000 pixel_size = wall_size / canvas_pixels half = wall_size / 2 c = Canvas(canvas_pixels, canvas_pixels) col = color(1, 0, 0) start_time = time.time() for y in range(canvas_pixels): elapsed_time = time.time() - start_time if elapsed_time > 0 and y > 0: print("time_to_finish: " + str(1.0 * (canvas_pixels - y) /
def test_sphere_has_default_material(self): s = Sphere() m = s.material self.assertEqual(Material(), m)