diff --git a/deno.json b/deno.json index f17def0..3ce9b68 100644 --- a/deno.json +++ b/deno.json @@ -2,7 +2,7 @@ "name": "@ltgc/kd-tree", "version": "0.0.2", "exports": { - "./kd-tree": "./dist/kd-tree.mjs" + "./kd-tree": "./dist/kd-tree.d.mts" }, "publish": { "include": [ @@ -26,4 +26,4 @@ }, "nodeModulesDir": false, "npmRegistry": "http://127.0.0.1:80/" -} \ No newline at end of file +} diff --git a/sh/publish.sh b/sh/publish.sh index f44889b..f8c1b55 100644 --- a/sh/publish.sh +++ b/sh/publish.sh @@ -2,5 +2,5 @@ echo "Building..." shx build echo "Publishing to JSR..." -deno publish --config ./deno.json +deno publish --allow-dirty --config ./deno.json exit \ No newline at end of file diff --git a/src/kd-tree/index.d.mts b/src/kd-tree/index.d.mts new file mode 100644 index 0000000..6a4044f --- /dev/null +++ b/src/kd-tree/index.d.mts @@ -0,0 +1,98 @@ +/** + * k-d Tree JavaScript - v1.1 + * + * https://github.com/ubilabs/kd-tree-javascript + * https://github.com/ltgcgo/kd-tree + * https://codeberg.org/ltgc/kd-tree + * + * @author Mircea Pricop , 2012 + * @author Martin Kleppe , 2012 + * @author Ubilabs http://ubilabs.net, 2012 + * @author Lumière Élevé , 2025 + * @author Lightingale Community https://ltgc.cc, 2025 + * @license MIT License + */ + +/** + * A k-dimensional tree. + * ```js + * import { + * KDTree + * } from "./kd-tree.mjs"; + * + * const points = [ + * [0, 0, 0], + * [11, 45, 14], + * [191, 98, 10], + * [255, 255, 255] + * ]; + * const prop3Dim = [0, 1, 2]; + * const dist3Dim = (a, b) => { + * let sum = 0; + * for (let e of prop3Dim) { + * let diff = a[e] - b[e]; + * sum += diff * diff; + * }; + * return sum; + * }; + * + * let exampleTree = new KDTree(points, dist3Dim, prop3Dim); + * ``` + @module + */ + +/* + * Node object of a k-dimensional tree. + */ +export class TreeNode { + /** The left side of the node. */ + left: TreeNode; + /** The right side of the node. */ + right: TreeNode; + /** The referred-to object. */ + obj: any; + /** The parent node of the node. */ + parent: TreeNode; + /** The dimension specifier of this node. */ + dimension: Number; + /** + @param object The referred-to object holding actual data. + @param dimension The dimension specifier of the node. + @param parent The parent of the node. + */ + constructor(object: any, dimension: Number, parent?: TreeNode); +} + +/* + * The k-dimensional tree. + */ +export class KDTree { + /** The root of the k-d tree. */ + root: TreeNode; + /** The dimension specifier array of the k-d tree. */ + get dimensions(): ArrayLike; + /** The function used to measure distance. */ + get metric(): Function; + /** Returns the serialized internal structure of the k-d tree. */ + toJSON(src?: TreeNode): object; + /** Insert a point. */ + insert(point: ArrayLike): void; + /** Remove a point. */ + remove(point: ArrayLike): void; + /** Returns the nearest N points around the specified coordinate. */ + nearest(point: ArrayLike, maxNodes: Number, maxDist: Number): ArrayLike>; + /** Returns the balance factor of the tree. */ + balanceFactor(): Number; + /** + * @param points All of the points to be put in the tree. + * @param metric The distance measuring function. + * @param dimensions The array containing the dimension specifiers. + */ + constructor(points: ArrayLike>, metric: Function, dimensions: ArrayLike); +} + +/* + * Binary heap implementation from http://eloquentjavascript.net/appendix2.html . Rewritten into modern syntax. + * I don't know how this specific implemention work yet, so no docs. + */ +export class KDBinaryHeap {} diff --git a/src/kd-tree/index.mjs b/src/kd-tree/index.mjs index f4a4487..6f021e8 100644 --- a/src/kd-tree/index.mjs +++ b/src/kd-tree/index.mjs @@ -398,5 +398,6 @@ let KDTree = class KDTree { export { KDTree, + TreeNode, KDBinaryHeap };