예제 #1
0
def render():
    floor = scene.Body(
        shape=geometry.make_plane(0, 1, 0, -200),
        material=raytracer.material.Monochrome(Color(170, 170, 170)),
    )

    left_wall = scene.Body(
        shape=geometry.make_plane(1, 0, 0, 250),
        material=raytracer.material.Checkered(
            square_width=80,
            lighter=Palette.WHITE,
            darker=Palette.BLACK,
            projection=raytracer.material.Checkered.project_to_local_yz,
        ),
    )

    right_wall = scene.Body(
        shape=geometry.make_plane(1, 0, 0, -350),
        material=raytracer.material.Mirror(),
    )

    far_wall = scene.Body(
        shape=geometry.make_plane(0, 0, 1, -2000),
        material=raytracer.material.Checkered(
            square_width=80,
            lighter=Palette.WHITE,
            darker=Color(15, 171, 18),
            projection=raytracer.material.Checkered.project_to_local_xy,
        ),
    )

    left_sphere = scene.Body(
        geometry.Sphere(center=geometry.make_point(75, 185, 400), radius=15),
        material=raytracer.material.Monochrome(Palette.GRAY),
    )
    right_sphere = scene.Body(
        geometry.Sphere(center=geometry.make_point(200, 150, 500), radius=50),
        material=raytracer.material.Mirror(),
    )
    left_light = geometry.make_point(0, 0, 400)
    right_light = geometry.make_point(450, 1000, 200)
    camera = geometry.make_point(150, 100, -300)
    bodies = [
        left_wall, right_wall, far_wall, floor, left_sphere, right_sphere
    ]
    s = scene.Scene(
        bodies=bodies,
        camera=camera,
        lights=[left_light, right_light],
        width=300,
        height=200,
    )
    image = s.render()
    return image
예제 #2
0
def test_solid_material():
    material = Monochrome(color=Palette.BLACK)
    assert material.get_color(geometry.make_point(0, 0, 0)) == Palette.BLACK
예제 #3
0
def test_checkered_project_to_local_xz():
    point = geometry.make_point(3, 4, 5)
    expected = geometry.make_point(3, 5, 0)
    assert np.array_equal(Checkered.project_to_local_xz(point), expected)
예제 #4
0
def test_checkered_project_to_local_xy():
    point = geometry.make_point(3, 4, 5)
    expected = geometry.make_point(3, 4, 0)
    assert np.array_equal(Checkered.project_to_local_xy(point), expected)


def test_checkered_project_to_local_xz():
    point = geometry.make_point(3, 4, 5)
    expected = geometry.make_point(3, 5, 0)
    assert np.array_equal(Checkered.project_to_local_xz(point), expected)


@pytest.mark.parametrize(
    "point, expected",
    [
        (geometry.make_point(10, 10, 90), Palette.WHITE),
        (geometry.make_point(-10, -10, 90), Palette.WHITE),
        (geometry.make_point(10, -10, 90), Palette.BLACK),
        (geometry.make_point(30, 10, 90), Palette.BLACK),
    ],
)
def test_checkered_material(point, expected):
    material = Checkered(
        square_width=20,
        lighter=Palette.WHITE,
        darker=Palette.BLACK,
        projection=Checkered.project_to_local_xy,
    )
    assert material.get_color(point) == expected
예제 #5
0
 def _points_on_screen(self) -> List[geometry.Point]:
     return [
         geometry.make_point(x, y, 0) for x in range(0, self.width)
         for y in range(0, self.height)
     ]
예제 #6
0
 def project_to_local_yz(point: geometry.Point) -> geometry.Point:
     return geometry.make_point(geometry.get_y(point),
                                geometry.get_z(point), 0)