Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ repository = "https://github.com/benbaarber/quadtree"
keywords = ["quadtree", "barnes-hut"]

[features]
default = ["std"]

serde = ["dep:serde", "glam/serde"]
std = ["glam/std"]
libm = ["glam/libm"]

[dependencies]
glam = "0.30.4"
glam = { version = "0.30.4", default-features = false, features = ["nostd-libm"] }
serde = { version = "1.0.219", optional = true, features = ["derive"] }

[dev-dependencies]
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![no_std]

mod quadtree;
pub mod shapes;
mod util;
Expand Down
5 changes: 4 additions & 1 deletion src/quadtree/barnes_hut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use crate::{
util::{Partition, bound_items},
};
use glam::Vec2;
use std::ops::Range;

extern crate alloc;
use alloc::vec::Vec;
use core::ops::Range;

/// A point with mass
#[derive(Debug, Default, Clone, Copy, PartialEq)]
Expand Down
34 changes: 33 additions & 1 deletion src/quadtree/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
pub mod barnes_hut;

use glam::Vec2;

extern crate alloc;
use alloc::{boxed::Box, vec, vec::Vec};

#[cfg(feature = "serde")]
use serde::{Serialize, Serializer, ser::SerializeSeq};

Expand Down Expand Up @@ -112,6 +116,11 @@ impl<T: Point + Clone> Quadtree<T> {
results
}

/// Shrink to fit inserted items
pub fn shrink(&mut self) {
self.root = self.root.shrink();
}

/// Delete items that are within a specified shape area
///
/// **Returns** the number of items that were deleted
Expand Down Expand Up @@ -229,7 +238,7 @@ impl<T: Point + Clone> Node<T> {
return true;
}

let mut data = std::mem::take(data);
let mut data = core::mem::take(data);
data.push(item.clone());
let children = self.subdivide();
*self = Self::Internal { bound, children };
Expand Down Expand Up @@ -487,6 +496,29 @@ impl<T: Point + Clone> Node<T> {
}
}

fn shrink(&mut self) -> Self {
let default: Self = Node::Empty {
bound: Rect::new(Vec2::default(), Vec2::default()),
};

match self {
Self::Empty { .. } => core::mem::replace(self, default),
Self::External { .. } => core::mem::replace(self, default),
Self::Internal { children, .. } => {
let mut nonempty: Vec<&mut Box<Self>> = children
.into_iter()
.filter(|e| !matches!(***e, Node::Empty { .. }))
.collect();

if nonempty.len() == 1 {
nonempty[0].shrink()
} else {
core::mem::replace(self, default)
}
}
}
}

fn center(&self) -> Vec2 {
self.bound().center()
}
Expand Down
7 changes: 5 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use glam::vec2;

extern crate alloc;
use alloc::vec::Vec;

use crate::{
Point,
shapes::{Rect, Shape},
Expand Down Expand Up @@ -56,7 +59,7 @@ impl<T> Partition<T> for [T] {
}

pub(crate) fn group_by_quadrant<T: Point>(rect: Rect, items: Vec<T>) -> [Vec<T>; 5] {
let mut groups: [Vec<T>; 5] = std::array::from_fn(|_| Vec::with_capacity(items.len()));
let mut groups: [Vec<T>; 5] = core::array::from_fn(|_| Vec::with_capacity(items.len()));
for item in items {
match rect.quadrant(item.point()) {
Some(q) => groups[q].push(item),
Expand All @@ -68,7 +71,7 @@ pub(crate) fn group_by_quadrant<T: Point>(rect: Rect, items: Vec<T>) -> [Vec<T>;

#[allow(unused)]
pub(crate) fn group_by_quadrant_slice<'a, T: Point>(rect: Rect, items: &'a [T]) -> [Vec<&'a T>; 5] {
let mut groups: [Vec<&T>; 5] = std::array::from_fn(|_| Vec::with_capacity(items.len()));
let mut groups: [Vec<&T>; 5] = core::array::from_fn(|_| Vec::with_capacity(items.len()));
for item in items {
match rect.quadrant(item.point()) {
Some(q) => groups[q].push(item),
Expand Down