예제 #1
0
 def test_hit_is_always_lowest_nonnegative_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)
     assert xs.hit() == i4
예제 #2
0
 def test_refracted_color_with_opaque_surface(self, default_world):
     w = default_world
     shape = w.objects[0]
     r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
     xs = Intersections(Intersection(4, shape), Intersection(6, shape))
     comps = xs[0].prepare_computations(r, xs)
     c = w.refracted_color(comps, 5)
     assert c == Color.black()
예제 #3
0
 def test_aggregate_intersections(self):
     s = Sphere()
     i1 = Intersection(1, s)
     i2 = Intersection(2, s)
     xs = Intersections(i1, i2)
     assert xs.count == 2
     assert xs[0].t == 1
     assert xs[1].t == 2
예제 #4
0
 def test_schlick_approximation_with_small_angle_and_n2_greater_than_n1(
         self, glass_sphere):
     shape = glass_sphere()
     r = Ray(Point(0, 0.99, -2), Vector(0, 0, 1))
     xs = Intersections(Intersection(1.8589, shape))
     comps = xs[0].prepare_computations(r, xs)
     reflectance = comps.schlick()
     assert reflectance == pytest.approx(0.48873, EPSILON)
예제 #5
0
 def test_schlick_approximation_with_perpendicular_viewing_angle(
         self, glass_sphere):
     shape = glass_sphere()
     r = Ray(Point(0, 0, 0), Vector(0, 1, 0))
     xs = Intersections(Intersection(-1, shape), Intersection(1, shape))
     comps = xs[1].prepare_computations(r, xs)
     reflectance = comps.schlick()
     assert reflectance == pytest.approx(0.04)
예제 #6
0
 def test_schlick_approximation_under_total_internal_reflection(
         self, glass_sphere):
     shape = glass_sphere()
     r = Ray(Point(0, 0, sqrt(2) / 2), Vector(0, 1, 0))
     xs = Intersections(Intersection(-sqrt(2) / 2, shape),
                        Intersection(sqrt(2) / 2, shape))
     comps = xs[1].prepare_computations(r, xs)
     reflectance = comps.schlick()
     assert reflectance == 1.0
예제 #7
0
 def test_under_point_is_offset_below_the_surface(self, glass_sphere):
     r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
     shape = glass_sphere()
     shape.transformation = translation(0, 0, 1)
     i = Intersection(5, shape)
     xs = Intersections(i)
     comps = i.prepare_computations(r, xs)
     assert comps.under_point.z > EPSILON / 2
     assert comps.point.z < comps.under_point.z
예제 #8
0
 def test_refracted_color_at_max_recursive_depth(self, default_world):
     w = default_world
     shape = w.objects[0]
     shape.material.transparency = 1.0
     shape.material.refractive_index = 1.5
     r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
     xs = Intersections(Intersection(4, shape), Intersection(6, shape))
     comps = xs[0].prepare_computations(r, xs)
     c = w.refracted_color(comps, 0)
     assert c == Color.black()
예제 #9
0
 def test_refracted_color_under_total_internal_reflection(self, default_world):
     w = default_world
     shape = w.objects[0]
     shape.material.transparency = 1.0
     shape.material.refractive_index = 1.5
     r = Ray(Point(0, 0, sqrt(2) / 2), Vector(0, 1, 0))
     xs = Intersections(Intersection(-sqrt(2) / 2, shape), Intersection(sqrt(2) / 2, shape))
     # NOTE: this time you're inside the sphere, so you need
     # to look at the second intersection, xs[1], not xs[0]
     comps = xs[1].prepare_computations(r, xs)
     c = w.refracted_color(comps, 5)
     assert c == Color.black()
예제 #10
0
 def test_refracted_color_with_refracted_ray(self, default_world):
     w = default_world
     a = w.objects[0]
     a.material.ambient = 1.0
     a.material.pattern = test_pattern()
     b = w.objects[1]
     b.material.transparency = 1.0
     b.material.refractive_index = 1.5
     r = Ray(Point(0, 0, 0.1), Vector(0, 1, 0))
     xs = Intersections(Intersection(-0.9899, a), Intersection(-0.4899, b),
                        Intersection(0.4899, b), Intersection(0.9899, a))
     comps = xs[2].prepare_computations(r, xs)
     c = w.refracted_color(comps, 5)
     assert c == Color(0, 0.99888, 0.04722)
예제 #11
0
 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)
예제 #12
0
 def test_finding_n1_and_n2_at_various_intersections(
         self, glass_sphere, index, n1, n2):
     a = glass_sphere()
     a.transformation = scaling(2, 2, 2)
     a.material.refractive_index = 1.5
     b = glass_sphere()
     b.transformation = translation(0, 0, -0.25)
     b.material.refractive_index = 2.0
     c = glass_sphere()
     c.transformation = translation(0, 0, 0.25)
     c.material.refractive_index = 2.5
     r = Ray(Point(0, 0, -4), Vector(0, 0, 1))
     xs = Intersections(Intersection(2, a), Intersection(2.75, b),
                        Intersection(3.25, c), Intersection(4.75, b),
                        Intersection(5.25, c), Intersection(6, a))
     comps = xs[index].prepare_computations(r, xs)
     assert comps.n1 == n1
     assert comps.n2 == n2
예제 #13
0
 def test_hit_when_all_intersections_have_negative_t(self):
     s = Sphere()
     i1 = Intersection(-2, s)
     i2 = Intersection(-1, s)
     xs = Intersections(i2, i1)
     assert xs.hit() is None
예제 #14
0
 def test_hit_when_some_intersections_have_negative_t(self):
     s = Sphere()
     i1 = Intersection(-1, s)
     i2 = Intersection(1, s)
     xs = Intersections(i2, i1)
     assert xs.hit() == i2
예제 #15
0
 def test_hit_when_all_intersections_have_positive_t(self):
     s = Sphere()
     i1 = Intersection(1, s)
     i2 = Intersection(2, s)
     xs = Intersections(i2, i1)
     assert xs.hit() == i1