KDTree:
Filter:
Classes (extension) | Collections > Unordered

KDTree : Object
ExtensionExtension

kd-tree data structure for efficient spatial search
Source: KDTree.sc

Description

A kd-tree is a data structure for storing points in a Euclidean space. It is not optimised for data that changes (insertions/deletions) but is very efficient at spatial queries such as nearest-neighbour search. See http://en.wikipedia.org/wiki/Kd_tree

KDTree is initialised using an array of points - each of these points must be an array, each of the same size. If you want to store an item or label along with each point, you can add it as the last item of each point array, and set the lastIsLabel constructor argument to true (see examples below). The things that you can store as "labels" can be anything at all, but they should be things that can meaningfully be compared for equality. Numbers, symbols, strings, etc, are all fine.

(Note: You typically create a KDTree using an Array of points, but the array and the ordering of elements is not stored. The asArray method will return an array of points stored in the tree but typically in a different order.)

Class Methods

KDTree.new(array, depth: 0, parent, lastIsLabel: false, uniqueid: 1)

Create a new KDTree from an array of data. See example code below.

Inherited class methods

Instance Methods

Inherited instance methods

Undocumented instance methods

.allNearest(bestDist: inf, incExact: true)

.asArray(incLabels: false, arr)

.collect(func, incDeleted: false, arraySoFar)

.do(func, incDeleted: false)

.dumpTree(maxDepth: inf)

.find(point, incDeleted: false)

.kNearest(point, k, nearestSoFar, bestDist: inf, incExact: true)

.kNearestToNode(k, nearestSoFar, bestDist: inf, incExact: true)

.label

.leftChild

.location

.max

.min

.nearest(point, nearestSoFar, bestDist: inf, incExact: true)

.nearestToNode(nearestSoFar, bestDist: inf, incExact: true)

.parent

.radiusSearch(point, radius: 1)

.recreate

.rectSearch(lo, hi)

.rightChild

.sibling

.size(incDeleted: false)

.uniqueid

Examples

Tests

For speed tests see KDTree_benchmarking. 

For unit tests (for correctness) there is a Unit Test class TestKDTree - basically just run KDTree.test