Esempio n. 1
0
 def test_ray_when_camera_is_transformed(self):
     transform = RotationY(math.pi / 4) * Translation(0, -2, 5)
     c = Camera(201, 101, math.pi / 2, transform)
     r = c.ray_for_pixel(100, 50)
     self.assertEqual(r.origin, Point(0, 2, -5))
     self.assertEqual(r.direction,
                      Vector(math.sqrt(2) / 2, 0, -math.sqrt(2) / 2))
Esempio n. 2
0
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)
Esempio n. 3
0
def test_render():
    w = World.default()
    c = Camera(11, 11, math.pi / 2)
    f = Point(0, 0, -5)
    to = Point(0, 0, 0)
    up = Vector(0, 1, 0)
    c.transform = ViewTransform(f, to, up)
    image = c.render(w)
    assert image.read_pixel(5, 5) == Color(0.38066, 0.47583, 0.2855)
Esempio n. 4
0
 def test_rendering_world_with_camera(self):
     w = World.default()
     c = Camera(11, 11, math.pi / 2)
     frm = Point(0, 0, -5)
     to = Point(0, 0, 0)
     up = Vector(0, 1, 0)
     c.transform = ViewTransform(frm, to, up)
     image = c.render(w)
     self.assertEqual(image.pixel_at(5, 5), Color(0.38066, 0.47583, 0.2855))
Esempio n. 5
0
 def test_render_world_with_camera(self, default_world):
     w = default_world
     c = Camera(11, 11, pi / 2)
     _from = Point(0, 0, -5)
     to = Point(0, 0, 0)
     up = Vector(0, 2, 0)
     c.transformation = view_transform(_from, to, up)
     image = c.render(w)
     assert image.pixel_at(5, 5) == Color(0.38066, 0.47583, 0.2855)
Esempio n. 6
0
 def test_camera_construction(self):
     hsize = 160
     vsize = 120
     fov = math.pi / 2
     c = Camera(hsize, vsize, fov)
     self.assertEqual(c.hsize, 160)
     self.assertEqual(c.vsize, 120)
     self.assertEqual(c.fov, math.pi / 2)
     self.assertEqual(c.transform, Identity())
Esempio n. 7
0
def test_camera_attributes():
    hsize = 160
    vsize = 120
    fov = math.pi / 2
    c = Camera(hsize, vsize, fov)
    assert c.hsize == 160
    assert c.vsize == 120
    assert c.fov == math.pi / 2
    assert c.transform == Identity()
 def test_construct_camera(self):
     hsize = 160
     vsize = 120
     field_of_view = pi / 2
     c = Camera(hsize, vsize, field_of_view)
     assert c.hsize == 160
     assert c.vsize == 120
     assert c.field_of_view == pi / 2
     assert c.transformation == Matrix.identity()
Esempio n. 9
0
def test_v_pixel_size():
    c = Camera(125, 200, math.pi / 2)
    assert equal(c.pixel_size, 0.01)
Esempio n. 10
0
def test_center_canvas_ray():
    c = Camera(201, 101, math.pi / 2)
    r = c.ray_for_pixel(100, 50)
    assert r.origin == Point(0, 0, 0)
    assert r.direction == Vector(0, 0, -1)
Esempio n. 11
0
def test_h_pixel_size():
    c = Camera(200, 125, math.pi / 2)
    assert equal(c.pixel_size, 0.01)
 def test_pixel_size_for_vertical_canvas(self):
     c = Camera(125, 200, pi / 2)
     assert c.pixel_size == pytest.approx(0.01)
 def test_construct_ray_through_corner_of_canvas(self):
     c = Camera(201, 101, pi / 2)
     r = c.ray_for_pixel(0, 0)
     assert r.origin == Point(0, 0, 0)
     assert r.direction == Vector(0.66519, 0.33259, -0.66851)
Esempio n. 14
0
from raytracer.parser import Parser
from raytracer.world import World
from raytracer.camera import Camera
from raytracer.lights import PointLight
from raytracer.base import Point, Color, ViewTransform, Vector
f = open("tests/obj_files/face.obj", "r")
p = Parser(f)
w = World()
w.objects.append(p.obj_to_group())
w.light = PointLight(Point(10, 5, 5), Color(1, 1, 1))
c = Camera(10, 10, 0.785)
c.transform = ViewTransform(Point(-6, 6, -10), Point(6, 0, 6),
                            Vector(-0.45, 1, 0))
canvas = c.render(w)
with open("images/triangl.ppm", "w") as f:
    f.write(canvas.to_ppm())
# This is wayyy too slow we're gonna need a triangle mesh or something
Esempio n. 15
0
right = Sphere()
right.set_transform(Translation(1.5, 0.5, -0.5))
right.set_transform(Scaling(0.5, 0.5, 0.5))
right.material.color = Color(0.5, 1, 0.1)
right.material.diffuse = 0.7
right.material.specular = 0.3

left = Sphere()
left.set_transform(Translation(-1.5, 0.33, -0.75))
left.set_transform(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.objects.append(middle)
world.objects.append(right)
world.objects.append(left)

world.light = PointLight(Point(-10, 10, -10), Color(1, 1, 1))
camera = Camera(
    200,
    100,
    math.pi / 3,
    ViewTransform(Point(0, 1.5, -5), Point(0, 1, 0), Vector(0, 1, 0)),
)

canvas = camera.render(world)
with open("second_scene.ppm", "w") as ppm_file:
    ppm_file.write(canvas.to_ppm())
Esempio n. 16
0
        t = 0.5 * (unit_direction.values[1] + 1)
        return (1.0 - t) * Vector(1., 1., 1.) + t * Vector(0.5, 0.7, 1.0)


image_file = os.path.join(os.getcwd(), 'image.txt')
with open(image_file, 'w') as f:
    nx = 200
    ny = 100
    ns = 100
    f.write("P3\n{} {}\n255\n".format(nx, ny))
    lower_left_corner = Vector(-2.0, -1.0, -1.0)
    horizontal = Vector(4.0, 0.0, 0.0)
    vertical = Vector(0.0, 2.0, 0.0)
    origin = Vector(0.0, 0.0, 0.0)
    spheres = [Sphere(Vector(0, 0, -1), 0.5), Sphere(Vector(0, -100.5, -1), 100)]
    camera = Camera()
    for j in range(ny - 1, -1, -1):
        for i in range(0, nx):
            u = float(i) / nx
            v = float(j) / ny

            col = Vector(0, 0, 0)
            for s in range(0, ns):
                u = (i + random.random()) / nx
                v = (j + random.random()) / ny
                r = camera.get_ray(u, v)
                p = r.point_at_parameter(2.0)
                col += color(r, HitableList(spheres))
            col.div_by_value(ns)

            ir = (int(255.99 * col.values[0]))
Esempio n. 17
0
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])
world.light = PointLight(Point(-10, 10, -10), Color(1, 1, 1))

camera = Camera(1024, 512, math.pi / 3)
camera.transform = ViewTransform(Point(0, 1.5, -5), Point(0, 1, 0),
                                 Vector(0, 1, 0))

canvas = camera.render(world)
with open("pattern.ppm", "w") as f:
    f.write(canvas.to_ppm())
Esempio n. 18
0
def test_corner_canvas_ray():
    c = Camera(201, 101, math.pi / 2)
    r = c.ray_for_pixel(0, 0)
    assert r.origin == Point(0, 0, 0)
    assert r.direction == Vector(0.66519, 0.33259, -0.66851)
from raytracer.lights import PointLight
from raytracer.patterns import *
from raytracer.matrices import scaling, rotation_x, rotation_y, rotation_z, translation, view_transform
from raytracer.scene import World
from raytracer.shapes import Cylinder, Plane
from raytracer.tuples import Color, Point, Vector

if __name__ == '__main__':
    floor = Plane()
    floor.material.color = Color(.8, .8, .8)
    floor.material.reflective = .2

    cylinder = Cylinder(closed=True)
    cylinder.maximum = 0.25
    cylinder.material.color = Color(.8, 0, 0)
    cylinder.material.reflective = .6
    cylinder.transformation = translation(0, 1, 0)

    world = World()
    world.add(floor, cylinder)
    world.light_source = PointLight(Point(-10, 10, -10), Color.white())

    camera = Camera(160, 120, pi / 3)
    camera.transformation = view_transform(Point(0, 1.5, -5), Point(
        0, 1, 0), Vector(0, 1, 0)) * rotation_y(-pi / 4) * rotation_x(-pi / 6)

    canvas = camera.render(world)

    write_ppm_to_file(canvas.to_ppm(),
                      f'..{sep}..{sep}resources{sep}cylinders.ppm')
Esempio n. 20
0
def build_camera(_data: dict) -> Camera:
    _camera = Camera(_data['width'], _data['height'], _data['field-of-view'])
    _camera.transformation = view_transform(point_from_tuple(_data['from']),
                                            point_from_tuple(_data['to']),
                                            vector_from_tuple(_data['up']))
    return _camera
 def test_pixel_size_for_horizontal_canvas(self):
     c = Camera(200, 125, pi / 2)
     assert c.pixel_size == pytest.approx(0.01)
Esempio n. 22
0
 def test_horizontal_pixel_size(self):
     c = Camera(200, 125, math.pi / 2)
     self.assertTrue(equal(c.pixel_size, 0.01))
 def test_construct_ray_through_center_of_canvas(self):
     c = Camera(201, 101, pi / 2)
     r = c.ray_for_pixel(100, 50)
     assert r.origin == Point(0, 0, 0)
     assert r.direction == Vector(0, 0, -1)
Esempio n. 24
0
 def test_vertical_pixel_size(self):
     c = Camera(125, 200, math.pi / 2)
     self.assertTrue(equal(c.pixel_size, 0.01))
 def test_construct_ray_when_camera_is_transformed(self):
     c = Camera(201, 101, pi / 2)
     c.transformation = rotation_y(pi / 4) * translation(0, -2, 5)
     r = c.ray_for_pixel(100, 50)
     assert r.origin == Point(0, 2, -5)
     assert r.direction == Vector(sqrt(2) / 2, 0, -sqrt(2) / 2)
Esempio n. 26
0
 def test_ray_through_center_of_canvas(self):
     c = Camera(201, 101, math.pi / 2)
     r = c.ray_for_pixel(100, 50)
     self.assertEqual(r.origin, Point(0, 0, 0))
     self.assertEqual(r.direction, Vector(0, 0, -1))
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.matrices import *
from raytracer.obj_file import parse_obj_file
from raytracer.scene import World
from raytracer.tuples import Color, Point, Vector


def teapot():
    parser = parse_obj_file(f'..{sep}resources{sep}Sting-Sword-lowpoly.obj')
    return parser.obj_to_group()


if __name__ == '__main__':
    world = World()
    world.add(teapot())
    world.light_source = PointLight(Point(-5, 5, -5), Color.white())

    camera = Camera(150, 100, pi / 3)
    camera.transformation = view_transform(Point(0, 1.5, -10), Point(0, 1, 0),
                                           Vector(0, 1, 0))

    canvas = camera.render(world)

    write_ppm_to_file(canvas.to_ppm(),
                      f'..{sep}..{sep}resources{sep}sting.ppm')
Esempio n. 28
0
 def test_ray_through_corner_of_canvas(self):
     c = Camera(201, 101, math.pi / 2)
     r = c.ray_for_pixel(0, 0)
     self.assertEqual(r.origin, Point(0, 0, 0))
     self.assertEqual(r.direction, Vector(0.66519, 0.33259, -0.66851))