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
//! [BSP](https://en.wikipedia.org/wiki/Binary_space_partitioning) tree node structure and operations
#[cfg(not(feature = "parallel"))]
use crate::float_types::EPSILON;
#[cfg(not(feature = "parallel"))]
use crate::mesh::vertex::Vertex;
use crate::float_types::Real;
use crate::mesh::plane::{BACK, COPLANAR, FRONT, Plane, SPANNING};
use crate::mesh::polygon::Polygon;
use std::fmt::Debug;
/// A [BSP](https://en.wikipedia.org/wiki/Binary_space_partitioning) tree node, containing polygons plus optional front/back subtrees
#[derive(Debug, Clone)]
pub struct Node<S: Clone> {
/// Splitting plane for this node *or* **None** for a leaf that
/// only stores polygons.
pub plane: Option<Plane>,
/// Polygons in *front* half‑spaces.
pub front: Option<Box<Node<S>>>,
/// Polygons in *back* half‑spaces.
pub back: Option<Box<Node<S>>>,
/// Polygons that lie *exactly* on `plane`
/// (after the node has been built).
pub polygons: Vec<Polygon<S>>,
}
impl<S: Clone + Send + Sync + Debug> Default for Node<S> {
fn default() -> Self {
Self::new()
}
}
impl<S: Clone + Send + Sync + Debug> Node<S> {
/// Create a new empty BSP node
pub const fn new() -> Self {
Self {
plane: None,
front: None,
back: None,
polygons: Vec::new(),
}
}
/// Creates a new BSP node from polygons
pub fn from_polygons(polygons: &[Polygon<S>]) -> Self {
let mut node = Self::new();
if !polygons.is_empty() {
node.build(polygons);
}
node
}
/// Invert all polygons in the BSP tree
#[cfg(not(feature = "parallel"))]
pub fn invert(&mut self) {
// Flip all polygons and plane in this node
self.polygons.iter_mut().for_each(|p| p.flip());
if let Some(ref mut plane) = self.plane {
plane.flip();
}
if let Some(ref mut front) = self.front {
front.invert();
}
if let Some(ref mut back) = self.back {
back.invert();
}
std::mem::swap(&mut self.front, &mut self.back);
}
pub fn pick_best_splitting_plane(&self, polygons: &[Polygon<S>]) -> Plane {
const K_SPANS: Real = 8.0; // Weight for spanning polygons
const K_BALANCE: Real = 1.0; // Weight for front/back balance
let mut best_plane = polygons[0].plane.clone();
let mut best_score = Real::MAX;
// Take a sample of polygons as candidate planes
let sample_size = polygons.len().min(20);
for p in polygons.iter().take(sample_size) {
let plane = &p.plane;
let mut num_front = 0;
let mut num_back = 0;
let mut num_spanning = 0;
for poly in polygons {
match plane.classify_polygon(poly) {
COPLANAR => {}, // Not counted for balance
FRONT => num_front += 1,
BACK => num_back += 1,
SPANNING => num_spanning += 1,
_ => num_spanning += 1, // Treat any other combination as spanning
}
}
let score = K_SPANS * num_spanning as Real
+ K_BALANCE * ((num_front - num_back) as Real).abs();
if score < best_score {
best_score = score;
best_plane = plane.clone();
}
}
best_plane
}
/// Recursively remove all polygons in `polygons` that are inside this BSP tree
/// **Mathematical Foundation**: Uses plane classification to determine polygon visibility.
/// Polygons entirely in BACK half-space are clipped (removed).
/// **Algorithm**: O(n log d) where n is polygon count, d is tree depth.
#[cfg(not(feature = "parallel"))]
pub fn clip_polygons(&self, polygons: &[Polygon<S>]) -> Vec<Polygon<S>> {
// If this node has no plane (i.e. it’s empty), just return
if self.plane.is_none() {
return polygons.to_vec();
}
let plane = self.plane.as_ref().unwrap();
// Pre-allocate for better performance
let mut front_polys = Vec::with_capacity(polygons.len());
let mut back_polys = Vec::with_capacity(polygons.len());
// Optimized polygon splitting with iterator patterns
for polygon in polygons {
let (coplanar_front, coplanar_back, mut front_parts, mut back_parts) =
plane.split_polygon(polygon);
// Efficient coplanar polygon classification using iterator chain
for coplanar_poly in coplanar_front.into_iter().chain(coplanar_back.into_iter()) {
if plane.orient_plane(&coplanar_poly.plane) == FRONT {
front_parts.push(coplanar_poly);
} else {
back_parts.push(coplanar_poly);
}
}
front_polys.append(&mut front_parts);
back_polys.append(&mut back_parts);
}
// Recursively clip with optimized pattern
let mut result = if let Some(front_node) = &self.front {
front_node.clip_polygons(&front_polys)
} else {
front_polys
};
if let Some(back_node) = &self.back {
result.extend(back_node.clip_polygons(&back_polys));
}
result
}
/// Remove all polygons in this BSP tree that are inside the other BSP tree
#[cfg(not(feature = "parallel"))]
pub fn clip_to(&mut self, bsp: &Node<S>) {
self.polygons = bsp.clip_polygons(&self.polygons);
if let Some(ref mut front) = self.front {
front.clip_to(bsp);
}
if let Some(ref mut back) = self.back {
back.clip_to(bsp);
}
}
/// Return all polygons in this BSP tree using an iterative approach,
/// avoiding potential stack overflow of recursive approach
pub fn all_polygons(&self) -> Vec<Polygon<S>> {
let mut result = Vec::new();
let mut stack = vec![self];
while let Some(node) = stack.pop() {
result.extend_from_slice(&node.polygons);
// Use iterator to add child nodes more efficiently
stack.extend(
[&node.front, &node.back]
.iter()
.filter_map(|child| child.as_ref().map(|boxed| boxed.as_ref())),
);
}
result
}
/// Build a BSP tree from the given polygons
#[cfg(not(feature = "parallel"))]
pub fn build(&mut self, polygons: &[Polygon<S>]) {
if polygons.is_empty() {
return;
}
// Choose the best splitting plane using a heuristic if not already set.
if self.plane.is_none() {
self.plane = Some(self.pick_best_splitting_plane(polygons));
}
let plane = self.plane.as_ref().unwrap();
// Pre-allocate with estimated capacity for better performance
let mut front = Vec::with_capacity(polygons.len() / 2);
let mut back = Vec::with_capacity(polygons.len() / 2);
// Optimized polygon classification using iterator pattern
// **Mathematical Theorem**: Each polygon is classified relative to the splitting plane
for polygon in polygons {
let (coplanar_front, coplanar_back, mut front_parts, mut back_parts) =
plane.split_polygon(polygon);
// Extend collections efficiently with iterator chains
self.polygons.extend(coplanar_front);
self.polygons.extend(coplanar_back);
front.append(&mut front_parts);
back.append(&mut back_parts);
}
// Build child nodes using lazy initialization pattern for memory efficiency
if !front.is_empty() {
self.front
.get_or_insert_with(|| Box::new(Node::new()))
.build(&front);
}
if !back.is_empty() {
self.back
.get_or_insert_with(|| Box::new(Node::new()))
.build(&back);
}
}
/// Slices this BSP node with `slicing_plane`, returning:
/// - All polygons that are coplanar with the plane (within EPSILON),
/// - A list of line‐segment intersections (each a [Vertex; 2]) from polygons that span the plane.
#[cfg(not(feature = "parallel"))]
pub fn slice(&self, slicing_plane: &Plane) -> (Vec<Polygon<S>>, Vec<[Vertex; 2]>) {
let all_polys = self.all_polygons();
let mut coplanar_polygons = Vec::new();
let mut intersection_edges = Vec::new();
for poly in &all_polys {
let vcount = poly.vertices.len();
if vcount < 2 {
continue; // degenerate polygon => skip
}
// Use iterator chain to compute vertex types more efficiently
let types: Vec<_> = poly
.vertices
.iter()
.map(|vertex| slicing_plane.orient_point(&vertex.pos))
.collect();
let polygon_type = types.iter().fold(0, |acc, &vertex_type| acc | vertex_type);
// Based on the combined classification of its vertices:
match polygon_type {
COPLANAR => {
// The entire polygon is in the plane, so push it to the coplanar list.
coplanar_polygons.push(poly.clone());
},
FRONT | BACK => {
// Entirely on one side => no intersection. We skip it.
},
SPANNING => {
// The polygon crosses the plane. We'll gather the intersection points
// (the new vertices introduced on edges that cross the plane).
let crossing_points: Vec<_> = (0..vcount)
.filter_map(|i| {
let j = (i + 1) % vcount;
let ti = types[i];
let tj = types[j];
let vi = &poly.vertices[i];
let vj = &poly.vertices[j];
if (ti | tj) == SPANNING {
let denom = slicing_plane.normal().dot(&(vj.pos - vi.pos));
if denom.abs() > EPSILON {
let intersection = (slicing_plane.offset()
- slicing_plane.normal().dot(&vi.pos.coords))
/ denom;
Some(vi.interpolate(vj, intersection))
} else {
None
}
} else {
None
}
})
.collect();
// Convert crossing points to intersection edges
intersection_edges.extend(
crossing_points
.chunks_exact(2)
.map(|chunk| [chunk[0].clone(), chunk[1].clone()]),
);
},
_ => {
// Shouldn't happen in a typical classification, but we can ignore
},
}
}
(coplanar_polygons, intersection_edges)
}
}
#[cfg(test)]
mod tests {
use crate::mesh::bsp::Node;
use crate::mesh::polygon::Polygon;
use crate::mesh::vertex::Vertex;
use nalgebra::{Point3, Vector3};
#[test]
fn test_bsp_basic_functionality() {
let vertices = vec![
Vertex::new(Point3::new(0.0, 0.0, 0.0), Vector3::new(0.0, 0.0, 1.0)),
Vertex::new(Point3::new(1.0, 0.0, 0.0), Vector3::new(0.0, 0.0, 1.0)),
Vertex::new(Point3::new(0.5, 1.0, 0.0), Vector3::new(0.0, 0.0, 1.0)),
];
let polygon: Polygon<i32> = Polygon::new(vertices, None);
let polygons = vec![polygon];
let node = Node::from_polygons(&polygons);
assert!(!node.all_polygons().is_empty());
}
}