Files
Aislo/B06_Section/logic/B06_Section_Station_Swap.ts
T
eomsangdonandClaude Opus 5.5 35758ac6ea fix(428 관 옮김 횡단 한 값): 옮긴 시설 측점 기본 설계 · 다시 셈 미교차 넓힘 · 관 PUT 끝 측점 세움 · 따라가기만 한 관 서버 누가거리
- 측점 갈아끼움 — 상단측 새 지반으로 · 0.5 m 넘게 옮긴 관 · 구조물 측점 = 기본 설계 + 사용자가 만진 칸
- 노선 초안 동안 관 길이 = 계산 줄 새 노선 상세
- 저장 노선 다시 셈 창구에 미교차 넓힘(서버 extend_unclosed 짝 · 격자 유무 지문)
- 관 PUT 끝 서버가 측점 다시 세움 · GET 관 지점에 x · y
- rebasePipeDraft — 땅 위 자리 = 정본인 관은 서버 누가거리
- 「측점이 없는 구조물」 알림 — 관 · 구조물 초안이 비면 저장 끝난 뒤 다시 봄
- 시험 test_428_pipe_move_mirror(옮긴 관 관 길이 브라우저 대 서버) · 416 출처 글 맞춤

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VmC34fsxxbweEG3RUjs3Mn
2026-10-18 15:53:03 +09:00

226 lines
9.8 KiB
TypeScript

/* =============================================================================
* B06_Section_Station_Swap.ts
* 측점 수가 바뀔 때 횡단 목록 갈아끼움(PLAN 346-2 4파) — 계산 묶음 측점(`PipelineStation[]`) → 새 상세 객체.
*
* · 같은 측점(열쇠 · 없으면 같은 자리 같은 종류)은 앞 횡단을 이어 받음 — 설계 · 구조물 제원 · 이름 그대로 ·
* 자리(누가거리 · 좌표 · 방위 · 확폭)와 지반 표본(채워 왔으면)만 새것.
* · 새 측점 = 지반 표본 + 기본 설계(계획고 0 → 낡음 → 브라우저 다시 셈이 곧 채움).
* · 상단측 = 새 지반 표본으로 다시(단면 유형 기본값 재료 · 서버 횡단 파일 `uphill_side` 짝 · 428).
* · 0.5 m 넘게 옮긴 관 · 구조물 측점 = 기본 설계 + 사용자가 만진 칸만 — 서버가 그 행을 지우고 기본 설계에
* [저장] 수정분을 얹는 것과 같음(`sync_cross_section_rows` · `compute_default_designs` · 428).
* · 빠진 측점 = 목록에서 뺌(세션 고침은 열쇠로 남음 · 노선이 바뀐 때만 「노선 밖 고침」 · 부르는 쪽).
* · 옛 상세는 그대로 둠 — 새 객체를 돌려줌(쥔 쪽이 갈아끼움).
*
* ⚠ 파이썬·TS 짝 — `B05_Profile_Engine_Sections_Core.py` `format_station` ↔ `formatStation`.
* 거울 시험 `resources/tester/test_346_2_station_swap.py`.
* 순수 계산(DOM · fetch · 세션 없음).
* ========================================================================== */
import type { PipelineStation } from "../../A00_Common/design/logic/design_pipeline_types";
import type { CrossSection, SectionDetailResponse, SectionStation } from "../B06_Section_Api_Fetch";
import { uphillSide } from "../../B05_Profile/logic/B05_Profile_Pipeline_Detail";
interface Sample {
offset_m: number;
elevation_m: number | null;
valid: boolean;
}
/** 지반 표본(중심 → 바깥 · 좌 +) → 엔진 표본(오프셋 오름차순 · 우 −). */
export function groundSamples(station: PipelineStation): Sample[] {
const ground = station.ground;
if (!ground) return [];
const point = (offset: number, z: number | null): Sample => ({
offset_m: offset,
elevation_m: z,
valid: z !== null && Number.isFinite(z),
});
const right = ground.right.map((s) => point(-s.offsetM, s.z));
const left = ground.left.map((s) => point(s.offsetM, s.z));
const out = [...right, ...left];
if (ground.centerZ !== null && !out.some((s) => Math.abs(s.offset_m) < 1e-9))
out.push(point(0, ground.centerZ));
return out.sort((a, b) => a.offset_m - b.offset_m);
}
/** 짝: `format_station` — `STA.k+mmm.mmm`. */
export function formatStation(chainageM: number): string {
const at = Math.max(chainageM, 0);
const km = Math.floor(at / 1000);
const rest = (at - km * 1000).toFixed(3).padStart(7, "0");
return `STA.${km}+${rest}`;
}
/** 서버 측점 id 꼴(`station_000000484210` = 누가거리 mm 12자리). */
export const stationIdOf = (chainageM: number): string =>
`station_${String(Math.round(chainageM * 1000)).padStart(12, "0")}`;
/** 새 측점의 기본 설계 — 계획고 0 이라 늘 낡음 → 다시 셈이 세션 선택 · 기본값으로 채움 · 단면 유형 = 상단측 깎기. */
const freshDesign = (uphill: CrossSection["uphill_side"]) =>
({
design_elevation_m: 0,
ground_type: "ripping_rock",
section_mode: `${uphill ?? "left"}_cut`,
ditch_type: "standard",
}) as unknown as NonNullable<CrossSection["design"]>;
/** 서버가 행을 지우고 새로 세우는 옮김 폭(m) — `sync_cross_section_rows` 의 0.5 m 짝. */
const FACILITY_RESEED_M = 0.5;
/** 다시 셈 입력 중 기본 설계가 정하는 칸 — 옮긴 시설 측점은 버림(세션 고침은 열쇠째 옮겨 다시 얹힘). */
const AUTO_INPUT_KEYS = [
"ditch_side",
"rock_boundary_offset_m",
"two_stage_slope",
"ditch_choice",
"ditch_enabled",
];
/** 옮긴 시설 측점 설계 — 기본 설계 위에 앞 설계의 나머지(사용자가 만진 칸 · 결과 칸은 다시 셈이 갈음). */
function reseeded(
prior: CrossSection["design"],
uphill: CrossSection["uphill_side"],
): NonNullable<CrossSection["design"]> {
const design = { ...prior, ...freshDesign(uphill) } as Record<string, unknown>;
for (const key of AUTO_INPUT_KEYS) delete design[key];
return design as unknown as NonNullable<CrossSection["design"]>;
}
function kindOf(station: PipelineStation): SectionStation["kind"] {
if (station.kind === "end") return station.key.endsWith("EP") ? "ep" : "bp";
if (station.kind === "curve") return "curve";
return station.kind === "grid" ? "regular" : "irregular";
}
/** 같은 목록인가 — 열쇠 · 누가거리가 차례대로 같음(제자리 갱신이면 갈아끼우지 않음). */
export function sameStationList(
sections: ReadonlyArray<{ chainage_m: number }>,
oldKeys: ReadonlyArray<string>,
stations: ReadonlyArray<PipelineStation>,
): boolean {
return (
sections.length === stations.length &&
stations.every(
(station, index) =>
Math.abs(station.chainageM - sections[index].chainage_m) < 1e-6 &&
(oldKeys[index] === station.key || kindOf(station) === "regular"),
)
);
}
/** 조작값 맵 열쇠(`85.05` · `85.05:outlet` · `b85.05`)의 누가거리를 새 자리로 — 안 옮기는 열쇠는 그대로. */
export function renameChainageKeys<V>(
values: ReadonlyMap<string, V>,
moves: ReadonlyMap<string, string>,
): Map<string, V> {
const out = new Map<string, V>();
for (const [key, value] of values) {
const match = /^(b?)(-?\d+\.\d{2})(:.*)?$/.exec(key);
const to = match && moves.get(match[2]);
out.set(to ? `${match[1]}${to}${match[3] ?? ""}` : key, value);
}
return out;
}
export interface StationSwap {
detail: SectionDetailResponse;
added: string[];
removed: string[];
/** 이어 받은 측점 중 누가거리가 바뀐 것 — 옛 0.01 m 글 → 새 글(조작값 맵 옮김). */
moves: Map<string, string>;
/** 이어 받은 관 · 구조물 밖 측점 중 좌표가 0.01 m 넘게 움직인 것이 있나 — 노선이 바뀐 차례(「노선 밖 고침」 셈). */
moved: boolean;
}
/**
* 새 측점 목록으로 상세를 새로 짬. `oldKeys` = 지금 상세 측점마다 열쇠(같은 차례 · `stationKeysOf`) ·
* 열쇠가 안 맞으면 같은 자리(0.01 m) · 같은 종류의 앞 횡단을 씀.
*/
export function swapCrossSections(
detail: SectionDetailResponse,
stations: ReadonlyArray<PipelineStation>,
oldKeys: ReadonlyArray<string>,
intervalM: number | null,
): StationSwap {
const byKey = new Map(detail.cross_sections.map((section, i) => [oldKeys[i], section]));
const used = new Set<CrossSection>();
let moved = false;
const added: string[] = [];
const moves = new Map<string, string>();
const text = (chainageM: number) => chainageM.toFixed(2);
const priorOf = (station: PipelineStation): CrossSection | undefined => {
const hit = byKey.get(station.key);
if (hit && !used.has(hit)) return hit;
return detail.cross_sections.find(
(section) =>
!used.has(section) &&
section.kind === kindOf(station) &&
Math.abs(section.chainage_m - station.chainageM) < 0.01,
);
};
const longStations: SectionStation[] = [];
const cross_sections = stations.map((station): CrossSection => {
const prior = priorOf(station);
if (prior) used.add(prior);
else added.push(station.key);
// 노선이 바뀜 = 관 · 구조물 밖 측점이 움직임 — 시설만 옮기면 고침을 열쇠째 옮김(415 H1)
if (
prior &&
station.kind !== "pipe" &&
station.kind !== "structure" &&
Math.hypot(prior.center_x - station.x, prior.center_y - station.y) > 0.01
)
moved = true;
if (prior && text(prior.chainage_m) !== text(station.chainageM))
moves.set(text(prior.chainage_m), text(station.chainageM));
const kind = kindOf(station);
const onGrid =
kind === "irregular" &&
!!intervalM &&
Math.abs(station.chainageM / intervalM - Math.round(station.chainageM / intervalM)) < 1e-6;
const place = {
station_id: prior?.station_id ?? stationIdOf(station.chainageM),
chainage_m: station.chainageM,
label: formatStation(station.chainageM),
kind,
...(onGrid ? { grid: true } : {}),
center_z: station.ground?.centerZ ?? prior?.center_z ?? null,
azimuth_deg: station.azimuthDeg,
plan_radius_m: station.planRadiusM,
curve_outer_side: station.curveOuterSide,
curve_widening_m: station.curveWideningM,
center_x: station.x,
center_y: station.y,
frame: { left_xy: [station.left[0], station.left[1]] as [number, number] },
};
longStations.push({ ...place, ...(prior?.structure ? { structure: prior.structure } : {}) });
const samples = station.ground ? groundSamples(station) : (prior?.samples ?? []);
const uphill = station.ground ? uphillSide(station) : (prior?.uphill_side ?? null);
if (!prior) return { ...place, uphill_side: uphill, samples, design: freshDesign(uphill) };
const reseed =
(station.kind === "pipe" || station.kind === "structure") &&
Math.abs(prior.chainage_m - station.chainageM) > FACILITY_RESEED_M;
return {
...prior,
...place,
uphill_side: uphill,
samples,
...(reseed ? { design: reseeded(prior.design, uphill) } : {}),
};
});
const removed = detail.cross_sections
.map((section, i) => (used.has(section) ? null : oldKeys[i]))
.filter((key): key is string => key !== null);
const last = stations[stations.length - 1]?.chainageM ?? detail.longitudinal.length_m;
return {
detail: {
...detail,
longitudinal: { ...detail.longitudinal, stations: longStations, length_m: last },
cross_sections,
},
added,
removed,
moved,
moves,
};
}