/* Draggable gizmo geometry and screen-space hit testing (Batch 2). * Desktop reference: transform_gizmo_3d.cc — one handle per axis, arrowheads * for translate, rings for rotate, boxes for scale, pick radius in pixels. */ import assert from "node:assert/strict"; import fs from "node:fs"; import path from "node:path"; import test from "node:test"; import ts from "typescript"; const repoRoot = path.resolve(import.meta.dirname, "../../.."); const transpile = (sourcePath) => { const result = ts.transpileModule(fs.readFileSync(sourcePath, "utf8"), { compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 }, fileName: sourcePath, reportDiagnostics: true, }); assert.deepEqual(result.diagnostics, []); return result.outputText; }; const threeStubs = ` export class LineSegments { constructor(geometry, material) { this.geometry = geometry; this.material = material; this.userData = {}; } removeFromParent() {} } export class LineBasicMaterial { constructor() {} dispose() {} } export class BufferGeometry { setAttribute() {} computeBoundingSphere() {} dispose() {} } export class Float32BufferAttribute { constructor() {} } export class Scene { add() {} } `; const toUrl = (source) => "data:text/javascript;base64," + Buffer.from(source).toString("base64"); /* The gizmo reuses the guide's colours and ring helper; link the real module. */ const guide = transpile(path.join(repoRoot, "web/app/src/three-adapter/transform-guide.ts")) .replace(/import\s*\{[^}]*\}\s*from\s*"\.\.\/vendor\/three\/three\.module\.js";/, threeStubs); const guideUrl = toUrl(guide); const gizmo = transpile(path.join(repoRoot, "web/app/src/three-adapter/transform-gizmo.ts")) .replace(/import\s*\{[^}]*\}\s*from\s*"\.\.\/vendor\/three\/three\.module\.js";/, threeStubs) .replace('from "./transform-guide"', `from ${JSON.stringify(guideUrl)}`); const { AXIS_COLORS } = await import(guideUrl); const { buildGizmoSegments, pickGizmoAxis, pickGizmoHandle, TransformGizmo } = await import(toUrl(gizmo)); const round = (values) => values.map((value) => Number(value.toFixed(6)) + 0); const base = { pivot: [1, 2, 3], axes: [[1, 0, 0], [0, 1, 0], [0, 0, 1]], tool: "translate", active: null, viewForward: [0, 0, -1], length: 2, }; test("translate draws an axis line plus two arrowhead strokes per axis", () => { const segments = buildGizmoSegments(base); assert.equal(segments.length, 3 * 3); for (const axis of [0, 1, 2]) { const own = segments.filter((segment) => segment.axis === axis); assert.deepEqual(round(own[0].from), [1, 2, 3], "every handle starts on the pivot"); const tip = round(own[0].to); assert.ok(Math.abs(Math.hypot(tip[0] - 1, tip[1] - 2, tip[2] - 3) - 2) < 1e-6, "tip at one length"); assert.deepEqual(round(own[0].color), round(AXIS_COLORS[axis])); for (const stroke of own.slice(1)) assert.deepEqual(round(stroke.from), tip, "arrowhead at the tip"); } /* Desktop ships Blender's axis colours. */ assert.deepEqual(round(AXIS_COLORS[0]), [1, 0.2, 0.33]); }); test("the constrained axis is brightened", () => { /* Z has headroom; X/Y are already at 1.0 on one channel. */ const active = buildGizmoSegments({ ...base, active: 2 }); const plainZ = buildGizmoSegments(base).find((segment) => segment.axis === 2).color; const activeZ = active.find((segment) => segment.axis === 2).color; assert.ok(activeZ[0] > plainZ[0]); assert.ok(activeZ[1] > plainZ[1]); }); test("rotate draws a ring per axis at 0.8 of the handle length", () => { const segments = buildGizmoSegments({ ...base, tool: "rotate" }); assert.equal(segments.length, 3 * 40); for (const axis of [0, 1, 2]) { const own = segments.filter((segment) => segment.axis === axis); const radius = 2 * 0.8; for (const segment of own) { for (const point of [segment.from, segment.to]) { assert.ok(Math.abs(Math.hypot(point[0] - 1, point[1] - 2, point[2] - 3) - radius) < 1e-6); /* Perpendicular to the axis: the offset along it is zero. */ assert.ok(Math.abs(point[axis] - base.pivot[axis]) < 1e-6, "ring stays in the axis plane"); } } } }); test("scale draws a square outline at each tip", () => { const segments = buildGizmoSegments({ ...base, tool: "scale" }); assert.equal(segments.length, 3 * 5); for (const axis of [0, 1, 2]) { const own = segments.filter((segment) => segment.axis === axis); /* Four outline strokes closing the square after the axis line. */ for (const corner of own.slice(1)) assert.ok(Math.abs(Math.hypot(corner.to[0] - 1, corner.to[1] - 2, corner.to[2] - 3) - 2) < 2 * 0.1); } }); test("hit testing picks the nearest handle inside the pixel tolerance", () => { const segments = [ { axis: 0, pivotIndex: 0, x1: 100, y1: 100, x2: 160, y2: 100 }, { axis: 1, pivotIndex: 0, x1: 100, y1: 100, x2: 100, y2: 40 }, { axis: 2, pivotIndex: 0, x1: 100, y1: 100, x2: 100, y2: 160 }, ]; assert.equal(pickGizmoAxis(150, 106, segments), 0, "6 px from X"); assert.equal(pickGizmoAxis(104, 60, segments), 1, "4 px from Y"); assert.equal(pickGizmoAxis(94, 150, segments), 2, "6 px from Z"); /* Beyond 12 px, or anywhere on empty space, nothing is grabbed. */ assert.equal(pickGizmoAxis(150, 113, segments), null); assert.equal(pickGizmoAxis(0, 0, segments), null); assert.equal(pickGizmoAxis(150, 100, []), null); /* The closer of two intersecting handles wins. */ assert.equal(pickGizmoAxis(120, 100, segments), 0); }); test("every handle carries the pivot index it was built for", () => { assert.ok(buildGizmoSegments(base).every((segment) => segment.pivotIndex === 0), "defaults to the first pivot"); assert.ok(buildGizmoSegments({ ...base, index: 3 }).every((segment) => segment.pivotIndex === 3)); }); test("hit testing reports which pivot's handle was grabbed", () => { /* INDIVIDUAL_ORIGINS draws the same axis at several pivots; the hit must * name the pivot, not just the axis, so the drag starts from that object. */ const segments = [ { axis: 0, pivotIndex: 0, x1: 100, y1: 100, x2: 160, y2: 100 }, { axis: 0, pivotIndex: 1, x1: 300, y1: 100, x2: 360, y2: 100 }, ]; assert.deepEqual(pickGizmoHandle(330, 104, segments), { axis: 0, pivotIndex: 1 }); assert.deepEqual(pickGizmoHandle(150, 104, segments), { axis: 0, pivotIndex: 0 }); assert.equal(pickGizmoHandle(500, 500, segments), null); }); test("only the grabbed pivot's axis brightens during a drag", () => { const plain = buildGizmoSegments({ ...base, index: 0 }).find((segment) => segment.axis === 0).color; const grabbed = buildGizmoSegments({ ...base, active: 0, activeIndex: 1, index: 1 }).find((segment) => segment.axis === 0).color; assert.ok(grabbed[2] > plain[2], "the grabbed pivot brightens"); const other = buildGizmoSegments({ ...base, active: 0, activeIndex: 1, index: 0 }).find((segment) => segment.axis === 0).color; assert.deepEqual(round(other), round(plain), "the other pivots stay plain"); }); test("a zero-length gizmo draws no handles at all", () => { assert.equal(buildGizmoSegments({ ...base, length: 0 }).length, 0); assert.equal(buildGizmoSegments({ ...base, length: -1 }).length, 0); }); test("TransformGizmo hides on null and disposes", () => { const gizmoScene = { added: [], add(node) { this.added.push(node); } }; const display = new TransformGizmo(gizmoScene); assert.equal(gizmoScene.added.length, 1); display.update(null); display.update(base); display.dispose(); });