BspTree

Struct BspTree 

Source
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_front_to_back(eye_position, &mut visitor);

§Future: Insertion

The data structure supports insertion of polygons into an existing tree. This operation will be implemented in a future version.

Implementations§

Source§

impl BspTree

Source

pub fn new() -> Self

Creates an empty BSP tree.

Source

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.

Source

pub fn from_polygons(polygons: Vec<Polygon>) -> Self

Builds a BSP tree using the default plane selector ([FirstPolygon]).

Source

pub fn is_empty(&self) -> bool

Returns true if the tree contains no polygons.

Source

pub fn root(&self) -> Option<&BspNode>

Returns a reference to the root node, if any.

Source

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.

Source

pub fn polygon_count(&self) -> usize

Returns the total number of polygons in the tree.

Source

pub fn depth(&self) -> usize

Returns the maximum depth of the tree (0 for empty tree).

Source

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).

Source

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.

Source

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§

Source§

impl Clone for BspTree

Source§

fn clone(&self) -> BspTree

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BspTree

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BspTree

Source§

fn default() -> BspTree

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.