Skip to content

Commit

Permalink
Document and refactor dodeca.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
patowen authored and Ralith committed Feb 16, 2025
1 parent 4f79f6b commit c217285
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 271 deletions.
25 changes: 11 additions & 14 deletions common/src/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::sync::OnceLock;
use std::sync::LazyLock;

use crate::dodeca::SIDE_COUNT;
use crate::dodeca::{Side, Vertex};
use crate::graph::{Graph, NodeId};
use crate::node::ChunkId;
Expand Down Expand Up @@ -29,9 +28,9 @@ impl Cursor {
// in both the dodecahedron sharing the face unique to the new vertex and that sharing the
// face that the new vertex isn't incident to.
let (a, b, c) = (self.a, self.b, self.c);
let a_prime = neighbors()[a as usize][b as usize][c as usize].unwrap();
let b_prime = neighbors()[b as usize][a as usize][c as usize].unwrap();
let c_prime = neighbors()[c as usize][b as usize][a as usize].unwrap();
let a_prime = NEIGHBORS[a as usize][b as usize][c as usize].unwrap();
let b_prime = NEIGHBORS[b as usize][a as usize][c as usize].unwrap();
let c_prime = NEIGHBORS[c as usize][b as usize][a as usize].unwrap();
use Dir::*;
let (sides, neighbor) = match dir {
Left => ((a, b, c_prime), c),
Expand All @@ -54,7 +53,7 @@ impl Cursor {
pub fn canonicalize(self, graph: &Graph) -> Option<ChunkId> {
graph.canonicalize(ChunkId::new(
self.node,
Vertex::from_sides(self.a, self.b, self.c).unwrap(),
Vertex::from_sides([self.a, self.b, self.c]).unwrap(),
))
}
}
Expand Down Expand Up @@ -105,10 +104,9 @@ impl std::ops::Neg for Dir {
}

/// Maps every (A, B, C) sharing a vertex to A', the side that shares edges with B and C but not A
fn neighbors() -> &'static [[[Option<Side>; SIDE_COUNT]; SIDE_COUNT]; SIDE_COUNT] {
static LOCK: OnceLock<[[[Option<Side>; SIDE_COUNT]; SIDE_COUNT]; SIDE_COUNT]> = OnceLock::new();
LOCK.get_or_init(|| {
let mut result = [[[None; SIDE_COUNT]; SIDE_COUNT]; SIDE_COUNT];
static NEIGHBORS: LazyLock<[[[Option<Side>; Side::COUNT]; Side::COUNT]; Side::COUNT]> =
LazyLock::new(|| {
let mut result = [[[None; Side::COUNT]; Side::COUNT]; Side::COUNT];
for a in Side::iter() {
for b in Side::iter() {
for c in Side::iter() {
Expand All @@ -129,8 +127,7 @@ fn neighbors() -> &'static [[[Option<Side>; SIDE_COUNT]; SIDE_COUNT]; SIDE_COUNT
}
}
result
})
}
});

#[cfg(test)]
mod tests {
Expand All @@ -142,8 +139,8 @@ mod tests {
for v in Vertex::iter() {
let [a, b, c] = v.canonical_sides();
assert_eq!(
neighbors()[a as usize][b as usize][c as usize],
neighbors()[a as usize][c as usize][b as usize]
NEIGHBORS[a as usize][b as usize][c as usize],
NEIGHBORS[a as usize][c as usize][b as usize]
);
}
}
Expand Down
Loading

0 comments on commit c217285

Please sign in to comment.