Skip to main content

QuadTree2D

Struct QuadTree2D 

Source
pub struct QuadTree2D { /* private fields */ }
Expand description

A 2D quadtree broad-phase acceleration structure, interface-compatible with SpatialHashGrid2D.

Bodies are inserted by their world-space axis-aligned bounding box. A node subdivides into four children once its entry count exceeds the capacity and its depth is below the maximum; an entry whose box straddles a split boundary stays in the parent node, so no body is ever duplicated and every query visits each body exactly once.

Unlike the uniform grid, the quadtree concentrates resolution where bodies are dense, which makes it the better structure for scenes with strongly clustered content. A body larger than the root region is still inserted and still returned by every query overlapping it — it simply resides at the root and is confirmed by its exact box test.

Implementations§

Source§

impl QuadTree2D

Implements construction, insertion, subdivision, and querying for QuadTree2D.

Source

pub fn create( bounds_min: Vector2D, bounds_max: Vector2D, capacity: usize, max_depth: usize, ) -> QuadTree2D

Creates a new 2D quadtree rooted at the given region.

The root region is normalized so the first corner is always the minimum, and a zero-width or zero-height region is widened by EPSILON so subdivision always makes progress. A capacity below one is raised to one, and a depth above [SPATIAL_MAX_DEPTH_2D] is clamped.

§Arguments
  • Vector2D - One corner of the root region.
  • Vector2D - The opposite corner of the root region.
  • usize - The number of entries a node may hold before subdividing.
  • usize - The deepest subdivision level a node may reach.
§Returns
  • QuadTree2D - The new empty quadtree.
Source

pub fn with_default_size() -> QuadTree2D

Creates a new 2D quadtree with the default capacity, depth, and a root region centred on the world origin.

§Returns
  • QuadTree2D - The new empty quadtree.
Source

pub fn with_half_extent(half_extent: f64) -> QuadTree2D

Creates a new 2D quadtree over a square region of the given half-extent, using the default capacity and depth.

§Arguments
  • f64 - The half-extent of the root region in world units.
§Returns
  • QuadTree2D - The new empty quadtree.
Source

pub fn len(&self) -> usize

Returns the number of bodies currently stored in the tree.

§Returns
  • usize - The number of inserted bodies.
Source

pub fn is_empty(&self) -> bool

Reports whether the tree holds no bodies.

§Returns
  • bool - true when no body is stored.
Source

pub fn bounds(&self) -> (Vector2D, Vector2D)

Returns the region covered by the root node.

§Returns
  • (Vector2D, Vector2D) - The minimum and maximum corners of the root region.
Source

pub fn insert(&mut self, index: usize, min: Vector2D, max: Vector2D)

Inserts a body index together with its exact bounding box.

The entry descends into the child region that fully contains it. An entry that straddles a split boundary, or that is larger than the node it lands in, stays in the current node and marks that node loose — that is the standard quadtree invariant, and it is what guarantees a body is stored exactly once, so queries never need dedup beyond the caller’s scratch set.

§Arguments
  • usize - The body index to insert.
  • Vector2D - The minimum corner of the bounding box.
  • Vector2D - The maximum corner of the bounding box.
Source

pub fn query(&self, min: Vector2D, max: Vector2D) -> Vec<usize>

Returns all body indices whose exact bounding box overlaps the query box.

Each candidate index appears at most once.

§Arguments
  • Vector2D - The minimum corner of the query box.
  • Vector2D - The maximum corner of the query box.
§Returns
  • Vec<usize> - The list of candidate body indices.
Source

pub fn query_into( &self, min: Vector2D, max: Vector2D, out: &mut Vec<usize>, seen: &mut HashSet<usize>, )

Appends all candidate body indices overlapping the query box into out, deduplicating via the caller-provided seen set.

Both out and seen are cleared first, so the caller can reuse the same buffers across all queries in a step without any per-query allocation.

Nodes are pruned by region overlap, then every entry that survives pruning is confirmed with an exact box-vs-box test. A node holding a body larger than its own region is marked loose and skips the region prune entirely, which is what keeps such a body — including one larger than the whole root region — returned by every query that overlaps it. The traversal uses an explicit stack, so no recursion is involved.

§Arguments
  • Vector2D - The minimum corner of the query box.
  • Vector2D - The maximum corner of the query box.
  • &mut Vec<usize> - The output buffer, cleared then filled with candidates.
  • &mut HashSet<usize> - The dedup scratch set, cleared then reused.
Source

pub fn clear(&mut self)

Removes every entry and child node, keeping only the empty root so the same tree can be reused for a fresh insertion pass.

Source

pub fn boxes_overlap( a_min: Vector2D, a_max: Vector2D, b_min: Vector2D, b_max: Vector2D, ) -> bool

Reports whether two axis-aligned boxes overlap.

Touching edges count as overlapping, matching the inclusive cell semantics of SpatialHashGrid2D::query.

§Arguments
  • Vector2D - The minimum corner of the first box.
  • Vector2D - The maximum corner of the first box.
  • Vector2D - The minimum corner of the second box.
  • Vector2D - The maximum corner of the second box.
§Returns
  • bool - true when the two boxes share at least one point.
Source§

impl QuadTree2D

Source

pub fn get_nodes(&self) -> &QuadTreeNodeList2D

Source

pub fn get_mut_nodes(&mut self) -> &mut QuadTreeNodeList2D

Source

pub fn set_nodes(&mut self, val: QuadTreeNodeList2D) -> &mut Self

Source

pub fn get_capacity(&self) -> usize

Source

pub fn get_mut_capacity(&mut self) -> &mut usize

Source

pub fn set_capacity(&mut self, val: usize) -> &mut Self

Source

pub fn get_max_depth(&self) -> usize

Source

pub fn get_mut_max_depth(&mut self) -> &mut usize

Source

pub fn set_max_depth(&mut self, val: usize) -> &mut Self

Source

pub fn get_count(&self) -> usize

Source

pub fn get_mut_count(&mut self) -> &mut usize

Trait Implementations§

Source§

impl Clone for QuadTree2D

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for QuadTree2D

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for QuadTree2D

Default-construction for QuadTree2D.

Source§

fn default() -> QuadTree2D

Constructs a default QuadTree2D value.

§Returns
  • QuadTree2D - A default-constructed instance with the documented initial state.
Source§

impl PartialEq for QuadTree2D

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for QuadTree2D

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more