Skip to content

Commit

Permalink
refactor(matrices): add/update types, minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 18, 2020
1 parent b341e1c commit 096b7c7
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 35 deletions.
2 changes: 2 additions & 0 deletions packages/matrices/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ export type MultiMatOp<T> = MultiVecOp<T>;

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;
Expand Down
2 changes: 1 addition & 1 deletion packages/matrices/src/concat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
2 changes: 1 addition & 1 deletion packages/matrices/src/frustum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/matrices/src/lookat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion packages/matrices/src/ortho.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/matrices/src/perspective.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
2 changes: 1 addition & 1 deletion packages/matrices/src/quat-axis-angle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};

Expand Down
6 changes: 3 additions & 3 deletions packages/matrices/src/quat-m33.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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`.
*
* @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;
Expand Down
4 changes: 2 additions & 2 deletions packages/matrices/src/quat-m44.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Expand Down
2 changes: 1 addition & 1 deletion packages/matrices/src/rotation-around-axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 9 additions & 9 deletions packages/matrices/src/rotation.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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`.
*
* @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);
};
Expand All @@ -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);
};
Expand All @@ -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);
};
Expand All @@ -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);
};
Expand All @@ -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);
};
Expand All @@ -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);
};
Expand All @@ -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);
};
Expand All @@ -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);
};
12 changes: 6 additions & 6 deletions packages/matrices/src/scale.ts
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -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])
);

Expand All @@ -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)
);

Expand All @@ -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])
);
Expand All @@ -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 || [],
Expand Down
4 changes: 2 additions & 2 deletions packages/matrices/src/shear.ts
Original file line number Diff line number Diff line change
@@ -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
);

Expand Down
5 changes: 2 additions & 3 deletions packages/matrices/src/skew.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Mat, MatOpN } from "./api";
import type { MatOpN } from "./api";
import {
shearX22,
shearX23,
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions packages/matrices/src/translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

/**
Expand All @@ -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);
2 changes: 1 addition & 1 deletion packages/matrices/src/viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 096b7c7

Please sign in to comment.