release: add sketch revolve and configurable gears

This commit is contained in:
Formsmith
2026-07-24 11:55:44 +03:00
parent 5dde9faceb
commit fe161f8d75
32 changed files with 1913 additions and 115 deletions
+74
View File
@@ -0,0 +1,74 @@
import { describe, expect, it } from "vitest";
import { createGearGeometry, gearSettings, normalizeGearTeeth, normalizeGearToothSize } from "@/lib/gearGeometry";
import type { GearType } from "@/types/sketchforge";
function edgeUseCounts(position: { count: number; getX: (index: number) => number; getY: (index: number) => number; getZ: (index: number) => number }) {
const uses = new Map<string, number>();
const keyForPoint = (index: number) => [position.getX(index), position.getY(index), position.getZ(index)]
.map((value) => value.toFixed(5))
.join(",");
for (let index = 0; index + 2 < position.count; index += 3) {
const triangle = [keyForPoint(index), keyForPoint(index + 1), keyForPoint(index + 2)];
for (let edge = 0; edge < 3; edge += 1) {
const a = triangle[edge];
const b = triangle[(edge + 1) % 3];
const key = a < b ? `${a}:${b}` : `${b}:${a}`;
uses.set(key, (uses.get(key) ?? 0) + 1);
}
}
return uses;
}
describe("gear geometry", () => {
it.each<GearType>(["spur", "helical", "bevel"])("creates a closed %s gear with the requested bounds", (gearType) => {
const geometry = createGearGeometry({
width: 30,
depth: 24,
height: 8,
teeth: 14,
toothSize: 2.5,
gearType,
});
const position = geometry.getAttribute("position");
expect(position.count).toBeGreaterThan(100);
expect(geometry.getIndex()).toBeNull();
expect([...edgeUseCounts(position).values()].every((uses) => uses === 2)).toBe(true);
expect(geometry.boundingBox?.min.y).toBeCloseTo(0, 5);
expect(geometry.boundingBox?.max.y).toBeCloseTo(8, 5);
expect((geometry.boundingBox?.max.x ?? 0) - (geometry.boundingBox?.min.x ?? 0)).toBeCloseTo(30, 4);
expect((geometry.boundingBox?.max.z ?? 0) - (geometry.boundingBox?.min.z ?? 0)).toBeCloseTo(24, 4);
});
it("creates a closed solid center when the center hole is zero", () => {
const geometry = createGearGeometry({
width: 30,
depth: 30,
height: 6,
teeth: 8,
toothSize: 4,
centerHoleSize: 0,
gearType: "spur",
});
const position = geometry.getAttribute("position");
expect([...edgeUseCounts(position).values()].every((uses) => uses === 2)).toBe(true);
expect((geometry.boundingBox?.max.x ?? 0) - (geometry.boundingBox?.min.x ?? 0)).toBeCloseTo(30, 4);
expect((geometry.boundingBox?.max.z ?? 0) - (geometry.boundingBox?.min.z ?? 0)).toBeCloseTo(30, 4);
});
it("normalizes editable gear settings to supported values", () => {
expect(normalizeGearTeeth(2)).toBe(6);
expect(normalizeGearTeeth(200)).toBe(64);
expect(normalizeGearToothSize(20, 30, 20)).toBe(4.4);
expect(gearSettings({ width: 30, depth: 30, teeth: 20, toothSize: 3, toothWidth: 2.4, centerHoleSize: 7, gearType: "bevel", helixAngle: -30, helixQuality: 24 })).toEqual({
teeth: 20,
toothSize: 3,
toothWidth: 2.4,
centerHoleSize: 7,
gearType: "bevel",
helixAngle: -30,
helixQuality: 24,
});
});
});
+13
View File
@@ -36,9 +36,22 @@ describe("shape catalog", () => {
it("uses shape-specific defaults for text and round profiles", () => {
const text = makeShapeFromAsset({ id: "text", name: "Text", src: "text.png", kind: "text", color: "#cf101b" });
const torus = makeShapeFromAsset({ id: "torus", name: "Torus", src: "torus.png", kind: "torus", color: "#0098c7" });
const gear = makeShapeFromAsset({ id: "gear", name: "Gear", src: "gear.svg", kind: "gear", color: "#6f7f8d" });
expect(text).toMatchObject({ width: 86, depth: 28, height: 10, text: "TEXT", font: "Multilanguage" });
expect(torus).toMatchObject({ size: 22, width: 22, depth: 22, height: 5 });
expect(gear).toMatchObject({
size: 30,
width: 30,
depth: 30,
height: 6,
teeth: 12,
toothSize: 2.5,
centerHoleSize: 6,
gearType: "spur",
helixAngle: 22.5,
helixQuality: 16,
});
});
it("creates canonical scene shapes with stable defaults", () => {
+93
View File
@@ -0,0 +1,93 @@
import { beforeAll, describe, expect, it } from "vitest";
import manifoldModule, { type ManifoldToplevel } from "manifold-3d";
import {
buildSketchRevolveMesh,
normalizeSketchRevolveSettings,
sketchProfileToRevolvePolygons,
} from "@/lib/sketchRevolve";
import type { SketchProfile } from "@/types/sketchforge";
function rectangleProfile(innerRadius = 5, outerRadius = 10, height = 20): SketchProfile {
return {
points: [
{ id: "a", x: -innerRadius, z: 0 },
{ id: "b", x: -outerRadius, z: 0 },
{ id: "c", x: -outerRadius, z: height },
{ id: "d", x: -innerRadius, z: height },
],
segments: [
{ id: "ab", startId: "a", endId: "b", kind: "line" },
{ id: "bc", startId: "b", endId: "c", kind: "line" },
{ id: "cd", startId: "c", endId: "d", kind: "line" },
{ id: "da", startId: "d", endId: "a", kind: "line" },
],
};
}
describe("sketch revolve", () => {
let runtime: ManifoldToplevel;
beforeAll(async () => {
runtime = await manifoldModule();
runtime.setup();
});
it("revolves a closed left-side profile around the center axis", () => {
const mesh = buildSketchRevolveMesh(runtime, rectangleProfile(), { sides: 32 });
expect(mesh.triangleCount).toBeGreaterThan(100);
expect(mesh.width).toBeCloseTo(20, 4);
expect(mesh.depth).toBeCloseTo(20, 4);
expect(mesh.height).toBeCloseTo(20, 4);
expect(Math.min(...mesh.positions.filter((_value, index) => index % 3 === 1))).toBeCloseTo(0, 6);
});
it("creates thickness automatically for an open profile", () => {
const profile: SketchProfile = {
points: [{ id: "a", x: -8, z: 0 }, { id: "b", x: -8, z: 20 }],
segments: [{ id: "ab", startId: "a", endId: "b", kind: "line" }],
};
const polygons = sketchProfileToRevolvePolygons(profile, { thickness: 2 });
expect(polygons).toHaveLength(1);
expect(Math.min(...polygons[0].map((point) => point[0]))).toBeCloseTo(7, 6);
expect(Math.max(...polygons[0].map((point) => point[0]))).toBeCloseTo(9, 6);
const mesh = buildSketchRevolveMesh(runtime, profile, { thickness: 2, sides: 24 });
expect(mesh.triangleCount).toBeGreaterThan(0);
expect(mesh.width).toBeCloseTo(18, 4);
expect(mesh.height).toBeCloseTo(20, 4);
});
it("allows construction points across the axis but only revolves the working-side segment", () => {
const profile: SketchProfile = {
points: [{ id: "construction", x: 10, z: 0 }, { id: "profile", x: -10, z: 20 }],
segments: [{ id: "crossing", startId: "construction", endId: "profile", kind: "line" }],
};
const polygons = sketchProfileToRevolvePolygons(profile, { thickness: 1 });
expect(polygons).toHaveLength(1);
expect(Math.max(...polygons[0].map((point) => point[1]))).toBeLessThan(11);
expect(Math.max(...polygons[0].map((point) => point[0]))).toBeGreaterThan(10);
const mesh = buildSketchRevolveMesh(runtime, profile, { thickness: 1, sides: 24 });
expect(mesh.triangleCount).toBeGreaterThan(0);
expect(mesh.height).toBeGreaterThan(9);
expect(mesh.height).toBeLessThan(11);
});
it("supports partial and reverse sweeps", () => {
const quarter = buildSketchRevolveMesh(runtime, rectangleProfile(), { startAngle: 30, sweepAngle: 90, sides: 48 });
const reverse = buildSketchRevolveMesh(runtime, rectangleProfile(), { startAngle: 120, sweepAngle: -90, sides: 48 });
expect(quarter.triangleCount).toBeGreaterThan(0);
expect(reverse.triangleCount).toBeGreaterThan(0);
expect(quarter.width).toBeLessThanOrEqual(20.001);
expect(quarter.depth).toBeLessThanOrEqual(20.001);
});
it("normalizes unsafe revolve settings", () => {
expect(normalizeSketchRevolveSettings({ startAngle: -30, sweepAngle: 0, sides: 500, quality: 0, thickness: -2 })).toEqual({
startAngle: 330,
sweepAngle: 1,
sides: 128,
quality: 1,
thickness: 0.1,
});
});
});
+38 -1
View File
@@ -74,7 +74,7 @@ describe("SketchForge .skf project packages", () => {
it("round-trips every supported native shape kind and editable properties", async () => {
const nativeKinds: ShapeKind[] = [
"box", "cylinder", "sphere", "sketch", "scribble", "cone", "pyramid", "roof", "text", "roundRoof",
"halfSphere", "torus", "tube", "ring", "wedge", "polygon", "icosahedron",
"halfSphere", "torus", "tube", "gear", "ring", "wedge", "polygon", "icosahedron",
];
const shapes = nativeKinds.map((kind, index) => shape(kind, `${kind}-${index}`, {
hole: index === 2,
@@ -82,6 +82,13 @@ describe("SketchForge .skf project packages", () => {
hidden: index === 4,
mirrorX: index === 5,
sides: index + 3,
teeth: kind === "gear" ? 18 : undefined,
toothSize: kind === "gear" ? 3.25 : undefined,
toothWidth: kind === "gear" ? 2.75 : undefined,
centerHoleSize: kind === "gear" ? 7.5 : undefined,
gearType: kind === "gear" ? "helical" : undefined,
helixAngle: kind === "gear" ? -30 : undefined,
helixQuality: kind === "gear" ? 24 : undefined,
text: kind === "text" ? "Editable" : undefined,
sketchProfile: kind === "sketch" ? {
points: [
@@ -117,6 +124,36 @@ describe("SketchForge .skf project packages", () => {
expect(document.assets.filter((entry) => entry.kind === "derived-mesh")).toHaveLength(0);
});
it("preserves editable revolve sketch settings and generated geometry", async () => {
const revolve = shape("mesh", "revolve-sketch", {
name: "Sketch revolve",
sketchOperation: "revolve",
sketchRevolve: { startAngle: 25, sweepAngle: -220, sides: 48, quality: 8, thickness: 1.5 },
sketchProfile: {
points: [{ id: "p1", x: -4, z: 0 }, { id: "p2", x: -8, z: 0 }, { id: "p3", x: -8, z: 16 }, { id: "p4", x: -4, z: 16 }],
segments: [
{ id: "s1", startId: "p1", endId: "p2", kind: "line" },
{ id: "s2", startId: "p2", endId: "p3", kind: "line" },
{ id: "s3", startId: "p3", endId: "p4", kind: "line" },
{ id: "s4", startId: "p4", endId: "p1", kind: "line" },
],
},
importedMesh: {
positions: [0, 0, 0, 1, 0, 0, 0, 1, 0],
baseWidth: 16,
baseDepth: 16,
baseHeight: 16,
triangleCount: 1,
sourceFormat: "json",
},
});
const restored = await importSkfProject(await exportSkfProject(input([revolve])));
expect(restored.shapes[0].sketchOperation).toBe("revolve");
expect(restored.shapes[0].sketchRevolve).toEqual(revolve.sketchRevolve);
expect(restored.shapes[0].sketchProfile).toEqual(revolve.sketchProfile);
expect(restored.shapes[0].importedMesh?.positions).toEqual(revolve.importedMesh?.positions);
});
it("preserves nested groups, holes, intersection metadata, edge history, B-Rep, and undo/redo", async () => {
const solid = shape("box", "solid", { x: 0 });
const hole = shape("cylinder", "hole", { hole: true, color: "#b8c2cc", x: 4 });
+5 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { interiorWorkplaneGridCoordinates, workplaneGridPalette, WORKPLANE_LINE_ELEVATION } from "@/lib/workplaneGrid";
import { interiorWorkplaneGridCoordinates, workplaneGridPalette, WORKPLANE_LINE_ELEVATION, WORKPLANE_MAJOR_GRID_INTERVAL } from "@/lib/workplaneGrid";
describe("workplane grid geometry", () => {
it("excludes both perimeter coordinates when spacing divides the workplane", () => {
@@ -22,6 +22,10 @@ describe("workplane grid geometry", () => {
expect(WORKPLANE_LINE_ELEVATION).toBe(0);
});
it("groups minor grid blocks into five-by-five major sections", () => {
expect(WORKPLANE_MAJOR_GRID_INTERVAL).toBe(5);
});
it("preserves the minor, major, and axis line hierarchy", () => {
const palette = workplaneGridPalette();
+9 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import type { WorkplaneWorkspaceSettings } from "@/types/sketchforge";
import { formatMeasurementNumber, lengthDisplayUnit, millimetersToDisplay, normalizeScaleForUnits, scaleOptionsForUnits } from "@/lib/measurementUnits";
import { formatMeasurementNumber, lengthDisplayUnit, millimetersToDisplay, normalizeScaleForUnits, parseMeasurementInput, scaleOptionsForUnits } from "@/lib/measurementUnits";
import { DEFAULT_SNAP_GRID, DEFAULT_WORKPLANE_WORKSPACE, normalizeSnapGrid, normalizeWorkspaceSettings, workplaneSettingsFingerprint, workspaceHydrationSyncDecision } from "@/lib/workplaneSettings";
describe("workplane settings helpers", () => {
@@ -78,6 +78,14 @@ describe("workplane settings helpers", () => {
expect(formatMeasurementNumber(0.0004, 1, 0.001)).toBe("0.0004");
});
it("accepts dot and comma decimal measurement input", () => {
expect(parseMeasurementInput("12.5")).toBe(12.5);
expect(parseMeasurementInput("12,5")).toBe(12.5);
expect(parseMeasurementInput("1.234,5")).toBe(1234.5);
expect(parseMeasurementInput("1,234.5")).toBe(1234.5);
expect(parseMeasurementInput("not a measurement")).toBeNaN();
});
it("fingerprints workspace and snap settings together", () => {
const base = workplaneSettingsFingerprint(DEFAULT_WORKPLANE_WORKSPACE, "1.0 mm");
const equivalentNewReference = workplaneSettingsFingerprint({ ...DEFAULT_WORKPLANE_WORKSPACE }, "1.0 mm");