diff --git a/worldsim/src/lib.rs b/worldsim/src/lib.rs index f2f215f3eb..4be0d1c184 100644 --- a/worldsim/src/lib.rs +++ b/worldsim/src/lib.rs @@ -1,8 +1,9 @@ -#![feature(euclidean_division, duration_float, trait_alias, bind_by_move_pattern_guards)] +#![feature(euclidean_division, duration_float, trait_alias, bind_by_move_pattern_guards, test)] extern crate serde_derive; #[macro_use] extern crate log; +extern crate test; pub mod job; pub mod regionmanager; diff --git a/worldsim/src/lodstore/index.rs b/worldsim/src/lodstore/index.rs index 6d65c9e0ed..9ab2ff5e7f 100644 --- a/worldsim/src/lodstore/index.rs +++ b/worldsim/src/lodstore/index.rs @@ -1,6 +1,8 @@ use vek::*; use std::ops::Sub; use std::ops::Add; +use std::cmp; +use std::fmt; /* For our LodStructures we need a type that covers the values from 0 - 2047 in steps of 1/32. @@ -66,8 +68,42 @@ impl LodIndex { (i / f) * f })) } + + pub fn get_highest_layer_that_fits(&self) -> u8 { + let pos = self.get(); + cmp::min( cmp::min(cmp::min(pos[0].trailing_zeros(), + pos[1].trailing_zeros()), pos[2].trailing_zeros()), 15) as u8 + } } +impl fmt::Display for LodIndex { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let xyz = self.get(); + //write!(f, "({}|{}|{}) <{}>", xyz[0], xyz[1], xyz[2], self.data) + write!(f, "({}|{}|{})", xyz[0], xyz[1], xyz[2]) + } +} + +#[derive(PartialEq, Eq, Clone, Copy, Hash, Debug)] +pub struct AbsIndex { + pub layer: u8, + pub index: usize, +} + +impl AbsIndex { + pub fn new(layer: u8, index: usize) -> Self { + AbsIndex { + layer, + index, + } + } +} + +impl fmt::Display for AbsIndex { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "[{}:{}]", self.layer, self.index) + } +} #[cfg(test)] mod tests { @@ -102,6 +138,79 @@ mod tests { let i = LodIndex::new(Vec3::new(42,1337,69)); assert_eq!(i.get(), Vec3::new(42,1337,69)); } + + #[test] + fn align() { + let i = LodIndex::new(Vec3::new(1337,0,0)).align_to_layer_id(4); + assert_eq!(i.get(), Vec3::new(1328,0,0)); + + let i = LodIndex::new(Vec3::new(1337,1800,0)).align_to_layer_id(5); + assert_eq!(i.get(), Vec3::new(1312,1792,0)); + + let i = LodIndex::new(Vec3::new(1337,0,50)).align_to_layer_id(3); + assert_eq!(i.get(), Vec3::new(1336,0,48)); + + let i = LodIndex::new(Vec3::new(1335,0,0)).align_to_layer_id(3); + assert_eq!(i.get(), Vec3::new(1328,0,0)); + + let i = LodIndex::new(Vec3::new(31337,22000,25000)).align_to_layer_id(7); + assert_eq!(i.get(), Vec3::new(31232,21888,24960)); + + let i = LodIndex::new(Vec3::new(31337,22000,25000)).align_to_layer_id(0); + assert_eq!(i.get(), Vec3::new(31337,22000,25000)); + + let i = LodIndex::new(Vec3::new(0,0,0)).align_to_layer_id(4); + assert_eq!(i.get(), Vec3::new(0,0,0)); + } + + #[test] + fn get_highest_layer_that_fits() { + let i = LodIndex::new(Vec3::new(0,0,0)); + assert_eq!(i.get_highest_layer_that_fits(), 15); + let i = LodIndex::new(Vec3::new(1,0,0)); + assert_eq!(i.get_highest_layer_that_fits(), 0); + let i = LodIndex::new(Vec3::new(2,0,0)); + assert_eq!(i.get_highest_layer_that_fits(), 1); + let i = LodIndex::new(Vec3::new(3,0,0)); + assert_eq!(i.get_highest_layer_that_fits(), 0); + let i = LodIndex::new(Vec3::new(4,0,0)); + assert_eq!(i.get_highest_layer_that_fits(), 2); + let i = LodIndex::new(Vec3::new(5,0,0)); + assert_eq!(i.get_highest_layer_that_fits(), 0); + + let i = LodIndex::new(Vec3::new(1337,0,0)); + assert_eq!(i.get_highest_layer_that_fits(), 0); + + let i = LodIndex::new(Vec3::new(1337,1800,0)); + assert_eq!(i.get_highest_layer_that_fits(), 0); + + let i = LodIndex::new(Vec3::new(1338,0,50)); + assert_eq!(i.get_highest_layer_that_fits(), 1); + + let i = LodIndex::new(Vec3::new(1336,0,0)); + assert_eq!(i.get_highest_layer_that_fits(), 3); + + let i = LodIndex::new(Vec3::new(31348,22000,25000)); + assert_eq!(i.get_highest_layer_that_fits(), 2); + + let i = LodIndex::new(Vec3::new(0,0,0)); + assert_eq!(i.get_highest_layer_that_fits(), 15); + + let i = LodIndex::new(Vec3::new(65536,0,0)); + assert_eq!(i.get_highest_layer_that_fits(), 15); + + let i = LodIndex::new(Vec3::new(32768,0,0)); + assert_eq!(i.get_highest_layer_that_fits(), 15); + + let i = LodIndex::new(Vec3::new(16384,0,0)); + assert_eq!(i.get_highest_layer_that_fits(), 14); + + let i = LodIndex::new(Vec3::new(8192,0,0)); + assert_eq!(i.get_highest_layer_that_fits(), 13); + + let i = LodIndex::new(Vec3::new(65536,0,8192)); + assert_eq!(i.get_highest_layer_that_fits(), 13); + } } impl Sub for LodIndex { @@ -122,51 +231,24 @@ impl Add for LodIndex { } } - -/* -impl LodIndex { - pub fn new(pos: Vec3) -> Self { - Self { - data: pos.map(|x| (x * 32 + 65535) as u32), - } - } - - pub fn newf(pos: Vec3) -> Self { - Self { - data: pos.map(|x| (x * 32.0).round() as u32 + 65535), - } - } - - pub fn to_pos_i(&self) -> Vec3 { self.data.map(|x| (x / 32 - 2048) as i32) } - - pub fn to_pos_f(&self) -> Vec3 { - self.data.map(|x| x as f32 / 32.0 - 2048.0) - } -} - -pub const LEVEL_LENGTH_POW_MAX: i8 = 11; -pub const LEVEL_LENGTH_POW_MIN: i8 = -4; - -pub const LEVEL_INDEX_POW_MAX: u8 = 15; -pub const LEVEL_INDEX_POW_MIN: u8 = 0; - -pub const fn length_to_index(n: i8) -> u8 { (n+4) as u8 } - -pub const fn two_pow_u(n: u8) -> u16 { - 1 << n -} - -pub fn two_pow_i(n: i8) -> f32 { - 2.0_f32.powi(n as i32) -} - -*/ - pub const fn two_pow_u(n: u8) -> u16 { 1 << n } pub fn relative_to_1d(index: LodIndex, relative_size: Vec3) -> usize { let index = index.get(); - (index[0] + index[1] * relative_size[0] + index[2] * relative_size[0] * relative_size[1]) as usize + (index[0] * relative_size[2] * relative_size[1] + index[1] * relative_size[2] + index[2]) as usize } + +pub fn min(lhs: LodIndex, rhs: LodIndex) -> LodIndex { + let lhs = lhs.get(); + let rhs = rhs.get(); + LodIndex::new(lhs.map2(rhs, |a,b| cmp::min(a,b))) +} + +pub fn max(lhs: LodIndex, rhs: LodIndex) -> LodIndex { + let lhs = lhs.get(); + let rhs = rhs.get(); + LodIndex::new(lhs.map2(rhs, |a,b| cmp::max(a,b))) +} + diff --git a/worldsim/src/lodstore/mod.rs b/worldsim/src/lodstore/mod.rs index ee9ced14a5..dd79b1ce14 100644 --- a/worldsim/src/lodstore/mod.rs +++ b/worldsim/src/lodstore/mod.rs @@ -4,6 +4,7 @@ use std::collections::HashMap; use vek::*; use index::{ LodIndex, + AbsIndex, relative_to_1d, }; @@ -13,36 +14,8 @@ This is the structure to store a region and all subscribed information */ pub trait LayerInfo { fn get_child_index(&self) -> Option; - const layer_volume: Vec3; // e.g. (1|1|1) for l0 or (4|4|4) for l2 optimization - const child_layer_id: Option; - const child_len: usize; //number of childs on this layer, MUST BE 2^(SELF::child_dim*3) } -/* for dyn trait objects, not really fast, but faster to code, use as a makeshift solution only! */ -pub trait LayerInfoDyn { - fn get_child_index(&self) -> Option; - fn get_layer_volume(&self) -> Vec3; - fn get_child_layer_id(&self) -> Option; - fn get_child_len(&self) -> usize; -} - -impl LayerInfoDyn for L { - fn get_child_index(&self) -> Option { - self.get_child_index() - } - fn get_layer_volume(&self) -> Vec3 { - L::layer_volume - } - fn get_child_layer_id(&self) -> Option { - L::child_layer_id - } - fn get_child_len(&self) -> usize { - L::child_len - } -} - - - pub trait LodConfig { type L0: LayerInfo; // 2^-4 type L1: LayerInfo; @@ -63,9 +36,13 @@ pub trait LodConfig { const anchor_layer_id: u8; + const layer_volume: [Vec3; 16]; // e.g. (1|1|1) for l0 or (4|4|4) for l2 optimization + const child_layer_id: [Option; 16]; + const child_len: [usize; 16]; //number of childs on this layer, MUST BE 2^(SELF::child_dim*3) + fn setup(&mut self); - fn drill_down(data: &mut LodData::, level: u8, index: usize) where Self: Sized; - fn drill_up(data: &mut LodData::, level: u8, parent_index: usize) where Self: Sized; + fn drill_down(data: &mut LodData::, abs: AbsIndex) where Self: Sized; + fn drill_up(data: &mut LodData::, parent_abs: AbsIndex) where Self: Sized; } /* @@ -77,8 +54,6 @@ One could think about multiple entry levels for foreign and owned data, but that So for now we go with a single anchorlevel for now, and hope the designer chooses good levels */ -//ERROR NEXT STEP IS TO WORK ON SYSTEMS IN ORDER TO KNOW WHAT EXACTLY WE NEED. BUILD A FAKE "RASTERIZER" E:G: - #[derive(Debug, Clone)] pub struct LodData { pub layer0: Vec, // 1/16 @@ -124,1175 +99,325 @@ impl LodData } } - /* - Da fuq is his code you might ask, - but seriosly. because of logic reasons you have to know the level you want anyway, so we go for it ;) - - int_getN => if you know the parent and absolute index, as well as parents absolut index, i return you your child - */ - - fn int_get0(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L0 { - debug_assert_eq!(T::child_layer_id, X::L0::child_layer_id); - &self.layer0[relative_to_1d(index - parent_index, T::layer_volume)] - } - - fn int_get1(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L1 { - debug_assert_eq!(T::child_layer_id, X::L1::child_layer_id); - &self.layer1[relative_to_1d(index - parent_index, T::layer_volume)] - } - - fn int_get2(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L2 { - debug_assert_eq!(T::child_layer_id, X::L2::child_layer_id); - &self.layer2[relative_to_1d(index - parent_index, T::layer_volume)] - } - - fn int_get3(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L3 { - debug_assert_eq!(T::child_layer_id, X::L3::child_layer_id); - &self.layer3[relative_to_1d(index - parent_index, T::layer_volume)] - } - - fn int_get4(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L4 { - debug_assert_eq!(T::child_layer_id, X::L4::child_layer_id); - &self.layer4[relative_to_1d(index - parent_index, T::layer_volume)] - } - - fn int_get5(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L5 { - debug_assert_eq!(T::child_layer_id, X::L5::child_layer_id); - &self.layer5[relative_to_1d(index - parent_index, T::layer_volume)] - } - - fn int_get6(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L6 { - debug_assert_eq!(T::child_layer_id, X::L6::child_layer_id); - &self.layer6[relative_to_1d(index - parent_index, T::layer_volume)] - } - - fn int_get7(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L7 { - debug_assert_eq!(T::child_layer_id, X::L7::child_layer_id); - &self.layer7[relative_to_1d(index - parent_index, T::layer_volume)] - } - - fn int_get8(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L8 { - debug_assert_eq!(T::child_layer_id, X::L8::child_layer_id); - &self.layer8[relative_to_1d(index - parent_index, T::layer_volume)] - } - - fn int_get9(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L9 { - debug_assert_eq!(T::child_layer_id, X::L9::child_layer_id); - &self.layer9[relative_to_1d(index - parent_index, T::layer_volume)] - } - - fn int_get10(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L10 { - debug_assert_eq!(T::child_layer_id, X::L10::child_layer_id); - &self.layer10[relative_to_1d(index - parent_index, T::layer_volume)] - } - - fn int_get11(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L11 { - debug_assert_eq!(T::child_layer_id, X::L11::child_layer_id); - &self.layer11[relative_to_1d(index - parent_index, T::layer_volume)] - } - - fn int_get12(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L12 { - debug_assert_eq!(T::child_layer_id, X::L12::child_layer_id); - &self.layer12[relative_to_1d(index - parent_index, T::layer_volume)] - } - - fn int_get13(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L13 { - debug_assert_eq!(T::child_layer_id, X::L13::child_layer_id); - &self.layer13[relative_to_1d(index - parent_index, T::layer_volume)] - } - - fn int_get14(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L14 { - debug_assert_eq!(T::child_layer_id, X::L14::child_layer_id); - &self.layer14[relative_to_1d(index - parent_index, T::layer_volume)] - } - - /* - These matches are const evaluatable, hope for the optimizer - */ - - fn int_hop_get14(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L14 { - match T::child_layer_id { - Some(14) => { - self.int_get14(parent, index, parent_index) - }, - _ => unreachable!("wrong layer info"), + // dynamically dispatches the get_child_index, this is most prob the bottleneck function. + // evaluate the performacne impact!!! + fn int_get_child_index(&self, abs: AbsIndex) -> Option { + match abs.layer { + 0 => self.layer0[abs.index].get_child_index(), + 1 => self.layer1[abs.index].get_child_index(), + 2 => self.layer2[abs.index].get_child_index(), + 3 => self.layer3[abs.index].get_child_index(), + 4 => self.layer4[abs.index].get_child_index(), + 5 => self.layer5[abs.index].get_child_index(), + 6 => self.layer6[abs.index].get_child_index(), + 7 => self.layer7[abs.index].get_child_index(), + 8 => self.layer8[abs.index].get_child_index(), + 9 => self.layer9[abs.index].get_child_index(), + 10 => self.layer10[abs.index].get_child_index(), + 11 => self.layer11[abs.index].get_child_index(), + 12 => self.layer12[abs.index].get_child_index(), + 13 => self.layer13[abs.index].get_child_index(), + 14 => self.layer14[abs.index].get_child_index(), + 15 => self.layer15[abs.index].get_child_index(), + _ => panic!("wrong abs index"), } } - fn int_hop_get13(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L13 { - match T::child_layer_id { - Some(14) => { - let l14 = self.int_get14(parent, index, parent_index); - self.int_hop_get13(l14, index, parent_index) - }, - Some(13) => { - self.int_get13(parent, index, parent_index) - }, - _ => unreachable!("wrong layer info"), - } + // Returns the childs AbsIndex of Parent AbsIndex + // child_lod must lie within parent + // uses parent_lod as buffer, to not calculate it again + // uses parent_child_index as a buffer, to not calculate it again + fn int_get(parent_abs: AbsIndex, child_lod: LodIndex, parent_lod: LodIndex, parent_child_index: usize) -> AbsIndex { + let child_layer = X::child_layer_id[parent_abs.layer as usize].unwrap(); + let child_lod = child_lod.align_to_layer_id(child_layer); + let child_offset = relative_to_1d(child_lod - parent_lod, X::layer_volume[parent_abs.layer as usize]); + println!("int_get - parent_abs {} child_lod {} parent_lod {} parent_child_index {} child_offset {}", parent_abs, child_lod, parent_lod, parent_child_index, child_offset); + AbsIndex::new(child_layer, parent_child_index + child_offset) } - fn int_hop_get12(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L12 { - match T::child_layer_id { - Some(14) => { - let l14 = self.int_get14(parent, index, parent_index); - self.int_hop_get12(l14, index, parent_index) - }, - Some(13) => { - let l13 = self.int_get13(parent, index, parent_index); - self.int_hop_get12(l13, index, parent_index) - }, - Some(12) => { - self.int_get12(parent, index, parent_index) - }, - _ => unreachable!("wrong layer info"), - } + // slower variant of int_get which requiere self lookups + fn int_get_lockup(&self, parent_abs: AbsIndex, child_lod: LodIndex) -> AbsIndex { + let parent_lod = child_lod.align_to_layer_id(parent_abs.layer); + let parent_child_index = self.int_get_child_index(parent_abs).unwrap(); + Self::int_get(parent_abs, child_lod, parent_lod, parent_child_index) } - fn int_hop_get11(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L11 { - match T::child_layer_id { - Some(14) => { - let l14 = self.int_get14(parent, index, parent_index); - self.int_hop_get11(l14, index, parent_index) - }, - Some(13) => { - let l13 = self.int_get13(parent, index, parent_index); - self.int_hop_get11(l13, index, parent_index) - }, - Some(12) => { - let l12 = self.int_get12(parent, index, parent_index); - self.int_hop_get11(l12, index, parent_index) - }, - Some(11) => { - self.int_get11(parent, index, parent_index) - }, - _ => unreachable!("wrong layer info"), - } - } - - fn int_hop_get10(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L10 { - match T::child_layer_id { - Some(14) => { - let l14 = self.int_get14(parent, index, parent_index); - self.int_hop_get10(l14, index, parent_index) - }, - Some(13) => { - let l13 = self.int_get13(parent, index, parent_index); - self.int_hop_get10(l13, index, parent_index) - }, - Some(12) => { - let l12 = self.int_get12(parent, index, parent_index); - self.int_hop_get10(l12, index, parent_index) - }, - Some(11) => { - let l11 = self.int_get11(parent, index, parent_index); - self.int_hop_get10(l11, index, parent_index) - }, - Some(10) => { - self.int_get10(parent, index, parent_index) - }, - _ => unreachable!("wrong layer info"), - } - } - - fn int_hop_get9(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L9 { - match T::child_layer_id { - Some(14) => { - let l14 = self.int_get14(parent, index, parent_index); - self.int_hop_get9(l14, index, parent_index) - }, - Some(13) => { - let l13 = self.int_get13(parent, index, parent_index); - self.int_hop_get9(l13, index, parent_index) - }, - Some(12) => { - let l12 = self.int_get12(parent, index, parent_index); - self.int_hop_get9(l12, index, parent_index) - }, - Some(11) => { - let l11 = self.int_get11(parent, index, parent_index); - self.int_hop_get9(l11, index, parent_index) - }, - Some(10) => { - let l10 = self.int_get10(parent, index, parent_index); - self.int_hop_get9(l10, index, parent_index) - }, - Some(9) => { - self.int_get9(parent, index, parent_index) - }, - _ => unreachable!("wrong layer info"), - } - } - - fn int_hop_get8(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L8 { - match T::child_layer_id { - Some(14) => { - let l14 = self.int_get14(parent, index, parent_index); - self.int_hop_get8(l14, index, parent_index) - }, - Some(13) => { - let l13 = self.int_get13(parent, index, parent_index); - self.int_hop_get8(l13, index, parent_index) - }, - Some(12) => { - let l12 = self.int_get12(parent, index, parent_index); - self.int_hop_get8(l12, index, parent_index) - }, - Some(11) => { - let l11 = self.int_get11(parent, index, parent_index); - self.int_hop_get8(l11, index, parent_index) - }, - Some(10) => { - let l10 = self.int_get10(parent, index, parent_index); - self.int_hop_get8(l10, index, parent_index) - }, - Some(9) => { - let l9 = self.int_get9(parent, index, parent_index); - self.int_hop_get8(l9, index, parent_index) - }, - Some(8) => { - self.int_get8(parent, index, parent_index) - }, - _ => unreachable!("wrong layer info"), - } - } - - fn int_hop_get7(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L7 { - match T::child_layer_id { - Some(14) => { - let l14 = self.int_get14(parent, index, parent_index); - self.int_hop_get7(l14, index, parent_index) - }, - Some(13) => { - let l13 = self.int_get13(parent, index, parent_index); - self.int_hop_get7(l13, index, parent_index) - }, - Some(12) => { - let l12 = self.int_get12(parent, index, parent_index); - self.int_hop_get7(l12, index, parent_index) - }, - Some(11) => { - let l11 = self.int_get11(parent, index, parent_index); - self.int_hop_get7(l11, index, parent_index) - }, - Some(10) => { - let l10 = self.int_get10(parent, index, parent_index); - self.int_hop_get7(l10, index, parent_index) - }, - Some(9) => { - let l9 = self.int_get9(parent, index, parent_index); - self.int_hop_get7(l9, index, parent_index) - }, - Some(8) => { - let l8 = self.int_get8(parent, index, parent_index); - self.int_hop_get7(l8, index, parent_index) - }, - Some(7) => { - self.int_get7(parent, index, parent_index) - }, - _ => unreachable!("wrong layer info"), - } - } - - fn int_hop_get6(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L6 { - match T::child_layer_id { - Some(14) => { - let l14 = self.int_get14(parent, index, parent_index); - self.int_hop_get6(l14, index, parent_index) - }, - Some(13) => { - let l13 = self.int_get13(parent, index, parent_index); - self.int_hop_get6(l13, index, parent_index) - }, - Some(12) => { - let l12 = self.int_get12(parent, index, parent_index); - self.int_hop_get6(l12, index, parent_index) - }, - Some(11) => { - let l11 = self.int_get11(parent, index, parent_index); - self.int_hop_get6(l11, index, parent_index) - }, - Some(10) => { - let l10 = self.int_get10(parent, index, parent_index); - self.int_hop_get6(l10, index, parent_index) - }, - Some(9) => { - let l9 = self.int_get9(parent, index, parent_index); - self.int_hop_get6(l9, index, parent_index) - }, - Some(8) => { - let l8 = self.int_get8(parent, index, parent_index); - self.int_hop_get6(l8, index, parent_index) - }, - Some(7) => { - let l7 = self.int_get7(parent, index, parent_index); - self.int_hop_get6(l7, index, parent_index) - }, - Some(6) => { - self.int_get6(parent, index, parent_index) - }, - _ => unreachable!("wrong layer info"), - } - } - - fn int_hop_get5(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L5 { - match T::child_layer_id { - Some(14) => { - let l14 = self.int_get14(parent, index, parent_index); - self.int_hop_get5(l14, index, parent_index) - }, - Some(13) => { - let l13 = self.int_get13(parent, index, parent_index); - self.int_hop_get5(l13, index, parent_index) - }, - Some(12) => { - let l12 = self.int_get12(parent, index, parent_index); - self.int_hop_get5(l12, index, parent_index) - }, - Some(11) => { - let l11 = self.int_get11(parent, index, parent_index); - self.int_hop_get5(l11, index, parent_index) - }, - Some(10) => { - let l10 = self.int_get10(parent, index, parent_index); - self.int_hop_get5(l10, index, parent_index) - }, - Some(9) => { - let l9 = self.int_get9(parent, index, parent_index); - self.int_hop_get5(l9, index, parent_index) - }, - Some(8) => { - let l8 = self.int_get8(parent, index, parent_index); - self.int_hop_get5(l8, index, parent_index) - }, - Some(7) => { - let l7 = self.int_get7(parent, index, parent_index); - self.int_hop_get5(l7, index, parent_index) - }, - Some(6) => { - let l6 = self.int_get6(parent, index, parent_index); - self.int_hop_get5(l6, index, parent_index) - }, - Some(5) => { - self.int_get5(parent, index, parent_index) - }, - _ => unreachable!("wrong layer info"), - } - } - - fn int_hop_get4(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L4 { - match T::child_layer_id { - Some(14) => { - let l14 = self.int_get14(parent, index, parent_index); - self.int_hop_get4(l14, index, parent_index) - }, - Some(13) => { - let l13 = self.int_get13(parent, index, parent_index); - self.int_hop_get4(l13, index, parent_index) - }, - Some(12) => { - let l12 = self.int_get12(parent, index, parent_index); - self.int_hop_get4(l12, index, parent_index) - }, - Some(11) => { - let l11 = self.int_get11(parent, index, parent_index); - self.int_hop_get4(l11, index, parent_index) - }, - Some(10) => { - let l10 = self.int_get10(parent, index, parent_index); - self.int_hop_get4(l10, index, parent_index) - }, - Some(9) => { - let l9 = self.int_get9(parent, index, parent_index); - self.int_hop_get4(l9, index, parent_index) - }, - Some(8) => { - let l8 = self.int_get8(parent, index, parent_index); - self.int_hop_get4(l8, index, parent_index) - }, - Some(7) => { - let l7 = self.int_get7(parent, index, parent_index); - self.int_hop_get4(l7, index, parent_index) - }, - Some(6) => { - let l6 = self.int_get6(parent, index, parent_index); - self.int_hop_get4(l6, index, parent_index) - }, - Some(5) => { - let l5 = self.int_get5(parent, index, parent_index); - self.int_hop_get4(l5, index, parent_index) - }, - Some(4) => { - self.int_get4(parent, index, parent_index) - }, - _ => unreachable!("wrong layer info"), - } - } - - fn int_hop_get3(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L3 { - match T::child_layer_id { - Some(14) => { - let l14 = self.int_get14(parent, index, parent_index); - self.int_hop_get3(l14, index, parent_index) - }, - Some(13) => { - let l13 = self.int_get13(parent, index, parent_index); - self.int_hop_get3(l13, index, parent_index) - }, - Some(12) => { - let l12 = self.int_get12(parent, index, parent_index); - self.int_hop_get3(l12, index, parent_index) - }, - Some(11) => { - let l11 = self.int_get11(parent, index, parent_index); - self.int_hop_get3(l11, index, parent_index) - }, - Some(10) => { - let l10 = self.int_get10(parent, index, parent_index); - self.int_hop_get3(l10, index, parent_index) - }, - Some(9) => { - let l9 = self.int_get9(parent, index, parent_index); - self.int_hop_get3(l9, index, parent_index) - }, - Some(8) => { - let l8 = self.int_get8(parent, index, parent_index); - self.int_hop_get3(l8, index, parent_index) - }, - Some(7) => { - let l7 = self.int_get7(parent, index, parent_index); - self.int_hop_get3(l7, index, parent_index) - }, - Some(6) => { - let l6 = self.int_get6(parent, index, parent_index); - self.int_hop_get3(l6, index, parent_index) + // target_layer is requiered because same LodIndex can exist for multiple layers, and guessing is stupid here + fn int_recursive_get(&self, parent_abs: AbsIndex, child_lod: LodIndex, target_layer:u8) -> AbsIndex { + let mut parent_abs = parent_abs; + while true { + println!("int_recursive_get {} - {}", parent_abs, target_layer); + parent_abs = self.int_get_lockup(parent_abs, child_lod); + if parent_abs.layer <= target_layer { + return parent_abs; } - Some(5) => { - let l5 = self.int_get5(parent, index, parent_index); - self.int_hop_get3(l5, index, parent_index) - }, - Some(4) => { - let l4 = self.int_get4(parent, index, parent_index); - self.int_hop_get3(l4, index, parent_index) - }, - Some(3) => { - self.int_get3(parent, index, parent_index) - }, - _ => unreachable!("wrong layer info"), } + unreachable!(); } - fn int_hop_get2(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L2 { - match T::child_layer_id { - Some(14) => { - let l14 = self.int_get14(parent, index, parent_index); - self.int_hop_get2(l14, index, parent_index) - }, - Some(13) => { - let l13 = self.int_get13(parent, index, parent_index); - self.int_hop_get2(l13, index, parent_index) - }, - Some(12) => { - let l12 = self.int_get12(parent, index, parent_index); - self.int_hop_get2(l12, index, parent_index) - }, - Some(11) => { - let l11 = self.int_get11(parent, index, parent_index); - self.int_hop_get2(l11, index, parent_index) - }, - Some(10) => { - let l10 = self.int_get10(parent, index, parent_index); - self.int_hop_get2(l10, index, parent_index) - }, - Some(9) => { - let l9 = self.int_get9(parent, index, parent_index); - self.int_hop_get2(l9, index, parent_index) - }, - Some(8) => { - let l8 = self.int_get8(parent, index, parent_index); - self.int_hop_get2(l8, index, parent_index) - }, - Some(7) => { - let l7 = self.int_get7(parent, index, parent_index); - self.int_hop_get2(l7, index, parent_index) - }, - Some(6) => { - let l6 = self.int_get6(parent, index, parent_index); - self.int_hop_get2(l6, index, parent_index) - }, - Some(5) => { - let l6 = self.int_get6(parent, index, parent_index); - self.int_hop_get2(l6, index, parent_index) - }, - Some(4) => { - let l6 = self.int_get6(parent, index, parent_index); - self.int_hop_get2(l6, index, parent_index) - }, - Some(3) => { - let l6 = self.int_get6(parent, index, parent_index); - self.int_hop_get2(l6, index, parent_index) - }, - Some(2) => { - self.int_get2(parent, index, parent_index) - }, - _ => unreachable!("wrong layer info"), - } - } - - fn int_hop_get1(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L1 { - match T::child_layer_id { - Some(14) => { - let l14 = self.int_get14(parent, index, parent_index); - self.int_hop_get1(l14, index, parent_index) - }, - Some(13) => { - let l13 = self.int_get13(parent, index, parent_index); - self.int_hop_get1(l13, index, parent_index) - }, - Some(12) => { - let l12 = self.int_get12(parent, index, parent_index); - self.int_hop_get1(l12, index, parent_index) - }, - Some(11) => { - let l11 = self.int_get11(parent, index, parent_index); - self.int_hop_get1(l11, index, parent_index) - }, - Some(10) => { - let l10 = self.int_get10(parent, index, parent_index); - self.int_hop_get1(l10, index, parent_index) - }, - Some(9) => { - let l9 = self.int_get9(parent, index, parent_index); - self.int_hop_get1(l9, index, parent_index) - }, - Some(8) => { - let l8 = self.int_get8(parent, index, parent_index); - self.int_hop_get1(l8, index, parent_index) - }, - Some(7) => { - let l7 = self.int_get7(parent, index, parent_index); - self.int_hop_get1(l7, index, parent_index) - }, - Some(6) => { - let l6 = self.int_get6(parent, index, parent_index); - self.int_hop_get1(l6, index, parent_index) - }, - Some(5) => { - let l5 = self.int_get5(parent, index, parent_index); - self.int_hop_get1(l5, index, parent_index) - }, - Some(4) => { - let l4 = self.int_get4(parent, index, parent_index); - self.int_hop_get1(l4, index, parent_index) - }, - Some(3) => { - let l3 = self.int_get3(parent, index, parent_index); - self.int_hop_get1(l3, index, parent_index) - }, - Some(2) => { - let l2 = self.int_get2(parent, index, parent_index); - self.int_hop_get1(l2, index, parent_index) - }, - Some(1) => { - self.int_get1(parent, index, parent_index) - }, - _ => unreachable!("wrong layer info"), - } - } - - fn int_hop_get0(&self, parent: &T, index: LodIndex, parent_index: LodIndex) -> &X::L0 { - match T::child_layer_id { - Some(14) => { - let l14 = self.int_get14(parent, index, parent_index); - self.int_hop_get0(l14, index, parent_index) - }, - Some(13) => { - let l13 = self.int_get13(parent, index, parent_index); - self.int_hop_get0(l13, index, parent_index) - }, - Some(12) => { - let l12 = self.int_get12(parent, index, parent_index); - self.int_hop_get0(l12, index, parent_index) - }, - Some(11) => { - let l11 = self.int_get11(parent, index, parent_index); - self.int_hop_get0(l11, index, parent_index) - }, - Some(10) => { - let l10 = self.int_get10(parent, index, parent_index); - self.int_hop_get0(l10, index, parent_index) - }, - Some(9) => { - let l9 = self.int_get9(parent, index, parent_index); - self.int_hop_get0(l9, index, parent_index) - }, - Some(8) => { - let l8 = self.int_get8(parent, index, parent_index); - self.int_hop_get0(l8, index, parent_index) - }, - Some(7) => { - let l7 = self.int_get7(parent, index, parent_index); - self.int_hop_get0(l7, index, parent_index) - }, - Some(6) => { - let l6 = self.int_get6(parent, index, parent_index); - self.int_hop_get0(l6, index, parent_index) - }, - Some(5) => { - let l5 = self.int_get5(parent, index, parent_index); - self.int_hop_get0(l5, index, parent_index) - }, - Some(4) => { - let l4 = self.int_get4(parent, index, parent_index); - self.int_hop_get0(l4, index, parent_index) - }, - Some(3) => { - let l3 = self.int_get3(parent, index, parent_index); - self.int_hop_get0(l3, index, parent_index) - }, - Some(2) => { - let l2 = self.int_get2(parent, index, parent_index); - self.int_hop_get0(l2, index, parent_index) - }, - Some(1) => { - let l1 = self.int_get1(parent, index, parent_index); - self.int_hop_get0(l1, index, parent_index) - }, - Some(0) => { - self.int_get0(parent, index, parent_index) - }, - _ => unreachable!("wrong layer info"), - } - } - - - - - - pub fn get15(&self, index: LodIndex) -> &X::L15 { - debug_assert!(Some(X::anchor_layer_id) > X::L15::child_layer_id); - &self.layer15[0] + let anchor_lod = index.align_to_layer_id(X::anchor_layer_id); + let anchor_abs = AbsIndex::new(X::anchor_layer_id, self.anchor[&anchor_lod]); + let wanted_abs = self.int_recursive_get(anchor_abs, index, 15); + debug_assert_eq!(wanted_abs.layer, 15); + &self.layer15[wanted_abs.index] } pub fn get14(&self, index: LodIndex) -> &X::L14 { - if Some(X::anchor_layer_id) == X::L15::child_layer_id { - let l15 = self.get15(index); - self.int_hop_get14(l15, index, index) - } else { - debug_assert!(Some(X::anchor_layer_id) > X::L14::child_layer_id); - &self.layer14[0] - } + let anchor_lod = index.align_to_layer_id(X::anchor_layer_id); + let anchor_abs = AbsIndex::new(X::anchor_layer_id, self.anchor[&anchor_lod]); + let wanted_abs = self.int_recursive_get(anchor_abs, index, 14); + debug_assert_eq!(wanted_abs.layer, 14); + &self.layer14[wanted_abs.index] } pub fn get13(&self, index: LodIndex) -> &X::L13 { - if Some(X::anchor_layer_id) == X::L15::child_layer_id { - let l15 = self.get15(index); - self.int_hop_get13(l15, index, index) - } else if Some(X::anchor_layer_id) == X::L14::child_layer_id { - let l14 = self.get14(index); - self.int_hop_get13(l14, index, index) - } else { - debug_assert!(Some(X::anchor_layer_id) > X::L13::child_layer_id); - &self.layer13[0] - } + let anchor_lod = index.align_to_layer_id(X::anchor_layer_id); + let anchor_abs = AbsIndex::new(X::anchor_layer_id, self.anchor[&anchor_lod]); + let wanted_abs = self.int_recursive_get(anchor_abs, index, 13); + debug_assert_eq!(wanted_abs.layer, 13); + &self.layer13[wanted_abs.index] } pub fn get12(&self, index: LodIndex) -> &X::L12 { - if Some(X::anchor_layer_id) == X::L15::child_layer_id { - let l15 = self.get15(index); - self.int_hop_get12(l15, index, index) - } else if Some(X::anchor_layer_id) == X::L14::child_layer_id { - let l14 = self.get14(index); - self.int_hop_get12(l14, index, index) - } else if Some(X::anchor_layer_id) == X::L13::child_layer_id { - let l13 = self.get13(index); - self.int_hop_get12(l13, index, index) - } else { - debug_assert!(Some(X::anchor_layer_id) > X::L12::child_layer_id); - &self.layer12[0] - } + let anchor_lod = index.align_to_layer_id(X::anchor_layer_id); + let anchor_abs = AbsIndex::new(X::anchor_layer_id, self.anchor[&anchor_lod]); + let wanted_abs = self.int_recursive_get(anchor_abs, index, 12); + debug_assert_eq!(wanted_abs.layer, 12); + &self.layer12[wanted_abs.index] } pub fn get11(&self, index: LodIndex) -> &X::L11 { - if Some(X::anchor_layer_id) == X::L15::child_layer_id { - let l15 = self.get15(index); - self.int_hop_get11(l15, index, index) - } else if Some(X::anchor_layer_id) == X::L14::child_layer_id { - let l14 = self.get14(index); - self.int_hop_get11(l14, index, index) - } else if Some(X::anchor_layer_id) == X::L13::child_layer_id { - let l13 = self.get13(index); - self.int_hop_get11(l13, index, index) - } else if Some(X::anchor_layer_id) == X::L12::child_layer_id { - let l12 = self.get12(index); - self.int_hop_get11(l12, index, index) - } else { - debug_assert!(Some(X::anchor_layer_id) > X::L11::child_layer_id); - &self.layer11[0] - } + let anchor_lod = index.align_to_layer_id(X::anchor_layer_id); + let anchor_abs = AbsIndex::new(X::anchor_layer_id, self.anchor[&anchor_lod]); + let wanted_abs = self.int_recursive_get(anchor_abs, index, 11); + debug_assert_eq!(wanted_abs.layer, 11); + &self.layer11[wanted_abs.index] } pub fn get10(&self, index: LodIndex) -> &X::L10 { - if Some(X::anchor_layer_id) == X::L15::child_layer_id { - let l15 = self.get15(index); - self.int_hop_get10(l15, index, index) - } else if Some(X::anchor_layer_id) == X::L14::child_layer_id { - let l14 = self.get14(index); - self.int_hop_get10(l14, index, index) - } else if Some(X::anchor_layer_id) == X::L13::child_layer_id { - let l13 = self.get13(index); - self.int_hop_get10(l13, index, index) - } else if Some(X::anchor_layer_id) == X::L12::child_layer_id { - let l12 = self.get12(index); - self.int_hop_get10(l12, index, index) - } else if Some(X::anchor_layer_id) == X::L11::child_layer_id { - let l11 = self.get11(index); - self.int_hop_get10(l11, index, index) - } else { - debug_assert!(Some(X::anchor_layer_id) > X::L10::child_layer_id); - &self.layer10[0] - } + let anchor_lod = index.align_to_layer_id(X::anchor_layer_id); + let anchor_abs = AbsIndex::new(X::anchor_layer_id, self.anchor[&anchor_lod]); + let wanted_abs = self.int_recursive_get(anchor_abs, index, 10); + debug_assert_eq!(wanted_abs.layer, 10); + &self.layer10[wanted_abs.index] } pub fn get9(&self, index: LodIndex) -> &X::L9 { - if Some(X::anchor_layer_id) == X::L15::child_layer_id { - let l15 = self.get15(index); - self.int_hop_get9(l15, index, index) - } else if Some(X::anchor_layer_id) == X::L14::child_layer_id { - let l14 = self.get14(index); - self.int_hop_get9(l14, index, index) - } else if Some(X::anchor_layer_id) == X::L13::child_layer_id { - let l13 = self.get13(index); - self.int_hop_get9(l13, index, index) - } else if Some(X::anchor_layer_id) == X::L12::child_layer_id { - let l12 = self.get12(index); - self.int_hop_get9(l12, index, index) - } else if Some(X::anchor_layer_id) == X::L11::child_layer_id { - let l11 = self.get11(index); - self.int_hop_get9(l11, index, index) - } else if Some(X::anchor_layer_id) == X::L10::child_layer_id { - let l10 = self.get10(index); - self.int_hop_get9(l10, index, index) - } else { - debug_assert!(Some(X::anchor_layer_id) > X::L9::child_layer_id); - &self.layer9[0] - } + let anchor_lod = index.align_to_layer_id(X::anchor_layer_id); + let anchor_abs = AbsIndex::new(X::anchor_layer_id, self.anchor[&anchor_lod]); + let wanted_abs = self.int_recursive_get(anchor_abs, index, 9); + debug_assert_eq!(wanted_abs.layer, 9); + &self.layer9[wanted_abs.index] } pub fn get8(&self, index: LodIndex) -> &X::L8 { - if Some(X::anchor_layer_id) == X::L15::child_layer_id { - let l15 = self.get15(index); - self.int_hop_get8(l15, index, index) - } else if Some(X::anchor_layer_id) == X::L14::child_layer_id { - let l14 = self.get14(index); - self.int_hop_get8(l14, index, index) - } else if Some(X::anchor_layer_id) == X::L13::child_layer_id { - let l13 = self.get13(index); - self.int_hop_get8(l13, index, index) - } else if Some(X::anchor_layer_id) == X::L12::child_layer_id { - let l12 = self.get12(index); - self.int_hop_get8(l12, index, index) - } else if Some(X::anchor_layer_id) == X::L11::child_layer_id { - let l11 = self.get11(index); - self.int_hop_get8(l11, index, index) - } else if Some(X::anchor_layer_id) == X::L10::child_layer_id { - let l10 = self.get10(index); - self.int_hop_get8(l10, index, index) - } else if Some(X::anchor_layer_id) == X::L9::child_layer_id { - let l9 = self.get9(index); - self.int_hop_get8(l9, index, index) - } else { - debug_assert!(Some(X::anchor_layer_id) > X::L8::child_layer_id); - &self.layer8[0] - } + let anchor_lod = index.align_to_layer_id(X::anchor_layer_id); + let anchor_abs = AbsIndex::new(X::anchor_layer_id, self.anchor[&anchor_lod]); + let wanted_abs = self.int_recursive_get(anchor_abs, index, 8); + debug_assert_eq!(wanted_abs.layer, 8); + &self.layer8[wanted_abs.index] } pub fn get7(&self, index: LodIndex) -> &X::L7 { - if Some(X::anchor_layer_id) == X::L15::child_layer_id { - let l15 = self.get15(index); - self.int_hop_get7(l15, index, index) - } else if Some(X::anchor_layer_id) == X::L14::child_layer_id { - let l14 = self.get14(index); - self.int_hop_get7(l14, index, index) - } else if Some(X::anchor_layer_id) == X::L13::child_layer_id { - let l13 = self.get13(index); - self.int_hop_get7(l13, index, index) - } else if Some(X::anchor_layer_id) == X::L12::child_layer_id { - let l12 = self.get12(index); - self.int_hop_get7(l12, index, index) - } else if Some(X::anchor_layer_id) == X::L11::child_layer_id { - let l11 = self.get11(index); - self.int_hop_get7(l11, index, index) - } else if Some(X::anchor_layer_id) == X::L10::child_layer_id { - let l10 = self.get10(index); - self.int_hop_get7(l10, index, index) - } else if Some(X::anchor_layer_id) == X::L9::child_layer_id { - let l9 = self.get9(index); - self.int_hop_get7(l9, index, index) - } else if Some(X::anchor_layer_id) == X::L8::child_layer_id { - let l8 = self.get8(index); - self.int_hop_get7(l8, index, index) - } else { - debug_assert!(Some(X::anchor_layer_id) > X::L7::child_layer_id); - &self.layer7[0] - } + let anchor_lod = index.align_to_layer_id(X::anchor_layer_id); + let anchor_abs = AbsIndex::new(X::anchor_layer_id, self.anchor[&anchor_lod]); + let wanted_abs = self.int_recursive_get(anchor_abs, index, 7); + debug_assert_eq!(wanted_abs.layer, 7); + &self.layer7[wanted_abs.index] } pub fn get6(&self, index: LodIndex) -> &X::L6 { - if Some(X::anchor_layer_id) == X::L15::child_layer_id { - let l15 = self.get15(index); - self.int_hop_get6(l15, index, index) - } else if Some(X::anchor_layer_id) == X::L14::child_layer_id { - let l14 = self.get14(index); - self.int_hop_get6(l14, index, index) - } else if Some(X::anchor_layer_id) == X::L13::child_layer_id { - let l13 = self.get13(index); - self.int_hop_get6(l13, index, index) - } else if Some(X::anchor_layer_id) == X::L12::child_layer_id { - let l12 = self.get12(index); - self.int_hop_get6(l12, index, index) - } else if Some(X::anchor_layer_id) == X::L11::child_layer_id { - let l11 = self.get11(index); - self.int_hop_get6(l11, index, index) - } else if Some(X::anchor_layer_id) == X::L10::child_layer_id { - let l10 = self.get10(index); - self.int_hop_get6(l10, index, index) - } else if Some(X::anchor_layer_id) == X::L9::child_layer_id { - let l9 = self.get9(index); - self.int_hop_get6(l9, index, index) - } else if Some(X::anchor_layer_id) == X::L8::child_layer_id { - let l8 = self.get8(index); - self.int_hop_get6(l8, index, index) - } else if Some(X::anchor_layer_id) == X::L7::child_layer_id { - let l7 = self.get7(index); - self.int_hop_get6(l7, index, index) - } else { - debug_assert!(Some(X::anchor_layer_id) > X::L6::child_layer_id); - &self.layer6[0] - } + let anchor_lod = index.align_to_layer_id(X::anchor_layer_id); + let anchor_abs = AbsIndex::new(X::anchor_layer_id, self.anchor[&anchor_lod]); + let wanted_abs = self.int_recursive_get(anchor_abs, index, 6); + debug_assert_eq!(wanted_abs.layer, 6); + &self.layer6[wanted_abs.index] } pub fn get5(&self, index: LodIndex) -> &X::L5 { - if Some(X::anchor_layer_id) == X::L15::child_layer_id { - let l15 = self.get15(index); - self.int_hop_get5(l15, index, index) - } else if Some(X::anchor_layer_id) == X::L14::child_layer_id { - let l14 = self.get14(index); - self.int_hop_get5(l14, index, index) - } else if Some(X::anchor_layer_id) == X::L13::child_layer_id { - let l13 = self.get13(index); - self.int_hop_get5(l13, index, index) - } else if Some(X::anchor_layer_id) == X::L12::child_layer_id { - let l12 = self.get12(index); - self.int_hop_get5(l12, index, index) - } else if Some(X::anchor_layer_id) == X::L11::child_layer_id { - let l11 = self.get11(index); - self.int_hop_get5(l11, index, index) - } else if Some(X::anchor_layer_id) == X::L10::child_layer_id { - let l10 = self.get10(index); - self.int_hop_get5(l10, index, index) - } else if Some(X::anchor_layer_id) == X::L9::child_layer_id { - let l9 = self.get9(index); - self.int_hop_get5(l9, index, index) - } else if Some(X::anchor_layer_id) == X::L8::child_layer_id { - let l8 = self.get8(index); - self.int_hop_get5(l8, index, index) - } else if Some(X::anchor_layer_id) == X::L7::child_layer_id { - let l7 = self.get7(index); - self.int_hop_get5(l7, index, index) - } else if Some(X::anchor_layer_id) == X::L6::child_layer_id { - let l6 = self.get6(index); - self.int_hop_get5(l6, index, index) - } else { - debug_assert!(Some(X::anchor_layer_id) > X::L5::child_layer_id); - &self.layer5[0] - } + let anchor_lod = index.align_to_layer_id(X::anchor_layer_id); + let anchor_abs = AbsIndex::new(X::anchor_layer_id, self.anchor[&anchor_lod]); + let wanted_abs = self.int_recursive_get(anchor_abs, index, 5); + debug_assert_eq!(wanted_abs.layer, 5); + &self.layer5[wanted_abs.index] } pub fn get4(&self, index: LodIndex) -> &X::L4 { - if Some(X::anchor_layer_id) == X::L15::child_layer_id { - let l15 = self.get15(index); - self.int_hop_get4(l15, index, index) - } else if Some(X::anchor_layer_id) == X::L14::child_layer_id { - let l14 = self.get14(index); - self.int_hop_get4(l14, index, index) - } else if Some(X::anchor_layer_id) == X::L13::child_layer_id { - let l13 = self.get13(index); - self.int_hop_get4(l13, index, index) - } else if Some(X::anchor_layer_id) == X::L12::child_layer_id { - let l12 = self.get12(index); - self.int_hop_get4(l12, index, index) - } else if Some(X::anchor_layer_id) == X::L11::child_layer_id { - let l11 = self.get11(index); - self.int_hop_get4(l11, index, index) - } else if Some(X::anchor_layer_id) == X::L10::child_layer_id { - let l10 = self.get10(index); - self.int_hop_get4(l10, index, index) - } else if Some(X::anchor_layer_id) == X::L9::child_layer_id { - let l9 = self.get9(index); - self.int_hop_get4(l9, index, index) - } else if Some(X::anchor_layer_id) == X::L8::child_layer_id { - let l8 = self.get8(index); - self.int_hop_get4(l8, index, index) - } else if Some(X::anchor_layer_id) == X::L7::child_layer_id { - let l7 = self.get7(index); - self.int_hop_get4(l7, index, index) - } else if Some(X::anchor_layer_id) == X::L6::child_layer_id { - let l6 = self.get6(index); - self.int_hop_get4(l6, index, index) - } else if Some(X::anchor_layer_id) == X::L5::child_layer_id { - let l5 = self.get5(index); - self.int_hop_get4(l5, index, index) - } else { - debug_assert!(Some(X::anchor_layer_id) > X::L4::child_layer_id); - &self.layer4[0] - } + let anchor_lod = index.align_to_layer_id(X::anchor_layer_id); + let anchor_abs = AbsIndex::new(X::anchor_layer_id, self.anchor[&anchor_lod]); + println!("4aa4 {:?} - {:?}", anchor_lod, anchor_abs ); + let wanted_abs = self.int_recursive_get(anchor_abs, index, 4); + debug_assert_eq!(wanted_abs.layer, 4); + &self.layer4[wanted_abs.index] } pub fn get3(&self, index: LodIndex) -> &X::L3 { - if Some(X::anchor_layer_id) == X::L15::child_layer_id { - let l15 = self.get15(index); - self.int_hop_get3(l15, index, index) - } else if Some(X::anchor_layer_id) == X::L14::child_layer_id { - let l14 = self.get14(index); - self.int_hop_get3(l14, index, index) - } else if Some(X::anchor_layer_id) == X::L13::child_layer_id { - let l13 = self.get13(index); - self.int_hop_get3(l13, index, index) - } else if Some(X::anchor_layer_id) == X::L12::child_layer_id { - let l12 = self.get12(index); - self.int_hop_get3(l12, index, index) - } else if Some(X::anchor_layer_id) == X::L11::child_layer_id { - let l11 = self.get11(index); - self.int_hop_get3(l11, index, index) - } else if Some(X::anchor_layer_id) == X::L10::child_layer_id { - let l10 = self.get10(index); - self.int_hop_get3(l10, index, index) - } else if Some(X::anchor_layer_id) == X::L9::child_layer_id { - let l9 = self.get9(index); - self.int_hop_get3(l9, index, index) - } else if Some(X::anchor_layer_id) == X::L8::child_layer_id { - let l8 = self.get8(index); - self.int_hop_get3(l8, index, index) - } else if Some(X::anchor_layer_id) == X::L7::child_layer_id { - let l7 = self.get7(index); - self.int_hop_get3(l7, index, index) - } else if Some(X::anchor_layer_id) == X::L6::child_layer_id { - let l6 = self.get6(index); - self.int_hop_get3(l6, index, index) - } else if Some(X::anchor_layer_id) == X::L5::child_layer_id { - let l5 = self.get5(index); - self.int_hop_get3(l5, index, index) - } else if Some(X::anchor_layer_id) == X::L4::child_layer_id { - let l4 = self.get4(index); - self.int_hop_get3(l4, index, index) - } else { - debug_assert!(Some(X::anchor_layer_id) > X::L3::child_layer_id); - &self.layer3[0] - } + let anchor_lod = index.align_to_layer_id(X::anchor_layer_id); + let anchor_abs = AbsIndex::new(X::anchor_layer_id, self.anchor[&anchor_lod]); + let wanted_abs = self.int_recursive_get(anchor_abs, index, 3); + debug_assert_eq!(wanted_abs.layer, 3); + &self.layer3[wanted_abs.index] } pub fn get2(&self, index: LodIndex) -> &X::L2 { - if Some(X::anchor_layer_id) == X::L15::child_layer_id { - let l15 = self.get15(index); - self.int_hop_get2(l15, index, index) - } else if Some(X::anchor_layer_id) == X::L14::child_layer_id { - let l14 = self.get14(index); - self.int_hop_get2(l14, index, index) - } else if Some(X::anchor_layer_id) == X::L13::child_layer_id { - let l13 = self.get13(index); - self.int_hop_get2(l13, index, index) - } else if Some(X::anchor_layer_id) == X::L12::child_layer_id { - let l12 = self.get12(index); - self.int_hop_get2(l12, index, index) - } else if Some(X::anchor_layer_id) == X::L11::child_layer_id { - let l11 = self.get11(index); - self.int_hop_get2(l11, index, index) - } else if Some(X::anchor_layer_id) == X::L10::child_layer_id { - let l10 = self.get10(index); - self.int_hop_get2(l10, index, index) - } else if Some(X::anchor_layer_id) == X::L9::child_layer_id { - let l9 = self.get9(index); - self.int_hop_get2(l9, index, index) - } else if Some(X::anchor_layer_id) == X::L8::child_layer_id { - let l8 = self.get8(index); - self.int_hop_get2(l8, index, index) - } else if Some(X::anchor_layer_id) == X::L7::child_layer_id { - let l7 = self.get7(index); - self.int_hop_get2(l7, index, index) - } else if Some(X::anchor_layer_id) == X::L6::child_layer_id { - let l6 = self.get6(index); - self.int_hop_get2(l6, index, index) - } else if Some(X::anchor_layer_id) == X::L5::child_layer_id { - let l5 = self.get5(index); - self.int_hop_get2(l5, index, index) - } else if Some(X::anchor_layer_id) == X::L4::child_layer_id { - let l4 = self.get4(index); - self.int_hop_get2(l4, index, index) - } else if Some(X::anchor_layer_id) == X::L3::child_layer_id { - let l3 = self.get3(index); - self.int_hop_get2(l3, index, index) - } else { - debug_assert!(Some(X::anchor_layer_id) > X::L2::child_layer_id); - &self.layer2[0] - } + let anchor_lod = index.align_to_layer_id(X::anchor_layer_id); + let anchor_abs = AbsIndex::new(X::anchor_layer_id, self.anchor[&anchor_lod]); + let wanted_abs = self.int_recursive_get(anchor_abs, index, 2); + debug_assert_eq!(wanted_abs.layer, 2); + &self.layer2[wanted_abs.index] } pub fn get1(&self, index: LodIndex) -> &X::L1 { - if Some(X::anchor_layer_id) == X::L15::child_layer_id { - let l15 = self.get15(index); - self.int_hop_get1(l15, index, index) - } else if Some(X::anchor_layer_id) == X::L14::child_layer_id { - let l14 = self.get14(index); - self.int_hop_get1(l14, index, index) - } else if Some(X::anchor_layer_id) == X::L13::child_layer_id { - let l13 = self.get13(index); - self.int_hop_get1(l13, index, index) - } else if Some(X::anchor_layer_id) == X::L12::child_layer_id { - let l12 = self.get12(index); - self.int_hop_get1(l12, index, index) - } else if Some(X::anchor_layer_id) == X::L11::child_layer_id { - let l11 = self.get11(index); - self.int_hop_get1(l11, index, index) - } else if Some(X::anchor_layer_id) == X::L10::child_layer_id { - let l10 = self.get10(index); - self.int_hop_get1(l10, index, index) - } else if Some(X::anchor_layer_id) == X::L9::child_layer_id { - let l9 = self.get9(index); - self.int_hop_get1(l9, index, index) - } else if Some(X::anchor_layer_id) == X::L8::child_layer_id { - let l8 = self.get8(index); - self.int_hop_get1(l8, index, index) - } else if Some(X::anchor_layer_id) == X::L7::child_layer_id { - let l7 = self.get7(index); - self.int_hop_get1(l7, index, index) - } else if Some(X::anchor_layer_id) == X::L6::child_layer_id { - let l6 = self.get6(index); - self.int_hop_get1(l6, index, index) - } else if Some(X::anchor_layer_id) == X::L5::child_layer_id { - let l5 = self.get5(index); - self.int_hop_get1(l5, index, index) - } else if Some(X::anchor_layer_id) == X::L4::child_layer_id { - let l4 = self.get4(index); - self.int_hop_get1(l4, index, index) - } else if Some(X::anchor_layer_id) == X::L3::child_layer_id { - let l3 = self.get3(index); - self.int_hop_get1(l3, index, index) - } else if Some(X::anchor_layer_id) == X::L2::child_layer_id { - let l2 = self.get2(index); - self.int_hop_get1(l2, index, index) - } else { - debug_assert!(Some(X::anchor_layer_id) > X::L1::child_layer_id); - &self.layer1[0] - } + let anchor_lod = index.align_to_layer_id(X::anchor_layer_id); + let anchor_abs = AbsIndex::new(X::anchor_layer_id, self.anchor[&anchor_lod]); + let wanted_abs = self.int_recursive_get(anchor_abs, index, 1); + debug_assert_eq!(wanted_abs.layer, 1); + &self.layer1[wanted_abs.index] } pub fn get0(&self, index: LodIndex) -> &X::L0 { - if Some(X::anchor_layer_id) == X::L15::child_layer_id { - let l15 = self.get15(index); - self.int_hop_get0(l15, index, index) - } else if Some(X::anchor_layer_id) == X::L14::child_layer_id { - let l14 = self.get14(index); - self.int_hop_get0(l14, index, index) - } else if Some(X::anchor_layer_id) == X::L13::child_layer_id { - let l13 = self.get13(index); - self.int_hop_get0(l13, index, index) - } else if Some(X::anchor_layer_id) == X::L12::child_layer_id { - let l12 = self.get12(index); - self.int_hop_get0(l12, index, index) - } else if Some(X::anchor_layer_id) == X::L11::child_layer_id { - let l11 = self.get11(index); - self.int_hop_get0(l11, index, index) - } else if Some(X::anchor_layer_id) == X::L10::child_layer_id { - let l10 = self.get10(index); - self.int_hop_get0(l10, index, index) - } else if Some(X::anchor_layer_id) == X::L9::child_layer_id { - let l9 = self.get9(index); - self.int_hop_get0(l9, index, index) - } else if Some(X::anchor_layer_id) == X::L8::child_layer_id { - let l8 = self.get8(index); - self.int_hop_get0(l8, index, index) - } else if Some(X::anchor_layer_id) == X::L7::child_layer_id { - let l7 = self.get7(index); - self.int_hop_get0(l7, index, index) - } else if Some(X::anchor_layer_id) == X::L6::child_layer_id { - let l6 = self.get6(index); - self.int_hop_get0(l6, index, index) - } else if Some(X::anchor_layer_id) == X::L5::child_layer_id { - let l5 = self.get5(index); - self.int_hop_get0(l5, index, index) - } else if Some(X::anchor_layer_id) == X::L4::child_layer_id { - let l4 = self.get4(index); - self.int_hop_get0(l4, index, index) - } else if Some(X::anchor_layer_id) == X::L3::child_layer_id { - let l3 = self.get3(index); - self.int_hop_get0(l3, index, index) - } else if Some(X::anchor_layer_id) == X::L2::child_layer_id { - let l2 = self.get2(index); - self.int_hop_get0(l2, index, index) - } else if Some(X::anchor_layer_id) == X::L1::child_layer_id { - let l1 = self.get1(index); - self.int_hop_get0(l1, index, index) - } else { - debug_assert!(Some(X::anchor_layer_id) > X::L0::child_layer_id); - &self.layer0[0] - } + let anchor_lod = index.align_to_layer_id(X::anchor_layer_id); + let anchor_abs = AbsIndex::new(X::anchor_layer_id, self.anchor[&anchor_lod]); + let wanted_abs = self.int_recursive_get(anchor_abs, index, 0); + debug_assert_eq!(wanted_abs.layer, 0); + &self.layer0[wanted_abs.index] } - /* function to return a trait object, should not be used because slow, so as a rule of thumb only use it in case i modify self, because modify should occur not that often, and is slow anyways */ - fn get_mut_dyn(&mut self, level: u8, i: usize) -> &mut dyn LayerInfoDyn { - match level { - 0 => &mut self.layer0[i], - 1 => &mut self.layer1[i], - 2 => &mut self.layer2[i], - 3 => &mut self.layer3[i], - 4 => &mut self.layer4[i], - 5 => &mut self.layer5[i], - 6 => &mut self.layer6[i], - 7 => &mut self.layer7[i], - 8 => &mut self.layer8[i], - 9 => &mut self.layer9[i], - 10 => &mut self.layer10[i], - 11 => &mut self.layer11[i], - 12 => &mut self.layer12[i], - 13 => &mut self.layer13[i], - 14 => &mut self.layer14[i], - 15 => &mut self.layer15[i], + fn get_mut_dyn(&mut self, abs: AbsIndex) -> &mut dyn LayerInfo { + match abs.layer { + 0 => &mut self.layer0[abs.index], + 1 => &mut self.layer1[abs.index], + 2 => &mut self.layer2[abs.index], + 3 => &mut self.layer3[abs.index], + 4 => &mut self.layer4[abs.index], + 5 => &mut self.layer5[abs.index], + 6 => &mut self.layer6[abs.index], + 7 => &mut self.layer7[abs.index], + 8 => &mut self.layer8[abs.index], + 9 => &mut self.layer9[abs.index], + 10 => &mut self.layer10[abs.index], + 11 => &mut self.layer11[abs.index], + 12 => &mut self.layer12[abs.index], + 13 => &mut self.layer13[abs.index], + 14 => &mut self.layer14[abs.index], + 15 => &mut self.layer15[abs.index], _ => panic!("invalid level"), } } + // returns the last LodIndex, that belongs to a parent AbsIndex + fn get_last_child_lod(parent: LodIndex, parent_level: u8) -> LodIndex { + let child_width = index::two_pow_u(X::child_layer_id[parent_level as usize].unwrap()) as u32; + parent + LodIndex::new(X::layer_volume[X::child_layer_id[parent_level as usize].unwrap() as usize].map(|e| (e-1)*child_width)) + } + + fn ppp(level: u8) -> &'static str { + match level { + 0 => " | ", + 1 => " - ", + 4 => " ---- ", + 5 => " ----- ", + 9 => " ---------- ", + 13 => " ------------- ", + _ => panic!("aaa"), + } + } + + /* + lower: must always be a LodIndex inside parent + upper: must always have same parent as lower -> parent + */ + fn int_make_at_least(&mut self, parent: AbsIndex, /*parent_lod2: LodIndex,*/ lower: LodIndex, upper: LodIndex, target_level: u8) { + let child_layer = X::child_layer_id[parent.layer as usize]; + let parent_lod_width = index::two_pow_u(parent.layer) as u32; + let parent_lod = lower.align_to_layer_id(parent.layer); + //assert_eq!(parent_lod, parent_lod2); + println!("{} lower, upper {} {} {} - {:?}", Self::ppp(parent.layer), lower, upper, parent_lod_width, child_layer); + println!("{} parent.layer {} child_layer {:?} target_level {}", Self::ppp(parent.layer), parent.layer, child_layer, target_level); + if parent.layer > target_level { + // create necessary childs: + X::drill_down(self, parent); + println!("{} DRILLED DOWN", Self::ppp(parent.layer)); + if child_layer.is_some() && child_layer.unwrap() > target_level { + let child_layer = child_layer.unwrap(); + let child_lod_width = index::two_pow_u(child_layer) as u32; + //calc childs which needs to be called recusivly, there childs will be the new parents + let child_lower = lower.align_to_layer_id(child_layer); + let child_upper = upper.align_to_layer_id(child_layer); + let child_base_abs_index = self.int_get_child_index(parent).unwrap(); + let parent_children = X::layer_volume[parent.layer as usize]; + // loop over childs and calculate correct lower and + let lower_xyz = child_lower.get().map(|e| e / child_lod_width); + let upper_xyz = child_upper.get().map(|e| e / child_lod_width); + println!("{} lxyz {}", Self::ppp(parent.layer), lower_xyz); + println!("{} uxyz {}", Self::ppp(parent.layer), upper_xyz); + println!("{} child_lod_width {}", Self::ppp(parent.layer), child_lod_width); + for x in lower_xyz[0]..upper_xyz[0]+1 { + for y in lower_xyz[1]..upper_xyz[1]+1 { + for z in lower_xyz[2]..upper_xyz[2]+1 { + println!("{} xyz {} {} {}", Self::ppp(parent.layer), x, y, z); + //calculate individual abs values, because we now, how they are ordered in the vec + let child_abs_index = child_base_abs_index + (x * parent_children[2] * parent_children[1] + y * parent_children[2] + z) as usize; + let child_abs = AbsIndex::new(child_layer, child_abs_index); + let child_lower = parent_lod + LodIndex::new(Vec3::new(x * child_lod_width, y * child_lod_width, z * child_lod_width)); + let child_upper = child_lower + LodIndex::new(Vec3::new(child_lod_width-1, child_lod_width-1, child_lod_width-1)); + + let inner_lower = index::max(lower, child_lower); + let inner_upper = index::min(upper, child_upper); + println!("{} potential restrict {} {} ", Self::ppp(parent.layer), child_lower, child_upper); + println!("{} restrict {} {} to {} {}", Self::ppp(parent.layer), lower, upper, inner_lower, inner_upper); + Self::int_make_at_least(self, child_abs, inner_lower, inner_upper, target_level); + } + } + } + } + } + } + /* These functions allow you to make the LodLayer provide a certain LOD for the specified area */ /*is at least minimum or maximum*/ - pub fn make_at_least(&mut self, lower: LodIndex, upper: LodIndex, level: u8) { + + pub fn make_at_least(&mut self, lower: LodIndex, upper: LodIndex, target_level: u8) { //ERROR, DOES NOT RECURSIVLY CALL let anchor_layer_id = X::anchor_layer_id; let anchor_lower = lower.align_to_layer_id(anchor_layer_id); let anchor_upper = upper.align_to_layer_id(anchor_layer_id); let lower_xyz = anchor_lower.get(); let upper_xyz = anchor_upper.get(); - let w = index::two_pow_u(level) as u32; + let anchor_width = index::two_pow_u(anchor_layer_id) as u32; let mut x = lower_xyz[0]; + println!("{} xxx lower, upper {} {} {}", Self::ppp(anchor_layer_id), lower_xyz, upper_xyz, anchor_width); while x <= upper_xyz[0] { let mut y = lower_xyz[1]; while y <= upper_xyz[1] { let mut z = lower_xyz[2]; while z <= upper_xyz[2] { - let xyz = LodIndex::new(Vec3::new(x,y,z)); - let i = self.anchor[&xyz]; - X::drill_down(self, anchor_layer_id, i); - z += w; + let anchor_lod = LodIndex::new(Vec3::new(x,y,z)); + let anchor_abs = AbsIndex::new(anchor_layer_id, self.anchor[&anchor_lod]); ; + if anchor_abs.layer > target_level { + X::drill_down(self, anchor_abs); + let child_lod_upper = Self::get_last_child_lod(anchor_lod, anchor_abs.layer); + + let inner_lower = index::max(lower, anchor_lod); + let inner_upper = index::min(upper, child_lod_upper); + + println!("{}call child with lower, upper {} {} instead of {} {} ", Self::ppp(anchor_layer_id), inner_lower, inner_upper, anchor_lod, child_lod_upper); + self.int_make_at_least(anchor_abs, inner_lower, inner_upper, target_level); + } + z += anchor_width; } - y += w; + y += anchor_width; } - x += w; + x += anchor_width; } + const p: u8 = 3; + let u = X::child_len[7]; } fn make_at_most(&mut self, lower: LodIndex, upper: LodIndex, level: i8) { @@ -1306,79 +431,4 @@ impl LayerInfo for () { fn get_child_index(self: &Self) -> Option { None } - const child_layer_id: Option = Some(0); - const layer_volume: Vec3 = Vec3{x: 1, y: 1,z: 1}; - const child_len: usize = 0; -} - -/* -#[derive(Debug, Clone)] -pub struct LodLayer { - pub data: E, - pub childs: Vec>, //Optimization potential: size_of == 24 and last layer doesnt need Vec at all. -} - -pub trait Layer: Sized { - fn new() -> LodLayer; - - fn get_level(layer: &LodLayer) -> i8; - fn get_lower_level(layer: &LodLayer) -> Option; - - /*Drills down the layer and creates childs*/ - fn drill_down(layer: &mut LodLayer); - - /*needs to recalc parent values and remove childs*/ - fn drill_up(parent: &mut LodLayer); -} - -impl LodLayer { - pub fn new_data(data: E) -> Self { - Self { - data, - childs: Vec::new(), - } - } -} - -impl LodLayer { - // gets the internal index on this layer from relative position - - fn get_internal_index(&self, relative: LodIndex) -> Vec3 { - let ll = length_to_index(E::get_lower_level(self).expect("your type is wrong configured!, configure Layer trait correctly")); - let length_per_children: u16 = two_pow_u(ll); - let child_index = relative.map(|i| (i / length_per_children)); - return child_index; - } - - fn get_internal_index_and_remainder(&self, relative: LodIndex) -> (Vec3, LodIndex) { - let ll = length_to_index(E::get_lower_level(self).expect("your type is wrong configured!, configure Layer trait correctly")); - let length_per_children: u16 = two_pow_u(ll); - let child_index = relative.map(|i| (i / length_per_children)); - let remainder_index = relative.map2(child_index, |i,c| (i - c * length_per_children)); - return (child_index, remainder_index); - } - - /*flatten the (1,2,3) child to 1*4+2*4+3*3*4 = 48*/ - fn get_flat_index(&self, internal_index: Vec3) -> usize { - let ll = E::get_lower_level(self).expect("your type is wrong configured!, configure Layer trait correctly"); - let cl = E::get_level(self); - let childs_per_dimentsion = (cl - ll) as usize; - let index = internal_index.x as usize + internal_index.y as usize * childs_per_dimentsion + internal_index.z as usize * childs_per_dimentsion * childs_per_dimentsion; - return index; - } - - //index must be local to self - fn get(&self, relative: LodIndex) -> &LodLayer { - // index is local for now - if self.childs.is_empty() { - return &self - } else { - let (int, rem) = self.get_internal_index_and_remainder(relative); - let index = self.get_flat_index(int); - &self.childs.get(index).unwrap().get(rem) - } - } - - -} -*/ +} \ No newline at end of file diff --git a/worldsim/src/region/lod/example.rs b/worldsim/src/region/lod/example.rs index 6556358b34..a55e8d49c4 100644 --- a/worldsim/src/region/lod/example.rs +++ b/worldsim/src/region/lod/example.rs @@ -3,6 +3,7 @@ use crate::lodstore::{ LayerInfo, LodConfig, index::LodIndex, + index::AbsIndex, }; use vek::*; @@ -42,34 +43,22 @@ impl LayerInfo for Example9 { fn get_child_index(self: &Self) -> Option { self.child_id.map(|n| n as usize) } - const child_layer_id: Option = Some(9); - const layer_volume: Vec3 = Vec3{x: 16, y: 16, z: 16}; - const child_len: usize = 4096;//2_usize.pow(Self::child_dim*3); } impl LayerInfo for Example5 { fn get_child_index(self: &Self) -> Option { self.child_id.map(|n| n as usize) } - const child_layer_id: Option = Some(4); - const layer_volume: Vec3 = Vec3{x: 32, y: 32, z: 32}; - const child_len: usize = 32768;//2_usize.pow(Self::child_dim*3); } impl LayerInfo for Example0 { fn get_child_index(self: &Self) -> Option { self.child_id.map(|n| n as usize) } - const child_layer_id: Option = Some(0); - const layer_volume: Vec3 = Vec3{x: 16, y: 16, z: 16}; - const child_len: usize = 4096;//2_usize.pow(Self::child_dim*3); } impl LayerInfo for Example_4 { fn get_child_index(self: &Self) -> Option { None } - const child_layer_id: Option = None; - const layer_volume: Vec3 = Vec3{x: 1, y: 1, z: 1}; - const child_len: usize = 0; } #[derive(Debug, Clone)] @@ -95,32 +84,91 @@ impl LodConfig for ExampleLodConfig { const anchor_layer_id: u8 = 13; + const layer_volume: [Vec3; 16] = [ + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 16, y: 16, z: 16}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 32, y: 32, z: 32}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 16, y: 16, z: 16}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 1, y: 1, z: 1}, + ]; + const child_layer_id: [Option; 16] = [ + None, + None, + None, + None, + Some(0), + None, + None, + None, + None, + Some(4), + None, + None, + None, + Some(9), + None, + None, + ]; + + const child_len: [usize; 16] = [ + (Self::layer_volume[0].x * Self::layer_volume[0].y * Self::layer_volume[0].z) as usize, + (Self::layer_volume[1].x * Self::layer_volume[1].y * Self::layer_volume[1].z) as usize, + (Self::layer_volume[2].x * Self::layer_volume[2].y * Self::layer_volume[2].z) as usize, + (Self::layer_volume[3].x * Self::layer_volume[3].y * Self::layer_volume[3].z) as usize, + (Self::layer_volume[4].x * Self::layer_volume[4].y * Self::layer_volume[4].z) as usize, + (Self::layer_volume[5].x * Self::layer_volume[5].y * Self::layer_volume[5].z) as usize, + (Self::layer_volume[6].x * Self::layer_volume[6].y * Self::layer_volume[6].z) as usize, + (Self::layer_volume[7].x * Self::layer_volume[7].y * Self::layer_volume[7].z) as usize, + (Self::layer_volume[8].x * Self::layer_volume[8].y * Self::layer_volume[8].z) as usize, + (Self::layer_volume[9].x * Self::layer_volume[9].y * Self::layer_volume[9].z) as usize, + (Self::layer_volume[10].x * Self::layer_volume[10].y * Self::layer_volume[10].z) as usize, + (Self::layer_volume[11].x * Self::layer_volume[11].y * Self::layer_volume[11].z) as usize, + (Self::layer_volume[12].x * Self::layer_volume[12].y * Self::layer_volume[12].z) as usize, + (Self::layer_volume[13].x * Self::layer_volume[13].y * Self::layer_volume[13].z) as usize, + (Self::layer_volume[14].x * Self::layer_volume[14].y * Self::layer_volume[14].z) as usize, + (Self::layer_volume[15].x * Self::layer_volume[15].y * Self::layer_volume[15].z) as usize, + ]; + + fn setup(&mut self) { } - fn drill_down(data: &mut LodData::, level: u8, index: usize) { - match level { + fn drill_down(data: &mut LodData::, abs: AbsIndex) { + match abs.layer { 0 => { panic!("cannot drill down further"); }, 4 => { - if data.layer4[index].child_id.is_some() {return;} + if data.layer4[abs.index].child_id.is_some() {return;} let insert = data.layer0.len(); - data.layer4.reserve(Example_4::child_len); - data.layer4[index].child_id = Some(insert as u32); - for i in 0..Example0::child_len { + data.layer0.reserve(Self::child_len[4]); + data.layer4[abs.index].child_id = Some(insert as u32); + println!("set0 {:?} = {}", abs, insert); + for i in 0..Self::child_len[4] { data.layer0.push(Example_4{ data: 0, }); } }, 9 => { - if data.layer9[index].child_id.is_some() {return;} + if data.layer9[abs.index].child_id.is_some() {return;} let insert = data.layer4.len(); - data.layer9.reserve(Example0::child_len); - data.layer9[index].child_id = Some(insert as u32); - for i in 0..Example5::child_len { + data.layer4.reserve(Self::child_len[9]); + data.layer9[abs.index].child_id = Some(insert as u32); + println!("set {:?} = {}", abs, insert); + for i in 0..Self::child_len[9] { data.layer4.push(Example0{ data: 0, child_id: None, @@ -128,11 +176,12 @@ impl LodConfig for ExampleLodConfig { } }, 13 => { - if data.layer13[index].child_id.is_some() {return;} + if data.layer13[abs.index].child_id.is_some() {return;} let insert = data.layer9.len(); - data.layer13.reserve(Example9::child_len); - data.layer13[index].child_id = Some(insert as u32); - for i in 0..Example9::child_len { + data.layer9.reserve(Self::child_len[13]); + data.layer13[abs.index].child_id = Some(insert as u32); + println!("set13 {:?} = {}", abs, insert); + for i in 0..Self::child_len[13] { data.layer9.push(Example5{ data: [0; 130], child_id: None, @@ -144,25 +193,25 @@ impl LodConfig for ExampleLodConfig { } - fn drill_up(data: &mut LodData::, level: u8, parent_index: usize) { - match level { + fn drill_up(data: &mut LodData::, parent_abs: AbsIndex) { + match parent_abs.layer { 0 => { panic!("SubBlocks_4 does not have children"); }, 4 => { - let delete = data.layer4[parent_index].child_id.expect("has no childs to drill up") as usize; - data.layer4[parent_index].child_id = None; - data.layer0.drain(delete..delete+Example0::child_len); + let delete = data.layer4[parent_abs.index].child_id.expect("has no childs to drill up") as usize; + data.layer4[parent_abs.index].child_id = None; + data.layer0.drain(delete..delete+Self::child_len[0]); }, 9 => { - let delete = data.layer9[parent_index].child_id.expect("has no childs to drill up") as usize; - data.layer9[parent_index].child_id = None; - data.layer4.drain(delete..delete+Example5::child_len); + let delete = data.layer9[parent_abs.index].child_id.expect("has no childs to drill up") as usize; + data.layer9[parent_abs.index].child_id = None; + data.layer4.drain(delete..delete+Self::child_len[5]); }, 13 => { - let delete = data.layer13[parent_index].child_id.expect("has no childs to drill up") as usize; - data.layer13[parent_index].child_id = None; - data.layer9.drain(delete..delete+Example9::child_len); + let delete = data.layer13[parent_abs.index].child_id.expect("has no childs to drill up") as usize; + data.layer13[parent_abs.index].child_id = None; + data.layer9.drain(delete..delete+Self::child_len[9]); }, _ => unreachable!(), } @@ -182,6 +231,7 @@ mod tests { use vek::*; use rand::Rng; use rand::ThreadRng; + use test::Bencher; fn randIndex(rng: &mut ThreadRng) -> LodIndex { let x: u16 = rng.gen(); @@ -212,62 +262,109 @@ mod tests { for z in 0..8 { println!("{:?}", Vec3::new(x*w9,y*w9,z*w9)); println!("{:?}", LodIndex::new(Vec3::new(x*w9,y*w9,z*w9))); - result.anchor.insert(LodIndex::new(Vec3::new(x*w9,y*w9,z*w9)), (x+y*8+z*8*8) as usize); + result.anchor.insert(LodIndex::new(Vec3::new(x*w9,y*w9,z*w9)), (x*8*8+y*8+z) as usize); } } } + println!("creating Region with {} 5er, {} 0er, {} -4er", act5, act0 , act_4); while result.layer9.len() < act5 as usize { let index = randIndex(&mut rng); let low = index.align_to_layer_id(9); result.make_at_least(low,low,9); - }/* - while result.layer5.len() < act0 as usize { + } + while result.layer4.len() < act0 as usize { let index = randIndex(&mut rng); - let low = index.align_to_layer_id(5); - result.make_at_least(low,low,5); - println!("{}", result.layer5.len()); - }*//* + let low = index.align_to_layer_id(4); + result.make_at_least(low,low,4); + } while result.layer0.len() < act_4 as usize { let index = randIndex(&mut rng); let low = index.align_to_layer_id(0); result.make_at_least(low,low,0); - }*/ + } println!("creating Region with {} 5er, {} 0er, {} -4er", act5, act0 , act_4); - println!("created Region l13: {} l9: {} l5: {} l0: {}", result.layer13.len(), result.layer9.len(), result.layer5.len(), result.layer0.len()); + println!("created Region l13: {} l9: {} l5: {} l0: {}", result.layer13.len(), result.layer9.len(), result.layer4.len(), result.layer0.len()); println!("size {} {} {}", size_of::(), size_of::(), size_of::()); result } - +/* #[test] fn reagiontest() { - let reg = createRegion(0.15, 0.01, 0.001, 0.1); + let reg = createRegion(0.0015, 0.01, 0.0000001, 0.1); thread::sleep(time::Duration::from_secs(4)); - /* - let i = LodIndex::new(Vec3::new(0,0,0)); - assert_eq!(i.get(), Vec3::new(0,0,0)); - - let i = LodIndex::new(Vec3::new(1337,0,0)); - assert_eq!(i.get(), Vec3::new(1337,0,0)); - - let i = LodIndex::new(Vec3::new(0,1337,0)); - assert_eq!(i.get(), Vec3::new(0,1337,0)); - - let i = LodIndex::new(Vec3::new(0,0,1337)); - assert_eq!(i.get(), Vec3::new(0,0,1337)); - - let i = LodIndex::new(Vec3::new(1,1,1)); - assert_eq!(i.get(), Vec3::new(1,1,1)); - - let i = LodIndex::new(Vec3::new(262143,262143,262143)); - assert_eq!(i.get(), Vec3::new(262143,262143,262143)); - - let i = LodIndex::new(Vec3::new(262144,262144,262144)); //overflow - assert_eq!(i.get(), Vec3::new(0,0,0)); - - let i = LodIndex::new(Vec3::new(42,1337,69)); - assert_eq!(i.get(), Vec3::new(42,1337,69)); - */ } + + #[test] + fn reagionmake_at_least() { + let mut reg = createRegion(0.0, 0.0, 0.0, 0.1); + let low = LodIndex::new(Vec3::new(8192, 8192, 8192)); + let high = LodIndex::new(Vec3::new(16384, 16384, 16384)); + reg.make_at_least(low,high,4); + } +*/ + /* + #[test] + fn access_0() { + let mut reg = createRegion(0.0, 0.0, 0.0, 0.1); + let low = LodIndex::new(Vec3::new(0, 0, 0)); + let high = LodIndex::new(Vec3::new(4, 4, 4)); + //thread::sleep(time::Duration::from_secs(10)); + reg.make_at_least(low,high,0); + reg.get0(LodIndex::new(Vec3::new(0, 0, 0))); + reg.get0(LodIndex::new(Vec3::new(1, 0, 0))); + reg.get0(LodIndex::new(Vec3::new(0, 1, 0))); + reg.get0(LodIndex::new(Vec3::new(0, 0, 1))); + reg.get0(LodIndex::new(Vec3::new(1, 1, 1))); + reg.get0(LodIndex::new(Vec3::new(2, 2, 2))); + reg.get0(LodIndex::new(Vec3::new(3, 3, 3))); + reg.get0(LodIndex::new(Vec3::new(4, 4, 4))); + }*/ + + #[test] + fn access_0b() { + let mut reg = createRegion(0.0, 0.0, 0.0, 0.1); + let low = LodIndex::new(Vec3::new(8192, 8192, 8192)); + let high = LodIndex::new(Vec3::new(10240, 10240, 10240)); + //thread::sleep(time::Duration::from_secs(10)); + reg.make_at_least(low,high,0); + } +/* + #[test] + fn access_0b() { + let mut reg = createRegion(0.0, 0.0, 0.0, 0.1); + let low = LodIndex::new(Vec3::new(0, 0, 0)); + let high = LodIndex::new(Vec3::new(4, 4, 4)); + reg.make_at_least(low,high,0); + reg.get0(LodIndex::new(Vec3::new(5, 5, 5))); //this access is not guaranteed but will work + } + + #[test] + #[should_panic] + fn access_0_fail() { + let mut reg = createRegion(0.0, 0.0, 0.0, 0.1); + let low = LodIndex::new(Vec3::new(0, 0, 0)); + let high = LodIndex::new(Vec3::new(4, 4, 4)); + reg.make_at_least(low,high,0); + reg.get0(LodIndex::new(Vec3::new(5, 5, 5))); //this access is not guaranteed but will work + reg.get0(LodIndex::new(Vec3::new(16, 16, 16))); // out of range + } + + #[bench] + fn bench_region(b: &mut Bencher) { + b.iter(|| createRegion(0.00015, 0.0001, 0.00000001, 0.1)); + } + + #[bench] + fn access_4(b: &mut Bencher) { + let mut reg = createRegion(0.0, 0.0, 0.0, 0.1); + let access = LodIndex::new(Vec3::new(9561, 9312, 8412)); + let low = LodIndex::new(Vec3::new(8192, 8192, 8192)); + let high = LodIndex::new(Vec3::new(10240, 10240, 10240)); + reg.make_at_least(low,high,4); + + + b.iter(|| reg.get4(access)); + }*/ } \ No newline at end of file diff --git a/worldsim/src/region/lod/terrain.rs b/worldsim/src/region/lod/terrain.rs index c847a06d90..c6fa478fb6 100644 --- a/worldsim/src/region/lod/terrain.rs +++ b/worldsim/src/region/lod/terrain.rs @@ -3,6 +3,7 @@ use crate::lodstore::{ LayerInfo, LodConfig, index::LodIndex, + index::AbsIndex, }; use vek::*; @@ -39,36 +40,24 @@ impl LayerInfo for Region9 { fn get_child_index(self: &Self) -> Option { self.child_id.map(|n| n as usize) } - const child_layer_id: Option = Some(9); - const layer_volume: Vec3 = Vec3{x: 16, y: 16, z: 16}; - const child_len: usize = 4096;//2_usize.pow(Self::child_dim*3); } impl LayerInfo for Chunk5 { fn get_child_index(self: &Self) -> Option { self.child_id.map(|n| n as usize) } - const child_layer_id: Option = Some(4); - const layer_volume: Vec3 = Vec3{x: 32, y: 32, z: 32}; - const child_len: usize = 32768;//2_usize.pow(Self::child_dim*3); } impl LayerInfo for Block0 { fn get_child_index(self: &Self) -> Option { self.child_id.map(|n| n as usize) } - const child_layer_id: Option = Some(0); - const layer_volume: Vec3 = Vec3{x: 16, y: 16, z: 16}; - const child_len: usize = 4096;//2_usize.pow(Self::child_dim*3); } impl LayerInfo for SubBlock_4 { fn get_child_index(self: &Self) -> Option { None } - const child_layer_id: Option = None; - const layer_volume: Vec3 = Vec3{x: 1, y: 1, z: 1}; - const child_len: usize = 0; } #[derive(Debug, Clone)] @@ -94,19 +83,74 @@ impl LodConfig for TerrainLodConfig { const anchor_layer_id: u8 = 13; + const layer_volume: [Vec3; 16] = [ + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 16, y: 16, z: 16}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 32, y: 32, z: 32}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 16, y: 16, z: 16}, + Vec3{x: 1, y: 1, z: 1}, + Vec3{x: 1, y: 1, z: 1}, + ]; + const child_layer_id: [Option; 16] = [ + None, + None, + None, + None, + Some(0), + None, + None, + None, + None, + Some(5), + None, + None, + None, + Some(9), + None, + None, + ]; + const child_len: [usize; 16] = [ + (Self::layer_volume[0].x * Self::layer_volume[0].y * Self::layer_volume[0].z) as usize, + (Self::layer_volume[1].x * Self::layer_volume[1].y * Self::layer_volume[1].z) as usize, + (Self::layer_volume[2].x * Self::layer_volume[2].y * Self::layer_volume[2].z) as usize, + (Self::layer_volume[3].x * Self::layer_volume[3].y * Self::layer_volume[3].z) as usize, + (Self::layer_volume[4].x * Self::layer_volume[4].y * Self::layer_volume[4].z) as usize, + (Self::layer_volume[5].x * Self::layer_volume[5].y * Self::layer_volume[5].z) as usize, + (Self::layer_volume[6].x * Self::layer_volume[6].y * Self::layer_volume[6].z) as usize, + (Self::layer_volume[7].x * Self::layer_volume[7].y * Self::layer_volume[7].z) as usize, + (Self::layer_volume[8].x * Self::layer_volume[8].y * Self::layer_volume[8].z) as usize, + (Self::layer_volume[9].x * Self::layer_volume[9].y * Self::layer_volume[9].z) as usize, + (Self::layer_volume[10].x * Self::layer_volume[10].y * Self::layer_volume[10].z) as usize, + (Self::layer_volume[11].x * Self::layer_volume[11].y * Self::layer_volume[11].z) as usize, + (Self::layer_volume[12].x * Self::layer_volume[12].y * Self::layer_volume[12].z) as usize, + (Self::layer_volume[13].x * Self::layer_volume[13].y * Self::layer_volume[13].z) as usize, + (Self::layer_volume[14].x * Self::layer_volume[14].y * Self::layer_volume[14].z) as usize, + (Self::layer_volume[15].x * Self::layer_volume[15].y * Self::layer_volume[15].z) as usize, + ]; + fn setup(&mut self) { } - fn drill_down(data: &mut LodData::, level: u8, index: usize) { - match level { + fn drill_down(data: &mut LodData::, abs: AbsIndex) { + match abs.layer { 0 => { panic!("cannot drill down further"); }, 4 => { let insert = data.layer0.len(); - data.layer4[index].child_id = Some(insert as u32); - for i in 0..Block0::child_len { + data.layer4[abs.index].child_id = Some(insert as u32); + for i in 0..Self::child_len[4] { data.layer0[i+insert] = SubBlock_4{ material: 0, }; @@ -114,8 +158,8 @@ impl LodConfig for TerrainLodConfig { }, 9 => { let insert = data.layer4.len(); - data.layer9[index].child_id = Some(insert as u32); - for i in 0..Chunk5::child_len { + data.layer9[abs.index].child_id = Some(insert as u32); + for i in 0..Self::child_len[9] { data.layer4[i+insert] = Block0{ material: 0, child_id: None, @@ -124,8 +168,8 @@ impl LodConfig for TerrainLodConfig { }, 13 => { let insert = data.layer9.len(); - data.layer13[index].child_id = Some(insert as u32); - for i in 0..Region9::child_len { + data.layer13[abs.index].child_id = Some(insert as u32); + for i in 0..Self::child_len[13] { data.layer9[i+insert] = Chunk5{ precent_air: 0.2, percent_forrest: 0.3, @@ -140,25 +184,25 @@ impl LodConfig for TerrainLodConfig { } - fn drill_up(data: &mut LodData::, level: u8, parent_index: usize) { - match level { + fn drill_up(data: &mut LodData::, parent_abs: AbsIndex) { + match parent_abs.layer { 0 => { panic!("SubBlocks_4 does not have children"); }, 4 => { - let delete = data.layer4[parent_index].child_id.expect("has no childs to drill up") as usize; - data.layer4[parent_index].child_id = None; - data.layer0.drain(delete..delete+Block0::child_len); + let delete = data.layer4[parent_abs.index].child_id.expect("has no childs to drill up") as usize; + data.layer4[parent_abs.index].child_id = None; + data.layer0.drain(delete..delete+Self::child_len[4]); }, 9 => { - let delete = data.layer9[parent_index].child_id.expect("has no childs to drill up") as usize; - data.layer9[parent_index].child_id = None; - data.layer4.drain(delete..delete+Chunk5::child_len); + let delete = data.layer9[parent_abs.index].child_id.expect("has no childs to drill up") as usize; + data.layer9[parent_abs.index].child_id = None; + data.layer4.drain(delete..delete+Self::child_len[9]); }, 13 => { - let delete = data.layer13[parent_index].child_id.expect("has no childs to drill up") as usize; - data.layer13[parent_index].child_id = None; - data.layer9.drain(delete..delete+Region9::child_len); + let delete = data.layer13[parent_abs.index].child_id.expect("has no childs to drill up") as usize; + data.layer13[parent_abs.index].child_id = None; + data.layer9.drain(delete..delete+Self::child_len[13]); }, _ => unreachable!(), }