mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-11-10 22:07:10 +01:00
494 ruff findings to 0, and mypy --strict is clean too -- the first course in this campaign where that is true of both. Two real bugs turned up on the way, neither of which ruff can see: - `main.py` passed `args.environment_map` to `ray_trace`, and argparse only ever defined `--environment`. Every `--algorithm ray_tracing0` run died with AttributeError before rendering a pixel, so `rendering.py` -- the largest file in the course -- had no way to be exercised at all. - `mouse_button` in the environment-map viewer assigned `mouse_last_x/y` without declaring them `global`, so the drag origin went to locals and was thrown away. Measured against HEAD: a 10x5 px drag turned the camera by 11.0/20.5 degrees because it measured from (0, 0); it is 1.0/0.5 now, which is what the 0.1 degrees-per-pixel constant says it should be. Fixing the first one is what made the rest safe. `ray_tracing0` has no RNG anywhere in it, so it renders byte-identically every time, and the whole refactor of `rendering.py` is verified by one md5 comparison: 02484bdb6b6c1ddeff317d7e5566580f, 248619 rays, before and after. The cornell_box path cannot do that -- its ray count drifts by ~20 between identical runs. Three files were over the 250-line cap and are now split, geometry away from the algorithm that uses it: rendering.py 462 -> 137 + raytracer.py, photon_mapping.py 261 -> 160 + photon_geometry.py, and code/photonmapping/python/main.py 257 -> 203 + geometry.py. No TRAK source file is over the cap now. The two photon mappers do use numpy's legacy global RNG, so NPY002 could not be bit-identical. Each was done in two steps: restructure first, verified identical under an external seed (digests 7220370f60206a62 and 21b4771504ab2351, and an identical 828 photon traces), then the swap to `default_rng` on its own, checked against the before/after distributions. Verified by running, not by reading: - cornell_box through `./run.sh run Programming/TRAK`: 24.27s, 13294 rays - all five scenes build after `from sightpy import *` became explicit imports - the standalone photon mapper end to end: 81.4s, 38742 photon traces - the EXR loader byte for byte: a0c3890350ce6101247c6948, 6291456 bytes - the GLUT viewer under xvfb-run, which drew a real 1024x512 frame `pyproject.toml` gains a mypy override for Imath, OpenEXR, OpenGL and matplotlib. Those four ship neither stubs nor py.typed, and without it mypy cannot import them at all and so reports nothing else about the file. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
147 lines
4.6 KiB
Python
Executable File
147 lines
4.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Renders an image using raytracing.
|
|
|
|
The geometry and shading live in `raytracer`; what is left here is the scene
|
|
this lab builds (a checkerboard plane and a row of spheres) and the pixel loop
|
|
that shoots one primary ray per pixel and reflects it up to `DEPTH_MAX` times.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import time
|
|
from typing import TYPE_CHECKING
|
|
|
|
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
from raytracer import (
|
|
SURFACE_OFFSET,
|
|
Lighting,
|
|
SceneObject,
|
|
make_plane,
|
|
make_sphere,
|
|
normalize,
|
|
trace_ray,
|
|
)
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Default light and material parameters.
|
|
AMBIENT = 0.05
|
|
DIFFUSE_C = 1.0
|
|
SPECULAR_C = 1.0
|
|
SPECULAR_K = 50
|
|
|
|
DEPTH_MAX = 5 # Maximum number of light reflections.
|
|
|
|
# Progress is logged every this many pixel columns.
|
|
_PROGRESS_EVERY = 10
|
|
|
|
|
|
def build_scene(num_spheres: int) -> list[SceneObject]:
|
|
"""Lay out a checkerboard plane and `num_spheres` spheres in a receding row."""
|
|
color_plane0 = 1.0 * np.ones(3)
|
|
color_plane1 = 0.0 * np.ones(3)
|
|
scene = [make_plane([0.0, -0.5, 0.0], [0.0, 1.0, 0.0], color_plane0, color_plane1)]
|
|
|
|
base_radius = 1 / np.sqrt(num_spheres) # Im więcej kul, tym mniejsze
|
|
base_distance = 4.5 / num_spheres
|
|
|
|
for i in range(num_spheres):
|
|
# Wyliczanie pozycji każdej kuli
|
|
x = (i - num_spheres // 2) * base_distance
|
|
y = 0.1
|
|
z = 1.0 + i * 0.5
|
|
|
|
# Dynamiczny kolor (gradient na podstawie indeksu)
|
|
color = np.array([i / num_spheres, (num_spheres - i) / num_spheres, 0.5])
|
|
|
|
# Dodanie kuli do sceny
|
|
scene.append(make_sphere([x, y, z], base_radius, color))
|
|
|
|
return scene
|
|
|
|
|
|
def ray_trace(
|
|
num_spheres: int,
|
|
_environment: str,
|
|
image_width: int = 400,
|
|
image_height: int = 300,
|
|
output_file: str | Path = "fig.png",
|
|
) -> None:
|
|
"""Render `num_spheres` spheres over a checkerboard and write `output_file`.
|
|
|
|
`_environment` is accepted for symmetry with the other two algorithms in
|
|
`main.py` and ignored: this renderer has no environment map.
|
|
"""
|
|
scene = build_scene(num_spheres)
|
|
lighting = Lighting(
|
|
position=np.array([5.0, 5.0, -10.0]),
|
|
color=np.ones(3),
|
|
ambient=AMBIENT,
|
|
diffuse_c=DIFFUSE_C,
|
|
specular_c=SPECULAR_C,
|
|
specular_k=SPECULAR_K,
|
|
)
|
|
|
|
color = np.zeros(3) # Current color.
|
|
camera_origin = np.array([0.0, 0.35, -1.0]) # Camera.
|
|
camera_target = np.array([0.0, 0.0, 0.0]) # Camera pointing to.
|
|
img = np.zeros((image_height, image_width, 3))
|
|
|
|
aspect = float(image_width) / image_height
|
|
# Screen coordinates: x0, y0, x1, y1.
|
|
screen = (-1.0, -1.0 / aspect + 0.25, 1.0, 1.0 / aspect + 0.25)
|
|
|
|
render_time = time.time()
|
|
reflections = 0
|
|
rays = 0
|
|
primary_rays = 0
|
|
# Loop through all pixels.
|
|
for i, x in enumerate(np.linspace(screen[0], screen[2], image_width)):
|
|
if i % _PROGRESS_EVERY == 0:
|
|
logger.info("%s %%", round(i / float(image_width) * 100, 2))
|
|
for j, y in enumerate(np.linspace(screen[1], screen[3], image_height)):
|
|
color[:] = 0
|
|
camera_target[:2] = (x, y)
|
|
ray_origin = camera_origin
|
|
ray_direction = normalize(camera_target - camera_origin)
|
|
depth = 0
|
|
reflection = 1.0
|
|
primary_rays += 1
|
|
# Loop through initial and secondary rays.
|
|
while depth < DEPTH_MAX:
|
|
traced = trace_ray(scene, lighting, ray_origin, ray_direction)
|
|
rays += 1
|
|
if not traced:
|
|
break
|
|
reflections += 1
|
|
obj, hit_point, hit_normal, color_ray = traced
|
|
# Reflection: create a new ray.
|
|
ray_origin = hit_point + hit_normal * SURFACE_OFFSET
|
|
ray_direction = normalize(
|
|
ray_direction - 2 * np.dot(ray_direction, hit_normal) * hit_normal
|
|
)
|
|
depth += 1
|
|
color += reflection * color_ray
|
|
reflection *= obj.get("reflection", 1.0)
|
|
img[image_height - j - 1, i, :] = np.clip(color, 0, 1)
|
|
render_time = time.time() - render_time
|
|
|
|
plt.imsave(output_file, img)
|
|
logger.info(
|
|
"Image saved as %s\nresolution: %sx%s\nrender time: %s s\nreflections: %s\n"
|
|
"rays (initial): %s\nrays (secondary): %s\nrays (total): %s",
|
|
output_file,
|
|
image_width,
|
|
image_height,
|
|
round(render_time, 2),
|
|
reflections,
|
|
primary_rays,
|
|
rays - primary_rays,
|
|
rays,
|
|
)
|