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

84 lines
3.2 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,
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;
});
}
/** stations 단계 — 입력 관 · 구조물(follow 뒤 누가거리) · 그린 노선 · 곡선표 · 노선 초안 스냅. */
export function createStationsStage(
options: Omit<StationsOptions, "stationSnap" | "stationSnapStep" | "types"> & {
types: () => ReadonlyArray<PlanType>;
},
): StageFn<"stations"> {
return (ctx: StageContext<"stations">) => {
const route = ctx.out.route;
if (!route) return [];
const next = buildStations(
route.line,
route.curves,
(ctx.inputs.pipes as PlanPipe[] | null) ?? [],
(ctx.inputs.structures as PlanStructure[] | null) ?? [],
{
...options,
types: options.types(),
stationSnap: ctx.inputs.route?.stationSnap ?? false,
stationSnapStep: ctx.inputs.route?.stationSnapStep,
},
);
return reuseOutside(next, ctx.prev, ctx.range);
};
}
/** 상세 측점 대 묶음 측점(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;
}