From 041313bc04a5be56fe1e9ab77060334bbba8aa28 Mon Sep 17 00:00:00 2001 From: Damien Seguin Date: Mon, 5 Feb 2024 13:39:02 +0000 Subject: [PATCH] feat: remove quat/mat4.targetTo and quat.fromTo Closes #30 Closes #31 --- benchmark/bench-gl-matrix.js | 16 -------- benchmark/bench-pex.js | 17 -------- mat4.js | 59 --------------------------- quat.js | 32 --------------- test/mat4.js | 78 ------------------------------------ test/quat.js | 20 --------- 6 files changed, 222 deletions(-) diff --git a/benchmark/bench-gl-matrix.js b/benchmark/bench-gl-matrix.js index bb3ed64..5987dbb 100644 --- a/benchmark/bench-gl-matrix.js +++ b/benchmark/bench-gl-matrix.js @@ -118,13 +118,6 @@ bench("mat4.lookAt", (b) => { run(() => mat4.lookAt(m, [0, 4, 2], [0, 0, 0], [0, 1, 0])); b.end(); }); -bench("mat4.targetTo", (b) => { - const m = mat4.create(); - - b.start(); - run(() => mat4.targetTo(m, [0, 4, 2], [0, 0, 0], [0, 1, 0])); - b.end(); -}); // quat bench("quat.create", (b) => { @@ -232,15 +225,6 @@ bench("quat.fromMat4", (b) => { run(() => mat4.getRotation(q, m)); b.end(); }); -bench("quat.fromTo", (b) => { - const q = quat.create(); - const v = vec3.create(); - const w = vec3.create(); - - b.start(); - run(() => quat.rotationTo(q, v, w)); - b.end(); -}); bench("quat.slerp", (b) => { const q1 = quat.create(); const q2 = quat.create(); diff --git a/benchmark/bench-pex.js b/benchmark/bench-pex.js index 638ceaa..a988ecd 100644 --- a/benchmark/bench-pex.js +++ b/benchmark/bench-pex.js @@ -131,13 +131,6 @@ bench("mat4.lookAt", (b) => { run(() => mat4.lookAt(m, [0, 4, 2], [0, 0, 0], [0, 1, 0])); b.end(); }); -bench("mat4.targetTo", (b) => { - const m = mat4.create(); - - b.start(); - run(() => mat4.targetTo(m, [0, 4, 2], [0, 0, 0], [0, 1, 0])); - b.end(); -}); // quat bench("quat.create", (b) => { @@ -246,16 +239,6 @@ bench("quat.fromMat4", (b) => { run(() => quat.fromMat4(q, m)); b.end(); }); -bench("quat.fromTo", (b) => { - const q = quat.create(); - const v = vec3.create(); - const w = vec3.create(); - - b.start(); - run(() => quat.fromTo(q, v, w)); - b.end(); -}); -// bench("quat.targetTo", (b) => {}); bench("quat.slerp", (b) => { const q1 = quat.create(); const q2 = quat.create(); diff --git a/mat4.js b/mat4.js index 9b5cebf..c505b4a 100644 --- a/mat4.js +++ b/mat4.js @@ -827,65 +827,6 @@ export function lookAt( return a; } -/** - * Sets a matrix from a vector to another. - * @param {import("./types.js").mat4} a - * @param {import("./types.js").vec3} from - * @param {import("./types.js").vec3} to - * @param {import("./types.js").vec3} [up=Y_UP] - * @returns {import("./types.js").mat4} - */ -export function targetTo( - a, - [eyex, eyey, eyez], - [targetx, targety, targetz], - [upx, upy, upz] = Y_UP, -) { - let z0 = eyex - targetx; - let z1 = eyey - targety; - let z2 = eyez - targetz; - - let len = z0 * z0 + z1 * z1 + z2 * z2; - - if (len > 0) { - len = 1 / Math.sqrt(len); - z0 *= len; - z1 *= len; - z2 *= len; - } - - let x0 = upy * z2 - upz * z1; - let x1 = upz * z0 - upx * z2; - let x2 = upx * z1 - upy * z0; - - len = x0 * x0 + x1 * x1 + x2 * x2; - - if (len > 0) { - len = 1 / Math.sqrt(len); - x0 *= len; - x1 *= len; - x2 *= len; - } - - a[0] = x0; - a[1] = x1; - a[2] = x2; - a[3] = 0; - a[4] = z1 * x2 - z2 * x1; - a[5] = z2 * x0 - z0 * x2; - a[6] = z0 * x1 - z1 * x0; - a[7] = 0; - a[8] = z0; - a[9] = z1; - a[10] = z2; - a[11] = 0; - a[12] = eyex; - a[13] = eyey; - a[14] = eyez; - a[15] = 1; - return a; -} - /** * Sets a matrix from a direction. * Note: we assume +Z facing models. diff --git a/quat.js b/quat.js index c6add07..4d2623d 100644 --- a/quat.js +++ b/quat.js @@ -256,38 +256,6 @@ export function fromMat4(a, m) { return _fromMat39(a, m[0], m[1], m[2], m[4], m[5], m[6], m[8], m[9], m[10]); } -/** - * Sets a quaternion to represent the shortest rotation from one vector to another. - * @param {import("./types.js").quat} a - * @param {import("./types.js").vec3} v - * @param {import("./types.js").vec3} w - * @returns {import("./types.js").quat} - */ -export const fromTo = (() => { - let u = []; - return (a, v, w) => { - u = vec3.cross(vec3.set(u, v), w); - a[0] = u[0]; - a[1] = u[1]; - a[2] = u[2]; - a[3] = 1 + vec3.dot(v, w); - normalize(a); - return a; - }; -})(); - -/** - * Sets a quaternion from a vector to another. - * @param {import("./types.js").quat} a - * @param {import("./types.js").vec3} eye - * @param {import("./types.js").vec3} target - * @param {import("./types.js").vec3} [up=Y_UP] - * @returns {import("./types.js").quat} - */ -export function targetTo(a, eye, target, up) { - return fromMat4(a, mat4.targetTo(TEMP_MAT4, eye, target, up)); -} - /** * Sets a quaternion from a direction * Note: we assume +Z facing models. diff --git a/test/mat4.js b/test/mat4.js index 3537dbc..faf927c 100644 --- a/test/mat4.js +++ b/test/mat4.js @@ -246,84 +246,6 @@ describe("mat4", () => { ], ); }); - describe("targetTo()", () => { - it("should set a matrix from a vector to another", () => { - deepEqual( - mat4.targetTo(mat4.create(), Y_UP, ORIGIN, Y_UP), - // prettier-ignore - [ - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 1, 0, 0, - 0, 1, 0, 1 - ], - ); - }); - it("direction independent from distance", () => { - deepEqual( - mat4.targetTo(mat4.create(), Y_UP, [0, -100, 0], Y_UP), - // prettier-ignore - [ - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 1, 0, 0, - 0, 1, 0, 1 - ], - ); - }); - it("up", () => { - deepEqual( - mat4.targetTo(mat4.create(), Y_UP, ORIGIN, Y_DOWN), - // prettier-ignore - [ - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 1, 0, 0, - 0, 1, 0, 1 - ], - ); - deepEqual( - mat4.targetTo(mat4.create(), Y_UP, ORIGIN, X_UP), - // prettier-ignore - [ - 0, 0, 1, 0, - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 1, 0, 1 - ], - ); - deepEqual( - mat4.targetTo(mat4.create(), Y_UP, ORIGIN, X_DOWN), - // prettier-ignore - [ - 0, 0, -1, 0, - -1, 0, 0, 0, - 0, 1, 0, 0, - 0, 1, 0, 1 - ], - ); - deepEqual( - mat4.targetTo(mat4.create(), Y_UP, ORIGIN, Z_UP), - // prettier-ignore - [ - -1, 0, 0, 0, - 0, 0, 1, 0, - 0, 1, 0, 0, - 0, 1, 0, 1 - ], - ); - deepEqual( - mat4.targetTo(mat4.create(), Y_UP, ORIGIN, Z_DOWN), - // prettier-ignore - [ - 1, 0, 0, 0, - 0, 0, -1, 0, - 0, 1, 0, 0, - 0, 1, 0, 1 - ], - ); - }); - }); describe("fromDirection()", () => { it("should set a matrix to identity when direction is +Z", () => { deepEqual(mat4.fromDirection(mat4.create(), Z_UP, Y_UP), IDENTITY_MAT4); diff --git a/test/quat.js b/test/quat.js index 13bbe22..1a52e16 100644 --- a/test/quat.js +++ b/test/quat.js @@ -227,26 +227,6 @@ describe("quat", () => { Z_QUAT, ); }); - it("fromTo() should set a quaternion to represent the shortest rotation from one vector to another", () => { - deepEqual(quat.fromTo(quat.create(), Y_UP, Y_UP), IDENTITY_QUAT); - // Right angle - deepAlmostEqual(quat.fromTo(quat.create(), Y_UP, X_DOWN), Z_QUAT); - // Opposite - deepAlmostEqual(quat.fromTo(quat.create(), Y_UP, Y_DOWN), [0, 0, 0, 0]); - }); - it("targetTo() should set a quaternion from a vector to another", () => { - deepEqual( - quat.normalize(quat.targetTo(quat.create(), Y_UP, Y_UP)), - IDENTITY_QUAT, - ); - // Right angle - deepAlmostEqual( - quat.targetTo(quat.create(), Y_UP, vec3.add(vec3.copy(Y_UP), X_DOWN)), - Y_QUAT, - ); - // Opposite - deepAlmostEqual(quat.targetTo(quat.create(), X_UP, X_DOWN), Y_QUAT); - }); it("fromDirection() should set a quaternion from a direction", () => { deepEqual( quat.normalize(quat.fromDirection(quat.create(), Z_UP, Y_UP)),