2019-01-13 20:53:55 +00:00
|
|
|
// Library
|
|
|
|
use vek::*;
|
|
|
|
|
|
|
|
// Local
|
|
|
|
use crate::vol::{
|
2019-01-14 18:49:53 +00:00
|
|
|
Vox,
|
2019-01-13 20:53:55 +00:00
|
|
|
BaseVol,
|
|
|
|
SizedVol,
|
|
|
|
ReadVol,
|
|
|
|
WriteVol,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub enum DynaErr {
|
|
|
|
OutOfBounds,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A volume with dimensions known only at the creation of the object.
|
|
|
|
// V = Voxel
|
|
|
|
// S = Size (replace when const generics are a thing)
|
|
|
|
// M = Metadata
|
2019-01-14 18:49:53 +00:00
|
|
|
pub struct Dyna<V: Vox, M> {
|
2019-01-13 20:53:55 +00:00
|
|
|
vox: Vec<V>,
|
|
|
|
meta: M,
|
|
|
|
sz: Vec3<u32>,
|
|
|
|
}
|
|
|
|
|
2019-01-14 18:49:53 +00:00
|
|
|
impl<V: Vox, M> Dyna<V, M> {
|
2019-01-13 20:53:55 +00:00
|
|
|
/// Used to transform a voxel position in the volume into its corresponding index in the voxel
|
|
|
|
// array.
|
|
|
|
#[inline(always)]
|
|
|
|
fn idx_for(sz: Vec3<u32>, pos: Vec3<i32>) -> Option<usize> {
|
|
|
|
if
|
|
|
|
pos.map(|e| e >= 0).reduce_and() &&
|
|
|
|
pos.map2(sz, |e, lim| e < lim as i32).reduce_and()
|
|
|
|
{
|
|
|
|
Some((
|
|
|
|
pos.x * sz.y as i32 * sz.z as i32 +
|
|
|
|
pos.y * sz.z as i32 +
|
|
|
|
pos.z
|
|
|
|
) as usize)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 18:49:53 +00:00
|
|
|
impl<V: Vox, M> BaseVol for Dyna<V, M> {
|
2019-01-13 20:53:55 +00:00
|
|
|
type Vox = V;
|
|
|
|
type Err = DynaErr;
|
|
|
|
}
|
|
|
|
|
2019-01-14 18:49:53 +00:00
|
|
|
impl<V: Vox, M> SizedVol for Dyna<V, M> {
|
2019-01-13 20:53:55 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn get_size(&self) -> Vec3<u32> { self.sz }
|
|
|
|
}
|
|
|
|
|
2019-01-14 18:49:53 +00:00
|
|
|
impl<V: Vox, M> ReadVol for Dyna<V, M> {
|
2019-01-13 20:53:55 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn get(&self, pos: Vec3<i32>) -> Result<&V, DynaErr> {
|
|
|
|
Self::idx_for(self.sz, pos)
|
|
|
|
.and_then(|idx| self.vox.get(idx))
|
|
|
|
.ok_or(DynaErr::OutOfBounds)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 18:49:53 +00:00
|
|
|
impl<V: Vox, M> WriteVol for Dyna<V, M> {
|
2019-01-13 20:53:55 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn set(&mut self, pos: Vec3<i32>, vox: Self::Vox) -> Result<(), DynaErr> {
|
|
|
|
Self::idx_for(self.sz, pos)
|
|
|
|
.and_then(|idx| self.vox.get_mut(idx))
|
|
|
|
.map(|old_vox| *old_vox = vox)
|
|
|
|
.ok_or(DynaErr::OutOfBounds)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 18:49:53 +00:00
|
|
|
impl<V: Vox + Clone, M> Dyna<V, M> {
|
2019-01-13 20:53:55 +00:00
|
|
|
/// Create a new `Dyna` with the provided dimensions and all voxels filled with duplicates of
|
|
|
|
/// the provided voxel.
|
|
|
|
pub fn filled(sz: Vec3<u32>, vox: V, meta: M) -> Self {
|
|
|
|
Self {
|
|
|
|
vox: vec![vox; sz.product() as usize],
|
|
|
|
meta,
|
|
|
|
sz,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a reference to the internal metadata.
|
|
|
|
pub fn metadata(&self) -> &M {
|
|
|
|
&self.meta
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a mutable reference to the internal metadata.
|
|
|
|
pub fn metadata_mut(&mut self) -> &mut M {
|
|
|
|
&mut self.meta
|
|
|
|
}
|
|
|
|
}
|