Struct kiddo::kiddo::KdTree[][src]

pub struct KdTree<A, T: PartialEq, const K: usize> { /* fields omitted */ }

Implementations

Creates a new KdTree with default capacity per node of 16.

Examples
use kiddo::KdTree;

let mut tree: KdTree<f64, usize, 3> = KdTree::new();

tree.add(&[1.0, 2.0, 5.0], 100)?;

Creates a new KdTree with a specific capacity per node. You may wish to experiment by tuning this value to best suit your workload via benchmarking: values between 10 and 40 often work best.

Examples
use kiddo::KdTree;

let mut tree: KdTree<f64, usize, 3> = KdTree::with_per_node_capacity(30)?;

tree.add(&[1.0, 2.0, 5.0], 100)?;
👎 Deprecated since 0.1.8:

with_capacity has a misleading name. Users should instead use with_per_node_capacity. with_capacity will be removed in a future release

Creates a new KdTree with a specific capacity per node.

Returns the current number of elements stored in the tree

Examples
use kiddo::KdTree;

let mut tree: KdTree<f64, usize, 3> = KdTree::new();

tree.add(&[1.0, 2.0, 5.0], 100)?;
tree.add(&[1.1, 2.1, 5.1], 101)?;

assert_eq!(tree.size(), 2);

Returns true if the node is a leaf node

Examples
use kiddo::KdTree;

let mut tree_1: KdTree<f64, usize, 3> = KdTree::with_capacity(2)?;

tree_1.add(&[1.0, 2.0, 5.0], 100)?;
tree_1.add(&[1.1, 2.1, 5.1], 101)?;

assert_eq!(tree_1.is_leaf(), true);

let mut tree_2: KdTree<f64, usize, 3> = KdTree::with_capacity(1)?;

tree_2.add(&[1.0, 2.0, 5.0], 100)?;
tree_2.add(&[1.1, 2.1, 5.1], 101)?;

assert_eq!(tree_2.is_leaf(), false);

Queries the tree to find the nearest num elements to point, using the specified distance metric function.

Examples
use kiddo::KdTree;
use kiddo::distance::squared_euclidean;

let mut tree: KdTree<f64, usize, 3> = KdTree::new();

tree.add(&[1.0, 2.0, 5.0], 100)?;
tree.add(&[2.0, 3.0, 6.0], 101)?;

let nearest = tree.nearest(&[1.0, 2.0, 5.1], 1, &squared_euclidean)?;

assert_eq!(nearest.len(), 1);
assert!((nearest[0].0 - 0.01f64).abs() < f64::EPSILON);
assert_eq!(*nearest[0].1, 100);

Queries the tree to find the nearest element to point, using the specified distance metric function. Faster than querying for nearest(point, 1, …) due to not needing to allocate a Vec for the result

Examples
use kiddo::KdTree;
use kiddo::distance::squared_euclidean;

let mut tree: KdTree<f64, usize, 3> = KdTree::new();

tree.add(&[1.0, 2.0, 5.0], 100)?;
tree.add(&[2.0, 3.0, 6.0], 101)?;

let nearest = tree.nearest_one(&[1.0, 2.0, 5.1], &squared_euclidean)?;

assert!((nearest.0 - 0.01f64).abs() < f64::EPSILON);
assert_eq!(*nearest.1, 100);

Queries the tree to find all elements within radius of point, using the specified distance metric function. Results are returned sorted nearest-first

Examples
use kiddo::KdTree;
use kiddo::distance::squared_euclidean;

let mut tree: KdTree<f64, usize, 3> = KdTree::new();

tree.add(&[1.0, 2.0, 5.0], 100)?;
tree.add(&[2.0, 3.0, 6.0], 101)?;
tree.add(&[200.0, 300.0, 600.0], 102)?;

let within = tree.within(&[1.0, 2.0, 5.0], 10f64, &squared_euclidean)?;

assert_eq!(within.len(), 2);

Queries the tree to find all elements within radius of point, using the specified distance metric function. Results are returned in arbitrary order. Faster than within()

Examples
use kiddo::KdTree;
use kiddo::distance::squared_euclidean;

let mut tree: KdTree<f64, usize, 3> = KdTree::new();

tree.add(&[1.0, 2.0, 5.0], 100)?;
tree.add(&[2.0, 3.0, 6.0], 101)?;
tree.add(&[200.0, 300.0, 600.0], 102)?;

let within = tree.within(&[1.0, 2.0, 5.0], 10f64, &squared_euclidean)?;

assert_eq!(within.len(), 2);

Queries the tree to find the best n elements within radius of point, using the specified distance metric function. Results are returned in arbitrary order. ‘Best’ is determined by performing a comparison of the elements using < (ie, std::ord::lt)

Examples
use kiddo::KdTree;
use kiddo::distance::squared_euclidean;

let mut tree: KdTree<f64, usize, 3> = KdTree::new();

tree.add(&[1.0, 2.0, 5.0], 100)?;
tree.add(&[2.0, 3.0, 6.0], 1)?;
tree.add(&[200.0, 300.0, 600.0], 102)?;

let best_n_within = tree.best_n_within(&[1.0, 2.0, 5.0], 10f64, 1, &squared_euclidean)?;

assert_eq!(best_n_within[0], 1);

Queries the tree to find the best n elements within radius of point, using the specified distance metric function. Results are returned in arbitrary order. ‘Best’ is determined by performing a comparison of the elements using < (ie, std::ord::lt). Returns an iterator.

Examples
use kiddo::KdTree;
use kiddo::distance::squared_euclidean;

let mut tree: KdTree<f64, usize, 3> = KdTree::new();

tree.add(&[1.0, 2.0, 5.0], 100)?;
tree.add(&[2.0, 3.0, 6.0], 1)?;
tree.add(&[200.0, 300.0, 600.0], 102)?;

let mut best_n_within_iter = tree.best_n_within_into_iter(&[1.0, 2.0, 5.0], 10f64, 1, &squared_euclidean);
let first = best_n_within_iter.next().unwrap();

assert_eq!(first, 1);

Returns an iterator over all elements in the tree, sorted nearest-first to the query point.

Examples
use kiddo::KdTree;
use kiddo::distance::squared_euclidean;

let mut tree: KdTree<f64, usize, 3> = KdTree::new();

tree.add(&[1.0, 2.0, 5.0], 100)?;
tree.add(&[2.0, 3.0, 6.0], 101)?;

let mut nearest_iter = tree.iter_nearest(&[1.0, 2.0, 5.1], &squared_euclidean)?;

let nearest_first = nearest_iter.next().unwrap();

assert!((nearest_first.0 - 0.01f64).abs() < f64::EPSILON);
assert_eq!(*nearest_first.1, 100);

Add an element to the tree. The first argument specifies the location in kd space at which the element is located. The second argument is the data associated with that point in space.

Examples
use kiddo::KdTree;

let mut tree: KdTree<f64, usize, 3> = KdTree::new();

tree.add(&[1.0, 2.0, 5.0], 100)?;
tree.add(&[1.1, 2.1, 5.1], 101)?;

assert_eq!(tree.size(), 2);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into #41263)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.