import type { CadModifierEdge } from "@/lib/cadModifierTypes"; export const CAD_MODIFIER_RUNTIME_BASE = "/occt"; export const CAD_MODIFIER_REQUEST_TIMEOUT_MS = 30_000; export const CAD_MODIFIER_MAX_SHARP_ANGLE = 90; export type CadModifierRequestPhase = "prepare" | "preview"; 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 ); } export function isCadModifierWasmMemoryFault(message: string, errorName = "") { return ( /memory access out of bounds|out of bounds memory access|\babort(?:ed)?\b/i.test(message) || /^(?:WebAssembly\.)?RuntimeError$/i.test(errorName) ); } export function defaultCadModifierTangentChain(appliedFeatureCount: number) { return appliedFeatureCount === 0; } export function cadModifierTopologyEdgeIsSelectable( edge: Pick, ) { return edge.manifold && !edge.boundary && edge.points.length >= 6; } export function selectableCadModifierEdge( edge: Pick, sharpAngle: number, ) { return edge.selectable && edge.manifold && !edge.boundary && edge.angle + 1e-3 >= sharpAngle; } export function edgeModifierSelectionStatus(prepared: boolean, selectedCount: number, availableCount: number) { return prepared ? `${selectedCount} of ${availableCount} sharp edges selected` : "Preparing edges\u2026"; } export function cadModifierTimeoutMessage(phase: CadModifierRequestPhase) { if (phase === "preview") { return "The edge preview timed out. Cancel the tool and try again."; } return "Edge preparation timed out. Update to Firefox 121+, Chrome/Brave 114+, or Safari 17.2+, then try again."; } export function cadModifierWorkerFailureMessage() { return "The CAD worker could not start. Update to Firefox 121+, Chrome/Brave 114+, or Safari 17.2+, then try again."; }