ridge-bake: the ring is the bake, 32 azimuth tiles per card (schema 3)
The 72 degree window and the baked camera are deleted. The march's 6400 columns are the bake: seams run periodic round the ring (DP over three laps, middle kept), ridge.cards runs per tile on 204 columns with the matching normals, and the vertical anchors read the skyline of the whole ring. Each card tile carries its own distance_m; camera.r_max_m is the largest offset the per-card linear slide fits within 24 card px (and the resident gate allows). Tile, place and resident budgets replace the window's. Manifest schema 3, baker version 4. Tests and mutants rewritten for the ring. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
d851a5fca9
commit
65db62c986
5 changed files with 583 additions and 375 deletions
|
|
@ -1,10 +1,11 @@
|
|||
"""Ridge backdrop export: bake a place with ridge.py and write its K+1 G-buffer cards (colour + geometry WebP each), a manifest v2 and the index.
|
||||
"""Ridge backdrop export: bake a place with ridge.py and write its K+1 G-buffer cards as 32 azimuth tiles each (colour + geometry WebP per card per tile), a manifest v3 and the index.
|
||||
|
||||
python3 -I scripts/ridge-bake/export.py <place-id> --tiles DIR --out public/ridge/ [--work DIR] [--sheet contact.png]
|
||||
python3 -I scripts/ridge-bake/export.py --write-schema (rewrites ridge-bake.schema.json from SCHEMA below)
|
||||
|
||||
The runtime reads manifest.json (SCHEMA is its contract) and nothing else about the bake; the camera (ruling ridge-camera-on-circle) is the baker's, so per-card scale and the heading range ride the manifest; it lights the cards itself (ruling ridge-cards-runtime-lit). The pure parts (budget, bake_id, manifest, index, schema check)
|
||||
import no baker; bake_place imports ridge, which needs RIDGE_TILES and RIDGE_WORK (main sets them from --tiles / --work)."""
|
||||
The runtime reads manifest.json (SCHEMA is its contract) and nothing else about the bake. The ring is the whole 360 degrees (ruling ridge-camera-full-ring), absolute compass, column 0 = north, cut into TILES tiles of TILE_W columns plus an APRON of real neighbour
|
||||
columns each side; the bake holds no camera: it ships each card tile's distance_m and camera.r_max_m, the largest offset the per-card linear slide can take (ruling ridge-offset-range-fit); the client lights the cards itself (ruling ridge-cards-runtime-lit).
|
||||
The pure parts (budget, bake_id, manifest, index, schema check) import no baker; bake_place imports ridge, which needs RIDGE_TILES and RIDGE_WORK (main sets them from --tiles / --work)."""
|
||||
import os, sys, json, math, shutil, hashlib, argparse, tempfile
|
||||
from pathlib import Path
|
||||
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
||||
|
|
@ -12,18 +13,22 @@ HERE = Path(__file__).resolve().parent
|
|||
sys.path.insert(0, str(HERE))
|
||||
import numpy as np, imagecodecs
|
||||
|
||||
BAKER_VERSION = 3
|
||||
WINDOW_DEG = 72
|
||||
PLANE_W = 1280 # every card is the whole 72 deg window wide
|
||||
PLANE_W_MAX = 4096
|
||||
MAX_CARDS = 6 # per place; a full-frame card decodes to 2 textures x 1280 x 600 x 4 B = 6.1 MB, the decode budget below holds 3 of them and cropping to the row band is what lets a livelier place have more
|
||||
TOTAL_BYTES_MAX = 1_500_000 # every card file of a place together
|
||||
DECODE_BYTES_MAX = 20_000_000 # the cards decoded: sum over cards of 2 textures x w x band h x 4 bytes
|
||||
COVER_TOL = 0.02 # the composite of the cards may differ from the opaque view by this much of a pixel, nowhere more
|
||||
CAMERA_R_M = 200.0 # ruling ridge-camera-on-circle: the camera sits on a circle of this radius round the bake centre, signed, + forward along its heading and - backward; places.json may set camera_r_m per place. The steward's calibration for feel, tuned in the art pass
|
||||
HEADING_MAX_DEG = 23.0 # the drift's heading range each side of look_az: today's phone sweep, 409 card px (the runtime caps it by what a viewport can cover)
|
||||
R_FRAC_MAX = 0.5 # |r| stays under this share of the nearest card's distance: the offset must stay well inside the nearest cylinder
|
||||
REST_SCALE_MIN = 1 / 1.1 # a 16:9 viewport is width-bound and the runtime's plane is 1.1 viewport widths wide (view.ts windowFor, DRIFT_FRAC 0.05 a side): a card scaled below this leaves the viewport's edge bare at rest
|
||||
BAKER_VERSION = 4
|
||||
RING_W = 6400 # the ring's columns (ridge.W, asserted in bake_place): column c is at azimuth (c + 0.5) / RING_W x 360, column 0 = north
|
||||
TILES = 32 # azimuth tiles of the ring, each card cut into TILES files per kind (ruling ridge-camera-full-ring: the client loads the arc near its heading)
|
||||
TILE_W = RING_W // TILES # 200 columns = 11.25 degrees owned by a tile
|
||||
APRON = 2 # real neighbour columns each side in a tile's file: the client's ink pass and lighting read +-1 texel; the client crops it
|
||||
LIT_W = TILE_W + 2 # a lit canvas keeps one apron texel each side
|
||||
TILE_DEG = 360.0 / TILES
|
||||
MAX_CARDS = 6 # per place; a card is cropped to its row band in every tile, which is what lets a livelier place have more
|
||||
TILE_BYTES_MAX = 235_000 # every file of one tile (1.5 MB per 72 degrees, scaled to 11.25)
|
||||
PLACE_BYTES_MAX = 7_500_000 # every file of the place
|
||||
RESIDENT_BYTES_MAX = 20_000_000 # the lit canvases (4 B x LIT_W x band h per card) of the worst run of consecutive tiles, cyclic; the loader decodes one tile at a time, so the decode transient is not gated
|
||||
HFOV_MAX_DEG = 72 / 1.1 # the widest viewport: a 16:9 viewport with 1.1 widths of plane (view.ts windowFor)
|
||||
FIT_PX_MAX = 24.0 # the per-card linear slide may err by this many card pixels over the viewport (provisional, tuned by eye: ruling ridge-offset-range-fit)
|
||||
R_MAX_FLOOR_M = 25.0 # a bake whose cards cannot take this offset is refused
|
||||
PHI_STEPS = 24 # the camera's angle on its circle against its heading, sampled by the r_max search
|
||||
COVER_TOL = 0.02 # the composite of a tile's cards may differ from the opaque view by this much of a pixel, nowhere more
|
||||
WEBP_QUALITY = 80
|
||||
SHEET_SKY = (0.80, 0.86, 0.94) # the flat sky of the contact sheet
|
||||
SHEET_LIGHT = (-0.35, 0.55, 0.76) # the fixed light of the contact sheet in (along ray, across ray, up); the sheet is a look at the cards, the client lights the real thing
|
||||
|
|
@ -38,19 +43,23 @@ _num, _int, _str = {'type': 'number'}, {'type': 'integer'}, {'type': 'string'}
|
|||
_numnull = {'type': ['number', 'null']}
|
||||
_tz = {'type': 'string', 'format': 'iana-tz'} # an IANA zone the system knows, as ridge.load_preset requires; validate() checks it
|
||||
def _obj(**props): return {'type': 'object', 'required': list(props), 'additionalProperties': False, 'properties': props}
|
||||
def _objnull(**props): return dict(_obj(**props), type=['object', 'null'])
|
||||
_file = _obj(file=_str, bytes=_int)
|
||||
SCHEMA = {'$schema': 'https://json-schema.org/draft/2020-12/schema', 'title': 'ridge-bake manifest v2 and index v1', '$defs': {
|
||||
'manifest': _obj(schema={'const': 2}, baker_version=_int, bake_id=_str,
|
||||
SCHEMA = {'$schema': 'https://json-schema.org/draft/2020-12/schema', 'title': 'ridge-bake manifest v3 and index v1', '$defs': {
|
||||
'manifest': _obj(schema={'const': 3}, baker_version=_int, bake_id=_str,
|
||||
place=_obj(id=_str, name=_str, lat=_num, lon=_num, altitude_km=_num, eye_m=_num, tz=_tz),
|
||||
view=_obj(look_az_deg=_num, window_deg={'const': WINDOW_DEG}, plane_w={'type': 'integer', 'maximum': PLANE_W_MAX}, plane_h=_int, px_per_deg_h=_num, px_per_deg_v=_num, exaggeration=_num, horizon_row=_num, sky_row=_num, near_row=_num),
|
||||
view=_obj(look_az_deg=_num, ring_w={'const': RING_W}, tile_w={'const': TILE_W}, tiles={'const': TILES}, apron={'const': APRON}, plane_h=_int, px_per_deg_h=_num, px_per_deg_v=_num, exaggeration=_num, horizon_row=_num, sky_row=_num, near_row=_num),
|
||||
curvature=_obj(r_earth_m=_num, k_ref=_num),
|
||||
encoding=_obj(log_d_min=_num, log_d_max=_num, normal={'const': 'rt8'}),
|
||||
camera=_obj(r_m=_num, heading_max_deg=_num),
|
||||
cards={'type': 'array', 'minItems': 1, 'maxItems': MAX_CARDS, 'items': dict(_obj(top=_int, h=_int, near_m=_numnull, far_m=_numnull, median_m=_num, distance_m=_num, scale=_num, colour=_file, geometry=_file),
|
||||
description="nearest first; the runtime paints the list last to first. top and h place the card back at its rows of the frame; near_m and far_m are the median seam depths, null at the ends; median_m and distance_m are the median distance of the card's solid pixels that are not wall; scale = distance_m / (distance_m - camera.r_m) is the card's pan rate per unit of heading and its static size.")},
|
||||
w=_int, h=_int, bytes=_int,
|
||||
camera=_obj(r_max_m=_num),
|
||||
cards={'type': 'array', 'minItems': 1, 'maxItems': MAX_CARDS, 'items': _obj(near_m=_numnull, far_m=_numnull),
|
||||
'description': "nearest first; the runtime paints the list last to first. near_m and far_m are the median seam depths over the ring, null at the ends."},
|
||||
tiles={'type': 'array', 'minItems': TILES, 'maxItems': TILES, 'items': {'type': 'array', 'minItems': 1, 'maxItems': MAX_CARDS, 'items': _objnull(top=_int, h=_int, distance_m=_num, colour=_file, geometry=_file)},
|
||||
'description': "tiles[t][i] is card i in tile t, or null where the card holds nothing in that tile (the client takes its distance from the neighbouring tiles). Tile t owns the ring columns [tile_w t, tile_w (t + 1)), column c at azimuth (c + 0.5) / ring_w x 360, 0 = north; its files hold [tile_w t - apron, tile_w (t + 1) + apron), wrapping at the ring's end, and are tile_w + 2 apron columns wide. top and h place the card's band back at its rows of the frame; distance_m = exp(median ln d) of the card's solid pixels in the tile that are not wall (the interior tile_w columns)."},
|
||||
bytes=_int,
|
||||
attribution=_obj(credit_line=_str, licence=_str, dem_source=_str, url=_str)),
|
||||
'index': _obj(schema={'const': 1}, default=_str, places={'type': 'object', 'additionalProperties': _str})}}
|
||||
def tile_file(i, t, kind): return f'card-{i}-t{t:02d}-{kind}.webp'
|
||||
|
||||
def validate(x, s, path='$'):
|
||||
"""The subset of JSON Schema SCHEMA uses; raises ValueError naming the path."""
|
||||
|
|
@ -75,6 +84,18 @@ def validate(x, s, path='$'):
|
|||
elif extra is False: raise ValueError(f'{path}: extra key {k}')
|
||||
elif isinstance(extra, dict): validate(v, extra, f'{path}.{k}')
|
||||
|
||||
def validate_manifest(m):
|
||||
"""The schema plus what it cannot say: every tile lists one entry per card, every card holds something in some tile, each file is named for its card and tile, bytes is the sum of the files."""
|
||||
validate(m, SCHEMA['$defs']['manifest']); k = len(m['cards'])
|
||||
for t, row in enumerate(m['tiles']):
|
||||
if len(row) != k: raise ValueError(f'$.tiles[{t}]: {len(row)} entries for {k} cards')
|
||||
for i, e in enumerate(row):
|
||||
for kind in (('colour', 'geometry') if e else ()):
|
||||
if e[kind]['file'] != tile_file(i, t, kind): raise ValueError(f'$.tiles[{t}][{i}].{kind}: file {e[kind]["file"]!r} is not {tile_file(i, t, kind)!r}')
|
||||
for i in range(k):
|
||||
if all(row[i] is None for row in m['tiles']): raise ValueError(f'card {i} is null in every tile')
|
||||
if m['bytes'] != sum(e[kind]['bytes'] for row in m['tiles'] for e in row if e for kind in ('colour', 'geometry')): raise ValueError('bytes is not the sum of the files')
|
||||
|
||||
# ------------------------------------------------------------------------------------ cards -> bytes
|
||||
def u8(a): return np.clip(np.rint(np.asarray(a) * 255.0), 0, 255).astype(np.uint8)
|
||||
def encode_colour(albedo, alpha):
|
||||
|
|
@ -101,47 +122,92 @@ def check_cover(cards, opaque, tol=COVER_TOL):
|
|||
err = np.abs(composite_alpha(cards, *opaque.shape) - opaque); bad = err > tol
|
||||
if bad.any(): y, x = np.unravel_index(int(np.argmax(err)), err.shape); raise ValueError(f'the cards do not composite to the opaque view within {tol}: {int(bad.sum())} pixels differ, worst {err.max():.3f} at x={x} row {y}')
|
||||
|
||||
def check_budget(card_bytes, w, bands):
|
||||
"""Refuse a bake the runtime could not hold: more than MAX_CARDS cards, a card wider than PLANE_W_MAX, every card file together above TOTAL_BYTES_MAX, or the cards decoding (2 textures x w x band rows x 4 bytes each) above DECODE_BYTES_MAX."""
|
||||
if len(card_bytes) > MAX_CARDS: raise ValueError(f'{len(card_bytes)} cards, above {MAX_CARDS}')
|
||||
if w > PLANE_W_MAX: raise ValueError(f'card width {w} above {PLANE_W_MAX}')
|
||||
if sum(card_bytes) > TOTAL_BYTES_MAX: raise ValueError(f'the cards are {sum(card_bytes)} bytes, above {TOTAL_BYTES_MAX}')
|
||||
decoded = sum(2 * w * h * 4 for h in bands)
|
||||
if decoded > DECODE_BYTES_MAX: raise ValueError(f'the cards decode to {decoded} bytes, above {DECODE_BYTES_MAX}')
|
||||
def run_tiles(r_max_m, rmin_m):
|
||||
"""N, the consecutive tiles the loader keeps resident: the viewport arc at the smallest slide rate plus the offset's own swing (arc_max = HFOV_MAX_DEG / s_min + 2 (r_max / Rmin) 57.3 degrees, s_min = Rmin / (Rmin + r_max)), one straddled tile and two tiles of margin each side."""
|
||||
s_min = rmin_m / (rmin_m + r_max_m); arc = HFOV_MAX_DEG / s_min + 2 * (r_max_m / rmin_m) * 57.3
|
||||
return min(TILES, math.ceil(arc / TILE_DEG) + 1 + 4)
|
||||
def resident_bytes(bands, n):
|
||||
"""The lit canvases (4 bytes x LIT_W x band rows, per card) of the worst run of n consecutive tiles of the ring, wrapping. bands[t]: the row counts of the cards that tile holds."""
|
||||
per = [sum(4 * LIT_W * h for h in b) for b in bands]; n = min(n, TILES)
|
||||
return max(sum(per[(t + k) % TILES] for k in range(n)) for t in range(TILES))
|
||||
def check_budget(n_cards, tile_bytes, bands, r_max_m, rmin_m):
|
||||
"""Refuse a bake the runtime could not hold: more than MAX_CARDS cards, a tile whose files together are above TILE_BYTES_MAX, every file of the place above PLACE_BYTES_MAX, or the worst run of run_tiles(r_max_m, rmin_m) consecutive tiles lit above RESIDENT_BYTES_MAX."""
|
||||
if n_cards > MAX_CARDS: raise ValueError(f'{n_cards} cards, above {MAX_CARDS}')
|
||||
for t, b in enumerate(tile_bytes):
|
||||
if b > TILE_BYTES_MAX: raise ValueError(f'tile {t} is {b} bytes, above {TILE_BYTES_MAX}')
|
||||
if sum(tile_bytes) > PLACE_BYTES_MAX: raise ValueError(f'the place is {sum(tile_bytes)} bytes, above {PLACE_BYTES_MAX}')
|
||||
n = run_tiles(r_max_m, rmin_m); res = resident_bytes(bands, n)
|
||||
if res > RESIDENT_BYTES_MAX: raise ValueError(f'{n} consecutive tiles light to {res} bytes, above {RESIDENT_BYTES_MAX}')
|
||||
|
||||
def card_scales(distances, r_m):
|
||||
"""scale_i = d_i / (d_i - r) for the cards' distances (m), nearest first: the card's pan rate per unit of heading and its static size seen from a camera r metres along its heading (ruling ridge-camera-on-circle; + forward, - backward). Refuses |r| >= R_FRAC_MAX x the nearest distance."""
|
||||
if abs(r_m) >= R_FRAC_MAX * min(distances): raise ValueError(f"the camera offset {r_m:g} m is not under {R_FRAC_MAX} x the nearest card's distance {min(distances):g} m")
|
||||
return [d / (d - r_m) for d in distances]
|
||||
def check_camera(scales, tops, bands, view):
|
||||
"""Refuse a camera whose scaled cards leave the frame uncovered: the nearest card, scaled about the horizon row, must still reach the frame bottom (a backward camera shrinks it away), and no card may be smaller than REST_SCALE_MIN (a 16:9 viewport's edge)."""
|
||||
hr = view['horizon_row']; bottom = hr + (tops[0] + bands[0] - hr) * scales[0]
|
||||
if bottom < view['plane_h'] - 1e-6: raise ValueError(f"the camera lifts the nearest card to row {bottom:.1f}, off the frame bottom (row {view['plane_h']})")
|
||||
if min(scales) < REST_SCALE_MIN: raise ValueError(f'a card scaled {min(scales):.3f} cannot cover a 16:9 viewport at rest (below {REST_SCALE_MIN:.3f})')
|
||||
def fit_px(d, rs, r, px_per_deg):
|
||||
"""The worst error, in card pixels, of the card's linear slide at offset r (m): over the camera's angle phi on its circle against its heading (PHI_STEPS) and the viewport (beta in +-HFOV_MAX_DEG / 2), the exact view angle of a cylinder point at distance R,
|
||||
alpha = atan2(R sin beta - r sin phi, R cos beta - r cos phi), against the least-squares line a + s beta fitted to it for the tile's distance_m d; R runs over rs, the card's per-column median distances in the tile, so a card whose distance varies inside the tile counts."""
|
||||
b = np.radians(np.linspace(-HFOV_MAX_DEG / 2, HFOV_MAX_DEG / 2, 33))[None, None, :]; p = np.linspace(0, 2 * math.pi, PHI_STEPS, endpoint=False)[None, :, None]
|
||||
ang = lambda R: np.arctan2(R * np.sin(b) - r * np.sin(p), R * np.cos(b) - r * np.cos(p)); bc = b - b.mean(); A = ang(np.float64(d)); s = (A * bc).sum(-1, keepdims=True) / (bc * bc).sum(); line = A.mean(-1, keepdims=True) + s * bc
|
||||
return math.degrees(float(np.abs(ang(np.asarray(rs, np.float64)[:, None, None]) - line).max())) * px_per_deg
|
||||
def fit_table(dist, cols, r, px_per_deg):
|
||||
"""(tiles, cards) fit_px of every card tile at offset r; nan where the card is null in that tile."""
|
||||
return np.array([[np.nan if d is None else fit_px(d, cols[t][i], r, px_per_deg) for i, d in enumerate(row)] for t, row in enumerate(dist)])
|
||||
def solve_r_max(dist, cols, bands, px_per_deg):
|
||||
"""The largest offset r (m, down to 0.1 m) at which every card tile fits within FIT_PX_MAX and the resident gate holds: never above half the nearest card's smallest tile distance. Scans r upward from 0 on 128 steps to the first failure, then bisects that step.
|
||||
Refuses (naming the card and tile that bind) a bake whose r_max_m is below R_MAX_FLOOR_M."""
|
||||
near = min((row[0], t) for t, row in enumerate(dist) if row[0] is not None); rmin = near[0]; cap = 0.5 * rmin
|
||||
ok = lambda r: bool(np.nanmax(fit_table(dist, cols, r, px_per_deg)) <= FIT_PX_MAX) and resident_bytes(bands, run_tiles(r, rmin)) <= RESIDENT_BYTES_MAX
|
||||
lo, hi = 0.0, None
|
||||
for r in np.linspace(0, cap, 129)[1:]:
|
||||
if ok(r): lo = float(r)
|
||||
else: hi = float(r); break
|
||||
if hi is None: lo = cap
|
||||
else:
|
||||
for _ in range(24):
|
||||
mid = 0.5 * (lo + hi)
|
||||
if ok(mid): lo = mid
|
||||
else: hi = mid
|
||||
r_max = math.floor(lo * 10) / 10
|
||||
if r_max < R_MAX_FLOOR_M:
|
||||
if cap < R_MAX_FLOOR_M: raise ValueError(f'r_max_m {r_max:g} is below {R_MAX_FLOOR_M:g} m: card 0 stands {rmin:.0f} m away in tile {near[1]} and the offset stays under half of that')
|
||||
tb = fit_table(dist, cols, R_MAX_FLOOR_M, px_per_deg); t, i = np.unravel_index(int(np.nanargmax(tb)), tb.shape)
|
||||
if tb[t, i] > FIT_PX_MAX: raise ValueError(f'r_max_m {r_max:g} is below {R_MAX_FLOOR_M:g} m: card {i} in tile {t} errs {tb[t, i]:.1f} card px at {R_MAX_FLOOR_M:g} m (the bound is {FIT_PX_MAX:g})')
|
||||
n = run_tiles(R_MAX_FLOOR_M, rmin); raise ValueError(f'r_max_m {r_max:g} is below {R_MAX_FLOOR_M:g} m: at {R_MAX_FLOOR_M:g} m, {n} consecutive tiles light to {resident_bytes(bands, n)} bytes, above {RESIDENT_BYTES_MAX}')
|
||||
return r_max
|
||||
|
||||
def bake_id(cards):
|
||||
"""First 12 hex of sha256 over the card files in index order, colour then geometry. cards: [(colour bytes, geometry bytes)]."""
|
||||
def bake_id(files):
|
||||
"""First 12 hex of sha256 over the tile files, tile-major then card, colour then geometry (a null card tile writes none). files: [(colour bytes, geometry bytes)] in that order."""
|
||||
h = hashlib.sha256()
|
||||
for colour, geometry in cards: h.update(colour); h.update(geometry)
|
||||
for colour, geometry in files: h.update(colour); h.update(geometry)
|
||||
return h.hexdigest()[:12]
|
||||
|
||||
def write_bake(out, place, view, meta, cards, opaque, default=None):
|
||||
"""Write out/<place>-<bake_id>/ (card WebPs + manifest.json), rewrite out/index.json, delete the place's previous dir. cards: [dict(top, colour bytes, geometry bytes, near_m, far_m, median_m, distance_m, scale)], nearest first; opaque (h, w) is
|
||||
the opaque view's coverage. Refuses before writing a bake whose files decode to different sizes, leave the frame or the view's plane_h, break the budget or composite away from the opaque view. meta: dict(curvature, encoding, camera dict(r_m, heading_max_deg)). Returns the directory."""
|
||||
dec = [(decode(c['colour']), decode(c['geometry'])) for c in cards]; shapes = {a.shape[:2] for pair in dec for a in pair}; bands = [a.shape[0] for a, _ in dec]
|
||||
if any(c.shape[:2] != g.shape[:2] for c, g in dec): raise ValueError('a card colour and geometry are not one size')
|
||||
if len({a.shape[1] for pair in dec for a in pair}) != 1: raise ValueError(f'the cards are not one width: {sorted(shapes)}')
|
||||
w = dec[0][0].shape[1]; h = view['plane_h']
|
||||
if any(band < 1 or c['top'] < 0 or c['top'] + band > h for c, band in zip(cards, bands)): raise ValueError(f'a card leaves the {h} rows of the frame')
|
||||
if opaque.shape != (h, w): raise ValueError(f'the opaque view is {opaque.shape}, the frame {(h, w)}')
|
||||
sizes = [len(c['colour']) + len(c['geometry']) for c in cards]; check_budget(sizes, w, bands)
|
||||
check_cover([(c['top'], col[..., 3] / 255.0) for c, (col, _) in zip(cards, dec)], opaque); check_camera([c['scale'] for c in cards], [c['top'] for c in cards], bands, view)
|
||||
bid = bake_id([(c['colour'], c['geometry']) for c in cards]); name = f"{place['id']}-{bid}"; out = Path(out); dest = out / name
|
||||
manifest = dict(schema=2, baker_version=BAKER_VERSION, bake_id=bid, place=place, view=dict(view, window_deg=WINDOW_DEG, plane_w=w), curvature=meta['curvature'], encoding=dict(meta['encoding'], normal='rt8'), camera=dict(r_m=meta['camera']['r_m'], heading_max_deg=meta['camera']['heading_max_deg']),
|
||||
cards=[dict(top=c['top'], h=band, near_m=c['near_m'], far_m=c['far_m'], median_m=c['median_m'], distance_m=c['distance_m'], scale=c['scale'], colour=dict(file=f'card-{i}-colour.webp', bytes=len(c['colour'])), geometry=dict(file=f'card-{i}-geometry.webp', bytes=len(c['geometry'])))
|
||||
for i, (c, band) in enumerate(zip(cards, bands))], w=w, h=h, bytes=sum(sizes), attribution=ATTRIBUTION)
|
||||
validate(manifest, SCHEMA['$defs']['manifest']); out.mkdir(parents=True, exist_ok=True); tmp = Path(tempfile.mkdtemp(dir=out, prefix='.bake-'))
|
||||
for i, c in enumerate(cards): (tmp / f'card-{i}-colour.webp').write_bytes(c['colour']); (tmp / f'card-{i}-geometry.webp').write_bytes(c['geometry'])
|
||||
def write_bake(out, place, view, meta, packed, opaque, default=None):
|
||||
"""Write out/<place>-<bake_id>/ (tile WebPs + manifest.json), rewrite out/index.json, delete the place's previous dir. packed: dict(cards [dict(near_m, far_m)], tiles [TILES][cards] of dict(top, colour bytes, geometry bytes, distance_m) or None,
|
||||
camera dict(r_max_m)); opaque: TILES arrays (plane_h, TILE_W + 2 APRON), each the opaque view's coverage over a tile's columns. Refuses before writing a bake whose files decode to the wrong size, leave the frame, break a budget (the resident gate at r_max_m) or composite
|
||||
away from the opaque view in any tile. meta: dict(curvature, encoding). Returns the directory."""
|
||||
cards, tiles, camera = packed['cards'], packed['tiles'], packed['camera']; k = len(cards); h = view['plane_h']; w = TILE_W + 2 * APRON
|
||||
if len(tiles) != TILES or len(opaque) != TILES or any(len(row) != k for row in tiles): raise ValueError(f'the bake needs {TILES} tiles of {k} cards each')
|
||||
for i in range(k):
|
||||
if all(row[i] is None for row in tiles): raise ValueError(f'card {i} is null in every tile')
|
||||
dec = [[None if e is None else (decode(e['colour']), decode(e['geometry'])) for e in row] for row in tiles]; bands = [[pair[0].shape[0] for pair in row if pair] for row in dec]; tile_bytes = []
|
||||
for t, (row, drow) in enumerate(zip(tiles, dec)):
|
||||
tile_bytes.append(sum(len(e['colour']) + len(e['geometry']) for e in row if e))
|
||||
for i, (e, pair) in enumerate(zip(row, drow)):
|
||||
if e is None: continue
|
||||
c, g = pair
|
||||
if c.shape[:2] != g.shape[:2]: raise ValueError(f'tile {t} card {i}: colour and geometry are not one size')
|
||||
if c.shape[1] != w: raise ValueError(f'tile {t} card {i} is {c.shape[1]} columns wide, not {w} (tile {TILE_W} + 2 apron {APRON})')
|
||||
if c.shape[0] < 1 or e['top'] < 0 or e['top'] + c.shape[0] > h: raise ValueError(f'tile {t} card {i} leaves the {h} rows of the frame')
|
||||
if opaque[t].shape != (h, w): raise ValueError(f'tile {t}: the opaque view is {opaque[t].shape}, the frame {(h, w)}')
|
||||
rmin = min(row[0]['distance_m'] for row in tiles if row[0]); check_budget(k, tile_bytes, bands, camera['r_max_m'], rmin)
|
||||
for t, (row, drow) in enumerate(zip(tiles, dec)):
|
||||
try: check_cover([(e['top'], pair[0][..., 3] / 255.0) for e, pair in zip(row, drow) if e], opaque[t])
|
||||
except ValueError as ex: raise ValueError(f'tile {t}: {ex}')
|
||||
bid = bake_id([(e['colour'], e['geometry']) for row in tiles for e in row if e]); name = f"{place['id']}-{bid}"; out = Path(out); dest = out / name
|
||||
manifest = dict(schema=3, baker_version=BAKER_VERSION, bake_id=bid, place=place, view=dict(view, ring_w=RING_W, tile_w=TILE_W, tiles=TILES, apron=APRON), curvature=meta['curvature'], encoding=dict(meta['encoding'], normal='rt8'), camera=dict(r_max_m=camera['r_max_m']),
|
||||
cards=[dict(near_m=c['near_m'], far_m=c['far_m']) for c in cards],
|
||||
tiles=[[None if e is None else dict(top=e['top'], h=pair[0].shape[0], distance_m=e['distance_m'], colour=dict(file=tile_file(i, t, 'colour'), bytes=len(e['colour'])), geometry=dict(file=tile_file(i, t, 'geometry'), bytes=len(e['geometry'])))
|
||||
for i, (e, pair) in enumerate(zip(row, drow))] for t, (row, drow) in enumerate(zip(tiles, dec))], bytes=sum(tile_bytes), attribution=ATTRIBUTION)
|
||||
validate_manifest(manifest); out.mkdir(parents=True, exist_ok=True); tmp = Path(tempfile.mkdtemp(dir=out, prefix='.bake-'))
|
||||
for t, row in enumerate(tiles):
|
||||
for i, e in enumerate(row):
|
||||
if e: (tmp / tile_file(i, t, 'colour')).write_bytes(e['colour']); (tmp / tile_file(i, t, 'geometry')).write_bytes(e['geometry'])
|
||||
(tmp / 'manifest.json').write_text(json.dumps(manifest, indent=1) + '\n')
|
||||
idx_path = out / 'index.json'; idx = json.loads(idx_path.read_text()) if idx_path.exists() else dict(schema=1, default=place['id'], places={})
|
||||
old = idx['places'].get(place['id']); idx['places'][place['id']] = name; idx['default'] = default or (idx['default'] if idx['default'] in idx['places'] else place['id'])
|
||||
|
|
@ -152,33 +218,46 @@ def write_bake(out, place, view, meta, cards, opaque, default=None):
|
|||
return dest
|
||||
|
||||
# ------------------------------------------------------------------------------------ the bake
|
||||
def bake_place(pid):
|
||||
"""stage 0, prefilter, set_view, coverage, seams, cards: dict(place, view, meta, cards (ridge.cards arrays), seams (K, w) in m, cov (bins, w), opaque (h, w) coverage). Needs RIDGE_TILES / RIDGE_WORK."""
|
||||
def cut_ring(d, a, h, splits_km):
|
||||
"""The ring cut of a prefiltered march (set_view done): normals, coverage, the periodic seams on all RING_W columns, then ridge.cards per tile on its TILE_W + 2 APRON columns with the matching normals. Returns dict(cards [TILES][cards] (ridge.cards arrays),
|
||||
splits_km, seams (K, ring) in m, cov (bins, ring), opaque [TILES] (rows, TILE_W + 2 APRON) coverage, edges)."""
|
||||
import ridge
|
||||
assert ridge.WIN == PLANE_W and ridge.H > 0
|
||||
p = ridge.configure(ridge.load_preset(pid)); ridge.mosaic(p); d, _, Hh, _ = ridge.stage0(False); a, h = ridge.prefilter(d, Hh, p.get('prefilter_m', 0)); v = ridge.set_view(a.max(0))
|
||||
nrm = tuple(ridge.win(x) for x in ridge.normals(d, h)); aw, hw = ridge.win(a), ridge.win(h); cov = ridge.coverage(d, aw); sm = ridge.seams(cov, [k * 1000.0 for k in p['card_split_km']], d)
|
||||
nrm = ridge.normals(d, h); cov = ridge.coverage(d, a); sm = ridge.seams(cov, [k * 1000.0 for k in splits_km], d); op = ridge.opaque(a); cols = lambda x, t: ridge.ring_cols(x, t * TILE_W - APRON, TILE_W + 2 * APRON); tiles = []
|
||||
for t in range(TILES): tiles.append(ridge.cards(d, cols(a, t), cols(h, t), cols(sm, t), tuple(cols(x, t) for x in nrm))); print(f'tile {t + 1}/{TILES}', file=sys.stderr, flush=True)
|
||||
return dict(cards=tiles, splits_km=list(splits_km), seams=sm, cov=cov, opaque=[cols(op, t) for t in range(TILES)], edges=ridge.log_edges(d))
|
||||
|
||||
def bake_place(pid):
|
||||
"""stage 0, prefilter, set_view on the whole ring, cut_ring: dict(place, view, meta, v, and cut_ring's cards, splits_km, seams, cov, opaque, edges). Needs RIDGE_TILES / RIDGE_WORK."""
|
||||
import ridge
|
||||
assert ridge.W == RING_W and ridge.H > 0
|
||||
p = ridge.configure(ridge.load_preset(pid)); ridge.mosaic(p); d, _, Hh, _ = ridge.stage0(False); a, h = ridge.prefilter(d, Hh, p.get('prefilter_m', 0)); v = ridge.set_view(a.max(0)); ring = cut_ring(d, a, h, p['card_split_km'])
|
||||
place = dict(id=p['id'], name=p['name'], lat=p['lat'], lon=p['lon'], altitude_km=p['ground_m'] / 1000.0, eye_m=p['eye_m'], tz=p['tz'])
|
||||
view = dict(look_az_deg=p['az_center'], plane_h=ridge.H, px_per_deg_h=ridge.HPX_DEG, px_per_deg_v=v['px_per_deg'], exaggeration=v['exaggeration'], horizon_row=v['horizon_row'], sky_row=v['sky_row'] * ridge.H, near_row=v['near_row'] * ridge.H)
|
||||
meta = dict(curvature=dict(r_earth_m=ridge.R_EARTH, k_ref=ridge.KREF), encoding=dict(log_d_min=math.log(d[0]), log_d_max=math.log(d[-1])), camera=dict(r_m=float(p.get('camera_r_m', CAMERA_R_M)), heading_max_deg=HEADING_MAX_DEG))
|
||||
return dict(place=place, view=view, meta=meta, cards=ridge.cards(d, aw, hw, sm, nrm), splits_km=list(p['card_split_km']), seams=sm, cov=cov, opaque=ridge.opaque(aw), edges=ridge.log_edges(d))
|
||||
meta = dict(curvature=dict(r_earth_m=ridge.R_EARTH, k_ref=ridge.KREF), encoding=dict(log_d_min=math.log(d[0]), log_d_max=math.log(d[-1])))
|
||||
return dict(place=place, view=view, meta=meta, v=v, **ring)
|
||||
|
||||
def pack(bake):
|
||||
"""The cards of a bake as bytes plus their manifest facts: median seam depths either side, the median distance of the card's solid pixels that are not wall (median_m = distance_m), and scale = distance_m / (distance_m - camera r). Refuses a card
|
||||
with no solid terrain apart from its wall rows, and a camera offset the cards cannot take (card_scales)."""
|
||||
enc = bake['meta']['encoding']; sm = bake['seams']; out = []; sp = bake['splits_km']; cs = bake['cards']
|
||||
for i, c in enumerate(cs):
|
||||
if c['alpha'].shape[0] == 0: raise ValueError(f"card {i} is empty: it lies between the splits {' and '.join(f'{sp[j]:g} km' for j in (i - 1, i) if 0 <= j < len(sp))} (card_split_km {sp}) and holds no terrain nothing nearer hides; move or remove a split")
|
||||
dist = []
|
||||
for i, c in enumerate(cs):
|
||||
ground = (c['alpha'] >= 0.5) & ~c['wall']
|
||||
if not ground.any(): raise ValueError(f'card {i} holds no solid terrain apart from its wall rows')
|
||||
dist.append(float(np.exp(np.median(c['logd'][ground]))))
|
||||
scales = card_scales(dist, bake['meta']['camera']['r_m'])
|
||||
for i, c in enumerate(cs):
|
||||
out.append(dict(top=c['top'], colour=encode_colour(c['albedo'], c['alpha']), geometry=encode_geometry(c['n_r'], c['n_t'], c['logd'], enc, c['wall']),
|
||||
near_m=float(np.median(sm[i - 1])) if i else None, far_m=float(np.median(sm[i])) if i < len(sm) else None, median_m=dist[i], distance_m=dist[i], scale=scales[i]))
|
||||
return out
|
||||
"""The tiles of a bake as bytes plus their manifest facts. Per card tile: distance_m = exp(median ln d) over the solid (alpha >= 0.5) pixels that are not wall of the interior TILE_W columns (a tile with none falls back to its non-wall, then any, lit pixels);
|
||||
a card with no rows in a tile is null there. Per card: the median seam depths over the ring. camera.r_max_m: solve_r_max over the card tiles' distances and per-column median distances. Refuses a card empty in every tile (naming the splits) and a card with
|
||||
no solid terrain apart from its wall in any tile. Returns dict(cards, tiles, camera, report) (report is printed, not written)."""
|
||||
sm = bake['seams']; sp = bake['splits_km']; k = len(sp) + 1; ppd = bake['view']['px_per_deg_h']; inner = slice(APRON, APRON + TILE_W); enc = bake['meta']['encoding']
|
||||
for i in range(k):
|
||||
if all(tile[i]['alpha'].shape[0] == 0 for tile in bake['cards']): raise ValueError(f"card {i} is empty: it lies between the splits {' and '.join(f'{sp[j]:g} km' for j in (i - 1, i) if 0 <= j < len(sp))} (card_split_km {sp}) and holds no terrain nothing nearer hides; move or remove a split")
|
||||
tiles, dist, cols, bands = [], [], [], []; any_solid = [False] * k
|
||||
for tile in bake['cards']:
|
||||
row, drow, crow, brow = [], [], [], []
|
||||
for i, c in enumerate(tile):
|
||||
if c['alpha'].shape[0] == 0: row.append(None); drow.append(None); crow.append(None); continue
|
||||
al, wl, ld = c['alpha'][:, inner], c['wall'][:, inner], c['logd'][:, inner]; solid = (al >= 0.5) & ~wl; any_solid[i] |= bool(solid.any())
|
||||
pool = next((m, l) for m, l in ((solid, ld), ((al > 0) & ~wl, ld), (al > 0, ld), (c['alpha'] > 0, c['logd'])) if m.any())
|
||||
dm = float(np.exp(np.median(pool[1][pool[0]]))); rc = [float(np.exp(np.median(ld[:, x][solid[:, x]]))) for x in range(TILE_W) if solid[:, x].any()]
|
||||
row.append(dict(top=c['top'], colour=encode_colour(c['albedo'], c['alpha']), geometry=encode_geometry(c['n_r'], c['n_t'], c['logd'], enc, c['wall']), distance_m=dm)); drow.append(dm); crow.append(np.array(rc if rc else [dm])); brow.append(c['alpha'].shape[0])
|
||||
tiles.append(row); dist.append(drow); cols.append(crow); bands.append(brow)
|
||||
for i in range(k):
|
||||
if not any_solid[i]: raise ValueError(f'card {i} holds no solid terrain apart from its wall rows')
|
||||
r_max = solve_r_max(dist, cols, bands, ppd); rmin = min(row[0] for row in dist if row[0] is not None); n = run_tiles(r_max, rmin); worst = np.nanmax(fit_table(dist, cols, r_max, ppd), axis=0)
|
||||
cards = [dict(near_m=float(np.median(sm[i - 1])) if i else None, far_m=float(np.median(sm[i])) if i < len(sm) else None) for i in range(k)]
|
||||
return dict(cards=cards, tiles=tiles, camera=dict(r_max_m=r_max), report=dict(fit_px=[float(x) for x in worst], run_tiles=n, resident_bytes=resident_bytes(bands, n), nearest_m=rmin))
|
||||
|
||||
def lit(c):
|
||||
"""albedo x (0.35 + 0.65 max(n.L, 0)) under the fixed sheet light, with the card's alpha: (rgb, alpha). Wall pixels are lit with the face up, as the runtime reads their code."""
|
||||
|
|
@ -186,16 +265,23 @@ def lit(c):
|
|||
up = np.sqrt(np.clip(1 - n_r ** 2 - n_t ** 2, 0, 1)); L = np.array(SHEET_LIGHT) / np.linalg.norm(SHEET_LIGHT); nl = np.clip(n_r * L[0] + n_t * L[1] + up * L[2], 0, 1)
|
||||
return c['albedo'] * (0.35 + 0.65 * nl)[..., None], c['alpha']
|
||||
def sheet(bake):
|
||||
"""Contact sheet (png bytes), stacked: the composite, every card alone, and the depth image (log-distance bins, far at the top, every seam drawn). All lit by SHEET_LIGHT over the flat SHEET_SKY; reads the arrays the encoder writes."""
|
||||
h, w = bake['opaque'].shape; sky = np.broadcast_to(np.array(SHEET_SKY), (h, w, 3)); comp = sky.copy(); panels = []
|
||||
for c in bake['cards']:
|
||||
rgb, al = lit(c); alone = sky.copy(); sl = slice(c['top'], c['top'] + al.shape[0]); alone[sl] = sky[sl] * (1 - al[..., None]) + rgb * al[..., None]; panels.append(alone)
|
||||
for c in reversed(bake['cards']): rgb, al = lit(c); sl = slice(c['top'], c['top'] + al.shape[0]); comp[sl] = comp[sl] * (1 - al[..., None]) + rgb * al[..., None]
|
||||
cov = bake['cov']; img = np.repeat(np.log1p(cov[::-1]) / np.log1p(cov.max()), 2, 0); depth = np.dstack([img] * 3) * 0.92 + 0.04; nb = cov.shape[0]; cols = np.arange(w)
|
||||
for k, s in enumerate(bake['seams']):
|
||||
j = np.clip(np.searchsorted(bake['edges'], np.log(s), 'right') - 1, 0, nb - 1); col = [(0.24, 0.78, 1.0), (1.0, 0.47, 0.24), (0.6, 1.0, 0.4), (1.0, 0.9, 0.3), (0.9, 0.4, 1.0)][k % 5]
|
||||
for dy in (0, 1): depth[(nb - 1 - j) * 2 + dy, cols] = col
|
||||
return bytes(imagecodecs.png_encode(u8(np.concatenate([comp] + panels + [depth], 0))))
|
||||
"""Contact sheet (png bytes) at 1/4 of the ring's width, stacked: the ring composite, every card's strip alone, and the depth image (log-distance bins, far at the top, every seam); the TILES tile boundaries drawn on each. All lit by SHEET_LIGHT over the flat
|
||||
SHEET_SKY; reads the arrays the encoder writes (each tile's interior columns, the apron dropped)."""
|
||||
h = bake['opaque'][0].shape[0]; k = len(bake['cards'][0]); sky = np.broadcast_to(np.array(SHEET_SKY, np.float32), (h, RING_W, 3)); rgbs = [np.zeros((h, RING_W, 3), np.float32) for _ in range(k)]; als = [np.zeros((h, RING_W), np.float32) for _ in range(k)]
|
||||
for t, tile in enumerate(bake['cards']):
|
||||
for i, c in enumerate(tile):
|
||||
if c['alpha'].shape[0] == 0: continue
|
||||
rgb, al = lit(c); sl = slice(c['top'], c['top'] + al.shape[0]); x0 = t * TILE_W; rgbs[i][sl, x0:x0 + TILE_W] = rgb[:, APRON:APRON + TILE_W]; als[i][sl, x0:x0 + TILE_W] = al[:, APRON:APRON + TILE_W]
|
||||
comp = sky.copy(); panels = []
|
||||
for rgb, al in zip(rgbs, als): panels.append(sky * (1 - al[..., None]) + rgb * al[..., None])
|
||||
for rgb, al in zip(reversed(rgbs), reversed(als)): comp = comp * (1 - al[..., None]) + rgb * al[..., None]
|
||||
q = lambda a: a.reshape((a.shape[0], RING_W // 4, 4) + a.shape[2:]).mean(2); mark = np.arange(TILES) * TILE_W // 4; red = np.array([1.0, 0.2, 0.2])
|
||||
def bounds(img): img[:, mark] = 0.45 * img[:, mark] + 0.55 * red; return img
|
||||
cov = bake['cov'][:, ::4]; w = cov.shape[1]; img = np.repeat(np.log1p(cov[::-1]) / np.log1p(cov.max()), 2, 0); depth = np.dstack([img] * 3) * 0.92 + 0.04; nb = cov.shape[0]; cols = np.arange(w)
|
||||
for j, s in enumerate(bake['seams']):
|
||||
b = np.clip(np.searchsorted(bake['edges'], np.log(s[::4]), 'right') - 1, 0, nb - 1); col = [(0.24, 0.78, 1.0), (1.0, 0.47, 0.24), (0.6, 1.0, 0.4), (1.0, 0.9, 0.3), (0.9, 0.4, 1.0)][j % 5]
|
||||
for dy in (0, 1): depth[(nb - 1 - b) * 2 + dy, cols] = col
|
||||
return bytes(imagecodecs.png_encode(u8(np.concatenate([bounds(q(comp))] + [bounds(q(p)) for p in panels] + [bounds(depth)], 0))))
|
||||
|
||||
def main(argv=None):
|
||||
ap = argparse.ArgumentParser(description=__doc__.split('\n')[0]); ap.add_argument('place', nargs='?'); ap.add_argument('--tiles'); ap.add_argument('--out'); ap.add_argument('--work'); ap.add_argument('--sheet'); ap.add_argument('--write-schema', action='store_true')
|
||||
|
|
@ -205,7 +291,8 @@ def main(argv=None):
|
|||
os.environ['RIDGE_TILES'] = a.tiles; os.environ['RIDGE_WORK'] = a.work or os.path.join(os.environ.get('TMPDIR', tempfile.gettempdir()), 'ridge-bake-work'); os.makedirs(os.environ['RIDGE_WORK'], exist_ok=True)
|
||||
bake = bake_place(a.place)
|
||||
if a.sheet: Path(a.sheet).write_bytes(sheet(bake))
|
||||
dest = write_bake(a.out, bake['place'], bake['view'], bake['meta'], pack(bake), bake['opaque'])
|
||||
print(dest); return 0
|
||||
packed = pack(bake); rp = packed['report']
|
||||
print(f"r_max_m {packed['camera']['r_max_m']:g} nearest card {rp['nearest_m']:.0f} m resident run {rp['run_tiles']} tiles = {rp['resident_bytes']} bytes worst fit error per card (card px, bound {FIT_PX_MAX:g}) {[round(x, 1) for x in rp['fit_px']]}", file=sys.stderr, flush=True)
|
||||
dest = write_bake(a.out, bake['place'], bake['view'], bake['meta'], packed, bake['opaque']); print(dest); return 0
|
||||
|
||||
if __name__ == '__main__': sys.exit(main())
|
||||
|
|
|
|||
Loading…
Reference in a new issue