2026-10-15 23:03:27 +08:00
|
|
|
|
"""`dwg_review.tiling` 单元测试(纯函数,不依赖 CAD / LLM / Pillow)。"""
|
|
|
|
|
|
|
2026-10-16 19:15:32 +08:00
|
|
|
|
import pytest
|
|
|
|
|
|
|
2026-10-15 23:03:27 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-10-15 23:22:15 +08:00
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-10-15 23:03:27 +08:00
|
|
|
|
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
|
2026-10-16 19:15:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# --- 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
|