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
/*
Copyright 2016 Martin Buck
This file is part of rust-3d.
rust-3d is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
rust-3d is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with rust-3d.  If not, see <http://www.gnu.org/licenses/>.
*/

//! OcTree

//@todo clean up similar to pc code

use std::collections::HashSet;
use std::iter::IntoIterator;

use prelude::*;

#[derive (Default, Debug, PartialEq, PartialOrd, Ord, Eq, Clone, Hash)]
/// OcTree
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);
        }
        //let mut set: HashSet<P> = pc.data.into_iter().unbox().collect();
        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 {

    //@todo rewrite or make new method which returns cog instead of stopping recursion
    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
    }
}