Enum KdNode

Source
pub enum KdNode<T: KDT> {
    Empty,
    Node {
        point: Point<T>,
        dim: Dim,
        left: Box<KdNode<T>>,
        right: Box<KdNode<T>>,
    },
}

Variants§

§

Empty

§

Node

Fields

§point: Point<T>
§dim: Dim
§left: Box<KdNode<T>>
§right: Box<KdNode<T>>

Implementations§

Source§

impl<T: KDT + Mul<Output = T> + Sub<Output = T> + Add<Output = T> + Debug> KdNode<T>

Source

pub fn new() -> Self

Create a new empty tree

Examples found in repository?
examples/insert.rs (line 8)
7fn main() {
8    let mut node: KdNode<i32> = KdNode::new();
9    assert_eq!(node, Empty);
10
11    // Tree Root
12    node.insert(1, 1);
13    node.insert(2, 2);
14    node.insert(2, -12);
15
16    println!("{:?}", node);
17    println!("{:?}", node.nearest_neighbor_x_y(1, 1, 1.0));
18    println!("{:?}", node.nearest_neighbor(Point{x: 1, y: 1}, 1.0));
19}
Source

pub fn insert(&mut self, x: T, y: T) -> &Self

Insert a new item into the tree

This should used sparingly as it can unbalance the tree and reduce performance. If there is a large change to the dataset it is better to create a new tree. This effect has not been tested though and could be totally fine in terms of performance for a large number of inserts. A good rule of thumb may be if the tree size is going to increase by more than 10% it may be better to create a new tree.

Examples found in repository?
examples/insert.rs (line 12)
7fn main() {
8    let mut node: KdNode<i32> = KdNode::new();
9    assert_eq!(node, Empty);
10
11    // Tree Root
12    node.insert(1, 1);
13    node.insert(2, 2);
14    node.insert(2, -12);
15
16    println!("{:?}", node);
17    println!("{:?}", node.nearest_neighbor_x_y(1, 1, 1.0));
18    println!("{:?}", node.nearest_neighbor(Point{x: 1, y: 1}, 1.0));
19}
Source

pub fn insert_point(&mut self, item: Point<T>) -> &Self

Insert a new item into the tree

This is the same as insert but takes a Point instead of x and y

Source

pub fn nearest_neighbor<'a>( &self, origin: Point<T>, radius: f64, ) -> Vec<Point<T>>

Find the nearest neighbors to the origin point

This will return a vector of points that are within the radius of the origin point. The radius is inclusive so if a point is exactly on the radius it will be included.

Examples found in repository?
examples/insert.rs (line 18)
7fn main() {
8    let mut node: KdNode<i32> = KdNode::new();
9    assert_eq!(node, Empty);
10
11    // Tree Root
12    node.insert(1, 1);
13    node.insert(2, 2);
14    node.insert(2, -12);
15
16    println!("{:?}", node);
17    println!("{:?}", node.nearest_neighbor_x_y(1, 1, 1.0));
18    println!("{:?}", node.nearest_neighbor(Point{x: 1, y: 1}, 1.0));
19}
More examples
Hide additional examples
examples/build.rs (line 22)
6fn main() {
7    let points: Vec<Point<i32>> = vec![
8        Point { x: 1, y: 8 },
9        Point { x: 2, y: 2 },
10        Point { x: 3, y: 6 },
11        Point { x: 4, y: 9 },
12        Point { x: 7, y: 3 },
13        Point { x: 8, y: 8 },
14        Point { x: 9, y: 1 },
15        Point { x: 9, y: 9 },
16    ];
17
18    let node: KdNode<i32> = KdNode::build(points);
19
20    let radius: f64 = 1.5;
21    let origin: Point<i32> = Point { x: 8, y: 8 };
22    let nearest = node.nearest_neighbor(origin, radius);
23    assert_eq!(
24        nearest,
25        vec![Point { x: 8, y: 8 }, Point { x: 9, y: 9 }]
26    );
27    println!("Neighbours within 1.5 units of (1,1): {:?}", nearest);
28}
Source

pub fn nearest_neighbor_x_y<'a>(&self, x: T, y: T, radius: f64) -> Vec<Point<T>>

Insert a new item into the tree

This is the same as insert but takes a Point instead of x and y

Examples found in repository?
examples/insert.rs (line 17)
7fn main() {
8    let mut node: KdNode<i32> = KdNode::new();
9    assert_eq!(node, Empty);
10
11    // Tree Root
12    node.insert(1, 1);
13    node.insert(2, 2);
14    node.insert(2, -12);
15
16    println!("{:?}", node);
17    println!("{:?}", node.nearest_neighbor_x_y(1, 1, 1.0));
18    println!("{:?}", node.nearest_neighbor(Point{x: 1, y: 1}, 1.0));
19}
Source

pub fn n_nearest_neighbor<'a>( &self, origin: Point<T>, max: usize, ) -> Vec<Point<T>>

Find the nearest neighbors to the origin point

This will return a vector of points that are within the radius of the origin point. This is the same as nearest_neighbor but will only return the max number of points.

Source

pub fn build(points: Vec<Point<T>>) -> Self

Examples found in repository?
examples/build.rs (line 18)
6fn main() {
7    let points: Vec<Point<i32>> = vec![
8        Point { x: 1, y: 8 },
9        Point { x: 2, y: 2 },
10        Point { x: 3, y: 6 },
11        Point { x: 4, y: 9 },
12        Point { x: 7, y: 3 },
13        Point { x: 8, y: 8 },
14        Point { x: 9, y: 1 },
15        Point { x: 9, y: 9 },
16    ];
17
18    let node: KdNode<i32> = KdNode::build(points);
19
20    let radius: f64 = 1.5;
21    let origin: Point<i32> = Point { x: 8, y: 8 };
22    let nearest = node.nearest_neighbor(origin, radius);
23    assert_eq!(
24        nearest,
25        vec![Point { x: 8, y: 8 }, Point { x: 9, y: 9 }]
26    );
27    println!("Neighbours within 1.5 units of (1,1): {:?}", nearest);
28}

Trait Implementations§

Source§

impl<T: Debug + KDT> Debug for KdNode<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: PartialEq + KDT> PartialEq for KdNode<T>

Source§

fn eq(&self, other: &KdNode<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: KDT> StructuralPartialEq for KdNode<T>

Auto Trait Implementations§

§

impl<T> Freeze for KdNode<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for KdNode<T>
where T: RefUnwindSafe,

§

impl<T> Send for KdNode<T>
where T: Send,

§

impl<T> Sync for KdNode<T>
where T: Sync,

§

impl<T> Unpin for KdNode<T>
where T: Unpin,

§

impl<T> UnwindSafe for KdNode<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.