binary_greedy_meshing/
quad.rs

1use alloc::string::String;
2
3pub(crate) const MASK_6: u64 = 0b111111;
4
5#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
6pub struct Quad(pub u64);
7
8impl Quad {
9    /// x: 6 bits
10    /// y: 6 bits
11    /// z: 6 bits 18
12    /// width (w): 6 bits
13    /// height (h): 6 bits
14    /// voxel id (v): 32 bits
15    ///
16    /// ao (a): 2 bits
17    ///
18    /// layout:
19    /// 0bvvvv_vvvv_vvvv_vvvv_vvvv_vvvv_vvvv_vvvv_00hh_hhhh_wwww_wwzz_zzzz_yyyy_yyxx_xxxx
20    #[inline]
21    pub fn pack(x: usize, y: usize, z: usize, w: usize, h: usize, v_type: usize) -> Self {
22        Quad(((v_type << 32) | (h << 24) | (w << 18) | (z << 12) | (y << 6) | x) as u64)
23    }
24
25    #[inline]
26    pub fn xyz(&self) -> [u64; 3] {
27        let x = (self.0) & MASK_6;
28        let y = (self.0 >> 6) & MASK_6;
29        let z = (self.0 >> 12) & MASK_6;
30        [x, y, z]
31    }
32
33    #[inline]
34    pub fn width(&self) -> u64 {
35        (self.0 >> 18) & MASK_6
36    }
37
38    #[inline]
39    pub fn height(&self) -> u64 {
40        (self.0 >> 24) & MASK_6
41    }
42
43    #[inline]
44    pub fn voxel_id(&self) -> u64 {
45        self.0 >> 32
46    }
47
48    /// Unpacks quad data and formats it as "{x};{y};{z} {w}x{h} v={v_type}" for debugging
49    #[inline]
50    pub fn debug_quad(&self) -> String {
51        let mut quad = self.0;
52        let x = quad & MASK_6;
53        quad >>= 6;
54        let y = quad & MASK_6;
55        quad >>= 6;
56        let z = quad & MASK_6;
57        quad >>= 6;
58        let w = quad & MASK_6;
59        quad >>= 6;
60        let h = quad & MASK_6;
61        quad >>= 8;
62        let v_type = quad;
63        format!("{x};{y};{z} {w}x{h} v={v_type}")
64    }
65}
66
67impl alloc::fmt::Debug for Quad {
68    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
69        f.debug_struct("Quad")
70            .field("position", &self.xyz())
71            .field("width", &self.width())
72            .field("height", &self.height())
73            .field("voxel_id", &self.voxel_id())
74            .finish()
75    }
76}