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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::collections::HashSet;
use std::iter::IntoIterator;
use point_cloud_3d::{PointCloud3D};
use oc_node::{OcNode};
use traits::is_buildable_nd::*;
use traits::is_buildable_3d::*;
use traits::is_editable_3d::*;
use traits::has_bounding_box_3d::*;
use traits::is_tree_3d::*;
use traits::is_oc_tree::*;
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 {
fn new() -> OcTree<P> {
OcTree {
root: None,
min: *P::new(),
max: *P::new()
}
}
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>) -> bool {
match pc.bounding_box() {
Err(_) => false,
Ok((min, max)) => {
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::build(min.x, min.y, min.z);
self.max = *P::build(max.x, max.y, max.z);
self.root = Some(OcNode::new(&self.min, &self.max, unique_data));
true
}
}
}
}
impl<P> IsOcTree<P> for OcTree<P> where
P: IsEditable3D + IsBuildableND + IsBuildable3D + Clone {
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
}
}