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
//! This module contains tools for working with 3D voxel grids.
use std::collections::HashSet;
/// This function takes a set of coordinates in a 3D grid and returns a list of clusters of
/// connected voxel coordinates.
///
/// # Arguments
///
/// * `indices`:
///
/// returns: Vec<Vec<(u32, u32, u32), Global>, Global>
///
/// # Examples
///
/// ```
///
/// ```
pub fn clusters_from_sparse(mut indices: HashSet<(i32, i32, i32)>) -> Vec<Vec<(i32, i32, i32)>> {
let mut results = Vec::new();
while !indices.is_empty() {
let mut working = Vec::new();
let mut to_visit = Vec::new();
to_visit.push(pop_index(&mut indices));
while let Some(current) = to_visit.pop() {
working.push(current);
for x in -1..=1 {
for y in -1..=1 {
for z in -1..=1 {
if x == 0 && y == 0 && z == 0 {
continue;
}
let neighbor = (current.0 + x, current.1 + y, current.2 + z);
if indices.remove(&neighbor) {
to_visit.push(neighbor);
}
}
}
}
}
results.push(working.into_iter().collect());
}
results
}
fn pop_index(indices: &mut HashSet<(i32, i32, i32)>) -> (i32, i32, i32) {
let result = *indices.iter().next().unwrap();
indices.remove(&result);
result
}