Add workspace editor tools

This commit is contained in:
Formsmith
2026-06-14 22:08:55 +03:00
parent 9a889fdcb7
commit 753a1c2833
29 changed files with 2849 additions and 1397 deletions
+119
View File
@@ -0,0 +1,119 @@
import type { WorkplaneShape } from "@/types/sketchforge";
export function normalizeDegrees(value: number) {
return ((value % 360) + 360) % 360;
}
export function cleanRotationDegrees(value: number, precision = 1) {
if (!Number.isFinite(value)) {
return 0;
}
const normalized = normalizeDegrees(value);
const rounded = Number(normalized.toFixed(precision));
const zeroThreshold = precision <= 1 ? 0.5 : 0.05;
if (rounded < zeroThreshold || rounded >= 360 - zeroThreshold || Object.is(rounded, -0)) {
return 0;
}
return rounded;
}
export function cleanNearZero(value: number, epsilon = 0.005) {
return Math.abs(value) < epsilon ? 0 : value;
}
export function shapeWidth(shape: WorkplaneShape) {
return shape.width ?? shape.size;
}
export function shapeDepth(shape: WorkplaneShape) {
return shape.depth ?? shape.size;
}
export function resizedShapeSize(width: number, depth: number) {
return Math.max(width, depth);
}
export function proportionalResizeScale(startWidth: number, startDepth: number, nextWidth: number, nextDepth: number) {
const widthScale = nextWidth / Math.max(0.001, startWidth);
const depthScale = nextDepth / Math.max(0.001, startDepth);
if (!Number.isFinite(widthScale) || !Number.isFinite(depthScale)) {
return 1;
}
return Math.abs(widthScale - 1) >= Math.abs(depthScale - 1) ? widthScale : depthScale;
}
export function fallbackSolidColor(shape: WorkplaneShape) {
if (shape.kind === "cylinder") return "#d97813";
if (shape.kind === "sphere") return "#0098c7";
if (shape.kind === "cone") return "#6e2786";
if (shape.kind === "pyramid") return "#f2cf10";
return "#d41721";
}
export function mirrorSign(value?: boolean) {
return value ? -1 : 1;
}
export function mirroredAxisCount(shape: WorkplaneShape) {
return [shape.mirrorX, shape.mirrorY, shape.mirrorZ].filter(Boolean).length;
}
export function canonicalizeShape(shape: WorkplaneShape): WorkplaneShape {
const next: WorkplaneShape = {
...shape,
rotation: cleanRotationDegrees(shape.rotation ?? 0),
rotationX: cleanRotationDegrees(shape.rotationX ?? 0),
rotationZ: cleanRotationDegrees(shape.rotationZ ?? 0),
mirrorX: shape.mirrorX || undefined,
mirrorY: shape.mirrorY || undefined,
mirrorZ: shape.mirrorZ || undefined,
};
if (shape.groupedShapes) {
next.groupedShapes = shape.groupedShapes.map(canonicalizeShape);
}
return next;
}
export function workplaneShapesEqual(a: WorkplaneShape, b: WorkplaneShape) {
return (
a.id === b.id &&
a.name === b.name &&
a.kind === b.kind &&
a.color === b.color &&
a.hole === b.hole &&
a.x === b.x &&
a.z === b.z &&
a.elevation === b.elevation &&
a.size === b.size &&
a.width === b.width &&
a.depth === b.depth &&
a.height === b.height &&
a.rotation === b.rotation &&
a.rotationX === b.rotationX &&
a.rotationZ === b.rotationZ &&
a.mirrorX === b.mirrorX &&
a.mirrorY === b.mirrorY &&
a.mirrorZ === b.mirrorZ &&
a.radius === b.radius &&
a.steps === b.steps &&
a.sides === b.sides &&
a.bevel === b.bevel &&
a.segments === b.segments &&
a.topRadius === b.topRadius &&
a.baseRadius === b.baseRadius &&
a.text === b.text &&
a.font === b.font &&
a.importedMesh === b.importedMesh &&
a.imagePlate === b.imagePlate &&
a.groupedShapes === b.groupedShapes &&
a.groupedBaseWidth === b.groupedBaseWidth &&
a.groupedBaseDepth === b.groupedBaseDepth &&
a.groupedBaseHeight === b.groupedBaseHeight &&
a.locked === b.locked &&
a.hidden === b.hidden
);
}
export function serializeShapesForSync(shapes: WorkplaneShape[]) {
return JSON.stringify(shapes.map(canonicalizeShape));
}