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_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_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_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_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_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_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 is_shadowed(self, point): v = self.light.position - point distance = np.linalg.norm(v) direction = normalize(v) r = Ray(point, direction) intersections = self.intersect(r) h = intersections.hit() return h is not None and h.t < distance
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_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_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_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 ray_for_pixel(self, px, py): xoffset = (px + 0.5) * self.pixel_size yoffset = (py + 0.5) * self.pixel_size world_x = self._half_width - xoffset world_y = self._half_height - yoffset inverse_camera_transform = invert(self._transform) pixel = inverse_camera_transform(point(world_x, world_y, -1)) origin = inverse_camera_transform(point(0, 0, 0)) direction = normalize(pixel - origin) return Ray(origin, direction)
def test_a_ray_intersects_a_sphere_at_two_points(): xs = Sphere().intersect(Ray(point(0, 0, -5), vector(0, 0, 1))) assert(len(xs) == 2) assert(xs[0].t == 4.0) assert(xs[1].t == 6.0)
def test_the_color_when_the_ray_misses(): w = default_world() r = Ray(point(0, 0, -5), vector(0, 1, 0)) c = w.color_at(r) assert(np.allclose(black, c))
def test_the_color_when_the_ray_hits(): w = default_world() r = Ray(point(0, 0, -5), vector(0, 0, 1)) c = w.color_at(r) assert(np.allclose(color(0.38066, 0.47583, 0.2855), c, atol=0.0001))
def test_a_ray_intersects_a_sphere_at_a_tangent(): xs = Sphere().intersect(Ray(point(0, 1, -5), vector(0, 0, 1))) assert(len(xs) == 2) assert(xs[0].t == 5.0) assert(xs[1].t == 5.0)
def test_a_ray_misses_a_sphere(): xs = Sphere().intersect(Ray(point(0, 2, -5), vector(0, 0, 1))) assert(len(xs) == 0)
def test_a_ray_originates_inside_a_sphere(): xs = Sphere().intersect(Ray(point(0, 0, 0), vector(0, 0, 1))) assert(len(xs) == 2) assert(xs[0].t == -1.0) assert(xs[1].t == 1.0)
def test_a_sphere_behind_a_ary(): xs = Sphere().intersect(Ray(point(0, 0, 5), vector(0, 0, 1))) assert(len(xs) == 2) assert(xs[0].t == -6.0) assert(xs[1].t == -4.0)