@thi.ng/distance 2.2.13 → 2.3.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**: 2023-04-08T11:09:50Z
3
+ - **Last updated**: 2023-05-28T09:56:37Z
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,13 @@ 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
+ ## [2.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/distance@2.3.0) (2023-05-28)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add Radial neighborhood impls ([667fed9](https://github.com/thi-ng/umbrella/commit/667fed9))
17
+ - add radial/2/3/N() factories
18
+
12
19
  ## [2.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/distance@2.2.0) (2023-02-05)
13
20
 
14
21
  #### 🚀 Features
package/README.md CHANGED
@@ -14,6 +14,7 @@ This project is part of the
14
14
  - [Neighborhoods](#neighborhoods)
15
15
  - [Nearest](#nearest)
16
16
  - [KNearest](#knearest)
17
+ - [Radial](#radial)
17
18
  - [Status](#status)
18
19
  - [Support packages](#support-packages)
19
20
  - [Related packages](#related-packages)
@@ -81,6 +82,12 @@ results can be optionally returned in order of proximity (via `.deref()` or
81
82
  `.values()`). For K=1 it will be more efficient to use `Nearest` to avoid the
82
83
  additional overhead.
83
84
 
85
+ #### Radial
86
+
87
+ An unbounded and unsorted version of [`KNearest`](#knearest), selecting _all_
88
+ items around the target location and given search radius. Qualifying neighbors
89
+ will be accumulated in order of processing via an internal array.
90
+
84
91
  ## Status
85
92
 
86
93
  **STABLE** - used in production
@@ -122,7 +129,7 @@ For Node.js REPL:
122
129
  const distance = await import("@thi.ng/distance");
123
130
  ```
124
131
 
125
- Package sizes (brotli'd, pre-treeshake): ESM: 1.15 KB
132
+ Package sizes (brotli'd, pre-treeshake): ESM: 1.24 KB
126
133
 
127
134
  ## Dependencies
128
135
 
package/index.d.ts CHANGED
@@ -5,5 +5,6 @@ export * from "./haversine.js";
5
5
  export * from "./knearest.js";
6
6
  export * from "./manhattan.js";
7
7
  export * from "./nearest.js";
8
+ export * from "./radial.js";
8
9
  export * from "./squared.js";
9
10
  //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -5,4 +5,5 @@ export * from "./haversine.js";
5
5
  export * from "./knearest.js";
6
6
  export * from "./manhattan.js";
7
7
  export * from "./nearest.js";
8
+ export * from "./radial.js";
8
9
  export * from "./squared.js";
package/knearest.d.ts CHANGED
@@ -5,7 +5,8 @@ import type { IDistance, INeighborhood, Neighbor } from "./api.js";
5
5
  /**
6
6
  * A {@link INeighborhood} implementation for K-nearest neighbor queries around
7
7
  * a given target location, initial query radius and {@link IDistance} metric to
8
- * determine proximity.
8
+ * determine proximity. See {@link Radial} for an unbounded and unsorted
9
+ * version.
9
10
  *
10
11
  * @remarks
11
12
  * The K-nearest neighbors will be accumulated via an internal
package/knearest.js CHANGED
@@ -5,7 +5,8 @@ import { DIST_SQ, DIST_SQ1, DIST_SQ2, DIST_SQ3 } from "./squared.js";
5
5
  /**
6
6
  * A {@link INeighborhood} implementation for K-nearest neighbor queries around
7
7
  * a given target location, initial query radius and {@link IDistance} metric to
8
- * determine proximity.
8
+ * determine proximity. See {@link Radial} for an unbounded and unsorted
9
+ * version.
9
10
  *
10
11
  * @remarks
11
12
  * The K-nearest neighbors will be accumulated via an internal
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/distance",
3
- "version": "2.2.13",
3
+ "version": "2.3.0",
4
4
  "description": "N-dimensional distance metrics & K-nearest neighborhoods for point queries",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -34,19 +34,19 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/api": "^8.8.0",
38
- "@thi.ng/checks": "^3.3.12",
39
- "@thi.ng/errors": "^2.2.15",
40
- "@thi.ng/heaps": "^2.1.31",
41
- "@thi.ng/math": "^5.4.9",
42
- "@thi.ng/vectors": "^7.6.13"
37
+ "@thi.ng/api": "^8.8.1",
38
+ "@thi.ng/checks": "^3.3.13",
39
+ "@thi.ng/errors": "^2.2.16",
40
+ "@thi.ng/heaps": "^2.1.32",
41
+ "@thi.ng/math": "^5.4.10",
42
+ "@thi.ng/vectors": "^7.6.14"
43
43
  },
44
44
  "devDependencies": {
45
- "@microsoft/api-extractor": "^7.34.4",
46
- "@thi.ng/testament": "^0.3.15",
47
- "rimraf": "^4.4.1",
45
+ "@microsoft/api-extractor": "^7.34.8",
46
+ "@thi.ng/testament": "^0.3.16",
47
+ "rimraf": "^5.0.0",
48
48
  "tools": "^0.0.1",
49
- "typedoc": "^0.23.28",
49
+ "typedoc": "^0.24.6",
50
50
  "typescript": "^5.0.4"
51
51
  },
52
52
  "keywords": [
@@ -97,6 +97,9 @@
97
97
  "./nearest": {
98
98
  "default": "./nearest.js"
99
99
  },
100
+ "./radial": {
101
+ "default": "./radial.js"
102
+ },
100
103
  "./squared": {
101
104
  "default": "./squared.js"
102
105
  }
@@ -109,5 +112,5 @@
109
112
  ],
110
113
  "year": 2021
111
114
  },
112
- "gitHead": "ca22f02a137c0a4e3a38ef81e82e2bc7e3c43849\n"
115
+ "gitHead": "065e40c90123541fcb126b825a07f84975431fb6\n"
113
116
  }
package/radial.d.ts ADDED
@@ -0,0 +1,93 @@
1
+ import type { IDeref } from "@thi.ng/api";
2
+ import type { ReadonlyVec } from "@thi.ng/vectors";
3
+ import type { IDistance, INeighborhood, Neighbor } from "./api.js";
4
+ /**
5
+ * A {@link INeighborhood} implementation for radial neighbor queries around a
6
+ * given target location, initial query radius and {@link IDistance} metric to
7
+ * determine proximity. Unbounded and unsorted version of {@link KNearest}.
8
+ *
9
+ * @remarks
10
+ * Qualifying neighbors will be accumulated in order of processing via an
11
+ * internal array and can be obtained via {@link Radial.deref} or
12
+ * {@link Radial.values}.
13
+ *
14
+ * @typeParam D - spatial position for distance metric
15
+ * @typeParam T - indexed value
16
+ */
17
+ export declare class Radial<D, T> implements INeighborhood<D, T>, IDeref<Neighbor<T>[]> {
18
+ readonly dist: IDistance<D>;
19
+ target: D;
20
+ radius: number;
21
+ protected _r: number;
22
+ protected _items: [number, T][];
23
+ constructor(dist: IDistance<D>, target: D, radius?: number);
24
+ /**
25
+ * Clears current results.
26
+ */
27
+ reset(): this;
28
+ /**
29
+ * Resets search/reference position and clears current results.
30
+ *
31
+ * @param target
32
+ */
33
+ setTarget(target: D): void;
34
+ /**
35
+ * Resets search/query radius and clears current results.
36
+ *
37
+ * @param r
38
+ */
39
+ setRadius(r: number): void;
40
+ /**
41
+ * Returns an array of current neighbor result tuples (each `[dist, val]`).
42
+ *
43
+ * @remarks
44
+ * Use {@link Radial.values} to obtain result values **without** their
45
+ * distance metrics.
46
+ */
47
+ deref(): [number, T][];
48
+ /**
49
+ * Similar to {@link Radial.deref}, but returns array of result values **without**
50
+ * their distance metrics.
51
+ */
52
+ values(): T[];
53
+ includesDistance(d: number, eucledian?: boolean): boolean;
54
+ consider(pos: D, val: T): number;
55
+ }
56
+ /**
57
+ * Defines a {@link Radial} instance for arbitrary length vector positions
58
+ * and, by default, using an infinite region radius and {@link DIST_SQ} distance
59
+ * metric.
60
+ *
61
+ * @param p -
62
+ * @param r -
63
+ * @param dist -
64
+ */
65
+ export declare const radial: <T>(p: ReadonlyVec, r?: number, dist?: import("./squared.js").Squared<ReadonlyVec>) => Radial<ReadonlyVec, T>;
66
+ /**
67
+ * Defines a {@link Radial} instance for 2D vector positions and, by default,
68
+ * using an infinite region radius and {@link DIST_SQ2} distance metric.
69
+ *
70
+ * @param p -
71
+ * @param r -
72
+ * @param dist -
73
+ */
74
+ export declare const radial2: <T>(p: ReadonlyVec, r?: number, dist?: import("./squared.js").Squared<ReadonlyVec>) => Radial<ReadonlyVec, T>;
75
+ /**
76
+ * Defines a {@link Radial} instance for 3D vector positions, by default,
77
+ * using an infinite region radius and {@link DIST_SQ3} distance metric.
78
+ *
79
+ * @param p -
80
+ * @param r -
81
+ * @param dist -
82
+ */
83
+ export declare const radial3: <T>(p: ReadonlyVec, r?: number, dist?: import("./squared.js").Squared<ReadonlyVec>) => Radial<ReadonlyVec, T>;
84
+ /**
85
+ * Defines a {@link KNearest} instance for numeric positions and, by default,
86
+ * using an infinite region radius and {@link DIST_SQ1} distance metric.
87
+ *
88
+ * @param p -
89
+ * @param r -
90
+ * @param dist -
91
+ */
92
+ export declare const radialN: <T>(p: number, r?: number, dist?: import("./squared.js").Squared<number>) => Radial<number, T>;
93
+ //# sourceMappingURL=radial.d.ts.map
package/radial.js ADDED
@@ -0,0 +1,113 @@
1
+ import { clamp0 } from "@thi.ng/math/interval";
2
+ import { DIST_SQ, DIST_SQ1, DIST_SQ2, DIST_SQ3 } from "./squared.js";
3
+ /**
4
+ * A {@link INeighborhood} implementation for radial neighbor queries around a
5
+ * given target location, initial query radius and {@link IDistance} metric to
6
+ * determine proximity. Unbounded and unsorted version of {@link KNearest}.
7
+ *
8
+ * @remarks
9
+ * Qualifying neighbors will be accumulated in order of processing via an
10
+ * internal array and can be obtained via {@link Radial.deref} or
11
+ * {@link Radial.values}.
12
+ *
13
+ * @typeParam D - spatial position for distance metric
14
+ * @typeParam T - indexed value
15
+ */
16
+ export class Radial {
17
+ constructor(dist, target, radius = Infinity) {
18
+ this.dist = dist;
19
+ this.target = target;
20
+ this.radius = radius;
21
+ this.setRadius(radius);
22
+ }
23
+ /**
24
+ * Clears current results.
25
+ */
26
+ reset() {
27
+ this._items = [];
28
+ return this;
29
+ }
30
+ /**
31
+ * Resets search/reference position and clears current results.
32
+ *
33
+ * @param target
34
+ */
35
+ setTarget(target) {
36
+ this.target = target;
37
+ this.reset();
38
+ }
39
+ /**
40
+ * Resets search/query radius and clears current results.
41
+ *
42
+ * @param r
43
+ */
44
+ setRadius(r) {
45
+ this.radius = clamp0(r);
46
+ this._r = this.dist.to(this.radius);
47
+ this.reset();
48
+ }
49
+ /**
50
+ * Returns an array of current neighbor result tuples (each `[dist, val]`).
51
+ *
52
+ * @remarks
53
+ * Use {@link Radial.values} to obtain result values **without** their
54
+ * distance metrics.
55
+ */
56
+ deref() {
57
+ return this._items;
58
+ }
59
+ /**
60
+ * Similar to {@link Radial.deref}, but returns array of result values **without**
61
+ * their distance metrics.
62
+ */
63
+ values() {
64
+ return this._items.map((x) => x[1]);
65
+ }
66
+ includesDistance(d, eucledian = true) {
67
+ return (eucledian ? this.dist.to(d) : d) <= this._r;
68
+ }
69
+ consider(pos, val) {
70
+ const d = this.dist.metric(this.target, pos);
71
+ if (d <= this._r) {
72
+ this._items.push([d, val]);
73
+ }
74
+ return d;
75
+ }
76
+ }
77
+ /**
78
+ * Defines a {@link Radial} instance for arbitrary length vector positions
79
+ * and, by default, using an infinite region radius and {@link DIST_SQ} distance
80
+ * metric.
81
+ *
82
+ * @param p -
83
+ * @param r -
84
+ * @param dist -
85
+ */
86
+ export const radial = (p, r, dist = DIST_SQ) => new Radial(dist, p, r);
87
+ /**
88
+ * Defines a {@link Radial} instance for 2D vector positions and, by default,
89
+ * using an infinite region radius and {@link DIST_SQ2} distance metric.
90
+ *
91
+ * @param p -
92
+ * @param r -
93
+ * @param dist -
94
+ */
95
+ export const radial2 = (p, r, dist = DIST_SQ2) => new Radial(dist, p, r);
96
+ /**
97
+ * Defines a {@link Radial} instance for 3D vector positions, by default,
98
+ * using an infinite region radius and {@link DIST_SQ3} distance metric.
99
+ *
100
+ * @param p -
101
+ * @param r -
102
+ * @param dist -
103
+ */
104
+ export const radial3 = (p, r, dist = DIST_SQ3) => new Radial(dist, p, r);
105
+ /**
106
+ * Defines a {@link KNearest} instance for numeric positions and, by default,
107
+ * using an infinite region radius and {@link DIST_SQ1} distance metric.
108
+ *
109
+ * @param p -
110
+ * @param r -
111
+ * @param dist -
112
+ */
113
+ export const radialN = (p, r, dist = DIST_SQ1) => new Radial(dist, p, r);