refactor Cell back to 4 byte size

This commit is contained in:
aljazerzen
2021-03-26 11:05:51 +01:00
parent 474094de03
commit c8136a44de
3 changed files with 46 additions and 27 deletions

View File

@ -1,23 +1,28 @@
use std::num::NonZeroU8;
use crate::vol::Vox; use crate::vol::Vox;
use vek::*; use vek::*;
pub(super) const GLOWY: u8 = 1 << 0; const GLOWY: u8 = 1 << 1;
pub(super) const SHINY: u8 = 1 << 1; const SHINY: u8 = 1 << 2;
#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(packed)]
pub struct CellData { pub struct CellData {
pub col: Rgb<u8>, pub col: Rgb<u8>,
pub attr: u8, // 0 = glowy, 1 = shiny pub attr: NonZeroU8, // 1 = glowy, 2 = shiny
}
impl CellData {
pub(super) fn new(col: Rgb<u8>, glowy: bool, shiny: bool) -> Self {
CellData {
col,
attr: NonZeroU8::new(1 + glowy as u8 * GLOWY + shiny as u8 * SHINY).unwrap(),
}
}
} }
impl Default for CellData { impl Default for CellData {
fn default() -> Self { fn default() -> Self { Self::new(Rgb::broadcast(255), false, false) }
Self {
col: Rgb::broadcast(255),
attr: 0,
}
}
} }
/// A type representing a single voxel in a figure. /// A type representing a single voxel in a figure.
@ -29,10 +34,7 @@ pub enum Cell {
impl Cell { impl Cell {
pub fn new(col: Rgb<u8>, glowy: bool, shiny: bool) -> Self { pub fn new(col: Rgb<u8>, glowy: bool, shiny: bool) -> Self {
Cell::Filled(CellData { Cell::Filled(CellData::new(col, glowy, shiny))
col,
attr: glowy as u8 * GLOWY + shiny as u8 * SHINY,
})
} }
pub fn get_color(&self) -> Option<Rgb<u8>> { pub fn get_color(&self) -> Option<Rgb<u8>> {
@ -44,14 +46,14 @@ impl Cell {
pub fn is_glowy(&self) -> bool { pub fn is_glowy(&self) -> bool {
match self { match self {
Cell::Filled(data) => data.attr & GLOWY != 0, Cell::Filled(data) => data.attr.get() & GLOWY != 0,
Cell::Empty => false, Cell::Empty => false,
} }
} }
pub fn is_shiny(&self) -> bool { pub fn is_shiny(&self) -> bool {
match self { match self {
Cell::Filled(data) => data.attr & SHINY != 0, Cell::Filled(data) => data.attr.get() & SHINY != 0,
Cell::Empty => false, Cell::Empty => false,
} }
} }
@ -67,3 +69,14 @@ impl Vox for Cell {
} }
} }
} }
#[cfg(test)]
mod test {
use super::*;
#[test]
fn cell_size() {
assert_eq!(4, std::mem::size_of::<Cell>());
assert_eq!(1, std::mem::align_of::<Cell>());
}
}

View File

@ -27,3 +27,14 @@ impl Vox for MatCell {
fn is_empty(&self) -> bool { matches!(self, MatCell::None) } fn is_empty(&self) -> bool { matches!(self, MatCell::None) }
} }
#[cfg(test)]
mod test {
use super::*;
#[test]
fn met_cell_size() {
assert_eq!(5, std::mem::size_of::<MatCell>());
assert_eq!(1, std::mem::align_of::<MatCell>());
}
}

View File

@ -8,7 +8,6 @@ pub use self::{
mat_cell::MatCell, mat_cell::MatCell,
}; };
use self::cell::{GLOWY, SHINY};
use crate::{ use crate::{
vol::{IntoFullPosIterator, IntoFullVolIterator, ReadVol, SizedVol, Vox, WriteVol}, vol::{IntoFullPosIterator, IntoFullVolIterator, ReadVol, SizedVol, Vox, WriteVol},
volumes::dyna::Dyna, volumes::dyna::Dyna,
@ -159,10 +158,7 @@ impl MatSegment {
for (pos, vox) in self.full_vol_iter() { for (pos, vox) in self.full_vol_iter() {
let data = match vox { let data = match vox {
MatCell::None => continue, MatCell::None => continue,
MatCell::Mat(mat) => CellData { MatCell::Mat(mat) => CellData::new(map(*mat), false, false),
col: map(*mat),
attr: 0,
},
MatCell::Normal(data) => *data, MatCell::Normal(data) => *data,
}; };
vol.set(pos, Cell::Filled(data)).unwrap(); vol.set(pos, Cell::Filled(data)).unwrap();
@ -222,12 +218,11 @@ impl MatSegment {
.get(index as usize) .get(index as usize)
.copied() .copied()
.unwrap_or_else(|| Rgb::broadcast(0)); .unwrap_or_else(|| Rgb::broadcast(0));
MatCell::Normal(CellData { MatCell::Normal(CellData::new(
col: color, color,
attr: 0 (13..16).contains(&index),
| ((13..16).contains(&index) as u8 * GLOWY) (8..13).contains(&index),
| ((8..13).contains(&index) as u8 * SHINY), ))
})
}, },
}; };