Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tianyishi2001/kdtree
k-dimensional tree in Rust
https://github.com/tianyishi2001/kdtree
Last synced: about 1 month ago
JSON representation
k-dimensional tree in Rust
- Host: GitHub
- URL: https://github.com/tianyishi2001/kdtree
- Owner: TianyiShi2001
- License: mit
- Created: 2020-12-23T09:27:26.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2020-12-24T01:59:49.000Z (almost 4 years ago)
- Last Synced: 2024-09-15T02:56:34.462Z (2 months ago)
- Language: Rust
- Size: 12.7 KB
- Stars: 4
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# kdtree
k-dimensional tree data structure implemented with const generics, used for finding k-nearest neighbours (KNN).
## Example Usage
```rust
use kdt::*;
use ordered_float::OrderedFloat;
use rand::{thread_rng, Rng};fn main() {
let mut points = {
let mut rng = thread_rng();
(0..100)
.map(|_| {
Point([
rng.gen_range(-50.0..50.0),
rng.gen_range(-50.0..50.0),
rng.gen_range(-50.0..50.0),
])
})
.collect::>()
};
let kdt = KdTree::from_slice(&mut points);
let query = Point([0.0, 0.0, 0.0]);
let nearest = kdt
.k_nearest_neighbors(&query, 10)
.into_iter()
// each point is returned as a reference. In most use cases you don't need to `clone`
.map(|(dist, point)| (dist, point.clone()))
// by default results are sorted in descending order of squared Eucledian distance to the query point
.rev()
.collect::>();
// compute by brutal force
let mut expected = points
.into_iter()
.map(|p| (p.squared_eucledian(&query), p))
.collect::>();
expected.sort_unstable_by_key(|p| OrderedFloat(p.0));
assert_eq!(&nearest[..], &expected[..10]);
}```