Skip to content

Commit

Permalink
feat: remove quat/mat4.targetTo and quat.fromTo
Browse files Browse the repository at this point in the history
Closes #30
Closes #31
  • Loading branch information
dmnsgn committed Feb 5, 2024
1 parent 839a776 commit 041313b
Show file tree
Hide file tree
Showing 6 changed files with 0 additions and 222 deletions.
16 changes: 0 additions & 16 deletions benchmark/bench-gl-matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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();
Expand Down
17 changes: 0 additions & 17 deletions benchmark/bench-pex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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();
Expand Down
59 changes: 0 additions & 59 deletions mat4.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 0 additions & 32 deletions quat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
78 changes: 0 additions & 78 deletions test/mat4.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 0 additions & 20 deletions test/quat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down

0 comments on commit 041313b

Please sign in to comment.