@thi.ng/geom-axidraw 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2022-12-22T21:47:07Z
3
+ - **Last updated**: 2022-12-29T20:56:59Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,21 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [0.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/geom-axidraw@0.2.0) (2022-12-29)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add shapesByNearestNeighbor() ([0059766](https://github.com/thi-ng/umbrella/commit/0059766))
17
+ - add skip attrib ([4675151](https://github.com/thi-ng/umbrella/commit/4675151))
18
+ - update group & points conversion fns
19
+ - update deps
20
+
21
+ #### ♻️ Refactoring
22
+
23
+ - update pointsByNearestNeighbor() ([8a171e0](https://github.com/thi-ng/umbrella/commit/8a171e0))
24
+ - lift `accel` spatial index impl as arg
25
+ - update doc strings
26
+
12
27
  ## [0.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/geom-axidraw@0.1.0) (2022-12-10)
13
28
 
14
29
  #### 🚀 Features
package/api.d.ts CHANGED
@@ -52,6 +52,12 @@ export interface AxiDrawAttribs {
52
52
  * - {@link shapesByProximity} (for `group()`)
53
53
  */
54
54
  sort: PointOrdering | ShapeOrdering;
55
+ /**
56
+ * Only used for groups or point clouds. If given, only every (n+1)th child
57
+ * shape or point is being processed and the others ignored. Useful for low
58
+ * detail test runs.
59
+ */
60
+ skip: number;
55
61
  }
56
62
  export interface AsAxiDrawOpts {
57
63
  /**
package/as-axidraw.js CHANGED
@@ -6,6 +6,7 @@ import { pointInPolygon2 } from "@thi.ng/geom-isec/point";
6
6
  import { applyTransforms } from "@thi.ng/geom/apply-transforms";
7
7
  import { asPolyline } from "@thi.ng/geom/as-polyline";
8
8
  import { __dispatch } from "@thi.ng/geom/internal/dispatch";
9
+ import { takeNth } from "@thi.ng/transducers/take-nth";
9
10
  import { pointsByNearestNeighbor } from "./sort.js";
10
11
  /**
11
12
  * Lazily converts given shape (or group) into an iterable of thi.ng/axidraw
@@ -67,9 +68,10 @@ export const asAxiDraw = defmulti(__dispatch, {
67
68
  group: ($, opts) => __group($, opts),
68
69
  });
69
70
  function* __group($, opts) {
70
- const { sort } = __axiAttribs($.attribs);
71
- const children = sort ? sort($.children) : $.children;
72
- for (let child of children) {
71
+ const { skip, sort } = __axiAttribs($.attribs);
72
+ const children = skip ? [...takeNth(skip + 1, $.children)] : $.children;
73
+ const childrenIter = sort ? sort(children) : children;
74
+ for (let child of childrenIter) {
73
75
  const shape = applyTransforms(child);
74
76
  shape.attribs = {
75
77
  ...shape.attribs,
@@ -81,7 +83,7 @@ function* __group($, opts) {
81
83
  function* __points(pts, attribs, opts) {
82
84
  if (!pts.length)
83
85
  return;
84
- const { clip, delayDown, delayUp, down, speed, sort } = {
86
+ const { clip, delayDown, delayUp, down, skip, speed, sort } = {
85
87
  sort: pointsByNearestNeighbor(),
86
88
  ...__axiAttribs(attribs),
87
89
  };
@@ -91,6 +93,9 @@ function* __points(pts, attribs, opts) {
91
93
  if (!pts.length)
92
94
  return;
93
95
  }
96
+ if (skip) {
97
+ pts = [...takeNth(skip + 1, pts)];
98
+ }
94
99
  yield UP;
95
100
  if (down != undefined)
96
101
  yield ["pen", down];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/geom-axidraw",
3
- "version": "0.1.3",
3
+ "version": "0.2.0",
4
4
  "description": "Conversion and preparation of thi.ng/geom shapes & shape groups to AxiDraw pen plotter draw commands",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -35,16 +35,17 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "@thi.ng/api": "^8.6.2",
38
- "@thi.ng/arrays": "^2.4.7",
39
- "@thi.ng/axidraw": "^0.2.3",
38
+ "@thi.ng/arrays": "^2.5.0",
39
+ "@thi.ng/axidraw": "^0.3.0",
40
40
  "@thi.ng/compare": "^2.1.21",
41
41
  "@thi.ng/defmulti": "^2.1.26",
42
- "@thi.ng/geom": "^4.1.0",
43
- "@thi.ng/geom-accel": "^3.2.31",
44
- "@thi.ng/geom-api": "^3.3.23",
45
- "@thi.ng/geom-clip-line": "^2.2.3",
46
- "@thi.ng/geom-isec": "^2.1.40",
47
- "@thi.ng/vectors": "^7.5.29"
42
+ "@thi.ng/geom": "^4.1.1",
43
+ "@thi.ng/geom-accel": "^3.2.32",
44
+ "@thi.ng/geom-api": "^3.3.24",
45
+ "@thi.ng/geom-clip-line": "^2.2.4",
46
+ "@thi.ng/geom-isec": "^2.1.41",
47
+ "@thi.ng/transducers": "^8.3.29",
48
+ "@thi.ng/vectors": "^7.5.30"
48
49
  },
49
50
  "devDependencies": {
50
51
  "@microsoft/api-extractor": "^7.33.7",
@@ -117,5 +118,5 @@
117
118
  "status": "alpha",
118
119
  "year": 2022
119
120
  },
120
- "gitHead": "bc6f7f5e2765bb96fe64db804eaf4b2443b47fc6\n"
121
+ "gitHead": "28bb74c67217a352d673b6efdab234921d4a370e\n"
121
122
  }
package/sort.d.ts CHANGED
@@ -1,19 +1,22 @@
1
+ import type { IRegionQuery, IShape, ISpatialMap, ISpatialSet } from "@thi.ng/geom-api";
1
2
  import { ReadonlyVec } from "@thi.ng/vectors/api";
2
3
  import type { PointOrdering, ShapeOrdering } from "./api.js";
3
4
  /**
4
- * Higher order point ordering fn. Lazily sorts points by nearest neighbor
5
+ * Higher order point ordering fn. Adds points to given spatial
6
+ * index/acceleration structure and then lazily sorts them by nearest neighbor
5
7
  * distance, starting selection of first point based on given `ref` point
6
8
  * (default: [0, 0]).
7
9
  *
8
10
  * @remarks
9
- * Internally uses a
11
+ * By default is using a
10
12
  * [`KdTreeSet`](https://docs.thi.ng/umbrella/geom-accel/classes/KdTreeSet.html)
11
13
  * to index all points and then successively perform efficient nearest neighbor
12
14
  * searches (always w.r.t the most recent result point).
13
15
  *
16
+ * @param accel
14
17
  * @param ref
15
18
  */
16
- export declare const pointsByNearestNeighbor: (ref?: ReadonlyVec) => PointOrdering;
19
+ export declare const pointsByNearestNeighbor: (accel?: ISpatialSet<ReadonlyVec> & IRegionQuery<ReadonlyVec, ReadonlyVec, number>, ref?: ReadonlyVec) => PointOrdering;
17
20
  /**
18
21
  * Higher order point ordering fn. Sorts points by proximity to given `ref` point
19
22
  * (default: [0, 0]).
@@ -28,4 +31,19 @@ export declare const pointsByProximity: (ref?: ReadonlyVec) => PointOrdering;
28
31
  * @param ref
29
32
  */
30
33
  export declare const shapesByProximity: (ref?: ReadonlyVec) => ShapeOrdering;
34
+ /**
35
+ * Similar to {@link pointsByNearestNeighbor}, however for shapes and requiring
36
+ * an
37
+ * [`ISpatialMap`](https://docs.thi.ng/umbrella/geom-api/interfaces/ISpatialMap.html)
38
+ * implementation and is using shape centroid (auto-computed) to perform
39
+ * indexing and nearest neighbor queries.
40
+ *
41
+ * @remarks
42
+ * Currently recommended to use
43
+ * [`NdQuadtreeMap`](https://docs.thi.ng/umbrella/geom-accel/classes/NdQuadtreeMap.html).
44
+ *
45
+ * @param accel
46
+ * @param ref
47
+ */
48
+ export declare const shapesByNearestNeighbor: (accel: ISpatialMap<ReadonlyVec, IShape> & IRegionQuery<ReadonlyVec, IShape, number>, ref?: ReadonlyVec) => ShapeOrdering;
31
49
  //# sourceMappingURL=sort.d.ts.map
package/sort.js CHANGED
@@ -5,23 +5,26 @@ import { centroid } from "@thi.ng/geom/centroid";
5
5
  import { ZERO2 } from "@thi.ng/vectors/api";
6
6
  import { distSq2 } from "@thi.ng/vectors/distsq";
7
7
  /**
8
- * Higher order point ordering fn. Lazily sorts points by nearest neighbor
8
+ * Higher order point ordering fn. Adds points to given spatial
9
+ * index/acceleration structure and then lazily sorts them by nearest neighbor
9
10
  * distance, starting selection of first point based on given `ref` point
10
11
  * (default: [0, 0]).
11
12
  *
12
13
  * @remarks
13
- * Internally uses a
14
+ * By default is using a
14
15
  * [`KdTreeSet`](https://docs.thi.ng/umbrella/geom-accel/classes/KdTreeSet.html)
15
16
  * to index all points and then successively perform efficient nearest neighbor
16
17
  * searches (always w.r.t the most recent result point).
17
18
  *
19
+ * @param accel
18
20
  * @param ref
19
21
  */
20
- export const pointsByNearestNeighbor = (ref = ZERO2) => function* (pts) {
21
- const index = new KdTreeSet(2, pts);
22
- while (index.size) {
23
- ref = index.queryKeys(ref, 1e4, 1)[0];
24
- index.remove(ref);
22
+ export const pointsByNearestNeighbor = (accel = new KdTreeSet(2), ref = ZERO2) => function* (pts) {
23
+ accel.into(pts);
24
+ // const index = new KdTreeSet(2, pts);
25
+ while (accel.size) {
26
+ ref = accel.queryKeys(ref, 1e4, 1)[0];
27
+ accel.remove(ref);
25
28
  yield ref;
26
29
  }
27
30
  };
@@ -43,3 +46,26 @@ export const pointsByProximity = (ref = ZERO2) => (pts) => {
43
46
  export const shapesByProximity = (ref = ZERO2) => (shapes) => {
44
47
  return sortByCachedKey(shapes.slice(), (s) => distSq2(centroid(s) || ZERO2, ref), compareNumAsc);
45
48
  };
49
+ /**
50
+ * Similar to {@link pointsByNearestNeighbor}, however for shapes and requiring
51
+ * an
52
+ * [`ISpatialMap`](https://docs.thi.ng/umbrella/geom-api/interfaces/ISpatialMap.html)
53
+ * implementation and is using shape centroid (auto-computed) to perform
54
+ * indexing and nearest neighbor queries.
55
+ *
56
+ * @remarks
57
+ * Currently recommended to use
58
+ * [`NdQuadtreeMap`](https://docs.thi.ng/umbrella/geom-accel/classes/NdQuadtreeMap.html).
59
+ *
60
+ * @param accel
61
+ * @param ref
62
+ */
63
+ export const shapesByNearestNeighbor = (accel, ref = ZERO2) => function* (shapes) {
64
+ accel.into(shapes.map((s) => [centroid(s) || [0, 0], s]));
65
+ while (accel.size) {
66
+ const pair = accel.query(ref, 1e4, 1)[0];
67
+ ref = pair[0];
68
+ accel.remove(ref);
69
+ yield pair[1];
70
+ }
71
+ };