Skip to main content

copc_streaming/
types.rs

1/// Octree node key: (level, x, y, z).
2#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
3pub struct VoxelKey {
4    pub level: i32,
5    pub x: i32,
6    pub y: i32,
7    pub z: i32,
8}
9
10impl VoxelKey {
11    pub fn child(&self, dir: i32) -> VoxelKey {
12        VoxelKey {
13            level: self.level + 1,
14            x: (self.x << 1) | (dir & 0x1),
15            y: (self.y << 1) | ((dir >> 1) & 0x1),
16            z: (self.z << 1) | ((dir >> 2) & 0x1),
17        }
18    }
19
20    pub fn children(&self) -> [VoxelKey; 8] {
21        [
22            self.child(0),
23            self.child(1),
24            self.child(2),
25            self.child(3),
26            self.child(4),
27            self.child(5),
28            self.child(6),
29            self.child(7),
30        ]
31    }
32
33    pub fn bounds(&self, root_bounds: &Aabb) -> Aabb {
34        let side = (root_bounds.max[0] - root_bounds.min[0]) / 2_u32.pow(self.level as u32) as f64;
35        Aabb {
36            min: [
37                root_bounds.min[0] + self.x as f64 * side,
38                root_bounds.min[1] + self.y as f64 * side,
39                root_bounds.min[2] + self.z as f64 * side,
40            ],
41            max: [
42                root_bounds.min[0] + (self.x + 1) as f64 * side,
43                root_bounds.min[1] + (self.y + 1) as f64 * side,
44                root_bounds.min[2] + (self.z + 1) as f64 * side,
45            ],
46        }
47    }
48}
49
50/// Axis-aligned bounding box.
51#[derive(Debug, Clone, Copy, PartialEq)]
52pub struct Aabb {
53    pub min: [f64; 3],
54    pub max: [f64; 3],
55}
56
57impl Aabb {
58    pub fn intersects(&self, other: &Aabb) -> bool {
59        self.min[0] <= other.max[0]
60            && self.max[0] >= other.min[0]
61            && self.min[1] <= other.max[1]
62            && self.max[1] >= other.min[1]
63            && self.min[2] <= other.max[2]
64            && self.max[2] >= other.min[2]
65    }
66}