/* Pivot and orientation basis shared by the modal transform and the 3D gizmo. * Desktop reference: transform_convert_object.cc feeds `td->center` (median, * bound, active or cursor) and transform_orientations.cc picks the axes. */ 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 sourcePath = path.join(repoRoot, "web/app/src/three-adapter/transform-pivot.ts"); const transpiled = ts.transpileModule(fs.readFileSync(sourcePath, "utf8"), { compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 }, fileName: sourcePath, reportDiagnostics: true, }); assert.deepEqual(transpiled.diagnostics, []); /* Only type imports come from viewport-transform, so no three stub is needed. */ const moduleUrl = "data:text/javascript;base64," + Buffer.from(transpiled.outputText).toString("base64"); const { axesForOrientation, localAxesOf, normalize, originOf, pivotCenter, toBlender } = await import(moduleUrl); const round = (values) => values.map((value) => Number(value.toFixed(6)) + 0); const identity = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]; /** Column-major matrix placing an object at (x,y,z) with scale/rotation applied. */ const placed = (x, y, z) => [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1]; test("display Y-up maps to Blender Z-up and back", () => { /* Blender (x,y,z) → display (x, z, -y); the inverse used for eye/forward. */ assert.deepEqual(toBlender(1, 2, 3), [1, -3, 2]); assert.deepEqual(normalize([0, 0, 0]), [0, 0, 0]); assert.deepEqual(round(normalize([0, 3, 4])), [0, 0.6, 0.8]); }); test("local axes are the normalised matrix columns", () => { const rotated = [0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 5, 6, 7, 1]; assert.deepEqual(round(originOf({ id: "a", worldMatrix: rotated })), [5, 6, 7]); const axes = localAxesOf(rotated); assert.deepEqual(round(axes[0]), [0, 1, 0]); assert.deepEqual(round(axes[1]), [-1, 0, 0]); assert.deepEqual(round(axes[2]), [0, 0, 1]); }); test("builtin orientations: GLOBAL is world, LOCAL/NORMAL/GIMBAL follow the object", () => { const rotated = [0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]; assert.deepEqual(round(axesForOrientation("GLOBAL", rotated)[0]), [1, 0, 0]); for (const name of ["LOCAL", "NORMAL", "GIMBAL"]) { assert.deepEqual(round(axesForOrientation(name, rotated)[0]), [0, 1, 0], name); } /* A custom orientation is stored as RNA rows; column c is (m[0][c], m[1][c], m[2][c]). */ const custom = [0, 0, 1, 0, 1, 0, -1, 0, 0]; const axes = axesForOrientation("MyRig", identity, custom); assert.deepEqual(round(axes[0]), [0, 0, -1]); assert.deepEqual(round(axes[1]), [0, 1, 0]); assert.deepEqual(round(axes[2]), [1, 0, 0]); /* A builtin name must never be overridden by a stale custom matrix. */ assert.deepEqual(round(axesForOrientation("GLOBAL", identity, custom)[0]), [1, 0, 0]); }); test("VIEW takes its axes from the published viewport camera", () => { globalThis.window = { __webViewportCamera: { matrixWorld: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] } }; assert.deepEqual(round(axesForOrientation("VIEW", identity)[0]), [1, 0, 0]); /* Third axis is the camera forward: display -Z is Blender +Y. */ assert.deepEqual(round(axesForOrientation("VIEW", identity)[2]), [0, 1, 0]); /* Display +Y (up) is Blender +Z. */ assert.deepEqual(round(axesForOrientation("VIEW", identity)[1]), [0, 0, 1]); delete globalThis.window; }); test("pivot kinds match the desktop object-mode rules", () => { const nodes = [ { id: "a", worldMatrix: placed(0, 0, 0) }, { id: "b", worldMatrix: placed(2, 0, 0) }, ]; const cursor = { pivot: "CURSOR", cursorLocation: [5, 5, 5] }; assert.deepEqual(pivotCenter(cursor, nodes, "a"), [5, 5, 5]); /* INDIVIDUAL_ORIGINS has no shared pivot: the modal transforms each origin. */ assert.equal(pivotCenter({ pivot: "INDIVIDUAL_ORIGINS", cursorLocation: [0, 0, 0] }, nodes, "a"), undefined); assert.deepEqual(round(pivotCenter({ pivot: "MEDIAN_POINT", cursorLocation: [0, 0, 0] }, nodes, "a")), [1, 0, 0]); assert.deepEqual(round(pivotCenter({ pivot: "ACTIVE_ELEMENT", cursorLocation: [0, 0, 0] }, nodes, "b")), [2, 0, 0]); /* ACTIVE_ELEMENT with no active member falls back to the median. */ assert.deepEqual(round(pivotCenter({ pivot: "ACTIVE_ELEMENT", cursorLocation: [0, 0, 0] }, nodes, "missing")), [1, 0, 0]); assert.deepEqual(round(pivotCenter({ pivot: "BOUNDING_BOX_CENTER", cursorLocation: [0, 0, 0] }, nodes, "a")), [1, 0, 0]); }); test("a single object pivots on its own origin for every shared pivot", () => { const single = [{ id: "a", worldMatrix: placed(1, 2, 3) }]; for (const pivot of ["MEDIAN_POINT", "BOUNDING_BOX_CENTER", "ACTIVE_ELEMENT"]) { assert.deepEqual(round(pivotCenter({ pivot, cursorLocation: [9, 9, 9] }, single, "a")), [1, 2, 3], pivot); } /* No selection at all still yields the cursor for a cursor pivot. */ assert.deepEqual(pivotCenter({ pivot: "CURSOR", cursorLocation: [4, 0, 0] }, [], null), [4, 0, 0]); });