From 0dfe8640a58e2dc67f1c030209932dfb62594196 Mon Sep 17 00:00:00 2001 From: Martin Indra Date: Sun, 17 Mar 2024 19:02:39 +0100 Subject: [PATCH] WIP --- crates/quadtree/src/lib.rs | 126 +++++++++++++++++------------------- crates/quadtree/src/tree.rs | 34 ++++++++-- 2 files changed, 85 insertions(+), 75 deletions(-) diff --git a/crates/quadtree/src/lib.rs b/crates/quadtree/src/lib.rs index 0b882172..dba0df5d 100644 --- a/crates/quadtree/src/lib.rs +++ b/crates/quadtree/src/lib.rs @@ -53,83 +53,73 @@ where // self.leafs[target].insert(item); // } - // pub fn remove(&mut self, pos: Vec2, item: T) { - // let item = Item { pos, item }; - - // let mut rect = self.rect.clone(); - // let mut current = Slot::Inner(0); - - // let target = loop { - // match current { - // Slot::Inner(index) => { - // let quadrant = rect.quadrant(item.pos); - // match self.inner[index].children.get(quadrant) { - // Some(slot) => { - // rect = rect.child(quadrant); - // current = *slot; - // } - // None => { - // // TODO point does not exist - // } - // } - // } - // Slot::Leaf(index) => { - // break index; - // } - // } - // }; + pub fn remove(&mut self, pos: Vec2, item: T) { + let item = Item { pos, item }; + + let mut rect = self.rect.clone(); + let mut current = Tree::>::ROOT; + + let target = loop { + match current { + Node::Inner(index) => { + let quadrant = rect.quadrant(item.pos); + match self.tree.children(index).get(quadrant) { + Some(slot) => { + rect = rect.child(quadrant); + current = *slot; + } + None => { + // TODO point does not exist + } + } + } + Node::Leaf(index) => { + break index; + } + } + }; - // let leaf = &mut self.leafs[target]; - // let mut parent = leaf.parent; + let items = self.tree.get_leaf_mut(target).unwrap(); + if !items.remove(item) { + // TODO point does not exist + } + let is_empty = items.len == 0; - // // TODO return the bool - // leaf.remove(item); + let mut parent = self.tree.leaf_parent(target); + if is_empty { + self.tree.remove_leaf(target); + } - // if leaf.len == 0 { - // self.remove_leaf(target, None); - // } + while self.mergable(parent) { + let leaf_index = self.merge(parent); + parent = self.tree.leaf_parent(leaf_index); + } + } - // while self.mergable(parent) { - // let leaf_index = self.merge(parent); - // parent = self.leafs[leaf_index].parent; - // } - // } + fn split(&mut self, index: usize, rect: &Rect) -> usize { + let (new_inner_index, items) = self.tree.replace_leaf(index); + for item in items.items.into_iter().take(items.len) { + self.insert(new_inner_index, rect, item); + } + new_inner_index + } - // fn split(&mut self, index: usize, rect: &Rect) -> usize { - // let inner_index = self.inner.len(); - // let removed = self.remove_leaf(index, Some(Slot::Inner(inner_index))); - - // let mut leafs = Quadrants::new( - // Some(Leaf::new(inner_index)), - // Some(Leaf::new(inner_index)), - // Some(Leaf::new(inner_index)), - // Some(Leaf::new(inner_index)), - // ); - - // for item in removed.items.into_iter().take(removed.len) { - // leafs.get_mut(rect.quadrant(item.pos)).unwrap().insert(item); - // } - - // let all_leafs = &mut self.leafs; - // let new_inner = Inner::new( - // Some(removed.parent), - // leafs.map(move |leaf| { - // let slot = Slot::Leaf(all_leafs.len()); - // all_leafs.push(leaf); - // slot - // }), - // ); - - // self.inner.push(new_inner); - // inner_index - // } + fn insert(&mut self, index: usize, rect: &Rect, item: Item) { + let quadrant = rect.quadrant(item.pos); + let node = self.tree.init_child(index, quadrant, Items::new); + let items = match node { + Node::Inner(_) => panic!("Cannot insert item to an inner node."), + Node::Leaf(index) => self.tree.get_leaf_mut(index).unwrap(), + }; + items.insert(item); + } fn mergable(&self, index: usize) -> bool { if index == 0 { return false; } - // TODO use a constant + // TODO use a constant (half of max size) self.num_children(index).map_or(false, |num| num <= 8) } @@ -156,12 +146,12 @@ where } let removed = self.tree.remove_children(index); - let new_leaf_index = self.tree.replace_inner(index); + let new_leaf_index = self.tree.replace_inner(index, Items::new()); let leaf = self.tree.get_leaf_mut(new_leaf_index).unwrap(); for items in &removed { - for &item in items.items.iter().take(items.len) { + for item in items.items.into_iter().take(items.len) { leaf.items[leaf.len] = item; leaf.len += 1; } diff --git a/crates/quadtree/src/tree.rs b/crates/quadtree/src/tree.rs index 53324dcd..77a49c6e 100644 --- a/crates/quadtree/src/tree.rs +++ b/crates/quadtree/src/tree.rs @@ -15,6 +15,8 @@ impl Tree where S: Default, { + pub(super) const ROOT: Node = Node::Inner(0); + pub(super) fn new() -> Self { let mut inner = Packed::new(); // Add empty root. @@ -33,10 +35,31 @@ where self.leafs.get_mut(index).map(|l| &mut l.item) } + pub(super) fn leaf_parent(&self, index: usize) -> usize { + self.leafs.get(index).unwrap().parent + } + pub(super) fn children(&self, index: usize) -> &Quadrants { &self.inner.get(index).unwrap().children } + pub(super) fn init_child(&mut self, index: usize, quadrant: Quadrant, init: F) -> Node + where + F: Fn() -> S, + { + let inner = self.inner.get_mut(index).unwrap(); + + match inner.children.get(quadrant) { + Some(node) => *node, + None => { + let node = Node::Leaf(self.leafs.len()); + inner.children.set(quadrant, Some(node)); + self.leafs.push(Leaf::new(index, init())); + node + } + } + } + pub(super) fn remove_children(&mut self, index: usize) -> Quadrants { let mut removed = Quadrants::empty(); @@ -69,11 +92,11 @@ where self.replace_internal(Node::Leaf(index), None).unwrap() } - pub(super) fn replace_inner(&mut self, index: usize) -> usize { + pub(super) fn replace_inner(&mut self, index: usize, item: S) -> usize { let parent = self.inner.get(index).unwrap().parent; let new_leaf_index = self.leafs.len(); - self.leafs.push(Leaf::new(parent)); + self.leafs.push(Leaf::new(parent, item)); self.replace_internal(Node::Inner(index), Some(Node::Leaf(new_leaf_index))); new_leaf_index @@ -172,10 +195,7 @@ impl Leaf where S: Default, { - fn new(parent: usize) -> Self { - Self { - parent, - item: S::default(), - } + fn new(parent: usize, item: S) -> Self { + Self { parent, item } } }