示例#1
0
def test_computing_a_point_from_a_distance():
    # Given
    r = Ray(Point(2, 3, 4), Vector(1, 0, 0))
    # Then
    assert r.position(0) == Point(2, 3, 4)
    assert r.position(1) == Point(3, 3, 4)
    assert r.position(-1) == Point(1, 3, 4)
    assert r.position(2.5) == Point(4.5, 3, 4)
示例#2
0
def test_scaling_a_ray():
    # Given
    r = Ray(Point(1, 2, 3), Vector(0, 1, 0))
    m = scaling(2, 3, 4)
    # When
    r2 = r.transform(m)
    # Then
    assert r2.origin == Point(2, 6, 12)
    assert r2.direction == Vector(0, 3, 0)
示例#3
0
def test_translating_a_ray():
    # Given
    r = Ray(Point(1, 2, 3), Vector(0, 1, 0))
    m = translation(3, 4, 5)
    # When
    r2 = r.transform(m)
    # Then
    assert r2.origin == Point(4, 6, 8)
    assert r2.direction == Vector(0, 1, 0)
def test_the_color_when_a_ray_misses__only_negative_intersection_t():
    w = World()
    w.light = PointLight(Point(-10, 10, -10), Color(1, 1, 1))
    w.objects = [Plane()]
    r = Ray(Point(0, 10, 0), Vector(0, 1, 1).normalize())
    c = w.color_at(r)
    assert c == Color(0, 0, 0)
    def reflected_color(self, comps, remaining=4):
        if comps.object.material.reflective == 0 or remaining <= 0:
            return Color(0, 0, 0)
        reflect_ray = Ray(comps.over_point, comps.reflectv)
        color = self.color_at(reflect_ray, remaining - 1)

        return color * comps.object.material.reflective
def test_intersect_with_a_coplanar_ray():
    # Given
    p = Plane()
    r = Ray(Point(0, 0, 0), Vector(0, 0, 1))
    # When
    xs = p.local_intersect(r)
    # Then
    assert len(xs) == 0
def test_intersect_with_a_ray_parallel_to_the_plane():
    # Given
    p = Plane()
    r = Ray(Point(0, 10, 0), Vector(0, 0, 1))
    # When
    xs = p.local_intersect(r)
    # Then
    assert len(xs) == 0
def test_the_color_when_a_ray_misses():
    # Given
    w = default_world()
    r = Ray(Point(0, 0, -5), Vector(0, 1, 0))
    # When
    c = w.color_at(r)
    # Then
    assert c == Color(0, 0, 0)
def test_a_ray_misses_s_sphere():
    # Given
    r = Ray(Point(0, 2, -5), Vector(0, 0, 1))
    s = Sphere()
    # When
    xs = s.intersect(r)
    # Then
    assert len(xs) == 0
def test_the_color_when_a_ray_hits():
    # Given
    w = default_world()
    r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
    # When
    c = w.color_at(r)
    # Then
    assert c == Color(0.38066, 0.47583, 0.2855)
def test_intersectinga_ray_with_an_empty_group():
    # Given
    g = Group()
    r = Ray(Point(0, 0, 0), Vector(0, 0, 1))
    # When
    xs = g.local_intersect(r)
    # Then
    assert len(xs) == 0
示例#12
0
def precomputing_the_reflection_vector():
    # Given
    shape = Plane()
    r = Ray(Point(0, 1, -1), Vector(0, -sqrt(2) / 2, sqrt(2) / 2))
    i = Intersection(sqrt(2), shape)
    # When
    comps = i.prepare_computations(r)
    # Then
    assert comps.reflectv == Vector(0, sqrt(2) / 2, sqrt(2) / 2)
示例#13
0
def test_the_hit__when_an_intersection_occurs_on_the_outside():
    # Given
    r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
    shape = Sphere()
    i = Intersection(4, shape)
    # When
    comps = i.prepare_computations(r)
    # Then
    assert comps.inside is False
def test_intersecting_a_translated_sphere_with_a_ray():
    # Given
    r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
    s = Sphere()
    # When
    s.transform = translation(5, 0, 0)
    xs = s.intersect(r)
    # Then
    assert len(xs) == 0
示例#15
0
def test_creating_and_querying_a_ray():
    # Given
    origin = Point(1, 2, 3)
    direction = Vector(4, 5, 6)
    # When
    r = Ray(origin, direction)
    # Then
    assert r.origin == origin
    assert r.direction == direction
示例#16
0
def test_intersecting_a_scaled_shape_with_a_ray():
    # Given
    r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
    s = MockShape()
    # When
    s.transform = scaling(2, 2, 2)
    _ = s.intersect(r)
    # Then
    assert s.saved_ray.origin == Point(0, 0, -2.5)
    assert s.saved_ray.direction == Vector(0, 0, 0.5)
def test_a_ray_intersects_a_sphere_at_two_points():
    # Given
    r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
    s = Sphere()
    # When
    xs = s.intersect(r)
    # Then
    assert len(xs) == 2
    assert xs[0].t == 4.0
    assert xs[1].t == 6.0
def test_intersect_sets_the_object_on_the_intersection():
    # Given
    r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
    s = Sphere()
    # When
    xs = s.intersect(r)
    # Then
    assert len(xs) == 2
    assert xs[0].object == s
    assert xs[1].object == s
def test_a_sphere_is_behind_a_ray():
    # Given
    r = Ray(Point(0, 0, 5), Vector(0, 0, 1))
    s = Sphere()
    # When
    xs = s.intersect(r)
    # Then
    assert len(xs) == 2
    assert xs[0].t == -6.0
    assert xs[1].t == -4.0
def test_a_ray_originates_inside_a_sphere():
    # Given
    r = Ray(Point(0, 0, 0), Vector(0, 0, 1))
    s = Sphere()
    # When
    xs = s.intersect(r)
    # Then
    assert len(xs) == 2
    assert xs[0].t == -1.0
    assert xs[1].t == 1.0
def test_a_ray_intersects_a_sphere_at_a_tangent():
    # Then
    r = Ray(Point(0, 1, -5), Vector(0, 0, 1))
    s = Sphere()
    # When
    xs = s.intersect(r)
    # Then
    assert len(xs) == 2
    assert xs[0].t == 5.0
    assert xs[1].t == 5.0
示例#22
0
def test_intersecting_a_cone_with_a_ray_parallel_to_one_of_its_halves():
    # Given
    shape = Cone()
    direction = Vector(0, 1, 1).normalize()
    r = Ray(Point(0, 0, -1), direction)
    # When
    xs = shape.local_intersect(r)
    # Then
    assert len(xs) == 1
    assert equal(xs[0].t, 0.35355)
def test_a_ray_intersecting_a_plane_from_below():
    # Given
    p = Plane()
    r = Ray(Point(0, -1, 0), Vector(0, 1, 0))
    # When
    xs = p.local_intersect(r)
    # Then
    assert len(xs) == 1
    assert xs[0].t == 1
    assert xs[0].object == p
示例#24
0
def test_the_schlick_approximation_with_a_perpendicular_viewing_angle():
    # Given
    shape = glass_sphere()
    r = Ray(Point(0, 0, 0), Vector(0, 1, 0))
    xs = Intersections(Intersection(-1, shape), Intersection(1, shape))
    # When
    comps = xs[1].prepare_computations(r, xs)
    reflectance = comps.schlick()
    # Then
    assert equal(reflectance, 0.04)
示例#25
0
def test_intersecting_a_translated_shape_with_a_ray():
    # Given
    r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
    s = MockShape()
    # When
    s.transform = translation(5, 0, 0)
    _ = s.intersect(r)
    # Then
    assert s.saved_ray.origin == Point(-5, 0, -5)
    assert s.saved_ray.direction == Vector(0, 0, 1)
示例#26
0
def test_the_schlick_approximation_with_small_angle_and_n2_greater_than_n1():
    # Given
    shape = glass_sphere()
    r = Ray(Point(0, 0.99, -2), Vector(0, 0, 1))
    xs = Intersections(Intersection(1.8589, shape))
    # When
    comps = xs[0].prepare_computations(r, xs)
    reflectance = comps.schlick()
    # Then
    assert equal(reflectance, 0.48873)
def test_intersecting_a_scaled_sphere_with_a_ray():
    # Given
    r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
    s = Sphere()
    # When
    s.transform = scaling(2, 2, 2)
    xs = s.intersect(r)
    # Then
    assert len(xs) == 2
    assert xs[0].t == 3
    assert xs[1].t == 7
def test_shading_an_intersection():
    # Given
    w = default_world()
    r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
    shape = w.objects[0]
    i = Intersection(4, shape)
    # When
    comps = i.prepare_computations(r)
    c = w.shade_hit(comps)
    # Then
    assert c == Color(0.38066, 0.47583, 0.2855)
示例#29
0
def test_the_hit_should_offset_the_point():
    # Given
    r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
    shape = Sphere()
    shape.transform = translation(0, 0, 1)
    i = Intersection(5, shape)
    # When
    comps = i.prepare_computations(r)
    # Then
    assert comps.over_point.z < -EPSILON / 2
    assert comps.point.z > comps.over_point.z
示例#30
0
def test_the_schlick_approximation_under_total_internal_reflection():
    # Given
    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))
    # When
    comps = xs[1].prepare_computations(r, xs)
    reflectance = comps.schlick()
    # Then
    assert reflectance == 1.0