WUT_Computer_Science/Programming/TRAK/scenes/three_spheres.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

87 lines
2.3 KiB
Python

"""Three coloured glass spheres on a checkered floor, over a photo backdrop.
Each sphere filters a different channel, so the floor seen through them shows
what the refractive material does to each colour separately.
"""
import numpy as np
from sightpy import Glossy, Plane, Refractive, Scene, Sphere, image, rgb, vec3
def setup_scene(
width: int = 400, height: int = 300, environment: str = "miramar.jpeg"
) -> Scene:
"""Build the three-sphere scene at `width` x `height` over `environment`."""
blue_glass = Refractive(
n=vec3(1.5 + 4e-8j, 1.5 + 4e-8j, 1.5 + 0.0j)
) # n = index of refraction
green_glass = Refractive(n=vec3(1.5 + 4e-8j, 1.5 + 0.0j, 1.5 + 4e-8j))
red_glass = Refractive(n=vec3(1.5 + 0.0j, 1.5 + 5e-8j, 1.5 + 5e-8j))
floor = Glossy(
diff_color=image("checkered_floor.png", repeat=80.0),
n=vec3(1.2 + 0.3j, 1.2 + 0.3j, 1.1 + 0.3j),
roughness=0.2,
spec_coeff=0.3,
diff_coeff=0.9,
)
# Set Scene
scene = Scene(ambient_color=rgb(0.05, 0.05, 0.05))
angle = np.pi / 2 * 0.3
scene.add_Camera(
look_from=vec3(2.5 * np.sin(angle), 0.25, 2.5 * np.cos(angle) - 1.5),
look_at=vec3(0.0, 0.25, -1.5),
screen_width=width,
screen_height=height,
)
scene.add_DirectionalLight(Ldir=vec3(0.52, 0.45, -0.5), color=rgb(0.15, 0.15, 0.15))
scene.add(
Sphere(
material=blue_glass,
center=vec3(-1.2, 0.0, -1.5),
radius=0.5,
shadow=False,
max_ray_depth=3,
)
)
scene.add(
Sphere(
material=green_glass,
center=vec3(0.0, 0.0, -1.5),
radius=0.5,
shadow=False,
max_ray_depth=3,
)
)
scene.add(
Sphere(
material=red_glass,
center=vec3(1.2, 0.0, -1.5),
radius=0.5,
shadow=False,
max_ray_depth=3,
)
)
scene.add(
Plane(
material=floor,
center=vec3(0, -0.5, -3.0),
width=120.0,
height=120.0,
u_axis=vec3(1.0, 0, 0),
v_axis=vec3(0, 0, -1.0),
max_ray_depth=3,
)
)
# see sightpy/backgrounds
scene.add_Background(environment)
return scene