Skip to content

Class: PointIndex ​

Experimental

Static 2D spatial index over a point cloud, backed by a packed Hilbert R-tree (flatbush). Used for CPU picking of 2D traces (ADR-010): hover (nearest), box select (withinRect), lasso (withinPolygon) and brush-radius queries.

The index is unit-agnostic: all distances are Euclidean in the units of the input arrays. For hover hit-testing pass pixel-space coordinates (or data pre-scaled so both axes share a unit), otherwise anisotropic axes make "nearest" meaningless.

Non-finite points (NaN gaps, ±Infinity) are skipped and never returned; every returned index refers to the ORIGINAL position in the x/y arrays. If the arrays differ in length, the shorter length is used.

Construction is O(1): the tree is built lazily on the first query, and invalidate simply marks it dirty, so frequently-updated data only pays for a rebuild when actually queried.

Constructors ​

Constructor ​

ts
new PointIndex(
   x,
   y,
   options?
): PointIndex;

Experimental

Parameters ​

ParameterType
xArrayLike<number>
yArrayLike<number>
optionsPointIndexOptions

Returns ​

PointIndex

Accessors ​

built ​

Get Signature ​

ts
get built(): boolean;

Experimental

Whether the tree is currently built (false until the first query, and after invalidate).

Returns ​

boolean


size ​

Get Signature ​

ts
get size(): number;

Experimental

Number of valid (finite) points indexed. Triggers a build if needed.

Returns ​

number

Methods ​

invalidate() ​

ts
invalidate(x?, y?): void;

Experimental

Mark the index stale, optionally swapping in new coordinate arrays. The rebuild happens lazily on the next query. Call this after mutating the arrays in place, too.

Parameters ​

ParameterType
x?ArrayLike<number>
y?ArrayLike<number>

Returns ​

void


nearest() ​

ts
nearest(
   x,
   y,
   maxDistance?
): number;

Experimental

Original index of the point closest to (x, y) within maxDistance, or -1 if none.

Parameters ​

ParameterTypeDefault value
xnumberundefined
ynumberundefined
maxDistancenumberInfinity

Returns ​

number


nearestK() ​

ts
nearestK(
   x,
   y,
   k,
   maxDistance?
): number[];

Experimental

Original indices of up to k points closest to (x, y), sorted by increasing distance.

Parameters ​

ParameterTypeDefault value
xnumberundefined
ynumberundefined
knumberundefined
maxDistancenumberInfinity

Returns ​

number[]


withinPolygon() ​

ts
withinPolygon(polygon): number[];

Experimental

Original indices of points inside a flat [x0, y0, x1, y1, ...] polygon (even-odd rule), ascending. The polygon's bounding box prefilters candidates through the tree; only those get the exact point-in-polygon test. Fewer than 3 vertices returns [].

Parameters ​

ParameterType
polygonArrayLike<number>

Returns ​

number[]


withinRadius() ​

ts
withinRadius(
   x,
   y,
   r
): number[];

Experimental

Original indices of points within distance r (inclusive) of (x, y), ascending.

Parameters ​

ParameterType
xnumber
ynumber
rnumber

Returns ​

number[]


withinRect() ​

ts
withinRect(
   x0,
   y0,
   x1,
   y1
): number[];

Experimental

Original indices of points inside the rectangle, ascending. Corners may be given in any order; edges are inclusive.

Parameters ​

ParameterType
x0number
y0number
x1number
y1number

Returns ​

number[]

Released under the MIT License.