110 lines
5.1 KiB
JavaScript
110 lines
5.1 KiB
JavaScript
// Compares a built line-course checkpoint with line-spec.json.
|
|
// Geometry/centre come from the Node WASM probe (inspectFcstd); colours come
|
|
// from Document.xml's WebDisplayColor property, which the Web save persists.
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { execFileSync } from "node:child_process";
|
|
import { fileURLToPath } from "node:url";
|
|
import { inspectFcstd } from "../../carrier-course/lib/inspect.mjs";
|
|
|
|
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
const spec = JSON.parse(fs.readFileSync(path.join(here, "..", "line-spec.json"), "utf8"));
|
|
const partByName = new Map(spec.models.flatMap(model =>
|
|
model.parts.map(part => [part.partName, { ...part, model: model.name }])));
|
|
const modelByName = new Map(spec.models.map(model => [model.name, model]));
|
|
|
|
const nearArray = (actual, expected, tolerance, label, issues) => {
|
|
if (!Array.isArray(actual) || actual.length !== expected.length) {
|
|
issues.push({ name: label, detail: { actual, expected } });
|
|
return;
|
|
}
|
|
const worst = Math.max(...actual.map((value, index) => Math.abs(Number(value) - Number(expected[index]))));
|
|
if (!Number.isFinite(worst) || worst > tolerance)
|
|
issues.push({ name: label, detail: { actual, expected, worst, tolerance } });
|
|
};
|
|
|
|
const parseHex = hex => {
|
|
const match = /^#?([0-9a-f]{6})$/i.exec(String(hex ?? "").trim());
|
|
if (!match) return null;
|
|
const value = parseInt(match[1], 16);
|
|
return [(value >> 16) & 255, (value >> 8) & 255, value & 255].map(channel => channel / 255);
|
|
};
|
|
|
|
const readDocumentColors = filePath => {
|
|
const xml = execFileSync("/usr/bin/unzip", ["-p", filePath, "Document.xml"],
|
|
{ encoding: "utf8", maxBuffer: 64 * 1024 * 1024 });
|
|
const colors = new Map();
|
|
const objects = xml.split("<Object ").slice(1);
|
|
for (const block of objects) {
|
|
const name = /\bname="([^"]*)"/.exec(block)?.[1];
|
|
const label = /<Property name="Label"[^>]*>\s*<String value="([^"]*)"/s.exec(block)?.[1] ?? name;
|
|
const color = /<Property name="WebDisplayColor"[^>]*>\s*<String value="([^"]*)"/s.exec(block)?.[1];
|
|
if (label) colors.set(label, color ?? null);
|
|
if (name && name !== label) colors.set(name, color ?? null);
|
|
}
|
|
return colors;
|
|
};
|
|
|
|
export function compareLinePart(expected, actual, color) {
|
|
const issues = [];
|
|
nearArray(actual.bbox, expected.worldBBox, 1.5, "bbox", issues);
|
|
nearArray(actual.center, expected.center, 3, "center", issues);
|
|
if (actual.valid !== true) issues.push({ name: "valid", detail: actual.valid });
|
|
const actualColor = parseHex(color);
|
|
const expectedColor = expected.color;
|
|
if (!actualColor) {
|
|
issues.push({ name: "color", detail: { actual: color, expected: expectedColor } });
|
|
} else {
|
|
const worst = Math.max(...actualColor.map((value, index) => Math.abs(value - expectedColor[index])));
|
|
if (worst > 0.02) issues.push({ name: "color", detail: { actual: actualColor, expected: expectedColor, worst } });
|
|
}
|
|
return { name: expected.partName, label: expected.label, model: expected.model,
|
|
tolerance: 1.5, ok: issues.length === 0, issues };
|
|
}
|
|
|
|
export async function checkLineCheckpoint(filePath, expectedNames) {
|
|
const { objects } = await inspectFcstd(filePath);
|
|
const byLabel = new Map(objects.map(object => [object.label, object]));
|
|
const colors = readDocumentColors(filePath);
|
|
const results = [];
|
|
for (const name of expectedNames) {
|
|
const expected = partByName.get(name);
|
|
if (!expected) throw new Error(`line-spec has no part ${name}`);
|
|
const actual = byLabel.get(name) ?? objects.find(object => object.name === name);
|
|
if (!actual) {
|
|
results.push({ name, ok: false, issues: [{ name: "missing", detail: expected.label }] });
|
|
continue;
|
|
}
|
|
results.push(compareLinePart(expected, actual, colors.get(actual.label) ?? colors.get(actual.name)));
|
|
}
|
|
const extra = objects.filter(object => !expectedNames.includes(object.label)).map(object => object.label);
|
|
const built = objects.filter(object => expectedNames.includes(object.label));
|
|
|
|
const models = [];
|
|
for (const model of spec.models) {
|
|
const names = model.parts.map(part => part.partName).filter(partName => expectedNames.includes(partName));
|
|
if (!names.length) continue;
|
|
const builtParts = built.filter(object => names.includes(object.label));
|
|
const issues = [];
|
|
nearArray(
|
|
builtParts.length ? [0, 1, 2].map(axis => Math.min(...builtParts.map(object => object.bbox[axis])))
|
|
.concat([0, 1, 2].map(axis => Math.max(...builtParts.map(object => object.bbox[axis + 3])))) : [],
|
|
[model.overallBBox[0], model.overallBBox[1], model.overallBBox[2],
|
|
model.overallBBox[3], model.overallBBox[4], model.overallBBox[5]],
|
|
model.parts.length <= 2 ? 2 : 5, "model-bbox", issues);
|
|
models.push({ name: model.name, label: model.label, parts: names.length, ok: issues.length === 0, issues });
|
|
}
|
|
|
|
return {
|
|
schema: "freecad-web-line-course/precision/v1",
|
|
objectCount: objects.length,
|
|
extra,
|
|
results,
|
|
models,
|
|
ok: results.every(result => result.ok) && extra.length === 0 && models.every(model => model.ok),
|
|
};
|
|
}
|
|
|
|
export function loadLineSpec() { return spec; }
|
|
export function lineModel(name) { return modelByName.get(name); }
|