"""视野边界:可见性能不能算出 hidden 和 dangling。 M2 的输入契约卡在一个循环上:``Observation.hidden`` 和 ``Observation.dangling`` 说"有东西我没看到",而现在的夹具是拿**完整状态减去可见集合**算出来的。真实场景 里没有完整状态可用,否则就不用谈不确定了。 出路只有一条:把"我没看到"换成某个能从**观测本身**算出来的量。最容易想到的是 表面可见性——拿几个正交视角,一个面如果背朝所有相机、或被别的零件挡住,就是 "没看到的面",而没看到的面正是关系可能附着的地方。 这个模块就是为了把这条路走到底并给出判决,而不是为了得到一个好用的特征: * :func:`unobserved_faces` 只吃可见实体和相机方向,不碰完整状态; * :func:`report` 把它和 ``dangling`` 的锚点逐实体对账。 结论写在 ``docs/M2_VIEW_CONTRACT.md``:这条路走不通,而且是两头都不通。 """ from __future__ import annotations import sys from . import generator from .geom import box_faces # An inspection rig: five orthographic views. Not a rendering camera -- the point # is only that "seen" has to mean something computable from geometry alone. VIEWS = ((1.0, 0.0, 0.0), (-1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, -1.0, 0.0), (0.0, 0.0, 1.0)) def _box(entity): b = entity.attrs.get("box") return tuple(float(v) for v in b) if b else None def _face_center(box, axis, sign): x0, y0, z0, x1, y1, z1 = box c = [(x0 + x1) / 2.0, (y0 + y1) / 2.0, (z0 + z1) / 2.0] c[axis] = x1 if sign > 0 else x0 return c def _ray_hits_box(box, p, d, eps: float = 1e-9) -> bool: """Slab test for the ray p + t*d, t > eps. Used for occlusion only.""" tmin, tmax = 0.0, float("inf") for k in range(3): lo, hi = box[k], box[k + 3] if abs(d[k]) < 1e-12: if p[k] <= lo or p[k] >= hi: return False else: t1, t2 = (lo - p[k]) / d[k], (hi - p[k]) / d[k] if t1 > t2: t1, t2 = t2, t1 if t1 > tmin: tmin = t1 if t2 < tmax: tmax = t2 if tmin > tmax: return False return tmax > eps def face_seen(box, face, others, eps: float = 1e-9) -> bool: """Is this face visible from at least one view, with nothing in the way. ``others`` are the boxes that may occlude it. The ray starts just off the surface so a face flush against another part does not test against itself, and it is a single ray from the face centre -- an approximation that only ever over-reports occlusion for concave parts, which these fixtures do not have. """ axis, sign, _, _ = face n = [0.0, 0.0, 0.0] n[axis] = float(sign) p = _face_center(box, axis, sign) for d in VIEWS: if sum(n[k] * d[k] for k in range(3)) <= 0: continue # back-facing under this view start = [p[k] + n[k] * eps for k in range(3)] if not any(_ray_hits_box(o, start, d, eps) for o in others): return True return False def unobserved_faces(entities) -> dict: """eid -> number of its six faces no view can see. Box entities only.""" boxes = {e.eid: _box(e) for e in entities if _box(e) is not None} out = {} for eid, box in boxes.items(): others = [b for k, b in boxes.items() if k != eid] out[eid] = sum(1 for f in box_faces(box) if not face_seen(box, f, others)) return out def dangling_anchors(obs) -> set: return {e.a for e in obs.dangling} def report(n: int = 24) -> None: """Per family: do "a relation I cannot see the far end of" and "a surface no view can see" pick out the same entities. Two ways to be wrong, and both matter: * ``d-only`` -- an entity carrying a dangling relation that every view sees completely. Geometry has no way to say "something attaches here", so the fact can only be *declared*. Inner-layer conductors in a PCB are exactly this: the conductor is not occluded, it is inside another material. * ``u-only`` -- an entity with unseen surfaces and no dangling relation. A part resting on a pedestal has an unseeable bottom face every time, so the signal is on for nearly everything and stops distinguishing anything. """ print(f"{'family':11s} {'可见':>4s} {'有盒':>4s} {'悬空锚':>5s} {'有未见面':>6s} " f"{'两者':>4s} {'仅悬空':>5s} {'仅见面':>5s} {'平均未见面':>8s}") print("-" * 66) for fam in generator.FAMILIES: vis = boxed = both = u_only = dang = unobs = 0 tot_faces = 0 for i in range(n): sc = generator.make(fam, seed=5, index=i) ents = list(sc.obs.entities) vis += len(ents) un = unobserved_faces(ents) boxed += len(un) tot_faces += sum(un.values()) anchors = dangling_anchors(sc.obs) dang += len(anchors) unobs += sum(1 for v in un.values() if v > 0) both += sum(1 for eid in anchors if un.get(eid, 0) > 0) u_only += sum(1 for eid, v in un.items() if v > 0 and eid not in anchors) per = max(1, n) avg = (tot_faces / boxed) if boxed else 0.0 print(f"{fam:11s} {vis / per:4.1f} {boxed / per:4.1f} {dang / per:5.1f} " f"{unobs / per:6.1f} {both / per:4.1f} {(dang - both) / per:7.1f} " f"{u_only / per:5.1f} {avg:8.2f}") print() print("可见 = 观测里的实体数(含无几何的地面/焊盘)") print("有盒 = 能用表面可见性说话的实体数;连接域恒为 0,那类的隐藏不是遮挡") print("悬空锚 = 挂着悬空关系的实体数(悬空关系 = 一端不可见,只剩锚点)") print("有未见面 = 至少有一张面任何视角都看不到的实体数") print("仅悬空 = 有悬空关系但六张面全可见 —— 表面可见性对它无话可说,只能靠声明") print("仅见面 = 有未见面但没有悬空关系 —— 坐着的东西底面永远看不到,信号恒亮") print("平均未见面 = 有盒实体平均有多少张面看不到(0-6)") def main(argv=None) -> int: argv = list(sys.argv[1:] if argv is None else argv) report(int(argv[0]) if argv else 24) return 0 if __name__ == "__main__": sys.exit(main())