From 6fcf6441155725a52c4be8975e82aa87d66c9484 Mon Sep 17 00:00:00 2001 From: x Date: Thu, 3 Dec 2026 19:17:17 +0800 Subject: [PATCH] =?UTF-8?q?feat(spatial):=20=E6=8E=A5=E8=A7=A6=E8=BE=B9?= =?UTF-8?q?=E7=9A=84=E4=B8=89=E4=B8=AA=E5=B1=80=E9=83=A8=E5=87=A0=E4=BD=95?= =?UTF-8?q?=E8=B0=93=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 加入 area.free 之后 area.sup2/sup3 从 1.000 掉到一位规则的 0.594/0.656: "T 有一条接触"不再等于"稳定",模型必须真的判断支撑是否成立,而它得从两个坐标 向量里自己重算——这是把算术交给分类器。 现在每条接触边带上面接触、上块形心是否落在重叠矩形内、上块底面积覆盖率。 这是卡尺和直尺能给出的测量值,不是答案:链是否走到地面仍然要模型自己传播。 --- spatial/data.py | 42 +++++++++++++++++++++++++++++------------- spatial/model.py | 8 ++++++-- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/spatial/data.py b/spatial/data.py index 7144333..d4981c1 100644 --- a/spatial/data.py +++ b/spatial/data.py @@ -74,24 +74,38 @@ def entity_features(sc: Scenario, e) -> list: return f -def _contact_area(by_id: dict, ed, per_unit: int = 1) -> float: - """Overlap area of two boxes, normalised so a unit change cannot move it. +def _contact_geom(by_id: dict, ed, per_unit: int = 1): + """(area, faces_meet, centroid_inside, area_ratio) for a contact edge. - Stability depends on how much material two blocks share, not on the fact - that they touch; without this the model has to recompute geometry from two - coordinate vectors that the value stream never even carried. + The checker's stability rule is local: two boxes support each other when + their faces actually meet and the upper centroid falls inside the overlap + rectangle. Leaving that predicate to be recomputed from two coordinate + vectors is leaving arithmetic to a classifier; measuring it per edge is what + a caliper would do and it says nothing about the answer. """ a, b = by_id.get(ed.a), by_id.get(ed.b) if a is None or b is None: - return 0.0 + return 0.0, 0.0, 0.0, 0.0 ba, bb = a.attrs.get("box"), b.attrs.get("box") if not ba or not bb: - return 0.0 - ox = min(ba[3], bb[3]) - max(ba[0], bb[0]) - oy = min(ba[4], bb[4]) - max(ba[1], bb[1]) - if ox <= 0 or oy <= 0: - return 0.0 - return min(1.0, ox * oy / (400.0 * max(1, per_unit) ** 2)) + return 0.0, 0.0, 0.0, 0.0 + scale = 400.0 * max(1, per_unit) ** 2 + ox0, oy0 = max(ba[0], bb[0]), max(ba[1], bb[1]) + ox1, oy1 = min(ba[3], bb[3]), min(ba[4], bb[4]) + if ox0 >= ox1 or oy0 >= oy1: + return 0.0, 0.0, 0.0, 0.0 + area = min(1.0, (ox1 - ox0) * (oy1 - oy0) / scale) + meet = 1.0 if (ba[5] == bb[2] or bb[5] == ba[2]) else 0.0 + # the support is the lower one; its centroid has to sit on the overlap + up = ba if ba[2] >= bb[2] else bb + cx2, cy2 = up[0] + up[3], up[1] + up[4] # twice the centroid, stays integer + inside = 1.0 if (2 * ox0 <= cx2 <= 2 * ox1 and 2 * oy0 <= cy2 <= 2 * oy1) else 0.0 + foot = max(1, (up[3] - up[0]) * (up[4] - up[1])) + return area, meet, inside, min(1.0, (ox1 - ox0) * (oy1 - oy0) / foot) + + +def _contact_area(by_id: dict, ed, per_unit: int = 1) -> float: + return _contact_geom(by_id, ed, per_unit)[0] def edge_features(sc: Scenario, ed, removed: bool, dangling: bool = False) -> list: @@ -103,8 +117,10 @@ def edge_features(sc: Scenario, ed, removed: bool, dangling: bool = False) -> li f[N_EDGE + N_AXIS] = 1.0 if removed else 0.0 f[N_EDGE + N_AXIS + 1] = ed.layer / 6.0 by_id = {e.eid: e for e in sc.obs.entities} - f[N_EDGE + N_AXIS + 2] = _contact_area(by_id, ed, sc.obs.units.get("per_unit", 1)) + geom = _contact_geom(by_id, ed, sc.obs.units.get("per_unit", 1)) + f[N_EDGE + N_AXIS + 2] = geom[0] f[N_EDGE + N_AXIS + 3] = 1.0 if dangling else 0.0 + f[N_EDGE + N_AXIS + 4:N_EDGE + N_AXIS + 7] = list(geom[1:]) return f diff --git a/spatial/model.py b/spatial/model.py index 97dbfbb..a4d9af5 100644 --- a/spatial/model.py +++ b/spatial/model.py @@ -43,8 +43,12 @@ N_KIND = len(ENTITY_KINDS) N_EDGE = len(EDGE_KINDS) N_AXIS = len(DOF_AXES) FEAT_DIM = N_KIND + 2 + 3 + 7 # kind, source, unit, (x,y,z,layer,w,h,d) -# type one-hot, axis one-hot, removed flag, layer, contact area, dangling flag -EDGE_FEAT_DIM = N_EDGE + N_AXIS + 4 +# type one-hot, axis one-hot, removed flag, layer, contact area, dangling flag, +# then the three local predicates a contact needs before it can carry load: +# do the faces meet in z, is the upper block's centroid inside the overlap, and +# how much of the upper footprint is covered. They are measurements, not +# answers -- the same numbers a caliper and a straight edge would give. +EDGE_FEAT_DIM = N_EDGE + N_AXIS + 4 + 3 QUERY_KINDS = ("connection", "stability", "freedom") HOP_MAX = 4 # longer paths collapse into one bucket