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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#![feature(untagged_unions)]
#![feature(const_fn)]

mod arena;
pub mod bounds;
pub mod dir;
pub mod index_path;
pub mod octree;

pub use arena::{Arena, ArenaHandle};
pub use bounds::Bounds;
pub use dir::{Corner, Edge, Face, Quadrant};
pub use index_path::IndexPath;
pub use octree::{NodeRef, NodeRefMut, Octree};

use std::fmt::Debug;

pub trait Voxel: Copy + Clone + Default + Eq + Debug {
    fn avg(voxels: &[Self; 8]) -> Self;
}

#[cfg(test)]
mod tests {
    use super::*;
    impl Voxel for u32 {
        fn avg(arr: &[Self; 8]) -> Self {
            // find most frequent element
            let mut arr = arr.clone();
            arr.sort();

            let mut count: u8 = 0;
            let mut max_count: u8 = 0;
            let mut max_element: Self = 0;
            let mut last_element: Self = 0;
            for i in &arr {
                if *i != last_element {
                    if count > max_count {
                        max_count = count;
                        max_element = *i;
                    }
                    count = 0;
                }
                count += 1;
                last_element = *i;
            }
            max_element
        }
    }
    impl Voxel for u16 {
        fn avg(arr: &[Self; 8]) -> Self {
            // find most frequent element
            let mut arr = arr.clone();
            arr.sort();

            let mut count: u8 = 0;
            let mut max_count: u8 = 0;
            let mut max_element: Self = 0;
            let mut last_element: Self = 0;
            for i in &arr {
                if *i != last_element {
                    if count > max_count {
                        max_count = count;
                        max_element = *i;
                    }
                    count = 0;
                }
                count += 1;
                last_element = *i;
            }
            max_element
        }
    }
}