Files
Aislo/A00_Common/design/logic/design_pipeline_stations.ts
T

118 lines
4.8 KiB
TypeScript

/* =============================================================================
* A00_Common/design/logic/design_pipeline_stations.ts
* 계산 묶음 stations 단계(PLAN 346-1) — 측점 계산은 `B05_Profile/logic/B05_Profile_Sections_Stations.ts`
* (서버 `generate_sections` 짝) · 여기는 단계 꼴 · 범위 밖 앞 값 재사용만.
* ========================================================================== */
import {
buildStations,
REUSE_MARGIN_M,
type StationsOptions,
} from "../../../B05_Profile/logic/B05_Profile_Sections_Stations";
import type {
PlanPipe,
PlanStructure,
PlanType,
} from "../../../common_util/common_util_structure_station";
import type {
PipelineRunRecord,
PipelineStation,
RouteDraft,
StageContext,
StageFn,
} from "./design_pipeline_types";
import type { SectionDetailResponse } from "../../../B06_Section/B06_Section_Api_Fetch";
/** 범위 밖 측점은 앞 값(같은 열쇠 · 같은 자리)을 그대로 — 뒤 단계가 같은 객체로 「안 바뀜」을 앎. */
export function reuseOutside(
next: PipelineStation[],
prev: ReadonlyArray<PipelineStation> | null,
range: { fromM: number; toM: number } | null,
): PipelineStation[] {
if (!prev || !range) return next;
const old = new Map(prev.map((station) => [station.key, station]));
return next.map((station) => {
const inside =
station.chainageM >= range.fromM - REUSE_MARGIN_M &&
station.chainageM <= range.toM + REUSE_MARGIN_M;
const before = old.get(station.key);
return !inside && before && Math.abs(before.chainageM - station.chainageM) < 1e-9
? before
: station;
});
}
/** 저장 노선의 측점 스냅 설정(`RoutePlanResponse` 칸). */
export interface SavedSnap {
station_snap?: boolean;
station_snap_step?: number | null;
}
/** 측점 스냅 설정 — 노선 초안이 있으면 그 값 · 없으면 저장 노선 값(441 · 저장 스냅 노선 측점 어긋남).
* 간격 = 10 만 그 값 · 옛 초안 0.1 · 1 m 등 = null — 짝 `B05_Profile_Engine_Sections.py` load_station_snap_step(505-2). */
export function snapOptions(
draft: RouteDraft | null | undefined,
saved: SavedSnap | null | undefined,
): Pick<StationsOptions, "stationSnap" | "stationSnapStep"> {
const step = draft ? draft.stationSnapStep : saved?.station_snap_step;
return {
stationSnap: draft ? draft.stationSnap : (saved?.station_snap ?? false),
stationSnapStep: step === 10 ? 10 : null,
};
}
/** `buildStations` 인자 한 벌 — 화면 셈 · 서버 셈(계산 자리 「서버」)이 같은 값을 받음. */
export type StationsArgs = Parameters<typeof buildStations>;
/** stations 단계 — 입력 관 · 구조물(follow 뒤 누가거리) · 그린 노선 · 곡선표 · 스냅(초안 · 없으면 저장 노선).
* `remote` = 계산 자리 「서버」면 서버 Node 값(같은 `buildStations` · 511-B B-2) · null = 화면 셈 · 서버가
* 못 내면(끊김 · 실패) 화면 셈. */
export function createStationsStage(
options: Omit<StationsOptions, "stationSnap" | "stationSnapStep" | "types"> & {
types: () => ReadonlyArray<PlanType>;
saved?: () => SavedSnap | null;
remote?: (
args: StationsArgs,
signal: AbortSignal,
) => Promise<PipelineStation[] | undefined> | null;
},
): StageFn<"stations"> {
const { saved, remote, ...rest } = options;
return (ctx: StageContext<"stations">) => {
const route = ctx.out.route;
if (!route) return [];
const args: StationsArgs = [
route.line,
route.curves,
(ctx.inputs.pipes as PlanPipe[] | null) ?? [],
(ctx.inputs.structures as PlanStructure[] | null) ?? [],
{
...rest,
types: options.types(),
...snapOptions(ctx.inputs.route, saved?.()),
plus: ctx.inputs.plusStations ?? undefined, // follow 가 옮긴 추가 측점(459 · 가게보다 먼저)
},
];
const done = (next: PipelineStation[] | undefined) =>
reuseOutside(next ?? buildStations(...args), ctx.prev, ctx.range);
const asked = remote?.(args, ctx.signal);
return asked ? asked.then(done) : done(undefined); // 화면 셈 = 동기 그대로
};
}
/** 상세 측점 대 묶음 측점(1 mm) — 같으면 null · 다르면 첫 다른 자리 · 개수(묶음 기록 `restoreDiff`). */
export function stationDiff(
detail: SectionDetailResponse,
stations: ReadonlyArray<PipelineStation>,
): NonNullable<PipelineRunRecord["restoreDiff"]> | null {
const saved = detail.cross_sections.map((section) => section.chainage_m);
const n = Math.max(saved.length, stations.length);
for (let i = 0; i < n; i += 1) {
const pipelineM = stations[i]?.chainageM ?? null;
if (saved[i] !== undefined && pipelineM !== null && Math.abs(saved[i] - pipelineM) < 1e-3)
continue;
return { savedM: saved[i] ?? null, pipelineM, saved: saved.length, pipeline: stations.length };
}
return null;
}