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

76 lines
4.1 KiB
Python

"""Plan one uniform survey over observed ground, with an explicit coverage ledger."""
from pathlib import Path
import json
import time
import numpy as np
from PIL import Image
from fit_scene import project, world
from ground_observations import GroundObservations, POLICY, VERSION, sha
def prepare(out):
started = time.monotonic()
out = Path(out); out.mkdir(parents=True, exist_ok=True)
folder = out/'symbol-patches'; folder.mkdir(exist_ok=True)
observations = GroundObservations()
inventory = observations.inventory()
(out/'observations.json').write_text(json.dumps(inventory, indent=2))
bounds = np.asarray([t['bounds_xy'] for t in observations.tiles])
low, high = bounds[:, 0].min(0), bounds[:, 1].max(0)
step, width = POLICY['stride_m'], POLICY['window_m']
origin = np.floor(low/step)*step
patches, ledger, scheduled = [], [], []
offset = np.arange(-step/2+.25, step/2, .5)
ox, oy = np.meshgrid(offset, offset)
samples = np.c_[ox.ravel(), oy.ravel()]
for ix in range(int(np.ceil((high[0]-origin[0])/step))):
for iy in range(int(np.ceil((high[1]-origin[1])/step))):
center = origin+(np.array([ix, iy])+.5)*step
pid = f'ground-{ix:04d}-{iy:04d}'
record = dict(id=pid, center=center.tolist(), reason='no-covering-observation')
selected = None
for k in np.argsort(np.linalg.norm(observations.centers-center, axis=1), kind='stable'):
# A 60 m image cannot cover a 16 m window farther than 43 m.
if np.linalg.norm(observations.centers[k]-center) > 43:
continue
name = observations.tiles[k]['id']; g = observations.camera(name)
corners = center + np.array([[-1,-1],[1,-1],[1,1],[-1,1]])*width/2
uv = project(corners, g)
if uv.min() < 10 or uv.max() >= 1190:
continue
fraction = float(observations.surface(name, center+samples).mean())
record.update(reason='insufficient-observed-ground', observed_core_fraction=fraction)
if fraction < POLICY['core_fraction']:
continue
selected = name, g, uv, fraction
break
if selected is None:
ledger.append(record); continue
name, g, uv, fraction = selected
scheduled.append((pid,name,g,uv,fraction))
ledger.append(dict(record, reason='scheduled', observed_core_fraction=fraction, source_tile=name))
# The same grid order determines identity. Only disk access order changes.
materialized={}
for pid,name,g,uv,fraction in sorted(scheduled,key=lambda row:row[1]):
raw=observations.rgb(name);size=raw.shape[0]
if raw.shape[1]!=size:raise ValueError('Expected square orthographic render')
native=(uv+.5)*size/1200-.5
x0,y0=np.floor(native.min(0)).astype(int);x1,y1=np.ceil(native.max(0)).astype(int)
crop=raw[y0:y1,x0:x1];path=folder/(pid+'.png');Image.fromarray(crop).save(path)
corners=np.array([[x0,y0],[x1,y0],[x1,y1],[x0,y1]],float);xy=world((corners+.5)*1200/size-.5,g)
materialized[pid]=dict(id=pid,source_tile=name,box_native=[int(x0),int(y0),int(x1),int(y1)],
native_size=size,world_corners=xy.tolist(),width_px=crop.shape[1],height_px=crop.shape[0],mpp=60/size,sha256=sha(path))
if len(materialized)%50==0:print('GROUND_WINDOWS',len(materialized),flush=True)
patches=[materialized[row[0]] for row in scheduled]
manifest = dict(version=VERSION, patches=patches, policy=POLICY, manual_rois=[],
observation_snapshot=inventory['snapshot'], selection='observed-ground-uniform-grid',
perspective_mask_input=False, seconds=time.monotonic()-started)
(out/'symbol-patches.json').write_text(json.dumps(manifest, indent=2))
(out/'coverage-ledger.json').write_text(json.dumps(dict(cells=ledger, policy=POLICY,
inferred_coverage=False), indent=2))
print('PREPARED', len(patches), 'CELLS', len(ledger), flush=True)
if __name__ == '__main__':
prepare('/out')