diff --git a/kdtree/src/build.rs b/kdtree/src/build.rs index 81b85220..27e23224 100644 --- a/kdtree/src/build.rs +++ b/kdtree/src/build.rs @@ -1,4 +1,4 @@ -use geometry::{aap::Aap, bound::geometries_bounding_box, geometry::Geometry}; +use geometry::{bound::geometries_bounding_box, geometry::Geometry}; use glam::Vec3; use crate::{ @@ -8,13 +8,6 @@ use crate::{ use super::{KdNode, KdTree}; -#[derive(Debug)] -pub(crate) struct KdSplit { - pub(crate) plane: Aap, - pub(crate) left: KdCell, - pub(crate) right: KdCell, -} - fn starting_box(geometries: &[Geometry]) -> KdCell { KdCell::new( geometries_bounding_box(geometries).enlarge(Vec3::new(1.0, 1.0, 1.0)), @@ -61,7 +54,7 @@ pub fn build_kdtree(geometries: Vec, max_depth: u32, cost: &SahCost) - #[cfg(test)] mod tests { - use geometry::triangle::Triangle; + use geometry::{aap::Aap, triangle::Triangle}; use glam::Vec3; use crate::{build::build_kdtree, KdNode}; diff --git a/kdtree/src/lib.rs b/kdtree/src/lib.rs index 6bfb5ecf..514fa440 100644 --- a/kdtree/src/lib.rs +++ b/kdtree/src/lib.rs @@ -5,7 +5,7 @@ use geometry::{aap::Aap, geometry::Geometry, ray::Ray}; use intersection::{intersect_closest_geometry, KdIntersection}; pub mod build; -pub mod cell; +mod cell; pub mod format; pub mod intersection; pub mod sah; diff --git a/kdtree/src/sah.rs b/kdtree/src/sah.rs index ec45f5dc..2f8b34e6 100644 --- a/kdtree/src/sah.rs +++ b/kdtree/src/sah.rs @@ -1,5 +1,7 @@ -use super::{build::KdSplit, split::split_and_partition}; -use crate::{cell::KdCell, split::SplitPartitioning}; +use crate::{ + cell::KdCell, + split::{partition_clipped_geometries, KdPartitioning, KdSplit}, +}; use geometry::{aap::Aap, geometry::Geometry}; pub struct SahCost { @@ -22,7 +24,7 @@ impl SahCost { empty_factor * (self.traverse_cost + intersect_cost) } - fn calculate_for_split(&self, split: &SplitPartitioning) -> f32 { + fn calculate_for_split(&self, split: &KdPartitioning) -> f32 { let surface_area = split.parent_aabb.surface_area(); let probability_left = split.left_aabb.surface_area() / surface_area; let probability_right = split.right_aabb.surface_area() / surface_area; @@ -47,7 +49,7 @@ impl Default for SahCost { fn select_best_split_based_on_cost( cost: &SahCost, - split: SplitPartitioning, + split: KdPartitioning, ) -> Option<(KdSplit, f32)> { // TODO: Place planes to the left or to the right depending on what gives best cost. if (split.left_aabb.volume() == 0.0 || split.right_aabb.volume() == 0.0) @@ -90,7 +92,7 @@ pub(crate) fn find_best_split( .filter_map(|plane| { select_best_split_based_on_cost( cost, - split_and_partition(&clipped, cell.boundary, plane), + partition_clipped_geometries(&clipped, cell.boundary, plane), ) }) .reduce(min_by_snd) diff --git a/kdtree/src/split.rs b/kdtree/src/split.rs index fcad2e90..93cfe2bc 100644 --- a/kdtree/src/split.rs +++ b/kdtree/src/split.rs @@ -1,6 +1,15 @@ use geometry::{aabb::Aabb, aap::Aap}; -pub(crate) fn partition_triangles( +use crate::cell::KdCell; + +#[derive(Debug)] +pub(crate) struct KdSplit { + pub(crate) plane: Aap, + pub(crate) left: KdCell, + pub(crate) right: KdCell, +} + +fn partition_triangles( clipped_triangles: &[(u32, Aabb)], plane: &Aap, ) -> (Vec, Vec, Vec) { @@ -27,26 +36,26 @@ pub(crate) fn partition_triangles( (left_triangles, middle_triangles, right_triangles) } -pub(crate) struct SplitPartitioning { - pub plane: Aap, - pub parent_aabb: Aabb, - pub left_aabb: Aabb, - pub right_aabb: Aabb, - pub left_indices: Vec, - pub middle_indices: Vec, - pub right_indices: Vec, +pub(crate) struct KdPartitioning { + pub(crate) plane: Aap, + pub(crate) parent_aabb: Aabb, + pub(crate) left_aabb: Aabb, + pub(crate) right_aabb: Aabb, + pub(crate) left_indices: Vec, + pub(crate) middle_indices: Vec, + pub(crate) right_indices: Vec, } -pub(crate) fn split_and_partition( +pub(crate) fn partition_clipped_geometries( clipped: &[(u32, Aabb)], - aabb: Aabb, + parent_aabb: Aabb, plane: Aap, -) -> SplitPartitioning { - let (left_aabb, right_aabb) = aabb.split(&plane); +) -> KdPartitioning { + let (left_aabb, right_aabb) = parent_aabb.split(&plane); let (left_indices, middle_indices, right_indices) = partition_triangles(clipped, &plane); - SplitPartitioning { + KdPartitioning { plane, - parent_aabb: aabb, + parent_aabb, left_aabb, right_aabb, left_indices, @@ -56,7 +65,7 @@ pub(crate) fn split_and_partition( } #[cfg(test)] -mod partition_triangles_tests { +mod tests { use geometry::axis::Axis; use glam::Vec3;