use morton::Morton;
use std::collections::{HashMap, VecDeque};
pub struct LinearHashedOctree<Node> {
nodes: HashMap<Morton, Node>,
leaves: Vec<Morton>,
}
impl<Node> LinearHashedOctree<Node> {
pub fn new() -> Self {
Self {
nodes: HashMap::new(),
leaves: Vec::new(),
}
}
pub fn build<R, C>(&mut self, mut should_refine: R, mut construct_node: C)
where
R: FnMut(Morton, &Node) -> bool,
C: FnMut(Morton) -> Node,
{
let mut queue = VecDeque::new();
queue.push_back(Morton::new());
while let Some(key) = queue.pop_front() {
let node = construct_node(key);
if should_refine(key, &node) {
for i in 0..8 {
queue.push_back(key.child(i));
}
} else {
self.leaves.push(key);
}
self.nodes.insert(key, node);
}
}
pub fn walk_leaves<W>(&self, mut walker: W)
where
W: FnMut(Morton),
{
for &key in self.leaves.iter() {
walker(key);
}
}
pub fn get_node(&self, key: &Morton) -> Option<&Node> {
self.nodes.get(key)
}
}