#kdtree #nearest-neighbor #neighbor #nearest #kd

fast-kd

Rust 中用于快速地理空间索引和最近邻查找的多维树

1 个不稳定版本

0.1.1 2022年3月23日
0.1.0 2022年3月23日

#4#kd

MIT/Apache

19KB
431 行(不包括注释)

kiddo

多维树库(桶点区域实现)。kdtree 的分支。重构以使用 const 泛型,并有一些性能改进和额外功能。感谢mrhooray为其原始 kdtree 库,kiddo 依赖于它。

适用于对天文和地理空间数据集进行最近邻查询。

安装

kiddo 添加到 Cargo.toml

[dependencies]
kiddo = "0.2.1"

用法

use kiddo::KdTree;
use kiddo::ErrorKind;
use kiddo::distance::squared_euclidean;

let a: ([f64; 2], usize) = ([0f64, 0f64], 0);
let b: ([f64; 2], usize) = ([1f64, 1f64], 1);
let c: ([f64; 2], usize) = ([2f64, 2f64], 2);
let d: ([f64; 2], usize) = ([3f64, 3f64], 3);

let mut kdtree = KdTree::new();

kdtree.add(&a.0, a.1)?;
kdtree.add(&b.0, b.1)?;
kdtree.add(&c.0, c.1)?;
kdtree.add(&d.0, d.1)?;

assert_eq!(kdtree.size(), 4);
assert_eq!(
    kdtree.nearest(&a.0, 0, &squared_euclidean)?,
    vec![]
);
assert_eq!(
    kdtree.nearest(&a.0, 1, &squared_euclidean)?,
    vec![(0f64, &0)]
);
assert_eq!(
    kdtree.nearest(&a.0, 2, &squared_euclidean)?,
    vec![(0f64, &0), (2f64, &1)]
);
assert_eq!(
    kdtree.nearest(&a.0, 3, &squared_euclidean)?,
    vec![(0f64, &0), (2f64, &1), (8f64, &2)]
);
assert_eq!(
    kdtree.nearest(&a.0, 4, &squared_euclidean)?,
    vec![(0f64, &0), (2f64, &1), (8f64, &2), (18f64, &3)]
);
assert_eq!(
    kdtree.nearest(&a.0, 5, &squared_euclidean)?,
    vec![(0f64, &0), (2f64, &1), (8f64, &2), (18f64, &3)]
);
assert_eq!(
    kdtree.nearest(&b.0, 4, &squared_euclidean)?,
    vec![(0f64, &1), (2f64, &0), (2f64, &2), (8f64, &3)]
);

依赖项

~180–415KB