feat(review): 几何干涉检查 find_collisions(BVH 粗筛 + 容器过滤)
C++(cad_mcp_plugins):
- 新增 bvh.h:header-only 2D BVH(质心中位分割,叶<=4,SelfPairs 交叉对枚举)
- 新增 find_collisions.cpp:粗筛 AABB 相交实体对,附重叠盒 + 逐轴重叠比 ratio
(= 逐轴重叠长/较短长 之积,退化轴取 1.0),按 ratio 降序 + 分页;
narrow=true 只对返回页跑 intersectWith 细筛(附 exact)
- 容器过滤(关键):图框块/分区边界会与一切相交,用重叠【度】判定
deg >= max(exclude_container_deg=8, exclude_container_frac=0.25 * 其他实体数),
与图幅/缩放无关;被剔除者在 excluded_containers 里如实报告
(初版用面积判据对多图框并排失效:3 框只占并集 27/13/7% 全部漏过,
全模型 163 对噪声;改度判据后图框 deg=35/25/23 vs 其余 deg<=3,分离干净)
- cmakelists.txt + 两个 mcp_config.json 注册(28 工具)
agent 侧(Python):
- cad_tools.READ_ONLY_TOOLS += find_collisions(受控子循环可调用)
- checks.check_collisions + run_deterministic_checks(受 CHECK_COLLISIONS 开关,默认关)
- config += CHECK_COLLISIONS=False / COLLISION_MIN_OVERLAP=0.5
- checklist.py / review_checklist.json += 「干涉/碰撞」类别(high)+ 提示词协议
(先 find_collisions 拿候选 → inspect_region 目视确认;AABB 相交 != 真干涉)
验证:
- verify_collisions.py 17/17 PASS:全模型候选 163→80(全部 ratio=0.000),
min_overlap=0.5 → 0 对;逐图框 0 对;narrow top-5 中 4 条 exact=true;
--inject 注入深重叠矩形报出 ratio=0.902 并清理 2/2
- pytest 355 passed(新增 12 项)
- 端到端:LLM 主动调用 find_collisions{min_overlap:0.2,narrow:true,window:<图框>},未误报
This commit is contained in:
1 parent
a77a090281
commit
d4a9442e30
20 files changed
+1152
-13
No files matched your search
@@ -17,6 +17,7 @@ READ_ONLY_TOOLS = frozenset(
|
||||
"detect_drawing_frames",
|
||||
"find_duplicate_entities",
|
||||
"find_zero_length",
|
||||
"find_collisions",
|
||||
"zoom_to_window",
|
||||
"zoom_to_entity",
|
||||
"get_viewport_screenshot",
|
||||
|
||||
@@ -15,6 +15,8 @@ DEFAULT_CHECKLIST = {
|
||||
"checks": ["关键尺寸缺失", "标注值异常"]},
|
||||
{"title": "几何", "severity": "medium",
|
||||
"checks": ["零长度/退化实体", "重复重叠实体", "未闭合轮廓"]},
|
||||
{"title": "干涉/碰撞", "severity": "high",
|
||||
"checks": ["构件/管线间非预期侵入", "同位置重复叠画", "引线/填充异常遮挡"]},
|
||||
{"title": "图面整体", "severity": "low",
|
||||
"checks": ["图面完整性", "内容是否超出图框"]},
|
||||
],
|
||||
@@ -67,7 +69,11 @@ def build_system_prompt(checklist, *, allow_tools=True):
|
||||
"若它提示区域过大,请缩小 `target`(或用 `span` 缩小范围)后重试。",
|
||||
"",
|
||||
"结构化数据不足以判断时:用 `get_entity_detail` / `find_duplicate_entities` /",
|
||||
"`find_zero_length` / `get_layers` 等查证后再下结论。",
|
||||
"`find_zero_length` / `find_collisions` / `get_layers` 等查证后再下结论。",
|
||||
"排查「干涉/碰撞」时,先调 `find_collisions` 拿到**包围盒相交候选**"
|
||||
"(按重叠比降序,`min_overlap` 可调阈值),",
|
||||
"再对可疑的候选 handle 用 `inspect_region` 放大目视确认——"
|
||||
"**AABB 相交不等于真干涉**,务必核实后再下结论。",
|
||||
"",
|
||||
]
|
||||
|
||||
@@ -84,7 +90,7 @@ def build_system_prompt(checklist, *, allow_tools=True):
|
||||
' "summary": "<该图框的一句话整体结论;若未发现问题,写“未发现明显问题”>",',
|
||||
' "issues": [',
|
||||
" {",
|
||||
' "category": "<图层/文字/标注/几何/图面整体 之一>",',
|
||||
' "category": "<图层/文字/标注/几何/干涉/图面整体 之一>",',
|
||||
' "severity": "<high|medium|low>",',
|
||||
' "message": "<问题描述,简洁具体>",',
|
||||
' "handle": "<该问题对应的实体句柄;无法定位到具体实体时省略此字段>"',
|
||||
|
||||
+47
-1
@@ -1,4 +1,5 @@
|
||||
"""确定性检查:MCP 工具(find_zero_length / find_duplicate_entities)+ 纯 Python 派生。
|
||||
"""确定性检查:MCP 工具(find_zero_length / find_duplicate_entities / find_collisions)
|
||||
+ 纯 Python 派生。
|
||||
|
||||
产出的 issue 带 `source="deterministic"`,其 handle 来自工具/数据本身,
|
||||
因此被视为**可信**,可直接进入批注链路,不依赖 LLM 回忆。
|
||||
@@ -101,6 +102,44 @@ def check_duplicate_entities(ctx, window, *, tolerance, group_limit, log=print):
|
||||
return out
|
||||
|
||||
|
||||
def check_collisions(ctx, window, *, min_overlap, limit, log=print):
|
||||
"""find_collisions(window) → 每条「包围盒干涉候选」转 issue。
|
||||
|
||||
**候选**语义:AABB 相交不等于真干涉(L 形布局、相邻管段接口、标注引线跨越
|
||||
图元都会相交),故严重度取 medium、category 单列「干涉」,由报告层 / 人复核。
|
||||
重叠比 `ratio` 写进 message,便于按可疑程度排序与人工筛选。
|
||||
"""
|
||||
try:
|
||||
resp = ctx.tools.call(
|
||||
"find_collisions",
|
||||
{"window": window, "min_overlap": min_overlap, "limit": limit},
|
||||
)
|
||||
except (McpError, McpToolError) as exc:
|
||||
log(f" [确定性] find_collisions 失败: {exc}")
|
||||
return []
|
||||
|
||||
out = []
|
||||
for item in _iter_items(result_json(resp, {})):
|
||||
a = item.get("a") if isinstance(item.get("a"), dict) else {}
|
||||
b = item.get("b") if isinstance(item.get("b"), dict) else {}
|
||||
ha, hb = a.get("handle"), b.get("handle")
|
||||
if not ha or not hb:
|
||||
continue
|
||||
ov = item.get("overlap") if isinstance(item.get("overlap"), dict) else {}
|
||||
ratio = ov.get("ratio")
|
||||
ratio_txt = f"{ratio:.2f}" if isinstance(ratio, (int, float)) else "?"
|
||||
out.append(
|
||||
make_issue(
|
||||
"干涉", "medium",
|
||||
f"包围盒干涉候选({a.get('type')}/{ha} × {b.get('type')}/{hb},"
|
||||
f"重叠比 {ratio_txt},同层={item.get('same_layer')})",
|
||||
handle=ha,
|
||||
handles=[ha, hb],
|
||||
)
|
||||
)
|
||||
return out
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 纯 Python 派生(零额外 MCP 往返)
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -161,4 +200,11 @@ def run_deterministic_checks(ctx, evidence, *, log=print):
|
||||
group_limit=config.DUP_GROUP_LIMIT,
|
||||
log=log,
|
||||
)
|
||||
if config.CHECK_COLLISIONS:
|
||||
issues += check_collisions(
|
||||
ctx, window,
|
||||
min_overlap=config.COLLISION_MIN_OVERLAP,
|
||||
limit=config.FRAME_ENTITY_LIMIT,
|
||||
log=log,
|
||||
)
|
||||
return issues
|
||||
@@ -119,6 +119,13 @@ ZERO_LENGTH_TOLERANCE = float(os.environ.get("ZERO_LENGTH_TOLERANCE", "0.001"))
|
||||
DUP_TOLERANCE = float(os.environ.get("DUP_TOLERANCE", "0.001"))
|
||||
# find_duplicate_entities 的 limit(语义是「最大重复组数」,非条目数)
|
||||
DUP_GROUP_LIMIT = int(os.environ.get("DUP_GROUP_LIMIT", "50"))
|
||||
# 是否在确定性检查里跑 find_collisions(包围盒干涉**候选**粗筛)。
|
||||
# 默认关闭:平面图里管线穿墙、标注引线跨图元等属正常相交,AABB 相交 ≠ 真干涉,
|
||||
# 全量跑会淹没报告;改由审查 LLM 按「干涉/碰撞」清单条目**按需**调用该工具。
|
||||
CHECK_COLLISIONS = _read_bool("CHECK_COLLISIONS", False)
|
||||
# find_collisions 的最小重叠比阈值(逐轴重叠比之积,见 find_collisions.cpp 注释)。
|
||||
# 0.5 已能滤掉「仅角部相接 / 细长交叉」的弱候选,只留明显互相侵入的对。
|
||||
COLLISION_MIN_OVERLAP = float(os.environ.get("COLLISION_MIN_OVERLAP", "0.5"))
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 截图(视觉 token 成本控制)
|
||||
|
||||
Reference in new issue
Block a user