1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::num::NonZeroU8;

use crate::vol::FilledVox;
use vek::*;

const GLOWY: u8 = 1 << 1;
const SHINY: u8 = 1 << 2;
const HOLLOW: u8 = 1 << 3;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct CellData {
    pub col: Rgb<u8>,
    pub attr: NonZeroU8, // 1 = glowy, 2 = shiny, 3 = hollow
}

impl CellData {
    pub(super) fn new(col: Rgb<u8>, glowy: bool, shiny: bool, hollow: bool) -> Self {
        CellData {
            col,
            attr: NonZeroU8::new(
                1 + glowy as u8 * GLOWY + shiny as u8 * SHINY + hollow as u8 * HOLLOW,
            )
            .unwrap(),
        }
    }

    pub fn is_hollow(&self) -> bool { self.attr.get() & HOLLOW != 0 }
}

impl Default for CellData {
    fn default() -> Self { Self::new(Rgb::broadcast(255), false, false, false) }
}

/// A type representing a single voxel in a figure.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Cell {
    Filled(CellData),
    Empty,
}

impl Cell {
    pub fn new(col: Rgb<u8>, glowy: bool, shiny: bool, hollow: bool) -> Self {
        Cell::Filled(CellData::new(col, glowy, shiny, hollow))
    }

    pub fn get_color(&self) -> Option<Rgb<u8>> {
        match self {
            Cell::Filled(data) => Some(data.col),
            Cell::Empty => None,
        }
    }

    pub fn is_glowy(&self) -> bool {
        match self {
            Cell::Filled(data) => data.attr.get() & GLOWY != 0,
            Cell::Empty => false,
        }
    }

    pub fn is_shiny(&self) -> bool {
        match self {
            Cell::Filled(data) => data.attr.get() & SHINY != 0,
            Cell::Empty => false,
        }
    }

    pub fn is_hollow(&self) -> bool {
        match self {
            Cell::Filled(data) => data.is_hollow(),
            Cell::Empty => false,
        }
    }
}

impl FilledVox for Cell {
    fn default_non_filled() -> Self { Cell::Empty }

    fn is_filled(&self) -> bool { matches!(self, Cell::Filled(_)) }
}

#[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>());
    }
}