Stage B2(CAD 实测标定)与收尾:
- 新增 verify_text_resolution.py:临时加字(字高 200 图形单位 = 1:100 纸面 2mm)+
平行线对,按目标像素换算 zoom 窗口 → 截图 → 问 VLM;测完 delete_entities 清理不保存。
- 标定结论:
* **可读下限与字体强相关**——离线合成(Consolas 抗锯齿填充字体)12px;
真实 CAD 单线体 .shx(描边字体)6px 即可读对(3/3,两轮)。
* 线条「1 条 vs 2 条」:2/3/4px 失败,5/8/12px 成功 → R_line ≈ 5px。
* **必须用 ACI 7 白字**:实测红字(ACI 1)12px 因对比度不足完全读不出。
- config 定稿:TEXT_READABLE_PX=8(折中)、LINE_READABLE_PX=6、
EVIDENCE_MAX_IMAGES=12(原 5 会把规划结果截到只剩 4 张瓦片)。
- 新增 heatmap.occupied_boxes:占用格的**世界 bbox**。
- tiling.plan_regions 新增 keep 谓词;evidence._plan_tiles 按占用格剔除**空瓦片**
(簇 bbox 是包围盒,L 形/斜向分布含大片空白,实测有效)。
- 测试:test_heatmap 补 occupied_boxes;test_tiling 补 keep 过滤;全量 286 项全绿。
- 真实图纸端到端验证:A0 图框出 12 张(1 整体 + 11 瓦片),单框审查 1m38s 通过。
- 记忆库同步:工具语义坑(type_filter 必须传列表 / layer_filter 是通配字符串 /
add_* 返回句柄在 created 键)、标定结论与默认值。
224 lines
8.5 KiB
Python
224 lines
8.5 KiB
Python
"""`dwg_review.tiling` 单元测试(纯函数,不依赖 CAD / LLM / Pillow)。"""
|
||
|
||
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
|