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
74
75
76
77
use std::collections::HashSet;
use std::iter::IntoIterator;
use prelude::*;
#[derive (Default, Debug, PartialEq, PartialOrd, Ord, Eq, Clone, Hash)]
pub struct OcTree<P> where
P: IsEditable3D + IsBuildable3D {
pub root: Option<OcNode<P>>,
pub min: P,
pub max: P
}
impl<P> IsTree3D<P> for OcTree<P> where
P: IsEditable3D + IsBuildableND + IsBuildable3D + Clone + Default {
fn size(&self) -> usize {
match self.root {
None => 0,
Some(ref node) => node.size()
}
}
fn to_pointcloud(&self) -> PointCloud3D<P> {
self.collect(-1)
}
fn build(&mut self, pc: PointCloud3D<P>) -> Result<()> {
let bb = pc.bounding_box()?;
let mut unique_data = Vec::new();
let mut set = HashSet::new();
for p in pc.data {
set.insert(*p);
}
unique_data.extend(set.into_iter());
self.min = *P::new(bb.min_p().x, bb.min_p().y, bb.min_p().z);
self.max = *P::new(bb.max_p().x, bb.max_p().y, bb.max_p().z);
self.root = Some(OcNode::new(&self.min, &self.max, unique_data));
Ok(())
}
}
impl<P> IsOcTree<P> for OcTree<P> where
P: IsEditable3D + IsBuildableND + IsBuildable3D + Clone + Default {
fn collect(&self, maxdepth: i8) -> PointCloud3D<P> {
let mut result = PointCloud3D::new();
if let Some(ref node) = self.root {
node.collect(0, maxdepth, &mut result);
}
result
}
}