beevee: bvh: remove reference to objects in BVH

This commit is contained in:
Bruno BELANYI 2020-03-24 20:29:29 +01:00
parent 69779d7dd4
commit 45fca6f3ed

View file

@ -25,12 +25,11 @@ struct Node {
/// ///
/// [`Bounded`]: ../aabb/trait.Bounded.html /// [`Bounded`]: ../aabb/trait.Bounded.html
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct BVH<'o, O: Bounded> { pub struct BVH {
objects: &'o [O],
tree: Node, tree: Node,
} }
impl<'o, O: Bounded> BVH<'o, O> { impl BVH {
/// Build a [`BVH`] for the given slice of objects. /// Build a [`BVH`] for the given slice of objects.
/// Each leaf node will be built in a way to try and contain less than 32 objects. /// Each leaf node will be built in a way to try and contain less than 32 objects.
/// ///
@ -40,6 +39,7 @@ impl<'o, O: Bounded> BVH<'o, O> {
/// use beevee::aabb::{AABB, Bounded}; /// use beevee::aabb::{AABB, Bounded};
/// use beevee::bvh::BVH; /// use beevee::bvh::BVH;
/// ///
/// #[derive(Clone, Debug, PartialEq)]
/// struct Sphere { /// struct Sphere {
/// center: Point, /// center: Point,
/// radius: f32, /// radius: f32,
@ -58,7 +58,7 @@ impl<'o, O: Bounded> BVH<'o, O> {
/// let spheres: &mut [Sphere] = &mut [Sphere{ center: Point::origin(), radius: 2.5 }]; /// let spheres: &mut [Sphere] = &mut [Sphere{ center: Point::origin(), radius: 2.5 }];
/// let bvh = BVH::build(spheres); /// let bvh = BVH::build(spheres);
/// ``` /// ```
pub fn build(objects: &'o mut [O]) -> Self { pub fn build<O: Bounded>(objects: &mut [O]) -> Self {
Self::with_max_capacity(objects, 32) Self::with_max_capacity(objects, 32)
} }
@ -72,6 +72,7 @@ impl<'o, O: Bounded> BVH<'o, O> {
/// use beevee::aabb::{AABB, Bounded}; /// use beevee::aabb::{AABB, Bounded};
/// use beevee::bvh::BVH; /// use beevee::bvh::BVH;
/// ///
/// #[derive(Clone, Debug, PartialEq)]
/// struct Sphere { /// struct Sphere {
/// center: Point, /// center: Point,
/// radius: f32, /// radius: f32,
@ -90,9 +91,9 @@ impl<'o, O: Bounded> BVH<'o, O> {
/// let spheres: &mut [Sphere] = &mut [Sphere{ center: Point::origin(), radius: 2.5 }]; /// let spheres: &mut [Sphere] = &mut [Sphere{ center: Point::origin(), radius: 2.5 }];
/// let bvh = BVH::with_max_capacity(spheres, 32); /// let bvh = BVH::with_max_capacity(spheres, 32);
/// ``` /// ```
pub fn with_max_capacity(objects: &'o mut [O], max_cap: usize) -> Self { pub fn with_max_capacity<O: Bounded>(objects: &mut [O], max_cap: usize) -> Self {
let tree = build_node(objects, 0, objects.len(), max_cap); let tree = build_node(objects, 0, objects.len(), max_cap);
Self { objects, tree } Self { tree }
} }
/// Return the true if the [`BVH`] has been built soundly: /// Return the true if the [`BVH`] has been built soundly:
@ -106,6 +107,7 @@ impl<'o, O: Bounded> BVH<'o, O> {
/// # use beevee::aabb::{AABB, Bounded}; /// # use beevee::aabb::{AABB, Bounded};
/// # use beevee::bvh::BVH; /// # use beevee::bvh::BVH;
/// # /// #
/// # #[derive(Clone, Debug, PartialEq)]
/// # struct Sphere { /// # struct Sphere {
/// # center: Point, /// # center: Point,
/// # radius: f32, /// # radius: f32,
@ -124,9 +126,10 @@ impl<'o, O: Bounded> BVH<'o, O> {
/// // Using the same sphere definition than build /// // Using the same sphere definition than build
/// let spheres: &mut [Sphere] = &mut [Sphere{ center: Point::origin(), radius: 2.5 }]; /// let spheres: &mut [Sphere] = &mut [Sphere{ center: Point::origin(), radius: 2.5 }];
/// let bvh = BVH::with_max_capacity(spheres, 32); /// let bvh = BVH::with_max_capacity(spheres, 32);
/// assert!(bvh.is_sound()); /// assert!(bvh.is_sound(spheres));
/// ``` /// ```
pub fn is_sound(&self) -> bool {
pub fn is_sound<O: Bounded>(&self, objects: &[O]) -> bool {
fn check_node<O: Bounded>(objects: &[O], node: &Node) -> bool { fn check_node<O: Bounded>(objects: &[O], node: &Node) -> bool {
if node.begin > node.end { if node.begin > node.end {
return false; return false;
@ -146,7 +149,7 @@ impl<'o, O: Bounded> BVH<'o, O> {
} }
} }
}; };
check_node(self.objects, &self.tree) check_node(objects, &self.tree)
} }
} }
@ -220,7 +223,6 @@ fn compute_sah<O: Bounded>(objects: &mut [O], surface: f32, max_cap: usize) -> (
left_surfaces.clear(); left_surfaces.clear();
right_surfaces.clear(); right_surfaces.clear();
// Sort in order along the axis // Sort in order along the axis
// FIXME: use parallel sort
objects.sort_by(|lhs, rhs| { objects.sort_by(|lhs, rhs| {
lhs.centroid()[axis] lhs.centroid()[axis]
.partial_cmp(&rhs.centroid()[axis]) .partial_cmp(&rhs.centroid()[axis])