def test_chained_transformations_are_in_reverse_order(self): p = Point(1, 0, 1) a = rotation_x(math.pi / 2) b = scaling(5, 5, 5) c = translation(10, 5, 7) t = c * b * a * p assert t == Point(15, 0, 7)
def test_pattern_with_both_object_and_pattern_transformation(self): obj = Sphere() obj.transformation = scaling(2, 2, 2) pattern = test_pattern() pattern.transformation = translation(0.5, 1, 1.5) c = pattern.pattern_at_shape(obj, Point(2.5, 3, 3.5)) assert c == Color(0.75, 0.5, 0.25)
def default_world(): w = World() w.light_source = PointLight(Point(-10, 10, -10), Color.white()) s1 = Sphere() s1.material.color = Color(0.8, 1.0, 0.6) s1.material.diffuse = 0.7 s1.material.specular = 0.2 s2 = Sphere() s2.transformation = scaling(0.5, 0.5, 0.5) w.add(s1, s2) return w
def test_individual_transformations_in_sequence(self): p = Point(1, 0, 1) a = rotation_x(math.pi / 2) b = scaling(5, 5, 5) c = translation(10, 5, 7) p2 = a * p assert p2 == Point(1, -1, 0) p3 = b * p2 assert p3 == Point(5, -5, 0) p4 = c * p3 assert p4 == Point(15, 0, 7)
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
from raytracer.scene import World from raytracer.shapes import Sphere, Plane from raytracer.tuples import Color, Point, Vector if __name__ == '__main__': floor = Plane() floor.material = Material() floor.material.color = Color(1, 0.9, 0.9) floor.material.specular = 0 middle = Sphere() middle.transformation = translation(-0.5, 1, 0.5) middle.material = Material() middle.material.pattern = CheckersPattern(Color(0.1, 1, 0.5), Color(1, 0, 0)) middle.material.pattern.transformation = scaling(0.25, 0.25, 0.5) * rotation_y(pi / 4) middle.material.diffuse = 0.7 middle.material.specular = 0.3 right = Sphere() right.transformation = translation(1.5, 0.5, -0.5) * scaling(0.5, 0.5, 0.5) right.material = Material() right.material.color = Color(0.5, 1, 0.1) right.material.diffuse = 0.7 right.material.specular = 0.3 left = Sphere() left.transformation = translation(-1.5, 0.33, -0.75) * scaling( 0.33, 0.33, 0.33) left.material = Material() left.material.color = Color(1, 0.8, 0.1)
def test_scale_ray(self): r = Ray(Point(1, 2, 3), Vector(0, 1, 0)) m = scaling(2, 3, 4) r2 = r.transform(m) assert r2.origin == Point(2, 6, 12) assert r2.direction == Vector(0, 3, 0)
def test_pattern_with_pattern_transformation(self): obj = Sphere() pattern = test_pattern() pattern.transformation = scaling(2, 2, 2) c = pattern.pattern_at_shape(obj, Point(2, 3, 4)) assert c == Color(1, 1.5, 2)
from raytracer.matrices import scaling, rotation_z, shearing from raytracer.rays import Ray from raytracer.shapes import Sphere from raytracer.tuples import Point, Color if __name__ == '__main__': ray_origin = Point(0, 0, -5) wall_z = 10 wall_size = 7.0 canvas_pixels = 100 pixel_size = wall_size / canvas_pixels half = wall_size / 2 canvas = Canvas(canvas_pixels, canvas_pixels) red = Color(1, 0, 0) sphere = Sphere() sphere.transformation = scaling(1, .5, 1).shear(1, 0, 0, 0, 0, 0) for y in range(canvas_pixels): world_y = half - pixel_size * y for x in range(canvas_pixels): world_x = -half + pixel_size * x position = Point(world_x, world_y, wall_z) ray = Ray(ray_origin, (position - ray_origin).normalize()) if sphere.intersect(ray).hit(): canvas.write_pixel(x, y, red) write_ppm_to_file(canvas.to_ppm(), f'..{sep}..{sep}resources{sep}circle.ppm')
from math import pi from os import sep from raytracer.camera import Camera from raytracer.canvas import write_ppm_to_file from raytracer.lights import PointLight from raytracer.materials import Material from raytracer.matrices import scaling, rotation_y, rotation_x, translation, view_transform from raytracer.scene import World from raytracer.shapes import Sphere from raytracer.tuples import Color, Point, Vector if __name__ == '__main__': floor = Sphere() floor.transformation = scaling(10, 0.01, 10) floor.material = Material() floor.material.color = Color(1, 0.9, 0.9) floor.material.specular = 0 left_wall = Sphere() left_wall.transformation = translation(0, 0, 5) * \ rotation_y(-pi / 4) * rotation_x(pi / 2) * \ scaling(10, 0.01, 10) left_wall.material = floor.material right_wall = Sphere() right_wall.transformation = translation(0, 0, 5) * \ rotation_y(pi / 4) * rotation_x(pi / 2) * \ scaling(10, 0.01, 10) right_wall.material = floor.material middle = Sphere()
def test_multiply_by_inverse_of_scaling_matrix(self): transform = scaling(2, 3, 4) inv = transform.inverse() v = Vector(-4, 6, 8) assert inv * v == Vector(-2, 2, 2)
def test_scaling_matrix_applied_to_vector(self): transform = scaling(2, 3, 4) v = Vector(-4, 6, 8) assert transform * v == Vector(-8, 18, 32)
def test_scaling_matrix_applied_to_point(self): transform = scaling(2, 3, 4) p = Point(-4, 6, 8) assert transform * p == Point(-8, 18, 32)
def test_view_transformation_looking_in_z_positive_direction(self): _from = Point(0, 0, 0) to = Point(0, 0, 1) up = Vector(0, 1, 0) t = view_transform(_from, to, up) assert t == scaling(-1, 1, -1) # reflecting across z and x
if __name__ == '__main__': floor = Plane() floor.material = Material() floor.material.color = Color(1, 0.9, 0.9) floor.material.specular = 0 middle = Sphere() middle.transformation = translation(-0.5, 1, 0.5) middle.material = Material() middle.material.color = Color(0.5, 0, 0) middle.material.diffuse = 0.7 middle.material.specular = 0.3 middle.material.reflective = 1 right = Sphere() right.transformation = translation(1.5, 0.5, -0.5) * scaling(0.5, 0.5, 0.5) right.material = Material() right.material.color = Color(0.2, 0.2, 0.8) right.material.diffuse = 0.7 right.material.specular = 0.3 right.material.reflective = .5 left = Sphere() left.transformation = translation(-1.5, 0.33, -0.75) * scaling(0.33, 0.33, 0.33) left.material = Material() left.material.color = Color(1, 0.8, 0.1) left.material.diffuse = 0.7 left.material.specular = 0.3 world = World() world.add(floor, middle, left, right)
from raytracer.patterns import * from raytracer.matrices import scaling, rotation_y, rotation_x, translation, view_transform from raytracer.scene import World from raytracer.shapes import Sphere, Plane from raytracer.tuples import Color, Point, Vector if __name__ == '__main__': floor = Plane() floor.material.color = Color(.1, 1, .1) floor.material.reflective = 0.9 floor.material.transparency = 1 floor.material.refractive_index = 1.333 under = Sphere() under.transformation = translation(0, -.5, 0) * scaling(.25, .25, .25) under.material.color = Color(0, .8, 0) under.material.refractive_index = 1.5 under.material.ambient = .8 above = Sphere() above.transformation = translation(1, .5, 0) * scaling(.5, .5, .5) above.material.color = Color(.2, 0, 0) above.material.diffuse = .8 above.material.ambient = .5 bottom = Plane() bottom.transformation = translation(0, -1.25, 0) bottom.material.pattern = StripePattern(Color.black(), Color(0, .2, .2)) bottom.material.diffuse = .25 bottom.material.ambient = .25
def test_reflection_is_scaling_by_negative_value(self): transform = scaling(-1, 1, 1) p = Point(2, 3, 4) assert transform * p == Point(-2, 3, 4)