Struct comfy::spatial_hash::SpatialHash
source · pub struct SpatialHash {
pub grid_size: f32,
pub inner: HashMap<(i32, i32), Vec<SpatialHashData>, BuildHasherDefault<FxHasher>>,
}
Fields§
§grid_size: f32
§inner: HashMap<(i32, i32), Vec<SpatialHashData>, BuildHasherDefault<FxHasher>>
Implementations§
source§impl SpatialHash
impl SpatialHash
sourcepub fn new() -> SpatialHash
pub fn new() -> SpatialHash
Examples found in repository?
examples/spatial_benchmark.rs (line 5)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
fn main() {
let mut spatial = SpatialHash::new();
loop {
let now = Instant::now();
// insert 1000 entities
for _ in 0..1000 {
// random vec
let vec = vec2(random() * 20.0, random() * 20.0);
spatial.add_shape(
Shape::Circle(CircleShape {
center: vec,
radius: random() * 2.0,
}),
UserData { entity_type: 0, entity: None },
);
}
// total
let mut total = 0;
for cell in spatial.inner.iter() {
total += cell.1.len();
}
let mut count = 0;
// query 1000 times
for _ in 0..1000 {
let vec = vec2(random() * 20.0, random() * 20.0);
let result = spatial.query(SpatialQuery::ShapeQuery(
Shape::Circle(CircleShape { center: vec, radius: 2.0 }),
));
count += result.count(); // assuming result is a Vec or similar collection
}
println!(
"Count: {} ... {}us ... cells: {} ... average per cell: {}",
count,
now.elapsed().as_micros(),
spatial.inner.len(),
total / usize::max(spatial.inner.len(), 1)
);
spatial.clear();
}
}
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Examples found in repository?
examples/spatial_benchmark.rs (line 53)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
fn main() {
let mut spatial = SpatialHash::new();
loop {
let now = Instant::now();
// insert 1000 entities
for _ in 0..1000 {
// random vec
let vec = vec2(random() * 20.0, random() * 20.0);
spatial.add_shape(
Shape::Circle(CircleShape {
center: vec,
radius: random() * 2.0,
}),
UserData { entity_type: 0, entity: None },
);
}
// total
let mut total = 0;
for cell in spatial.inner.iter() {
total += cell.1.len();
}
let mut count = 0;
// query 1000 times
for _ in 0..1000 {
let vec = vec2(random() * 20.0, random() * 20.0);
let result = spatial.query(SpatialQuery::ShapeQuery(
Shape::Circle(CircleShape { center: vec, radius: 2.0 }),
));
count += result.count(); // assuming result is a Vec or similar collection
}
println!(
"Count: {} ... {}us ... cells: {} ... average per cell: {}",
count,
now.elapsed().as_micros(),
spatial.inner.len(),
total / usize::max(spatial.inner.len(), 1)
);
spatial.clear();
}
}
sourcepub fn add_shape(&mut self, shape: Shape, data: UserData)
pub fn add_shape(&mut self, shape: Shape, data: UserData)
Examples found in repository?
examples/spatial_benchmark.rs (lines 15-21)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
fn main() {
let mut spatial = SpatialHash::new();
loop {
let now = Instant::now();
// insert 1000 entities
for _ in 0..1000 {
// random vec
let vec = vec2(random() * 20.0, random() * 20.0);
spatial.add_shape(
Shape::Circle(CircleShape {
center: vec,
radius: random() * 2.0,
}),
UserData { entity_type: 0, entity: None },
);
}
// total
let mut total = 0;
for cell in spatial.inner.iter() {
total += cell.1.len();
}
let mut count = 0;
// query 1000 times
for _ in 0..1000 {
let vec = vec2(random() * 20.0, random() * 20.0);
let result = spatial.query(SpatialQuery::ShapeQuery(
Shape::Circle(CircleShape { center: vec, radius: 2.0 }),
));
count += result.count(); // assuming result is a Vec or similar collection
}
println!(
"Count: {} ... {}us ... cells: {} ... average per cell: {}",
count,
now.elapsed().as_micros(),
spatial.inner.len(),
total / usize::max(spatial.inner.len(), 1)
);
spatial.clear();
}
}
sourcepub fn query(&self, query: SpatialQuery) -> impl Iterator<Item = &UserData>
pub fn query(&self, query: SpatialQuery) -> impl Iterator<Item = &UserData>
Examples found in repository?
examples/spatial_benchmark.rs (lines 37-39)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
fn main() {
let mut spatial = SpatialHash::new();
loop {
let now = Instant::now();
// insert 1000 entities
for _ in 0..1000 {
// random vec
let vec = vec2(random() * 20.0, random() * 20.0);
spatial.add_shape(
Shape::Circle(CircleShape {
center: vec,
radius: random() * 2.0,
}),
UserData { entity_type: 0, entity: None },
);
}
// total
let mut total = 0;
for cell in spatial.inner.iter() {
total += cell.1.len();
}
let mut count = 0;
// query 1000 times
for _ in 0..1000 {
let vec = vec2(random() * 20.0, random() * 20.0);
let result = spatial.query(SpatialQuery::ShapeQuery(
Shape::Circle(CircleShape { center: vec, radius: 2.0 }),
));
count += result.count(); // assuming result is a Vec or similar collection
}
println!(
"Count: {} ... {}us ... cells: {} ... average per cell: {}",
count,
now.elapsed().as_micros(),
spatial.inner.len(),
total / usize::max(spatial.inner.len(), 1)
);
spatial.clear();
}
}
pub fn raycast( &self, start: Vec2, end: Vec2 ) -> Option<(Intersection, &UserData)>
Auto Trait Implementations§
impl Freeze for SpatialHash
impl RefUnwindSafe for SpatialHash
impl Send for SpatialHash
impl Sync for SpatialHash
impl Unpin for SpatialHash
impl UnwindSafe for SpatialHash
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more