Crates.io | spatial_hash_3d |
lib.rs | spatial_hash_3d |
version | 0.1.4 |
source | src |
created_at | 2023-02-23 21:21:05.842297 |
updated_at | 2023-11-24 10:05:54.959993 |
description | 3D spatial hash grid implementation optimized for speed. What it does/why you'd want one: https://www.youtube.com/watch?v=sx4IIQL0x7c |
homepage | |
repository | https://github.com/alexpyattaev/spatialhash |
max_upload_size | |
id | 793061 |
size | 56,022 |
Generic spatial hash grid in Rust.
Allows to efficiently translate spatial requests into cells with content. The implementation does not care what is stored in the cells, as long as it is Sized. All storage is in one block of memory for best possible cache locality.
use spatial_hash_3d::SpatialHashGrid;
use cgmath::Vector3;
// create spatial hash grid of size 5x10x20 filled with None.
let mut sh: SpatialHashGrid<Option<u64>> = SpatialHashGrid::new(5, 10, 20, ||{None});
// fill the cube at coordinates (1,2,3) with content 42
sh[Vector3::new(1, 2, 3)] = Some(42);
// iterate by mutable reference over all filled boxes within a given bounding volume
for (c, _idx, elem) in sh.iter_cubes_mut(Vector3::new(1, 2, 3), Vector3::new(4, 5, 4)) {
match elem{
Some(e)=> *e += 1,
None=>{}
}
}
// retrieve coordinates and references to contents of all filled boxes within a given volume
for (c,elem) in sh.iter_cubes(Vector3::new(1, 2, 3), Vector3::new(4, 5, 4)).filter_map(|(c, e)| Some((c,e.as_ref()?))) {
println!("{c:?} {elem}");
}
When creating spatial hash, you can pass any closure, so you can have different content in cells at creation.
# use spatial_hash_3d::SpatialHashGrid;
# use cgmath::Vector3;
// create spatial hash grid of size 5x10x20 filled with different numbers.
let mut cnt:u64 = 42;
let mut sh: SpatialHashGrid<u64> = SpatialHashGrid::new(5, 10, 20, ||{cnt +=1; cnt});
This was optimized for speed and not memory efficiency.
Iterators are provided to access content within a given axis-aligned volume. All iterators have complexity proportional to volume they select from, i.e. the larger the volume the longer the iterator will run.
When iterating it may be useful to "cache" references into the spatial hash for further use. For this purpose a flat u64 index is provided (which can be returned by any iterator or obtained from valid coordinates).
# use spatial_hash_3d::SpatialHashGrid;
# use cgmath::Vector3;
# let mut sh: SpatialHashGrid<u64> = SpatialHashGrid::new(5, 10, 20, ||{0});
// construct iterator that returns indices
let cubes_iter = sh.iter_cubes(Vector3::new(1, 2, 3), Vector3::new(4, 5, 4)).with_index();
// vec to hold interesting cube references
let mut interesting = vec![];
// go over the iterator saving references
for (c,idx, elem) in cubes_iter {
println!("{c:?} {idx} {elem}");
if *elem == 42{
interesting.push(idx);
}
}
for i in interesting {
let v = sh[i];
println!("Selected {v} @ idx {i}");
}
This is typically safer than holding references directly into the storage, and will also keep borrow checker happy, and your game free from segfaults.