Files
NODEDC_MISSION_CORE/simulation/urban-plan-research/topdown/pixel_geometry.py
T
DCCONSTRUCTIONS 69dc566665 feat(research): preserve urban geometry pipelines and qualification suites
Preserve offline geometry, marking, replay and evaluation pipelines with source manifests and synthetic tests. Research qualification does not activate these methods as production navigation.
2026-10-29 09:10:00 +03:00

32 lines
1.6 KiB
Python

"""Exact pixel-cell footprints in world coordinates, shared by proposal and fit.
OpenCV contour vertices mark centres of boundary pixels. Treating that line
as the object's outside edge systematically removes a half pixel on each side
of a thin stem. This module preserves the area of every observed pixel.
"""
import numpy as np
import shapely
from shapely.affinity import affine_transform
from shapely.geometry import Polygon
def vector(binary, patch):
mask=np.asarray(binary,bool)
if mask.shape!=(patch['height_px'],patch['width_px']):raise ValueError('Pixel shape mismatch')
rectangles=[];active={}
for y,row in enumerate(mask):
changes=np.diff(np.r_[False,row,False].astype('int8'))
runs=set(zip(np.flatnonzero(changes==1).tolist(),np.flatnonzero(changes==-1).tolist()))
for run in set(active)-runs:
rectangles.append((run[0]-.5,active.pop(run)-.5,run[1]-.5,y-.5))
for run in runs-set(active):active[run]=y
for run,start in active.items():rectangles.append((run[0]-.5,start-.5,run[1]-.5,len(mask)-.5))
if not rectangles:return Polygon()
bounds=np.asarray(rectangles,float)
pixels=shapely.union_all(shapely.box(*bounds.T))
corners=np.asarray(patch['world_corners'],float)
if not np.allclose(corners[2],corners[1]+corners[3]-corners[0],atol=1e-7,rtol=0):
raise ValueError('Pixel footprints require an affine orthographic patch')
u=(corners[1]-corners[0])/patch['width_px'];v=(corners[3]-corners[0])/patch['height_px']
return affine_transform(pixels,[u[0],v[0],u[1],v[1],*corners[0]])