WUT_Computer_Science/Programming/TRAK/main.py
Krzysztof kuhy Rudnicki 24f800d15a TRAK: zero findings, and a byte-identical render proving it
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>
2026-08-29 07:54:51 +02:00

173 lines
5.4 KiB
Python
Executable File

#!/usr/bin/env python3
"""Entry point: render a scene with one of the three algorithms in this course.
The algorithm, scene, resolution and output path all default to `config.ini`
and are overridable on the command line.
"""
import argparse
import importlib
import logging
from pathlib import Path
import matplotlib.pyplot as plt
from photon_mapping import render_photon_mapping
from rendering import ray_trace
from utils import load_config, parse_resolution
logger = logging.getLogger(__name__)
def main() -> None:
"""Parse the command line over `config.ini` defaults and run the renderer."""
# default config
config = load_config("config.ini")
# Parse
parser = argparse.ArgumentParser(description="Rendering Program")
parser.add_argument(
"--algorithm",
type=str,
help="Algorithm to use",
default=config.get("DEFAULT", "algorithm"),
)
parser.add_argument(
"--scene",
type=str,
help="Name of the scene to render (without .py).",
default=config.get("DEFAULT", "scene"),
)
parser.add_argument(
"--environment",
type=str,
help="Environment file",
default=config.get("DEFAULT", "environment"),
)
parser.add_argument(
"--env_blur",
type=str,
help="Environment blur",
default=config.get("DEFAULT", "env_blur"),
)
parser.add_argument(
"--resolution",
type=str,
help="Image resolution (WIDTHxHEIGHT)",
default=config.get("DEFAULT", "resolution"),
)
parser.add_argument(
"--samples_per_pixel",
type=int,
default=config.get("ray_tracing", "samples_per_pixel"),
help="Samples per pixel for rendering.",
)
parser.add_argument(
"--output",
type=str,
default=config.get("DEFAULT", "output"),
help="Output file name.",
)
parser.add_argument(
"--num_spheres",
type=int,
default=3,
help="Number of spheres in the scene for Ray Tracing 0",
)
parser.add_argument(
"--num_photons",
type=int,
default=config.getint("photon_mapping", "num_photons"),
help="Number of photons for photon mapping",
)
parser.add_argument(
"--max_depth",
type=int,
default=config.getint("photon_mapping", "max_depth"),
help="Maximum depth for photon tracing",
)
parser.add_argument(
"--gather_radius",
type=float,
default=config.getfloat("photon_mapping", "gather_radius"),
help="Radius for radiance estimation in photon mapping",
)
# The render is always written to outputs/; this only controls whether a
# viewer is ALSO popped up. It defaults off because img.show() shells out
# to xdg-open and then blocks until a human closes the window -- which
# left ../../run.sh run Programming/TRAK hanging for 45 minutes after the
# ray trace had actually finished.
parser.add_argument(
"--show",
action="store_true",
help="also open the finished image in a viewer (blocks until closed)",
)
args = parser.parse_args()
width, height = parse_resolution(args.resolution)
# Run the selected algorithm
if args.algorithm == "ray_tracing0":
logger.info("Starting ray tracing zero...")
ray_trace(
args.num_spheres,
args.environment,
image_width=width,
# na razie generujemy w kodzie, ale potem trzeba będzie obj wczytywać
image_height=height,
output_file=Path("outputs") / args.output,
)
elif args.algorithm == "ray_tracing":
logger.info("Starting ray tracing...")
try:
logger.info("%s", args.scene)
scene_module = importlib.import_module(f"scenes.{args.scene}")
except ModuleNotFoundError:
logger.info(
"Error: Scene '%s' not found in the 'scenes' directory.", args.scene
)
return
try:
scene = scene_module.setup_scene(
width=width, height=height, environment=f"{args.environment}"
)
except AttributeError:
logger.info(
"Error: Scene '%s' does not define a `setup_scene` function.",
args.scene,
)
return
# Renderowanie
logger.info(
"Rendering scene '%s' with %s samples per pixel...",
args.scene,
args.samples_per_pixel,
)
img = scene.render(samples_per_pixel=args.samples_per_pixel)
output_path = Path("outputs") / args.output
img.save(output_path)
logger.info("Image saved to %s", output_path)
if args.show:
img.show()
elif args.algorithm == "photon_mapping":
logger.info("Starting photon mapping...")
image = render_photon_mapping(
width, height, args.num_photons, args.max_depth, args.gather_radius
)
plt.imshow(image)
plt.axis("off")
output_path = Path("outputs") / args.output
plt.savefig(output_path)
logger.info("Image saved to %s", output_path)
if args.show:
plt.show()
else:
logger.info("Unknown algorithm: %s", args.algorithm)
return
if __name__ == "__main__":
logging.basicConfig(format="%(message)s", level=logging.INFO)
main()