2026-07-28 14:52:59 +03:00
|
|
|
import type { CadModifierEdge } from "@/lib/cadModifierTypes";
|
|
|
|
|
|
2026-07-11 20:34:13 +03:00
|
|
|
export const CAD_MODIFIER_RUNTIME_BASE = "/occt";
|
|
|
|
|
export const CAD_MODIFIER_REQUEST_TIMEOUT_MS = 30_000;
|
2026-08-02 17:38:53 +03:00
|
|
|
export const CAD_MODIFIER_MAX_PREPARE_TIMEOUT_MS = 180_000;
|
2026-07-14 09:43:37 +03:00
|
|
|
export const CAD_MODIFIER_MAX_SHARP_ANGLE = 90;
|
2026-07-11 20:34:13 +03:00
|
|
|
|
|
|
|
|
export type CadModifierRequestPhase = "prepare" | "preview";
|
|
|
|
|
|
2026-07-28 14:52:59 +03:00
|
|
|
export function cadTransformRequiresGeneralTransform(transform: number[]) {
|
|
|
|
|
if (transform.length !== 12 || !transform.every(Number.isFinite)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const x = [transform[0], transform[4], transform[8]];
|
|
|
|
|
const y = [transform[1], transform[5], transform[9]];
|
|
|
|
|
const z = [transform[2], transform[6], transform[10]];
|
|
|
|
|
const dot = (a: number[], b: number[]) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
|
|
|
const xLengthSquared = dot(x, x);
|
|
|
|
|
const yLengthSquared = dot(y, y);
|
|
|
|
|
const zLengthSquared = dot(z, z);
|
|
|
|
|
const scaleSquared = Math.max(xLengthSquared, yLengthSquared, zLengthSquared);
|
|
|
|
|
if (scaleSquared <= 1e-18) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const tolerance = scaleSquared * 1e-9;
|
|
|
|
|
return (
|
|
|
|
|
Math.abs(dot(x, y)) > tolerance ||
|
|
|
|
|
Math.abs(dot(x, z)) > tolerance ||
|
|
|
|
|
Math.abs(dot(y, z)) > tolerance ||
|
|
|
|
|
Math.abs(xLengthSquared - yLengthSquared) > tolerance ||
|
|
|
|
|
Math.abs(xLengthSquared - zLengthSquared) > tolerance ||
|
|
|
|
|
Math.abs(yLengthSquared - zLengthSquared) > tolerance
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-19 15:58:52 +03:00
|
|
|
export function isCadModifierWasmMemoryFault(message: string, errorName = "") {
|
|
|
|
|
return (
|
2026-07-28 14:52:59 +03:00
|
|
|
/memory access out of bounds|out of bounds memory access|\babort(?:ed)?\b/i.test(message) ||
|
2026-07-19 15:58:52 +03:00
|
|
|
/^(?:WebAssembly\.)?RuntimeError$/i.test(errorName)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-28 14:52:59 +03:00
|
|
|
export function defaultCadModifierTangentChain(appliedFeatureCount: number) {
|
|
|
|
|
return appliedFeatureCount === 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function cadModifierTopologyEdgeIsSelectable(
|
|
|
|
|
edge: Pick<CadModifierEdge, "manifold" | "boundary" | "points">,
|
|
|
|
|
) {
|
|
|
|
|
return edge.manifold && !edge.boundary && edge.points.length >= 6;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function selectableCadModifierEdge(
|
|
|
|
|
edge: Pick<CadModifierEdge, "display" | "selectable" | "manifold" | "boundary" | "angle">,
|
|
|
|
|
sharpAngle: number,
|
|
|
|
|
) {
|
|
|
|
|
return edge.selectable && edge.manifold && !edge.boundary && edge.angle + 1e-3 >= sharpAngle;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-11 20:34:13 +03:00
|
|
|
export function edgeModifierSelectionStatus(prepared: boolean, selectedCount: number, availableCount: number) {
|
|
|
|
|
return prepared ? `${selectedCount} of ${availableCount} sharp edges selected` : "Preparing edges\u2026";
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-02 17:38:53 +03:00
|
|
|
export function cadModifierPrepareTimeoutMs(meshTriangleCount: number) {
|
|
|
|
|
if (!Number.isFinite(meshTriangleCount) || meshTriangleCount <= 0) {
|
|
|
|
|
return CAD_MODIFIER_REQUEST_TIMEOUT_MS;
|
|
|
|
|
}
|
|
|
|
|
const normalizedTriangleCount = Math.max(0, Math.floor(meshTriangleCount));
|
|
|
|
|
const meshPreparationBudget = 45_000 + normalizedTriangleCount * 0.75;
|
|
|
|
|
return Math.min(
|
|
|
|
|
CAD_MODIFIER_MAX_PREPARE_TIMEOUT_MS,
|
|
|
|
|
Math.max(60_000, Math.ceil(meshPreparationBudget)),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-11 20:34:13 +03:00
|
|
|
export function cadModifierTimeoutMessage(phase: CadModifierRequestPhase) {
|
|
|
|
|
if (phase === "preview") {
|
|
|
|
|
return "The edge preview timed out. Cancel the tool and try again.";
|
|
|
|
|
}
|
2026-08-02 17:38:53 +03:00
|
|
|
return "Edge preparation timed out. This mesh needs more CAD processing than the interactive limit allows. Try a repaired or lower-detail STL.";
|
2026-07-11 20:34:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function cadModifierWorkerFailureMessage() {
|
|
|
|
|
return "The CAD worker could not start. Update to Firefox 121+, Chrome/Brave 114+, or Safari 17.2+, then try again.";
|
|
|
|
|
}
|