2026-06-08 22:30:39 +02:00
|
|
|
import type { AppConfig, Calibration, Mat3, Vec2, RenderStyle } from './types';
|
2026-06-08 22:28:07 +02:00
|
|
|
|
2026-06-08 22:30:39 +02:00
|
|
|
export const DEFAULT_RENDER: RenderStyle = { cutColor: '#00e5ff', rapidColor: '#ff9800', lineWidth: 1.5 };
|
|
|
|
|
|
|
|
|
|
function isFiniteNumber(n: unknown): n is number {
|
|
|
|
|
return typeof n === 'number' && Number.isFinite(n);
|
|
|
|
|
}
|
2026-06-08 22:28:07 +02:00
|
|
|
|
|
|
|
|
function isVec2Array(v: unknown): v is Vec2[] {
|
2026-06-08 22:30:39 +02:00
|
|
|
return Array.isArray(v) && v.every((p) => Array.isArray(p) && p.length === 2 && p.every(isFiniteNumber));
|
2026-06-08 22:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function parseCalibration(c: unknown): Calibration | null {
|
|
|
|
|
if (!c || typeof c !== 'object') return null;
|
|
|
|
|
const obj = c as Record<string, unknown>;
|
|
|
|
|
if (!isVec2Array(obj.imagePoints) || !isVec2Array(obj.machinePoints)) return null;
|
|
|
|
|
if (!Array.isArray(obj.homography) || obj.homography.length !== 9) return null;
|
2026-06-08 22:30:39 +02:00
|
|
|
if (!obj.homography.every(isFiniteNumber)) return null;
|
2026-06-08 22:28:07 +02:00
|
|
|
if (obj.imagePoints.length !== obj.machinePoints.length || obj.imagePoints.length < 4) return null;
|
|
|
|
|
return {
|
|
|
|
|
imagePoints: obj.imagePoints,
|
|
|
|
|
machinePoints: obj.machinePoints,
|
|
|
|
|
homography: obj.homography as Mat3,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-08 22:30:39 +02:00
|
|
|
function parseRenderStyle(r: unknown): RenderStyle {
|
|
|
|
|
const obj = (r && typeof r === 'object' ? r : {}) as Record<string, unknown>;
|
|
|
|
|
return {
|
|
|
|
|
cutColor: typeof obj.cutColor === 'string' ? obj.cutColor : DEFAULT_RENDER.cutColor,
|
|
|
|
|
rapidColor: typeof obj.rapidColor === 'string' ? obj.rapidColor : DEFAULT_RENDER.rapidColor,
|
|
|
|
|
lineWidth: isFiniteNumber(obj.lineWidth) ? obj.lineWidth : DEFAULT_RENDER.lineWidth,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-08 22:28:07 +02:00
|
|
|
export async function loadConfig(url: string): Promise<AppConfig> {
|
|
|
|
|
const res = await fetch(url);
|
|
|
|
|
if (!res.ok) throw new Error(`Failed to load ${url}: ${res.status}`);
|
|
|
|
|
const raw = (await res.json()) as Record<string, unknown>;
|
|
|
|
|
return {
|
|
|
|
|
streamUrl: typeof raw.streamUrl === 'string' ? raw.streamUrl : '',
|
|
|
|
|
calibration: parseCalibration(raw.calibration),
|
2026-06-08 22:30:39 +02:00
|
|
|
renderDefaults: parseRenderStyle(raw.renderDefaults),
|
2026-06-08 22:28:07 +02:00
|
|
|
};
|
|
|
|
|
}
|