Skip to content

Commit

Permalink
feat(geom): add Points3 and supporting ops
Browse files Browse the repository at this point in the history
- points3() ctor
- area()
- bounds()
- centroid()
- flip()
- pointInside()
- transform()
- translate()
- vertices()
  • Loading branch information
postspectacular committed Feb 23, 2020
1 parent 49c88d9 commit 7e1adb7
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 18 deletions.
14 changes: 14 additions & 0 deletions packages/geom/src/api/points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,17 @@ export class Points extends APC implements IHiccupShape {
return ["points", this.attribs, this.points];
}
}

export class Points3 extends APC implements IHiccupShape {
get type() {
return Type.POINTS3;
}

copy(): Points3 {
return <Points3>copyShape(Points3, this);
}

toHiccup() {
return ["points3", this.attribs, this.points];
}
}
5 changes: 4 additions & 1 deletion packages/geom/src/ctors/points.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Attribs } from "@thi.ng/geom-api";
import { Vec } from "@thi.ng/vectors";
import { Points } from "../api/points";
import { Points, Points3 } from "../api/points";

export const points = (pts?: Vec[], attribs?: Attribs) =>
new Points(pts, attribs);

export const points3 = (pts?: Vec[], attribs?: Attribs) =>
new Points3(pts, attribs);
14 changes: 13 additions & 1 deletion packages/geom/src/internal/transform-points.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PCLike, PCLikeConstructor } from "@thi.ng/geom-api";
import { mulV, ReadonlyMat } from "@thi.ng/matrices";
import { mulV, mulV344, ReadonlyMat } from "@thi.ng/matrices";
import { ReadonlyVec } from "@thi.ng/vectors";
import { copyAttribs } from "./copy-attribs";

Expand All @@ -10,7 +10,19 @@ export const transformPoints = (pts: ReadonlyVec[], mat: ReadonlyMat) => (
export const transformedPoints = (pts: ReadonlyVec[], mat: ReadonlyMat) =>
pts.map((p) => mulV([], mat, p));

export const transformPoints3 = (pts: ReadonlyVec[], mat: ReadonlyMat) => (
pts.forEach((p) => mulV344(null, mat, p)!), pts
);

export const transformedPoints3 = (pts: ReadonlyVec[], mat: ReadonlyMat) =>
pts.map((p) => mulV344([], mat, p)!);

export const transformedShape = (ctor: PCLikeConstructor) => (
$: PCLike,
mat: ReadonlyMat
) => new ctor(transformedPoints($.points, mat), copyAttribs($));

export const transformedShape3 = (ctor: PCLikeConstructor) => (
$: PCLike,
mat: ReadonlyMat
) => new ctor(transformedPoints3($.points, mat), copyAttribs($));
4 changes: 2 additions & 2 deletions packages/geom/src/internal/translate-points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export const translatedPoints = (pts: ReadonlyVec[], delta: ReadonlyVec) =>

export const translatedShape = (ctor: PCLikeConstructor) => (
$: PCLike,
mat: ReadonlyVec
) => new ctor(translatedPoints($.points, mat), copyAttribs($));
delta: ReadonlyVec
) => new ctor(translatedPoints($.points, delta), copyAttribs($));
16 changes: 7 additions & 9 deletions packages/geom/src/ops/area.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { IObjectOf } from "@thi.ng/api";
import { defmulti, Implementation1O, MultiFn1O } from "@thi.ng/defmulti";
import {
DEFAULT,
defmulti,
Implementation1O,
MultiFn1O
} from "@thi.ng/defmulti";
import { IShape, Type } from "@thi.ng/geom-api";
import { polyArea2 } from "@thi.ng/geom-poly-utils";
import { PI } from "@thi.ng/math";
Expand Down Expand Up @@ -49,6 +54,7 @@ import { dispatch } from "../internal/dispatch";
* @param signed - true, if signed area
*/
export const area: MultiFn1O<IShape, boolean, number> = defmulti(dispatch);
area.add(DEFAULT, () => 0);

area.addAll(<IObjectOf<Implementation1O<unknown, boolean, number>>>{
[Type.AABB]: ({ size: [w, h, d] }: AABB) => 2 * (w * h + w * d + h * d),
Expand All @@ -64,8 +70,6 @@ area.addAll(<IObjectOf<Implementation1O<unknown, boolean, number>>>{
[Type.GROUP]: ({ children }: Group) =>
children.reduce((sum, $) => sum + area($, false), 0),

[Type.POINTS]: () => 0,

[Type.PLANE]: () => Infinity,

[Type.POLYGON]: ($: Polygon, signed?) => {
Expand All @@ -83,10 +87,4 @@ area.addAll(<IObjectOf<Implementation1O<unknown, boolean, number>>>{
}
});

area.isa(Type.ARC, Type.POINTS);
area.isa(Type.CUBIC, Type.POINTS);
area.isa(Type.LINE, Type.POINTS);
area.isa(Type.POLYLINE, Type.POINTS);
area.isa(Type.QUAD, Type.POLYGON);
area.isa(Type.QUADRATIC, Type.POINTS);
area.isa(Type.RAY, Type.POINTS);
12 changes: 11 additions & 1 deletion packages/geom/src/ops/bounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ import {
import {
max,
MAX2,
MAX3,
min,
MIN2,
MIN3,
mul2,
mulN2,
set2,
set3,
sub2,
subN2
} from "@thi.ng/vectors";
Expand All @@ -36,6 +39,7 @@ import { Line } from "../api/line";
import { Path } from "../api/path";
import { Quadratic } from "../api/quadratic";
import { Rect } from "../api/rect";
import { aabbFromMinMax } from "../ctors/aabb";
import { rectFromMinMax } from "../ctors/rect";
import { collBounds } from "../internal/coll-bounds";
import { dispatch } from "../internal/dispatch";
Expand Down Expand Up @@ -69,7 +73,10 @@ bounds.addAll(<IObjectOf<Implementation1<unknown, AABBLike>>>{
const b = collBounds(
[
...iterator1(
comp(map((s: PathSegment) => s.geo!), filter((s) => !!s)),
comp(
map((s: PathSegment) => s.geo!),
filter((s) => !!s)
),
path.segments
)
],
Expand All @@ -81,6 +88,9 @@ bounds.addAll(<IObjectOf<Implementation1<unknown, AABBLike>>>{
[Type.POINTS]: ($: PCLike) =>
rectFromMinMax(..._bounds($.points, set2([], MAX2), set2([], MIN2))),

[Type.POINTS3]: ($: PCLike) =>
aabbFromMinMax(..._bounds($.points, set3([], MAX3), set3([], MIN3))),

[Type.QUADRATIC]: ({ points }: Quadratic) =>
rectFromMinMax(...quadraticBounds(points[0], points[1], points[2])),

Expand Down
1 change: 1 addition & 0 deletions packages/geom/src/ops/centroid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ centroid.isa(Type.ARC, Type.CIRCLE);
centroid.isa(Type.AABB, Type.RECT);
centroid.isa(Type.ELLIPSE, Type.CIRCLE);
centroid.isa(Type.LINE3, Type.LINE);
centroid.isa(Type.POINTS3, Type.POINTS);
centroid.isa(Type.POLYLINE, Type.POINTS);
centroid.isa(Type.QUAD, Type.POLYGON);
centroid.isa(Type.SPHERE, Type.CIRCLE);
Expand Down
1 change: 1 addition & 0 deletions packages/geom/src/ops/flip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ flip.addAll(<IObjectOf<Implementation1<unknown, IShape>>>{

flip.isa(Type.CUBIC, Type.POINTS);
flip.isa(Type.LINE, Type.POINTS);
flip.isa(Type.POINTS3, Type.POINTS);
flip.isa(Type.POLYGON, Type.POINTS);
flip.isa(Type.POLYLINE, Type.POINTS);
flip.isa(Type.QUAD, Type.POINTS);
Expand Down
3 changes: 2 additions & 1 deletion packages/geom/src/ops/point-inside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ pointInside.addAll(<IObjectOf<Implementation2<unknown, ReadonlyVec, boolean>>>{
pointInTriangle2(p, ...(<[Vec, Vec, Vec]>tri.points))
});

pointInside.isa(Type.SPHERE, Type.CIRCLE);
pointInside.isa(Type.POINTS3, Type.POINTS);
pointInside.isa(Type.QUAD, Type.POLYGON);
pointInside.isa(Type.SPHERE, Type.CIRCLE);
6 changes: 4 additions & 2 deletions packages/geom/src/ops/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Cubic } from "../api/cubic";
import { Group } from "../api/group";
import { Line } from "../api/line";
import { Path } from "../api/path";
import { Points } from "../api/points";
import { Points, Points3 } from "../api/points";
import { Polygon } from "../api/polygon";
import { Polyline } from "../api/polyline";
import { Quad } from "../api/quad";
Expand All @@ -22,7 +22,7 @@ import { Rect } from "../api/rect";
import { Triangle } from "../api/triangle";
import { copyAttribs } from "../internal/copy-attribs";
import { dispatch } from "../internal/dispatch";
import { transformedShape as tx } from "../internal/transform-points";
import { transformedShape as tx, transformedShape3 as tx3 } from "../internal/transform-points";
import { asPath } from "./as-path";
import { asPolygon } from "./as-polygon";

Expand Down Expand Up @@ -74,6 +74,8 @@ transform.addAll(<IObjectOf<Implementation2<unknown, ReadonlyMat, IShape>>>{

[Type.POINTS]: tx(Points),

[Type.POINTS3]: tx3(Points3),

[Type.POLYGON]: tx(Polygon),

[Type.POLYLINE]: tx(Polyline),
Expand Down
4 changes: 3 additions & 1 deletion packages/geom/src/ops/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Ellipse } from "../api/ellipse";
import { Group } from "../api/group";
import { Line } from "../api/line";
import { Path } from "../api/path";
import { Points } from "../api/points";
import { Points, Points3 } from "../api/points";
import { Polygon } from "../api/polygon";
import { Polyline } from "../api/polyline";
import { Quad } from "../api/quad";
Expand Down Expand Up @@ -71,6 +71,8 @@ translate.addAll(<IObjectOf<Implementation2<unknown, ReadonlyVec, IShape>>>{

[Type.POINTS]: tx(Points),

[Type.POINTS3]: tx(Points3),

[Type.POLYGON]: tx(Polygon),

[Type.POLYLINE]: tx(Polyline),
Expand Down
1 change: 1 addition & 0 deletions packages/geom/src/ops/vertices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ vertices.addAll(<
});

vertices.isa(Type.LINE, Type.POLYLINE);
vertices.isa(Type.POINTS3, Type.POINTS);
vertices.isa(Type.QUAD, Type.POLYGON);
vertices.isa(Type.TRIANGLE, Type.POLYGON);

Expand Down

0 comments on commit 7e1adb7

Please sign in to comment.