Generalization hardening from test3d-7 round 1 (黄桥文体中心): 8 gap fixes
Conversion/encoding chain (libredwg on Chinese TArch DWGs):
- run_dwg2dxf: capture bytes, decode leniently (GBK console output crashed
the batch under subprocess text=True)
- sanitize_dxf_comments: byte-level, code/value pair walk — the UTF-8
errors="ignore" round-trip mangled every GBK CJK string ("一层平面图"
-> "一平图") and eating XRECORD "999" VALUE lines corrupted files
- read_doc: recover fallback also on ValueError; scrub_surrogates keeps one
corrupt drawing from killing the whole report
- decode_mif in report + frame detector: \M+5XXXX escapes (whole 黄桥 set)
restored so plan-title detection works at all
- ODA auto-fallback: reconvert DWGs whose libredwg DXF fails inspection or
reads an empty modelspace (黄桥 plans: 0 entities via dwg2dxf, 21k via
ODA); env ODA_CONVERTER override, fake-converter regression test
Frame detection:
- detect_sheet_frames expands INSERTs (memoized, depth<=3) when collecting
axis/wall lines — 黄桥 wraps each frame's grid in anonymous A$C... blocks
(msp had 8 DOTE lines, blocks held 700); all 7 frames went from WEAK
clip-corner fallback to grid-aligned votes x=14
Extraction judgment rules (both loud, both default-on, both switchable):
- drop_isolated_clusters: tiny segment clusters far from the main mass are
sheet-legend content drawn on real layers (title-block window symbols made
every floor's footprint grow a sliver fin)
- drop_unsupported_roof_lines: roof outlines with no footprint/cap plate
within 2m beneath are hatch-only-support canopies that extrude into
floating ribbons (F3 SW 坡屋面, B1 ground canopy at -5.85m)
Tests: 64 -> 75 checks, incl. GBK/MIF synthetic DXFs, block-wrapped grid
alignment (non-uniform bays — uniform grids make subset correlation
ambiguous), legend-cluster and unsupported-roof fixtures.
references/generalization_test_log.md tracks the test protocol, per-project
gate table, and every gap with its regression coverage.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -771,6 +771,163 @@ def compute_caps(floors: dict[str, Any], config: dict[str, Any]) -> None:
|
||||
fl.setdefault("caps", []).append({"z": round(ftop, 6), "polygons": polys})
|
||||
|
||||
|
||||
def drop_isolated_clusters(segments: dict[str, list[dict[str, Any]]], config: dict[str, Any], floor_key: str) -> dict[str, list[dict[str, Any]]]:
|
||||
"""Drop tiny segment clusters far away from the floor's main mass.
|
||||
|
||||
Sheet legends and title blocks draw window/door symbols with REAL geometry
|
||||
layers (黄桥: four 1 m CURTWALL strips 30 m east of the building on every
|
||||
frame), so layer exclusion cannot catch them — and the generated footprint
|
||||
hull then stretches into sliver fins ("每层一个尖角"). Cluster all
|
||||
classified segments by endpoint proximity, keep the largest cluster plus
|
||||
anything near it or substantial, and WARN about what was dropped.
|
||||
Config: {"drop_isolated_clusters": false} disables; tuning knobs
|
||||
cluster_linkage (m, 3.0), isolation_distance (m, 12.0),
|
||||
isolation_min_length (m, 15.0).
|
||||
"""
|
||||
if not config.get("drop_isolated_clusters", True):
|
||||
return segments
|
||||
linkage = float(config.get("cluster_linkage", 3.0))
|
||||
iso_dist = float(config.get("isolation_distance", 12.0))
|
||||
min_len = float(config.get("isolation_min_length", 15.0))
|
||||
|
||||
items: list[tuple[str, int, dict[str, Any]]] = []
|
||||
for category, segs in segments.items():
|
||||
for i, seg in enumerate(segs):
|
||||
items.append((category, i, seg))
|
||||
if len(items) < 2:
|
||||
return segments
|
||||
|
||||
parent = list(range(len(items)))
|
||||
|
||||
def find(a: int) -> int:
|
||||
while parent[a] != a:
|
||||
parent[a] = parent[parent[a]]
|
||||
a = parent[a]
|
||||
return a
|
||||
|
||||
def union(a: int, b: int) -> None:
|
||||
ra, rb = find(a), find(b)
|
||||
if ra != rb:
|
||||
parent[rb] = ra
|
||||
|
||||
cell = max(linkage, 1e-6)
|
||||
buckets: dict[tuple[int, int], list[int]] = {}
|
||||
for idx, (_c, _i, seg) in enumerate(items):
|
||||
for px, py in (seg["start"], seg["end"]):
|
||||
buckets.setdefault((int(px // cell), int(py // cell)), []).append(idx)
|
||||
for (bx, by), members in buckets.items():
|
||||
for gx in (bx - 1, bx, bx + 1):
|
||||
for gy in (by - 1, by, by + 1):
|
||||
for other in buckets.get((gx, gy), []):
|
||||
union(members[0], other)
|
||||
for other in members[1:]:
|
||||
union(members[0], other)
|
||||
|
||||
clusters: dict[int, list[int]] = {}
|
||||
for idx in range(len(items)):
|
||||
clusters.setdefault(find(idx), []).append(idx)
|
||||
|
||||
def cluster_stats(indices: list[int]) -> tuple[float, list[float]]:
|
||||
total = 0.0
|
||||
bx = [math.inf, math.inf, -math.inf, -math.inf]
|
||||
for idx in indices:
|
||||
seg = items[idx][2]
|
||||
(x1, y1), (x2, y2) = seg["start"], seg["end"]
|
||||
total += math.hypot(x2 - x1, y2 - y1)
|
||||
bx[0] = min(bx[0], x1, x2); bx[1] = min(bx[1], y1, y2)
|
||||
bx[2] = max(bx[2], x1, x2); bx[3] = max(bx[3], y1, y2)
|
||||
return total, bx
|
||||
|
||||
stats = {root: cluster_stats(idx_list) for root, idx_list in clusters.items()}
|
||||
main_root = max(stats, key=lambda r: stats[r][0])
|
||||
main_box = stats[main_root][1]
|
||||
|
||||
def box_gap(a: list[float], b: list[float]) -> float:
|
||||
dx = max(0.0, max(a[0] - b[2], b[0] - a[2]))
|
||||
dy = max(0.0, max(a[1] - b[3], b[1] - a[3]))
|
||||
return math.hypot(dx, dy)
|
||||
|
||||
dropped: set[int] = set()
|
||||
for root, idx_list in clusters.items():
|
||||
if root == main_root:
|
||||
continue
|
||||
total, box = stats[root]
|
||||
gap = box_gap(main_box, box)
|
||||
if gap > iso_dist and total < min_len:
|
||||
dropped.update(idx_list)
|
||||
print(
|
||||
f"WARNING isolated_cluster {floor_key}: dropped {len(idx_list)} segments "
|
||||
f"({total:.1f}m total, {gap:.0f}m from main mass) — likely sheet legend/"
|
||||
"title-block content; set drop_isolated_clusters=false to keep",
|
||||
file=sys.stderr,
|
||||
)
|
||||
if not dropped:
|
||||
return segments
|
||||
dropped_keys = {(items[d][0], items[d][1]) for d in dropped}
|
||||
return {
|
||||
category: [seg for i, seg in enumerate(segs) if (category, i) not in dropped_keys]
|
||||
for category, segs in segments.items()
|
||||
}
|
||||
|
||||
|
||||
def drop_unsupported_roof_lines(floors: dict[str, Any], config: dict[str, Any]) -> None:
|
||||
"""Drop roof outlines that would extrude into ribbons floating in mid-air.
|
||||
|
||||
Plans draw low canopies / porch roofs whose supports are hatch-only
|
||||
columns the extractor cannot capture (黄桥: 坡屋面 outlines on the F3
|
||||
sheet with zero walls beneath on any floor). A roof segment is kept only
|
||||
if some plate exists under it — any floor's footprint or cap polygon
|
||||
within roof_support_distance (m, default 2.0). Dropped segments are
|
||||
reported loudly; keep them via {"drop_unsupported_roof_lines": false} or
|
||||
model the canopy explicitly with a roof_specs plate.
|
||||
"""
|
||||
if not config.get("drop_unsupported_roof_lines", True):
|
||||
return
|
||||
try:
|
||||
from shapely.geometry import Point, Polygon
|
||||
from shapely.ops import unary_union
|
||||
except ImportError:
|
||||
return
|
||||
plates = []
|
||||
for fl in floors.values():
|
||||
polys = list(fl.get("footprint", {}).get("polygons", []))
|
||||
for cap in fl.get("caps", []):
|
||||
polys.extend(cap.get("polygons", []))
|
||||
for coords in polys:
|
||||
try:
|
||||
poly = Polygon([(float(x), float(y)) for x, y in coords])
|
||||
if not poly.is_valid:
|
||||
poly = poly.buffer(0)
|
||||
if not poly.is_empty:
|
||||
plates.append(poly)
|
||||
except Exception:
|
||||
continue
|
||||
if not plates:
|
||||
return # no footprint evidence at all — nothing to judge against
|
||||
support = unary_union(plates).buffer(float(config.get("roof_support_distance", 2.0)))
|
||||
for key, fl in floors.items():
|
||||
roof = fl.get("segments", {}).get("roof")
|
||||
if not roof:
|
||||
continue
|
||||
kept, dropped_len, dropped = [], 0.0, 0
|
||||
for seg in roof:
|
||||
mid = Point((seg["start"][0] + seg["end"][0]) / 2.0, (seg["start"][1] + seg["end"][1]) / 2.0)
|
||||
if support.contains(mid):
|
||||
kept.append(seg)
|
||||
else:
|
||||
dropped += 1
|
||||
dropped_len += math.hypot(seg["end"][0] - seg["start"][0], seg["end"][1] - seg["start"][1])
|
||||
if dropped:
|
||||
print(
|
||||
f"WARNING unsupported_roof {key}: dropped {dropped} roof segments "
|
||||
f"({dropped_len:.1f}m) with no slab/cap beneath — likely a canopy whose "
|
||||
"supports are hatch-only; set drop_unsupported_roof_lines=false or add a "
|
||||
"roof_specs plate to keep them",
|
||||
file=sys.stderr,
|
||||
)
|
||||
fl["segments"]["roof"] = kept
|
||||
|
||||
|
||||
def finalize_spec(
|
||||
floor_data: dict[str, dict[str, Any]],
|
||||
config: dict[str, Any],
|
||||
@@ -804,6 +961,9 @@ def finalize_spec(
|
||||
"source_type": raw["source_type"],
|
||||
}
|
||||
)
|
||||
segments = drop_isolated_clusters(segments, config, key)
|
||||
xs = [p for segs in segments.values() for seg in segs for p in (seg["start"][0], seg["end"][0])]
|
||||
ys = [p for segs in segments.values() for seg in segs for p in (seg["start"][1], seg["end"][1])]
|
||||
bounds = [
|
||||
round(min(xs), 6) if xs else 0.0,
|
||||
round(min(ys), 6) if ys else 0.0,
|
||||
@@ -834,6 +994,7 @@ def finalize_spec(
|
||||
gap_tol=float(config.get("glazing_gap_tol", 3.6)),
|
||||
)
|
||||
compute_caps(floors, config)
|
||||
drop_unsupported_roof_lines(floors, config)
|
||||
|
||||
# Topless-roof guard: the top above-ground floor of each group must end in
|
||||
# a cap or a config roof_specs entry, otherwise the model renders open-top
|
||||
|
||||
Reference in New Issue
Block a user