Esempio n. 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)
Esempio n. 2
0
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
Esempio n. 3
0
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
Esempio n. 4
0
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)
Esempio n. 5
0
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)
Esempio n. 6
0
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)
Esempio n. 7
0
 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
Esempio n. 8
0
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)
Esempio n. 9
0
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
Esempio n. 10
0
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)
Esempio n. 11
0
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
Esempio n. 12
0
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)
Esempio n. 13
0
# shininess: 200
# reflective: 0.7
# transparency: 0.7
# refractive-index: 1.5
# transform:
# - large-object
s = Sphere()
s.material.color = Color(0.393, 0.404, 0.550)
s.material.diffuse = 0.2
s.material.ambient = 0.0
s.material.specular = 1.0
s.material.shininess = 200
s.material.reflective = 0.7
s.material.transparency = 0.7
s.material.refractive_index = 1.5
s.set_transform(large)
w.objects.append(s)

# - add: cube
# material: white-material
# transform:
# - medium-object
# - [ translate, 4, 0, 0 ]
c1 = Cube()
c1.material = white
c1.set_transform(Translation(4, 0, 0) * medium)
w.objects.append(c1)

# - add: cube
# material: blue-material
# transform:
Esempio n. 14
0
    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