pub struct BspTree { /* private fields */ }Expand description
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:
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:
tree.traverse_back_to_front(eye_position, &mut visitor);
tree.traverse_front_to_back(eye_position, &mut visitor);Implementations§
Source§impl BspTree
impl BspTree
Sourcepub fn build<S: PlaneSelector>(polygons: Vec<Polygon>, selector: &S) -> Self
pub fn build<S: PlaneSelector>(polygons: Vec<Polygon>, selector: &S) -> Self
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.
Sourcepub fn from_polygons(polygons: Vec<Polygon>) -> Self
pub fn from_polygons(polygons: Vec<Polygon>) -> Self
Builds a BSP tree using the default plane selector ([FirstPolygon]).
Sourcepub fn root_mut(&mut self) -> Option<&mut BspNode>
pub fn root_mut(&mut self) -> Option<&mut BspNode>
Returns a mutable reference to the root node, if any.
This is primarily for future insert operations.
Sourcepub fn polygon_count(&self) -> usize
pub fn polygon_count(&self) -> usize
Returns the total number of polygons in the tree.
Sourcepub fn traverse_front_to_back<V: BspVisitor>(
&self,
eye: Point3<f32>,
visitor: &mut V,
)
pub fn traverse_front_to_back<V: BspVisitor>( &self, eye: Point3<f32>, visitor: &mut V, )
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).
Sourcepub fn traverse_back_to_front<V: BspVisitor>(
&self,
eye: Point3<f32>,
visitor: &mut V,
)
pub fn traverse_back_to_front<V: BspVisitor>( &self, eye: Point3<f32>, visitor: &mut V, )
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.
Sourcepub fn collect_polygons(&self) -> Vec<Polygon>
pub fn collect_polygons(&self) -> Vec<Polygon>
Collects all polygons in the tree into a vector.
The order of polygons is not guaranteed.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for BspTree
impl RefUnwindSafe for BspTree
impl Send for BspTree
impl Sync for BspTree
impl Unpin for BspTree
impl UnsafeUnpin for BspTree
impl UnwindSafe for BspTree
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.