Skip to content

Commit

Permalink
Refactor KdIntersection to GeometryIntersection
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Oom committed Sep 5, 2024
1 parent c17ab10 commit 1084c62
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 95 deletions.
44 changes: 44 additions & 0 deletions geometry/src/intersection.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::ops::RangeInclusive;

use crate::{geometry::Geometry, ray::Ray};

#[derive(Debug, Clone, PartialEq)]
pub struct PointIntersection {
pub u: f32,
Expand Down Expand Up @@ -30,3 +34,43 @@ impl RayIntersection {
RayIntersection { t, u, v }
}
}

#[derive(Debug, Clone, PartialEq)]
pub struct GeometryIntersection {
pub index: u32,
pub inner: RayIntersection,
}

impl GeometryIntersection {
#[inline]
pub fn new(index: u32, inner: RayIntersection) -> Self {
GeometryIntersection { index, inner }
}

#[inline]
pub fn min(self, other: Self) -> Self {
if self.inner.t <= other.inner.t {
self
} else {
other
}
}
}

pub fn intersect_closest_geometry(
geometries: &[Geometry],
indices: impl Iterator<Item = u32>,
ray: &Ray,
t_range: RangeInclusive<f32>,
) -> Option<GeometryIntersection> {
indices
.filter_map(|index| {
let geometry = unsafe { geometries.get_unchecked(index as usize) };
geometry.intersect_ray(ray).and_then(|inner| {
t_range
.contains(&inner.t)
.then_some(GeometryIntersection { index, inner })
})
})
.reduce(GeometryIntersection::min)
}
23 changes: 10 additions & 13 deletions kdtree-tester-cli/src/checked_intersection.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use geometry::ray::Ray;
use kdtree::intersection::KdIntersection;
use geometry::{intersection::GeometryIntersection, ray::Ray};

#[derive(Debug, Clone)]
pub struct CheckedIntersection {
pub ray: Ray,
pub reference: Option<KdIntersection>,
pub kdtree: Option<KdIntersection>,
pub reference: Option<GeometryIntersection>,
pub kdtree: Option<GeometryIntersection>,
}

impl CheckedIntersection {
Expand All @@ -16,9 +15,9 @@ impl CheckedIntersection {
(None, None) => true,
(Some(a), Some(b)) => {
a.index == b.index
&& (a.intersection.t - b.intersection.t).abs() < T_TOLERANCE
&& (a.intersection.u - b.intersection.u).abs() < UV_TOLERANCE
&& (a.intersection.v - b.intersection.v).abs() < UV_TOLERANCE
&& (a.inner.t - b.inner.t).abs() < T_TOLERANCE
&& (a.inner.u - b.inner.u).abs() < UV_TOLERANCE
&& (a.inner.v - b.inner.v).abs() < UV_TOLERANCE
}
_ => false,
}
Expand All @@ -27,17 +26,15 @@ impl CheckedIntersection {
pub fn as_bytes(&self, iteration: u16) -> [u8; 50] {
let mut bytes = [0u8; 50];
let ray = if let Some(kdtree) = &self.kdtree {
&self.ray.extended(kdtree.intersection.t)
&self.ray.extended(kdtree.inner.t)
} else if let Some(reference) = &self.reference {
&self.ray.extended(reference.intersection.t)
&self.ray.extended(reference.inner.t)
} else {
&self.ray
};
let correct_point = self
.ray
.param(self.reference.as_ref().unwrap().intersection.t);
let correct_point = self.ray.param(self.reference.as_ref().unwrap().inner.t);
let actual_point = if let Some(kdtree) = &self.kdtree {
self.ray.param(kdtree.intersection.t)
self.ray.param(kdtree.inner.t)
} else {
[0.0, 0.0, 0.0].into()
};
Expand Down
14 changes: 7 additions & 7 deletions kdtree-tester-cli/src/ray_bouncer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::checked_intersection::CheckedIntersection;
use geometry::ray::Ray;
use glam::{UVec2, Vec2};
use kdtree::{
intersection::{intersect_closest_geometry, KdIntersection},
KdNode,
use geometry::{
intersection::{intersect_closest_geometry, GeometryIntersection},
ray::Ray,
};
use glam::{UVec2, Vec2};
use kdtree::KdNode;
use rand::{rngs::SmallRng, SeedableRng};
use scene::{camera::Pinhole, Scene};
use std::ops::RangeInclusive;
Expand All @@ -26,7 +26,7 @@ impl RayBouncer {
&self,
ray: &Ray,
t_range: RangeInclusive<f32>,
) -> Option<KdIntersection> {
) -> Option<GeometryIntersection> {
let indices = 0u32..self.scene.geometries().len() as u32;
intersect_closest_geometry(self.scene.geometries(), indices, ray, t_range)
}
Expand Down Expand Up @@ -63,7 +63,7 @@ impl RayBouncer {
};
let intersection = intersection.reference?;
let intersection_index = intersection.index;
let intersection = intersection.intersection;
let intersection = intersection.inner;

let wi = -ray.direction;
let n = self.scene.get_normal(intersection_index, &intersection);
Expand Down
18 changes: 10 additions & 8 deletions kdtree-tester-cli/src/reducer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ use std::{

use rand::{rngs::SmallRng, seq::SliceRandom, SeedableRng};

use geometry::{geometry::Geometry, intersection::RayIntersection, ray::Ray, triangle::Triangle};
use kdtree::{
build::build_kdtree, format::write_tree_json, intersection::KdIntersection, sah::SahCost,
KdNode,
use geometry::{
geometry::Geometry,
intersection::{GeometryIntersection, RayIntersection},
ray::Ray,
triangle::Triangle,
};
use kdtree::{build::build_kdtree, format::write_tree_json, sah::SahCost, KdNode};
use wavefront::obj;

use crate::checked_intersection::CheckedIntersection;
Expand All @@ -28,7 +30,7 @@ fn verify_removal(
) -> bool {
let intersection = tree.intersect(geometries, ray, 0.0..=f32::MAX).unwrap();
let same_geometry = geometries[intersection.index as usize] == actual.0;
let same_intersection = intersection.intersection == actual.1;
let same_intersection = intersection.inner == actual.1;
same_geometry && same_intersection
}

Expand All @@ -53,7 +55,7 @@ fn reduce_tree(
) -> (Vec<Geometry>, KdNode) {
let actual = (
geometries[intersection.kdtree.as_ref().unwrap().index as usize].clone(),
intersection.kdtree.as_ref().unwrap().intersection.clone(),
intersection.kdtree.as_ref().unwrap().inner.clone(),
);
let mut geometries = geometries;
geometries.swap(0, intersection.reference.unwrap().index as usize);
Expand Down Expand Up @@ -99,15 +101,15 @@ pub(crate) fn kdtree_reduce(input: PathBuf, output: PathBuf, fail: Option<PathBu
[3.897963, 0.24242611, -4.203691].into(),
[-13.897963, 9.757574, 14.2036915].into(),
),
reference: Some(KdIntersection::new(
reference: Some(GeometryIntersection::new(
7589,
RayIntersection {
t: 0.0004729527,
u: 0.09395919,
v: 0.47453666,
},
)),
kdtree: Some(KdIntersection::new(
kdtree: Some(GeometryIntersection::new(
5556,
RayIntersection {
t: 0.05429069,
Expand Down
47 changes: 0 additions & 47 deletions kdtree/src/intersection.rs

This file was deleted.

Loading

0 comments on commit 1084c62

Please sign in to comment.