Files
cad-agent/tests/test_tiling.py
T
gjm b757892c67 feat(review): agent 侧虚拟工具 inspect_region —— 按可读分辨率放大指定区域
解决三个缺口:① 放大没复用标定/瓦片数学(LLM 一次 zoom 只出一张,宽区域仍读不清);
②「看清没」由 VLM 自评(易过度自信/幻觉);③ 只能按 handle / 手算 bbox 指认,
图上看到但没 handle 的东西指认不了。

核心思路:把「放大」升级成一个语义化工具——LLM 只指认「要看哪里」,由 agent 复用
tiling.py 的标定数学算出该切几张、每张覆盖多大,用构造保证可读。

新增 dwg_review/inspect.py(agent 侧,不编 DLL):
- TOOL_SCHEMA:inspect_region(target, purpose);target 三形态 {handle} / {window} /
  {label,cx,cy,span},purpose = text(8px) / line(6px)。
- InspectState:单图框内「截图标签 → 世界可见范围」登记表,支持逐级下钻
  (返回的 inspect-N 可继续作为 label 指认)。
- resolve_target / _point_box:label+cx+cy → WCS。cy 是图像坐标(上→下),
  世界 y 必须翻转(y = y2 - cy*H)——单测专门钉住。
- plan_tiles:复用 world_per_px + tiles_for_box + subdivide;超 INSPECT_MAX_TILES(4)
  则硬失败并给出「缩小 target / 分多次请求」的指引(不静默降级);拿不到比例/像素
  则退回单张并如实告知。

关键几何:CAD 的 _.ZOOM _W 是等比适配(不拉伸),故「截图上的归一化坐标」必须按
max(W/ew, H/eh) 反算真实可见范围(新增 tiling.visible_box)——直接用送进去的窗口会偏。
zoom_to_window 返回的 view 是「加过 margin 的窗口」,不是可见范围,不能混用。

证据层配套:evidence_model.make_image(..., window=) 记录每张图的可见世界范围;
capture_images 用 view_px(overall 图原始像素)+ visible_box 逐张登记。

子循环接入:_run_readonly_tool 在只读白名单校验之前拦截 inspect_region(它不在
READ_ONLY_TOOLS 里,内部只调白名单内的只读 MCP 工具);_review_tools 启用时用
inspect_region 取代三个裸视口工具;inspect_region 计入 _VIEW_TOOLS → 结束复位视口。
配置:INSPECT_ENABLED(True) / INSPECT_MAX_TILES(4)。

端到端实测(真实图纸 1 图框):取证与审查两阶段均在最大化态、结束精确还原(4 项 PASS);
LLM 的 11 次取证调用里 2 次是 inspect_region,且都显式给 window + purpose:"text"
(x[20000,115000] y[-6500,3000],即 A0 图框底部横条=标题栏所在)。
验收口径达成:报告报出标题栏乱码 ???????????????,而 get_text_content 对全图返回 0 条文字
(本图文字全在块/自定义实体内)→ 块内文字是被视觉放大读出来的。
pytest 343 passed。
2026-10-16 19:15:32 +08:00

283 lines
11 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""`dwg_review.tiling` 单元测试(纯函数,不依赖 CAD / LLM / Pillow)。"""
import pytest
from dwg_review import tiling as tl
def _frame(bbox, sheet_size="A0", sheet_scale=0.0):
return {"bbox": list(bbox), "sheet_size": sheet_size, "sheet_scale": sheet_scale}
def _cluster(bbox, count=10):
return {"bbox": tuple(float(v) for v in bbox), "cells": [], "count": count}
# --- norm_box / frame_bbox --------------------------------------------------
def test_norm_box_valid_and_invalid():
assert tl.norm_box([0, 0, 10, 20]) == (0.0, 0.0, 10.0, 20.0)
assert tl.norm_box([10, 20, 0, 0]) == (0.0, 0.0, 10.0, 20.0)
assert tl.norm_box([1, 2, 3]) is None
assert tl.norm_box(None) is None
assert tl.norm_box([0, 0, float("nan"), 1]) is None
def test_frame_bbox_rejects_degenerate_and_non_dict():
assert tl.frame_bbox(_frame((0, 0, 100, 50))) == (0.0, 0.0, 100.0, 50.0)
assert tl.frame_bbox(_frame((0, 0, 0, 50))) is None # 零宽
assert tl.frame_bbox(_frame((0, 0, 100, 0))) is None # 零高
assert tl.frame_bbox({"bbox": [1, 2, 3]}) is None
assert tl.frame_bbox(None) is None
def test_frame_world_size():
assert tl.frame_world_size(_frame((0, 0, 100, 50))) == (100.0, 50.0)
assert tl.frame_world_size(_frame((0, 0, 0, 0))) == (0.0, 0.0)
# --- frame_scale:三条路径 --------------------------------------------------
def test_frame_scale_prefers_sheet_scale():
assert tl.frame_scale(_frame((0, 0, 100, 50), sheet_scale=100.0)) == 100.0
def test_frame_scale_derived_landscape():
# A0 横放:世界 118900 x 84100 → 118900/1189 = 100
f = _frame((0, 0, 118900, 84100), sheet_size="A0")
assert tl.frame_scale(f) == 100.0
def test_frame_scale_derived_portrait():
# A0 纵放:世界宽 ↔ 纸高 841
f = _frame((0, 0, 84100, 118900), sheet_size="A0")
assert tl.frame_scale(f) == 100.0
def test_frame_scale_falls_back_to_default():
assert tl.frame_scale(_frame((0, 0, 100, 50), sheet_size="ZZ"), default=7.0) == 7.0
assert tl.frame_scale(_frame((0, 0, 0, 0)), default=7.0) == 7.0
assert tl.frame_scale(None, default=7.0) == 7.0
# --- effective_px / world_per_px -------------------------------------------
def test_effective_px_no_resize_when_small():
assert tl.effective_px((800, 600), 1280) == (800, 600)
def test_effective_px_downsamples_long_edge():
# 实测 CAD 绘图区 2110x462 → 长边缩到 1280
assert tl.effective_px((2110, 462), 1280) == (1280, 280)
def test_effective_px_invalid():
assert tl.effective_px(None, 1280) == (0, 0)
assert tl.effective_px((0, 100), 1280) == (0, 0)
assert tl.effective_px((100, 100), 0) == (100, 100) # 0 = 不缩放
def test_world_per_px():
# 1:100、纸面 2mm、VLM 需 10px → 每像素 20 图形单位
assert tl.world_per_px(100.0, 2.0, 10.0) == 20.0
assert tl.world_per_px(0, 2.0, 10.0) == 0.0
assert tl.world_per_px(100.0, 2.0, 0) == 0.0
# --- is_text_type -----------------------------------------------------------
def test_is_text_type_standard():
for t in ("TEXT", "MTEXT", "ATTRIB", "ATTDEF", "DIMENSION", "text"):
assert tl.is_text_type(t) is True
def test_is_text_type_custom_keywords():
for t in ("AECDLABEL", "AECDDIMTEXT", "MYNOTE", "PARTTAG", "ANNOTATION", "TITLEBLK"):
assert tl.is_text_type(t) is True
def test_is_text_type_negative():
for t in ("AECDBEWPIPE", "LINE", "LWPOLYLINE", "INSERT", "HATCH", "", None, 123):
assert tl.is_text_type(t) is False
# --- 相交 / 文字簇标记 -------------------------------------------------------
def test_boxes_intersect():
assert tl.boxes_intersect((0, 0, 10, 10), (5, 5, 15, 15)) is True
assert tl.boxes_intersect((0, 0, 10, 10), (10, 10, 20, 20)) is True # 接触算相交
assert tl.boxes_intersect((0, 0, 10, 10), (11, 11, 20, 20)) is False
assert tl.boxes_intersect(None, (0, 0, 1, 1)) is False
def test_cluster_text_flags():
clusters = [_cluster((0, 0, 10, 10)), _cluster((100, 100, 110, 110))]
flags = tl.cluster_text_flags(clusters, [(5, 5, 6, 6)])
assert flags == [True, False]
assert tl.cluster_text_flags(clusters, []) == [False, False]
assert tl.cluster_text_flags([], [(0, 0, 1, 1)]) == []
# --- tiles_for_box / subdivide ---------------------------------------------
def test_tiles_for_box_fits_single():
assert tl.tiles_for_box((0, 0, 1000, 500), 1000, 500) == (1, 1)
def test_tiles_for_box_dense():
assert tl.tiles_for_box((0, 0, 2000, 1000), 1000, 500) == (2, 2)
def test_tiles_for_box_overlap_increases_count():
assert tl.tiles_for_box((0, 0, 1000, 500), 1000, 500, overlap=0.1) == (2, 2)
def test_tiles_for_box_invalid():
assert tl.tiles_for_box(None, 10, 10) == (1, 1)
assert tl.tiles_for_box((0, 0, 0, 0), 10, 10) == (1, 1)
assert tl.tiles_for_box((0, 0, 10, 10), 0, 10) == (1, 1)
def test_subdivide_row_major_top_first():
wins = tl.subdivide((0, 0, 100, 100), 2, 2)
assert len(wins) == 4
assert wins[0] == {"min_x": 0.0, "min_y": 50.0, "max_x": 50.0, "max_y": 100.0}
assert wins[1] == {"min_x": 50.0, "min_y": 50.0, "max_x": 100.0, "max_y": 100.0}
assert wins[2] == {"min_x": 0.0, "min_y": 0.0, "max_x": 50.0, "max_y": 50.0}
assert wins[3] == {"min_x": 50.0, "min_y": 0.0, "max_x": 100.0, "max_y": 50.0}
def test_subdivide_overlap_expands_then_clamps():
wins = tl.subdivide((0, 0, 100, 100), 2, 2, overlap=0.1, frame_win=(0, 0, 100, 100))
# 每格 50x50,外扩 5 → 左上格:x 0..55(左被夹到 0),y 45..100(上被夹到 100)
assert wins[0] == {"min_x": 0.0, "min_y": 45.0, "max_x": 55.0, "max_y": 100.0}
# --- plan_regions -----------------------------------------------------------
def test_plan_regions_sparse_single_tile():
frame = _frame((0, 0, 1000, 500))
clusters = [_cluster((100, 100, 400, 300))]
wins = tl.plan_regions(frame, clusters, s_line=10.0, view_px=(100, 50))
assert len(wins) == 1
assert wins[0]["min_x"] <= 100 and wins[0]["max_x"] >= 400
def test_plan_regions_dense_splits():
frame = _frame((0, 0, 2000, 1000))
clusters = [_cluster((0, 0, 2000, 1000))]
wins = tl.plan_regions(frame, clusters, s_line=10.0, view_px=(100, 50))
assert len(wins) == 4 # 2x2
def test_plan_regions_text_uses_finer_scale():
frame = _frame((0, 0, 1000, 500))
clusters = [_cluster((0, 0, 1000, 500))]
line_only = tl.plan_regions(frame, clusters, s_line=10.0, view_px=(100, 50))
text = tl.plan_regions(frame, clusters, text_clusters={0}, s_text=5.0,
s_line=10.0, view_px=(100, 50))
assert len(line_only) == 1
assert len(text) == 4 # 半径减半 → 2x2
def test_plan_regions_text_cluster_prioritized_on_truncation():
frame = _frame((0, 0, 4000, 2000))
clusters = [_cluster((0, 0, 2000, 1000), count=99), # 非文字,排前面(输入序)
_cluster((2000, 1000, 4000, 2000), count=1)] # 文字
wins = tl.plan_regions(frame, clusters, text_clusters={1},
s_text=10.0, s_line=10.0, view_px=(100, 50), max_tiles=2)
assert len(wins) == 2
# 文字簇(下标 1,位于右上)应优先出图
assert wins[0]["min_x"] >= 2000 and wins[0]["min_y"] >= 1000
def test_plan_regions_keep_filters_empty_tiles():
"""`keep` 谓词按占用情况剔除空瓦片(L 形分布时省下大片空白)。"""
frame = _frame((0, 0, 2000, 1000))
clusters = [_cluster((0, 0, 2000, 1000))]
assert len(tl.plan_regions(frame, clusters, s_line=10.0, view_px=(100, 50))) == 4
def keep_top(win):
return win["min_y"] >= 500 # 只保留上半(row 0)
wins = tl.plan_regions(frame, clusters, s_line=10.0, view_px=(100, 50),
keep=keep_top)
assert len(wins) == 2
assert all(w["min_y"] >= 500 for w in wins)
def test_plan_regions_degrades():
frame = _frame((0, 0, 1000, 500))
clusters = [_cluster((0, 0, 1000, 500))]
assert tl.plan_regions(frame, clusters, s_line=0.0, view_px=(100, 50)) == []
assert tl.plan_regions(frame, clusters, s_line=10.0, view_px=(0, 0)) == []
assert tl.plan_regions({"bbox": [0, 0, 0, 0]}, clusters, s_line=10.0,
view_px=(100, 50)) == []
assert tl.plan_regions(frame, [], s_line=10.0, view_px=(100, 50)) == []
def test_plan_regions_clamps_to_frame():
frame = _frame((0, 0, 1000, 500))
clusters = [_cluster((0, 0, 2000, 1000))] # 超出图框
wins = tl.plan_regions(frame, clusters, s_line=10.0, view_px=(100, 50), max_tiles=4)
for w in wins:
assert w["min_x"] >= 0 and w["max_x"] <= 1000
assert w["min_y"] >= 0 and w["max_y"] <= 500
# --- box / window 互转与可见范围 -------------------------------------------
def test_box_window_roundtrip():
box = (1.0, 2.0, 3.0, 4.0)
win = tl.box_to_window(box)
assert win == {"min_x": 1.0, "min_y": 2.0, "max_x": 3.0, "max_y": 4.0}
assert tl.window_to_box(win) == box
def test_box_to_window_invalid():
assert tl.box_to_window(None) is None
assert tl.box_to_window([1, 2, 3]) is None
def test_window_to_box_invalid():
assert tl.window_to_box(None) is None
assert tl.window_to_box({}) is None
assert tl.window_to_box({"min_x": 1, "min_y": 2}) is None
def test_expand_box_by_fraction():
assert tl.expand_box((0, 0, 100, 50), 0.05) == pytest.approx((-5.0, -2.5, 105.0, 52.5))
assert tl.expand_box((0, 0, 100, 50), 0.0) == (0.0, 0.0, 100.0, 50.0)
assert tl.expand_box(None, 0.1) is None
# 非法 frac 视为 0(不抛异常)
assert tl.expand_box((0, 0, 10, 10), "x") == (0.0, 0.0, 10.0, 10.0)
def test_visible_box_widens_to_viewport_aspect():
# 1000x500 的窗口在 1280x280(约 4.57:1)视口下等比适配 → 横向露出更多,纵向刚好贴合
vis = tl.visible_box((0, 0, 1000, 500), (1280, 280), 0.0)
assert vis[0] < 0 and vis[2] > 1000
assert vis[1] == pytest.approx(0.0)
assert vis[3] == pytest.approx(500.0)
assert (vis[0] + vis[2]) / 2 == pytest.approx(500.0) # 中心不变
assert (vis[1] + vis[3]) / 2 == pytest.approx(250.0)
def test_visible_box_tall_window_is_fit_by_height():
# 100x1000(很窄很高)在 1280x280 视口下按**高**适配 → 纵向刚好贴合,横向大幅露出
vis = tl.visible_box((0, 0, 100, 1000), (1280, 280), 0.0)
assert vis[1] == pytest.approx(0.0) and vis[3] == pytest.approx(1000.0)
assert vis[0] < 0 and vis[2] > 100
assert (vis[0] + vis[2]) / 2 == pytest.approx(50.0) # 中心不变
def test_visible_box_without_view_px_falls_back_to_margin():
assert tl.visible_box((0, 0, 100, 50), None, 0.1) == pytest.approx(
(-10.0, -5.0, 110.0, 55.0)
)
assert tl.visible_box((0, 0, 100, 50), (0, 0), 0.0) == (0.0, 0.0, 100.0, 50.0)
def test_visible_box_invalid_box():
assert tl.visible_box(None, (100, 100), 0.1) is None