def test_reflecting_a_vector_approaching_at_45_degrees(): v = vector(1, -1, 0) n = vector(0, 1, 0) r = reflect(v, n) assert ((vector(1, 1, 0) == r).all())
def test_reflecting_a_vector_off_a_slanted_surface(): v = vector(0, -1, 0) n = vector(math.sqrt(2) / 2, math.sqrt(2) / 2, 0) r = reflect(v, n) assert (np.allclose(vector(1, 0, 0), r))
def test_lighting_with_the_surface_in_shadow(material, position): eyev = vector(0, 0, -1) normalv = vector(0, 0, -1) light = PointLight(point(0, 0, -10), color(1, 1, 1)) in_shadow = True result = material.lighting(light, position, eyev, normalv, in_shadow) assert (np.allclose(color(0.1, 0.1, 0.1), result))
def test_multiplying_the_inverse_of_a_scaling_matrix(): transform = transformations.scaling(2, 3, 4) inverse = transformations.invert(transform) v = vector(-4, 6, 8) expected = vector(-2, 2, 2) actual = inverse(v) assert ((expected == actual).all())
def test_lighting_with_the_light_behind_the_surface(material, position): eyev = vector(0, 0, -1) normalv = vector(0, 0, -1) light = PointLight(point(0, 0, 10), color(1, 1, 1)) result = material.lighting(light, position, eyev, normalv, False) assert (np.array_equal(color(0.1, 0.1, 0.1), result))
def test_lighting_with_the_eye_in_the_path_of_the_reflection_vector( material, position): eyev = vector(0, -math.sqrt(2) / 2, -math.sqrt(2) / 2) normalv = vector(0, 0, -1) light = PointLight(point(0, 10, -10), color(1, 1, 1)) result = material.lighting(light, position, eyev, normalv, False) assert (np.allclose(color(1.6364, 1.6364, 1.6364), result))
def test_lighting_with_the_eye_opposite_surface_light_offest_45_degrees( material, position): eyev = vector(0, 0, -1) normalv = vector(0, 0, -1) light = PointLight(point(0, 10, -10), color(1, 1, 1)) result = material.lighting(light, position, eyev, normalv, False) assert (np.allclose(color(0.7364, 0.7364, 0.7364), result))
def test_lighting_with_the_eye_between_the_light_and_the_surface_and_wyw_offset_45_degrees( material, position): eyev = vector(0, math.sqrt(2) / 2, -math.sqrt(2) / 2) normalv = vector(0, 0, -1) light = PointLight(point(0, 0, -10), color(1, 1, 1)) result = material.lighting(light, position, eyev, normalv, False) assert (np.array_equal(color(1.0, 1.0, 1.0), result))
def test_translating_a_ray(): origin = point(1, 2, 3) direction = vector(0, 1, 0) r = Ray(origin, direction) m = transformations.translation(3, 4, 5) translated_ray = r.transform(m) assert (np.allclose(point(4, 6, 8), translated_ray.origin)) assert (np.allclose(vector(0, 1, 0), translated_ray.direction))
def test_scaling_a_ray(): origin = point(1, 2, 3) direction = vector(0, 1, 0) r = Ray(origin, direction) m = transformations.scaling(2, 3, 4) scaled_ray = r.transform(m) assert (np.allclose(point(2, 6, 12), scaled_ray.origin)) assert (np.allclose(vector(0, 3, 0), scaled_ray.direction))
def test_the_hit_when_an_intersection_occurs_on_the_inside(): r = Ray(point(0, 0, 0), vector(0, 0, 1)) shape = Sphere() i = Intersection(1, shape) comps = i.prepare_computations(r) assert(np.allclose(point(0, 0, 1), comps.point)) assert(np.allclose(vector(0, 0, -1), comps.eyev)) assert(comps.inside == True) assert(np.allclose(vector(0, 0, -1), comps.normalv))
def test_precompute_the_state_of_an_intersection(): r = Ray(point(0, 0, -5), vector(0, 0, 1)) shape = Sphere() i = Intersection(4, shape) comps = i.prepare_computations(r) assert(comps.t == i.t) assert(comps.object is i.object) assert(np.allclose(point(0, 0, -1), comps.point)) assert(np.allclose(vector(0, 0, -1), comps.eyev)) assert(np.allclose(vector(0, 0, -1), comps.normalv))
def test_intersecting_a_translated_sphere_with_a_ray(): sphere = Sphere(transformation=transformations.translation(5, 0, 0)) ray = Ray(point(0, 0, -5), vector(0, 0, 1)) xs = sphere.intersect(ray) assert(len(xs) == 0)
def test_hit_should_offset_the_point(): r = Ray(point(0, 0, -5), vector(0, 0, 1)) shape = Sphere(transformation=translation(0, 0, 1)) i = Intersection(5, shape) comps = i.prepare_computations(r) assert(comps.point[2] < -EPSILON/2)
def test_constructing_a_ray_when_the_camera_is_transformed(): c = Camera(201, 101, math.pi/2, transform=concat(rotation_y(math.pi/4), translation(0, -2, 5))) r = c.ray_for_pixel(100, 50) assert(np.allclose(point(0, 2, -5), r.origin)) assert(np.allclose(vector(math.sqrt(2)/2, 0, -math.sqrt(2)/2), r.direction))
def test_the_hit_when_an_intersection_occurs_on_the_outside(): r = Ray(point(0, 0, -5), vector(0, 0, 1)) shape = Sphere() i = Intersection(4, shape) comps = i.prepare_computations(r) assert(comps.inside == False)
def test_intersecting_a_scaled_sphere_with_a_ray(): sphere = Sphere(transformation=transformations.scaling(2, 2, 2)) ray = Ray(point(0, 0, -5), vector(0, 0, 1)) xs = sphere.intersect(ray) assert(xs[0].t == 3) assert(xs[1].t == 7)
def test_computing_the_normal_on_a_transformed_sphere(): s = transformations.scaling(1, 0.5, 1) r = transformations.rotation_z(math.pi/5) transform = transformations.concat(s, r) sphere = Sphere(transform) n = sphere.normal_at(point(0, math.sqrt(2)/2, -math.sqrt(2)/2)) assert(np.allclose(vector(0, 0.97014, -0.242535), n))
def test_intersect_sets_the_object_on_the_intersection(): r = Ray(point(0, 0, -5), vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) assert(len(xs) == 2) assert(xs[0].object is s) assert(xs[1].object is s)
def test_computing_a_point_from_a_distance(): origin = point(2, 3, 4) direction = vector(1, 0, 0) r = Ray(origin, direction) assert ((point(2, 3, 4) == r.position(0)).all()) assert ((point(3, 3, 4) == r.position(1)).all()) assert ((point(1, 3, 4) == r.position(-1)).all()) assert ((point(4.5, 3, 4) == r.position(2.5)).all())
def test_shading_an_intersection(): w = default_world() r = Ray(point(0, 0, -5), vector(0, 0, 1)) shape = w[0] i = Intersection(4, shape) comps = i.prepare_computations(r) c = w.shade_hit(comps) assert(np.allclose(color(0.38066, 0.47583, 0.2855), c, atol=0.0001))
def test_the_view_transformation_moves_the_world(): from_ = point(0, 0, 8) to = point(0, 0, 0) up = vector(0, 1, 0) t = view_transformation(from_, to, up) actual_transform_matrix = t(identity_matrix()) expected_transform_matrix = translation(0, 0, -8)(identity_matrix()) assert (np.allclose(actual_transform_matrix, expected_transform_matrix))
def test_the_color_with_an_intersection_behind_the_ray(): w = default_world() outer = w[0] outer._material._ambient = 1 inner = w[1] inner._material._ambient = 1 r = Ray(point(0, 0, 0.75), vector(0, 0, -1)) c = w.color_at(r) assert(np.allclose(c, inner.material.color))
def test_shading_an_intersection_from_the_inside(): w = default_world() w._light = PointLight(point(0, 0.25, 0), color(1, 1, 1)) r = Ray(point(0, 0, 0), vector(0, 0, 1)) shape = w[1] i = Intersection(0.5, shape) comps = i.prepare_computations(r) c = w.shade_hit(comps) assert(np.allclose(color(0.90498, 0.90498, 0.90498), c))
def test_intersect_a_world_with_a_ray(): w = default_world() r = Ray(point(0, 0, -5), vector(0, 0, 1)) xs = w.intersect(r) assert(len(xs) == 4) assert(xs[0].t == 4) assert(xs[1].t == 4.5) assert(xs[2].t == 5.5) assert(xs[3].t == 6)
def test_a_transformation_matrix_looking_in_positive_z_direction(): from_ = point(0, 0, 0) to = point(0, 0, 1) up = vector(0, 1, 0) t = view_transformation(from_, to, up) actual_transform_matrix = t(identity_matrix()) expected_transform_matrix = scaling(-1, 1, -1)(identity_matrix()) assert (np.allclose(actual_transform_matrix, expected_transform_matrix))
def test_rendering_a_world_with_a_camera(): w = default_world() from_ = point(0, 0, -5) to = point(0, 0, 0) up = vector(0, 1, 0) c = Camera(11, 11, math.pi/2, transform=view_transformation(from_, to, up)) image = c.render(w) assert(np.allclose(image.pixel_at(5, 5), color( 0.38066, 0.47583, 0.2855), atol=0.00001))
def test_shade_hit_is_given_an_intersection_in_shadow(): light = PointLight(point(0, 0, -10), color(1, 1, 1)) s1 = Sphere() s2 = Sphere(transformation=translation(0, 0, 10)) w = World(light, s1, s2) r = Ray(point(0, 0, 5), vector(0, 0, 1)) i = Intersection(4, s2) comps = i.prepare_computations(r) c = w.shade_hit(comps) assert(np.allclose(color(0.1, 0.1, 0.1), c))
def test_the_view_transformation_matrix_for_the_default_orientation(): from_ = point(0, 0, 0) to = point(0, 0, -1) up = vector(0, 1, 0) t = view_transformation(from_, to, up) actual_transform_matrix = t(identity_matrix()) expected_transform_matrix = identity_matrix() assert (np.allclose(actual_transform_matrix, expected_transform_matrix))
def test_an_arbitrary_view_transformation(): from_ = point(1, 3, 2) to = point(4, -2, 8) up = vector(1, 1, 0) view_transform = view_transformation(from_, to, up) actual_transform_matrix = view_transform(identity_matrix()) expected_transform_matrix = np.array([ [-0.50709, 0.50709, 0.67612, -2.36643], [0.76772, 0.60609, 0.12122, -2.82843], [-0.35857, 0.59761, -0.71714, 0], [0, 0, 0, 1], ]) # assert(actual_transform_matrix[0][0] == expected_transform_matrix[0][0]) assert (np.allclose(actual_transform_matrix, expected_transform_matrix, atol=0.0001))