diff --git a/geometry/src/ray.rs b/geometry/src/ray.rs index b6b8f8df..285e2264 100644 --- a/geometry/src/ray.rs +++ b/geometry/src/ray.rs @@ -7,6 +7,10 @@ pub struct Ray { } impl Ray { + pub fn new(origin: Vec3, direction: Vec3) -> Ray { + Ray { origin, direction } + } + pub fn between(a: Vec3, b: Vec3) -> Ray { Ray { origin: a, diff --git a/kdtree-reducer-cli/src/main.rs b/kdtree-reducer-cli/src/main.rs index 33a8c6d9..2f00596e 100644 --- a/kdtree-reducer-cli/src/main.rs +++ b/kdtree-reducer-cli/src/main.rs @@ -118,10 +118,10 @@ struct Args { fn main() { let args = Args::parse(); - let ray = Ray { - origin: [3.897963, 0.24242611, -4.203691].into(), - direction: [-13.897963, 9.757574, 14.2036915].into(), - }; + let ray = Ray::new( + [3.897963, 0.24242611, -4.203691].into(), + [-13.897963, 9.757574, 14.2036915].into(), + ); let expected_intersection = ( 7589, RayIntersection { diff --git a/kdtree-tester-cli/src/main.rs b/kdtree-tester-cli/src/main.rs index 897a85a0..3d717020 100644 --- a/kdtree-tester-cli/src/main.rs +++ b/kdtree-tester-cli/src/main.rs @@ -161,14 +161,14 @@ impl RayBouncer { } let sample = material.sample(&IncomingRay { wi, n, uv }, rng); - let next_ray = Ray { - origin: if sample.wo.dot(n) >= 0.0 { + let next_ray = Ray::new( + if sample.wo.dot(n) >= 0.0 { point_above } else { point_below }, - direction: sample.wo, - }; + sample.wo, + ); self.bounce(rng, &next_ray, accumulated_bounces + 1) } diff --git a/kdtree/src/lib.rs b/kdtree/src/lib.rs index 46ee1645..82b83c60 100644 --- a/kdtree/src/lib.rs +++ b/kdtree/src/lib.rs @@ -428,10 +428,10 @@ mod tests { geometries: vec![triangle.into()], root, }; - let ray = Ray { - origin: Vec3::new(0.0, 0.0, 3.0), - direction: Vec3::new(0.06646079, 0.08247295, -0.9238795), - }; + let ray = Ray::new( + Vec3::new(0.0, 0.0, 3.0), + Vec3::new(0.06646079, 0.08247295, -0.9238795), + ); let actual = tree.intersect(&ray, 0.0..=f32::MAX); @@ -459,10 +459,10 @@ mod tests { root: KdNode::new_node(Aap::new_x(-1.0), KdNode::empty(), KdNode::new_leaf(vec![0])), geometries: vec![triangle.into()], }; - let ray = Ray { - origin: Vec3::new(-0.5170438, -0.4394186, -0.045965273), - direction: Vec3::new(-0.8491798, -0.1408107, -0.5089852), - }; + let ray = Ray::new( + Vec3::new(-0.5170438, -0.4394186, -0.045965273), + Vec3::new(-0.8491798, -0.1408107, -0.5089852), + ); let actual = tree.intersect(&ray, 0.0..=f32::MAX); diff --git a/scene/src/camera.rs b/scene/src/camera.rs index 21fc58a1..f9a3ac53 100644 --- a/scene/src/camera.rs +++ b/scene/src/camera.rs @@ -68,10 +68,7 @@ impl Pinhole { #[inline] pub fn ray(&self, x: f32, y: f32) -> Ray { - Ray { - origin: self.camera.position, - direction: self.plane + x * self.dx + y * self.dy, - } + Ray::new(self.camera.position, self.plane + x * self.dx + y * self.dy) } } diff --git a/tracing/src/pathtracer.rs b/tracing/src/pathtracer.rs index 512badf6..b1135ae3 100644 --- a/tracing/src/pathtracer.rs +++ b/tracing/src/pathtracer.rs @@ -81,14 +81,14 @@ impl Pathtracer { let accumulated_radiance = accumulated_radiance + accumulated_transport * incoming_radiance; let sample = material.sample(&IncomingRay { wi, n, uv }, rng); - let next_ray = Ray { - origin: if sample.wo.dot(n) >= 0.0 { + let next_ray = Ray::new( + if sample.wo.dot(n) >= 0.0 { point_above } else { point_below }, - direction: sample.wo, - }; + sample.wo, + ); if sample.pdf <= 0.01 { return accumulated_radiance; diff --git a/tracing/src/raytracer.rs b/tracing/src/raytracer.rs index e23b1aec..062355e8 100644 --- a/tracing/src/raytracer.rs +++ b/tracing/src/raytracer.rs @@ -47,10 +47,7 @@ impl Raytracer { let this = &self; let material: &MaterialModel = &triangle.material; let direction = light.center - point; - let shadow_ray = Ray { - origin: offset_point, - direction, - }; + let shadow_ray = Ray::new(offset_point, direction); if this.intersect_any(&shadow_ray, 0.0..=1.0) { return Vec3::ZERO; }