/* Edit-mode element transform helpers: the desktop rules these mirror are * calculateCenterMedian over element centers and getTransformOrientation_ex / * createSpaceNormalTangent for the NORMAL basis. */ 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/mesh-element-transform.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, []); const moduleUrl = "data:text/javascript;base64," + Buffer.from(transpiled.outputText).toString("base64"); const { elementCenters, elementVertexIndices, mat4InvertAffine, mat4Multiply, medianPoint, normalBasis, selectionIslands, selectionNormal, transformPoint, } = await import(moduleUrl); /* A 1x1 quad in the XY plane, split into two triangles, plus one free vertex. */ const geometry = { positions: new Float32Array([ 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 5, 5, 0, ]), normals: new Float32Array([ 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, ]), /* Face 0 = the quad (two triangles), face 1 = one triangle wound the other way. */ indices: new Uint32Array([0, 1, 2, 1, 3, 2, 0, 2, 4]), triangleCornerIndices: new Uint32Array([0, 1, 2, 3, 4, 5, 6, 7, 8]), triangleFaceIndices: new Uint32Array([0, 0, 1]), edgeVertexIndices: new Uint32Array([0, 1, 1, 3, 3, 2, 2, 0]), }; const round = (values) => values.map((value) => Number(value.toFixed(6)) + 0); test("FACE selection expands to the face's unique vertices", () => { const { elementVertexIndices } = context; assert.deepEqual(elementVertexIndices("FACE", [0], geometry), [0, 1, 2, 3]); assert.deepEqual(elementVertexIndices("FACE", [1], geometry), [0, 2, 4]); assert.deepEqual(elementVertexIndices("EDGE", [1], geometry), [1, 3]); assert.deepEqual(elementVertexIndices("VERT", [4, 0], geometry), [0, 4]); }); test("face center and median point follow the element centers", () => { assert.deepEqual(round(elementCenters("FACE", [0], geometry)[0]), [0.5, 0.5, 0]); assert.deepEqual(round(elementCenters("EDGE", [0], geometry)[0]), [0.5, 0, 0]); assert.deepEqual(round(elementCenters("VERT", [4], geometry)[0]), [5, 5, 0]); assert.deepEqual(round(medianPoint([[0, 0, 0], [4, 2, 0]])), [2, 1, 0]); }); test("selection normal is the averaged face normal (desktop sums fac->no)", () => { assert.deepEqual(round(selectionNormal("FACE", [0], geometry)), [0, 0, 1]); assert.deepEqual(round(selectionNormal("FACE", [1], geometry)), [0, 0, -1]); assert.deepEqual(round(selectionNormal("VERT", [0, 1], geometry)), [0, 0, 1]); }); const cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]]; const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; test("normal basis matches createSpaceNormalTangent (Z = normal, Y = -tangent)", () => { /* desktop: mat[2]=normal, mat[1]=-tangent, mat[0]=Z×Y, mat[1]=Z×X */ const [x, y, z] = normalBasis([0, 0, 1], [1, 0, 0]); assert.deepEqual(round(z), [0, 0, 1]); assert.deepEqual(round(x), [0, -1, 0]); assert.deepEqual(round(y), [1, 0, 0]); assert.ok(Math.abs(dot(x, y)) < 1e-6 && Math.abs(dot(x, z)) < 1e-6 && Math.abs(dot(y, z)) < 1e-6); assert.ok(Math.abs(dot(cross(x, y), z) - 1) < 1e-6); const [ax, ay, az] = normalBasis([1, 0, 0], [0, 1, 0]); assert.deepEqual(round(az), [1, 0, 0]); assert.ok(Math.abs(dot(ax, az)) < 1e-6 && Math.abs(dot(ay, az)) < 1e-6); assert.ok(Math.abs(dot(cross(ax, ay), az) - 1) < 1e-6); }); test("affine inverse and multiply round trip, and points follow the matrix", () => { /* Column-major: rotate 90° about Z, then translate. */ const rotation = [0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]; const translate = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 4, 2, 0, 1]; const combined = mat4Multiply(translate, rotation); assert.deepEqual(round(transformPoint(combined, [1, 0, 0])), [4, 3, 0]); const identity = mat4Multiply(combined, mat4InvertAffine(combined)); const expected = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]; round(identity).forEach((value, index) => assert.ok(Math.abs(value - expected[index]) < 1e-5, `${index}: ${value}`)); assert.deepEqual(round(transformPoint(mat4InvertAffine(combined), [4, 3, 0])), [1, 0, 0]); }); const context = { elementVertexIndices, elementCenters, medianPoint, selectionNormal, normalBasis, mat4InvertAffine, mat4Multiply, transformPoint }; /* INDIVIDUAL_ORIGINS pivots on connected islands, like desktop's * `transform_convert_mesh_islands_calc`: one island per connected selection * group, centered on its own average vertex position. */ test("selectionIslands splits the selection into connected components", () => { /* 0-1-3 are chained by edges; 4 is selected but unconnected. */ const islands = selectionIslands([0, 1, 3, 4], geometry); assert.equal(islands.length, 2); assert.deepEqual(islands[0].vertexIndices, [0, 1, 3]); assert.deepEqual(round(islands[0].center), round([2 / 3, 1 / 3, 0])); assert.deepEqual(islands[1].vertexIndices, [4]); assert.deepEqual(round(islands[1].center), [5, 5, 0]); }); test("selectionIslands keeps a connected selection as one island", () => { const islands = selectionIslands([0, 1, 2, 3], geometry); assert.equal(islands.length, 1); assert.deepEqual(islands[0].vertexIndices, [0, 1, 2, 3]); assert.deepEqual(round(islands[0].center), [0.5, 0.5, 0]); }); test("selectionIslands ignores edges to unselected vertices and empty input", () => { assert.deepEqual(selectionIslands([], geometry), []); /* 0 and 3 sit in the same face but share no edge; every path between them * runs through unselected vertices, so they are two islands. */ const islands = selectionIslands([0, 3], geometry); assert.equal(islands.length, 2); assert.deepEqual(islands.map((island) => island.vertexIndices), [[0], [3]]); /* Without an edge list every selected vertex is its own island. */ const bare = { positions: geometry.positions }; assert.equal(selectionIslands([0, 1], bare).length, 2); });