From 7e91f93fe0f110608c06381f1987229ec97ef492 Mon Sep 17 00:00:00 2001 From: adia redmoon Date: Sun, 5 May 2024 19:05:10 -0700 Subject: [PATCH] to tower pc --- src/vvlib/mod.rs | 1 + src/vvlib/s_true_generic_octtree.rs | 115 ++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 src/vvlib/s_true_generic_octtree.rs diff --git a/src/vvlib/mod.rs b/src/vvlib/mod.rs index 854fcd2..6a70ef1 100644 --- a/src/vvlib/mod.rs +++ b/src/vvlib/mod.rs @@ -7,6 +7,7 @@ pub mod s_obb; pub mod s_oct_asset; pub mod s_octtree; pub mod s_structure_asset; +pub mod s_true_generic_octtree; pub const RANDOM_SPACE: f32 = 5.; // size of the random possibility space. pub const INTERVAL: f32 = 0.3; // how often to add ^ that many and remove ^ that many random locations diff --git a/src/vvlib/s_true_generic_octtree.rs b/src/vvlib/s_true_generic_octtree.rs new file mode 100644 index 0000000..a84bf3b --- /dev/null +++ b/src/vvlib/s_true_generic_octtree.rs @@ -0,0 +1,115 @@ +/*use bevy::math::Vec3; +pub trait OctTreeStorage { + type Identifier: Sized; + type Storage: ElementTraversal; +} +pub trait ElementTraversal { + fn as_ref(&self, subject: S) -> Option<&T>; + fn as_mut(&mut self, subject: S) -> Option<&mut T>; + fn child_ref(&self, subject: S, index: usize) -> Option<&Self>; + fn child_mut(&self, subject: S, index: usize) -> Option<&mut Self>; + fn children_ref(&self, subject: S) -> Option<[&Self; 8]>; + fn children_mut(&self, subject: S) -> Option<[&mut Self; 8]>; + // match avoidance helpers + fn is_none(&self, subject: S) -> bool; + fn is_some(&self, subject: S) -> bool { + self.is_leaf(subject) || self.is_child(subject) + } + fn is_leaf(&self, subject: S) -> bool; + fn is_child(&self, subject: S) -> bool; +} + +pub struct Path { + packed: u64, + length: usize, +} +impl Path { + pub fn parent(&self) -> Path { + let mut duplicate = self.clone(); + duplicate.set_index(self.length - 1, 0); + duplicate.length = duplicate.length - 1; + return duplicate; + } + pub fn pop(&mut self) -> Option { + if self.length == 0 { + return None; + } + self.length -= 1; + return Some(self.get_index(self.length)); + } + pub fn append(&mut self, value: usize) { + self.set_index(self.length, value); + } + pub fn to_local(&self, grid: &OctTree) -> Vec3 { + let (center, size) = (grid.center, grid.size); + self.to_vec3(center, size); + } + pub fn to_vec3(&self, center: Vec3, size: usize) -> Vec3 { + let mut iter = (center, size); + for i in 0..self.length { + iter = super::constants::sub_region( + iter.0, + iter.1, + self.get_index(i.try_into().unwrap()).try_into().unwrap(), + ); + } + return iter.0; + } + pub fn get_index(&self, index: usize) -> usize { + if index >= 22 { + panic!("the Path can only hold 21 layers! this equates to 2,097,152 voxels across any oct-tree grid."); + } + if self.length < index { + panic!("in too deep!"); + } + let shift_amount = index as u64 * 3; + let unshifted_result = self.packed >> shift_amount; + (unshifted_result & 7) as usize + } + pub fn set_index(&mut self, index: usize, value: usize) { + if index >= 22 { + panic!("the Path can only hold 21 layers! this equates to 2,097,152 voxels across any oct-tree grid."); + } + if value > 7 { + panic!("cannot set to more than 7!"); + } + if index + 1 > self.length { + self.length = index + 1; + } + let shift_amount = index as u64 * 3; + let shifted_value = (value as u64) << shift_amount; + let shift_mask = 7_u64 << shift_amount; + let shift_mask = !shift_mask; + self.packed = self.packed & shift_mask; + self.packed = self.packed | shifted_value; + } +} + +pub struct OctTree { + storage: S, + root: Box>, + pub size: usize, + pub center: Vec3, +} + +struct GridID { + id: Option, + location: Vec3, +} + +enum VoxelGridLookupError { + LocationNotOccupied, + LocationNotInGrid, +} + +pub trait VoxelGrid { + type Identifier; + fn new() -> Self; + fn pop(&mut self, subject: GridID) -> Result; + fn remove(&mut self, subject: GridID) -> Option; + fn set(&mut self, subject: GridID) -> bool; +} + +// implement indexing trait for Path and Vec3 +// implement indexing for each element in the tree, using a type that is either path or vec3 (constructs path while iterating down) +*/