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)
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)
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))
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)))
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_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))
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_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))
def test_computing_the_normal_on_a_translated_sphere(): sphere = Sphere(transformations.translation(0, 1, 0)) n = sphere.normal_at(point(0, 1.70711, -0.70711)) assert(np.allclose(vector(0, 0.70711, -0.70711), n))
def test_multiplying_by_a_translation_matrix(): transform = transformations.translation(5, -3, 2) p = point(-3, 4, 5) assert ((point(2, 1, 7) == transform(p)).all())
def test_translation_does_not_affect_vectors(): transform = transformations.translation(5, -3, 2) v = vector(-3, 4, 5) assert ((v == transform(v)).all())
def test_multiplying_by_the_inverse_of_a_translation_matrix(): transform = transformations.translation(5, -3, 2) transform = transformations.invert(transform) p = point(-3, 4, 5) assert ((point(-8, 7, 3) == transform(p)).all())
from pytracer.spheres import Sphere from pytracer.transformations import scaling, rotation_y, rotation_x, translation, concat, view_transformation from pytracer.materials import Material from pytracer.colors import color from pytracer.camera import Camera from pytracer.tuples import point, vector from pytracer.lights import PointLight from pytracer.world import World import math floor_material = Material(color=color(1, 0.9, 0.9), specular=0) floor = Sphere(transformation=scaling(10, 0.01, 10), material=floor_material) left_wall_transformation = concat(translation(0, 0, 5), rotation_y(-math.pi / 4), rotation_x(math.pi / 2), scaling(10, 0.01, 10)) left_wall = Sphere(transformation=left_wall_transformation, material=floor_material) right_wall_transform = concat(translation(0, 0, 5), rotation_y(math.pi / 4), rotation_x(math.pi / 2), scaling(10, 0.01, 10)) right_wall = Sphere(transformation=right_wall_transform, material=floor_material) middle_transform = translation(-0.5, 1, 0.5) middle_material = Material(color=color(0.1, 1, 0.5), diffuse=0.7, specular=0.3) middle = Sphere(material=middle_material, transformation=middle_transform) right_transform = concat(translation(1.5, 0.5, -0.5), scaling(0.5, 0.5, 0.5)) right_material = Material(color=color(0.5, 1, 0.1), diffuse=0.7, specular=0.3)