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
//! The 26-bit-per-voxel connectivity graph.
use crate::voxel::VolumeShape;
/// A read-only view over a voxel connectivity graph buffer.
///
/// The buffer must have one `u32` per voxel, in the same order as the volume.
#[derive(Debug, Clone, Copy)]
pub struct ConnectivityGraph<'a> {
buf: &'a [u32],
shape: VolumeShape,
}
impl<'a> ConnectivityGraph<'a> {
/// Wrap an existing buffer. Returns `None` if the buffer length doesn't
/// match the shape's voxel count.
pub fn new(buf: &'a [u32], shape: VolumeShape) -> Option<Self> {
if buf.len() as u64 == shape.total() {
Some(Self { buf, shape })
} else {
None
}
}
/// Volume shape this graph corresponds to.
pub fn shape(&self) -> VolumeShape {
self.shape
}
/// Check whether direction `dir_bit` is permitted at linear index `loc`.
///
/// `dir_bit` should come from `neighborhood::neighborhood_masks()`.
#[inline]
pub fn allows(&self, loc: u64, dir_bit: u32) -> bool {
// The caller is expected to pass a valid `loc`; retain a bounds check
// because its cost is small relative to queue processing.
self.buf
.get(loc as usize)
.is_some_and(|word| (*word & dir_bit) != 0)
}
}