def test_the_inverse_of_an_x_rotation_rotates_in_the_opposite_direction():
    p = point(0, 1, 0)
    half_quarter = transformations.rotation_x(math.pi / 4)
    inverse = transformations.invert(half_quarter)

    assert (np.allclose(point(0,
                              math.sqrt(2) / 2, -math.sqrt(2) / 2),
                        inverse(p)))
def test_rotating_a_point_around_the_z_axis():
    p = point(0, 1, 0)
    half_quarter = transformations.rotation_z(math.pi / 4)
    full_quarter = transformations.rotation_z(math.pi / 2)

    assert (np.allclose(point(-math.sqrt(2) / 2,
                              math.sqrt(2) / 2, 0), half_quarter(p)))
    assert (np.allclose(point(-1, 0, 0), full_quarter(p)))
def test_chained_transofrmations_must_be_applied_in_reverse_order():
    p = point(1, 0, 1)
    A = transformations.rotation_x(math.pi / 2)
    B = transformations.scaling(5, 5, 5)
    C = transformations.translation(10, 5, 7)

    CBA = transformations.concat(C, B, A)

    assert (np.allclose(point(15, 0, 7), CBA(p)))
Exemplo n.º 4
0
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())
Exemplo n.º 5
0
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))
Exemplo n.º 6
0
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_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))
Exemplo n.º 8
0
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))
Exemplo n.º 9
0
def test_rendering_a_world_with_a_camera():
    w = default_world()
    from_ = point(0, 0, -5)
    to = point(0, 0, 0)
    up = vector(0, 1, 0)
    c = Camera(11, 11, math.pi/2, transform=view_transformation(from_, to, up))

    image = c.render(w)

    assert(np.allclose(image.pixel_at(5, 5), color(
        0.38066, 0.47583, 0.2855), atol=0.00001))
def test_the_view_transformation_moves_the_world():
    from_ = point(0, 0, 8)
    to = point(0, 0, 0)
    up = vector(0, 1, 0)

    t = view_transformation(from_, to, up)

    actual_transform_matrix = t(identity_matrix())
    expected_transform_matrix = translation(0, 0, -8)(identity_matrix())

    assert (np.allclose(actual_transform_matrix, expected_transform_matrix))
def test_a_transformation_matrix_looking_in_positive_z_direction():
    from_ = point(0, 0, 0)
    to = point(0, 0, 1)
    up = vector(0, 1, 0)

    t = view_transformation(from_, to, up)

    actual_transform_matrix = t(identity_matrix())
    expected_transform_matrix = scaling(-1, 1, -1)(identity_matrix())

    assert (np.allclose(actual_transform_matrix, expected_transform_matrix))
def test_the_view_transformation_matrix_for_the_default_orientation():
    from_ = point(0, 0, 0)
    to = point(0, 0, -1)
    up = vector(0, 1, 0)

    t = view_transformation(from_, to, up)

    actual_transform_matrix = t(identity_matrix())
    expected_transform_matrix = identity_matrix()

    assert (np.allclose(actual_transform_matrix, expected_transform_matrix))
Exemplo n.º 13
0
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))
Exemplo n.º 14
0
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))
Exemplo n.º 15
0
    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_individual_transformations_are_applied_in_sequence():
    p = point(1, 0, 1)
    A = transformations.rotation_x(math.pi / 2)
    B = transformations.scaling(5, 5, 5)
    C = transformations.translation(10, 5, 7)

    p2 = A(p)
    assert (np.allclose(point(1, -1, 0), p2))

    p3 = B(p2)
    assert (np.allclose(point(5, -5, 0), p3))

    p4 = C(p3)
    assert (np.allclose(point(15, 0, 7), p4))
Exemplo n.º 17
0
def test_constructing_a_ray_when_the_camera_is_transformed():
    c = Camera(201, 101, math.pi/2,
               transform=concat(rotation_y(math.pi/4), translation(0, -2, 5)))
    r = c.ray_for_pixel(100, 50)

    assert(np.allclose(point(0, 2, -5), r.origin))
    assert(np.allclose(vector(math.sqrt(2)/2, 0, -math.sqrt(2)/2), r.direction))
Exemplo n.º 18
0
def default_world():
    light = PointLight(point(-10, 10, -10), white)
    m = Material(color=color(0.8, 1.0, 0.6), diffuse=0.7, specular=0.2)
    s1 = Sphere(material=m)
    s2 = Sphere(transformation=scaling(0.5, 0.5, 0.5))

    return World(light, s1, s2)
Exemplo n.º 19
0
 def normal_at(self, p):
     transformation = invert(self.transformation)
     object_point = transformation(p)
     object_normal = object_point - point(0, 0, 0)
     world_normal = transpose(transformation)(object_normal)
     world_normal[3] = 0
     return normalize(world_normal)
Exemplo n.º 20
0
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)
Exemplo n.º 21
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)
Exemplo n.º 22
0
def test_lighting_with_the_light_behind_the_surface(material, position):
    eyev = vector(0, 0, -1)
    normalv = vector(0, 0, -1)
    light = PointLight(point(0, 0, 10), color(1, 1, 1))

    result = material.lighting(light, position, eyev, normalv, False)

    assert (np.array_equal(color(0.1, 0.1, 0.1), result))
Exemplo n.º 23
0
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)
Exemplo n.º 24
0
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)
Exemplo n.º 25
0
def test_lighting_with_the_surface_in_shadow(material, position):
    eyev = vector(0, 0, -1)
    normalv = vector(0, 0, -1)
    light = PointLight(point(0, 0, -10), color(1, 1, 1))
    in_shadow = True

    result = material.lighting(light, position, eyev, normalv, in_shadow)
    assert (np.allclose(color(0.1, 0.1, 0.1), result))
Exemplo n.º 26
0
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)
Exemplo n.º 27
0
def test_lighting_with_the_eye_between_the_light_and_the_surface_and_wyw_offset_45_degrees(
        material, position):
    eyev = vector(0, math.sqrt(2) / 2, -math.sqrt(2) / 2)
    normalv = vector(0, 0, -1)
    light = PointLight(point(0, 0, -10), color(1, 1, 1))

    result = material.lighting(light, position, eyev, normalv, False)

    assert (np.array_equal(color(1.0, 1.0, 1.0), result))
Exemplo n.º 28
0
def test_computing_the_normal_on_a_transformed_sphere():
    s = transformations.scaling(1, 0.5, 1)
    r = transformations.rotation_z(math.pi/5)
    transform = transformations.concat(s, r)

    sphere = Sphere(transform)

    n = sphere.normal_at(point(0, math.sqrt(2)/2, -math.sqrt(2)/2))
    assert(np.allclose(vector(0, 0.97014, -0.242535), n))
Exemplo n.º 29
0
def test_lighting_with_the_eye_opposite_surface_light_offest_45_degrees(
        material, position):
    eyev = vector(0, 0, -1)
    normalv = vector(0, 0, -1)
    light = PointLight(point(0, 10, -10), color(1, 1, 1))

    result = material.lighting(light, position, eyev, normalv, False)

    assert (np.allclose(color(0.7364, 0.7364, 0.7364), result))
Exemplo n.º 30
0
def test_lighting_with_the_eye_in_the_path_of_the_reflection_vector(
        material, position):
    eyev = vector(0, -math.sqrt(2) / 2, -math.sqrt(2) / 2)
    normalv = vector(0, 0, -1)
    light = PointLight(point(0, 10, -10), color(1, 1, 1))

    result = material.lighting(light, position, eyev, normalv, False)

    assert (np.allclose(color(1.6364, 1.6364, 1.6364), result))