ploc_bvh/
lib.rs

1#![doc = include_str!("../README.md")]
2#![warn(missing_docs)]
3
4mod morton;
5mod search;
6
7pub mod dim2;
8pub mod dim3;
9
10/// A generic bounding volume supported by the BVH. Adds a few extra methods on top of bevy's
11/// [`BoundingVolume`](bevy_math::bounding::BoundingVolume) trait
12pub trait BvhVolume: bevy_math::bounding::BoundingVolume + Clone + Debug {
13    /// An infinite bounding volume at the zero position
14    const INFINITY: Self;
15
16    /// Get the morton code for the center of the volume
17    fn morton_code(&self) -> usize;
18}
19
20/// A generic BVH, can support any dimension that gets an implementation.
21pub struct Bvh<Volume: BvhVolume, T: Copy> {
22    nodes: Vec<BvhNode<Volume>>,
23    items: Vec<BvhItem<Volume, T>>,
24}
25
26impl<Volume: BvhVolume, T: Copy> Default for Bvh<Volume, T> {
27    fn default() -> Self {
28        Self {
29            nodes: Vec::new(),
30            items: Vec::new(),
31        }
32    }
33}
34
35impl<Volume: BvhVolume, T: Copy> Bvh<Volume, T> {
36    /// Get the number of nodes in the BVH. The number of nodes is somewhere between
37    /// the number of items (n) and 2n - 1
38    pub fn n_nodes(&self) -> usize {
39        self.nodes.len()
40    }
41
42    /// Get the number of items in the BVH
43    pub fn n_items(&self) -> usize {
44        self.items.len()
45    }
46
47    /// Get an iterator over the BVH's nodes
48    pub fn nodes(&self) -> impl Iterator<Item = &BvhNode<Volume>> {
49        self.nodes.iter()
50    }
51
52    /// Get an iterator over the BVH's items
53    pub fn items(&self) -> impl Iterator<Item = &BvhItem<Volume, T>> {
54        self.items.iter()
55    }
56}
57
58mod construct;
59mod debug;
60
61pub mod traverse;
62
63pub mod prelude {
64    //! The prelude, exporting all the necessary things to get started
65
66    pub use crate::{dim2::*, dim3::*, traverse::Stack};
67}
68
69use std::fmt::Debug;
70
71/// A node on the BVH
72#[derive(Clone, Copy, Debug)]
73pub struct BvhNode<Volume: BvhVolume> {
74    /// The volume of the node
75    pub volume: Volume,
76    /// The number of leaves. 0 if the node points to other nodes
77    pub count: u32,
78    /// The start index of the leaves. If count is 0 this points to other nodes
79    pub start_index: u32,
80}
81
82/// An item in the BHV
83#[derive(Clone, Copy, Debug)]
84pub struct BvhItem<Volume: BvhVolume, T: Copy> {
85    /// The volume of the item
86    pub volume: Volume,
87    /// The value of the bvh item
88    pub t: T,
89}