binary_space_partition/
lib.rs

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*!
Binary Space Partitioning (BSP)

Provides an abstract `BspNode` structure, which can be seen as a tree.
Useful for quickly ordering polygons along a particular view vector.
Is not tied to a particular math library.
*/
#![warn(missing_docs)]

use std::cmp;


/// The result of one plane being cut by another one.
/// The "cut" here is an attempt to classify a plane as being
/// in front or in the back of another one.
#[derive(Debug)]
pub enum PlaneCut<T> {
    /// The planes are one the same geometrical plane.
    Sibling(T),
    /// Planes are different, thus we can either determine that
    /// our plane is completely in front/back of another one,
    /// or split it into these sub-groups.
    Cut {
        /// Sub-planes in front of the base plane.
        front: Vec<T>,
        /// Sub-planes in the back of the base plane.
        back: Vec<T>,
    },
}

/// A plane abstracted to the matter of partitioning.
pub trait Plane: Sized + Clone {
    /// Try to cut a different plane by this one.
    fn cut(&self, Self) -> PlaneCut<Self>;
    /// Check if a different plane is aligned in the same direction
    /// as this one.
    fn is_aligned(&self, &Self) -> bool;
}

/// Add a list of planes to a particular front/end branch of some root node.
fn add_side<T: Plane>(side: &mut Option<Box<BspNode<T>>>, mut planes: Vec<T>) {
    if planes.len() != 0 {
        if side.is_none() {
            *side = Some(Box::new(BspNode::new()));
        }
        let mut node = side.as_mut().unwrap();
        for p in planes.drain(..) {
            node.insert(p)
        }
    }
}


/// A node in the `BspTree`, which can be considered a tree itself.
#[derive(Clone, Debug)]
pub struct BspNode<T> {
    values: Vec<T>,
    front: Option<Box<BspNode<T>>>,
    back: Option<Box<BspNode<T>>>,
}

impl<T> BspNode<T> {
    /// Create a new node.
    pub fn new() -> Self {
        BspNode {
            values: Vec::new(),
            front: None,
            back: None,
        }
    }

    /// Check if this node is a leaf of the tree.
    pub fn is_leaf(&self) -> bool {
        self.front.is_none() && self.back.is_none()
    }

    /// Get the tree depth starting with this node.
    pub fn get_depth(&self) -> usize {
        if self.values.is_empty() {
            return 0
        }
        let df = match self.front {
            Some(ref node) => node.get_depth(),
            None => 0,
        };
        let db = match self.back {
            Some(ref node) => node.get_depth(),
            None => 0,
        };
        1 + cmp::max(df, db)
    }
}

impl<T: Plane> BspNode<T> {
    /// Insert a value into the sub-tree starting with this node.
    /// This operation may spawn additional leafs/branches of the tree.
    pub fn insert(&mut self, value: T) {
        if self.values.is_empty() {
            self.values.push(value);
            return
        }
        match self.values[0].cut(value) {
            PlaneCut::Sibling(value) => self.values.push(value),
            PlaneCut::Cut { front, back } => {
                add_side(&mut self.front, front);
                add_side(&mut self.back, back);
            }
        }
    }

    /// Build the draw order of this sub-tree into an `out` vector,
    /// so that the contained planes are sorted back to front according
    /// to the view vector defines as the `base` plane front direction.
    pub fn order(&self, base: &T, out: &mut Vec<T>) {
        let (former, latter) = match self.values.first() {
            None => return,
            Some(ref first) if base.is_aligned(first) => (&self.front, &self.back),
            Some(_) => (&self.back, &self.front),
        };

        if let Some(ref node) = *former {
            node.order(base, out);
        }

        out.extend_from_slice(&self.values);

        if let Some(ref node) = *latter {
            node.order(base, out);
        }
    }
}


#[cfg(test)]
mod tests {
    extern crate rand;
    use super::*;
    use self::rand::Rng;

    #[derive(Clone, Debug, PartialEq)]
    struct Plane1D(i32, bool);

    impl Plane for Plane1D {
        fn cut(&self, plane: Self) -> PlaneCut<Self> {
            if self.0 == plane.0 {
                PlaneCut::Sibling(plane)
            } else if (plane.0 > self.0) == self.1 {
                PlaneCut::Cut {
                    front: vec![plane],
                    back: vec![],
                }
            } else {
                PlaneCut::Cut {
                    front: vec![],
                    back: vec![plane],
                }
            }
        }

        fn is_aligned(&self, plane: &Self) -> bool {
            self.1 == plane.1
        }
    }


    #[test]
    fn test_add_side() {
        let mut node_opt = None;
        let p0: Vec<Plane1D> = Vec::new();
        add_side(&mut node_opt, p0);
        assert!(node_opt.is_none());

        let p1 = Plane1D(1, true);
        add_side(&mut node_opt, vec![p1.clone()]);
        assert_eq!(node_opt.as_ref().unwrap().values, vec![p1.clone()]);
        assert!(node_opt.as_ref().unwrap().is_leaf());

        let p23 = vec![Plane1D(0, false), Plane1D(2, false)];
        add_side(&mut node_opt, p23);
        let node = node_opt.unwrap();
        assert_eq!(node.values, vec![p1.clone()]);
        assert!(node.front.is_some() && node.back.is_some());
    }

    #[test]
    fn test_insert_depth() {
        let mut node = BspNode::new();
        assert_eq!(node.get_depth(), 0);
        node.insert(Plane1D(0, true));
        assert_eq!(node.get_depth(), 1);
        node.insert(Plane1D(6, true));
        assert_eq!(node.get_depth(), 2);
        node.insert(Plane1D(8, true));
        assert_eq!(node.get_depth(), 3);
        node.insert(Plane1D(6, true));
        assert_eq!(node.get_depth(), 3);
        node.insert(Plane1D(-5, false));
        assert_eq!(node.get_depth(), 3);
    }

    #[test]
    fn test_order() {
        let mut rng = rand::thread_rng();
        let mut node = BspNode::new();
        let mut out = Vec::new();

        node.order(&Plane1D(0, true), &mut out);
        assert!(out.is_empty());

        for _ in 0 .. 100 {
            let plane = Plane1D(rng.gen(), rng.gen());
            node.insert(plane);
        }

        node.order(&Plane1D(0, true), &mut out);
        let mut out2 = out.clone();
        out2.sort_by_key(|p| -p.0);
        assert_eq!(out, out2);
    }
}