def test_hit3(self): s = Sphere() i1 = Intersection(-2, s) i2 = Intersection(-1, s) xs = Intersections([i1, i2]) i = xs.hit() self.assertTrue(i == None)
def test_hit1(self): s = Sphere() i1 = Intersection(1, s) i2 = Intersection(2, s) xs = Intersections([i1, i2]) i = xs.hit() self.assertTrue(i == i1)
def test_int2(self): s = Sphere() i1 = Intersection(1, s) i2 = Intersection(2, s) xs = Intersections([i1, i2]) self.assertEqual(xs.count, 2) self.assertEqual(xs[0].t, 1) self.assertEqual(xs[1].t, 2)
def test_hit4(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]) i = xs.hit() self.assertTrue(i == i4)
def test_intersect5(self): origin = Point(0, 0, 5) direction = Vector(0, 0, 1) r = Ray(origin, direction) s = Sphere() i1 = Intersection(-6, s) i2 = Intersection(-4, s) xs = Intersections([i1, i2]) self.assertTrue(s.intersect(r) == xs)
def test_schlick_approximation_with_perpendicular_viewing_angle(self): shape = GlassSphere() r = Ray(Point(0, 0, 0), Vector(0, 1, 0)) ls = [ Intersection(-1, shape), Intersection(1, shape) ] xs = Intersections(ls) comps = xs[1].prepare_computations(r, xs) reflectance = comps.schlick() self.assertTrue(equals(reflectance, 0.04))
def local_intersect(self, ray): xtmin, xtmax = self.check_axis(ray.origin.x, ray.direction.x) ytmin, ytmax = self.check_axis(ray.origin.y, ray.direction.y) ztmin, ztmax = self.check_axis(ray.origin.z, ray.direction.z) tmin = max(xtmin, ytmin, ztmin) tmax = min(xtmax, ytmax, ztmax) if tmin > tmax: return Intersections([]) ls = [Intersection(tmin, self), Intersection(tmax, self)] xs = Intersections(ls) return xs
def test_schlick_approximation_under_total_internal_reflection(self): shape = GlassSphere() r = Ray(Point(0, 0, sqrt(2)/2), Vector(0, 1, 0)) ls = [ Intersection(-sqrt(2)/2, shape), Intersection(sqrt(2)/2, shape) ] xs = Intersections(ls) comps = xs[1].prepare_computations(r, xs) reflectance = comps.schlick() self.assertTrue(equals(reflectance, 1.0))
def test_refracted_color_opaque_surface(self): w = World() shape = w.objs[0] r = Ray(Point(0, 0, -5), Vector(0, 0, -1)) ls = [ Intersection(4, shape), Intersection(6, shape) ] xs = Intersections(ls) comps = xs[0].prepare_computations(r, xs) c = w.refracted_color(comps, 5) self.assertTrue(Color(0, 0, 0) == c)
def test_refracted_color_at_maximum_recursive_depth(self): w = World() shape = w.objs[0] shape.material.transparency = 1.0 shape.material.refractive_index = 1.5 r = Ray(Point(0, 0, -5), Vector(0, 0, -1)) ls = [ Intersection(4, shape), Intersection(6, shape) ] xs = Intersections(ls) comps = xs[0].prepare_computations(r, xs) c = w.refracted_color(comps, 0) self.assertTrue(Color(0, 0, 0) == c)
def local_intersect(self, ray): if abs(ray.direction.y) < EPSILON: return Intersections([]) else: t1 = -ray.origin.y / ray.direction.y i1 = Intersection(t1, self) return Intersections([i1])
def test_refracted_color_under_total_internal_reflection(self): w = World() shape = w.objs[0] shape.material.transparency = 1.0 shape.material.refractive_index = 1.5 r = Ray(Point(0, 0, sqrt(2)/2), Vector(0, 1, 0)) ls = [ Intersection(-sqrt(2)/2, shape), Intersection(sqrt(2)/2, shape) ] xs = Intersections(ls) comps = xs[1].prepare_computations(r, xs) c = w.refracted_color(comps, 5) self.assertTrue(c == Color(0, 0, 0))
def local_intersect(self, ray): # ray2 = self.transform.inverse() * ray # ray = Ray(self.transform.inverse()*ray.origin, self.transform.inverse()*ray.direction) sphere2ray = ray.origin - Point(0, 0, 0) a = ray.direction.dot(ray.direction) b = 2 * ray.direction.dot(sphere2ray) c = sphere2ray.dot(sphere2ray) - 1 discriminant = b * b - 4 * a * c if discriminant < 0: return Intersections([]) else: t1 = (-b - sqrt(discriminant)) / (2 * a) t2 = (-b + sqrt(discriminant)) / (2 * a) i1 = Intersection(t1, self) i2 = Intersection(t2, self) return Intersections([i1, i2])
def test_world3(self): w = World() r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) shape = w.objs[0] i = Intersection(4, shape) comps = i.prepare_computations(r) c = w.shade_hit(comps) self.assertTrue(Color(0.38066, 0.47583, 0.2855).equals(c))
def test_offset1(self): r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) shape = Sphere() shape.transform = translate(0, 0, 1) i = Intersection(5, shape) comps = i.prepare_computations(r) self.assertTrue(comps.over_point.z < -EPSILON / 2) self.assertTrue(comps.point.z > comps.over_point.z)
def test_reflect1(self): w = World() r = Ray(Point(0, 0, 0), Vector(0, 0, 1)) shape = w.objs[1] shape.material.abmient = 1 i = Intersection(1, shape) comps = i.prepare_computations(r) color = w.reflected_color(comps) self.assertTrue(Color(0, 0, 0).equals(color))
def test_reflective2(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) self.assertTrue( Vector(0, sqrt(2) / 2, sqrt(2) / 2).equals(comps.reflectv))
def test_maximum_recursive_depth(self): w = World() shape = Plane() shape.material.reflective = 0.5 shape.set_transform(translate(0, -1, 0)) 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) self.assertTrue(Color(0, 0, 0).equals(color))
def test_schlick_approximation_with_small_angle_and_n2_larger_than_n1(self): shape = GlassSphere() r = Ray(Point(0, 0.99, -2), Vector(0, 0, 1)) ls = [ Intersection(1.8589, shape), ] xs = Intersections(ls) comps = xs[0].prepare_computations(r, xs) reflectance = comps.schlick() self.assertTrue(equals(reflectance, 0.48873))
def test_prepare3(self): r = Ray(Point(0, 0, 0), Vector(0, 0, 1)) s = Sphere() i = Intersection(1, s) comps = i.prepare_computations(r) self.assertTrue(comps.inside) self.assertTrue(Point(0, 0, 1).equals(comps.point)) self.assertTrue(Vector(0, 0, -1).equals(comps.eyev)) self.assertTrue(Vector(0, 0, -1).equals(comps.normalv))
def test_prepare1(self): r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) s = Sphere() i = Intersection(4, s) comps = i.prepare_computations(r) self.assertEqual(i.t, comps.t) self.assertTrue(i.obj == comps.obj) self.assertTrue(Point(0, 0, -1).equals(comps.point)) self.assertTrue(Vector(0, 0, -1).equals(comps.eyev)) self.assertTrue(Vector(0, 0, -1).equals(comps.normalv))
def test_shade_hit(self): w = World() shape = Plane() shape.material.reflective = 0.5 shape.set_transform(translate(0.0, -1.0, 0.0)) w.objs.append(shape) r = Ray(Point(0.0, 0.0, -3.0), Vector(0.0, -sqrt(2)/2, sqrt(2)/2)) i = Intersection(sqrt(2), shape) comps = i.prepare_computations(r) color = w.shade_hit(comps) self.assertTrue(Color(0.87677, 0.92436, 0.82918).equals(color))
def test_reflect1(self): w = World() shape = Plane() shape.material.reflective = 0.5 shape.set_transform(translate(0.0, -1.0, 0.0)) w.objs.append(shape) r = Ray(Point(0.0, 0.0, -3.0), Vector(0.0, -sqrt(2)/2, sqrt(2)/2)) i = Intersection(sqrt(2), shape) comps = i.prepare_computations(r) color = w.reflected_color(comps) # print(color) self.assertTrue(Color(0.19032, 0.2379, 0.14274).equals(color))
def test_under_point1(self): r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) shape = GlassSphere() shape.set_transform(translate(0, 0, 1)) i = Intersection(5, shape) xs = Intersections([i]) comps = i.prepare_computations(r, xs) # print(comps.normalv) # print(comps.point) # print(comps.under_point.z) self.assertTrue(comps.under_point.z > EPSILON / 2) self.assertTrue(comps.point.z < comps.under_point.z)
def test_shadow5(self): w = World() light = Light(Point(0, 0, -10), Color(1, 1, 1)) w.light = light s1 = Sphere() s2 = Sphere() s2.transform = translate(0, 0, 10) w.objs = [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) self.assertTrue(Color(0.1, 0.1, 0.1).equals(c))
def test_refracted_color_with_refracted_ray(self): # this test failed! w = World() A = w.objs[0] A.material.ambient = 1 A.material.pattern = TestPattern() B = w.objs[1] B.material.transparency = 1.0 B.material.refractive_index = 1.5 r = Ray(Point(0, 0, 0.1), Vector(0, 1, 0)) ls = [ Intersection(-0.9899, A), Intersection(-0.4899, B), Intersection(0.4899, B), Intersection(0.9899, A) ] xs = Intersections(ls) comps = xs[2].prepare_computations(r, xs) c = w.refracted_color(comps, 5) # the expected answer is color(0, 0.99888, 0.04725) self.assertTrue(c == Color(0, 0.99888, 0.04721))
def test_refract1(self): A = GlassSphere() A.set_transform(scale(2, 2, 2)) A.material.refractive_index = 1.5 B = GlassSphere() B.set_transform(translate(0, 0, -0.25)) B.material.refractive_index = 2.0 C = GlassSphere() C.set_transform(translate(0, 0, 0.25)) C.material.refractive_index = 2.5 r = Ray(Point(0, 0, -4), Vector(0, 0, 1)) ls = [ Intersection(2, A), Intersection(2.75, B), Intersection(3.25, C), Intersection(4.75, B), Intersection(5.25, C), Intersection(6, A) ] xs = Intersections(ls) comps0 = xs[0].prepare_computations(r, xs) self.assertEqual(comps0.n1, 1.0) self.assertEqual(comps0.n2, 1.5) comps1 = xs[1].prepare_computations(r, xs) self.assertEqual(comps1.n1, 1.5) self.assertEqual(comps1.n2, 2.0) comps2 = xs[2].prepare_computations(r, xs) self.assertEqual(comps2.n1, 2.0) self.assertEqual(comps2.n2, 2.5) comps3 = xs[3].prepare_computations(r, xs) self.assertEqual(comps3.n1, 2.5) self.assertEqual(comps3.n2, 2.5) comps4 = xs[4].prepare_computations(r, xs) self.assertEqual(comps4.n1, 2.5) self.assertEqual(comps4.n2, 1.5) comps5 = xs[5].prepare_computations(r, xs) self.assertEqual(comps5.n1, 1.5) self.assertEqual(comps5.n2, 1.0)
def test_shade_hit_with_reflective_transparent_material(self): w = World() r = Ray(Point(0, 0, -3), Vector(0, -sqrt(2)/2, sqrt(2)/2)) floor = Plane() floor.set_transform(translate(0, -1, 0)) floor.material.reflective = 0.5 floor.material.transparency = 0.5 floor.material.refractive_index = 1.5 w.objs.append(floor) ball = Sphere() ball.material.color = Color(1, 0, 0) ball.material.ambient = 0.5 ball.set_transform(translate(0, -3.5, -0.5)) w.objs.append(ball) ls = [Intersection(sqrt(2), floor)] xs = Intersections(ls) comps = xs[0].prepare_computations(r, xs) color = w.shade_hit(comps, 5) # print(color) self.assertTrue(Color(0.93391, 0.69643, 0.69243) == color)
def test_shade_hit_with_transparent_material(self): w = World() floor = Plane() floor.set_transform(translate(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.set_transform(translate(0, -3.5, -0.5)) w.objs = [floor, ball] r = Ray(Point(0, 0, -3), Vector(0, -sqrt(2)/2, sqrt(2)/2)) ls = [Intersection(sqrt(2), floor)] xs = Intersections(ls) comps = xs[0].prepare_computations(r, xs) color = w.shade_hit(comps, 5) self.assertTrue(Color(0.93642, 0.68642, 0.68642) == color)
def test_prepare2(self): r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) s = Sphere() i = Intersection(4, s) comps = i.prepare_computations(r) self.assertFalse(comps.inside)