示例#1
0
def test_lightning_eye_angle():
    m = DefaultMaterial()
    position = Point(0, 0, 0)
    eyev = Vector(0, sqrt(2) / 2, -sqrt(2) / 2)
    normalv = Vector(0, 0, -1)
    light = PointLight(Point(0, 0, -10), Color(1, 1, 1))

    result = m.lighting(light, position, eyev, normalv)
    assert result == Color(1.0, 1.0, 1.0)
示例#2
0
def test_lightning_light_behind():
    m = DefaultMaterial()
    position = Point(0, 0, 0)
    eyev = Vector(0, 0, -1)
    normalv = Vector(0, 0, -1)
    light = PointLight(Point(0, 0, 10), Color(1, 1, 1))

    result = m.lighting(light, position, eyev, normalv)
    assert result == Color(0.1, 0.1, 0.1)
示例#3
0
def test_lighting_direct():
    m = DefaultMaterial()
    position = Point(0, 0, 0)
    eyev = Vector(0, 0, -1)
    normalv = Vector(0, 0, -1)
    light = PointLight(Point(0, 0, -10), Color(1, 1, 1))

    result = m.lighting(light, position, eyev, normalv)
    assert result == Color(1.9, 1.9, 1.9)
示例#4
0
def test_lightning_light_angle():
    m = DefaultMaterial()
    position = Point(0, 0, 0)
    eyev = Vector(0, 0, -1)
    normalv = Vector(0, 0, -1)
    light = PointLight(Point(0, 10, -10), Color(1, 1, 1))

    result = m.lighting(light, position, eyev, normalv)
    assert result == Color(0.7363961030678927, 0.7363961030678927,
                           0.7363961030678927)
示例#5
0
def test_tupl_vec_cross():
    t1 = Vector(1, 2, 3)
    t2 = Vector(2, 3, 4)

    c1 = Vector(-1, 2, -1)
    c2 = Vector(1, -2, 1)

    cross1 = t1.cross(t2)
    cross2 = t2.cross(t1)
    assert cross1 == c1 and cross2 == c2
示例#6
0
def test_ray_position():
    r = Ray(Point(2, 3, 4), Vector(1, 0, 0))

    assert r.position(0) == Point(2, 3, 4)
    assert r.position(1) == Point(3, 3, 4)
    assert r.position(-1) == Point(1, 3, 4)
    assert r.position(2.5) == Point(4.5, 3, 4)
示例#7
0
def test_ray_init():
    origin = Point(1, 2, 3)
    direct = Vector(4, 5, 6)

    r = Ray(origin, direct)
    assert r.origin == origin
    assert r.direction == direct
示例#8
0
def test_ray_intersect_from_middle():
    r = Ray(Point(0, 0, 0), Vector(0, 0, 1))
    s = Sphere()
    x = r.intersects(s)

    assert len(x) == 2
    assert x[0].t == -1
    assert x[1].t == 1
示例#9
0
def test_ray_intersect_from_behind():
    r = Ray(Point(0, 0, 5), Vector(0, 0, 1))
    s = Sphere()
    x = r.intersects(s)

    assert len(x) == 2
    assert x[0].t == -6
    assert x[1].t == -4
示例#10
0
def test_ray_intersects_sphere_object():
    r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
    s = Sphere()
    x = r.intersects(s)

    assert len(x) == 2
    assert x[0].object == s
    assert x[1].object == s
示例#11
0
def test_sphere_intersection_scaled():
    r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
    s = Sphere()
    s.set_transform(scaling(2, 2, 2))
    x = r.intersects(s)
    assert len(x) == 2
    assert x[0].t == 3
    assert x[1].t == 7
示例#12
0
def test_ray_intersect_sphere_tangent():
    r = Ray(Point(0, 1, -5), Vector(0, 0, 1))
    s = Sphere()
    x = r.intersects(s)

    assert len(x) == 2
    assert x[0].t == 5
    assert x[1].t == 5
示例#13
0
def test_sphere_normal_at_arbitrary():
    s = Sphere()
    n = s.normal_at(Point(sqrt(3) / 3, sqrt(3) / 3, sqrt(3) / 3))

    assert n == Vector(sqrt(3) / 3, sqrt(3) / 3, sqrt(3) / 3)
示例#14
0
def test_sphere_normal_at_z():
    s = Sphere()
    n = s.normal_at(Point(0, 0, 1))
    assert n == Vector(0, 0, 1)
示例#15
0
def test_sphere_normal_at_x():
    s = Sphere()
    n = s.normal_at(Point(1, 0, 0))

    assert n == Vector(1, 0, 0)
示例#16
0
def test_sphere_intersection_translate():
    r = Ray(Point(0, 0, -5), Vector(0, 0, 1))
    s = Sphere()
    s.set_transform(translation(5, 0, 0))
    x = r.intersects(s)
    assert len(x) == 0
示例#17
0
def test_scale():
    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)
示例#18
0
def test_translation_vector_invariance():
    t = translation(5, -3, 2)
    v = Vector(-3, 4, 5)

    assert t * v == v
示例#19
0
from src.canvas import Canvas
from src.color import Color
from src.tupl import Vector, Point


def tick(p, v):
    p += v
    v += Vector(0, -0.1, 0) + Vector(-0.01, 0, 0)
    return p, v


c = Canvas(900, 550)
col = Color(255, 255, 255)

if __name__ == '__main__':
    p = Point(0, 1, 0)
    v = Vector(1, 1.8, 0).normalize() * 11.25

    for i in range(100):
        X = int(p.x)
        Y = int(p.y)

        if p.y <= 0:
            break
        if (0 <= X < c.width) and (0 <= c.height - Y < c.height):
            c.write_pixel(X, c.height - Y, col)
        p, v = tick(p, v)

    c.to_ppm('trajectory.ppm')
示例#20
0
def test_sphere_normal_at_translate():
    s = Sphere()
    s.set_transform(translation(0, 1, 0))
    n = s.normal_at(Point(0, 1.70711, -0.70711))
    assert n == Vector(0, 0.7071067811865475, -0.7071067811865476)
示例#21
0
def test_tupl_sub():
    t1 = Point(3, 2, 5)
    t2 = Point(3, 2, 1)
    t3 = Vector(0, 0, 4)

    assert t1 - t2 == t3 and (t1 - t2).is_vector()
示例#22
0
def test_tupl_vec_magnitude():
    t1 = Vector(1, 1, 1)
    eps = 1.0e-14

    assert abs(sqrt(3) - t1.magnitude) < eps
示例#23
0
def test_ray_intersect_sphere_no_intersection():
    r = Ray(Point(0, 2, -5), Vector(0, 0, 1))
    s = Sphere()
    x = r.intersects(s)

    assert len(x) == 0
示例#24
0
def test_tupl_vec_normalize():
    t1 = Vector(3, 2, 5)
    eps = 1.0e-14

    n1 = t1.normalize()
    assert abs(n1.magnitude - 1) < eps
示例#25
0
def test_scaling_inverse():
    t = scaling(2, 3, 4)
    inv = t.inv
    v = Vector(-4, 6, 8)

    assert inv * v == Vector(-2, 2, 2)
示例#26
0
def test_scaling_vector():
    t = scaling(2, 3, 4)
    p = Vector(-4, 6, 8)

    assert t * p == Vector(-8, 18, 32)
示例#27
0
def test_sphere_normal_at_scaled():
    s = Sphere()
    s.set_transform(scaling(1, 0.5, 1))
    n = s.normal_at(Point(0, sqrt(2) / 2, -sqrt(2) / 2))
    assert n == Vector(0, 0.9701425001453319, -0.24253562503633297)
示例#28
0
def test_translate():
    r = Ray(Point(1, 2, 3), Vector(0, 1, 0))
    m = translation(3, 4, 5)
    r2 = r.transform(m)
    assert r2.origin == Point(4, 6, 8)
    assert r2.direction == Vector(0, 1, 0)
示例#29
0
def tick(p, v):
    p += v
    v += Vector(0, -0.1, 0) + Vector(-0.01, 0, 0)
    return p, v
示例#30
0
def test_tupl_vec_dot():
    t1 = Vector(1, 0, 0)
    t2 = Vector(0, 1, 1)
    t3 = Vector(0, 3, 4)

    assert t1.dot(t2) == 0 and t2.dot(t1) == 0 and t2.dot(t3) == 7