Skip to content

Commit

Permalink
refactor(geom): update everything to use new vectors package
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Dec 1, 2018
1 parent 68806d9 commit d4172ee
Show file tree
Hide file tree
Showing 38 changed files with 363 additions and 471 deletions.
9 changes: 5 additions & 4 deletions packages/geom2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
"@thi.ng/api": "^4.2.3",
"@thi.ng/checks": "^1.5.13",
"@thi.ng/defmulti": "^0.5.0",
"@thi.ng/hiccup": "^2.4.3",
"@thi.ng/hiccup-svg": "^2.0.4",
"@thi.ng/hiccup": "^2.6.0",
"@thi.ng/hiccup-svg": "^2.0.6",
"@thi.ng/malloc": "^0.2.0",
"@thi.ng/math": "^0.2.0",
"@thi.ng/vectors2": "^0.0.1"
"@thi.ng/math": "^0.2.1",
"@thi.ng/matrices": "^0.0.1",
"@thi.ng/vectors3": "^0.0.1"
},
"keywords": [
"ES6",
Expand Down
47 changes: 22 additions & 25 deletions packages/geom2/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@ import {
} from "@thi.ng/defmulti";
import { equiv } from "@thi.ng/equiv";
import { cossin } from "@thi.ng/math/angle";
import {
add,
copy,
IMatrix,
max,
min,
mixNewN,
mul,
ReadonlyVec,
rotateZ,
subNew,
Vec,
neg
} from "@thi.ng/vectors2/api";
import { perpendicularLeft2, set2 } from "@thi.ng/vectors2/vec2";
import { ReadonlyMat } from "@thi.ng/matrices/api";
import { add } from "@thi.ng/vectors3/add";
import { ReadonlyVec, Vec } from "@thi.ng/vectors3/api";
import { copy } from "@thi.ng/vectors3/copy";
import { mixN } from "@thi.ng/vectors3/mixn";
import { mul } from "@thi.ng/vectors3/mul";
import { min } from "@thi.ng/vectors3/min";
import { max } from "@thi.ng/vectors3/max";
import { neg } from "@thi.ng/vectors3/neg";
import { sub } from "@thi.ng/vectors3/sub";
import { perpendicularLeft2 } from "@thi.ng/vectors3/perpendicular";
import { rotateZ } from "@thi.ng/vectors3/rotate";
import { subdivKernel3 } from "./internal/subdiv-curve";
import { warpPoints } from "./internal/warp";

Expand Down Expand Up @@ -231,8 +228,8 @@ export const bounds = defmulti<IShape, AABBLike>(dispatch);

export const center: MultiFn1O<IShape, ReadonlyVec, IShape> = defmulti(dispatch);
center.add(DEFAULT, (x, origin?) => {
const delta = neg(centroid(x));
return translate(x, origin ? add(delta, origin) : delta);
const delta = neg(null, centroid(x));
return translate(x, origin ? add(null, delta, origin) : delta);
});

export const centroid: MultiFn1O<IShape, Vec, Vec> = defmulti(dispatch);
Expand Down Expand Up @@ -271,7 +268,7 @@ export const isEmpty = defmulti<IShape, boolean>(dispatch);
export const mapPoint: MultiFn2O<IShape, ReadonlyVec, Vec, Vec> = defmulti(dispatch);

export const normalAt: MultiFn2O<IShape, number, number, Vec> = defmulti(dispatch);
normalAt.add(DEFAULT, (shape, t, n = 1) => perpendicularLeft2(tangentAt(shape, t, n)));
normalAt.add(DEFAULT, (shape, t, n = 1) => perpendicularLeft2(null, tangentAt(shape, t, n)));

export const offset: MultiFn2O<IShape, number, number, IShape> = defmulti(dispatch);

Expand Down Expand Up @@ -300,7 +297,7 @@ export const tangentAt: MultiFn2O<IShape, number, number, Vec> = defmulti(dispat

export const tessellate = defmulti<IShape, Iterable<Tessellator>, Vec[][]>(dispatch);

export const transform = defmulti<IShape, IMatrix<any>, IShape>(dispatch);
export const transform = defmulti<IShape, ReadonlyMat, IShape>(dispatch);

export const translate = defmulti<IShape, ReadonlyVec, IShape>(dispatch);

Expand Down Expand Up @@ -431,7 +428,7 @@ export class Arc2 implements
}

pointAtTheta(theta: number, pos: Vec = []) {
return add(rotateZ(mul(set2(pos, cossin(theta)), this.r), this.axis), this.pos);
return add(null, rotateZ(null, mul(pos, cossin(theta), this.r), this.axis), this.pos);
}

toHiccup() {
Expand Down Expand Up @@ -489,7 +486,7 @@ export class Cubic2 extends PointContainer implements
IHiccupPathSegment {

static fromLine(a: Vec, b: Vec, attribs?: Attribs) {
return new Cubic2([a, mixNewN(a, b, 1 / 3), mixNewN(b, a, 1 / 3), b], attribs);
return new Cubic2([a, mixN([], a, b, 1 / 3), mixN([], b, a, 1 / 3), b], attribs);
}

constructor(points: Vec[], attribs?: Attribs) {
Expand Down Expand Up @@ -721,7 +718,7 @@ export class Quadratic2 extends PointContainer implements
IHiccupPathSegment {

static fromLine(a: Vec, b: Vec, attribs?: Attribs) {
return new Quadratic2([a, mixNewN(a, b, 0.5), b], attribs);
return new Quadratic2([a, mixN([], a, b, 0.5), b], attribs);
}

constructor(points: Vec[], attribs?: Attribs) {
Expand Down Expand Up @@ -752,9 +749,9 @@ export class Rect2 implements
IShape {

static fromMinMax(mi: Vec, mx: Vec, attribs?: Attribs) {
const _mi = min(copy(mi), mx);
const _mx = max(copy(mx), mi);
return new Rect2(_mi, subNew(_mx, _mi), attribs);
const _mi = min([], mi, mx);
const _mx = max([], mx, mi);
return new Rect2(_mi, sub(null, _mx, _mi), attribs);
}

pos: Vec;
Expand Down
35 changes: 19 additions & 16 deletions packages/geom2/src/arc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ import { push } from "@thi.ng/transducers/rfn/push";
import { transduce } from "@thi.ng/transducers/transduce";
import { filter } from "@thi.ng/transducers/xform/filter";
import { map } from "@thi.ng/transducers/xform/map";
import { abs2 } from "@thi.ng/vectors3/abs";
import { add2 } from "@thi.ng/vectors3/add";
import { angleBetween } from "@thi.ng/vectors3/angle-between";
import {
abs,
add,
angleBetween,
magSq,
mulN,
MAX2,
MIN2,
ReadonlyVec,
subNew,
Vec
} from "@thi.ng/vectors2/api";
import { Vec2 } from "@thi.ng/vectors2/vec2";
Vec,
X2
} from "@thi.ng/vectors3/api";
import { magSq2 } from "@thi.ng/vectors3/magsq";
import { mulN2 } from "@thi.ng/vectors3/muln";
import { sub2 } from "@thi.ng/vectors3/sub";
import "./bezier";
import { bounds as _bounds } from "./internal/bounds";
import { Sampler } from "./internal/sampler";
Expand Down Expand Up @@ -57,17 +59,17 @@ export function arcFrom2Points(
large = false,
clockwise = true) {

const r = abs([...radii]);
const r = abs2([], radii);
const co = Math.cos(axisTheta);
const si = Math.sin(axisTheta);
const m = mulN(subNew(a, b), 0.5);
const m = mulN2(null, sub2([], a, b), 0.5);
const px = co * m[0] + si * m[1];
const py = -si * m[0] + co * m[1];
const px2 = px * px;
const py2 = py * py;

const l = px2 / (r[0] * r[0]) + py2 / (r[1] * r[1]);
l > 1 && mulN(r, Math.sqrt(l));
l > 1 && mulN2(null, r, Math.sqrt(l));

const rx2 = r[0] * r[0];
const ry2 = r[1] * r[1];
Expand All @@ -81,7 +83,7 @@ export function arcFrom2Points(
const d1 = [(px - tc[0]) / r[0], (py - tc[1]) / r[1]];
const d2 = [(-px - tc[0]) / r[0], (-py - tc[1]) / r[1]];

const theta = angleBetween(Vec2.X_AXIS, d1, true);
const theta = angleBetween(X2, d1, true);
let delta = angleBetween(d1, d2, true);

if (clockwise && delta < 0) {
Expand All @@ -106,14 +108,15 @@ implementations(
const [sphi, cphi] = sincos(arc.axis);
const dx = cphi * (p[0] - q[0]) / 2 + sphi * (p[1] - q[1]) / 2;
const dy = -sphi * (p[0] - q[0]) / 2 + cphi * (p[1] - q[1]) / 2;
if ((dx === 0 && dy === 0) || magSq(arc.r) < EPS) {
if ((dx === 0 && dy === 0) || magSq2(arc.r) < EPS) {
return [Cubic2.fromLine(p, q, { ...arc.attribs })];
}

const mapP = (x: number, y: number) => {
x *= rx;
y *= ry;
return add(
return add2(
null,
[
cphi * x - sphi * y,
sphi * x + cphi * y
Expand Down Expand Up @@ -163,7 +166,7 @@ implementations(
)
]
);
return Rect2.fromMinMax(..._bounds(pts, [...Vec2.MAX], [...Vec2.MIN]));
return Rect2.fromMinMax(..._bounds(pts, [...MAX2], [...MIN2]));
},

centroid,
Expand Down
Loading

0 comments on commit d4172ee

Please sign in to comment.