diff --git a/tools/maps/sion/cga_builder.py b/tools/maps/sion/cga_builder.py index 952505f..68eaf32 100644 --- a/tools/maps/sion/cga_builder.py +++ b/tools/maps/sion/cga_builder.py @@ -22,7 +22,7 @@ import zlib import numpy as np import pygltflib import shapely -from shapely.geometry import LineString, MultiPolygon, Polygon, box +from shapely.geometry import LinearRing, LineString, MultiPolygon, Polygon, box from shapely.ops import unary_union @@ -169,12 +169,16 @@ def _clean_polygon_2d(poly: Polygon, min_area: float = 0.05) -> Polygon | None: p = max(geoms, key=lambda g: g.area) if not isinstance(p, Polygon) or p.area < min_area: return None - # Strip tiny sliver holes + # Strip tiny sliver holes and holes touching/pinching the exterior or each other + ext_ring = LinearRing(p.exterior.coords) good_holes = [] + kept_hole_polys: list[Polygon] = [] for ring in p.interiors: hp = Polygon(ring) - if hp.area >= 0.15: - good_holes.append(list(ring.coords)) + if hp.area >= 0.50 and LinearRing(ring.coords).distance(ext_ring) > 0.10: + if all(hp.distance(kh) > 0.10 for kh in kept_hole_polys): + good_holes.append(list(ring.coords)) + kept_hole_polys.append(hp) p = Polygon(list(p.exterior.coords), good_holes) if not p.is_valid: p = p.buffer(0) @@ -781,7 +785,7 @@ def _build_multi_edge_wall_bay_geometry( raw_polys.sort(key=lambda p: p.area, reverse=True) kept: list[Polygon] = [] for rp in raw_polys: - cleaned = _clean_polygon_2d(rp, min_area=0.02) + cleaned = _clean_polygon_2d(rp.simplify(0.15, preserve_topology=True), min_area=0.02) or _clean_polygon_2d(rp, min_area=0.02) if cleaned is not None and (not kept or all(cleaned.distance(k) > 0.05 for k in kept)): kept.append(cleaned) if not kept: @@ -1019,7 +1023,7 @@ def _build_roof_zones( geoms = list(merged.geoms) if hasattr(merged, "geoms") else [merged] for g in geoms: if isinstance(g, Polygon) and g.area > 0.8: - simp_r = 0.65 if micro_zone in (MICRO_FORTRESS, MICRO_OLD_TOWN, MICRO_CIVIC) else 1.25 + simp_r = 0.65 if micro_zone in (MICRO_FORTRESS, MICRO_OLD_TOWN, MICRO_CIVIC) else 1.65 gs = g.simplify(simp_r, preserve_topology=True) if isinstance(gs, Polygon) and gs.area > 0.8: g = gs @@ -1060,7 +1064,7 @@ def _build_roof_zones( mat_top=roof_mat, mat_bot=pal["trim"], mat_side=pal["trim"], ao_top=245, ao_bot=165, ao_side=200, ) - l2_roof_tol = 2.4 if micro_zone in (MICRO_FORTRESS, MICRO_OLD_TOWN, MICRO_CIVIC) else 3.6 + l2_roof_tol = 2.4 if micro_zone in (MICRO_FORTRESS, MICRO_OLD_TOWN, MICRO_CIVIC) else 4.0 l2_poly_s = poly.simplify(l2_roof_tol, preserve_topology=True) l2_poly = _clean_polygon_2d(l2_poly_s if isinstance(l2_poly_s, Polygon) else poly, min_area=0.8) or poly l2_roof_parts = extrude_2d_poly_prism( @@ -1088,7 +1092,7 @@ def _build_roof_zones( mat_top=roof_mat, mat_bot=pal["slab"], mat_side=pal["trim"], ao_top=240, ao_bot=175, ao_side=205, ) - l2_roof_tol = 2.2 if micro_zone in (MICRO_FORTRESS, MICRO_OLD_TOWN, MICRO_CIVIC) else 3.6 + l2_roof_tol = 2.2 if micro_zone in (MICRO_FORTRESS, MICRO_OLD_TOWN, MICRO_CIVIC) else 4.0 l2_fp_s = uv_poly.simplify(l2_roof_tol, preserve_topology=True) l2_uv = _clean_polygon_2d(l2_fp_s if isinstance(l2_fp_s, Polygon) else uv_poly, min_area=0.8) or uv_poly l2_roof_parts = extrude_2d_poly_prism( @@ -1185,10 +1189,6 @@ def build_cga_building( for fp in footprints: ps = fp.simplify(plinth_tol, preserve_topology=True) pc = _clean_polygon_2d(ps if isinstance(ps, Polygon) else fp, min_area=1.2) or fp - # Strip sub-10cm hairline spikes before Plinth/WallBay/FloorSlab construction - pc_sp = _clean_polygon_2d(pc.buffer(-0.06, join_style="mitre").buffer(0.06, join_style="mitre"), min_area=1.2) - if pc_sp is not None: - pc = pc_sp if not plinth_fps or all(pc.distance(prev) > 0.25 for prev in plinth_fps): plinth_fps.append(pc) plinth_fps.sort(key=lambda p: p.area, reverse=True) @@ -1304,7 +1304,7 @@ def build_cga_building( # Build real-footprint + pitched-roof watertight LOD2 solids lod2_solids: list[tuple[np.ndarray, int]] = [] lod2_body_h = max(1.0, eave_y - plinth_bot_y - 0.02) - l2_simp = 2.2 if micro_zone in (MICRO_FORTRESS, MICRO_CIVIC, MICRO_OLD_TOWN) else 4.2 + l2_simp = 2.2 if micro_zone in (MICRO_FORTRESS, MICRO_CIVIC, MICRO_OLD_TOWN) else 4.5 l2_wings = plinth_fps if micro_zone in (MICRO_FORTRESS, MICRO_CIVIC) else plinth_fps[:1] for pfp in l2_wings: l2_fp = pfp.simplify(l2_simp, preserve_topology=True) @@ -1379,7 +1379,7 @@ def build_cga_building( # Cap structural tiers cleanly so total city RCGA + LOD2 fits well <= 16 MiB (and total sion <= 85 MiB) avail_for_stories = MAX_ZONES_PER_BUILDING - (2 + n_roofs) - max_tiers = 4 if wall_h >= 22.0 else (3 if (hero_zone and wall_h >= 12.5) else (2 if ((hero_zone and wall_h >= 6.8) or wall_h >= 8.2) else 1)) + max_tiers = 4 if wall_h >= 25.0 else (3 if (hero_zone and wall_h >= 12.5) else (2 if ((hero_zone and wall_h >= 6.8) or wall_h >= 10.5) else 1)) n_tiers = min(n_stories, max_tiers, max(1, avail_for_stories // max(4, min(raw_bays_len + 1, 5)))) n_tiers = max(1, n_tiers) @@ -1407,7 +1407,7 @@ def build_cga_building( slab_inset = min(wall_thick * 0.45, 0.20) slab_polys_uv: list[Polygon] = [] for pfp in (plinth_fp,): - sfp = pfp.simplify(2.0, preserve_topology=True) + sfp = pfp.simplify(2.0 if hero_zone else 3.5, preserve_topology=True) if not isinstance(sfp, Polygon) or sfp.is_empty or sfp.area < 0.8: sfp = pfp sfp = sfp.buffer(-slab_inset, join_style="mitre") @@ -1453,8 +1453,7 @@ def build_cga_building( blen = float(np.linalg.norm(sb - sa)) want_win = (not is_mauer) and ( (hero_zone and blen >= 5.2 and t_idx == 0 and (b_i == 0 or (micro_zone in (MICRO_FORTRESS, MICRO_CIVIC) and b_i == 2 and blen >= 6.5))) - or (micro_zone in (MICRO_VISSIGEN, MICRO_CLAVAU, MICRO_SUBURBAN) and blen >= 16.0 and b_i == 0 and t_idx == 0) - or (micro_zone == MICRO_RONQUOZ and blen >= 22.0 and b_i == 0 and t_idx == 0) + or (micro_zone in (MICRO_VISSIGEN, MICRO_CLAVAU, MICRO_SUBURBAN) and blen >= 22.0 and b_i == 0 and t_idx == 0) ) r_parts, c_parts, has_win = _build_wall_bay_geometry( sa, sb, blen, y0, bay_y_top, wall_thick, pal, micro_zone,