Skip to content

Commit

Permalink
Move KdSplit to split module
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Oom committed Jul 18, 2024
1 parent 434ae11 commit e6dcb0f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
11 changes: 2 additions & 9 deletions kdtree/src/build.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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)),
Expand Down Expand Up @@ -61,7 +54,7 @@ pub fn build_kdtree(geometries: Vec<Geometry>, 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};
Expand Down
2 changes: 1 addition & 1 deletion kdtree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions kdtree/src/sah.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
41 changes: 25 additions & 16 deletions kdtree/src/split.rs
Original file line number Diff line number Diff line change
@@ -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<u32>, Vec<u32>, Vec<u32>) {
Expand All @@ -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<u32>,
pub middle_indices: Vec<u32>,
pub right_indices: Vec<u32>,
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<u32>,
pub(crate) middle_indices: Vec<u32>,
pub(crate) right_indices: Vec<u32>,
}

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,
Expand All @@ -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;

Expand Down

0 comments on commit e6dcb0f

Please sign in to comment.