Files
NODEDC_MISSION_CORE/simulation/urban-plan-research/topdown/observed_fixed.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

83 lines
4.0 KiB
Python

"""Fit one immutable preset under the same geometric constraints as admission."""
import cv2
import shapely
import numpy as np
from scipy.ndimage import map_coordinates
from scipy.optimize import least_squares
from standard_arrow_templates import preset
from pixel_geometry import vector
from direct_fixed import fit_target
from registration import POLICY as GEOMETRY_POLICY
from plan_marking_policy import source_correspondence, MIN_SOURCE_CORRESPONDENCE
VERSION = 'topdown-object-observation-v22'
def grid_polygon(mask, grid):
cell=grid['cell'];height,width=grid['size'];low=grid['lo']
corners=np.array([low,low+[width*cell,0],low+[width*cell,height*cell],low+[0,height*cell]])
patch=dict(world_corners=corners,width_px=width,height_px=height,mpp=cell)
value=vector(mask,patch)
return value
def constraints_for(grid, proposal_targets=None):
# Use the exact sampled registration contract. The former independent
# polygon-area and interpolated-distance constraints could veto a pose
# passing final checks. No admission threshold changes here.
from registration_evaluator import RegistrationEvaluator
if 'points' not in grid:
y,x=np.indices(grid['size']);grid['points']=np.c_[x.ravel(),y.ravel()]*grid['cell']+grid['lo']
evaluator=RegistrationEvaluator(grid)
def constraints(q):
values=evaluator.constraints(q)
if proposal_targets:
overlap=max(source_correspondence(q,p) for p in proposal_targets)
values.append(overlap-MIN_SOURCE_CORRESPONDENCE)
return values
return constraints
def boundary_seed(grid):
cell=grid['cell'];low=grid['lo'];fields=[]
for v in grid['views']:
paint=v['target'].astype('uint8')
if not paint.any():continue
outside=cv2.distanceTransform(1-paint,cv2.DIST_L2,cv2.DIST_MASK_PRECISE)
inside=cv2.distanceTransform(paint,cv2.DIST_L2,cv2.DIST_MASK_PRECISE)
fields.append((((outside-inside)*cell).astype('float64'),v['observed']))
def seed(kind,profile,pose,bounds):
template=preset(kind,profile)
distances=np.arange(0,template.length,.04)
unit=shapely.get_coordinates(shapely.line_interpolate_point(template.boundary,distances))
def residual(delta):
x,y,a,factor=pose+delta;rotation=np.array([[np.cos(a),np.sin(a)],[-np.sin(a),np.cos(a)]])
uv=(unit@rotation*factor+[x,y]-low)/cell;values=[]
for field,observed in fields:
error=map_coordinates(field,[uv[:,1],uv[:,0]],order=1,mode='constant',cval=1.,prefilter=False)
known=map_coordinates(observed.astype('uint8'),[uv[:,1],uv[:,0]],order=0,mode='constant',cval=0,prefilter=False)>0
values.extend(np.where(known,error,0.))
return values
lower,upper=np.asarray(bounds).T
solved=least_squares(residual,np.zeros(4),bounds=(lower-pose,upper-pose),x_scale=[.2,.2,.03,.03],
loss='soft_l1',f_scale=.05,max_nfev=80,
ftol=1e-6,xtol=1e-6,gtol=1e-6)
return pose+solved.x
return seed
def fit_grid(grid, proposal_targets=None, object_constraints=None):
view=grid['views'][0]
target=grid_polygon(view['target'] & view['allowed'] & view['observed'],grid)
if target.is_empty:return []
def possible(template):
if not proposal_targets:return True
minimum,maximum=template.area*.75**2,template.area*1.25**2
# Intersection cannot exceed the smaller area; union cannot be less
# than the larger. This proves some type/profile pairs infeasible
# before evaluating expensive pixel evidence, without changing a gate.
return min(maximum,target.area)/max(minimum,target.area)>=GEOMETRY_POLICY['min_iou']
return fit_target(target,geometric_constraints=(lambda q: []) if object_constraints is not None else constraints_for(grid,proposal_targets),
object_constraints=object_constraints,preset_filter=possible,evidence_seed=boundary_seed(grid),
seed_targets=proposal_targets)