Skip to content

Commit

Permalink
feat(geom): add clipConvex, scatter, warpPoints
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 17, 2019
1 parent 61cfb0f commit d09cc79
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/geom3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"scripts": {
"build": "yarn clean && yarn build:es6 && yarn build:bundle",
"build:es6": "tsc --declaration",
"build:bundle": "../../scripts/bundle-module geom3 api checks defmulti equiv errors hiccup hiccup-svg math matrices transducers vectors3",
"build:bundle": "../../scripts/bundle-module geom3 api checks defmulti equiv errors hiccup hiccup-svg math matrices random transducers vectors3",
"test": "rimraf build && tsc -p test/tsconfig.json && nyc mocha build/test/*.js",
"clean": "rimraf *.js *.d.ts .nyc_output build coverage doc lib",
"cover": "yarn test && nyc report --reporter=lcov",
Expand All @@ -41,6 +41,7 @@
"@thi.ng/hiccup-svg": "^2.0.10",
"@thi.ng/math": "^0.2.2",
"@thi.ng/matrices": "^0.0.1",
"@thi.ng/random": "^0.1.1",
"@thi.ng/transducers": "^2.3.2",
"@thi.ng/vectors3": "^0.0.1"
},
Expand Down
30 changes: 24 additions & 6 deletions packages/geom3/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { illegalState } from "@thi.ng/errors";
import { cossin } from "@thi.ng/math";
import {
add2,
add3,
maddN2,
mul2,
ReadonlyVec,
Expand Down Expand Up @@ -77,6 +78,8 @@ export interface IShape extends
export interface AABBLike extends IShape {
pos: Vec;
size: Vec;

max(): Vec;
}

export interface IHiccupShape extends IShape, IToHiccup { }
Expand Down Expand Up @@ -184,7 +187,7 @@ export abstract class APC implements
}

export class AABB implements
IShape {
AABBLike {

pos: Vec;
size: Vec;
Expand All @@ -203,8 +206,13 @@ export class AABB implements
copy() {
return new AABB(set([], this.pos), set([], this.size), { ...this.attribs });
}

max() {
return add3([], this.pos, this.size);
}
}


export class Arc implements
IHiccupShape,
IHiccupPathSegment {
Expand Down Expand Up @@ -488,7 +496,8 @@ export class Path implements
}
}

export class Points extends APC {
export class Points extends APC implements
IHiccupShape {

get type() {
return Type.POINTS;
Expand All @@ -503,7 +512,8 @@ export class Points extends APC {
}
}

export class Polygon extends APC {
export class Polygon extends APC implements
IHiccupShape {

get type() {
return Type.POLYGON;
Expand Down Expand Up @@ -543,7 +553,8 @@ export class Polyline extends APC implements
}
}

export class Quad extends APC {
export class Quad extends APC implements
IHiccupShape {

get type() {
return Type.QUAD;
Expand All @@ -559,6 +570,7 @@ export class Quad extends APC {
}

export class Quadratic extends APC implements
IHiccupShape,
IHiccupPathSegment {

get type() {
Expand Down Expand Up @@ -611,6 +623,7 @@ export class Ray implements
}

export class Rect implements
AABBLike,
IHiccupShape {

pos: Vec;
Expand All @@ -631,13 +644,17 @@ export class Rect implements
return new Rect(set([], this.pos), set([], this.size), { ...this.attribs });
}

max() {
return add2([], this.pos, this.size);
}

toHiccup() {
return ["rect", this.attribs, this.pos, this.size];
}
}

export class Sphere implements
IShape {
IHiccupShape {

pos: Vec;
r: number;
Expand All @@ -662,7 +679,8 @@ export class Sphere implements
}
}

export class Triangle extends APC {
export class Triangle extends APC implements
IHiccupShape {

get type() {
return Type.TRIANGLE;
Expand Down
5 changes: 5 additions & 0 deletions packages/geom3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from "./ctors/ellipse";
export * from "./ctors/group";
export * from "./ctors/line";
export * from "./ctors/path";
export * from "./ctors/points";
export * from "./ctors/polygon";
export * from "./ctors/polyline";
export * from "./ctors/quad";
Expand All @@ -23,13 +24,15 @@ export * from "./ops/bounds";
export * from "./ops/center";
export * from "./ops/centroid";
export * from "./ops/classify-point";
export * from "./ops/clip-convex";
export * from "./ops/closest-point";
export * from "./ops/convex-hull";
export * from "./ops/fit-into-bounds";
export * from "./ops/map-point";
export * from "./ops/point-at";
export * from "./ops/point-inside";
export * from "./ops/resample";
export * from "./ops/scatter";
export * from "./ops/simplify";
export * from "./ops/split-at";
export * from "./ops/subdiv-curve";
Expand All @@ -39,6 +42,8 @@ export * from "./ops/translate";
export * from "./ops/union";
export * from "./ops/unmap-point";
export * from "./ops/vertices";
export * from "./ops/warp-points";
export * from "./ops/with-attribs";

export * from "./internal/bounds";
export * from "./internal/centroid";
Expand Down
1 change: 1 addition & 0 deletions packages/geom3/src/ops/as-polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ asPolygon.addAll({
asPolygon.isa(Type.CIRCLE, Type.POINTS);
asPolygon.isa(Type.ELLIPSE, Type.POINTS);
asPolygon.isa(Type.LINE, Type.POINTS);
asPolygon.isa(Type.PATH, Type.POINTS);
asPolygon.isa(Type.POLYGON, Type.POINTS);
asPolygon.isa(Type.POLYLINE, Type.POINTS);
asPolygon.isa(Type.QUAD, Type.POINTS);
Expand Down
31 changes: 31 additions & 0 deletions packages/geom3/src/ops/clip-convex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { defmulti } from "@thi.ng/defmulti";
import { IShape, Polygon, Type } from "../api";
import { dispatch } from "../internal/dispatch";
import { sutherlandHodgeman } from "../internal/sutherland-hodgeman";
import { vertices } from "./vertices";
import { centroid } from "./centroid";

export const clipConvex = defmulti<IShape, IShape, Polygon>(dispatch);

clipConvex.addAll({

[Type.POLYGON]:
($: Polygon, boundary: IShape) =>
new Polygon(
sutherlandHodgeman($.points, vertices(boundary), centroid(boundary)),
{ ...$.attribs }
),

[Type.RECT]:
($, boundary: IShape) =>
new Polygon(
sutherlandHodgeman(vertices($), vertices(boundary), centroid(boundary)),
{ ...$.attribs }
)
});

clipConvex.isa(Type.CIRCLE, Type.RECT);
clipConvex.isa(Type.ELLIPSE, Type.RECT);
clipConvex.isa(Type.PATH, Type.RECT);
clipConvex.isa(Type.QUAD, Type.POLYGON);
clipConvex.isa(Type.TRIANGLE, Type.POLYGON);
22 changes: 22 additions & 0 deletions packages/geom3/src/ops/scatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { IShape } from "../api";
import { Vec, randMinMax } from "@thi.ng/vectors3";
import { SYSTEM } from "@thi.ng/random";
import { bounds } from "./bounds";
import { pointInside } from "./point-inside";

export const scatter =
(shape: IShape, num: number, rnd = SYSTEM, out: Vec[] = []) => {
const b = bounds(shape);
const mi = b.pos;
const mx = b.max();
for (; --num >= 0;) {
while (true) {
const p = randMinMax([], mi, mx, rnd);
if (pointInside(shape, p)) {
out.push(p);
break;
}
}
}
return out;
};
13 changes: 13 additions & 0 deletions packages/geom3/src/ops/warp-points.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ReadonlyVec, Vec } from "@thi.ng/vectors3";
import { IShape } from "../api";
import { mapPoint } from "./map-point";
import { unmapPoint } from "./unmap-point";

export const warpPoints =
(pts: ReadonlyVec[], dest: IShape, src: IShape) => {
const res: Vec[] = [];
for (let n = pts.length, i = 0; i < n; i++) {
res.push(unmapPoint(dest, mapPoint(src, pts[i])));
}
return res;
};
7 changes: 7 additions & 0 deletions packages/geom3/src/ops/with-attribs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Attribs, IShape } from "../api";

export const withAttribs =
(shape: IShape, attribs: Attribs, replace = true) => {
shape.attribs = replace ? attribs : { ...shape.attribs, ...attribs };
return shape;
};

0 comments on commit d09cc79

Please sign in to comment.