Skip to content

Commit

Permalink
feat(vectors): add swizzle fns, update/unify fn naming
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jul 29, 2018
1 parent e3c6167 commit 5bba592
Show file tree
Hide file tree
Showing 13 changed files with 488 additions and 284 deletions.
6 changes: 6 additions & 0 deletions packages/vectors/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ export type Mat = number[] | TypedArray;
export type ReadonlyMat = ArrayLike<number>;

export type VecOp = (a: Vec, b: ReadonlyVec, ia: number, ib: number, sa: number, sb: number) => Vec;

export interface IVec {
buf: Vec;
i: number;
s: number;
}
6 changes: 3 additions & 3 deletions packages/vectors/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReadonlyVec, Vec, VecOp } from "./api";
import { EPS, eqDelta } from "./math";
import { EPS, eqDelta1 } from "./math";

export const x = (v: ReadonlyVec, i = 0, _?) => v[i];
export const y = (v: ReadonlyVec, i = 0, s = 1) => v[i + s];
Expand Down Expand Up @@ -43,9 +43,9 @@ export const sliceOpN = (fn: VecOp, a: Vec, b: ReadonlyVec, n: number, ia: numbe
return a;
};

export const eqDeltaN = (a: Vec, b: Vec, n: number, eps = EPS, ia = 0, ib = 0, sa = 1, sb = 1) => {
export const eqDelta = (a: Vec, b: Vec, n: number, eps = EPS, ia = 0, ib = 0, sa = 1, sb = 1) => {
for (; --n >= 0; ia += sa, ib += sb) {
if (!eqDelta(a[ia], b[ib], eps)) {
if (!eqDelta1(a[ia], b[ib], eps)) {
return false;
}
}
Expand Down
62 changes: 31 additions & 31 deletions packages/vectors/src/mat23.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ICopy, IEqualsDelta } from "@thi.ng/api/api";
import { isArrayLike } from "@thi.ng/checks/is-arraylike";
import { Mat, ReadonlyMat, Vec } from "./api";
import { eqDeltaN } from "./common";
import { eqDelta } from "./common";
import { EPS } from "./math";
import {
cross2,
dot2,
set2s,
setS2,
Vec2
} from "./vec2";

Expand Down Expand Up @@ -35,7 +35,7 @@ export const set23 = (a: Mat, b: Mat, ia = 0, ib = 0) => (
* @param m21
* @param i
*/
export const set23s = (m: Mat, m00: number, m01: number, m10: number, m11: number, m20: number, m21: number, i = 0) => (
export const setS23 = (m: Mat, m00: number, m01: number, m10: number, m11: number, m20: number, m21: number, i = 0) => (
m[i] = m00,
m[i + 1] = m01,
m[i + 2] = m10,
Expand All @@ -46,48 +46,48 @@ export const set23s = (m: Mat, m00: number, m01: number, m10: number, m11: numbe
);

export const identity23 = (m?: Mat, i = 0) =>
set23s(m || [], 1, 0, 0, 1, 0, 0, i);
setS23(m || [], 1, 0, 0, 1, 0, 0, i);

export const rotation23 = (m: Mat, theta: number, i = 0) => {
const s = Math.sin(theta);
const c = Math.cos(theta);
return set23s(m || [], c, s, -s, c, 0, 0, i);
return setS23(m || [], c, s, -s, c, 0, 0, i);
};

export const rotationAroundPoint23 = (m: Mat, p: Vec, theta: number, im = 0, iv = 0, sv = 1) =>
concat23(
translation23v(m || [], p, im, iv, sv), im,
translationV23(m || [], p, im, iv, sv), im,
rotation23([], theta),
translation23s([], -p[iv], -p[iv + sv])
translationS23([], -p[iv], -p[iv + sv])
);

export const scale23v = (m: Mat, v: Vec, i = 0, iv = 0, sv = 1) =>
scale23s(m, v[iv], v[iv + sv], i);
export const scaleV23 = (m: Mat, v: Vec, i = 0, iv = 0, sv = 1) =>
scaleS23(m, v[iv], v[iv + sv], i);

export const scale23n = (m: Mat, n: number, i = 0) =>
scale23s(m, n, n, i);
export const scaleN23 = (m: Mat, n: number, i = 0) =>
scaleS23(m, n, n, i);

export const scale23s = (m: Mat, sx: number, sy: number, i = 0) =>
set23s(m || [], sx, 0, 0, sy, 0, 0, i);
export const scaleS23 = (m: Mat, sx: number, sy: number, i = 0) =>
setS23(m || [], sx, 0, 0, sy, 0, 0, i);

export const scaleWithCenter23 = (m: Mat, p: Vec, sx: number, sy: number, im = 0, iv = 0, sv = 1) =>
concat23(
translation23v(m || [], p, im, iv, sv), im,
scale23s([], sx, sy),
translation23s([], -p[iv], -p[iv + sv])
translationV23(m || [], p, im, iv, sv), im,
scaleS23([], sx, sy),
translationS23([], -p[iv], -p[iv + sv])
);

export const translation23v = (m: Mat, v: Vec, i = 0, iv = 0, sv = 1) =>
translation23s(m, v[iv], v[iv + sv], i);
export const translationV23 = (m: Mat, v: Vec, i = 0, iv = 0, sv = 1) =>
translationS23(m, v[iv], v[iv + sv], i);

export const translation23s = (m: Mat, x: number, y: number, i = 0) =>
set23s(m || [], 1, 0, 0, 1, x, y, i);
export const translationS23 = (m: Mat, x: number, y: number, i = 0) =>
setS23(m || [], 1, 0, 0, 1, x, y, i);

export const shearX23 = (m: Mat, x: number, i = 0) =>
set23s(m || [], 1, 0, x, 1, 0, 0, i);
setS23(m || [], 1, 0, x, 1, 0, 0, i);

export const shearY23 = (m: Mat, y: number, i = 0) =>
set23s(m || [], 1, y, 0, 1, 0, 0, i);
setS23(m || [], 1, y, 0, 1, 0, 0, i);

export const skewX23 = (m: Mat, theta: number, i = 0) =>
shearX23(m, Math.tan(theta), i);
Expand All @@ -96,7 +96,7 @@ export const skewY23 = (m: Mat, theta: number, i = 0) =>
shearY23(m, Math.tan(theta), i);

export const mul23 = (a: Mat, b: ReadonlyMat, ia = 0, ib = 0) =>
set23s(
setS23(
a,
dot2(a, b, ia, ib, 2),
dot2(a, b, ia + 1, ib, 2),
Expand All @@ -116,7 +116,7 @@ export const concat23 = (a: Mat, ia: number, ...xs: (ReadonlyMat | [ReadonlyMat,
);

export const mulV23 = (m: ReadonlyMat, v: Vec, im = 0, iv = 0, sv = 1) =>
set2s(
setS2(
v,
dot2(m, v, im, iv, 2, sv) + m[im + 4],
dot2(m, v, im + 1, iv, 2, sv) + m[im + 5],
Expand All @@ -138,7 +138,7 @@ export const invert23 = (m: Mat, i = 0) => {
return;
}
det = 1.0 / det;
return set23s(
return setS23(
m,
m11 * det,
-m01 * det,
Expand Down Expand Up @@ -168,8 +168,8 @@ export class Mat23 implements
static scale(x: any, y = x) {
return new Mat23(
x instanceof Vec2 ?
scale23v([], x.buf, 0, x.i, x.s) :
scale23s([], x, y)
scaleV23([], x.buf, 0, x.i, x.s) :
scaleS23([], x, y)
);
}

Expand All @@ -182,8 +182,8 @@ export class Mat23 implements
static translation(x: any, y?: any) {
return new Mat23(
x instanceof Vec2 ?
translation23v([], x.buf, 0, x.i) :
translation23s([], x, y)
translationV23([], x.buf, 0, x.i) :
translationS23([], x, y)
);
}

Expand Down Expand Up @@ -221,7 +221,7 @@ export class Mat23 implements
}

eqDelta(m: Mat23, eps = EPS) {
return eqDeltaN(this.buf, m.buf, 6, eps, this.i, m.i);
return eqDelta(this.buf, m.buf, 6, eps, this.i, m.i);
}

identity() {
Expand All @@ -235,7 +235,7 @@ export class Mat23 implements
}

setS(m00: number, m01: number, m10: number, m11: number, m20: number, m21: number) {
set23s(this.buf, m00, m01, m10, m11, m20, m21, this.i);
setS23(this.buf, m00, m01, m10, m11, m20, m21, this.i);
return this;
}

Expand Down
48 changes: 24 additions & 24 deletions packages/vectors/src/mat33.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { ICopy, IEqualsDelta } from "@thi.ng/api/api";
import { isArrayLike } from "@thi.ng/checks/is-arraylike";
import { Mat, ReadonlyMat, Vec } from "./api";
import { eqDeltaN } from "./common";
import { eqDelta } from "./common";
import { EPS } from "./math";
import {
dot3,
set3,
set3s,
setS3,
Vec3
} from "./vec3";
import { set4s } from "./vec4";
import { setS4 } from "./vec4";

export const set33 = (a: Mat, b: Mat, ia = 0, ib = 0) => (
a[ia] = b[ib],
Expand Down Expand Up @@ -43,7 +43,7 @@ export const set33 = (a: Mat, b: Mat, ia = 0, ib = 0) => (
* @param m22
* @param i
*/
export const set33s = (m: Mat, m00: number, m01: number, m02: number, m10: number, m11: number, m12: number, m20: number, m21: number, m22: number, i = 0) => (
export const setS33 = (m: Mat, m00: number, m01: number, m02: number, m10: number, m11: number, m12: number, m20: number, m21: number, m22: number, i = 0) => (
m[i] = m00,
m[i + 1] = m01,
m[i + 2] = m02,
Expand All @@ -57,7 +57,7 @@ export const set33s = (m: Mat, m00: number, m01: number, m02: number, m10: numbe
);

export const identity33 = (m?: Mat, i = 0) =>
set33s(m || [],
setS33(m || [],
1, 0, 0,
0, 1, 0,
0, 0, 1,
Expand All @@ -67,7 +67,7 @@ export const identity33 = (m?: Mat, i = 0) =>
export const rotationX33 = (m: Mat, theta: number, i = 0) => {
const s = Math.sin(theta);
const c = Math.cos(theta);
return set33s(m || [],
return setS33(m || [],
1, 0, 0,
0, c, s,
0, -s, c,
Expand All @@ -78,7 +78,7 @@ export const rotationX33 = (m: Mat, theta: number, i = 0) => {
export const rotationY33 = (m: Mat, theta: number, i = 0) => {
const s = Math.sin(theta);
const c = Math.cos(theta);
return set33s(m || [],
return setS33(m || [],
c, 0, -s,
0, 1, 0,
s, 0, c,
Expand All @@ -89,30 +89,30 @@ export const rotationY33 = (m: Mat, theta: number, i = 0) => {
export const rotationZ33 = (m: Mat, theta: number, i = 0) => {
const s = Math.sin(theta);
const c = Math.cos(theta);
return set33s(m || [],
return setS33(m || [],
c, s, 0,
-s, c, 0,
0, 0, 1,
i
);
};

export const scale33v = (m: Mat, v: Vec, i = 0, iv = 0, sv = 1) =>
scale33s(m, v[iv], v[iv + sv], v[iv + 2 * sv], i);
export const scaleV33 = (m: Mat, v: Vec, i = 0, iv = 0, sv = 1) =>
scaleS33(m, v[iv], v[iv + sv], v[iv + 2 * sv], i);

export const scale33n = (m: Mat, n: number, i = 0) =>
scale33s(m, n, n, n, i);
export const scaleN33 = (m: Mat, n: number, i = 0) =>
scaleS33(m, n, n, n, i);

export const scale33s = (m: Mat, sx: number, sy: number, sz: number, i = 0) =>
set33s(m || [],
export const scaleS33 = (m: Mat, sx: number, sy: number, sz: number, i = 0) =>
setS33(m || [],
sx, 0, 0,
0, sy, 0,
0, 0, sz,
i
);

export const mul33 = (a: Mat, b: ReadonlyMat, ia = 0, ib = 0) =>
set33s(
setS33(
a,
dot3(a, b, ia, ib, 3),
dot3(a, b, ia + 1, ib, 3),
Expand All @@ -135,7 +135,7 @@ export const concat33 = (a: Mat, ia: number, ...xs: (ReadonlyMat | [ReadonlyMat,
);

export const mulV33 = (m: ReadonlyMat, v: Vec, im = 0, iv = 0, sv = 1) =>
set3s(
setS3(
v,
dot3(m, v, im, iv, 3, sv),
dot3(m, v, im + 1, iv, 3, sv),
Expand Down Expand Up @@ -177,7 +177,7 @@ export const invert33 = (m: Mat, i = 0) => {
return;
}
det = 1.0 / det;
return set33s(
return setS33(
m,
d01 * det,
(-m22 * m01 + m02 * m21) * det,
Expand All @@ -193,7 +193,7 @@ export const invert33 = (m: Mat, i = 0) => {
}

export const transpose33 = (m: Mat, i = 0) =>
set33s(
setS33(
m,
m[i], m[i + 3], m[i + 6],
m[i + 1], m[i + 4], m[i + 7],
Expand All @@ -205,8 +205,8 @@ export const mat33to44 = (m44: Mat, m33: Mat, ia = 0, ib = 0) => (
set3(m44, m33, ia, ib),
set3(m44, m33, ia + 4, ib + 3),
set3(m44, m33, ia + 8, ib + 6),
set3s(m44, 0, 0, 0, ia + 12),
set4s(m44, 0, 0, 0, 1, ia + 3, 4),
setS3(m44, 0, 0, 0, ia + 12),
setS4(m44, 0, 0, 0, 1, ia + 3, 4),
m44
);

Expand All @@ -232,8 +232,8 @@ export class Mat33 implements
static scale(x: any, y = x, z = x) {
return new Mat33(
x instanceof Vec3 ?
scale33v([], x.buf, 0, x.i) :
scale33s([], x, y, z)
scaleV33([], x.buf, 0, x.i) :
scaleS33([], x, y, z)
);
}

Expand All @@ -255,7 +255,7 @@ export class Mat33 implements
}

eqDelta(m: Mat33, eps = EPS) {
return eqDeltaN(this.buf, m.buf, 9, eps, this.i, m.i);
return eqDelta(this.buf, m.buf, 9, eps, this.i, m.i);
}

identity() {
Expand All @@ -269,7 +269,7 @@ export class Mat33 implements
}

setS(m00: number, m01: number, m02: number, m10: number, m11: number, m12: number, m20: number, m21: number, m22: number) {
set33s(this.buf, m00, m01, m02, m10, m11, m12, m20, m21, m22, this.i);
setS33(this.buf, m00, m01, m02, m10, m11, m12, m20, m21, m22, this.i);
return this;
}

Expand Down
Loading

0 comments on commit 5bba592

Please sign in to comment.