perf(863-3): 등고선 뽑기 한 줄 통째 계산

- 등고선 줄마다 점 하나씩 돌던 쌍선형 보간 · 무효 칸 판정을 배열 한 번으로 · 끝수는 파이썬 round 그대로 · 무효 점에서 끊는 규칙 같음
- 실측(검증_sub2 격자 셋 · 간격 1 · 2 · 5 m · 화면 중심 옮김 포함) — 결과 JSON 글자까지 같음 · 1 m 26.3 → 3.8 초 · 21.9 → 5.0 초(IMPLICIT 격자)
- 시험 test_863_3_contour_vector — 원뿔 등고선 좌표 · 끊김 · 중심 옮김

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 14:26:34 +09:00
co-authored by Claude Opus 5.5
parent 8d79797509
commit e316c6402a
2 changed files with 86 additions and 47 deletions
+37 -47
View File
@@ -57,62 +57,52 @@ def extract_contours_from_grid(
cx, cy, cz = scene_center if scene_center is not None else (0.0, 0.0, 0.0)
x_coords = np.asarray(x_coords)
y_coords = np.asarray(y_coords)
for level in levels:
level_value = (
round(float(level - cz), 3) if scene_center is not None else round(float(level), 3)
)
for contour in measure.find_contours(z_grid_masked, level):
current_segment: list[list[float]] = []
for y_idx, x_idx in contour:
x_idx_c = np.clip(x_idx, 0, len(x_coords) - 1)
y_idx_c = np.clip(y_idx, 0, len(y_coords) - 1)
x_0, x_1 = int(np.floor(x_idx_c)), int(np.ceil(x_idx_c))
y_0, y_1 = int(np.floor(y_idx_c)), int(np.ceil(y_idx_c))
is_valid = True
if valid_mask is not None and not (
# 줄 전체를 한 번에 — 점마다 쓰던 식과 같은 float64 연산 · 끝수는 파이썬 round 그대로
x_idx_c = np.clip(contour[:, 1], 0, len(x_coords) - 1)
y_idx_c = np.clip(contour[:, 0], 0, len(y_coords) - 1)
x_0, x_1 = np.floor(x_idx_c).astype(np.intp), np.ceil(x_idx_c).astype(np.intp)
y_0, y_1 = np.floor(y_idx_c).astype(np.intp), np.ceil(y_idx_c).astype(np.intp)
if valid_mask is not None:
valid = np.asarray(
valid_mask[y_0, x_0]
and valid_mask[y_0, x_1]
and valid_mask[y_1, x_0]
and valid_mask[y_1, x_1]
):
is_valid = False
if not is_valid:
if len(current_segment) >= 2:
mid_idx = len(current_segment) // 2
contours_geojson_list.append(
{
"level": float(level),
"coordinates": current_segment,
"label_position": current_segment[mid_idx],
}
)
current_segment = []
& valid_mask[y_0, x_1]
& valid_mask[y_1, x_0]
& valid_mask[y_1, x_1],
dtype=bool,
)
else:
valid = np.ones(len(contour), dtype=bool)
tx = x_idx_c - x_0
ty = y_idx_c - y_0
x_val = (1.0 - tx) * x_coords[x_0] + tx * x_coords[x_1]
y_val = (1.0 - ty) * y_coords[y_0] + ty * y_coords[y_1]
if scene_center is not None:
first, second = x_val - cx, -(y_val - cy)
else:
first, second = x_val, y_val
# 무효 점에서 끊긴 유효 구간(점 둘 이상)마다 한 줄
edges = np.flatnonzero(np.diff(np.concatenate(([0], valid.astype(np.int8), [0]))))
for start, stop in zip(edges[::2].tolist(), edges[1::2].tolist()):
if stop - start < 2:
continue
tx = x_idx_c - x_0
ty = y_idx_c - y_0
x_val = (1.0 - tx) * x_coords[x_0] + tx * x_coords[x_1]
y_val = (1.0 - ty) * y_coords[y_0] + ty * y_coords[y_1]
a = [round(v, 3) for v in first[start:stop].tolist()]
b = [round(v, 3) for v in second[start:stop].tolist()]
if scene_center is not None:
current_segment.append(
[
round(float(x_val - cx), 3),
round(float(level - cz), 3),
round(float(-(y_val - cy)), 3),
]
)
segment = [[p, level_value, q] for p, q in zip(a, b)]
else:
current_segment.append(
[round(float(x_val), 3), round(float(y_val), 3), round(float(level), 3)]
)
if len(current_segment) >= 2:
mid_idx = len(current_segment) // 2
segment = [[p, q, level_value] for p, q in zip(a, b)]
contours_geojson_list.append(
{
"level": float(level),
"coordinates": current_segment,
"label_position": current_segment[mid_idx],
"coordinates": segment,
"label_position": segment[len(segment) // 2],
}
)
@@ -0,0 +1,49 @@
"""863-3 등고선 뽑기(한 줄 통째 계산) — 좌표 · 끝수 · 무효 칸에서 끊김 · 화면 중심 옮김."""
from __future__ import annotations
import numpy as np
from B04_PreProcess.B04_PreProcess_Engine_Contour import extract_contours_from_grid
XS = np.linspace(1000.0, 1299.0, 300)
YS = np.linspace(2000.0, 2199.0, 200)
YY, XX = np.mgrid[0:200, 0:300].astype(float)
CONE = np.hypot(XX - 150, YY - 100) * 0.5 # 원뿔 — 등고선 = 중심 (1150, 2100) 둘레 원
def test_원뿔_등고선은_원() -> None:
lines = extract_contours_from_grid(XS, YS, CONE, None, 10.0)
assert {line["level"] for line in lines} == set(np.arange(10.0, 91.0, 10.0).tolist())
for line in lines:
pts = np.array(line["coordinates"])
radius = np.hypot(pts[:, 0] - 1150.0, pts[:, 1] - 2100.0)
assert np.allclose(radius, line["level"] / 0.5, atol=0.3)
assert np.all(pts[:, 2] == line["level"])
assert all(round(v, 3) == v for v in pts[:, :2].ravel().tolist())
assert line["label_position"] == line["coordinates"][len(line["coordinates"]) // 2]
def test_무효_칸에서_끊김() -> None:
mask = np.ones(CONE.shape, dtype=bool)
mask[95:106, :] = False # 가로 띠 — 원이 위 · 아래 두 줄로 갈림
whole = extract_contours_from_grid(XS, YS, CONE, None, 20.0)
cut = extract_contours_from_grid(XS, YS, CONE, mask, 20.0)
full = [line for line in whole if line["level"] <= 40.0] # 반지름 < 100 칸 = 격자 안 온 원
assert len(full) == 2
assert len([line for line in cut if line["level"] <= 40.0]) == 2 * len(full)
for line in cut:
ys = np.array(line["coordinates"])[:, 1]
assert np.all(ys <= 2095.0) or np.all(ys >= 2105.0)
def test_화면_중심_옮김() -> None:
plain = extract_contours_from_grid(XS, YS, CONE, None, 20.0)
shifted = extract_contours_from_grid(
XS, YS, CONE, None, 20.0, scene_center=(1150.0, 2100.0, 5.0)
)
for a, b in zip(plain, shifted):
for (x, y, z), (sx, sz, sy) in zip(a["coordinates"], b["coordinates"]):
assert abs(sx - (x - 1150.0)) < 2e-3
assert abs(sy + (y - 2100.0)) < 2e-3
assert sz == round(z - 5.0, 3)