Example #1
0
 def test_hit_should_offset_the_point(self):
     r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
     shape = Sphere()
     shape.transformation = translation(0, 0, 1)
     i = Intersection(5, shape)
     comps = i.prepare_computations(r)
     assert comps.over_point.z < -EPSILON / 2
     assert comps.point.z > comps.over_point.z
Example #2
0
 def test_shading_intersection(self, default_world):
     w = default_world
     r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
     shape = w.objects[0]
     i = Intersection(4, shape)
     comps = i.prepare_computations(r)
     c = w.shade_hit(comps)
     assert c == Color(0.38066, 0.47583, 0.2855)
Example #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
Example #4
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()
Example #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)
Example #6
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
Example #7
0
 def test_hit_when_intersection_occurs_on_inside(self):
     r = Ray(Point(0, 0, 0), Vector(0, 0, 1))
     shape = Sphere()
     i = Intersection(1, shape)
     comps = i.prepare_computations(r)
     assert comps.point == Point(0, 0, 1)
     assert comps.eyev == Vector(0, 0, -1)
     assert comps.inside
     assert comps.normalv == Vector(0, 0, -1)
Example #8
0
 def test_reflected_color_for_nonreflective_material(self, default_world):
     w = default_world
     r = Ray(Point(0, 0, 0), Vector(0, 0, 1))
     shape = w.objects[1]
     shape.material.ambient = 1
     i = Intersection(1, shape)
     comps = i.prepare_computations(r)
     color = w.reflected_color(comps)
     assert color == Color.black()
Example #9
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
Example #10
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
Example #11
0
 def test_shading_intersection_from_inside(self, default_world):
     w = default_world
     w.light_source = PointLight(Point(0, 0.25, 0), Color.white())
     r = Ray(Point(0, 0, 0), Vector(0, 0, 1))
     shape = w.objects[1]
     i = Intersection(0.5, shape)
     comps = i.prepare_computations(r)
     c = w.shade_hit(comps)
     assert c == Color(0.90498, 0.90498, 0.90498)
Example #12
0
 def test_precomputing_state_of_intersection(self):
     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 == i.object
     assert comps.point == Point(0, 0, -1)
     assert comps.eyev == Vector(0, 0, -1)
     assert comps.normalv == Vector(0, 0, -1)
Example #13
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()
Example #14
0
 def test_reflected_color_at_max_recursive_depth(self, default_world):
     w = default_world
     shape = Plane()
     shape.material.reflective = 0.5
     shape.transformation = translation(0, -1, 0)
     w.add(shape)
     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)
     assert color == Color.black()
Example #15
0
 def test_reflected_color_for_reflective_material(self, default_world):
     w = default_world
     shape = Plane()
     shape.material.reflective = 0.5
     shape.transformation = translation(0, -1, 0)
     w.add(shape)
     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)
     assert color == Color(0.19033, 0.23791, 0.14274)
Example #16
0
 def test_shade_hit_with_reflective_material(self, default_world):
     w = default_world
     shape = Plane()
     shape.material.reflective = 0.5
     shape.transformation = translation(0, -1, 0)
     w.add(shape)
     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.shade_hit(comps)
     assert color == Color(0.87676, 0.92434, 0.82917)
Example #17
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()
Example #18
0
 def test_shade_hit_is_given_an_intersection_in_shadow(self):
     w = World()
     w.light_source = PointLight(Point(0, 0, -10), Color.white())
     s1 = Sphere()
     s2 = Sphere()
     s2.transformation = translation(0, 0, 10)
     w.add(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 c == Color(0.1, 0.1, 0.1)
Example #19
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)
Example #20
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)
Example #21
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)
Example #22
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
Example #23
0
 def test_intersection_can_encapsulate_u_and_v(self):
     s = Triangle(Point(0, 1, 0), Point(-1, 0, 0), Point(1, 0, 0))
     i = Intersection(3.5, s, 0.2, 0.4)
     assert i.u == 0.2
     assert i.v == 0.4
Example #24
0
 def test_intersection_encapsulates_t_and_object(self):
     s = Sphere()
     i = Intersection(3.5, s)
     assert i.t == 3.5
     assert i.object == s
Example #25
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
Example #26
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
Example #27
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
Example #28
0
 def test_hit_when_intersection_occurs_on_outside(self):
     r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
     shape = Sphere()
     i = Intersection(4, shape)
     comps = i.prepare_computations(r)
     assert not comps.inside
Example #29
0
 def test_precompute_reflection_vector(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)
     assert comps.reflectv == Vector(0, sqrt(2) / 2, sqrt(2) / 2)