Skip to content

Commit

Permalink
feat(vectors): add matrix index & property accessors
Browse files Browse the repository at this point in the history
- refactor declareIndices() to take prop names
- add shared $iter helper fn
- update Mat23/33/44 & Vec2/3/4, GVec
  • Loading branch information
postspectacular committed Sep 10, 2018
1 parent ada7d34 commit 3dd0072
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 30 deletions.
27 changes: 20 additions & 7 deletions packages/vectors/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { ReadonlyVec, Vec, VecOp2, VecOp2o, ReadonlyVecOp1 } from "./api";
import {
ReadonlyVec,
ReadonlyVecOp1,
Vec,
VecOp2,
VecOp2o
} from "./api";
import { EPS, eqDelta1 } from "./math";

export const x: ReadonlyVecOp1<number> = (v: ReadonlyVec, i = 0) => v[i];
export const y: ReadonlyVecOp1<number> = (v: ReadonlyVec, i = 0, s = 1) => v[i + s];
export const z: ReadonlyVecOp1<number> = (v: ReadonlyVec, i = 0, s = 1) => v[i + 2 * s];
export const w: ReadonlyVecOp1<number> = (v: ReadonlyVec, i = 0, s = 1) => v[i + 3 * s];

export function* $iter(buf: Vec, n: number, i = 0, s = 1) {
for (; n > 0; n-- , i += s) {
yield buf[i];
}
}

/**
* Applies vector op `fn` to all raw vectors in array `a`, using the
* same raw vector `b` as 2nd argument for each iteration. Assumes `fn`
Expand Down Expand Up @@ -174,21 +186,22 @@ export const eqDelta = (a: ReadonlyVec, b: ReadonlyVec, n: number, eps = EPS, ia
};

/**
* Helper function to create property accessors for Vec2/3/4.
* Helper function to create vector/matrix index & property accessors.
*
* @param proto
* @param indices
* @param props
*/
export const declareIndices = (proto: any, indices: number[]) => {
const get = (i: number) => function () { return this.buf[this.i + i * this.s]; };
const set = (i: number) => function (n: number) { this.buf[this.i + i * this.s] = n; };
indices.forEach((i) => {
export const declareIndices = (proto: any, props: string[]) => {
const get = (i: number) => function () { return this.buf[this.i + i * (this.s || 1)]; };
const set = (i: number) => function (n: number) { this.buf[this.i + i * (this.s || 1)] = n; };
props.forEach((id, i) => {
Object.defineProperty(proto, i, {
get: get(i),
set: set(i),
enumerable: true,
});
Object.defineProperty(proto, "xyzw"[i], {
Object.defineProperty(proto, id, {
get: get(i),
set: set(i),
enumerable: true,
Expand Down
8 changes: 3 additions & 5 deletions packages/vectors/src/gvec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { isArrayLike } from "@thi.ng/checks/is-arraylike";
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";

import { IVec, ReadonlyVec, Vec } from "./api";
import { eqDelta, equiv } from "./common";
import { $iter, eqDelta, equiv } from "./common";
import {
clamp1,
EPS,
Expand Down Expand Up @@ -211,10 +211,8 @@ export class GVec implements
this.s = s;
}

*[Symbol.iterator]() {
for (let i = this.i, n = this.n; n > 0; n-- , i += this.s) {
yield this.buf[i];
}
[Symbol.iterator]() {
return $iter(this.buf, this.n, this.i, this.s);
}

get length() {
Expand Down
30 changes: 28 additions & 2 deletions packages/vectors/src/mat23.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { ICopy, IEqualsDelta } from "@thi.ng/api/api";
import { isArrayLike } from "@thi.ng/checks/is-arraylike";
import { Mat, ReadonlyMat, Vec, ReadonlyVec } from "./api";
import { eqDelta } from "./common";

import {
Mat,
ReadonlyMat,
ReadonlyVec,
Vec
} from "./api";
import { $iter, declareIndices, eqDelta } from "./common";
import { EPS } from "./math";
import {
cross2,
Expand Down Expand Up @@ -224,12 +230,27 @@ export class Mat23 implements

buf: Mat;
i: number;
m00: number;
m01: number;
m10: number;
m11: number;
m20: number;
m21: number;
[id: number]: number;

constructor(buf?: Mat, i = 0) {
this.buf = buf || (new Array(6).fill(0));
this.i = i;
}

[Symbol.iterator]() {
return $iter(this.buf, 6, this.i);
}

get length() {
return 6;
}

copy() {
return new Mat23(get23(this.buf, this.i));
}
Expand Down Expand Up @@ -282,3 +303,8 @@ export class Mat23 implements
return get23(this.buf, this.i);
}
}

declareIndices(
Mat23.prototype,
["m00", "m01", "m10", "m11", "m20", "m21"]
);
33 changes: 31 additions & 2 deletions packages/vectors/src/mat33.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { ICopy, IEqualsDelta } from "@thi.ng/api/api";
import { isArrayLike } from "@thi.ng/checks/is-arraylike";
import { Mat, ReadonlyMat, Vec, ReadonlyVec } from "./api";
import { eqDelta } from "./common";

import {
Mat,
ReadonlyMat,
ReadonlyVec,
Vec
} from "./api";
import { $iter, declareIndices, eqDelta } from "./common";
import { EPS } from "./math";
import {
dot3,
Expand Down Expand Up @@ -258,12 +264,30 @@ export class Mat33 implements

buf: Mat;
i: number;
m00: number;
m01: number;
m02: number;
m10: number;
m11: number;
m12: number;
m20: number;
m21: number;
m22: number;
[id: number]: number;

constructor(buf?: Mat, i = 0) {
this.buf = buf || (new Array(9).fill(0));
this.i = i;
}

[Symbol.iterator]() {
return $iter(this.buf, 9, this.i);
}

get length() {
return 9;
}

copy() {
return new Mat33(get33(this.buf, this.i));
}
Expand Down Expand Up @@ -325,3 +349,8 @@ ${b[i + 2]} ${b[i + 5]} ${b[i + 8]}`;
return get33(this.buf, this.i);
}
}

declareIndices(
Mat33.prototype,
["m00", "m01", "m02", "m10", "m11", "m12", "m20", "m21", "m22"]
);
45 changes: 43 additions & 2 deletions packages/vectors/src/mat44.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { ICopy, IEqualsDelta } from "@thi.ng/api/api";
import { isArrayLike } from "@thi.ng/checks/is-arraylike";
import { Mat, ReadonlyMat, Vec, ReadonlyVec } from "./api";
import { eqDelta } from "./common";

import {
Mat,
ReadonlyMat,
ReadonlyVec,
Vec
} from "./api";
import { $iter, declareIndices, eqDelta } from "./common";
import { Mat33 } from "./mat33";
import { EPS, rad } from "./math";
import {
Expand Down Expand Up @@ -463,12 +469,37 @@ export class Mat44 implements

buf: Mat;
i: number;
m00: number;
m01: number;
m02: number;
m03: number;
m10: number;
m11: number;
m12: number;
m13: number;
m20: number;
m21: number;
m22: number;
m23: number;
m30: number;
m31: number;
m32: number;
m33: number;
[id: number]: number;

constructor(buf?: Mat, i = 0) {
this.buf = buf || (new Array(16).fill(0));
this.i = i;
}

[Symbol.iterator]() {
return $iter(this.buf, 16, this.i);
}

get length() {
return 16;
}

copy() {
return new Mat44(get44(this.buf, this.i));
}
Expand Down Expand Up @@ -556,3 +587,13 @@ ${b[i + 3]} ${b[i + 7]} ${b[i + 11]} ${b[i + 15]}`;
return get44(this.buf, this.i);
}
}

declareIndices(
Mat44.prototype,
[
"m00", "m01", "m02", "m03",
"m10", "m11", "m12", "m13",
"m20", "m21", "m22", "m23",
"m30", "m31", "m32", "m33"
]
);
8 changes: 4 additions & 4 deletions packages/vectors/src/vec2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
Vec,
ZERO4
} from "./api";
import { declareIndices } from "./common";
import { declareIndices, $iter } from "./common";
import {
atan2Abs1,
EPS,
Expand Down Expand Up @@ -477,8 +477,8 @@ export class Vec2 implements
this.s = stride;
}

*[Symbol.iterator]() {
yield* this.array();
[Symbol.iterator]() {
return $iter(this.buf, 2, this.i, this.s);
}

array() {
Expand Down Expand Up @@ -781,4 +781,4 @@ export class Vec2 implements
}
}

declareIndices(Vec2.prototype, [0, 1]);
declareIndices(Vec2.prototype, ["x", "y"]);
8 changes: 4 additions & 4 deletions packages/vectors/src/vec3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
Vec,
ZERO4
} from "./api";
import { declareIndices } from "./common";
import { declareIndices, $iter } from "./common";
import {
atan2Abs1,
EPS,
Expand Down Expand Up @@ -613,8 +613,8 @@ export class Vec3 implements
this.s = stride;
}

*[Symbol.iterator]() {
yield* this.array();
[Symbol.iterator]() {
return $iter(this.buf, 3, this.i, this.s);
}

array() {
Expand Down Expand Up @@ -936,4 +936,4 @@ export class Vec3 implements
}
}

declareIndices(Vec3.prototype, [0, 1, 2]);
declareIndices(Vec3.prototype, ["x", "y", "z"]);
8 changes: 4 additions & 4 deletions packages/vectors/src/vec4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
Vec,
ZERO4
} from "./api";
import { declareIndices } from "./common";
import { declareIndices, $iter } from "./common";
import {
EPS,
eqDelta1,
Expand Down Expand Up @@ -549,8 +549,8 @@ export class Vec4 implements
this.s = stride;
}

*[Symbol.iterator]() {
yield* this.array();
[Symbol.iterator]() {
return $iter(this.buf, 4, this.i, this.s);
}

array() {
Expand Down Expand Up @@ -807,4 +807,4 @@ export class Vec4 implements
}
}

declareIndices(Vec4.prototype, [0, 1, 2, 3]);
declareIndices(Vec4.prototype, ["x", "y", "z", "w"]);

0 comments on commit 3dd0072

Please sign in to comment.