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)
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)
def test_multiplying_the_inverse_of_a_scaling_matrix(): transform = transformations.scaling(2, 3, 4) inverse = transformations.invert(transform) v = vector(-4, 6, 8) expected = vector(-2, 2, 2) actual = inverse(v) assert ((expected == actual).all())
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))
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_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))
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_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_reflection_is_scaling_by_a_negative_value(): transform = transformations.scaling(-1, 1, 1) p = point(2, 3, 4) assert ((point(-2, 3, 4) == (transform(p))).all())
def test_a_scaling_matrix_applied_to_a_vecor(): transform = transformations.scaling(2, 3, 4) v = vector(-4, 6, 8) assert ((vector(-8, 18, 32) == (transform(v))).all())
def test_a_scaling_matrix_applied_to_a_point(): transform = transformations.scaling(2, 3, 4) p = point(-4, 6, 8) assert ((point(-8, 18, 32) == (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)