2020-04-24 14:20:16 +00:00
|
|
|
use crate::vol::{BaseVol, ReadVol, SizedVol, Vox};
|
|
|
|
use vek::*;
|
|
|
|
|
|
|
|
pub struct Scaled<'a, V> {
|
|
|
|
pub inner: &'a V,
|
|
|
|
pub scale: Vec3<f32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, V: BaseVol> BaseVol for Scaled<'a, V> {
|
|
|
|
type Error = V::Error;
|
|
|
|
type Vox = V::Vox;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, V: ReadVol> ReadVol for Scaled<'a, V> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn get(&self, pos: Vec3<i32>) -> Result<&Self::Vox, Self::Error> {
|
2020-04-25 15:06:52 +00:00
|
|
|
let ideal_pos = pos.map2(self.scale, |e, scale| e as f32 / scale);
|
|
|
|
let pos = ideal_pos.map(|e| e.trunc() as i32);
|
|
|
|
|
|
|
|
let ideal_search_size = Vec3::<f32>::one() / self.scale;
|
|
|
|
let range_iter = |i: usize| {
|
|
|
|
std::iter::successors(Some(0), |p| Some(if *p < 0 { -*p } else { -(*p + 1) }))
|
2020-04-25 18:42:42 +00:00
|
|
|
.take_while(move |p| {
|
|
|
|
((ideal_pos[i] - ideal_search_size[i] / 2.0).round() as i32
|
|
|
|
..(ideal_pos[i] + ideal_search_size[i] / 2.0).round() as i32)
|
|
|
|
.contains(&(pos[i] + *p))
|
|
|
|
})
|
2020-04-24 15:08:51 +00:00
|
|
|
};
|
2020-04-25 15:06:52 +00:00
|
|
|
range_iter(0)
|
2020-04-25 18:42:42 +00:00
|
|
|
.map(|i| range_iter(1).map(move |j| range_iter(2).map(move |k| Vec3::new(i, j, k))))
|
2020-04-24 14:20:16 +00:00
|
|
|
.flatten()
|
|
|
|
.flatten()
|
|
|
|
.map(|offs| self.inner.get(pos + offs))
|
|
|
|
.find(|vox| vox.as_ref().map(|v| !v.is_empty()).unwrap_or(false))
|
|
|
|
.unwrap_or_else(|| self.inner.get(pos))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, V: SizedVol> SizedVol for Scaled<'a, V> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn lower_bound(&self) -> Vec3<i32> {
|
|
|
|
self.inner
|
|
|
|
.lower_bound()
|
2020-04-24 14:42:06 +00:00
|
|
|
.map2(self.scale, |e, scale| (e as f32 * scale).floor() as i32)
|
2020-04-24 14:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn upper_bound(&self) -> Vec3<i32> {
|
2020-04-24 15:08:51 +00:00
|
|
|
self.inner
|
|
|
|
.upper_bound()
|
|
|
|
.map2(self.scale, |e, scale| (e as f32 * scale).ceil() as i32 + 1)
|
2020-04-24 14:20:16 +00:00
|
|
|
}
|
|
|
|
}
|