"""Direct head-wing support integrated over immutable original pixel cells. Pixel-centre inclusion produces discontinuous area ratios on small wings. Intersecting pixel footprints preserves the actual observation and gives a continuous measurement as the four-parameter template moves. No dilation. """ import numpy as np from shapely.geometry import box from shapely.affinity import rotate,translate from pixel_geometry import vector from standard_arrow_templates import PROFILES def fields(grid): h,w=grid['size'];cell=grid['cell'];lo=grid.get('lo',grid['points'].min(0)) patch=dict(width_px=w,height_px=h,world_corners=np.array([lo,lo+[w*cell,0],lo+[w*cell,h*cell],lo+[0,h*cell]])) return [dict(observed=vector(v['observed'],patch), known=vector(v['observed']&v['allowed'],patch), paint=vector(v['paint']&v['observed']&v['allowed'],patch),cell=cell) for v in grid['views']] def measure(field,q,kind,parameters): if kind not in ('straight_arrow','straight_left_arrow','straight_right_arrow'):return None x,y,a,length,width,stroke=parameters[:6];scale=stroke/.15 head_lengths={v['L1'] for v in PROFILES.values()} if len(head_lengths)!=1:raise ValueError('Head measurement needs an explicit profile') start=length-next(iter(head_lengths))*scale;rows=[] for name,sign in [('left',1),('right',-1)]: local=box(start,stroke/2,length,width) if sign>0 else box(start,-width,length,-stroke/2) wing=q.intersection(translate(rotate(local,a,origin=(0,0),use_radians=True),xoff=x,yoff=y)) covered=wing.intersection(field['observed']);known=wing.intersection(field['known']) rows.append(dict(wing=name,coverage=min(1.,covered.area/max(wing.area,1e-12)), valid=min(1.,known.area/max(covered.area,1e-12)), direct_support=min(1.,known.intersection(field['paint']).area/max(known.area,1e-12)), pixels=known.area/field['cell']**2,measurement='original-pixel-cell-area')) return rows