1个不稳定发布
0.3.0 | 2024年5月16日 |
---|
#595 在 数据结构
53 每月下载次数
在 5 个 5 crate(4直接)中使用
59KB
1.5K SLoC
F3l 搜索树
搜索树实现。需要实现输入数据的 Into<[T; D]>
树
- kd树
- oc树
树搜索参数
搜索方法
- 计数:KNN
- 半径:半径搜索
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub enum SearchBy {
Count(usize),
Radius(f32),
}
数据
- 数组
pub fn test_insert_data_array() {
let mut tree = KdTree::<f32, 3>::new();
tree.set_data(&vec![[1f32, 2f32, 3f32], [4f32, 5f32, 6f32]]);
let mut d = 1.0f32;
tree.data.iter().for_each(|element| {
element.iter().for_each(|e| {
assert_relative_eq!(d, e);
d += 1f32;
})
});
}
- Glam
pub fn test_insert_data_glam() {
let mut tree = KdTree::<f32, 3>::new();
tree.set_data(&vec![Vec3::new(1.0, 2.0, 3.0), Vec3::new(4.0, 5.0, 6.0)]);
let mut d = 1.0f32;
tree.data.iter().for_each(|element| {
element.iter().for_each(|e| {
assert_relative_eq!(d, e);
d += 1f32;
})
});
}
- Nalgebra
pub fn test_insert_data_nalgebra() {
let mut tree = KdTree::<f32, 3>::new();
tree.set_data(&vec![
Point3::<f32>::new(1.0, 2.0, 3.0),
Point3::<f32>::new(4.0, 5.0, 6.0),
]);
let mut d = 1.0f32;
tree.data.iter().for_each(|element| {
element.iter().for_each(|e| {
assert_relative_eq!(d, e);
d += 1f32;
})
});
}
- 自定义
#[derive(Debug, Clone, Copy)]
struct MyStruct {
x: f32,
y: f32,
z: f32,
}
impl From<[f32; 3]> for MyStruct {
fn from(value: [f32; 3]) -> Self {
MyStruct {
x: value[0],
y: value[1],
z: value[2],
}
}
}
impl From<MyStruct> for [f32; 3] {
fn from(value: MyStruct) -> Self {
[value.x, value.y, value.z]
}
}
#[test]
pub fn test_insert_data_custom() {
let mut tree = KdTree::<f32, MyStruct>::new();
tree.set_data(&vec![
MyStruct {
x: 1.0f32,
y: 2.0f32,
z: 3.0f32,
},
MyStruct {
x: 4.0f32,
y: 5.0f32,
z: 6.0f32,
},
]);
let mut d = 1.0f32;
tree.data.iter().for_each(|element| {
element.iter().for_each(|e| {
assert_relative_eq!(d, e);
d += 1f32;
})
});
}
搜索
返回数据或实例的索引。
pub trait TreeSearch<P> {
fn search_knn_ids(&self, point: &P, k: usize) -> Vec<usize>;
fn search_radius_ids(&self, point: &P, radius: f32) -> Vec<usize>;
fn search_knn(&self, point: &P, k: usize) -> Vec<(P, f32)>;
fn search_radius(&self, point: &P, radius: f32) -> Vec<P>;
}
结果
- KNN
#[derive(Debug, Clone)]
pub struct TreeKnnResult {
/// KNN ids and distances.
pub data: Vec<(usize, f32)>,
/// Target of `K`.
pub size: usize,
/// Length of data.
pub count: usize,
/// Used in searching.
pub farthest: f32,
}
- 半径
#[derive(Debug, Clone)]
pub struct TreeRadiusResult {
/// Neighbors in radius.
pub data: Vec<usize>,
/// Length of data
pub count: usize,
/// Target radius
pub radius: f32,
/// `Optional`: full check when `count` more than `size`
pub size: Option<usize>,
}
依赖项
~0–6.5MB
~124K SLoC