Skip to content

Commit

Permalink
feat(vectors): replace math.ts w/ imports from @thi.ng/maths package
Browse files Browse the repository at this point in the history
BREAKING CHANGES: re-use @thi.ng/maths functionality instead of internal
maths functions.
  • Loading branch information
postspectacular committed Oct 17, 2018
1 parent f257330 commit 0967929
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 295 deletions.
3 changes: 2 additions & 1 deletion packages/vectors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@thi.ng/api": "^4.2.1",
"@thi.ng/checks": "^1.5.11",
"@thi.ng/errors": "^0.1.9",
"@thi.ng/math": "^0.0.1",
"@thi.ng/transducers": "^2.1.6"
},
"keywords": [
Expand All @@ -46,4 +47,4 @@
"publishConfig": {
"access": "public"
}
}
}
5 changes: 3 additions & 2 deletions packages/vectors/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { EPS } from "@thi.ng/math/api";
import { eqDelta as _eqDelta } from "@thi.ng/math/eqdelta";
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];
Expand Down Expand Up @@ -178,7 +179,7 @@ export const equiv = (a: ReadonlyVec, b: ReadonlyVec, n: number, ia = 0, ib = 0,
*/
export const eqDelta = (a: ReadonlyVec, b: ReadonlyVec, n: number, eps = EPS, ia = 0, ib = 0, sa = 1, sb = 1) => {
for (; n > 0; n-- , ia += sa, ib += sb) {
if (!eqDelta1(a[ia], b[ib], eps)) {
if (!_eqDelta(a[ia], b[ib], eps)) {
return false;
}
}
Expand Down
23 changes: 10 additions & 13 deletions packages/vectors/src/gvec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import {
} from "@thi.ng/api/api";
import { isArrayLike } from "@thi.ng/checks/is-arraylike";
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
import { sign as _sign } from "@thi.ng/math/abs";
import { EPS } from "@thi.ng/math/api";
import { clamp as _clamp } from "@thi.ng/math/interval";
import { fract as _fract } from "@thi.ng/math/prec";
import { smoothStep as _smoothStep, step as _step } from "@thi.ng/math/step";
import {
IDotProduct,
IMagnitude,
Expand All @@ -18,14 +23,6 @@ import {
Vec
} from "./api";
import { $iter, eqDelta, equiv } from "./common";
import {
clamp1,
EPS,
fract1,
sign1,
smoothStep1,
step1
} from "./math";


export const opg0 = (fn: () => number, a: Vec, num = a.length, i = 0, s = 1) => {
Expand Down Expand Up @@ -174,7 +171,7 @@ export const abs = (a: Vec, num = a.length, i = 0, s = 1) =>
opg1(Math.abs, a, num, i, s);

export const sign = (a: Vec, num = a.length, eps = EPS, i = 0, s = 1) =>
opg1((x) => sign1(x, eps), a, num, i, s);
opg1((x) => _sign(x, eps), a, num, i, s);

export const floor = (a: Vec, num = a.length, i = 0, s = 1) =>
opg1(Math.floor, a, num, i, s);
Expand All @@ -183,7 +180,7 @@ export const ceil = (a: Vec, num = a.length, i = 0, s = 1) =>
opg1(Math.ceil, a, num, i, s);

export const fract = (a: Vec, num = a.length, i = 0, s = 1) =>
opg1(fract1, a, num, i, s);
opg1(_fract, a, num, i, s);

export const sin = (a: Vec, num = a.length, i = 0, s = 1) =>
opg1(Math.sin, a, num, i, s);
Expand All @@ -207,13 +204,13 @@ export const max = (a: Vec, b: ReadonlyVec, num = a.length, ia = 0, ib = 0, sa =
opg2(Math.max, a, b, num, ia, ib, sa, sb);

export const clamp = (a: Vec, b: ReadonlyVec, c: ReadonlyVec, num = a.length, ia = 0, ib = 0, ic = 0, sa = 1, sb = 1, sc = 1) =>
opg3(clamp1, a, b, c, num, ia, ib, ic, sa, sb, sc);
opg3(_clamp, a, b, c, num, ia, ib, ic, sa, sb, sc);

export const step = (a: Vec, b: ReadonlyVec, num = a.length, ia = 0, ib = 0, sa = 1, sb = 1) =>
opg2((x, e) => step1(e, x), a, b, num, ia, ib, sa, sb);
opg2((x, e) => _step(e, x), a, b, num, ia, ib, sa, sb);

export const smoothStep = (a: Vec, b: ReadonlyVec, c: ReadonlyVec, num = a.length, ia = 0, ib = 0, ic = 0, sa = 1, sb = 1, sc = 1) =>
opg3((x, e1, e2) => smoothStep1(e1, e2, x), a, b, c, num, ia, ib, ic, sa, sb, sc);
opg3((x, e1, e2) => _smoothStep(e1, e2, x), a, b, c, num, ia, ib, ic, sa, sb, sc);

export const gvec = (...coords: number[]) =>
new GVec(coords, coords.length);
Expand Down
1 change: 0 additions & 1 deletion packages/vectors/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export * from "./common";
export * from "./mat23";
export * from "./mat33";
export * from "./mat44";
export * from "./math";
export * from "./gvec";
export * from "./vec2";
export * from "./vec3";
Expand Down
2 changes: 1 addition & 1 deletion packages/vectors/src/mat23.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ICopy, IEqualsDelta } from "@thi.ng/api/api";
import { isArrayLike } from "@thi.ng/checks/is-arraylike";
import { EPS } from "@thi.ng/math/api";
import {
Mat,
ReadonlyMat,
Expand All @@ -8,7 +9,6 @@ import {
} from "./api";
import { declareIndices } from "./codegen";
import { $iter, eqDelta } from "./common";
import { EPS } from "./math";
import {
cross2,
dot2,
Expand Down
2 changes: 1 addition & 1 deletion packages/vectors/src/mat33.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ICopy, IEqualsDelta } from "@thi.ng/api/api";
import { isArrayLike } from "@thi.ng/checks/is-arraylike";
import { EPS } from "@thi.ng/math/api";
import {
Mat,
ReadonlyMat,
Expand All @@ -8,7 +9,6 @@ import {
} from "./api";
import { declareIndices } from "./codegen";
import { $iter, eqDelta } from "./common";
import { EPS } from "./math";
import {
dot3,
set3,
Expand Down
4 changes: 2 additions & 2 deletions packages/vectors/src/mat44.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import { declareIndices } from "./codegen";
import { $iter, eqDelta } from "./common";
import { Mat33 } from "./mat33";
import { EPS, rad } from "./math";
import {
cross3,
dot3,
Expand All @@ -21,6 +20,7 @@ import {
Vec3
} from "./vec3";
import { dot4, setS4, Vec4 } from "./vec4";
import { EPS, DEG2RAD } from "@thi.ng/math/api";

export const get44 = (a: Mat, i = 0) =>
a.slice(i, i + 16);
Expand Down Expand Up @@ -159,7 +159,7 @@ export const frustum = (m: Mat, left: number, right: number, bottom: number, top
};

export const frustumBounds = (fovy: number, aspect: number, near: number, far: number) => {
const top = near * Math.tan(rad(fovy) / 2);
const top = near * Math.tan(fovy * DEG2RAD / 2);
const right = top * aspect;
return {
left: -right,
Expand Down
199 changes: 0 additions & 199 deletions packages/vectors/src/math.ts

This file was deleted.

Loading

0 comments on commit 0967929

Please sign in to comment.