Files

83 lines
3.9 KiB
Python

"""Observed XY contours -> qualified Gaussian heights; never reconstruct paint."""
import numpy as np
from paint_objects import surface_geometry_plane
from shapely.ops import transform
class HeightRefusal(ValueError):
"""Expected missing height evidence, not a malformed input or code error."""
def _inputs(depth, alpha, camera_eye_z, matrix):
depth, alpha = np.asarray(depth), np.asarray(alpha)
m = np.asarray(matrix, dtype=float)
if (depth.ndim != 2 or depth.shape != alpha.shape or not depth.size
or m.shape != (6,) or not np.isfinite(m).all()
or not np.isfinite(camera_eye_z)
or abs(m[0] * m[3] - m[1] * m[2]) < 1e-12):
raise ValueError("Invalid projection inputs")
return depth, alpha, m
def bilinear_geometry(g, *, depth, alpha, camera_eye_z, pixel_to_scene):
"""Require each contributing sample; ignore only exactly zero weights."""
depth, alpha, m = _inputs(depth, alpha, camera_eye_z, pixel_to_scene)
inverse = np.linalg.inv([[m[0], m[1]], [m[2], m[3]]])
def lift(x, y, z=None):
uv = (np.column_stack([x, y]) - m[4:]) @ inverse.T - 0.5
base = np.floor(uv).astype(int)
fraction = uv - base
height = np.zeros(len(uv))
for dx, dy in ((0, 0), (1, 0), (0, 1), (1, 1)):
weight = ((fraction[:, 0] if dx else 1 - fraction[:, 0])
* (fraction[:, 1] if dy else 1 - fraction[:, 1]))
needed = weight > 0
ix, iy = base[needed, 0] + dx, base[needed, 1] + dy
if (np.any(ix < 0) or np.any(iy < 0)
or np.any(ix >= depth.shape[1]) or np.any(iy >= depth.shape[0])):
raise HeightRefusal("outside-depth")
dd, aa = depth[iy, ix], alpha[iy, ix]
if not (np.isfinite(dd) & (dd > 0) & np.isfinite(aa) & (aa >= 0.95)).all():
raise HeightRefusal("missing-observed-vertex-height")
height[needed] += weight[needed] * (camera_eye_z - dd)
return x, y, height
return transform(lift, g)
def project_observation(g, *, depth, alpha, camera_eye_z, pixel_to_scene):
"""Preserve every XY ring. Return explicit 2D-only refusal without fake Z.
A local plane is estimated from the original contour's interior evidence,
never from a catalog template. The existing surface acceptance guards apply.
"""
depth, alpha, m = _inputs(depth, alpha, camera_eye_z, pixel_to_scene)
if g.is_empty or not g.is_valid or g.geom_type != "Polygon":
raise ValueError("A valid observation Polygon is required")
try:
placed = bilinear_geometry(g, depth=depth, alpha=alpha,
camera_eye_z=camera_eye_z, pixel_to_scene=m)
return placed, dict(method="observed-bilinear", placement_status="placed")
except HeightRefusal as error:
direct_refusal = str(error)
# Crop by an inverse affine of the envelope. This bounds per-object work
# without changing the support pixels (works for rotated grids as well).
inverse = np.linalg.inv([[m[0], m[1]], [m[2], m[3]]])
uv = (np.asarray(g.envelope.exterior.coords)[:, :2] - m[4:]) @ inverse.T
x0, y0 = np.maximum(np.floor(uv.min(0)).astype(int) - 2, 0)
x1, y1 = np.minimum(np.ceil(uv.max(0)).astype(int) + 2, depth.shape[1::-1])
if x1 <= x0 or y1 <= y0:
return None, dict(placement_status="2d-only", reason="outside-depth")
cropped = [*m[:4], m[4] + m[0] * x0 + m[1] * y0,
m[5] + m[2] * x0 + m[3] * y0]
height, info = surface_geometry_plane(
g, depth=depth[y0:y1, x0:x1], alpha=alpha[y0:y1, x0:x1],
camera_eye_z=camera_eye_z, pixel_to_scene=cropped,
)
info = dict(info, direct_refusal=direct_refusal)
if height is None:
return None, dict(info, placement_status="2d-only")
placed = transform(lambda x, y, z=None: (x, y, height(x, y)), g)
return placed, dict(info, method="observed-interior-plane", placement_status="placed")