"""도엽등고선 → 표고 격자 보간 — 라플라스(거리 비례 초기값 + 이웃 평균 다듬기). 비교용 다섯 방식(TIN 둘 · TPS · ANUDEM형 · 다중해상도)은 걷음(608) — 확정 · 자동 = 라플라스. builder는 `(spec, burned, features, cell_m) -> (R, C) float32` 격자를 돌려준다. `burned`는 등고 라인이 구워진 격자(라인 셀 = 표고, 그 외 NaN)다. 폐합 링 안쪽 처리와 프리뷰·저장은 호출측(`_SheetSurface`)이 방식과 무관하게 똑같이 해 준다. """ from typing import Any, Callable import numpy as np # 화면 버튼에 쓰는 이름 — 키는 surface_models.generation_params.source_filter 접미사다. SHEET_METHOD_LABELS: dict[str, str] = { "laplace": "라플라스", } # ── 거리 비례 (라플라스 초기값) ───────────────────────────────────────────── def build_distance(spec: Any, burned: np.ndarray, features: Any, cell_m: float) -> np.ndarray: """가장 가까운 서로 다른 표고 두 라인 사이를 거리 비례로 나눈다. z = (L1·d2 + L2·d1) / (d1 + d2) 지도 제작의 고전적 손보간을 그대로 옮긴 것이다. 원뿔·능선(z=r) 형상을 정확히 재현하고 계단이 생기지 않는다. 표고별 거리장을 돌며 가장 작은 두 값을 추적하므로 L1≠L2가 보장된다. """ from scipy.ndimage import distance_transform_edt levels = np.unique(burned[np.isfinite(burned)]) if len(levels) < 2: return np.full(burned.shape, levels[0] if len(levels) else np.nan, dtype=np.float32) infinity = np.float32(np.inf) first_d = np.full(burned.shape, infinity, dtype=np.float32) first_z = np.zeros(burned.shape, dtype=np.float32) second_d = np.full(burned.shape, infinity, dtype=np.float32) second_z = np.zeros(burned.shape, dtype=np.float32) for level in levels: distance = distance_transform_edt(burned != level, sampling=cell_m).astype(np.float32) beats_first = distance < first_d second_d = np.where(beats_first, first_d, second_d) second_z = np.where(beats_first, first_z, second_z) first_d = np.where(beats_first, distance, first_d) first_z = np.where(beats_first, np.float32(level), first_z) beats_second = ~beats_first & (distance < second_d) second_d = np.where(beats_second, distance, second_d) second_z = np.where(beats_second, np.float32(level), second_z) total = first_d + second_d usable = np.isfinite(second_d) & (total > 1e-9) surface = first_z.astype(np.float32) surface[usable] = ( ( first_z[usable].astype(np.float64) * second_d[usable].astype(np.float64) + second_z[usable].astype(np.float64) * first_d[usable].astype(np.float64) ) / total[usable].astype(np.float64) ).astype(np.float32) return surface def relax_laplace(surface: np.ndarray, fixed: np.ndarray, iterations: int) -> None: """등고 라인을 고정한 채 이웃 평균으로 다듬는다 (in-place, red-black 순서).""" if iterations <= 0: return free = ~fixed & np.isfinite(surface) if not free.any(): return rows, cols = np.indices(surface.shape) red = free & (((rows + cols) & 1) == 0) black = free & ~red padded = np.zeros((surface.shape[0] + 2, surface.shape[1] + 2), dtype=np.float32) for _ in range(iterations): for colour in (red, black): padded[1:-1, 1:-1] = surface padded[0, 1:-1] = surface[0] padded[-1, 1:-1] = surface[-1] padded[1:-1, 0] = surface[:, 0] padded[1:-1, -1] = surface[:, -1] neighbours = ( padded[:-2, 1:-1] + padded[2:, 1:-1] + padded[1:-1, :-2] + padded[1:-1, 2:] ) * np.float32(0.25) surface[colour] = neighbours[colour] # ── 라플라스(조화) ───────────────────────────────────────────────────────────── def build_laplace(spec: Any, burned: np.ndarray, features: Any, cell_m: float) -> np.ndarray: """등고선을 경계값으로 두고 Δz=0을 푼다. 면이 매끈해지지만 z=r(원뿔·능선)은 조화함수가 아니라 마루가 조금 눌린다. """ surface = build_distance(spec, burned, features, cell_m) relax_laplace(surface, np.isfinite(burned), 400) return surface SHEET_METHOD_BUILDERS: dict[str, Callable[[Any, np.ndarray, Any, float], np.ndarray]] = { "laplace": build_laplace, }