"""Raw observation projection must preserve holes, XY and explicit refusals.""" import sys from pathlib import Path import numpy as np import pytest from shapely.geometry import Polygon, box sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "simulation/semantic-learning")) import paint_projection as pp # noqa: E402 def test_zero_weight_neighbours_and_boundary_do_not_remove_observation(): d = np.full((3, 3), 20.) alpha = np.ones_like(d) alpha[1, 1] = 0 args = dict(depth=d, alpha=alpha, camera_eye_z=25., pixel_to_scene=[1, 0, 0, 1, 0, 0]) placed = pp.bilinear_geometry(box(.5, .5, 2.5, 2.5), **args) assert all(q[2] == 5 for q in placed.exterior.coords) with pytest.raises(pp.HeightRefusal, match="missing-observed"): pp.bilinear_geometry(box(.6, .6, 2.4, 2.4), **args) def test_raw_interior_plane_preserves_exact_xy_and_hole_with_unreliable_edge(): d = np.full((60, 60), 20.) alpha = np.ones_like(d) alpha[9:11, 9:11] = 0 g = Polygon(box(1, 1, 4, 4).exterior.coords, [box(2, 2, 2.5, 2.5).exterior.coords]) before = [list(r.coords) for r in [g.exterior, *g.interiors]] placed, info = pp.project_observation( g, depth=d, alpha=alpha, camera_eye_z=25., pixel_to_scene=[.1, 0, 0, .1, 0, 0], ) assert info["method"] == "observed-interior-plane" assert info["samples"] >= 24 and info["residual_p95_m"] < 1e-10 for old, ring in zip(before, [placed.exterior, *placed.interiors], strict=True): assert [q[:2] for q in ring.coords] == old assert np.allclose(np.asarray(ring.coords)[:, 2], 5) @pytest.mark.parametrize("bad", ["missing", "discontinuity"]) def test_unreliable_raw_surface_remains_2d_without_invented_height(bad): d = np.full((60, 60), 20.) alpha = np.ones_like(d) if bad == "missing": alpha[:] = 0 else: alpha[9:11, 9:11] = 0 d[:, 25:] += 4 placed, info = pp.project_observation( box(1, 1, 4, 4), depth=d, alpha=alpha, camera_eye_z=25., pixel_to_scene=[.1, 0, 0, .1, 0, 0], ) assert placed is None and info["placement_status"] == "2d-only" and info["reason"] def test_invalid_projection_inputs_are_not_hidden_as_missing_support(): with pytest.raises(ValueError, match="Invalid projection"): pp.project_observation(box(0, 0, 1, 1), depth=np.ones((2, 2)), alpha=np.ones((3, 3)), camera_eye_z=25, pixel_to_scene=[1, 0, 0, 1, 0, 0])