refactor: Splitting into crates
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "vxls-data"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
vxls-production = { src = "../vxls-production" }
|
||||
bytemuck.workspace = true
|
||||
itertools.workspace = true
|
||||
@@ -0,0 +1,24 @@
|
||||
//!
|
||||
//! This crate contains types and structures to store voxel data
|
||||
//!
|
||||
|
||||
pub mod sparse_tree;
|
||||
|
||||
/// A voxel attribute must be able to be turned into bytes, and just be a bit of data
|
||||
pub trait Attribute: bytemuck::Pod + bytemuck::Zeroable + Clone + Copy + 'static
|
||||
{
|
||||
// Whether the two attributes can be merged into one single voxel
|
||||
pub fn mergeable(&self, other: &Self) -> bool;
|
||||
|
||||
pub fn merge(&self, other: &Self) -> Self;
|
||||
pub fn merge_iter<I: Iterator<Item = S>, S: AsRef<Self>>(iter: I) -> Option<Self>
|
||||
{
|
||||
iter.reduce(<Self as Attribute>::merge).map(|x| *x.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Has<A: Attribute>
|
||||
{
|
||||
fn get(&self) -> &A;
|
||||
fn get_mut(&mut self) -> &mut A;
|
||||
}
|
||||
@@ -0,0 +1,461 @@
|
||||
use std::collections::HashMap;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use bytemuck::Pod;
|
||||
use bytemuck::Zeroable;
|
||||
use itertools::Itertools;
|
||||
|
||||
use vxls_production::tree_locator::TreeLocator;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Pod, Zeroable)]
|
||||
#[repr(C)]
|
||||
pub struct Color(pub f32, pub f32, pub f32, pub f32);
|
||||
|
||||
impl Color
|
||||
{
|
||||
pub fn average<I: Iterator<Item = Color>>(iter: I) -> Self
|
||||
{
|
||||
let mut c = Color(0., 0., 0., 0.);
|
||||
let mut count = 0;
|
||||
for Color(r, g, b, a) in iter
|
||||
{
|
||||
count += 1;
|
||||
c.0 += r;
|
||||
c.1 += g;
|
||||
c.2 += b;
|
||||
c.3 += a;
|
||||
}
|
||||
c.0 /= count as f32;
|
||||
c.1 /= count as f32;
|
||||
c.2 /= count as f32;
|
||||
c.3 /= count as f32;
|
||||
c
|
||||
}
|
||||
|
||||
pub fn as_packed_u32(&self) -> u32
|
||||
{
|
||||
let mapped_r = (self.0 * 255.0).clamp(0., 255.0) as u32;
|
||||
let mapped_g = (self.1 * 255.0).clamp(0., 255.0) as u32;
|
||||
let mapped_b = (self.2 * 255.0).clamp(0., 255.0) as u32;
|
||||
let mapped_a = (self.3 * 255.0).clamp(0., 255.0) as u32;
|
||||
mapped_r | (mapped_g << 8) | (mapped_b << 16) | (mapped_a << 24)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct NTree<const N: usize>
|
||||
{
|
||||
map: HashMap<TreeLocator<N>, NTreeNode>,
|
||||
|
||||
// Depth is the parameter such that
|
||||
// N^depth == max_number_of_voxels
|
||||
// one root => depth = 1
|
||||
depth: usize,
|
||||
}
|
||||
|
||||
pub struct NTreeIter<'a, const N: usize>
|
||||
{
|
||||
tree: &'a NTree<N>,
|
||||
dfs_stack: Vec<TreeLocator<N>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct NTreeNode
|
||||
{
|
||||
pub color: Color,
|
||||
pub subdivided: bool,
|
||||
}
|
||||
|
||||
impl<const N: usize> NTree<N>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
pub fn constant(color: Color, depth: usize) -> Self
|
||||
{
|
||||
let mut map = HashMap::new();
|
||||
let root = NTreeNode {
|
||||
color,
|
||||
subdivided: false,
|
||||
};
|
||||
map.insert(TreeLocator::<N>::root(), root);
|
||||
Self { map, depth }
|
||||
}
|
||||
|
||||
pub fn set(&mut self, x: usize, y: usize, z: usize, color: Color)
|
||||
{
|
||||
self.set_at_depth(x, y, z, self.depth, color);
|
||||
}
|
||||
|
||||
pub fn get(&self, x: usize, y: usize, z: usize) -> Color
|
||||
{
|
||||
assert!(x < N.pow(self.depth as u32));
|
||||
assert!(y < N.pow(self.depth as u32));
|
||||
assert!(z < N.pow(self.depth as u32));
|
||||
|
||||
// Try to walk down the tree, and create necessary nodes
|
||||
let mut current_node_loc = TreeLocator::<N>::root();
|
||||
let mut current_node_size = N.pow(self.depth as u32);
|
||||
let mut current_node = self.map.get(¤t_node_loc).unwrap();
|
||||
|
||||
loop
|
||||
{
|
||||
if !current_node.subdivided
|
||||
{
|
||||
return current_node.color;
|
||||
}
|
||||
|
||||
// Descend to relevant node,
|
||||
let child_node_size = current_node_size / N;
|
||||
let child_x = (x % current_node_size) / child_node_size;
|
||||
let child_y = (y % current_node_size) / child_node_size;
|
||||
let child_z = (z % current_node_size) / child_node_size;
|
||||
current_node_size = child_node_size;
|
||||
|
||||
current_node_loc =
|
||||
current_node_loc.child(child_x as u32, child_y as u32, child_z as u32);
|
||||
current_node = self.map.get(¤t_node_loc).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_at_depth(&mut self, x: usize, y: usize, z: usize, depth: usize, color: Color)
|
||||
{
|
||||
assert!(x < N.pow(depth as u32));
|
||||
assert!(y < N.pow(depth as u32));
|
||||
assert!(z < N.pow(depth as u32));
|
||||
|
||||
// Try to walk down the tree, and create necessary nodes
|
||||
let mut current_node_loc = TreeLocator::<N>::root();
|
||||
let mut current_node_size = N.pow(self.depth as u32);
|
||||
let mut current_depth = 0;
|
||||
let mut current_node = self.map.get_mut(¤t_node_loc).unwrap();
|
||||
|
||||
while current_depth != depth
|
||||
{
|
||||
if current_node.color == color
|
||||
{
|
||||
// Color is already correct, work is finished
|
||||
return;
|
||||
}
|
||||
|
||||
// Current is not subdivided, and color is different => subdivide
|
||||
if !current_node.subdivided
|
||||
{
|
||||
current_node.subdivided = true;
|
||||
let new_nodes = NTreeNode {
|
||||
color: current_node.color,
|
||||
subdivided: false,
|
||||
};
|
||||
current_node_loc.children().for_each(|child| {
|
||||
self.map.insert(child, new_nodes);
|
||||
});
|
||||
}
|
||||
|
||||
// Descend to relevant node,
|
||||
let child_node_size = current_node_size / N;
|
||||
let child_x = (x % current_node_size) / child_node_size;
|
||||
let child_y = (y % current_node_size) / child_node_size;
|
||||
let child_z = (z % current_node_size) / child_node_size;
|
||||
current_node_size = child_node_size;
|
||||
current_depth += 1;
|
||||
|
||||
current_node_loc =
|
||||
current_node_loc.child(child_x as u32, child_y as u32, child_z as u32);
|
||||
current_node = self.map.get_mut(¤t_node_loc).unwrap();
|
||||
}
|
||||
|
||||
if current_node.subdivided
|
||||
{
|
||||
self.make_constant(current_node_loc, color);
|
||||
}
|
||||
else
|
||||
{
|
||||
current_node.color = color;
|
||||
}
|
||||
|
||||
// We need to go back up the hierarchy to combine nodes
|
||||
// current_node is the modified node, go one up
|
||||
|
||||
loop
|
||||
{
|
||||
// Go one node up
|
||||
current_depth -= 1;
|
||||
current_node_loc = current_node_loc.parent().unwrap();
|
||||
//current_node_size *= N;
|
||||
|
||||
// Access all children's colors
|
||||
let children_color_avg = current_node_loc
|
||||
.children()
|
||||
.map(|child_loc| self.map.get(&child_loc).unwrap().color)
|
||||
// AGGREGATE AND AVERAGE HERE !!
|
||||
.next()
|
||||
.unwrap();
|
||||
|
||||
current_node = self.map.get_mut(¤t_node_loc).unwrap();
|
||||
current_node.color = children_color_avg;
|
||||
|
||||
if current_depth == 0
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn make_constant(&mut self, locator: TreeLocator<N>, color: Color)
|
||||
{
|
||||
let mut dfs_stack = Vec::new();
|
||||
dfs_stack.push(locator);
|
||||
|
||||
while let Some(current) = dfs_stack.pop()
|
||||
{
|
||||
// Remove element
|
||||
let removed = self.map.remove(¤t);
|
||||
let handle_children = removed.is_some_and(|x| x.subdivided);
|
||||
|
||||
// If children have to be handled, push on stack
|
||||
if handle_children
|
||||
{
|
||||
dfs_stack.extend(current.children());
|
||||
}
|
||||
}
|
||||
|
||||
self.map.insert(
|
||||
locator,
|
||||
NTreeNode {
|
||||
color,
|
||||
subdivided: false,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
pub fn from_array(array: &[Color], depth: usize) -> Self
|
||||
{
|
||||
let mut structure = HashMap::new();
|
||||
// Per voxel in the array :
|
||||
// 0 -> Voxel not merged yet
|
||||
// _ -> size of the block in which the voxel was merged
|
||||
let mut merged_size = vec![0; array.len()];
|
||||
let width = N.pow(depth as u32);
|
||||
|
||||
for (z, y) in (0..width).cartesian_product(0..width)
|
||||
{
|
||||
let mut x = 0;
|
||||
while x < width
|
||||
{
|
||||
if merged_size[x + y * width + z * width * width] != 0
|
||||
{
|
||||
// If current voxel has already been accounted for -> skip
|
||||
// We stumbled on a big merged block, we can skip it as a whole on the inner
|
||||
// dimension
|
||||
x += merged_size[x + y * width + z * width * width];
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check smallest depth aligned with current position
|
||||
let x_trailing = base_trailing_zeroes::<N>(x).unwrap_or(depth as u32);
|
||||
let y_trailing = base_trailing_zeroes::<N>(y).unwrap_or(depth as u32);
|
||||
let z_trailing = base_trailing_zeroes::<N>(z).unwrap_or(depth as u32);
|
||||
|
||||
// Biggest block size power of which this voxel can be a lower corner
|
||||
let block_power = x_trailing.min(y_trailing.min(z_trailing));
|
||||
|
||||
// Try to merge for each depth
|
||||
let mut max_power_merge = 0; // Single voxel can always be merged
|
||||
let color = array[x + y * width + z * width * width];
|
||||
let mut locator =
|
||||
TreeLocator::<N>::from_node_coords(depth as u32, x as u32, y as u32, z as u32);
|
||||
'power_search: for power in 1..=block_power
|
||||
{
|
||||
let block_size = N.pow(power);
|
||||
// Check if candidate block has the same color
|
||||
for ((merge_z, merge_y), merge_x) in (0..block_size)
|
||||
.cartesian_product(0..block_size)
|
||||
.cartesian_product(0..block_size)
|
||||
{
|
||||
if array
|
||||
[(x + merge_x) + (y + merge_y) * width + (z + merge_z) * width * width]
|
||||
!= color
|
||||
{
|
||||
break 'power_search;
|
||||
}
|
||||
}
|
||||
|
||||
// Update biggest candidate block
|
||||
locator = locator.parent().unwrap();
|
||||
max_power_merge = power;
|
||||
}
|
||||
|
||||
// We know which size we can know merge
|
||||
// Say we accounted for those voxels :
|
||||
let block_size = N.pow(max_power_merge);
|
||||
for ((merge_z, merge_y), merge_x) in (0..block_size)
|
||||
.cartesian_product(0..block_size)
|
||||
.cartesian_product(0..block_size)
|
||||
{
|
||||
merged_size
|
||||
[(x + merge_x) + (y + merge_y) * width + (z + merge_z) * width * width] =
|
||||
block_size;
|
||||
}
|
||||
|
||||
// Add new found block in our structure
|
||||
// If power is depth, this is the whole structure, thus depth is 0
|
||||
let node = NTreeNode {
|
||||
color,
|
||||
subdivided: false,
|
||||
};
|
||||
structure.insert(locator, node);
|
||||
x += block_size;
|
||||
}
|
||||
}
|
||||
|
||||
// The structure now consists of the leaf most nodes, we need to rebuild the hierarchy
|
||||
// bottom up
|
||||
for current_power in 1..=depth
|
||||
{
|
||||
// Iterate on the blocks of this size
|
||||
let block_size = N.pow(current_power as u32);
|
||||
let block_count = width / block_size;
|
||||
|
||||
for ((block_x, block_y), block_z) in (0..block_count)
|
||||
.cartesian_product(0..block_count)
|
||||
.cartesian_product(0..block_count)
|
||||
{
|
||||
// Check the lower corner of this block
|
||||
let corner = merged_size[(block_x * block_size)
|
||||
+ (block_y * block_size) * width
|
||||
+ (block_z * block_size) * width * width];
|
||||
if corner >= block_size
|
||||
{
|
||||
// Children are merged in bigger block
|
||||
continue;
|
||||
}
|
||||
|
||||
// Add hierarchy node
|
||||
let locator = TreeLocator::<N>::from_node_coords(
|
||||
(depth - current_power) as u32,
|
||||
block_x as u32,
|
||||
block_y as u32,
|
||||
block_z as u32,
|
||||
);
|
||||
|
||||
// Get average color of children
|
||||
let color = Color::average(
|
||||
locator
|
||||
.children()
|
||||
.map(|child_loc| structure.get(&child_loc).unwrap().color),
|
||||
);
|
||||
|
||||
let hierarchy_node = NTreeNode {
|
||||
color,
|
||||
subdivided: true,
|
||||
};
|
||||
structure.insert(locator, hierarchy_node);
|
||||
}
|
||||
}
|
||||
|
||||
Self {
|
||||
map: structure,
|
||||
depth,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_by_loc(&self, locator: TreeLocator<N>) -> Option<NTreeNode>
|
||||
{
|
||||
self.map.get(&locator).copied()
|
||||
}
|
||||
}
|
||||
|
||||
fn base_trailing_zeroes<const N: usize>(mut number: usize) -> Option<u32>
|
||||
{
|
||||
if number == 0
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let mut trailing_zeroes = 0;
|
||||
while number % N == 0
|
||||
{
|
||||
number /= N;
|
||||
trailing_zeroes += 1;
|
||||
}
|
||||
|
||||
Some(trailing_zeroes)
|
||||
}
|
||||
|
||||
pub fn load_ply<P: AsRef<std::path::Path>, const N: usize>(path: P) -> NTree<N>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
let file = std::fs::read_to_string(path).unwrap();
|
||||
|
||||
let mut colors = vec![Color(0., 0., 0., 0.); 256 * 256 * 256];
|
||||
for line in file.lines()
|
||||
{
|
||||
let coords = line
|
||||
.split(',')
|
||||
.take(3)
|
||||
.map(|s| s.trim_end().trim_start().parse::<u32>().unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
let (x, y, z) = (coords[0], coords[1], coords[2]);
|
||||
let index = x + z * 256 + y * 256 * 256;
|
||||
colors[index as usize] = Color(1., 1., 1., 1.);
|
||||
}
|
||||
|
||||
NTree::from_array(&colors, 4)
|
||||
}
|
||||
|
||||
mod test
|
||||
{
|
||||
use glam::Vec3;
|
||||
use rand::random;
|
||||
|
||||
use crate::sparse_tree::Color;
|
||||
use crate::sparse_tree::NTree;
|
||||
use vxls_production::tree_locator::TreeLocator;
|
||||
|
||||
#[test]
|
||||
fn constant_set()
|
||||
{
|
||||
let mut tree = NTree::<4>::constant(Color(0., 0., 0., 0.), 4);
|
||||
assert!(tree.get(0, 0, 0) == Color(0., 0., 0., 0.));
|
||||
tree.set(0, 0, 0, Color(1., 1., 1., 1.));
|
||||
assert!(tree.get(0, 0, 0) == Color(1., 1., 1., 1.));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_read_same()
|
||||
{
|
||||
let file =
|
||||
std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/../../dragon.txt"))
|
||||
.unwrap();
|
||||
|
||||
let mut colors = vec![Color(0., 0., 0., 0.); 256 * 256 * 256];
|
||||
for line in file.lines()
|
||||
{
|
||||
let coords = line
|
||||
.split(',')
|
||||
.take(3)
|
||||
.map(|s| s.trim_end().trim_start().parse::<u32>().unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
let (x, y, z) = (coords[0], coords[1], coords[2]);
|
||||
let index = x + z * 256 + y * 256 * 256;
|
||||
colors[index as usize] = Color(1., 1., 1., 1.);
|
||||
}
|
||||
|
||||
println!("building tree");
|
||||
let tree = NTree::<4>::from_array(&colors, 4);
|
||||
println!("ok");
|
||||
|
||||
for x in 0..256
|
||||
{
|
||||
for y in 0..256
|
||||
{
|
||||
for z in 0..256
|
||||
{
|
||||
let index = x + y * 256 + z * 256 * 256;
|
||||
let color = colors[index];
|
||||
let t = tree.get(x, y, z);
|
||||
assert!(t == color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user