Skip to content

Commit

Permalink
feat(geom-axidraw): add interleave opts support for shape groups
Browse files Browse the repository at this point in the history
- update asAxiDraw() impl for shape groups
  • Loading branch information
postspectacular committed Mar 18, 2023
1 parent 490b509 commit 57783ff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
6 changes: 3 additions & 3 deletions packages/geom-axidraw/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export interface AxiDrawAttribs {
*/
skip: number;
/**
* Currently only supported for point clouds. See {@link InterleaveOpts} for
* details.
* Currently only supported for shape groups and point clouds. See
* {@link InterleaveOpts} for details.
*/
interleave: InterleaveOpts;
}
Expand Down Expand Up @@ -97,7 +97,7 @@ export interface InterleaveOpts {
num: number;
/**
* Single arg function which is called every `num` elements (with the count
* of elements already processed) and each time yielding a
* of elements already processed given as arg) and each time yielding a
* [`DrawCommand`](https://docs.thi.ng/umbrella/axidraw/types/DrawCommand.html)
* sequence, which will be inserted as-is into the generated main command
* sequence of the currently processed shape.
Expand Down
38 changes: 26 additions & 12 deletions packages/geom-axidraw/src/as-axidraw.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { DrawCommand } from "@thi.ng/axidraw/api";
import { DOWN, MOVE, UP } from "@thi.ng/axidraw/commands.js";
import { DOWN, MOVE, UP } from "@thi.ng/axidraw/commands";
import { polyline } from "@thi.ng/axidraw/polyline";
import type { MultiFn1O } from "@thi.ng/defmulti";
import { defmulti } from "@thi.ng/defmulti/defmulti";
import type { Group } from "@thi.ng/geom";
import type { Attribs, IShape, PCLike } from "@thi.ng/geom-api";
import type { Attribs, IHiccupShape, IShape, PCLike } from "@thi.ng/geom-api";
import { clipPolylinePoly } from "@thi.ng/geom-clip-line/clip-poly";
import { pointInPolygon2 } from "@thi.ng/geom-isec/point";
import { applyTransforms } from "@thi.ng/geom/apply-transforms";
Expand Down Expand Up @@ -103,18 +103,32 @@ function* __group(
$: Group,
opts?: Partial<AsAxiDrawOpts>
): IterableIterator<DrawCommand> {
const { skip, sort } = __axiAttribs($.attribs);
const { skip, sort, interleave } = __axiAttribs($.attribs);
const sopts = __sampleAttribs(opts?.samples, $.attribs);
const children = skip ? [...takeNth(skip + 1, $.children)] : $.children;
const childrenIter = sort ? (<ShapeOrdering>sort)(children) : children;
for (let child of childrenIter) {
const shape = applyTransforms(child);
shape.attribs = {
...$.attribs,
...shape.attribs,
__samples: __sampleAttribs(sopts, shape.attribs),
};
yield* asAxiDraw(shape, opts);
function* emitChunk(chunk: IHiccupShape[]) {
const iter = sort ? (<ShapeOrdering>sort)(chunk) : chunk;
for (let child of iter) {
const shape = applyTransforms(child);
shape.attribs = {
...$.attribs,
...shape.attribs,
__samples: __sampleAttribs(sopts, shape.attribs),
};
yield* asAxiDraw(shape, opts);
}
}
if (interleave) {
const { num, commands } = interleave;
if (interleave.start !== false) yield* commands(0);
for (let i = 0, n = children.length; i < n; ) {
yield* emitChunk(children.slice(i, i + num));
i += num;
if (i < n) yield* commands(i);
}
if (interleave.end) yield* interleave.commands(children.length);
} else {
yield* emitChunk(children);
}
}

Expand Down

0 comments on commit 57783ff

Please sign in to comment.