path implementation (bitpacking!)
This commit is contained in:
parent
2a7c1f11c8
commit
4e9ca044fb
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
|
@ -1,5 +1,6 @@
|
|||
{
|
||||
"rust-analyzer.linkedProjects": [
|
||||
"./Cargo.toml"
|
||||
]
|
||||
],
|
||||
"rust-analyzer.cargo.target": "wasm32-unknown-unknown"
|
||||
}
|
|
@ -1,10 +1,10 @@
|
|||
#![allow(dead_code)]
|
||||
use bevy::math::Vec3;
|
||||
|
||||
use crate::oct_tree::constants::to_region_id;
|
||||
|
||||
use self::constants::{parent_region, sub_region};
|
||||
use self::constants::sub_region;
|
||||
use std::mem::swap;
|
||||
|
||||
#[derive(Clone, Default, Debug)]
|
||||
enum TreeNode<T> {
|
||||
Branch(Box<[TreeNode<T>; 8]>),
|
||||
|
@ -20,6 +20,36 @@ struct OctTree<T> {
|
|||
root: TreeNode<T>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
struct Path {
|
||||
packed: u64,
|
||||
length: usize,
|
||||
}
|
||||
|
||||
fn get_pindex(path: &Path, index: usize) -> usize {
|
||||
if path.length < index {
|
||||
panic!("in too deep!");
|
||||
}
|
||||
let shift_amount = index as u64 * 3;
|
||||
let unshifted_result = path.packed >> shift_amount;
|
||||
(unshifted_result & 7) as usize
|
||||
}
|
||||
|
||||
fn set_pindex(path: &mut Path, index: usize, value: usize) {
|
||||
if value > 7 {
|
||||
panic!("cannot set to more than 7!");
|
||||
}
|
||||
if index > path.length {
|
||||
path.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;
|
||||
path.packed = path.packed & shift_mask;
|
||||
path.packed = path.packed | shifted_value;
|
||||
}
|
||||
|
||||
impl<T: Clone> OctTree<T> {
|
||||
pub fn new(npos: Vec3, value: T) -> Self {
|
||||
Self {
|
||||
|
@ -153,7 +183,7 @@ impl<T: Clone> OctTree<T> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn voxel_at(&self, location: Vec3) -> Option<T> {
|
||||
pub fn voxel_at(&self, location: Vec3) -> Option<(T, Path)> {
|
||||
if bounds_contains(self.center, self.size, location) {
|
||||
let mut rc_center = self.center;
|
||||
let mut rc_size = self.size;
|
||||
|
@ -185,7 +215,7 @@ impl<T: Clone> OctTree<T> {
|
|||
match rc_loc {
|
||||
TreeNode::Leaf(value) => {
|
||||
println!("found at {rc_center}, {rc_size}");
|
||||
return Some(value.clone());
|
||||
return Some((value.clone(), Default::default()));
|
||||
}
|
||||
_ => {
|
||||
println!("Couldn't find it..");
|
||||
|
@ -195,6 +225,8 @@ impl<T: Clone> OctTree<T> {
|
|||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn remove_voxel(&mut self, location: Vec3) {}
|
||||
}
|
||||
|
||||
pub fn bounds_contains(center: Vec3, size: usize, subject: Vec3) -> bool {
|
||||
|
@ -229,7 +261,7 @@ mod constants {
|
|||
}
|
||||
pub fn to_index(vec: Vec3) -> i32 {
|
||||
let mut index = 0;
|
||||
for i in quadrant_offsets::ordered {
|
||||
for i in quadrant_offsets::ORDERED {
|
||||
if i == vec {
|
||||
return index;
|
||||
}
|
||||
|
@ -241,7 +273,7 @@ mod constants {
|
|||
if index < 0 || index >= 8 {
|
||||
return Vec3::ZERO;
|
||||
}
|
||||
return quadrant_offsets::ordered[index as usize];
|
||||
return quadrant_offsets::ORDERED[index as usize];
|
||||
}
|
||||
pub fn sub_region(position: Vec3, size: usize, subject: i32) -> (Vec3, usize) {
|
||||
let size = size as f32;
|
||||
|
@ -260,7 +292,7 @@ mod constants {
|
|||
let size = size as f32;
|
||||
let new_size = size * 2.;
|
||||
let half_size = size / 2.;
|
||||
let mut subvec = from_index(subject);
|
||||
let subvec = from_index(subject);
|
||||
//subvec *= -1.;
|
||||
let offset = Vec3 {
|
||||
x: subvec.x * half_size,
|
||||
|
@ -290,7 +322,7 @@ mod constants {
|
|||
pub const PNP: Vec3 = Vec3::new(1., -1., 1.);
|
||||
pub const PPN: Vec3 = Vec3::new(1., 1., -1.);
|
||||
pub const PPP: Vec3 = Vec3::new(1., 1., 1.);
|
||||
pub const ordered: [Vec3; 8] = [NNN, NNP, NPN, NPP, PNN, PNP, PPN, PPP];
|
||||
pub const ORDERED: [Vec3; 8] = [NNN, NNP, NPN, NPP, PNN, PNP, PPN, PPP];
|
||||
}
|
||||
mod cardinal_directions {
|
||||
use bevy::math::Vec3;
|
||||
|
@ -313,9 +345,19 @@ pub fn test_octtree() {
|
|||
let far_off_bullshit = Vec3::new(26., 26., 26.);
|
||||
test_oct.set_voxel(far_off_bullshit, 5);
|
||||
dbg!(&test_oct);
|
||||
assert_eq!(test_oct.voxel_at(neighbor), Some(2));
|
||||
assert_eq!(test_oct.voxel_at(origin), Some(1));
|
||||
assert_eq!(test_oct.voxel_at(far_off_bullshit), Some(5));
|
||||
assert_eq!(test_oct.voxel_at(neighbor).unwrap().0, 2);
|
||||
assert_eq!(test_oct.voxel_at(origin).unwrap().0, 1);
|
||||
assert_eq!(test_oct.voxel_at(far_off_bullshit).unwrap().0, 5);
|
||||
}
|
||||
#[test]
|
||||
pub fn test_path() {
|
||||
let mut path_test: Path = Default::default();
|
||||
set_pindex(&mut path_test, 0, 6);
|
||||
set_pindex(&mut path_test, 7, 4);
|
||||
assert_eq!(get_pindex(&path_test, 7), 4);
|
||||
println!("{:#?}", dbg!(path_test.clone()));
|
||||
assert_eq!(get_pindex(&path_test, 0), 6);
|
||||
println!("{:#?}", dbg!(path_test.clone()));
|
||||
}
|
||||
/*
|
||||
use rand::Rng;
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
pub mod mesh_plugin {
|
||||
use bevy::{
|
||||
prelude::*,
|
||||
render::{
|
||||
mesh::{Indices, VertexAttributeValues},
|
||||
render_resource::PrimitiveTopology,
|
||||
},
|
||||
render::{mesh::Indices, render_resource::PrimitiveTopology},
|
||||
};
|
||||
|
||||
use crate::orbit_camera::orbit_camera::*;
|
||||
|
|
Loading…
Reference in a new issue