Skip to content

Commit

Permalink
feat(vectors): add immutable vec2/3/4 ops
Browse files Browse the repository at this point in the history
- also provide wrapped versions as static methods
  • Loading branch information
postspectacular committed Sep 4, 2018
1 parent be6ae48 commit a3c0407
Show file tree
Hide file tree
Showing 3 changed files with 369 additions and 3 deletions.
102 changes: 99 additions & 3 deletions packages/vectors/src/vec2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,27 @@ export const eqDelta2 = (a: ReadonlyVec, b: ReadonlyVec, eps = EPS, ia = 0, ib =
export const add2 = (a: Vec, b: ReadonlyVec, ia = 0, ib = 0, sa = 1, sb = 1) =>
(a[ia] += b[ib], a[ia + sa] += b[ib + sb], a);

export const mul2 = (a: Vec, b: ReadonlyVec, ia = 0, ib = 0, sa = 1, sb = 1) =>
(a[ia] *= b[ib], a[ia + sa] *= b[ib + sb], a);

export const sub2 = (a: Vec, b: ReadonlyVec, ia = 0, ib = 0, sa = 1, sb = 1) =>
(a[ia] -= b[ib], a[ia + sa] -= b[ib + sb], a);

export const mul2 = (a: Vec, b: ReadonlyVec, ia = 0, ib = 0, sa = 1, sb = 1) =>
(a[ia] *= b[ib], a[ia + sa] *= b[ib + sb], a);

export const div2 = (a: Vec, b: ReadonlyVec, ia = 0, ib = 0, sa = 1, sb = 1) =>
(a[ia] /= b[ib], a[ia + sa] /= b[ib + sb], a);

export const add2new = (out: Vec, a: ReadonlyVec, b: ReadonlyVec, io = 0, ia = 0, ib = 0, so = 1, sa = 1, sb = 1) =>
(out[io] = a[ia] + b[ib], out[io + so] = a[ia + sa] + b[ib + sb], out);

export const sub2new = (out: Vec, a: ReadonlyVec, b: ReadonlyVec, io = 0, ia = 0, ib = 0, so = 1, sa = 1, sb = 1) =>
(out[io] = a[ia] - b[ib], out[io + so] = a[ia + sa] - b[ib + sb], out);

export const mul2new = (out: Vec, a: ReadonlyVec, b: ReadonlyVec, io = 0, ia = 0, ib = 0, so = 1, sa = 1, sb = 1) =>
(out[io] = a[ia] * b[ib], out[io + so] = a[ia + sa] * b[ib + sb], out);

export const div2new = (out: Vec, a: ReadonlyVec, b: ReadonlyVec, io = 0, ia = 0, ib = 0, so = 1, sa = 1, sb = 1) =>
(out[io] = a[ia] / b[ib], out[io + so] = a[ia + sa] / b[ib + sb], out);

export const addN2 = (a: Vec, n: number, ia = 0, sa = 1) =>
(a[ia] += n, a[ia + sa] += n, a);

Expand All @@ -98,6 +110,18 @@ export const mulN2 = (a: Vec, n: number, ia = 0, sa = 1) =>
export const divN2 = (a: Vec, n: number, ia = 0, sa = 1) =>
(a[ia] /= n, a[ia + sa] /= n, a);

export const addN2new = (out: Vec, a: ReadonlyVec, n: number, io = 0, ia = 0, so = 1, sa = 1) =>
(out[io] = a[ia] + n, out[io + so] = a[ia + sa] + n, out);

export const subN2new = (out: Vec, a: ReadonlyVec, n: number, io = 0, ia = 0, so = 1, sa = 1) =>
(out[io] = a[ia] - n, out[io + so] = a[ia + sa] - n, out);

export const mulN2new = (out: Vec, a: ReadonlyVec, n: number, io = 0, ia = 0, so = 1, sa = 1) =>
(out[io] = a[ia] * n, out[io + so] = a[ia + sa] * n, out);

export const divN2new = (out: Vec, a: ReadonlyVec, n: number, io = 0, ia = 0, so = 1, sa = 1) =>
(out[io] = a[ia] / n, out[io + so] = a[ia + sa] / n, out);

export const neg2 = (a: Vec, ia = 0, sa = 1) =>
mulN2(a, -1, ia, sa);

Expand Down Expand Up @@ -167,6 +191,18 @@ export const mixN2 = (a: Vec, b: ReadonlyVec, n: number, ia = 0, ib = 0, sa = 1,
a
);

export const mix2new = (out: Vec, a: ReadonlyVec, b: ReadonlyVec, t: ReadonlyVec, io = 0, ia = 0, ib = 0, it = 0, so = 1, sa = 1, sb = 1, st = 1) => (
out[io] = a[ia] + (b[ib] - a[ia]) * t[it],
out[io + so] = a[ia + sa] + (b[ib + sb] - a[ia + sa]) * t[it + st],
out
);

export const mixN2new = (out: Vec, a: ReadonlyVec, b: ReadonlyVec, n: number, io = 0, ia = 0, ib = 0, so = 1, sa = 1, sb = 1) => (
out[io] = a[ia] + (b[ib] - a[ia]) * n,
out[io + so] = a[ia + sa] + (b[ib + sb] - a[ia + sa]) * n,
out
);

export const mixBilinear2 = (
a: Vec, b: ReadonlyVec, c: ReadonlyVec, d: ReadonlyVec, u: number, v: number,
ia = 0, ib = 0, ic = 0, id = 0,
Expand Down Expand Up @@ -363,6 +399,66 @@ export class Vec2 implements
);
}

static add(a: Readonly<Vec2>, b: Readonly<Vec2>, out?: Vec2) {
!out && (out = new Vec2([]));
add2new(out.buf, a.buf, b.buf, out.i, a.i, b.i, out.s, a.s, b.s);
return out;
}

static sub(a: Readonly<Vec2>, b: Readonly<Vec2>, out?: Vec2) {
!out && (out = new Vec2([]));
sub2new(out.buf, a.buf, b.buf, out.i, a.i, b.i, out.s, a.s, b.s);
return out;
}

static mul(a: Readonly<Vec2>, b: Readonly<Vec2>, out?: Vec2) {
!out && (out = new Vec2([]));
mul2new(out.buf, a.buf, b.buf, out.i, a.i, b.i, out.s, a.s, b.s);
return out;
}

static div(a: Readonly<Vec2>, b: Readonly<Vec2>, out?: Vec2) {
!out && (out = new Vec2([]));
div2new(out.buf, a.buf, b.buf, out.i, a.i, b.i, out.s, a.s, b.s);
return out;
}

static addN(a: Readonly<Vec2>, n: number, out?: Vec2) {
!out && (out = new Vec2([]));
addN2new(out.buf, a.buf, n, out.i, a.i, out.s, a.s);
return out;
}

static subN(a: Readonly<Vec2>, n: number, out?: Vec2) {
!out && (out = new Vec2([]));
subN2new(out.buf, a.buf, n, out.i, a.i, out.s, a.s);
return out;
}

static mulN(a: Readonly<Vec2>, n: number, out?: Vec2) {
!out && (out = new Vec2([]));
mulN2new(out.buf, a.buf, n, out.i, a.i, out.s, a.s);
return out;
}

static divN(a: Readonly<Vec2>, n: number, out?: Vec2) {
!out && (out = new Vec2([]));
divN2new(out.buf, a.buf, n, out.i, a.i, out.s, a.s);
return out;
}

static mix(a: Readonly<Vec2>, b: Readonly<Vec2>, t: Readonly<Vec2>, out?: Vec2) {
!out && (out = new Vec2([]));
mix2new(out.buf, a.buf, b.buf, t.buf, out.i, a.i, b.i, t.i, out.s, a.s, b.s, t.s);
return out;
}

static mixN(a: Readonly<Vec2>, b: Readonly<Vec2>, t: number, out?: Vec2) {
!out && (out = new Vec2([]));
mixN2new(out.buf, a.buf, b.buf, t, out.i, a.i, b.i, out.s, a.s, b.s);
return out;
}

static readonly ZERO = Object.freeze(new Vec2(<number[]>ZERO4));
static readonly ONE = Object.freeze(new Vec2(<number[]>ONE4));
static readonly MIN = Object.freeze(new Vec2(<number[]>MIN4));
Expand Down
130 changes: 130 additions & 0 deletions packages/vectors/src/vec3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,34 @@ export const div3 = (a: Vec, b: ReadonlyVec, ia = 0, ib = 0, sa = 1, sb = 1) =>
a
);

export const add3new = (out: Vec, a: ReadonlyVec, b: ReadonlyVec, io = 0, ia = 0, ib = 0, so = 1, sa = 1, sb = 1) => (
out[io] = a[ia] + b[ib],
out[io + so] = a[ia + sa] + b[ib + sb],
out[io + 2 * so] = a[ia + 2 * sa] + b[ib + 2 * sb],
out
);

export const sub3new = (out: Vec, a: ReadonlyVec, b: ReadonlyVec, io = 0, ia = 0, ib = 0, so = 1, sa = 1, sb = 1) => (
out[io] = a[ia] - b[ib],
out[io + so] = a[ia + sa] - b[ib + sb],
out[io + 2 * so] = a[ia + 2 * sa] - b[ib + 2 * sb],
out
);

export const mul3new = (out: Vec, a: ReadonlyVec, b: ReadonlyVec, io = 0, ia = 0, ib = 0, so = 1, sa = 1, sb = 1) => (
out[io] = a[ia] * b[ib],
out[io + so] = a[ia + sa] * b[ib + sb],
out[io + 2 * so] = a[ia + 2 * sa] * b[ib + 2 * sb],
out
);

export const div3new = (out: Vec, a: ReadonlyVec, b: ReadonlyVec, io = 0, ia = 0, ib = 0, so = 1, sa = 1, sb = 1) => (
out[io] = a[ia] / b[ib],
out[io + so] = a[ia + sa] / b[ib + sb],
out[io + 2 * so] = a[ia + 2 * sa] / b[ib + 2 * sb],
out
);

export const addN3 = (a: Vec, n: number, ia = 0, sa = 1) =>
(a[ia] += n, a[ia + sa] += n, a[ia + 2 * sa] += n, a);

Expand All @@ -138,6 +166,34 @@ export const mulN3 = (a: Vec, n: number, ia = 0, sa = 1) =>
export const divN3 = (a: Vec, n: number, ia = 0, sa = 1) =>
(a[ia] /= n, a[ia + sa] /= n, a[ia + 2 * sa] /= n, a);

export const addN3new = (out: Vec, a: ReadonlyVec, n: number, io = 0, ia = 0, so = 1, sa = 1) => (
out[io] = a[ia] + n,
out[io + so] = a[ia + sa] + n,
out[io + 2 * so] = a[ia + 2 * sa] + n,
out
);

export const subN3new = (out: Vec, a: ReadonlyVec, n: number, io = 0, ia = 0, so = 1, sa = 1) => (
out[io] = a[ia] - n,
out[io + so] = a[ia + sa] - n,
out[io + 2 * so] = a[ia + 2 * sa] - n,
out
);

export const mulN3new = (out: Vec, a: ReadonlyVec, n: number, io = 0, ia = 0, so = 1, sa = 1) => (
out[io] = a[ia] * n,
out[io + so] = a[ia + sa] * n,
out[io + 2 * so] = a[ia + 2 * sa] * n,
out
);

export const divN3new = (out: Vec, a: ReadonlyVec, n: number, io = 0, ia = 0, so = 1, sa = 1) => (
out[io] = a[ia] / n,
out[io + so] = a[ia + sa] / n,
out[io + 2 * so] = a[ia + 2 * sa] / n,
out
);

export const neg3 = (a: Vec, ia = 0, sa = 1) =>
mulN3(a, -1, ia, sa);

Expand Down Expand Up @@ -237,6 +293,20 @@ export const mixN3 = (a: Vec, b: ReadonlyVec, n: number, ia = 0, ib = 0, sa = 1,
a
);

export const mix3new = (out: Vec, a: ReadonlyVec, b: ReadonlyVec, t: ReadonlyVec, io = 0, ia = 0, ib = 0, it = 0, so = 1, sa = 1, sb = 1, st = 1) => (
out[io] = a[ia] + (b[ib] - a[ia]) * t[it],
out[io + so] = a[ia + sa] + (b[ib + sb] - a[ia + sa]) * t[it + st],
out[io + 2 * so] = a[ia + 2 * sa] + (b[ib + 2 * sb] - a[ia + 2 * sa]) * t[it + 2 * st],
out
);

export const mixN3new = (out: Vec, a: ReadonlyVec, b: ReadonlyVec, n: number, io = 0, ia = 0, ib = 0, so = 1, sa = 1, sb = 1) => (
out[io] = a[ia] + (b[ib] - a[ia]) * n,
out[io + so] = a[ia + sa] + (b[ib + sb] - a[ia + sa]) * n,
out[io + 2 * so] = a[ia + 2 * sa] + (b[ib + 2 * sb] - a[ia + 2 * sa]) * n,
out
);

export const mixBilinear3 = (
a: Vec, b: ReadonlyVec, c: ReadonlyVec, d: ReadonlyVec, u: number, v: number,
ia = 0, ib = 0, ic = 0, id = 0,
Expand Down Expand Up @@ -464,6 +534,66 @@ export class Vec3 implements
return new Vec3(orthoNormal3(a.buf, b.buf, c.buf, a.i, b.i, c.i, a.s, b.s, c.s));
}

static add(a: Readonly<Vec3>, b: Readonly<Vec3>, out?: Vec3) {
!out && (out = new Vec3([]));
add3new(out.buf, a.buf, b.buf, out.i, a.i, b.i, out.s, a.s, b.s);
return out;
}

static sub(a: Readonly<Vec3>, b: Readonly<Vec3>, out?: Vec3) {
!out && (out = new Vec3([]));
sub3new(out.buf, a.buf, b.buf, out.i, a.i, b.i, out.s, a.s, b.s);
return out;
}

static mul(a: Readonly<Vec3>, b: Readonly<Vec3>, out?: Vec3) {
!out && (out = new Vec3([]));
mul3new(out.buf, a.buf, b.buf, out.i, a.i, b.i, out.s, a.s, b.s);
return out;
}

static div(a: Readonly<Vec3>, b: Readonly<Vec3>, out?: Vec3) {
!out && (out = new Vec3([]));
div3new(out.buf, a.buf, b.buf, out.i, a.i, b.i, out.s, a.s, b.s);
return out;
}

static addN(a: Readonly<Vec3>, n: number, out?: Vec3) {
!out && (out = new Vec3([]));
addN3new(out.buf, a.buf, n, out.i, a.i, out.s, a.s);
return out;
}

static subN(a: Readonly<Vec3>, n: number, out?: Vec3) {
!out && (out = new Vec3([]));
subN3new(out.buf, a.buf, n, out.i, a.i, out.s, a.s);
return out;
}

static mulN(a: Readonly<Vec3>, n: number, out?: Vec3) {
!out && (out = new Vec3([]));
mulN3new(out.buf, a.buf, n, out.i, a.i, out.s, a.s);
return out;
}

static divN(a: Readonly<Vec3>, n: number, out?: Vec3) {
!out && (out = new Vec3([]));
divN3new(out.buf, a.buf, n, out.i, a.i, out.s, a.s);
return out;
}

static mix(a: Readonly<Vec3>, b: Readonly<Vec3>, t: Readonly<Vec3>, out?: Vec3) {
!out && (out = new Vec3([]));
mix3new(out.buf, a.buf, b.buf, t.buf, out.i, a.i, b.i, t.i, out.s, a.s, b.s, t.s);
return out;
}

static mixN(a: Readonly<Vec3>, b: Readonly<Vec3>, t: number, out?: Vec3) {
!out && (out = new Vec3([]));
mixN3new(out.buf, a.buf, b.buf, t, out.i, a.i, b.i, out.s, a.s, b.s);
return out;
}

static readonly ZERO = Object.freeze(new Vec3(<number[]>ZERO4));
static readonly ONE = Object.freeze(new Vec3(<number[]>ONE4));
static readonly MIN = Object.freeze(new Vec3(<number[]>MIN4));
Expand Down
Loading

0 comments on commit a3c0407

Please sign in to comment.