Toolkit: segment, mass, render plates, batch, gate, matrices, cost, smoke test
Generalized from the render lanes' proven tools: map segmentation, 2.5D massing by wealth zone, a CPU ray marcher with analytic landmarks and true-scale proxy people, prompt composition from blocks, a self-calibrating batch submitter with a renderer adapter point, the plate-vs-frame gates (drift, ECC, landmark region, skyline and invented structures, figure brackets, corner sweep), matrix job builders, a cost line, and an end-to-end smoke test on a synthetic town.
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
"""Shared helpers for the city render toolkit: config loading, world/map transforms, image utilities.
|
||||
|
||||
The world frame is meters: X east, Y north, Z up. Z = 0 is the town's ground plane.
|
||||
A map bitmap maps to the world through `meters_per_px` and `origin_px` (the bitmap pixel that sits at world 0,0).
|
||||
Row index grows southward, so world Y = -(py - origin_py) * meters_per_px.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
def load_config(path):
|
||||
with open(path) as f:
|
||||
cfg = json.load(f)
|
||||
cfg["_dir"] = os.path.dirname(os.path.abspath(path))
|
||||
return cfg
|
||||
|
||||
|
||||
def resolve(cfg, p):
|
||||
"""Resolve a path from the config relative to the config file's directory."""
|
||||
if p is None:
|
||||
return None
|
||||
return p if os.path.isabs(p) else os.path.join(cfg["_dir"], p)
|
||||
|
||||
|
||||
class MapFrame:
|
||||
"""Pixel <-> world transforms for one bitmap."""
|
||||
|
||||
def __init__(self, meters_per_px, origin_px=(0.0, 0.0)):
|
||||
self.mpp = float(meters_per_px)
|
||||
self.ox, self.oy = float(origin_px[0]), float(origin_px[1])
|
||||
|
||||
def px_to_world(self, px, py):
|
||||
return (np.asarray(px, float) - self.ox) * self.mpp, -(np.asarray(py, float) - self.oy) * self.mpp
|
||||
|
||||
def world_to_px(self, X, Y):
|
||||
return np.asarray(X, float) / self.mpp + self.ox, -np.asarray(Y, float) / self.mpp + self.oy
|
||||
|
||||
|
||||
def ell(k):
|
||||
import cv2
|
||||
k = max(3, int(k)) | 1
|
||||
return cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (k, k))
|
||||
|
||||
|
||||
def h01(*a):
|
||||
"""Deterministic hash in [0, 1) from any printable arguments (stable across runs)."""
|
||||
import hashlib
|
||||
return int(hashlib.md5(repr(a).encode()).hexdigest()[:8], 16) / 0xFFFFFFFF
|
||||
|
||||
|
||||
def bil(A, r, c):
|
||||
"""Bilinear sample of a 2D array at fractional (row, col); clamps to the edges."""
|
||||
hh, ww = A.shape
|
||||
r = np.clip(r, 0, hh - 1.001)
|
||||
c = np.clip(c, 0, ww - 1.001)
|
||||
r0 = r.astype(np.int32)
|
||||
c0 = c.astype(np.int32)
|
||||
fr = (r - r0).astype(np.float32)
|
||||
fc = (c - c0).astype(np.float32)
|
||||
return ((A[r0, c0] * (1 - fc) + A[r0, c0 + 1] * fc) * (1 - fr)
|
||||
+ (A[r0 + 1, c0] * (1 - fc) + A[r0 + 1, c0 + 1] * fc) * fr)
|
||||
|
||||
|
||||
def save_json(obj, path):
|
||||
def conv(v):
|
||||
if hasattr(v, "tolist"):
|
||||
return v.tolist()
|
||||
if isinstance(v, dict):
|
||||
return {k: conv(x) for k, x in v.items()}
|
||||
if isinstance(v, (list, tuple)):
|
||||
return [conv(x) for x in v]
|
||||
return v
|
||||
with open(path, "w") as f:
|
||||
json.dump(conv(obj), f, indent=1)
|
||||
Reference in New Issue
Block a user