Skip to content

Commit

Permalink
feat(geom): update/refactor various shape ops
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Mar 11, 2022
1 parent ea81cb5 commit 0e3b99a
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 15 deletions.
16 changes: 5 additions & 11 deletions packages/geom/src/bounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ import type { MultiFn1 } from "@thi.ng/defmulti";
import { defmulti } from "@thi.ng/defmulti/defmulti";
import type { AABBLike, IShape, PathSegment, PCLike } from "@thi.ng/geom-api";
import { bounds as arcBounds } from "@thi.ng/geom-arc/bounds";
import { bounds as _bounds } from "@thi.ng/geom-poly-utils/bounds";
import { bounds2, bounds3 } from "@thi.ng/geom-poly-utils/bounds";
import { cubicBounds } from "@thi.ng/geom-splines/cubic-bounds";
import { quadraticBounds } from "@thi.ng/geom-splines/quadratic-bounds";
import { comp } from "@thi.ng/transducers/comp";
import { filter } from "@thi.ng/transducers/filter";
import { iterator1 } from "@thi.ng/transducers/iterator";
import { map } from "@thi.ng/transducers/map";
import { MAX2, MAX3, MIN2, MIN3 } from "@thi.ng/vectors/api";
import { max } from "@thi.ng/vectors/max";
import { min } from "@thi.ng/vectors/min";
import { mul2 } from "@thi.ng/vectors/mul";
import { mulN2 } from "@thi.ng/vectors/muln";
import { set2, set3 } from "@thi.ng/vectors/set";
import { set2 } from "@thi.ng/vectors/set";
import { sub2 } from "@thi.ng/vectors/sub";
import { subN2 } from "@thi.ng/vectors/subn";
import { aabbFromMinMax } from "./aabb.js";
Expand All @@ -39,6 +38,7 @@ export const bounds: MultiFn1<IShape, AABBLike | undefined> = defmulti<
__dispatch,
{
aabb: "rect",
bpatch: "points",
poly: "points",
polyline: "points",
quad: "points",
Expand Down Expand Up @@ -83,15 +83,9 @@ export const bounds: MultiFn1<IShape, AABBLike | undefined> = defmulti<
return b ? new Rect(...b) : undefined;
},

points: ($: PCLike) =>
rectFromMinMax(
..._bounds($.points, set2([], MAX2), set2([], MIN2))
),
points: ($: PCLike) => rectFromMinMax(...bounds2($.points)),

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

quadratic: ({ points }: Quadratic) =>
rectFromMinMax(...quadraticBounds(points[0], points[1], points[2])),
Expand Down
2 changes: 1 addition & 1 deletion packages/geom/src/center.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const center: MultiFn1O<IShape, ReadonlyVec, IShape | undefined> =
__dispatch,
{},
{
[DEFAULT]: ($, origin = ZERO3) => {
[DEFAULT]: ($: IShape, origin = ZERO3) => {
const c = centroid($);
return c ? translate($, submN(null, c, origin, -1)) : undefined;
},
Expand Down
2 changes: 1 addition & 1 deletion packages/geom/src/flip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const flip: MultiFn1<IShape, IShape> = defmulti<any, IShape>(
tri: "points",
},
{
[DEFAULT]: (x) => x,
[DEFAULT]: (x: IShape) => x,

arc: ($: Arc) => {
const t = $.start;
Expand Down
2 changes: 2 additions & 0 deletions packages/geom/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "./api/aabb.js";
export * from "./api/apc.js";
export * from "./api/arc.js";
export * from "./api/bpatch.js";
export * from "./api/circle.js";
export * from "./api/cubic.js";
export * from "./api/ellipse.js";
Expand All @@ -22,6 +23,7 @@ export * from "./api/triangle.js";

export * from "./aabb.js";
export * from "./arc.js";
export * from "./bpatch.js";
export * from "./circle.js";
export * from "./cubic.js";
export * from "./ellipse.js";
Expand Down
3 changes: 2 additions & 1 deletion packages/geom/src/tessellate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const tessellate = defmulti<IShape, Tessellator[], Vec[][]>(
__dispatch,
{},
{
[DEFAULT]: ($, fns) => _tessellate(vertices($), fns),
[DEFAULT]: ($: IShape, fns: Tessellator[]) =>
_tessellate(vertices($), fns),
}
);
3 changes: 2 additions & 1 deletion packages/geom/src/vertices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const vertices: MultiFn1O<
__dispatch,
{
line: "polyline",
bpatch: "points",
points3: "points",
quad: "poly",
tri: "poly",
Expand Down Expand Up @@ -143,7 +144,7 @@ export const vertices: MultiFn1O<
* Takes array of vectors or an `IShape`. If the latter, calls {@link vertices}
* and return result, else returns original array.
*
* @param shape -
* @param shape -
*/
export const ensureVertices = (shape: IShape | Vec[]) =>
isArray(shape) ? shape : vertices(shape);
Expand Down

0 comments on commit 0e3b99a

Please sign in to comment.