def test_mutually_reflective_color_at(): w = World() w.light = PointLight(Point(0, 0, 0), Color(1, 1, 1)) lower = Plane() lower.material.reflective = 1 lower.set_transform(Translation(0, -1, 0)) w.objects.append(lower) upper = Plane() upper.material.reflective = 1 upper.set_transform(Translation(0, 1, 0)) w.objects.append(upper) r = Ray(Point(0, 0, 0), Vector(0, 1, 0)) assert w.color_at(r) is not None
def test_camera_transform_ray(): c = Camera(201, 101, math.pi / 2) c.transform = RotationY(math.pi / 4) * Translation(0, -2, 5) r = c.ray_for_pixel(100, 50) assert r.origin == Point(0, 2, -5) print(r.direction) assert r.direction == Vector(math.sqrt(2) / 2, 0, -math.sqrt(2) / 2)
def test_ray_translation(): r = Ray(Point(1, 2, 3), Vector(0, 1, 0)) m = Translation(3, 4, 5) r2 = r.transform(m) print(r2.origin) assert r2.origin == Point(4, 6, 8) assert r2.direction == Vector(0, 1, 0)
def test_both_transform(): s = Sphere() s.set_transform(Scaling(2, 2, 2)) pattern = _TestPattern() pattern.set_pattern_transform(Translation(0.5, 1, 1.5)) c = pattern.pattern_at_shape(s, Point(2.5, 3, 3.5)) assert c == Color(0.75, 0.5, 0.25)
def test_translated_shape_intersection(): r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) s = _TestShape() s.set_transform(Translation(5, 0, 0)) _ = s.intersect(r) assert s.saved_ray.origin == Point(-5, 0, -5) assert s.saved_ray.direction == Vector(0, 0, 1)
def test_nonempty_group(): g = Group() s1 = Sphere() s2 = Sphere() s2.set_transform(Translation(0, 0, -3)) s3 = Sphere() s3.set_transform(Translation(5, 0, 0)) g.add_child(s1) g.add_child(s2) g.add_child(s3) r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) xs = g.local_intersect(r) assert len(xs) == 4 assert xs[0].object == s2 assert xs[1].object == s2 assert xs[2].object == s1 assert xs[3].object == s1
def test_trans_chaining(): p = Point(1, 0, 1) a = RotationX(math.pi / 2) b = Scaling(5, 5, 5) c = Translation(10, 5, 7) t = c * b * a print(t * p) assert t * p == Point(15, 0, 7)
def test_hit_offset(): r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) shape = Sphere() shape.set_transform(Translation(0, 0, 1)) i = Intersection(5, shape) comps = i.prepare_computation(r) assert comps.over_point.z < -EPSILON / 2 assert comps.point.z > comps.over_point.z
def shade_hit_transparent(): w = World().default() floor = Plane() floor.set_transform(Translation(0, -1, 0)) floor.material.transparency = 0.5 floor.material.refractive_index = 1.5 w.objects.append(floor) ball = Sphere() ball.material.color = Color(1, 0, 0) ball.material.ambient = 0.5 ball.set_transform(Translation(0, -3.5, -0.5)) w.objects.append(ball) r = Ray(Point(0, 0, -3), Vector(0, -math.sqrt(2) / 2, math.sqrt(2) / 2)) xs = Intersections(Intersection(math.sqrt(2), floor)) comps = xs[0].prepare_computations(r, xs) color = w.shade_hit(comps, 5) assert color == Color(0.93642, 0.68642, 0.68642)
def test_transformed_group(): g = Group() g.set_transform(Scaling(2, 2, 2)) s = Sphere() s.set_transform(Translation(5, 0, 0)) g.add_child(s) r = Ray(Point(10, 0, -10), Vector(0, 0, 1)) xs = g.intersect(r) assert len(xs) == 2
def test_under_point(): r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) s = GlassSphere() s.set_transform(Translation(0, 0, 1)) i = Intersection(5, s) xs = Intersections(i) comps = i.prepare_computation(r, xs) assert comps.under_point.z > EPSILON / 2 assert comps.point.z < comps.under_point.z
def test_trans_sequence(): p = Point(1, 0, 1) a = RotationX(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_world_to_object(): g1 = Group() g1.set_transform(RotationY(math.pi / 2)) g2 = Group() g2.set_transform(Scaling(2, 2, 2)) g1.add_child(g2) s = Sphere() s.set_transform(Translation(5, 0, 0)) g2.add_child(s) p = s.world_to_object(Point(-2, 0, -10)) assert p == Point(0, 0, -1)
def test_child_normal(): g1 = Group() g1.set_transform(RotationY(math.pi / 2)) g2 = Group() g2.set_transform(Scaling(1, 2, 3)) g1.add_child(g2) s = Sphere() s.set_transform(Translation(5, 0, 0)) g2.add_child(s) n = s.normal_at(Point(1.7321, 1.1547, -5.5774)) assert n == Vector(0.2857, 0.4286, -0.8571)
def test_maximum_recursion_depth_color_at(): w = World().default() s = Plane() s.material.reflective = 0.5 s.set_transform(Translation(0, -1, 0)) w.objects.append(s) r = Ray(Point(0, 0, -3), Vector(0, -math.sqrt(2) / 2, math.sqrt(2) / 2)) i = Intersection(math.sqrt(2), s) comps = i.prepare_computation(r) color = w.reflected_color(comps, 0) assert color == Color(0, 0, 0)
def test_shade_hit_reflective(): w = World().default() s = Plane() s.material.reflective = 0.5 s.set_transform(Translation(0, -1, 0)) w.objects.append(s) r = Ray(Point(0, 0, -3), Vector(0, -math.sqrt(2) / 2, math.sqrt(2) / 2)) i = Intersection(math.sqrt(2), s) comps = i.prepare_computation(r) c = w.shade_hit(comps) assert c == Color(0.87677, 0.92436, 0.82918)
def test_reflected_color(): w = World().default() s = Plane() s.material.reflective = 0.5 s.set_transform(Translation(0, -1, 0)) w.objects.append(s) r = Ray(Point(0, 0, -3), Vector(0, -math.sqrt(2) / 2, math.sqrt(2) / 2)) i = Intersection(math.sqrt(2), s) comps = i.prepare_computation(r) c = w.reflected_color(comps) print(c) assert c == Color(0.19032, 0.2379, 0.14274)
def test_shadow_shade_hit(): w = World() w.light = PointLight(Point(0, 0, -10), Color(1, 1, 1)) s1 = Sphere() s2 = Sphere() s2.set_transform(Translation(0, 0, 10)) w.objects.extend([s1, s2]) r = Ray(Point(0, 0, 5), Vector(0, 0, 1)) i = Intersection(4, s2) comps = i.prepare_computation(r) c = w.shade_hit(comps) print(c) assert c == Color(0.1, 0.1, 0.1)
def test_normal_object_to_world(): g1 = Group() g1.set_transform(RotationY(math.pi / 2)) g2 = Group() g2.set_transform(Scaling(1, 2, 3)) g1.add_child(g2) s = Sphere() s.set_transform(Translation(5, 0, 0)) g2.add_child(s) n = s.normal_to_world( Vector(math.sqrt(3) / 3, math.sqrt(3) / 3, math.sqrt(3) / 3)) assert n == Vector(0.2857, 0.4286, -0.8571)
def check_n1_n2(index): a = GlassSphere() a.set_transform(Scaling(2, 2, 2)) a.material.refractive_index = 1.5 b = GlassSphere() b.set_transform(Translation(0, 0, -0.25)) b.material.refractive_index = 2.0 c = GlassSphere() c.set_transform(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_computation(r, xs) return (comps.n1, comps.n2)
def test_translated_shape_normal(): s = _TestShape() s.set_transform(Translation(0, 1, 0)) n = s.normal_at(Point(0, 1.70711, -0.70711)) assert n == Vector(0, 0.70711, -0.70711)
def test_translation(): t = Translation(5, -3, 2) p = Point(-3, 4, 5) p_exp = Point(2, 1, 7) assert t * p == p_exp
def test_movement(): f = Point(0, 0, 8) to = Point(0, 0, 0) up = Vector(0, 1, 0) t = ViewTransform(f, to, up) assert t == Translation(0, 0, -8)
def test_assign_transform(): pattern = _TestPattern() pattern.set_pattern_transform(Translation(1, 2, 3)) assert pattern.transform == Translation(1, 2, 3)
def test_inverse_translation(): t = Translation(5, -3, 2) i = t.inverse() p = Point(-3, 4, 5) assert i * p == Point(-8, 7, 3)
def test_vector_translation(): t = Translation(5, -3, 2) v = Vector(-3, 4, 5) assert t * v == v
# color: [ 0.941, 0.322, 0.388 ] red = copy.deepcopy(white) red.color = Color(0.941, 0.322, 0.388) # - define: purple-material # extend: white-material # value: # color: [ 0.373, 0.404, 0.550 ] purple = copy.deepcopy(white) purple.color = Color(0.373, 0.322, 0.388) # - define: standard-transform # value: # - [ translate, 1, -1, 1 ] # - [ scale, 0.5, 0.5, 0.5 ] standard = Scaling(0.5, 0.5, 0.5) * Translation(1, -1, 1) # - define: large-object # value: # - standard-transform # - [ scale, 3.5, 3.5, 3.5 ] large = Scaling(3.5, 3.5, 3.5) * standard # - define: medium-object # value: # - standard-transform # - [ scale, 3, 3, 3 ] medium = Scaling(3, 3, 3) * standard # - define: small-object # value:
Vector, ) from raytracer.patterns import CheckersPattern, StripePattern from raytracer.lights import PointLight from raytracer.camera import Camera from raytracer.world import World import math floor = Plane() floor.material.color = Color(1.0, 0.9, 0.9) floor.material.pattern = CheckersPattern(Color(1, 1, 1), Color(0, 0, 0)) floor.material.reflective = 0.5 floor.material.specular = 0 middle = Sphere() middle.set_transform(Translation(-0.5, 1, 0.5)) middle.material.transparency = 0.9 middle.material.diffuse = 0.1 middle.material.reflective = 0.9 middle.material.refractive_index = 1.5 middle.material.specular = 1 middle.material.shininess = 300 right = Sphere() right.set_transform(Translation(1.5, 0.5, -0.5) * Scaling(0.5, 1, 0.5)) right.material.pattern = StripePattern(Color(1, 1, 1), Color(0, 1, 0)) right.material.pattern.set_pattern_transform( Scaling(0.1, 0.1, 0.1) * RotationY(math.pi / 4)) right.material.color = Color(0.1, 1, 0.5) right.material.diffuse = 0.7 right.material.specular = 0.3
def test_assign_transform(): s = _TestShape() s.set_transform(Translation(2, 3, 4)) assert s.transform == Translation(2, 3, 4)