Initial commit
This commit is contained in:
15
node_modules/kdbush/LICENSE
generated
vendored
Normal file
15
node_modules/kdbush/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
ISC License
|
||||
|
||||
Copyright (c) 2018, Vladimir Agafonkin
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any purpose
|
||||
with or without fee is hereby granted, provided that the above copyright notice
|
||||
and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
||||
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
||||
THIS SOFTWARE.
|
||||
102
node_modules/kdbush/README.md
generated
vendored
Normal file
102
node_modules/kdbush/README.md
generated
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
## KDBush
|
||||
|
||||
A very fast static spatial index for 2D points based on a flat KD-tree.
|
||||
Compared to [RBush](https://github.com/mourner/rbush):
|
||||
|
||||
- **Points only** — no rectangles.
|
||||
- **Static** — you can't add/remove items after initial indexing.
|
||||
- **Faster** indexing and search, with lower **memory** footprint.
|
||||
- Index is stored as a single **array buffer** (so you can [transfer](https://developer.mozilla.org/en-US/docs/Glossary/Transferable_objects) it between threads or store it as a compact file).
|
||||
|
||||
|
||||
If you need a static index for rectangles, not only points, see [Flatbush](https://github.com/mourner/flatbush). When indexing points, KDBush has the advantage of taking ~2x less memory than Flatbush.
|
||||
|
||||
[](https://github.com/mourner/kdbush/actions)
|
||||
[](https://github.com/mourner/projects)
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
// initialize KDBush for 1000 items
|
||||
const index = new KDBush(1000);
|
||||
|
||||
// fill it with 1000 points
|
||||
for (const {x, y} of items) {
|
||||
index.add(x, y);
|
||||
}
|
||||
|
||||
// perform the indexing
|
||||
index.finish();
|
||||
|
||||
// make a bounding box query
|
||||
const foundIds = index.range(minX, minY, maxX, maxY);
|
||||
|
||||
// map ids to original items
|
||||
const foundItems = foundIds.map(i => items[i]);
|
||||
|
||||
// make a radius query
|
||||
const neighborIds = index.within(x, y, 5);
|
||||
|
||||
// instantly transfer the index from a worker to the main thread
|
||||
postMessage(index.data, [index.data]);
|
||||
|
||||
// reconstruct the index from a raw array buffer
|
||||
const index = KDBush.from(e.data);
|
||||
```
|
||||
|
||||
## Install
|
||||
|
||||
Install with NPM: `npm install kdbush`, then import as a module:
|
||||
|
||||
```js
|
||||
import KDBush from 'kdbush';
|
||||
```
|
||||
|
||||
Or use as a module directly in the browser with [jsDelivr](https://www.jsdelivr.com/esm):
|
||||
|
||||
```html
|
||||
<script type="module">
|
||||
import KDBush from 'https://cdn.jsdelivr.net/npm/kdbush/+esm';
|
||||
</script>
|
||||
```
|
||||
|
||||
Alternatively, there's a browser bundle with a `KDBush` global variable:
|
||||
|
||||
```html
|
||||
<script src="https://cdn.jsdelivr.net/npm/kdbush"></script>
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
#### new KDBush(numItems[, nodeSize, ArrayType])
|
||||
|
||||
Creates an index that will hold a given number of points (`numItems`). Additionally accepts:
|
||||
|
||||
- `nodeSize`: Size of the KD-tree node, `64` by default. Higher means faster indexing but slower search, and vise versa.
|
||||
- `ArrayType`: Array type to use for storing coordinate values. `Float64Array` by default, but if your coordinates are integer values, `Int32Array` makes the index faster and smaller.
|
||||
|
||||
#### index.add(x, y)
|
||||
|
||||
Adds a given point to the index. Returns a zero-based, incremental number that represents the newly added point.
|
||||
|
||||
#### index.range(minX, minY, maxX, maxY)
|
||||
|
||||
Finds all items within the given bounding box and returns an array of indices that refer to the order the items were added (the values returned by `index.add(x, y)`).
|
||||
|
||||
#### index.within(x, y, radius)
|
||||
|
||||
Finds all items within a given radius from the query point and returns an array of indices.
|
||||
|
||||
#### `KDBush.from(data)`
|
||||
|
||||
Recreates a KDBush index from raw `ArrayBuffer` data
|
||||
(that's exposed as `index.data` on a previously indexed KDBush instance).
|
||||
Very useful for transferring or sharing indices between threads or storing them in a file.
|
||||
|
||||
### Properties
|
||||
|
||||
- `data`: array buffer that holds the index.
|
||||
- `numItems`: number of stored items.
|
||||
- `nodeSize`: number of items in a KD-tree node.
|
||||
- `ArrayType`: array type used for internal coordinates storage.
|
||||
- `IndexArrayType`: array type used for internal item indices storage.
|
||||
53
node_modules/kdbush/index.d.ts
generated
vendored
Normal file
53
node_modules/kdbush/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
export default class KDBush {
|
||||
/**
|
||||
* Creates an index from raw `ArrayBuffer` data.
|
||||
* @param {ArrayBuffer} data
|
||||
*/
|
||||
static from(data: ArrayBuffer): KDBush;
|
||||
/**
|
||||
* Creates an index that will hold a given number of items.
|
||||
* @param {number} numItems
|
||||
* @param {number} [nodeSize=64] Size of the KD-tree node (64 by default).
|
||||
* @param {TypedArrayConstructor} [ArrayType=Float64Array] The array type used for coordinates storage (`Float64Array` by default).
|
||||
* @param {ArrayBuffer} [data] (For internal use only)
|
||||
*/
|
||||
constructor(numItems: number, nodeSize?: number | undefined, ArrayType?: TypedArrayConstructor | undefined, data?: ArrayBuffer | undefined);
|
||||
numItems: number;
|
||||
nodeSize: number;
|
||||
ArrayType: TypedArrayConstructor;
|
||||
IndexArrayType: Uint16ArrayConstructor | Uint32ArrayConstructor;
|
||||
data: ArrayBuffer;
|
||||
ids: Uint16Array | Uint32Array;
|
||||
coords: Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
|
||||
_pos: number;
|
||||
_finished: boolean;
|
||||
/**
|
||||
* Add a point to the index.
|
||||
* @param {number} x
|
||||
* @param {number} y
|
||||
* @returns {number} An incremental index associated with the added item (starting from `0`).
|
||||
*/
|
||||
add(x: number, y: number): number;
|
||||
/**
|
||||
* Perform indexing of the added points.
|
||||
*/
|
||||
finish(): KDBush;
|
||||
/**
|
||||
* Search the index for items within a given bounding box.
|
||||
* @param {number} minX
|
||||
* @param {number} minY
|
||||
* @param {number} maxX
|
||||
* @param {number} maxY
|
||||
* @returns {number[]} An array of indices correponding to the found items.
|
||||
*/
|
||||
range(minX: number, minY: number, maxX: number, maxY: number): number[];
|
||||
/**
|
||||
* Search the index for items within a given radius.
|
||||
* @param {number} qx
|
||||
* @param {number} qy
|
||||
* @param {number} r Query radius.
|
||||
* @returns {number[]} An array of indices correponding to the found items.
|
||||
*/
|
||||
within(qx: number, qy: number, r: number): number[];
|
||||
}
|
||||
export type TypedArrayConstructor = Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor;
|
||||
327
node_modules/kdbush/index.js
generated
vendored
Normal file
327
node_modules/kdbush/index.js
generated
vendored
Normal file
@@ -0,0 +1,327 @@
|
||||
|
||||
const ARRAY_TYPES = [
|
||||
Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array,
|
||||
Int32Array, Uint32Array, Float32Array, Float64Array
|
||||
];
|
||||
|
||||
/** @typedef {Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor} TypedArrayConstructor */
|
||||
|
||||
const VERSION = 1; // serialized format version
|
||||
const HEADER_SIZE = 8;
|
||||
|
||||
export default class KDBush {
|
||||
|
||||
/**
|
||||
* Creates an index from raw `ArrayBuffer` data.
|
||||
* @param {ArrayBuffer} data
|
||||
*/
|
||||
static from(data) {
|
||||
if (!(data instanceof ArrayBuffer)) {
|
||||
throw new Error('Data must be an instance of ArrayBuffer.');
|
||||
}
|
||||
const [magic, versionAndType] = new Uint8Array(data, 0, 2);
|
||||
if (magic !== 0xdb) {
|
||||
throw new Error('Data does not appear to be in a KDBush format.');
|
||||
}
|
||||
const version = versionAndType >> 4;
|
||||
if (version !== VERSION) {
|
||||
throw new Error(`Got v${version} data when expected v${VERSION}.`);
|
||||
}
|
||||
const ArrayType = ARRAY_TYPES[versionAndType & 0x0f];
|
||||
if (!ArrayType) {
|
||||
throw new Error('Unrecognized array type.');
|
||||
}
|
||||
const [nodeSize] = new Uint16Array(data, 2, 1);
|
||||
const [numItems] = new Uint32Array(data, 4, 1);
|
||||
|
||||
return new KDBush(numItems, nodeSize, ArrayType, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an index that will hold a given number of items.
|
||||
* @param {number} numItems
|
||||
* @param {number} [nodeSize=64] Size of the KD-tree node (64 by default).
|
||||
* @param {TypedArrayConstructor} [ArrayType=Float64Array] The array type used for coordinates storage (`Float64Array` by default).
|
||||
* @param {ArrayBuffer} [data] (For internal use only)
|
||||
*/
|
||||
constructor(numItems, nodeSize = 64, ArrayType = Float64Array, data) {
|
||||
if (isNaN(numItems) || numItems < 0) throw new Error(`Unpexpected numItems value: ${numItems}.`);
|
||||
|
||||
this.numItems = +numItems;
|
||||
this.nodeSize = Math.min(Math.max(+nodeSize, 2), 65535);
|
||||
this.ArrayType = ArrayType;
|
||||
this.IndexArrayType = numItems < 65536 ? Uint16Array : Uint32Array;
|
||||
|
||||
const arrayTypeIndex = ARRAY_TYPES.indexOf(this.ArrayType);
|
||||
const coordsByteSize = numItems * 2 * this.ArrayType.BYTES_PER_ELEMENT;
|
||||
const idsByteSize = numItems * this.IndexArrayType.BYTES_PER_ELEMENT;
|
||||
const padCoords = (8 - idsByteSize % 8) % 8;
|
||||
|
||||
if (arrayTypeIndex < 0) {
|
||||
throw new Error(`Unexpected typed array class: ${ArrayType}.`);
|
||||
}
|
||||
|
||||
if (data && (data instanceof ArrayBuffer)) { // reconstruct an index from a buffer
|
||||
this.data = data;
|
||||
this.ids = new this.IndexArrayType(this.data, HEADER_SIZE, numItems);
|
||||
this.coords = new this.ArrayType(this.data, HEADER_SIZE + idsByteSize + padCoords, numItems * 2);
|
||||
this._pos = numItems * 2;
|
||||
this._finished = true;
|
||||
} else { // initialize a new index
|
||||
this.data = new ArrayBuffer(HEADER_SIZE + coordsByteSize + idsByteSize + padCoords);
|
||||
this.ids = new this.IndexArrayType(this.data, HEADER_SIZE, numItems);
|
||||
this.coords = new this.ArrayType(this.data, HEADER_SIZE + idsByteSize + padCoords, numItems * 2);
|
||||
this._pos = 0;
|
||||
this._finished = false;
|
||||
|
||||
// set header
|
||||
new Uint8Array(this.data, 0, 2).set([0xdb, (VERSION << 4) + arrayTypeIndex]);
|
||||
new Uint16Array(this.data, 2, 1)[0] = nodeSize;
|
||||
new Uint32Array(this.data, 4, 1)[0] = numItems;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a point to the index.
|
||||
* @param {number} x
|
||||
* @param {number} y
|
||||
* @returns {number} An incremental index associated with the added item (starting from `0`).
|
||||
*/
|
||||
add(x, y) {
|
||||
const index = this._pos >> 1;
|
||||
this.ids[index] = index;
|
||||
this.coords[this._pos++] = x;
|
||||
this.coords[this._pos++] = y;
|
||||
return index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform indexing of the added points.
|
||||
*/
|
||||
finish() {
|
||||
const numAdded = this._pos >> 1;
|
||||
if (numAdded !== this.numItems) {
|
||||
throw new Error(`Added ${numAdded} items when expected ${this.numItems}.`);
|
||||
}
|
||||
// kd-sort both arrays for efficient search
|
||||
sort(this.ids, this.coords, this.nodeSize, 0, this.numItems - 1, 0);
|
||||
|
||||
this._finished = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the index for items within a given bounding box.
|
||||
* @param {number} minX
|
||||
* @param {number} minY
|
||||
* @param {number} maxX
|
||||
* @param {number} maxY
|
||||
* @returns {number[]} An array of indices correponding to the found items.
|
||||
*/
|
||||
range(minX, minY, maxX, maxY) {
|
||||
if (!this._finished) throw new Error('Data not yet indexed - call index.finish().');
|
||||
|
||||
const {ids, coords, nodeSize} = this;
|
||||
const stack = [0, ids.length - 1, 0];
|
||||
const result = [];
|
||||
|
||||
// recursively search for items in range in the kd-sorted arrays
|
||||
while (stack.length) {
|
||||
const axis = stack.pop() || 0;
|
||||
const right = stack.pop() || 0;
|
||||
const left = stack.pop() || 0;
|
||||
|
||||
// if we reached "tree node", search linearly
|
||||
if (right - left <= nodeSize) {
|
||||
for (let i = left; i <= right; i++) {
|
||||
const x = coords[2 * i];
|
||||
const y = coords[2 * i + 1];
|
||||
if (x >= minX && x <= maxX && y >= minY && y <= maxY) result.push(ids[i]);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// otherwise find the middle index
|
||||
const m = (left + right) >> 1;
|
||||
|
||||
// include the middle item if it's in range
|
||||
const x = coords[2 * m];
|
||||
const y = coords[2 * m + 1];
|
||||
if (x >= minX && x <= maxX && y >= minY && y <= maxY) result.push(ids[m]);
|
||||
|
||||
// queue search in halves that intersect the query
|
||||
if (axis === 0 ? minX <= x : minY <= y) {
|
||||
stack.push(left);
|
||||
stack.push(m - 1);
|
||||
stack.push(1 - axis);
|
||||
}
|
||||
if (axis === 0 ? maxX >= x : maxY >= y) {
|
||||
stack.push(m + 1);
|
||||
stack.push(right);
|
||||
stack.push(1 - axis);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the index for items within a given radius.
|
||||
* @param {number} qx
|
||||
* @param {number} qy
|
||||
* @param {number} r Query radius.
|
||||
* @returns {number[]} An array of indices correponding to the found items.
|
||||
*/
|
||||
within(qx, qy, r) {
|
||||
if (!this._finished) throw new Error('Data not yet indexed - call index.finish().');
|
||||
|
||||
const {ids, coords, nodeSize} = this;
|
||||
const stack = [0, ids.length - 1, 0];
|
||||
const result = [];
|
||||
const r2 = r * r;
|
||||
|
||||
// recursively search for items within radius in the kd-sorted arrays
|
||||
while (stack.length) {
|
||||
const axis = stack.pop() || 0;
|
||||
const right = stack.pop() || 0;
|
||||
const left = stack.pop() || 0;
|
||||
|
||||
// if we reached "tree node", search linearly
|
||||
if (right - left <= nodeSize) {
|
||||
for (let i = left; i <= right; i++) {
|
||||
if (sqDist(coords[2 * i], coords[2 * i + 1], qx, qy) <= r2) result.push(ids[i]);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// otherwise find the middle index
|
||||
const m = (left + right) >> 1;
|
||||
|
||||
// include the middle item if it's in range
|
||||
const x = coords[2 * m];
|
||||
const y = coords[2 * m + 1];
|
||||
if (sqDist(x, y, qx, qy) <= r2) result.push(ids[m]);
|
||||
|
||||
// queue search in halves that intersect the query
|
||||
if (axis === 0 ? qx - r <= x : qy - r <= y) {
|
||||
stack.push(left);
|
||||
stack.push(m - 1);
|
||||
stack.push(1 - axis);
|
||||
}
|
||||
if (axis === 0 ? qx + r >= x : qy + r >= y) {
|
||||
stack.push(m + 1);
|
||||
stack.push(right);
|
||||
stack.push(1 - axis);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Uint16Array | Uint32Array} ids
|
||||
* @param {InstanceType<TypedArrayConstructor>} coords
|
||||
* @param {number} nodeSize
|
||||
* @param {number} left
|
||||
* @param {number} right
|
||||
* @param {number} axis
|
||||
*/
|
||||
function sort(ids, coords, nodeSize, left, right, axis) {
|
||||
if (right - left <= nodeSize) return;
|
||||
|
||||
const m = (left + right) >> 1; // middle index
|
||||
|
||||
// sort ids and coords around the middle index so that the halves lie
|
||||
// either left/right or top/bottom correspondingly (taking turns)
|
||||
select(ids, coords, m, left, right, axis);
|
||||
|
||||
// recursively kd-sort first half and second half on the opposite axis
|
||||
sort(ids, coords, nodeSize, left, m - 1, 1 - axis);
|
||||
sort(ids, coords, nodeSize, m + 1, right, 1 - axis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom Floyd-Rivest selection algorithm: sort ids and coords so that
|
||||
* [left..k-1] items are smaller than k-th item (on either x or y axis)
|
||||
* @param {Uint16Array | Uint32Array} ids
|
||||
* @param {InstanceType<TypedArrayConstructor>} coords
|
||||
* @param {number} k
|
||||
* @param {number} left
|
||||
* @param {number} right
|
||||
* @param {number} axis
|
||||
*/
|
||||
function select(ids, coords, k, left, right, axis) {
|
||||
|
||||
while (right > left) {
|
||||
if (right - left > 600) {
|
||||
const n = right - left + 1;
|
||||
const m = k - left + 1;
|
||||
const z = Math.log(n);
|
||||
const s = 0.5 * Math.exp(2 * z / 3);
|
||||
const sd = 0.5 * Math.sqrt(z * s * (n - s) / n) * (m - n / 2 < 0 ? -1 : 1);
|
||||
const newLeft = Math.max(left, Math.floor(k - m * s / n + sd));
|
||||
const newRight = Math.min(right, Math.floor(k + (n - m) * s / n + sd));
|
||||
select(ids, coords, k, newLeft, newRight, axis);
|
||||
}
|
||||
|
||||
const t = coords[2 * k + axis];
|
||||
let i = left;
|
||||
let j = right;
|
||||
|
||||
swapItem(ids, coords, left, k);
|
||||
if (coords[2 * right + axis] > t) swapItem(ids, coords, left, right);
|
||||
|
||||
while (i < j) {
|
||||
swapItem(ids, coords, i, j);
|
||||
i++;
|
||||
j--;
|
||||
while (coords[2 * i + axis] < t) i++;
|
||||
while (coords[2 * j + axis] > t) j--;
|
||||
}
|
||||
|
||||
if (coords[2 * left + axis] === t) swapItem(ids, coords, left, j);
|
||||
else {
|
||||
j++;
|
||||
swapItem(ids, coords, j, right);
|
||||
}
|
||||
|
||||
if (j <= k) left = j + 1;
|
||||
if (k <= j) right = j - 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Uint16Array | Uint32Array} ids
|
||||
* @param {InstanceType<TypedArrayConstructor>} coords
|
||||
* @param {number} i
|
||||
* @param {number} j
|
||||
*/
|
||||
function swapItem(ids, coords, i, j) {
|
||||
swap(ids, i, j);
|
||||
swap(coords, 2 * i, 2 * j);
|
||||
swap(coords, 2 * i + 1, 2 * j + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {InstanceType<TypedArrayConstructor>} arr
|
||||
* @param {number} i
|
||||
* @param {number} j
|
||||
*/
|
||||
function swap(arr, i, j) {
|
||||
const tmp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} ax
|
||||
* @param {number} ay
|
||||
* @param {number} bx
|
||||
* @param {number} by
|
||||
*/
|
||||
function sqDist(ax, ay, bx, by) {
|
||||
const dx = ax - bx;
|
||||
const dy = ay - by;
|
||||
return dx * dx + dy * dy;
|
||||
}
|
||||
336
node_modules/kdbush/kdbush.js
generated
vendored
Normal file
336
node_modules/kdbush/kdbush.js
generated
vendored
Normal file
@@ -0,0 +1,336 @@
|
||||
(function (global, factory) {
|
||||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
||||
typeof define === 'function' && define.amd ? define(factory) :
|
||||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.KDBush = factory());
|
||||
})(this, (function () { 'use strict';
|
||||
|
||||
const ARRAY_TYPES = [
|
||||
Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array,
|
||||
Int32Array, Uint32Array, Float32Array, Float64Array
|
||||
];
|
||||
|
||||
/** @typedef {Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor} TypedArrayConstructor */
|
||||
|
||||
const VERSION = 1; // serialized format version
|
||||
const HEADER_SIZE = 8;
|
||||
|
||||
class KDBush {
|
||||
|
||||
/**
|
||||
* Creates an index from raw `ArrayBuffer` data.
|
||||
* @param {ArrayBuffer} data
|
||||
*/
|
||||
static from(data) {
|
||||
if (!(data instanceof ArrayBuffer)) {
|
||||
throw new Error('Data must be an instance of ArrayBuffer.');
|
||||
}
|
||||
const [magic, versionAndType] = new Uint8Array(data, 0, 2);
|
||||
if (magic !== 0xdb) {
|
||||
throw new Error('Data does not appear to be in a KDBush format.');
|
||||
}
|
||||
const version = versionAndType >> 4;
|
||||
if (version !== VERSION) {
|
||||
throw new Error(`Got v${version} data when expected v${VERSION}.`);
|
||||
}
|
||||
const ArrayType = ARRAY_TYPES[versionAndType & 0x0f];
|
||||
if (!ArrayType) {
|
||||
throw new Error('Unrecognized array type.');
|
||||
}
|
||||
const [nodeSize] = new Uint16Array(data, 2, 1);
|
||||
const [numItems] = new Uint32Array(data, 4, 1);
|
||||
|
||||
return new KDBush(numItems, nodeSize, ArrayType, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an index that will hold a given number of items.
|
||||
* @param {number} numItems
|
||||
* @param {number} [nodeSize=64] Size of the KD-tree node (64 by default).
|
||||
* @param {TypedArrayConstructor} [ArrayType=Float64Array] The array type used for coordinates storage (`Float64Array` by default).
|
||||
* @param {ArrayBuffer} [data] (For internal use only)
|
||||
*/
|
||||
constructor(numItems, nodeSize = 64, ArrayType = Float64Array, data) {
|
||||
if (isNaN(numItems) || numItems < 0) throw new Error(`Unpexpected numItems value: ${numItems}.`);
|
||||
|
||||
this.numItems = +numItems;
|
||||
this.nodeSize = Math.min(Math.max(+nodeSize, 2), 65535);
|
||||
this.ArrayType = ArrayType;
|
||||
this.IndexArrayType = numItems < 65536 ? Uint16Array : Uint32Array;
|
||||
|
||||
const arrayTypeIndex = ARRAY_TYPES.indexOf(this.ArrayType);
|
||||
const coordsByteSize = numItems * 2 * this.ArrayType.BYTES_PER_ELEMENT;
|
||||
const idsByteSize = numItems * this.IndexArrayType.BYTES_PER_ELEMENT;
|
||||
const padCoords = (8 - idsByteSize % 8) % 8;
|
||||
|
||||
if (arrayTypeIndex < 0) {
|
||||
throw new Error(`Unexpected typed array class: ${ArrayType}.`);
|
||||
}
|
||||
|
||||
if (data && (data instanceof ArrayBuffer)) { // reconstruct an index from a buffer
|
||||
this.data = data;
|
||||
this.ids = new this.IndexArrayType(this.data, HEADER_SIZE, numItems);
|
||||
this.coords = new this.ArrayType(this.data, HEADER_SIZE + idsByteSize + padCoords, numItems * 2);
|
||||
this._pos = numItems * 2;
|
||||
this._finished = true;
|
||||
} else { // initialize a new index
|
||||
this.data = new ArrayBuffer(HEADER_SIZE + coordsByteSize + idsByteSize + padCoords);
|
||||
this.ids = new this.IndexArrayType(this.data, HEADER_SIZE, numItems);
|
||||
this.coords = new this.ArrayType(this.data, HEADER_SIZE + idsByteSize + padCoords, numItems * 2);
|
||||
this._pos = 0;
|
||||
this._finished = false;
|
||||
|
||||
// set header
|
||||
new Uint8Array(this.data, 0, 2).set([0xdb, (VERSION << 4) + arrayTypeIndex]);
|
||||
new Uint16Array(this.data, 2, 1)[0] = nodeSize;
|
||||
new Uint32Array(this.data, 4, 1)[0] = numItems;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a point to the index.
|
||||
* @param {number} x
|
||||
* @param {number} y
|
||||
* @returns {number} An incremental index associated with the added item (starting from `0`).
|
||||
*/
|
||||
add(x, y) {
|
||||
const index = this._pos >> 1;
|
||||
this.ids[index] = index;
|
||||
this.coords[this._pos++] = x;
|
||||
this.coords[this._pos++] = y;
|
||||
return index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform indexing of the added points.
|
||||
*/
|
||||
finish() {
|
||||
const numAdded = this._pos >> 1;
|
||||
if (numAdded !== this.numItems) {
|
||||
throw new Error(`Added ${numAdded} items when expected ${this.numItems}.`);
|
||||
}
|
||||
// kd-sort both arrays for efficient search
|
||||
sort(this.ids, this.coords, this.nodeSize, 0, this.numItems - 1, 0);
|
||||
|
||||
this._finished = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the index for items within a given bounding box.
|
||||
* @param {number} minX
|
||||
* @param {number} minY
|
||||
* @param {number} maxX
|
||||
* @param {number} maxY
|
||||
* @returns {number[]} An array of indices correponding to the found items.
|
||||
*/
|
||||
range(minX, minY, maxX, maxY) {
|
||||
if (!this._finished) throw new Error('Data not yet indexed - call index.finish().');
|
||||
|
||||
const {ids, coords, nodeSize} = this;
|
||||
const stack = [0, ids.length - 1, 0];
|
||||
const result = [];
|
||||
|
||||
// recursively search for items in range in the kd-sorted arrays
|
||||
while (stack.length) {
|
||||
const axis = stack.pop() || 0;
|
||||
const right = stack.pop() || 0;
|
||||
const left = stack.pop() || 0;
|
||||
|
||||
// if we reached "tree node", search linearly
|
||||
if (right - left <= nodeSize) {
|
||||
for (let i = left; i <= right; i++) {
|
||||
const x = coords[2 * i];
|
||||
const y = coords[2 * i + 1];
|
||||
if (x >= minX && x <= maxX && y >= minY && y <= maxY) result.push(ids[i]);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// otherwise find the middle index
|
||||
const m = (left + right) >> 1;
|
||||
|
||||
// include the middle item if it's in range
|
||||
const x = coords[2 * m];
|
||||
const y = coords[2 * m + 1];
|
||||
if (x >= minX && x <= maxX && y >= minY && y <= maxY) result.push(ids[m]);
|
||||
|
||||
// queue search in halves that intersect the query
|
||||
if (axis === 0 ? minX <= x : minY <= y) {
|
||||
stack.push(left);
|
||||
stack.push(m - 1);
|
||||
stack.push(1 - axis);
|
||||
}
|
||||
if (axis === 0 ? maxX >= x : maxY >= y) {
|
||||
stack.push(m + 1);
|
||||
stack.push(right);
|
||||
stack.push(1 - axis);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the index for items within a given radius.
|
||||
* @param {number} qx
|
||||
* @param {number} qy
|
||||
* @param {number} r Query radius.
|
||||
* @returns {number[]} An array of indices correponding to the found items.
|
||||
*/
|
||||
within(qx, qy, r) {
|
||||
if (!this._finished) throw new Error('Data not yet indexed - call index.finish().');
|
||||
|
||||
const {ids, coords, nodeSize} = this;
|
||||
const stack = [0, ids.length - 1, 0];
|
||||
const result = [];
|
||||
const r2 = r * r;
|
||||
|
||||
// recursively search for items within radius in the kd-sorted arrays
|
||||
while (stack.length) {
|
||||
const axis = stack.pop() || 0;
|
||||
const right = stack.pop() || 0;
|
||||
const left = stack.pop() || 0;
|
||||
|
||||
// if we reached "tree node", search linearly
|
||||
if (right - left <= nodeSize) {
|
||||
for (let i = left; i <= right; i++) {
|
||||
if (sqDist(coords[2 * i], coords[2 * i + 1], qx, qy) <= r2) result.push(ids[i]);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// otherwise find the middle index
|
||||
const m = (left + right) >> 1;
|
||||
|
||||
// include the middle item if it's in range
|
||||
const x = coords[2 * m];
|
||||
const y = coords[2 * m + 1];
|
||||
if (sqDist(x, y, qx, qy) <= r2) result.push(ids[m]);
|
||||
|
||||
// queue search in halves that intersect the query
|
||||
if (axis === 0 ? qx - r <= x : qy - r <= y) {
|
||||
stack.push(left);
|
||||
stack.push(m - 1);
|
||||
stack.push(1 - axis);
|
||||
}
|
||||
if (axis === 0 ? qx + r >= x : qy + r >= y) {
|
||||
stack.push(m + 1);
|
||||
stack.push(right);
|
||||
stack.push(1 - axis);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Uint16Array | Uint32Array} ids
|
||||
* @param {InstanceType<TypedArrayConstructor>} coords
|
||||
* @param {number} nodeSize
|
||||
* @param {number} left
|
||||
* @param {number} right
|
||||
* @param {number} axis
|
||||
*/
|
||||
function sort(ids, coords, nodeSize, left, right, axis) {
|
||||
if (right - left <= nodeSize) return;
|
||||
|
||||
const m = (left + right) >> 1; // middle index
|
||||
|
||||
// sort ids and coords around the middle index so that the halves lie
|
||||
// either left/right or top/bottom correspondingly (taking turns)
|
||||
select(ids, coords, m, left, right, axis);
|
||||
|
||||
// recursively kd-sort first half and second half on the opposite axis
|
||||
sort(ids, coords, nodeSize, left, m - 1, 1 - axis);
|
||||
sort(ids, coords, nodeSize, m + 1, right, 1 - axis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom Floyd-Rivest selection algorithm: sort ids and coords so that
|
||||
* [left..k-1] items are smaller than k-th item (on either x or y axis)
|
||||
* @param {Uint16Array | Uint32Array} ids
|
||||
* @param {InstanceType<TypedArrayConstructor>} coords
|
||||
* @param {number} k
|
||||
* @param {number} left
|
||||
* @param {number} right
|
||||
* @param {number} axis
|
||||
*/
|
||||
function select(ids, coords, k, left, right, axis) {
|
||||
|
||||
while (right > left) {
|
||||
if (right - left > 600) {
|
||||
const n = right - left + 1;
|
||||
const m = k - left + 1;
|
||||
const z = Math.log(n);
|
||||
const s = 0.5 * Math.exp(2 * z / 3);
|
||||
const sd = 0.5 * Math.sqrt(z * s * (n - s) / n) * (m - n / 2 < 0 ? -1 : 1);
|
||||
const newLeft = Math.max(left, Math.floor(k - m * s / n + sd));
|
||||
const newRight = Math.min(right, Math.floor(k + (n - m) * s / n + sd));
|
||||
select(ids, coords, k, newLeft, newRight, axis);
|
||||
}
|
||||
|
||||
const t = coords[2 * k + axis];
|
||||
let i = left;
|
||||
let j = right;
|
||||
|
||||
swapItem(ids, coords, left, k);
|
||||
if (coords[2 * right + axis] > t) swapItem(ids, coords, left, right);
|
||||
|
||||
while (i < j) {
|
||||
swapItem(ids, coords, i, j);
|
||||
i++;
|
||||
j--;
|
||||
while (coords[2 * i + axis] < t) i++;
|
||||
while (coords[2 * j + axis] > t) j--;
|
||||
}
|
||||
|
||||
if (coords[2 * left + axis] === t) swapItem(ids, coords, left, j);
|
||||
else {
|
||||
j++;
|
||||
swapItem(ids, coords, j, right);
|
||||
}
|
||||
|
||||
if (j <= k) left = j + 1;
|
||||
if (k <= j) right = j - 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Uint16Array | Uint32Array} ids
|
||||
* @param {InstanceType<TypedArrayConstructor>} coords
|
||||
* @param {number} i
|
||||
* @param {number} j
|
||||
*/
|
||||
function swapItem(ids, coords, i, j) {
|
||||
swap(ids, i, j);
|
||||
swap(coords, 2 * i, 2 * j);
|
||||
swap(coords, 2 * i + 1, 2 * j + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {InstanceType<TypedArrayConstructor>} arr
|
||||
* @param {number} i
|
||||
* @param {number} j
|
||||
*/
|
||||
function swap(arr, i, j) {
|
||||
const tmp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} ax
|
||||
* @param {number} ay
|
||||
* @param {number} bx
|
||||
* @param {number} by
|
||||
*/
|
||||
function sqDist(ax, ay, bx, by) {
|
||||
const dx = ax - bx;
|
||||
const dy = ay - by;
|
||||
return dx * dx + dy * dy;
|
||||
}
|
||||
|
||||
return KDBush;
|
||||
|
||||
}));
|
||||
1
node_modules/kdbush/kdbush.min.js
generated
vendored
Normal file
1
node_modules/kdbush/kdbush.min.js
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(t="undefined"!=typeof globalThis?globalThis:t||self).KDBush=r()}(this,(function(){"use strict";const t=[Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array];class r{static from(n){if(!(n instanceof ArrayBuffer))throw new Error("Data must be an instance of ArrayBuffer.");const[s,e]=new Uint8Array(n,0,2);if(219!==s)throw new Error("Data does not appear to be in a KDBush format.");const i=e>>4;if(1!==i)throw new Error(`Got v${i} data when expected v1.`);const o=t[15&e];if(!o)throw new Error("Unrecognized array type.");const[h]=new Uint16Array(n,2,1),[a]=new Uint32Array(n,4,1);return new r(a,h,o,n)}constructor(r,n=64,s=Float64Array,e){if(isNaN(r)||r<0)throw new Error(`Unpexpected numItems value: ${r}.`);this.numItems=+r,this.nodeSize=Math.min(Math.max(+n,2),65535),this.ArrayType=s,this.IndexArrayType=r<65536?Uint16Array:Uint32Array;const i=t.indexOf(this.ArrayType),o=2*r*this.ArrayType.BYTES_PER_ELEMENT,h=r*this.IndexArrayType.BYTES_PER_ELEMENT,a=(8-h%8)%8;if(i<0)throw new Error(`Unexpected typed array class: ${s}.`);e&&e instanceof ArrayBuffer?(this.data=e,this.ids=new this.IndexArrayType(this.data,8,r),this.coords=new this.ArrayType(this.data,8+h+a,2*r),this._pos=2*r,this._finished=!0):(this.data=new ArrayBuffer(8+o+h+a),this.ids=new this.IndexArrayType(this.data,8,r),this.coords=new this.ArrayType(this.data,8+h+a,2*r),this._pos=0,this._finished=!1,new Uint8Array(this.data,0,2).set([219,16+i]),new Uint16Array(this.data,2,1)[0]=n,new Uint32Array(this.data,4,1)[0]=r)}add(t,r){const n=this._pos>>1;return this.ids[n]=n,this.coords[this._pos++]=t,this.coords[this._pos++]=r,n}finish(){const t=this._pos>>1;if(t!==this.numItems)throw new Error(`Added ${t} items when expected ${this.numItems}.`);return n(this.ids,this.coords,this.nodeSize,0,this.numItems-1,0),this._finished=!0,this}range(t,r,n,s){if(!this._finished)throw new Error("Data not yet indexed - call index.finish().");const{ids:e,coords:i,nodeSize:o}=this,h=[0,e.length-1,0],a=[];for(;h.length;){const d=h.pop()||0,f=h.pop()||0,p=h.pop()||0;if(f-p<=o){for(let o=p;o<=f;o++){const h=i[2*o],d=i[2*o+1];h>=t&&h<=n&&d>=r&&d<=s&&a.push(e[o])}continue}const c=p+f>>1,u=i[2*c],y=i[2*c+1];u>=t&&u<=n&&y>=r&&y<=s&&a.push(e[c]),(0===d?t<=u:r<=y)&&(h.push(p),h.push(c-1),h.push(1-d)),(0===d?n>=u:s>=y)&&(h.push(c+1),h.push(f),h.push(1-d))}return a}within(t,r,n){if(!this._finished)throw new Error("Data not yet indexed - call index.finish().");const{ids:s,coords:e,nodeSize:i}=this,h=[0,s.length-1,0],a=[],d=n*n;for(;h.length;){const f=h.pop()||0,p=h.pop()||0,c=h.pop()||0;if(p-c<=i){for(let n=c;n<=p;n++)o(e[2*n],e[2*n+1],t,r)<=d&&a.push(s[n]);continue}const u=c+p>>1,y=e[2*u],w=e[2*u+1];o(y,w,t,r)<=d&&a.push(s[u]),(0===f?t-n<=y:r-n<=w)&&(h.push(c),h.push(u-1),h.push(1-f)),(0===f?t+n>=y:r+n>=w)&&(h.push(u+1),h.push(p),h.push(1-f))}return a}}function n(t,r,e,i,o,h){if(o-i<=e)return;const a=i+o>>1;s(t,r,a,i,o,h),n(t,r,e,i,a-1,1-h),n(t,r,e,a+1,o,1-h)}function s(t,r,n,i,o,h){for(;o>i;){if(o-i>600){const e=o-i+1,a=n-i+1,d=Math.log(e),f=.5*Math.exp(2*d/3),p=.5*Math.sqrt(d*f*(e-f)/e)*(a-e/2<0?-1:1);s(t,r,n,Math.max(i,Math.floor(n-a*f/e+p)),Math.min(o,Math.floor(n+(e-a)*f/e+p)),h)}const a=r[2*n+h];let d=i,f=o;for(e(t,r,i,n),r[2*o+h]>a&&e(t,r,i,o);d<f;){for(e(t,r,d,f),d++,f--;r[2*d+h]<a;)d++;for(;r[2*f+h]>a;)f--}r[2*i+h]===a?e(t,r,i,f):(f++,e(t,r,f,o)),f<=n&&(i=f+1),n<=f&&(o=f-1)}}function e(t,r,n,s){i(t,n,s),i(r,2*n,2*s),i(r,2*n+1,2*s+1)}function i(t,r,n){const s=t[r];t[r]=t[n],t[n]=s}function o(t,r,n,s){const e=t-n,i=r-s;return e*e+i*i}return r}));
|
||||
49
node_modules/kdbush/package.json
generated
vendored
Normal file
49
node_modules/kdbush/package.json
generated
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
{
|
||||
"name": "kdbush",
|
||||
"version": "4.0.2",
|
||||
"description": "A very fast static 2D index for points based on kd-tree.",
|
||||
"type": "module",
|
||||
"main": "kdbush.js",
|
||||
"module": "index.js",
|
||||
"exports": "./index.js",
|
||||
"types": "index.d.ts",
|
||||
"sideEffects": false,
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/mourner/kdbush.git"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-terser": "^0.4.1",
|
||||
"eslint": "^8.38.0",
|
||||
"eslint-config-mourner": "^3.0.0",
|
||||
"rollup": "^3.20.6",
|
||||
"typescript": "^5.0.4"
|
||||
},
|
||||
"scripts": {
|
||||
"pretest": "eslint index.js test.js bench.js rollup.config.js",
|
||||
"test": "tsc && node test.js",
|
||||
"bench": "node bench.js",
|
||||
"build": "rollup -c",
|
||||
"prepublishOnly": "npm run test && npm run build"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"extends": "mourner"
|
||||
},
|
||||
"keywords": [
|
||||
"index",
|
||||
"points",
|
||||
"kd-tree",
|
||||
"data structures",
|
||||
"algorithms",
|
||||
"spatial",
|
||||
"geometry"
|
||||
],
|
||||
"files": [
|
||||
"kdbush.js",
|
||||
"kdbush.min.js",
|
||||
"index.js",
|
||||
"index.d.ts"
|
||||
],
|
||||
"author": "Vladimir Agafonkin",
|
||||
"license": "ISC"
|
||||
}
|
||||
Reference in New Issue
Block a user