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
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#![no_std]

mod quadtree;
pub mod shapes;
mod util;

pub use glam::{Vec2, vec2};
pub use quadtree::Quadtree;
pub use glam::{vec2, Vec2};
pub use quadtree::barnes_hut::{BHQuadtree, WeightedPoint};
pub use quadtree::Quadtree;

/// Trait for getting a 2d point position of data stored in the [`Quadtree`]
pub trait Point {
Expand Down
9 changes: 6 additions & 3 deletions src/quadtree/barnes_hut.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::{
Point,
shapes::Rect,
util::{Partition, bound_items},
util::{bound_items, Partition},
Point,
};
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
12 changes: 8 additions & 4 deletions src/quadtree/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
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};
use serde::{ser::SerializeSeq, Serialize, Serializer};

use crate::{
Point,
shapes::{Rect, Shape},
util::{determine_overlap_quadrants, group_by_quadrant},
Point,
};

/// A generic Quadtree implementation for spatial indexing of 2D points
Expand Down Expand Up @@ -229,7 +233,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 @@ -510,9 +514,9 @@ mod tests {
use glam::vec2;

use crate::{
Point,
shapes::Circle,
util::tests::{make_circle, make_rect},
Point,
};

use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/shapes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use glam::{Vec2, vec2};
use glam::{vec2, Vec2};

#[cfg(feature = "serde")]
use serde::Serialize;
Expand Down
9 changes: 6 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use glam::vec2;

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

use crate::{
Point,
shapes::{Rect, Shape},
Point,
};

/// Get bounding rect for a list of items
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