bsp-tree 0.2.1

Binary Space Partitioning (BSP) tree useful for 3D rendering. Works with flat polygons (triangles, quads, etc.).
Documentation
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! BSP tree container and construction.

use nalgebra::Point3;

use crate::{Classification, Cuttable, Polygon};

use super::node::{faces_same_direction, BspNode};
use super::selector::PlaneSelector;
use super::visitor::BspVisitor;

/// A Binary Space Partitioning tree for 3D polygons.
///
/// BSP trees recursively partition space using planes, enabling efficient
/// spatial queries and ordered traversal. Each node contains polygons that
/// are coplanar with its splitting plane, while non-coplanar polygons are
/// stored in front or back subtrees.
///
/// # Construction
///
/// Trees are built from a collection of polygons using a [`PlaneSelector`]
/// to choose splitting planes:
///
/// ```ignore
/// use bsp_tree::{BspTree, Polygon, FirstPolygon};
///
/// let polygons: Vec<Polygon> = /* ... */;
/// let tree = BspTree::build(polygons, &FirstPolygon);
/// ```
///
/// # Traversal
///
/// The tree supports front-to-back and back-to-front traversal relative to
/// a viewpoint, useful for painter's algorithm rendering:
///
/// ```ignore
/// tree.traverse_back_to_front(eye_position, &mut visitor);
/// tree.traverse_front_to_back(eye_position, &mut visitor);
/// ```
#[derive(Debug, Clone, Default)]
pub struct BspTree {
    root: Option<BspNode>,
}

impl BspTree {
    /// Creates an empty BSP tree.
    pub fn new() -> Self {
        Self { root: None }
    }

    /// Builds a BSP tree from a collection of polygons.
    ///
    /// Uses the provided [`PlaneSelector`] to choose splitting planes during
    /// construction. Polygons that span a splitting plane are automatically
    /// split using the [`Cuttable`] trait.
    ///
    /// Returns an empty tree if the input is empty.
    pub fn build<S: PlaneSelector>(polygons: Vec<Polygon>, selector: &S) -> Self {
        Self {
            root: build_node(polygons, selector),
        }
    }

    /// Builds a BSP tree using the default plane selector ([`FirstPolygon`]).
    pub fn from_polygons(polygons: Vec<Polygon>) -> Self {
        use super::selector::FirstPolygon;
        Self::build(polygons, &FirstPolygon)
    }

    /// Returns `true` if the tree contains no polygons.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.root.is_none()
    }

    /// Returns a reference to the root node, if any.
    #[inline]
    pub fn root(&self) -> Option<&BspNode> {
        self.root.as_ref()
    }

    /// Returns a mutable reference to the root node, if any.
    ///
    /// This is primarily for future insert operations.
    #[inline]
    pub fn root_mut(&mut self) -> Option<&mut BspNode> {
        self.root.as_mut()
    }

    /// Returns the total number of polygons in the tree.
    pub fn polygon_count(&self) -> usize {
        self.root.as_ref().map_or(0, |n| n.polygon_count())
    }

    /// Returns the maximum depth of the tree (0 for empty tree).
    pub fn depth(&self) -> usize {
        self.root.as_ref().map_or(0, |n| n.depth())
    }

    /// Traverses the tree front-to-back relative to the given viewpoint.
    ///
    /// Useful for early-Z occlusion culling in modern renderers with depth
    /// buffers, where drawing front objects first allows the GPU to reject
    /// occluded fragments.
    ///
    /// The visitor's `visit` method is called for each group of coplanar
    /// polygons, in front-to-back order (nearest first).
    pub fn traverse_front_to_back<V: BspVisitor>(&self, eye: Point3<f32>, visitor: &mut V) {
        if let Some(ref root) = self.root {
            traverse_front_to_back_node(root, eye, visitor);
        }
    }

    /// Traverses the tree back-to-front relative to the given viewpoint.
    ///
    /// This is the classic painter's algorithm ordering: far polygons are
    /// visited first, then closer polygons, so they can be drawn on top.
    /// Also useful for correct alpha blending of transparent surfaces.
    pub fn traverse_back_to_front<V: BspVisitor>(&self, eye: Point3<f32>, visitor: &mut V) {
        if let Some(ref root) = self.root {
            traverse_back_to_front_node(root, eye, visitor);
        }
    }

    /// Collects all polygons in the tree into a vector.
    ///
    /// The order of polygons is not guaranteed.
    pub fn collect_polygons(&self) -> Vec<Polygon> {
        let mut result = Vec::with_capacity(self.polygon_count());
        collect_polygons_recursive(self.root.as_ref(), &mut result);
        result
    }

    // TODO: Future insert operation
    // pub fn insert(&mut self, polygon: Polygon) { ... }
}

/// Recursively builds a BSP node from a list of polygons.
fn build_node<S: PlaneSelector>(mut polygons: Vec<Polygon>, selector: &S) -> Option<BspNode> {
    if polygons.is_empty() {
        return None;
    }

    // Select the splitting polygon and derive the plane
    let splitter_idx = polygons
        .iter()
        .position(|p| Some(p) == selector.select(&polygons))?;

    let splitter = polygons.swap_remove(splitter_idx);
    let plane = splitter.plane();

    // Initialize lists
    let mut coplanar_front = Vec::new();
    let mut coplanar_back = Vec::new();
    let mut front_list = Vec::new();
    let mut back_list = Vec::new();

    // The splitter itself is coplanar - determine its facing
    if faces_same_direction(&splitter, &plane) {
        coplanar_front.push(splitter);
    } else {
        coplanar_back.push(splitter);
    }

    // Classify and partition remaining polygons
    for polygon in polygons {
        match polygon.classify(&plane) {
            Classification::Front => {
                front_list.push(polygon);
            }
            Classification::Back => {
                back_list.push(polygon);
            }
            Classification::Coplanar => {
                if faces_same_direction(&polygon, &plane) {
                    coplanar_front.push(polygon);
                } else {
                    coplanar_back.push(polygon);
                }
            }
            Classification::Spanning => {
                let (front_part, back_part) = polygon.cut(&plane);
                if let Some(f) = front_part {
                    front_list.push(f);
                }
                if let Some(b) = back_part {
                    back_list.push(b);
                }
            }
        }
    }

    // Build the node with children
    let mut node = BspNode::with_coplanar(plane, coplanar_front, coplanar_back);
    node.set_front(build_node(front_list, selector));
    node.set_back(build_node(back_list, selector));

    Some(node)
}

/// Traverses a node subtree front-to-back.
fn traverse_front_to_back_node<V: BspVisitor>(node: &BspNode, eye: Point3<f32>, visitor: &mut V) {
    let side = node.plane().classify_point(eye);

    // Collect coplanar polygons for visiting
    let coplanar: Vec<Polygon> = node.all_coplanar().cloned().collect();

    match side {
        crate::PlaneSide::Front | crate::PlaneSide::OnPlane => {
            // Eye is in front: front subtree is closer
            if let Some(front) = node.front() {
                traverse_front_to_back_node(front, eye, visitor);
            }
            if !coplanar.is_empty() {
                visitor.visit(&coplanar);
            }
            if let Some(back) = node.back() {
                traverse_front_to_back_node(back, eye, visitor);
            }
        }
        crate::PlaneSide::Back => {
            // Eye is behind: back subtree is closer
            if let Some(back) = node.back() {
                traverse_front_to_back_node(back, eye, visitor);
            }
            if !coplanar.is_empty() {
                visitor.visit(&coplanar);
            }
            if let Some(front) = node.front() {
                traverse_front_to_back_node(front, eye, visitor);
            }
        }
    }
}

/// Traverses a node subtree back-to-front.
fn traverse_back_to_front_node<V: BspVisitor>(node: &BspNode, eye: Point3<f32>, visitor: &mut V) {
    let side = node.plane().classify_point(eye);

    let coplanar: Vec<Polygon> = node.all_coplanar().cloned().collect();

    match side {
        crate::PlaneSide::Front | crate::PlaneSide::OnPlane => {
            // Eye is in front: back subtree is farther
            if let Some(back) = node.back() {
                traverse_back_to_front_node(back, eye, visitor);
            }
            if !coplanar.is_empty() {
                visitor.visit(&coplanar);
            }
            if let Some(front) = node.front() {
                traverse_back_to_front_node(front, eye, visitor);
            }
        }
        crate::PlaneSide::Back => {
            // Eye is behind: front subtree is farther
            if let Some(front) = node.front() {
                traverse_back_to_front_node(front, eye, visitor);
            }
            if !coplanar.is_empty() {
                visitor.visit(&coplanar);
            }
            if let Some(back) = node.back() {
                traverse_back_to_front_node(back, eye, visitor);
            }
        }
    }
}

/// Recursively collects all polygons from a node subtree.
fn collect_polygons_recursive(node: Option<&BspNode>, result: &mut Vec<Polygon>) {
    if let Some(n) = node {
        result.extend(n.all_coplanar().cloned());
        collect_polygons_recursive(n.front(), result);
        collect_polygons_recursive(n.back(), result);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bsp::visitor::CollectingVisitor;
    use nalgebra::Point3;

    fn make_triangle(a: [f32; 3], b: [f32; 3], c: [f32; 3]) -> Polygon {
        Polygon::new(vec![
            Point3::new(a[0], a[1], a[2]),
            Point3::new(b[0], b[1], b[2]),
            Point3::new(c[0], c[1], c[2]),
        ])
    }

    #[test]
    fn empty_tree() {
        let tree = BspTree::new();
        assert!(tree.is_empty());
        assert_eq!(tree.polygon_count(), 0);
        assert_eq!(tree.depth(), 0);
    }

    #[test]
    fn build_empty() {
        let tree = BspTree::from_polygons(vec![]);
        assert!(tree.is_empty());
    }

    #[test]
    fn build_single_polygon() {
        let poly = make_triangle([0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]);
        let tree = BspTree::from_polygons(vec![poly]);

        assert!(!tree.is_empty());
        assert_eq!(tree.polygon_count(), 1);
        assert_eq!(tree.depth(), 1);
    }

    #[test]
    fn build_two_parallel_polygons() {
        // Two triangles on parallel planes (not coplanar)
        let poly1 = make_triangle([0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]);
        let poly2 = make_triangle([0.0, 0.0, 1.0], [1.0, 0.0, 1.0], [0.0, 1.0, 1.0]);

        let tree = BspTree::from_polygons(vec![poly1, poly2]);

        assert_eq!(tree.polygon_count(), 2);
        // One should be in front or back of the other
        assert!(tree.depth() >= 1);
    }

    #[test]
    fn build_coplanar_same_facing() {
        // Two triangles on the same plane, same winding
        let poly1 = make_triangle([0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]);
        let poly2 = make_triangle([1.0, 0.0, 0.0], [2.0, 0.0, 0.0], [1.0, 1.0, 0.0]);

        let tree = BspTree::from_polygons(vec![poly1, poly2]);

        assert_eq!(tree.polygon_count(), 2);
        // Both coplanar, should be in same node
        assert_eq!(tree.depth(), 1);

        let root = tree.root().unwrap();
        assert_eq!(root.coplanar_count(), 2);
    }

    #[test]
    fn build_spanning_polygon_gets_split() {
        // First polygon on Y=0 plane
        let splitter = make_triangle([0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]);

        // Second polygon spans the Y=0 plane
        let spanning = make_triangle([-0.5, -1.0, 0.5], [0.5, 1.0, 0.5], [0.5, -1.0, 0.5]);

        let tree = BspTree::from_polygons(vec![splitter, spanning]);

        // Original was 2 polygons, but spanning got split into 2
        // So we should have 3 total
        assert_eq!(tree.polygon_count(), 3);
    }

    #[test]
    fn traverse_front_to_back_single() {
        let poly = make_triangle([0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]);
        let tree = BspTree::from_polygons(vec![poly.clone()]);

        let mut visitor = CollectingVisitor::new();
        tree.traverse_front_to_back(Point3::new(0.0, 0.0, 10.0), &mut visitor);

        assert_eq!(visitor.polygons().len(), 1);
    }

    #[test]
    fn traverse_front_to_back_ordering() {
        // Create two polygons at different Z depths
        // poly_near at z=1, poly_far at z=-1
        let poly_near = make_triangle([0.0, 0.0, 1.0], [1.0, 0.0, 1.0], [0.0, 1.0, 1.0]);
        let poly_far = make_triangle([0.0, 0.0, -1.0], [1.0, 0.0, -1.0], [0.0, 1.0, -1.0]);

        let tree = BspTree::from_polygons(vec![poly_far.clone(), poly_near.clone()]);

        // Eye at z=10 looking toward origin
        let mut visitor = CollectingVisitor::new();
        tree.traverse_front_to_back(Point3::new(0.5, 0.5, 10.0), &mut visitor);

        let collected = visitor.into_polygons();
        assert_eq!(collected.len(), 2);

        // Front-to-back from z=10: poly_near (z=1) should come first
        // (it's in front from the eye's perspective)
        let first_z = collected[0].centroid().z;
        let second_z = collected[1].centroid().z;
        assert!(
            first_z > second_z,
            "Expected front-to-back order: first_z={} should be > second_z={}",
            first_z,
            second_z
        );
    }

    #[test]
    fn traverse_back_to_front_ordering() {
        let poly_near = make_triangle([0.0, 0.0, 1.0], [1.0, 0.0, 1.0], [0.0, 1.0, 1.0]);
        let poly_far = make_triangle([0.0, 0.0, -1.0], [1.0, 0.0, -1.0], [0.0, 1.0, -1.0]);

        let tree = BspTree::from_polygons(vec![poly_far.clone(), poly_near.clone()]);

        let mut visitor = CollectingVisitor::new();
        tree.traverse_back_to_front(Point3::new(0.5, 0.5, 10.0), &mut visitor);

        let collected = visitor.into_polygons();
        assert_eq!(collected.len(), 2);

        // Back-to-front: poly_far (z=-1) should come first
        let first_z = collected[0].centroid().z;
        let second_z = collected[1].centroid().z;
        assert!(
            first_z < second_z,
            "Expected back-to-front order: first_z={} should be < second_z={}",
            first_z,
            second_z
        );
    }

    #[test]
    fn collect_polygons() {
        let poly1 = make_triangle([0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]);
        let poly2 = make_triangle([0.0, 0.0, 1.0], [1.0, 0.0, 1.0], [0.0, 1.0, 1.0]);
        let poly3 = make_triangle([0.0, 0.0, 2.0], [1.0, 0.0, 2.0], [0.0, 1.0, 2.0]);

        let tree = BspTree::from_polygons(vec![poly1, poly2, poly3]);
        let collected = tree.collect_polygons();

        assert_eq!(collected.len(), 3);
    }
}