upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 23:15:53 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 23:15:53 -0500
commitd7efebf7d43492e1fefd9bf9e5f2e8e762fb20eb (patch)
tree4492c90b368bdcbe7091d4d785c5ddec8103b8c4
parente23327ccc14fe6589419f5f679be99bdf6f01547 (diff)
parentbda6858c16271c0103367b6ec325678de9102465 (diff)
Merge pull request #4555 from Vonsek
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
-rw-r--r--tests/test_first_light.py85
-rw-r--r--tools/first_light.py48
2 files changed, 116 insertions, 17 deletions
diff --git a/tests/test_first_light.py b/tests/test_first_light.py
index aeea7d4d3..8a8f58646 100644
--- a/tests/test_first_light.py
+++ b/tests/test_first_light.py
@@ -500,6 +500,91 @@ class OracleTests(unittest.TestCase):
500 check_image(b"", self.manifest) 500 check_image(b"", self.manifest)
501 501
502 502
503class ProbeCoordinateTests(unittest.TestCase):
504 rgba = (0.25, 0.75, 0.0, 1.0)
505 image = struct.pack("<16f", *([CLEAR] * 8 + list(rgba) * 2))
506
507 def setUp(self):
508 self.manifest = {"width": 2, "height": 2, "min_coverage": 0.5, "max_coverage": 0.5}
509
510 def test_rejects_invalid_coordinates_in_every_probe_path(self):
511 for point in ([-1, 1], [-1, 0], [2, 0], [0, 2], [0, -1], {}, [0], [True, 1], [0, 1.0]):
512 probes = (
513 ("clear_pixel", {"clear_pixel": point}),
514 ("centre_pixel", {"centre_pixel": point, "centre_uv": self.rgba[:2]}),
515 ("centre_pixel", {"centre_pixel": point, "centre_rgba": self.rgba}),
516 ("uv_probes", {"uv_probes": [{"pixel": point, "uv": self.rgba[:2]}]}),
517 ("rgba_probes", {"rgba_probes": [{"pixel": point, "rgba": self.rgba}]}),
518 )
519 for field, probe in probes:
520 with self.subTest(point=point, probe=probe):
521 with self.assertRaisesRegex(AssertionError, f"invalid {field}.*coordinates"):
522 check_image(self.image, self.manifest | probe)
523
524 def test_accepts_edges_and_optional_probes(self):
525 for probe in (
526 {},
527 {"clear_pixel": None, "centre_pixel": None, "centre_uv": self.rgba[:2]},
528 {"centre_uv": self.rgba[:2]},
529 {
530 "clear_pixel": [1, 0],
531 "centre_pixel": [0, 1],
532 "centre_uv": self.rgba[:2],
533 "uv_probes": [{"pixel": [1, 1], "uv": self.rgba[:2]}],
534 },
535 {
536 "centre_pixel": [1, 1],
537 "centre_rgba": self.rgba,
538 "centre_uv": [0.0, 0.0],
539 "rgba_probes": [{"pixel": [0, 1], "rgba": self.rgba}],
540 "uv_probes": [{"pixel": [1, 1], "uv": [0.0, 0.0]}],
541 },
542 ):
543 with self.subTest(probe=probe):
544 self.assertEqual(check_image(self.image, self.manifest | probe)["covered"], 2)
545
546 def test_empty_centre_pixel_uses_the_image_centre(self):
547 for key, expected in (("centre_uv", self.rgba[:2]), ("centre_rgba", self.rgba)):
548 with self.subTest(key=key):
549 manifest = self.manifest | {"centre_pixel": [], key: expected}
550 self.assertEqual(check_image(self.image, manifest)["covered"], 2)
551
552 def test_rgba_probes_still_reject_invalid_uv_coordinates(self):
553 manifest = self.manifest | {
554 "rgba_probes": [{"pixel": [0, 1], "rgba": self.rgba}],
555 "uv_probes": [{"pixel": [99, -5]}],
556 }
557 with self.assertRaisesRegex(AssertionError, r"invalid uv_probes pixel coordinates \[99, -5\]"):
558 check_image(self.image, manifest)
559
560 def test_null_uv_probes_are_optional_with_rgba(self):
561 manifest = self.manifest | {
562 "rgba_probes": [{"pixel": [0, 1], "rgba": self.rgba}],
563 "uv_probes": None,
564 }
565 self.assertEqual(check_image(self.image, manifest)["covered"], 2)
566 with self.assertRaises(TypeError):
567 check_image(self.image, manifest | {"uv_probes": False})
568
569 def test_check_reports_invalid_coordinates(self):
570 def draw(command, **kwargs):
571 Path(command[3]).write_bytes(self.image)
572
573 with tempfile.TemporaryDirectory() as tmp:
574 out = Path(tmp)
575 manifest = self.manifest | {
576 "files": {"mesh": "mesh.bin"},
577 "centre_uv": self.rgba[:2],
578 "centre_pixel": [2, 0],
579 }
580 (out / "manifest.json").write_text(json.dumps(manifest), encoding="utf-8", newline="\n")
581 with (patch.object(fl, "find_harness", return_value="vk_harness"),
582 patch.object(fl.subprocess, "run", side_effect=draw),
583 self.assertRaisesRegex(SystemExit, r"invalid centre_pixel coordinates \[2, 0\] for 2x2 image")):
584 fl.main(["--check", "--out", tmp])
585 self.assertFalse((out / "image.ppm").exists())
586
587
503class ReadObjTests(unittest.TestCase): 588class ReadObjTests(unittest.TestCase):
504 """Wavefront OBJ parsing in tools/first_light.py without a GPU or game package.""" 589 """Wavefront OBJ parsing in tools/first_light.py without a GPU or game package."""
505 590
diff --git a/tools/first_light.py b/tools/first_light.py
index 74540eb5e..d825c7c11 100644
--- a/tools/first_light.py
+++ b/tools/first_light.py
@@ -676,6 +676,17 @@ def check_image(data: bytes, manifest: dict) -> dict:
676 tests/test_first_light.py imports this function, so the test and ``--check`` agree. 676 tests/test_first_light.py imports this function, so the test and ``--check`` agree.
677 """ 677 """
678 width, height = manifest["width"], manifest["height"] 678 width, height = manifest["width"], manifest["height"]
679
680 def pixel_offset(point, field: str) -> int:
681 if (
682 not isinstance(point, (list, tuple)) or len(point) != 2
683 or any(type(coord) is not int for coord in point)
684 or not (0 <= point[0] < width and 0 <= point[1] < height)
685 ):
686 raise AssertionError(f"invalid {field} coordinates {point!r} for {width}x{height} image")
687 x, y = point
688 return (y * width + x) * 4
689
679 if len(data) != width * height * 16: 690 if len(data) != width * height * 16:
680 raise AssertionError( 691 raise AssertionError(
681 f"raw is {len(data)} bytes, expected {width * height * 16}" 692 f"raw is {len(data)} bytes, expected {width * height * 16}"
@@ -708,7 +719,7 @@ def check_image(data: bytes, manifest: dict) -> dict:
708 f"[{manifest.get('min_coverage', 0.0)}, {manifest.get('max_coverage', 1.0)}]" 719 f"[{manifest.get('min_coverage', 0.0)}, {manifest.get('max_coverage', 1.0)}]"
709 ) 720 )
710 if (clear := manifest.get("clear_pixel")) is not None: 721 if (clear := manifest.get("clear_pixel")) is not None:
711 i = (clear[1] * width + clear[0]) * 4 722 i = pixel_offset(clear, "clear_pixel")
712 if any(not _close(pixels[i + c], clear_val, 1e-5) for c in range(4)): 723 if any(not _close(pixels[i + c], clear_val, 1e-5) for c in range(4)):
713 raise AssertionError( 724 raise AssertionError(
714 f"clear pixel {clear} is {pixels[i : i + 4]}, expected {clear_val}" 725 f"clear pixel {clear} is {pixels[i : i + 4]}, expected {clear_val}"
@@ -728,9 +739,11 @@ def check_image(data: bytes, manifest: dict) -> dict:
728 raise AssertionError( 739 raise AssertionError(
729 f"textured manifest has no expected colour distinguishable from fallback {FALLBACK_RGBA} (tol {tol})" 740 f"textured manifest has no expected colour distinguishable from fallback {FALLBACK_RGBA} (tol {tol})"
730 ) 741 )
742 centre = manifest.get("centre_pixel")
743 if centre is None or centre == []:
744 centre = [width // 2, height // 2]
731 if (rgba := manifest.get("centre_rgba")) is not None: 745 if (rgba := manifest.get("centre_rgba")) is not None:
732 centre = manifest.get("centre_pixel") or [width // 2, height // 2] 746 i = pixel_offset(centre, "centre_pixel")
733 i = (centre[1] * width + centre[0]) * 4
734 tol = manifest.get("rgba_tolerance", 0.02) 747 tol = manifest.get("rgba_tolerance", 0.02)
735 for c, want in enumerate(rgba): 748 for c, want in enumerate(rgba):
736 if not _close(pixels[i + c], want, tol): 749 if not _close(pixels[i + c], want, tol):
@@ -738,8 +751,7 @@ def check_image(data: bytes, manifest: dict) -> dict:
738 f"centre {centre} is {pixels[i : i + 4]}, expected {rgba} (tol {tol})" 751 f"centre {centre} is {pixels[i : i + 4]}, expected {rgba} (tol {tol})"
739 ) 752 )
740 elif (uv := manifest.get("centre_uv")) is not None: 753 elif (uv := manifest.get("centre_uv")) is not None:
741 centre = manifest.get("centre_pixel") or [width // 2, height // 2] 754 i = pixel_offset(centre, "centre_pixel")
742 i = (centre[1] * width + centre[0]) * 4
743 for c, want in enumerate((uv[0], uv[1], 0.0, 1.0)): 755 for c, want in enumerate((uv[0], uv[1], 0.0, 1.0)):
744 if not _close(pixels[i + c], want, 2e-3): 756 if not _close(pixels[i + c], want, 2e-3):
745 raise AssertionError( 757 raise AssertionError(
@@ -748,24 +760,26 @@ def check_image(data: bytes, manifest: dict) -> dict:
748 if (rgba_probes := manifest.get("rgba_probes")) is not None: 760 if (rgba_probes := manifest.get("rgba_probes")) is not None:
749 tol = manifest.get("rgba_tolerance", 0.02) 761 tol = manifest.get("rgba_tolerance", 0.02)
750 for probe in rgba_probes: 762 for probe in rgba_probes:
751 px, py = probe["pixel"] 763 i = pixel_offset(probe.get("pixel"), "rgba_probes pixel")
752 i = (py * width + px) * 4
753 want_rgba = probe["rgba"] 764 want_rgba = probe["rgba"]
754 for c, want in enumerate(want_rgba): 765 for c, want in enumerate(want_rgba):
755 if not _close(pixels[i + c], want, tol): 766 if not _close(pixels[i + c], want, tol):
756 raise AssertionError( 767 raise AssertionError(
757 f"RGBA probe {probe['pixel']} is {pixels[i : i + 4]}, expected {want_rgba} (tol {tol})" 768 f"RGBA probe {probe['pixel']} is {pixels[i : i + 4]}, expected {want_rgba} (tol {tol})"
758 ) 769 )
759 else: 770 uv_probes = manifest.get("uv_probes")
760 for probe in manifest.get("uv_probes", []): 771 if uv_probes is None:
761 px, py = probe["pixel"] 772 uv_probes = []
762 i = (py * width + px) * 4 773 for probe in uv_probes:
763 u, v = probe["uv"] 774 i = pixel_offset(probe.get("pixel"), "uv_probes pixel")
764 for c, want in enumerate((u, v, 0.0, 1.0)): 775 if rgba_probes is not None:
765 if not _close(pixels[i + c], want, 2e-3): 776 continue
766 raise AssertionError( 777 u, v = probe["uv"]
767 f"UV probe {probe['pixel']} is {pixels[i : i + 4]}, expected {want} at {c}" 778 for c, want in enumerate((u, v, 0.0, 1.0)):
768 ) 779 if not _close(pixels[i + c], want, 2e-3):
780 raise AssertionError(
781 f"UV probe {probe['pixel']} is {pixels[i : i + 4]}, expected {want} at {c}"
782 )
769 return {"covered": covered, "ratio": ratio, "pixels": pixels} 783 return {"covered": covered, "ratio": ratio, "pixels": pixels}
770 784
771 785