/* ============================================================================= * common_util/common_util_structure_station.ts * 구조물 · 관 기준 측점 = 정수 m(반올림 · .5 는 올림) · 노선 끝 안쪽(요구 167 · 272). * * 측점 값(측점 + 거리 · 누가거리)에 소수를 안 씀 — 일반 측점(격자)과 같은 m 면 서버 측점 만들기가 * 한 측점으로 합침(구조물 측점이 위). 234-1 의 「일반 측점 1 m 안 붙기」는 뺌(그냥 정수 · 사용자 167). * 파이썬 짝 = `common_util/common_util_structure_station.py`(서버 자동 배치 · 노선 재투영). * 거울 시험 = `resources/tester/test_234_1_structure_station.py`. * 일반 측점 = 격자 · 시종점(구조물 측점 kind "irregular" 은 뺌) — 끝(노선 길이)을 넘지 않게 자르는 데만 · * 상세를 읽을 때 한 번 둔다. * * 측점 한 벌(272) — 측점을 심는 관 · 구조물(`plantedChainages`) = 서버 `resolve_extra_stations` * (B05_Profile_Engine_Sections.py) 짝 · 같은 m 면 측점 하나 · 구조물이 위(`oneStationList`) = 서버 * `_merge_irregular_into_longitudinal`(B05_Profile_Router_Confirm.py) 짝. 거울 시험 = * `resources/tester/test_272_1_station_list.py`. 종단 그래프 · 3D · 계획선 측점 줄(표 · ▲▼)이 * 이 목록 하나를 씀. * ========================================================================== */ const EPS = 1e-6; /** 같은 측점으로 볼 거리(m) — 서버 측점 만들기의 격자 붙임 폭(`Engine_Sections_Core`)과 같음. */ export const SAME_STATION_M = 0.005; let general: number[] = []; /** 일반 측점 목록을 둔다 — 횡단 상세를 읽을 때마다(B05 · B06 한 노선). */ export function setGeneralStations( stations: ReadonlyArray<{ chainage_m: number; kind?: string }> | null | undefined, ): void { general = (stations ?? []) .filter((station) => station.kind !== "irregular") .map((station) => station.chainage_m); } export const generalStations = (): readonly number[] => general; /** 기준 측점 = 정수 m(.5 는 올림) · 0 ~ 노선 끝(소수 끝은 그 아래 정수) 안. 두 번 맞춰도 같다. */ export function snapStructureStation( chainageM: number, stations: readonly number[] = general, ): number { if (!Number.isFinite(chainageM)) return chainageM; const end = stations.length ? Math.max(...stations) : Infinity; const whole = Math.floor(chainageM + 0.5); return Math.max(0, Math.min(whole, Math.floor(end + EPS))); } /** 방향키 한 걸음(1 m) — 맞춘 값이 제자리면 한 걸음 더 · 노선 밖이면 제자리. */ export function stepStructureStation( fromM: number, direction: 1 | -1, stations: readonly number[] = general, ): number { for (let steps = 1; steps <= 3; steps += 1) { const next = snapStructureStation(fromM + direction * steps, stations); if ((next - fromM) * direction > EPS) return next; } return fromM; } /** 이미 정수 m 인가(옛 소수 자료를 옮길 것을 가름). */ export const isSnappedStation = ( chainageM: number, stations: readonly number[] = general, ): boolean => Math.abs(snapStructureStation(chainageM, stations) - chainageM) < EPS; /* ── 측점을 심는 자리(272 · 서버 `resolve_extra_stations` · `is_station_planting_type` 짝) ── */ export interface PlantingPipe { chainage_m: number; facility?: string | null; start_m?: number | null; end_m?: number | null; } export interface PlantingStructure { type_id: string; chainage_m?: number | null; start_m?: number | null; end_m?: number | null; } export interface PlantingType { type_id: string; group: string; placement: string; managed_by?: string | null; } /** 측점 하나 — 구간형(독립 기슭막이 · C · D군)은 시작 · 기준 · 끝 셋, 그 밖은 기준 하나. */ export interface StationMark { chainage_m: number; role: "start" | "anchor" | "end"; } function marksOf( start: number | null | undefined, anchor: number | null | undefined, end: number | null | undefined, interval: boolean, ): StationMark[] { const marks: Array<[StationMark["role"], number | null | undefined]> = interval ? [ ["start", start], ["anchor", anchor], ["end", end], ] : [["anchor", anchor]]; return marks.flatMap(([role, at]) => typeof at === "number" && Number.isFinite(at) ? [{ chainage_m: at, role }] : [], ); } /** 관 시설 하나가 심는 측점 — 독립 기슭막이 = 시작 · 기준 · 끝 · 그 밖(물넘이포장 포함 · 286-1) = 기준. */ export function pipeStationMarks(pipe: PlantingPipe): StationMark[] { const facility = pipe.facility ?? "pipe"; return marksOf(pipe.start_m, pipe.chainage_m, pipe.end_m, facility === "revetment"); } /** 구조물 하나가 심는 측점 — A군 = 기준 · C · D군 구간형 = 시작 · 기준 · 끝 · 다른 정본 소관 · 그 밖 = 없음. */ export function structureStationMarks( structure: PlantingStructure, type: PlantingType | undefined, ): StationMark[] { if (!type || type.managed_by) return []; const interval = type.placement === "interval"; if (type.group !== "A" && !(interval && (type.group === "C" || type.group === "D"))) return []; const anchor = structure.chainage_m ?? structure.start_m; return marksOf(structure.start_m, anchor, structure.end_m, interval); } /** 측점을 심는 자리 전부(오름차순 · 겹침 그대로) — 서버 `resolve_extra_stations` 의 누가거리. */ export function plantedChainages( pipes: ReadonlyArray, structures: ReadonlyArray, types: ReadonlyArray, ): number[] { const byId = new Map(types.map((type) => [type.type_id, type])); return [ ...pipes.flatMap(pipeStationMarks), ...structures.flatMap((structure) => structureStationMarks(structure, byId.get(structure.type_id)), ), ] .map((mark) => mark.chainage_m) .sort((a, b) => a - b); } /* ── 측점 한 벌(272 · 서버 `_merge_irregular_into_longitudinal` 짝) ── */ export interface ListedStation { chainage_m: number; kind?: string; /** 구조물 측점이 얹힌 격자 자리 — 구조물이 떠나면 일반 측점으로 돌아옴(서버 표시). */ grid?: boolean; structure?: string; } /** * 측점 한 벌 = 서버 측점 중 일반 측점(격자 · 시종점 · 구조물이 떠난 격자 자리는 글 떼고 일반으로) + * 지금 심긴 구조물 측점(`planted`). 같은 m 면 구조물 측점 하나(일반은 숨김) · 서버의 구조물 측점은 * 지금 목록이 갈음(옮긴 관의 옛 자리가 안 남음). */ export function oneStationList( server: ReadonlyArray, planted: ReadonlyArray

, ): Array { const general = server .filter((station) => station.kind !== "irregular" || station.grid) .filter((station) => planted.every((mark) => Math.abs(mark.chainage_m - station.chainage_m) >= SAME_STATION_M), ) .map((station) => { if (!station.grid && station.structure === undefined) return station; const { grid: _grid, structure: _structure, ...rest } = station; return { ...rest, kind: station.kind === "irregular" ? "regular" : station.kind } as S; }); return [...general, ...planted].sort((a, b) => a.chainage_m - b.chainage_m); } /** * 사라진 구조물 측점(`gone`)의 계획선 꼭짓점을 새 측점(`born`)으로 옮기거나 지운다 — 서버 * `_follow_moved_stations`(B05_Profile_Router_Confirm.py) 짝: 0.006m 안 반올림 차이 · 옛 소수 측점의 * 정수 m 맞춤 자리(272)면 높이 그대로 옮기고, 나머지는 지움(측점 없는 꼭짓점이 계획선을 꺾지 않게). */ export function followMovedNodes( nodes: ReadonlyArray, gone: ReadonlyArray, born: ReadonlyArray, ): N[] { const target = (old: number): number | null => { const near = born.find((value) => Math.abs(value - old) < 0.006); if (near !== undefined) return near; const snapped = Math.floor(old + 0.5); return born.some((value) => Math.abs(value - snapped) < EPS) ? snapped : null; }; return nodes.flatMap((node) => { const old = gone.find((value) => Math.abs(value - node.chainage_m) < 0.006); if (old === undefined) return [node]; const to = target(old); return to === null ? [] : [{ ...node, chainage_m: to }]; }); }