def test_positive_z_orientation_matrix(): f = Point(0, 0, 0) to = Point(0, 0, 1) up = Vector(0, 1, 0) t = ViewTransform(f, to, up) print(t) assert t == Scaling(-1, 1, -1)
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_scaled_shape_intersection(): r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) s = _TestShape() s.set_transform(Scaling(2, 2, 2)) _ = s.intersect(r) assert s.saved_ray.origin == Point(0, 0, -2.5) assert s.saved_ray.direction == Vector(0, 0, 0.5)
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_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_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 default(cls): world = cls() world.light = PointLight(Point(-10, 10, -10), Color(1, 1, 1)) m = Material() m.color = Color(0.8, 1.0, 0.6) m.diffuse = 0.7 m.specular = 0.2 s1 = Sphere() s1.set_material(m) s2 = Sphere() s2.set_transform(Scaling(0.5, 0.5, 0.5)) world.objects.extend([s1, s2]) return world
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 test_default_world(): light = PointLight(Point(-10, 10, -10), Color(1, 1, 1)) s1 = Sphere() m = Material() m.color = Color(0.8, 1.0, 0.6) m.diffuse = 0.7 m.specular = 0.2 s1.set_material(m) s2 = Sphere() t = Scaling(0.5, 0.5, 0.5) s2.set_transform(t) w = World.default() assert w.light == light assert s1 in w.objects assert s2 in w.objects
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_reflection(): t = Scaling(-1, 1, 1) p = Point(2, 3, 4) assert t * p == Point(-2, 3, 4)
def test_transformed_shape_normal(): s = _TestShape() t = Scaling(1, 0.5, 1) * RotationZ(math.pi / 5) s.set_transform(t) n = s.normal_at(Point(0, math.sqrt(2) / 2, -math.sqrt(2) / 2)) assert n == Vector(0, 0.97014, -0.24254)
def test_point_scaling(): t = Scaling(2, 3, 4) p = Point(-4, 6, 8) assert t * p == Point(-8, 18, 32)
def test_pattern_transform(): s = Sphere() p = _TestPattern() p.set_pattern_transform(Scaling(2, 2, 2)) c = p.pattern_at_shape(s, Point(2, 3, 4)) assert c == Color(1, 1.5, 2)
def test_object_transform(): s = Sphere() s.set_transform(Scaling(2, 2, 2)) pattern = _TestPattern() c = pattern.pattern_at_shape(s, Point(2, 3, 4)) assert c == Color(1, 1.5, 2)
def test_vector_scaling(): t = Scaling(2, 3, 4) v = Vector(-4, 6, 8) assert t * v == Vector(-8, 18, 32)
def test_ray_scaling(): 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)
# 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:
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 left = Sphere() left.transform = Translation(-1.5, 0.33, -0.75) * Scaling(0.33, 0.33, 0.33) left.material.color = Color(1, 0.8, 0.1) left.material.diffuse = 0.7 left.material.specular = 0.3 world = World() world.objects.extend([floor, middle, right, left])
def test_inverse_scaling(): t = Scaling(2, 3, 4) i = t.inverse() v = Vector(-4, 6, 8) assert i * v == Vector(-2, 2, 2)