lattice.rs
1.7 kB · rust · 69 lines
1use mrlycore::tile::Design;2use mrlymath::three::Cell3d;34#[derive(Clone, Copy, PartialEq, Eq)]5pub enum Family {6 Carpet,7 Net,8 Tree,9 Void,10}1112pub const FAMILIES: [Family; 4] = [Family::Carpet, Family::Net, Family::Tree, Family::Void];1314impl Family {15 pub fn name(self) -> &'static str {16 match self {17 Family::Carpet => "carpet",18 Family::Net => "net",19 Family::Tree => "tree",20 Family::Void => "void",21 }22 }2324 fn design(self) -> Design {25 match self {26 Family::Carpet => Design::Carpet,27 Family::Net => Design::Net,28 Family::Tree => Design::Ztree,29 Family::Void => Design::Void,30 }31 }3233 pub fn cube(self, number: usize) -> Cell3d {34 mrlymath::three::named(self.design(), number, 1).expect("a named cube")35 }36}3738pub struct Rule {39 corners: [bool; 8],40}4142impl Rule {43 pub fn new(family: Family) -> Rule {44 let cube = family.cube(2);45 let mut corners = [false; 8];46 for (slot, corner) in corners.iter_mut().enumerate() {47 *corner = cube.types().get(&[slot >> 2, (slot >> 1) & 1, slot & 1]) == 1;48 }49 Rule { corners }50 }5152 pub fn filled(&self, x: i64, y: i64, z: i64) -> bool {53 let bit = |c: i64| (c.div_euclid(4) & 1) as usize;54 self.corners[bit(x) << 2 | bit(y) << 1 | bit(z)]55 }56}5758pub fn cell(rule: &Rule, n: i64, x: i64, z: i64) -> Option<bool> {59 let y = 6 * n - 2 - x - z;60 (0..4 * n).contains(&y).then(|| rule.filled(x, y, z))61}6263pub fn column(point: f64, n: i64) -> i64 {64 ((point * 4.0 * n as f64).floor() as i64).min(4 * n - 1)65}6667pub fn row(point: f64, n: i64) -> i64 {68 (2 * (point * 2.0 * n as f64).floor() as i64).min(4 * n - 2)69}