Tighten CGA polygon simplification, bay detailing, and LOD2 silhouettes for 16 MiB budget

This commit is contained in:
Meta Vens 2026-12-06 02:16:50 +01:00
commit 394e44369d

View file

@ -145,6 +145,8 @@ class CgaBuildingResult:
roof_y: float
ground_anchored_mask: int
zones: list[CgaZoneMesh]
lod2_tris: np.ndarray
lod2_mats: np.ndarray
# ---------------------------------------------------------------------------
@ -762,21 +764,18 @@ def _build_wall_bay_geometry(
return col_parts, col_parts, False
# Compute window openings within [0..bay_len] x [0..h]
# Keep at least 0.45m solid jambs on edges and 0.45m sill/header margins
# Keep 1-2 crisp windows per bay to hold <= 16 MiB RCGA city budget
if micro_zone == MICRO_FORTRESS:
win_step = 3.4
n_win = max(1, min(3, int((bay_len - 0.9) / win_step)))
win_w = min(0.70, (bay_len - 0.9) / n_win * 0.45)
n_win = 1 if bay_len < 6.0 else 2
win_w = min(0.68, (bay_len - 1.0) / n_win * 0.42)
win_h = min(1.75, h * 0.56)
elif micro_zone == MICRO_RONQUOZ:
win_step = 4.2
n_win = max(1, min(3, int((bay_len - 1.0) / win_step)))
win_w = min(2.20, (bay_len - 1.0) / n_win * 0.64)
win_h = min(1.30, h * 0.40)
n_win = 1 if bay_len < 7.5 else 2
win_w = min(2.40, (bay_len - 1.2) / n_win * 0.62)
win_h = min(1.25, h * 0.40)
else:
win_step = 3.0
n_win = max(1, min(3, int((bay_len - 0.8) / win_step)))
win_w = min(1.12, (bay_len - 0.8) / n_win * 0.48)
n_win = 1 if bay_len < 5.2 else 2
win_w = min(1.12, (bay_len - 0.9) / n_win * 0.46)
win_h = min(1.42, h * 0.52)
if win_w < 0.50 or win_h < 0.70:
@ -821,10 +820,8 @@ def _build_wall_bay_geometry(
ao_top=235, ao_bot=190, ao_side=155,
))
# Window glass backing + stone sills / lintels / optional Old Town shutters
# Kept strictly disjoint / >= 2cm overlap from coplanar faces so quantized welding stays manifold!
for wu0, wu1, wv0_i, wv1_i in win_boxes:
# Recessed dark glass box inside opening (penetrates jambs 2cm, front at d=-0.16)
# Window glass backing + selective stone sills / lintels / Old Town shutters
for wi_idx, (wu0, wu1, wv0_i, wv1_i) in enumerate(win_boxes):
parts.append(make_box_prism(
origin, u_axis, v_axis, n_axis,
wu0 - 0.02, wu1 + 0.02, wv0_i - 0.02, wv1_i + 0.02, -0.205, -0.16,
@ -832,23 +829,15 @@ def _build_wall_bay_geometry(
ao_front=190, ao_back=160, ao_side=150,
))
if micro_zone in (MICRO_OLD_TOWN, MICRO_CLAVAU, MICRO_FORTRESS, MICRO_CIVIC):
# Stone sill (projecting 0.10m outward, 0.11m tall)
if micro_zone in (MICRO_OLD_TOWN, MICRO_FORTRESS, MICRO_CIVIC):
parts.append(make_box_prism(
origin, u_axis, v_axis, n_axis,
wu0 - 0.06, wu1 + 0.06, max(0.05, wv0_i - 0.12), wv0_i - 0.015, -0.03, 0.10,
wu0 - 0.06, wu1 + 0.06, max(0.05, wv0_i - 0.12), wv0_i - 0.015, -0.03, 0.09,
pal["trim"], pal["trim"], pal["trim"],
ao_front=235, ao_back=180, ao_side=200,
))
# Stone lintel
parts.append(make_box_prism(
origin, u_axis, v_axis, n_axis,
wu0 - 0.06, wu1 + 0.06, wv1_i + 0.015, min(h - 0.05, wv1_i + 0.13), -0.03, 0.08,
pal["trim"], pal["trim"], pal["trim"],
ao_front=235, ao_back=180, ao_side=190,
))
if micro_zone in (MICRO_OLD_TOWN, MICRO_CLAVAU) and bay_len >= 4.2 and step_u >= 2.6:
if micro_zone == MICRO_OLD_TOWN and wi_idx == 0 and bay_len >= 4.2 and step_u >= 2.8:
sh_w = min(0.36, win_w * 0.40)
if wu0 - sh_w - 0.06 > 0.12 and wu1 + sh_w + 0.06 < bay_len - 0.12:
parts.append(make_box_prism(
@ -944,9 +933,9 @@ def _build_roof_zones(
merged = unary_union(polys)
geoms = list(merged.geoms) if hasattr(merged, "geoms") else [merged]
for g in geoms:
if isinstance(g, Polygon) and g.area > 0.6:
gs = g.simplify(0.22, preserve_topology=True)
if isinstance(gs, Polygon) and gs.area > 0.6:
if isinstance(g, Polygon) and g.area > 0.8:
gs = g.simplify(0.42, preserve_topology=True)
if isinstance(gs, Polygon) and gs.area > 0.8:
g = gs
# Add gentle eave overhang on pitched slopes
if n_axis[1] < 0.97 and micro_zone in (MICRO_OLD_TOWN, MICRO_CLAVAU, MICRO_SUBURBAN, MICRO_FORTRESS, MICRO_CIVIC):
@ -1019,7 +1008,7 @@ def build_cga_building(
micro_zone, arch_style = classify_sion_micro_zone(objektart, name, pts_enz, r_rtd, raw_min_y, raw_max_y)
pal = _choose_building_palette(b_hash, micro_zone, objektart)
simplify_tol = 0.14 if micro_zone == MICRO_FORTRESS else 0.28
simplify_tol = 0.25 if micro_zone in (MICRO_FORTRESS, MICRO_OLD_TOWN) else 0.55
footprints = _extract_footprint_polygons(fl_rtd, w_rtd, r_rtd, s_rtd, simplify_tol=simplify_tol)
if not footprints:
xmin, zmin = float(all_pts[:, 0].min()), float(all_pts[:, 2].min())
@ -1130,6 +1119,17 @@ def build_cga_building(
is_canopy_only = (len(w_rtd) == 0 and wall_h > 1.2) or objektart in ("Flugdach",)
is_mauer = (objektart == "Mauer gross")
# Build lean watertight LOD2 silhouette (single simplified prism + roof cap) for 620..2200m GLB
lod2_fp = primary_fp.simplify(0.85, preserve_topology=True)
lod2_fp = _clean_polygon_2d(lod2_fp if isinstance(lod2_fp, Polygon) else primary_fp, min_area=0.5) or primary_fp
lod2_uv = Polygon([(x, -z) for x, z in lod2_fp.exterior.coords])
lod2_h = max(1.2, max(eave_y, raw_max_y) - plinth_bot_y)
lod2_t, lod2_m, _, _ = extrude_2d_poly_prism(
np.array([0.0, plinth_bot_y, 0.0]), u_xz, v_xz, n_up, lod2_uv,
0.0, lod2_h,
mat_top=pal["roof"], mat_bot=pal["plinth"], mat_side=pal["wall"],
)
# Early exit for low/underground single-zone structures
if wall_h < 0.9 and not r_rtd:
c_pts = np.vstack([z.col_tris.reshape(-1, 3) for z in zones])
@ -1139,10 +1139,11 @@ def build_cga_building(
bounds_min=c_pts.min(axis=0), bounds_max=c_pts.max(axis=0),
ground_y=float(c_pts[:, 1].min()), roof_y=float(c_pts[:, 1].max()),
ground_anchored_mask=plinth_bit, zones=zones,
lod2_tris=lod2_t, lod2_mats=lod2_m,
)
# 2. Precompute Roof Zones so we know exact zone budget
max_roofs = 0 if is_mauer else 6
max_roofs = 0 if is_mauer else 4
roof_items = _build_roof_zones(
r_rtd, footprints, eave_y, bldg_center_xz, pal, max_roofs, micro_zone, objektart
)
@ -1179,21 +1180,20 @@ def build_cga_building(
}[micro_zone]
n_stories = max(1, int(round(wall_h / story_h_target)))
# Extract simplified perimeter segments for WallBays
ext_pts = _merge_short_ring_edges(list(primary_fp.exterior.coords), min_edge_len=1.6)
min_edge = 2.0 if micro_zone in (MICRO_OLD_TOWN, MICRO_FORTRESS) else 3.0
ext_pts = _merge_short_ring_edges(list(primary_fp.exterior.coords), min_edge_len=min_edge)
raw_bays_len = max(3, len(ext_pts))
# Budget: 1 (Plinth) + 1 (Core) + n_roofs + n_tiers * (bays_per_tier + 1 slab) <= 64
# Cap structural tiers cleanly so total city RCGA + LOD2 fits well <= 16 MiB
avail_for_stories = MAX_ZONES_PER_BUILDING - (2 + n_roofs)
n_tiers = min(n_stories, max(1, avail_for_stories // max(4, min(raw_bays_len + 1, 10))))
n_tiers = max(1, min(n_tiers, 8))
max_tiers = 5 if n_stories >= 8 else (4 if micro_zone in (MICRO_OLD_TOWN, MICRO_FORTRESS) else 3)
n_tiers = min(n_stories, max_tiers, max(1, avail_for_stories // max(4, min(raw_bays_len + 1, 10))))
n_tiers = max(1, n_tiers)
# Max bays per tier that fits inside budget
max_bays_per_tier = max(3, (avail_for_stories // n_tiers) - 1)
max_bays_per_tier = max(3, min(10, (avail_for_stories // n_tiers) - 1))
# Dynamically choose max_bay_len so bay count fits max_bays_per_tier cleanly
perim = float(primary_fp.exterior.length)
base_bay_len = 10.0 if (is_mauer or perim > 60.0) else 8.0
base_bay_len = 9.0 if micro_zone in (MICRO_OLD_TOWN, MICRO_FORTRESS) else (14.0 if (is_mauer or micro_zone == MICRO_RONQUOZ or perim > 70.0) else 11.5)
bays_2d: list[tuple[np.ndarray, np.ndarray, float]] = []
for mult in (1.0, 1.4, 2.0, 3.0, 5.0, 10.0):
cand = _split_ring_into_wall_bays(ext_pts, base_bay_len * mult)
@ -1201,15 +1201,13 @@ def build_cga_building(
bays_2d = cand
break
if not bays_2d:
# Simplify footprint more aggressively to a clean bounding/octagonal hull
simple_fp = primary_fp.simplify(max(0.6, perim * 0.03), preserve_topology=True)
if isinstance(simple_fp, Polygon) and not simple_fp.is_empty:
ext_pts = _merge_short_ring_edges(list(simple_fp.exterior.coords), min_edge_len=2.2)
ext_pts = _merge_short_ring_edges(list(simple_fp.exterior.coords), min_edge_len=2.6)
cand = _split_ring_into_wall_bays(ext_pts, max(base_bay_len, perim / max(3, max_bays_per_tier)))
if len(cand) <= max_bays_per_tier:
bays_2d = cand
else:
# Guaranteed 4-bay oriented bounding box fallback
minx, minz, maxx, maxz = primary_fp.bounds
box_pts = [(minx, minz), (maxx, minz), (maxx, maxz), (minx, maxz)]
bays_2d = _split_ring_into_wall_bays(box_pts, max(maxx - minx, maxz - minz) + 1.0)
@ -1233,6 +1231,9 @@ def build_cga_building(
slab_fp = primary_fp.buffer(-slab_inset, join_style="mitre")
if not isinstance(slab_fp, Polygon) or slab_fp.is_empty or slab_fp.area < 0.8:
slab_fp = primary_fp
slab_fp_s = slab_fp.simplify(0.60, preserve_topology=True)
if isinstance(slab_fp_s, Polygon) and not slab_fp_s.is_empty and slab_fp_s.area >= 0.8:
slab_fp = slab_fp_s
slab_uv = fp_to_uv(slab_fp)
slab_thick = 0.28
@ -1241,8 +1242,7 @@ def build_cga_building(
prev_bay_mids: list[np.ndarray] = []
all_tier_supports_mask = plinth_bit | core_bit
# Allow detailed windowed bays while keeping total city triangles crisp & compact
perim_details = (micro_zone in (MICRO_OLD_TOWN, MICRO_FORTRESS, MICRO_CIVIC, MICRO_CLAVAU)) or (perim <= 85.0)
hero_zone = micro_zone in (MICRO_OLD_TOWN, MICRO_FORTRESS, MICRO_CIVIC, MICRO_VISSIGEN)
for t_idx in range(n_tiers):
y0 = plinth_top_y + t_idx * tier_h
@ -1256,9 +1256,12 @@ def build_cga_building(
for b_i, (sa, sb, blen) in enumerate(bays_2d):
if len(zones) >= MAX_ZONES_PER_BUILDING - n_roofs - 1:
break
want_win = (not is_mauer) and (
(hero_zone and blen >= 2.8) or (blen >= 3.6 and (b_i % 2 == 0))
)
r_parts, c_parts, has_win = _build_wall_bay_geometry(
sa, sb, blen, y0, bay_y_top, wall_thick, pal, micro_zone,
allow_details=(not is_mauer) and (perim_details or (b_i % 2 == 0)),
allow_details=want_win,
)
if t_idx == 0:
sup_mask = plinth_bit
@ -1336,6 +1339,8 @@ def build_cga_building(
roof_y=float(bmax[1]),
ground_anchored_mask=g_mask,
zones=zones,
lod2_tris=lod2_t,
lod2_mats=lod2_m,
)
@ -1617,14 +1622,11 @@ def export_rcga_tile(tx: int, tz: int, buildings: list[CgaBuildingResult], out_p
def export_city_lod2_glb(tx: int, tz: int, buildings: list[CgaBuildingResult], out_path) -> tuple[int, int]:
"""Export simplified watertight LOD2 GLB (Plinth + Wall collision hulls + Roof hulls) on 16x16 grid."""
"""Export simplified watertight LOD2 GLB (one simplified silhouette hull per building) on 16x16 grid."""
mat_tris: dict[int, list[np.ndarray]] = defaultdict(list)
for b in buildings:
for z in b.zones:
if z.zone_kind in (ZONE_FLOOR_SLAB, ZONE_INTERIOR_CORE):
continue
for tri, mid in zip(z.col_tris, z.col_mats):
mat_tris[int(mid)].append(tri)
for tri, mid in zip(b.lod2_tris, b.lod2_mats):
mat_tris[int(mid)].append(tri)
out_path.parent.mkdir(parents=True, exist_ok=True)
blob = bytearray()