Files
openclaw-dnd-city-visualiza…/scripts/scene_solids.py
T
rick 3d50023cfa 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.
2026-10-14 11:39:57 +00:00

239 lines
9.9 KiB
Python

"""Analytic convex solids for hand-modeled landmarks and true-scale proxies (people, carts, horses, trees).
Everything is a convex polyhedron (half-space planes) or a sphere, so a ray hit is one interval test.
A scene file (JSON) lists solids in world meters; see examples/scene.example.json. Kinds:
box {c:[x,y,z], size:[sx,sy,sz], heading?:deg, mat, group}
prism {poly:[[x,y],...], z0, z1, mat, group} (vertical extrusion of a convex polygon)
cone {c:[x,y], r0, z0, z1, mat, group} (round tower roof; faceted)
gable {p0:[x,y], p1:[x,y], width, z_eave, rise, mat_wall, mat_roof, group} (house: walls + roof prism)
person {xy:[x,y], z0?, height?:1.72, heading?:deg, robe?:bool, cloth?:int, label?}
cart {xy, z0?, heading, load?:hay|sacks}
horse {xy, z0?, heading, pack?:bool}
tree {xy, z0?, h?:9, r?:3.2, conifer?:bool}
Proxy people are the point of the method: they are the scale anchors the re-surfacing model dresses in place.
"""
import numpy as np
MAT = dict(
stone=(176, 172, 164), stone_dk=(150, 146, 138), stone_lt=(194, 190, 182), slate=(82, 86, 94), lead=(116, 120, 126),
timber=(146, 126, 102), timber_dk=(118, 100, 80), plank=(156, 136, 108), shingle=(96, 84, 72), turf=(98, 110, 72),
oak=(78, 58, 42), skin=(198, 160, 134), legs=(70, 62, 54), leaf=(74, 98, 60), trunk=(84, 68, 54), wheel=(92, 74, 56),
canvas=(170, 160, 136), horse=(104, 78, 58), horse_dk=(70, 54, 42), hay=(176, 156, 98), sack=(160, 146, 118),
iron=(70, 70, 72),
)
CLOTH = [(92, 70, 52), (118, 54, 44), (56, 66, 92), (96, 98, 92), (70, 86, 60), (150, 138, 112), (60, 52, 48),
(128, 98, 64), (84, 60, 78), (110, 112, 118), (140, 120, 90), (74, 80, 96)]
for _i, _c in enumerate(CLOTH):
MAT["cloth%d" % _i] = _c
SOLIDS = []
GROUPS = {}
PEOPLE = []
def reset():
SOLIDS.clear()
GROUPS.clear()
PEOPLE.clear()
def add(planes, pts, mat, group):
oid = len(SOLIDS)
p = np.asarray(pts, float)
SOLIDS.append(dict(kind="convex", planes=np.asarray(planes, float), lo=p.min(0) - 0.05, hi=p.max(0) + 0.05,
mat=mat, oid=oid, group=group))
GROUPS.setdefault(group, []).append(oid)
return oid
def add_sphere(c, r, mat, group):
oid = len(SOLIDS)
c = np.asarray(c, float)
SOLIDS.append(dict(kind="sphere", c=c, r=float(r), lo=c - r, hi=c + r, mat=mat, oid=oid, group=group))
GROUPS.setdefault(group, []).append(oid)
return oid
def _planes_box(c, ax, hx):
P = []
for a, h in zip(ax, hx):
a = np.asarray(a, float)
P.append(np.r_[a, a @ c + h])
P.append(np.r_[-a, -(a @ c) + h])
return np.array(P)
def box_pts(c, ax, hx):
c = np.asarray(c, float)
ax = np.asarray(ax, float)
return np.array([c + sx * hx[0] * ax[0] + sy * hx[1] * ax[1] + sz * hx[2] * ax[2]
for sx in (-1, 1) for sy in (-1, 1) for sz in (-1, 1)])
def add_box(c, ax, hx, mat, group):
c = np.asarray(c, float)
return add(_planes_box(c, ax, hx), box_pts(c, ax, hx), mat, group)
def axes(heading_deg):
b = np.radians(heading_deg)
f = np.array([np.sin(b), np.cos(b), 0.0])
s = np.array([f[1], -f[0], 0.0])
return s, f, np.array([0, 0, 1.0])
def ccw(poly):
P = np.asarray(poly, float)
a = 0.5 * np.sum(P[:, 0] * np.roll(P[:, 1], -1) - np.roll(P[:, 0], -1) * P[:, 1])
return P if a > 0 else P[::-1]
def prism_planes(poly, z0, z1):
P = ccw(poly)
pl = [[0, 0, -1, -z0], [0, 0, 1, z1]]
for i in range(len(P)):
a, b = P[i], P[(i + 1) % len(P)]
e = b - a
n = np.array([e[1], -e[0], 0.0])
n /= np.linalg.norm(n)
pl.append(np.r_[n, n @ np.r_[a, 0]])
pts = [np.r_[p, z0] for p in P] + [np.r_[p, z1] for p in P]
return np.array(pl), pts
def add_prism(poly, z0, z1, mat, group):
pl, pts = prism_planes(poly, z0, z1)
return add(pl, pts, mat, group)
def add_cone(cx, cy, r0, z0, z1, mat, group, n=24, rtop=0.05):
th = np.linspace(0, 2 * np.pi, n, endpoint=False)
pl = [[0, 0, -1, -z0]]
h = z1 - z0
for t in th:
nx, ny = np.cos(t), np.sin(t)
nn = np.array([nx * h, ny * h, r0 - rtop])
nn /= np.linalg.norm(nn)
pl.append(np.r_[nn, nn @ np.array([cx + nx * r0, cy + ny * r0, z0])])
pts = [[cx + r0 * np.cos(t), cy + r0 * np.sin(t), z0] for t in th] + [[cx, cy, z1]]
return add(np.array(pl), pts, mat, group)
def add_gable(p0, p1, width, z_eave, rise, mat_wall, mat_roof, group, z0=0.0, overhang=0.35):
"""A house: walls as a box under the eaves, a roof prism with its ridge from p0 to p1."""
p0, p1 = np.asarray(p0, float), np.asarray(p1, float)
d = p1 - p0
L = np.linalg.norm(d)
d /= L
nrm = np.array([-d[1], d[0]])
c = (p0 + p1) / 2
ax = [np.r_[d, 0], np.r_[nrm, 0], [0, 0, 1.0]]
add_box(np.r_[c, z0 + (z_eave - z0) / 2], ax, [L / 2, width / 2, (z_eave - z0) / 2], mat_wall, group)
w2 = width / 2 + overhang
pl = [[0, 0, -1, -z_eave], np.r_[ax[0], ax[0] @ np.r_[c, 0] + L / 2 + overhang],
np.r_[-ax[0], -(ax[0] @ np.r_[c, 0]) + L / 2 + overhang]]
for sgn in (1, -1):
nn = np.r_[sgn * nrm * rise, w2]
nn /= np.linalg.norm(nn)
pl.append(np.r_[nn, nn @ np.r_[c + sgn * nrm * w2, z_eave]])
pts = [np.r_[c + a * d * (L / 2 + overhang) + s * nrm * w2, z_eave] for a in (-1, 1) for s in (-1, 1)]
pts += [np.r_[c + a * d * (L / 2 + overhang), z_eave + rise] for a in (-1, 1)]
add(np.array(pl), pts, mat_roof, group)
def person(xy, z0, height=1.72, heading=0.0, robe=False, cloth=0, group="people", stride=0.0, label=""):
"""A mannequin at true scale: legs, torso, head. Recorded in PEOPLE for the figure-scale gate."""
h = float(height)
s, f, z = axes(heading)
ax = [s, f, z]
c0 = np.r_[np.asarray(xy, float)[:2], z0]
col = "cloth%d" % (cloth % len(CLOTH))
k = h / 1.72
leg, tor, hr, sh_w = 0.47 * h, 0.30 * h, 0.062 * h, 0.12 * h
if robe:
zt = leg + tor
hw0, hw1, hd0, hd1 = 0.30 * k, sh_w, 0.20 * k, 0.12 * k
pl = [[0, 0, -1, -z0], [0, 0, 1, z0 + zt]]
for v, w0, w1 in ((s, hw0, hw1), (-s, hw0, hw1), (f, hd0, hd1), (-f, hd0, hd1)):
nn = v * zt + z * (w0 - w1)
nn /= np.linalg.norm(nn)
pl.append(np.r_[nn, nn @ (c0 + v * w0)])
pts = [c0 + a * hw0 + bb * hd0 for a in (s, -s) for bb in (f, -f)] + [c0 + z * zt + a * hw1 for a in (s, -s)]
add(np.array(pl), pts, col, group)
else:
for side in (-1, 1):
add_box(c0 + s * side * 0.075 * k + f * stride * side * 0.5 + z * (leg / 2), ax,
[0.065 * k, 0.08 * k, leg / 2], "legs", group)
add_box(c0 + z * (leg + tor / 2 - 0.02), ax, [sh_w, 0.12 * k, tor / 2 + 0.04], col, group)
for side in (-1, 1):
add_box(c0 + s * side * (sh_w + 0.05 * k) + z * (leg + tor * 0.55), ax, [0.045 * k, 0.05 * k, tor * 0.42], col, group)
add_sphere(c0 + z * (h - hr), hr, "skin", group)
PEOPLE.append(dict(x=float(c0[0]), y=float(c0[1]), z0=float(z0), h=h, label=label or group))
def cart(xy, z0, heading, load="hay", group="carts"):
s, f, z = axes(heading)
ax = [s, f, z]
c0 = np.r_[np.asarray(xy, float)[:2], z0]
add_box(c0 + z * 1.0, ax, [0.75, 1.3, 0.25], "plank", group)
add_box(c0 + z * (1.25 + 0.45), ax, [0.7, 1.2, 0.45], "hay" if load == "hay" else "sack", group)
for side in (-1, 1):
add_box(c0 + s * side * 0.9 + z * 0.65, ax, [0.05, 0.65, 0.65], "wheel", group)
return c0 + f * 1.3
def horse(xy, z0, heading, pack=False, group="animals"):
s, f, z = axes(heading)
ax = [s, f, z]
c0 = np.r_[np.asarray(xy, float)[:2], z0]
add_box(c0 + z * 1.15, ax, [0.3, 0.95, 0.32], "horse", group)
add_box(c0 + f * 1.1 + z * 1.45, ax, [0.15, 0.35, 0.3], "horse_dk", group)
for sx in (-1, 1):
for sy in (-1, 1):
add_box(c0 + s * sx * 0.18 + f * sy * 0.6 + z * 0.42, ax, [0.07, 0.07, 0.42], "horse_dk", group)
if pack:
add_box(c0 + z * 1.65, ax, [0.4, 0.5, 0.2], "sack", group)
def tree(xy, z0, h=9.0, r=3.2, conifer=False, group="trees"):
x, y = float(xy[0]), float(xy[1])
add_box([x, y, z0 + h * 0.2], [[1, 0, 0], [0, 1, 0], [0, 0, 1]], [0.18, 0.18, h * 0.2], "trunk", group)
if conifer:
add_cone(x, y, r, z0 + h * 0.25, z0 + h, "leaf", group, n=12)
else:
add_sphere([x, y, z0 + h * 0.65], r, "leaf", group)
def build_from_json(scene, ground_fn=None):
"""Populate SOLIDS from a scene dict. `ground_fn(x, y) -> z` places proxies on the terrain when z0 is absent."""
def z0_of(e):
if "z0" in e:
return float(e["z0"])
if ground_fn is None:
return 0.0
return float(ground_fn(np.array([e["xy"][0]]), np.array([e["xy"][1]]))[0])
for e in scene.get("solids", []):
k = e["kind"]
g = e.get("group", k)
if k == "box":
s, f, z = axes(e.get("heading", 0.0))
add_box(e["c"], [s, f, z], [v / 2 for v in e["size"]], e.get("mat", "stone"), g)
elif k == "prism":
add_prism(e["poly"], e["z0"], e["z1"], e.get("mat", "stone"), g)
elif k == "cone":
add_cone(e["c"][0], e["c"][1], e["r0"], e["z0"], e["z1"], e.get("mat", "slate"), g)
elif k == "gable":
add_gable(e["p0"], e["p1"], e["width"], e["z_eave"], e["rise"], e.get("mat_wall", "stone"),
e.get("mat_roof", "slate"), g, z0=e.get("z0", 0.0))
elif k == "person":
person(e["xy"], z0_of(e), e.get("height", 1.72), e.get("heading", 0.0), e.get("robe", False),
e.get("cloth", 0), "people", e.get("stride", 0.0), e.get("label", "person"))
elif k == "cart":
cart(e["xy"], z0_of(e), e.get("heading", 0.0), e.get("load", "hay"))
elif k == "horse":
horse(e["xy"], z0_of(e), e.get("heading", 0.0), e.get("pack", False))
elif k == "tree":
tree(e["xy"], z0_of(e), e.get("h", 9.0), e.get("r", 3.2), e.get("conifer", False))
else:
raise ValueError("unknown solid kind: %s" % k)