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

source

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();
    }
}
source

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();
    }
}
source

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();
    }
}
source

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();
    }
}
source

pub fn raycast( &self, start: Vec2, end: Vec2 ) -> Option<(Intersection, &UserData)>

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> Downcast<T> for T

source§

fn downcast(&self) -> &T

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<S> FromSample<S> for S

source§

fn from_sample_(s: S) -> S

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

source§

fn into_sample(self) -> T

source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

source§

fn to_sample_(self) -> U

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> Upcast<T> for T

source§

fn upcast(&self) -> Option<&T>

source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> Any for T
where T: Any,

source§

impl<T> Component for T
where T: Send + Sync + 'static,

source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

source§

impl<T> WasmNotSend for T
where T: Send,

source§

impl<T> WasmNotSendSync for T

source§

impl<T> WasmNotSync for T
where T: Sync,