Skip to content

Commit

Permalink
feat(geom): add/expose subdiv & tessellation presets
Browse files Browse the repository at this point in the history
- re-export aliases for subdivCurve() from thi.ng/geom-subdiv-curve (as `SUBDIV_XXX`...)
- re-export aliases for tessellate() from thi.ng/geom-tessellate (as `TESSELLATE_XXX`...)
  • Loading branch information
postspectacular committed Mar 24, 2023
1 parent 189446d commit 0f79c6d
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
65 changes: 64 additions & 1 deletion packages/geom/src/subdiv-curve.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { MultiFn2O } from "@thi.ng/defmulti";
import { defmulti } from "@thi.ng/defmulti/defmulti";
import type { IShape, SubdivKernel } from "@thi.ng/geom-api";
import * as sdc from "@thi.ng/geom-subdiv-curve/api";
import { subdivide } from "@thi.ng/geom-subdiv-curve/subdivide";
import { Polygon } from "./api/polygon.js";
import { Polyline } from "./api/polyline.js";
import { asPolygon } from "./as-polygon.js";
import { asPolyline } from "./as-polyline.js";
import { __copyAttribs } from "./internal/copy.js";
import { __dispatch } from "./internal/dispatch.js";

Expand All @@ -19,8 +22,20 @@ import { __dispatch } from "./internal/dispatch.js";
*
* Currently only implemented for:
*
* - {@link Arc}
* - {@link Circle}
* - {@link Ellipse}
* - {@link Line}
* - {@link Polygon}
* - {@link Polyline}
* - {@link Quad}
* - {@link Rect}
* - {@link Triangle}
*
* @example
* ```ts
*
* ```
*
* @param shape
* @param kernel
Expand All @@ -29,8 +44,20 @@ import { __dispatch } from "./internal/dispatch.js";
export const subdivCurve: MultiFn2O<IShape, SubdivKernel, number, IShape> =
defmulti<any, SubdivKernel, number | undefined, IShape>(
__dispatch,
{},
{
ellipse: "circle",
line: "polyline",
quad: "poly",
rect: "circle",
tri: "poly",
},
{
arc: ($, kernel, iter = 1) =>
subdivCurve(asPolyline($), kernel, iter),

circle: ($, kernel, iter = 1) =>
subdivCurve(asPolygon($), kernel, iter),

poly: ($: Polygon, kernel, iter = 1) =>
new Polygon(
subdivide($.points, kernel, iter),
Expand All @@ -44,3 +71,39 @@ export const subdivCurve: MultiFn2O<IShape, SubdivKernel, number, IShape> =
),
}
);

/**
* Re-export of thi.ng/geom-subdiv-curve
* [`SUBDIV_MID_OPEN`](https://docs.thi.ng/umbrella/geom-subdiv-curve/variables/SUBDIV_MID_OPEN.html)
*/
export const SUBDIV_MID_OPEN = sdc.SUBDIV_MID_OPEN;
/**
* Re-export of thi.ng/geom-subdiv-curve
* [`SUBDIV_MID_CLOSED`](https://docs.thi.ng/umbrella/geom-subdiv-curve/variables/SUBDIV_MID_CLOSED.html)
*/
export const SUBDIV_MID_CLOSED = sdc.SUBDIV_MID_CLOSED;
/**
* Re-export of thi.ng/geom-subdiv-curve
* [`SUBDIV_THIRDS_OPEN`](https://docs.thi.ng/umbrella/geom-subdiv-curve/variables/SUBDIV_THIRDS_OPEN.html)
*/
export const SUBDIV_THIRDS_OPEN = sdc.SUBDIV_THIRDS_OPEN;
/**
* Re-export of thi.ng/geom-subdiv-curve
* [`SUBDIV_THIRDS_CLOSED`](https://docs.thi.ng/umbrella/geom-subdiv-curve/variables/SUBDIV_THIRDS_CLOSED.html)
*/
export const SUBDIV_THIRDS_CLOSED = sdc.SUBDIV_THIRDS_CLOSED;
/**
* Re-export of thi.ng/geom-subdiv-curve
* [`SUBDIV_CHAIKIN_OPEN`](https://docs.thi.ng/umbrella/geom-subdiv-curve/variables/SUBDIV_CHAIKIN_OPEN.html)
*/
export const SUBDIV_CHAIKIN_OPEN = sdc.SUBDIV_CHAIKIN_OPEN;
/**
* Re-export of thi.ng/geom-subdiv-curve
* [`SUBDIV_CHAIKIN_CLOSED`](https://docs.thi.ng/umbrella/geom-subdiv-curve/variables/SUBDIV_CHAIKIN_CLOSED.html)
*/
export const SUBDIV_CHAIKIN_CLOSED = sdc.SUBDIV_CHAIKIN_CLOSED;
/**
* Re-export of thi.ng/geom-subdiv-curve
* [`SUBDIV_CUBIC_CLOSED`](https://docs.thi.ng/umbrella/geom-subdiv-curve/variables/SUBDIV_CUBIC_CLOSED.html)
*/
export const SUBDIV_CUBIC_CLOSED = sdc.SUBDIV_CUBIC_CLOSED;
37 changes: 37 additions & 0 deletions packages/geom/src/tessellate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { DEFAULT, defmulti } from "@thi.ng/defmulti/defmulti";
import type { IShape, Tessellator } from "@thi.ng/geom-api";
import { earCut2 } from "@thi.ng/geom-tessellate/earcut";
import { edgeSplit } from "@thi.ng/geom-tessellate/edge-split";
import { tesselInset } from "@thi.ng/geom-tessellate/inset";
import { quadFan } from "@thi.ng/geom-tessellate/quad-fan";
import { rimTris } from "@thi.ng/geom-tessellate/rim-tris";
import { tessellate as _tessellate } from "@thi.ng/geom-tessellate/tessellate";
import { triFan } from "@thi.ng/geom-tessellate/tri-fan";
import type { Vec } from "@thi.ng/vectors";
import { __dispatch } from "./internal/dispatch.js";
import { vertices } from "./vertices.js";
Expand All @@ -26,3 +32,34 @@ export const tessellate = defmulti<IShape, Tessellator[], Vec[][]>(
_tessellate(vertices($), fns),
}
);

/**
* Alias for thi.ng/geom-tessellate
* [`earCut2`](https://docs.thi.ng/umbrella/geom-tessellate/functions/earCut2.html)
*/
export const TESSELLATE_EARCUT = earCut2;
/**
* Alias for thi.ng/geom-tessellate
* [`edgeSplit`](https://docs.thi.ng/umbrella/geom-tessellate/functions/edgeSplit.html)
*/
export const TESSELLATE_EDGE_SPLIT = edgeSplit;
/**
* Alias for thi.ng/geom-tessellate
* [`tesselInset`](https://docs.thi.ng/umbrella/geom-tessellate/functions/tesselInset.html)
*/
export const TESSELLATE_INSET = tesselInset;
/**
* Alias for thi.ng/geom-tessellate
* [`quadFan`](https://docs.thi.ng/umbrella/geom-tessellate/functions/quadFan.html)
*/
export const TESSELLATE_QUAD_FAN = quadFan;
/**
* Alias for thi.ng/geom-tessellate
* [`rimTris`](https://docs.thi.ng/umbrella/geom-tessellate/functions/rimTris.html)
*/
export const TESSELLATE_RIM_TRIS = rimTris;
/**
* Alias for thi.ng/geom-tessellate
* [`triFan`](https://docs.thi.ng/umbrella/geom-tessellate/functions/triFan.html)
*/
export const TESSELLATE_TRI_FAN = triFan;

0 comments on commit 0f79c6d

Please sign in to comment.