From 096b7c7ffd3c35d262b476df4874f6dab948fd30 Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Fri, 18 Sep 2020 17:23:34 +0100 Subject: [PATCH] refactor(matrices): add/update types, minor refactor --- packages/matrices/src/api.ts | 2 ++ packages/matrices/src/concat.ts | 2 +- packages/matrices/src/frustum.ts | 2 +- packages/matrices/src/lookat.ts | 2 +- packages/matrices/src/ortho.ts | 2 +- packages/matrices/src/perspective.ts | 2 +- packages/matrices/src/quat-axis-angle.ts | 2 +- packages/matrices/src/quat-m33.ts | 6 +++--- packages/matrices/src/quat-m44.ts | 4 ++-- packages/matrices/src/rotation-around-axis.ts | 2 +- packages/matrices/src/rotation.ts | 18 +++++++++--------- packages/matrices/src/scale.ts | 12 ++++++------ packages/matrices/src/shear.ts | 4 ++-- packages/matrices/src/skew.ts | 5 ++--- packages/matrices/src/translation.ts | 4 ++-- packages/matrices/src/viewport.ts | 2 +- 16 files changed, 36 insertions(+), 35 deletions(-) diff --git a/packages/matrices/src/api.ts b/packages/matrices/src/api.ts index 93fdb7550f..5eea979da3 100644 --- a/packages/matrices/src/api.ts +++ b/packages/matrices/src/api.ts @@ -14,8 +14,10 @@ export type MultiMatOp = MultiVecOp; export type MatOp1 = (out: Mat | null) => Mat; export type MatOpM = (out: Mat | null, a: ReadonlyMat) => Mat; +export type MatOpV = (out: Mat | null, a: ReadonlyVec) => Mat; export type MatOpMU = (out: Mat | null, a: ReadonlyMat) => Mat | undefined; export type MatOpN = (out: Mat | null, n: number) => Mat; +export type MatOpNV = (out: Mat | null, n: number | ReadonlyVec) => Mat; export type MatOpMM = (out: Mat | null, a: ReadonlyMat, b: ReadonlyMat) => Mat; export type MatOpMV = (out: Vec | null, a: ReadonlyMat, b: ReadonlyVec) => Vec; export type MatOpMN = (out: Mat | null, a: ReadonlyMat, n: number) => Mat; diff --git a/packages/matrices/src/concat.ts b/packages/matrices/src/concat.ts index 5c11c03e7b..c1591bbbe4 100644 --- a/packages/matrices/src/concat.ts +++ b/packages/matrices/src/concat.ts @@ -16,4 +16,4 @@ export const concat = ( a: ReadonlyMat, b: ReadonlyMat, ...xs: ReadonlyMat[] -) => xs.reduce((acc: Mat, x) => mulM(acc, acc, x), mulM(out, a, b)); +): Mat => xs.reduce((acc: Mat, x) => mulM(acc, acc, x), mulM(out, a, b)); diff --git a/packages/matrices/src/frustum.ts b/packages/matrices/src/frustum.ts index a790a306c9..1125172dd8 100644 --- a/packages/matrices/src/frustum.ts +++ b/packages/matrices/src/frustum.ts @@ -22,7 +22,7 @@ export const frustum = ( top: number, near: number, far: number -) => { +): Mat => { const dx = 1 / (right - left); const dy = 1 / (top - bottom); const dz = 1 / (far - near); diff --git a/packages/matrices/src/lookat.ts b/packages/matrices/src/lookat.ts index faf19be8a7..c671d56c0f 100644 --- a/packages/matrices/src/lookat.ts +++ b/packages/matrices/src/lookat.ts @@ -23,7 +23,7 @@ export const lookAt = ( eye: ReadonlyVec, target: ReadonlyVec, up: ReadonlyVec -) => { +): Mat => { const z = normalize(null, sub3([], eye, target)); const x = normalize(null, cross3([], up, z)); const y = normalize(null, cross3([], z, x)); diff --git a/packages/matrices/src/ortho.ts b/packages/matrices/src/ortho.ts index 4e9eff65e3..baf5ab6fcc 100644 --- a/packages/matrices/src/ortho.ts +++ b/packages/matrices/src/ortho.ts @@ -21,7 +21,7 @@ export const ortho = ( top: number, near: number, far: number -) => { +): Mat => { const dx = 1 / (right - left); const dy = 1 / (top - bottom); const dz = 1 / (far - near); diff --git a/packages/matrices/src/perspective.ts b/packages/matrices/src/perspective.ts index 23b02d4aec..fa22987830 100644 --- a/packages/matrices/src/perspective.ts +++ b/packages/matrices/src/perspective.ts @@ -17,7 +17,7 @@ export const perspective = ( aspect: number, near: number, far: number -) => { +): Mat => { const f = frustumBounds(fov, aspect, near, far); return frustum(out, f.left, f.right, f.bottom, f.top, f.near, f.far); }; diff --git a/packages/matrices/src/quat-axis-angle.ts b/packages/matrices/src/quat-axis-angle.ts index 58ab625b10..d617366022 100644 --- a/packages/matrices/src/quat-axis-angle.ts +++ b/packages/matrices/src/quat-axis-angle.ts @@ -9,7 +9,7 @@ import { normalize, ReadonlyVec } from "@thi.ng/vectors"; * @param theta - */ export const quatFromAxisAngle = (axis: ReadonlyVec, theta: number) => { - theta /= 2; + theta *= 0.5; return normalize([0, 0, 0, Math.cos(theta)], axis, Math.sin(theta)); }; diff --git a/packages/matrices/src/quat-m33.ts b/packages/matrices/src/quat-m33.ts index 84be661560..932889e1ab 100644 --- a/packages/matrices/src/quat-m33.ts +++ b/packages/matrices/src/quat-m33.ts @@ -1,5 +1,5 @@ -import { ReadonlyVec, setC } from "@thi.ng/vectors"; -import type { Mat } from "./api"; +import { setC } from "@thi.ng/vectors"; +import type { MatOpV } from "./api"; /** * Converts quaternion into 3x3 matrix and writes result to `out`. @@ -7,7 +7,7 @@ import type { Mat } from "./api"; * @param out - * @param q - */ -export const quatToMat33 = (out: Mat | null, q: ReadonlyVec) => { +export const quatToMat33: MatOpV = (out, q) => { const [x, y, z, w] = q; const x2 = x + x; const y2 = y + y; diff --git a/packages/matrices/src/quat-m44.ts b/packages/matrices/src/quat-m44.ts index b6bfe61e4e..4dd01ac5c8 100644 --- a/packages/matrices/src/quat-m44.ts +++ b/packages/matrices/src/quat-m44.ts @@ -2,8 +2,8 @@ import { ReadonlyVec, setC, ZERO3 } from "@thi.ng/vectors"; import type { Mat } from "./api"; /** - * Converts quaternion into 4x4 matrix with optional translation offset - * `t`, then writes result to `out`. + * Converts quaternion into 4x4 matrix with optional 3D translation offset `t`, + * then writes result to `out`. * * @param out - * @param q - diff --git a/packages/matrices/src/rotation-around-axis.ts b/packages/matrices/src/rotation-around-axis.ts index 8598623500..563191c205 100644 --- a/packages/matrices/src/rotation-around-axis.ts +++ b/packages/matrices/src/rotation-around-axis.ts @@ -18,7 +18,7 @@ export const rotationAroundAxis33 = ( axis: ReadonlyVec, theta: number, normalize = false -) => { +): Mat => { const [x, y, z] = normalize ? _normalize([], axis) : axis; const [s, c] = sincos(theta); const t = 1 - c; diff --git a/packages/matrices/src/rotation.ts b/packages/matrices/src/rotation.ts index 993dc65d3f..1f803f02e5 100644 --- a/packages/matrices/src/rotation.ts +++ b/packages/matrices/src/rotation.ts @@ -1,6 +1,6 @@ import { sincos } from "@thi.ng/math"; import { setC, setC4, setC6 } from "@thi.ng/vectors"; -import type { Mat } from "./api"; +import type { MatOpN } from "./api"; /** * Constructs a 2x2 matrix rotation matrix for given `theta`. @@ -8,7 +8,7 @@ import type { Mat } from "./api"; * @param out - * @param theta - */ -export const rotation22 = (out: Mat | null, theta: number) => { +export const rotation22: MatOpN = (out, theta) => { const [s, c] = sincos(theta); return setC4(out || [], c, s, -s, c); }; @@ -19,7 +19,7 @@ export const rotation22 = (out: Mat | null, theta: number) => { * @param out - * @param theta - */ -export const rotation23 = (out: Mat | null, theta: number) => { +export const rotation23: MatOpN = (out, theta) => { const [s, c] = sincos(theta); return setC6(out || [], c, s, -s, c, 0, 0); }; @@ -30,7 +30,7 @@ export const rotation23 = (out: Mat | null, theta: number) => { * @param out - * @param theta - */ -export const rotationX33 = (out: Mat | null, theta: number) => { +export const rotationX33: MatOpN = (out, theta) => { const [s, c] = sincos(theta); return setC(out || [], 1, 0, 0, 0, c, s, 0, -s, c); }; @@ -41,7 +41,7 @@ export const rotationX33 = (out: Mat | null, theta: number) => { * @param out - * @param theta - */ -export const rotationY33 = (out: Mat | null, theta: number) => { +export const rotationY33: MatOpN = (out, theta) => { const [s, c] = sincos(theta); return setC(out || [], c, 0, -s, 0, 1, 0, s, 0, c); }; @@ -52,7 +52,7 @@ export const rotationY33 = (out: Mat | null, theta: number) => { * @param out - * @param theta - */ -export const rotationZ33 = (out: Mat | null, theta: number) => { +export const rotationZ33: MatOpN = (out, theta) => { const [s, c] = sincos(theta); return setC(out || [], c, s, 0, -s, c, 0, 0, 0, 1); }; @@ -63,7 +63,7 @@ export const rotationZ33 = (out: Mat | null, theta: number) => { * @param out - * @param theta - */ -export const rotationX44 = (out: Mat | null, theta: number) => { +export const rotationX44: MatOpN = (out, theta) => { const [s, c] = sincos(theta); return setC(out || [], 1, 0, 0, 0, 0, c, s, 0, 0, -s, c, 0, 0, 0, 0, 1); }; @@ -74,7 +74,7 @@ export const rotationX44 = (out: Mat | null, theta: number) => { * @param out - * @param theta - */ -export const rotationY44 = (out: Mat | null, theta: number) => { +export const rotationY44: MatOpN = (out, theta) => { const [s, c] = sincos(theta); return setC(out || [], c, 0, -s, 0, 0, 1, 0, 0, s, 0, c, 0, 0, 0, 0, 1); }; @@ -85,7 +85,7 @@ export const rotationY44 = (out: Mat | null, theta: number) => { * @param out - * @param theta - */ -export const rotationZ44 = (out: Mat | null, theta: number) => { +export const rotationZ44: MatOpN = (out, theta) => { const [s, c] = sincos(theta); return setC(out || [], c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); }; diff --git a/packages/matrices/src/scale.ts b/packages/matrices/src/scale.ts index 781ceec4d8..b189e07210 100644 --- a/packages/matrices/src/scale.ts +++ b/packages/matrices/src/scale.ts @@ -1,6 +1,6 @@ import { isNumber } from "@thi.ng/checks"; -import { ReadonlyVec, setC, setC4, setC6 } from "@thi.ng/vectors"; -import type { Mat } from "./api"; +import { setC, setC4, setC6 } from "@thi.ng/vectors"; +import type { MatOpNV } from "./api"; /** * Computes 2x2 matrix scale matrix and writes result to `out`. If `s` @@ -9,7 +9,7 @@ import type { Mat } from "./api"; * @param m - * @param s - */ -export const scale22 = (m: Mat | null, s: number | ReadonlyVec) => ( +export const scale22: MatOpNV = (m, s) => ( (s = isNumber(s) ? [s, s] : s), setC4(m || [], s[0], 0, 0, s[1]) ); @@ -20,7 +20,7 @@ export const scale22 = (m: Mat | null, s: number | ReadonlyVec) => ( * @param m - * @param s - */ -export const scale23 = (m: Mat | null, s: number | ReadonlyVec) => ( +export const scale23: MatOpNV = (m, s) => ( (s = isNumber(s) ? [s, s] : s), setC6(m || [], s[0], 0, 0, s[1], 0, 0) ); @@ -31,7 +31,7 @@ export const scale23 = (m: Mat | null, s: number | ReadonlyVec) => ( * @param m - * @param s - */ -export const scale33 = (m: Mat | null, s: number | ReadonlyVec) => ( +export const scale33: MatOpNV = (m, s) => ( (s = isNumber(s) ? [s, s, s] : s), setC(m || [], s[0], 0, 0, 0, s[1], 0, 0, 0, s[2]) ); @@ -43,7 +43,7 @@ export const scale33 = (m: Mat | null, s: number | ReadonlyVec) => ( * @param m - * @param s - */ -export const scale44 = (m: Mat | null, s: number | ReadonlyVec) => ( +export const scale44: MatOpNV = (m, s) => ( (s = isNumber(s) ? [s, s, s] : s), setC( m || [], diff --git a/packages/matrices/src/shear.ts b/packages/matrices/src/shear.ts index 54f8d73a4d..41b275f098 100644 --- a/packages/matrices/src/shear.ts +++ b/packages/matrices/src/shear.ts @@ -1,7 +1,7 @@ -import type { Mat, MatOp1 } from "./api"; +import type { MatOp1, MatOpN } from "./api"; import { identity22, identity23, identity33, identity44 } from "./identity"; -const $ = (f: MatOp1) => (i: number) => (m: Mat | null, x: number) => ( +const $ = (f: MatOp1) => (i: number): MatOpN => (m, x) => ( !m && (m = []), f(m), (m[i] = x), m ); diff --git a/packages/matrices/src/skew.ts b/packages/matrices/src/skew.ts index 768af3e674..29a14c3bcd 100644 --- a/packages/matrices/src/skew.ts +++ b/packages/matrices/src/skew.ts @@ -1,4 +1,4 @@ -import type { Mat, MatOpN } from "./api"; +import type { MatOpN } from "./api"; import { shearX22, shearX23, @@ -18,8 +18,7 @@ import { shearZY44, } from "./shear"; -const $ = (f: MatOpN) => (m: Mat | null, theta: number) => - f(m, Math.tan(theta)); +const $ = (f: MatOpN): MatOpN => (m, theta) => f(m, Math.tan(theta)); export const skewX22 = $(shearX22); export const skewY22 = $(shearY22); diff --git a/packages/matrices/src/translation.ts b/packages/matrices/src/translation.ts index 98e303e1b6..ac789658b1 100644 --- a/packages/matrices/src/translation.ts +++ b/packages/matrices/src/translation.ts @@ -7,7 +7,7 @@ import type { Mat } from "./api"; * @param out - * @param v - */ -export const translation23 = (m: Mat | null, v: ReadonlyVec) => +export const translation23 = (m: Mat | null, v: ReadonlyVec): Mat => setC6(m || [], 1, 0, 0, 1, v[0], v[1]); /** @@ -16,5 +16,5 @@ export const translation23 = (m: Mat | null, v: ReadonlyVec) => * @param out - * @param v - */ -export const translation44 = (m: Mat | null, v: ReadonlyVec) => +export const translation44 = (m: Mat | null, v: ReadonlyVec): Mat => setC(m || [], 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, v[0], v[1], v[2], 1); diff --git a/packages/matrices/src/viewport.ts b/packages/matrices/src/viewport.ts index 0bc7609525..5771ea4d6f 100644 --- a/packages/matrices/src/viewport.ts +++ b/packages/matrices/src/viewport.ts @@ -19,7 +19,7 @@ export const viewport = ( right: number, bottom: number, top: number -) => { +): Mat => { const x = (left + right) / 2; const y = (bottom + top) / 2; const w = (right - left) / 2;