From 5ec5060655888c0e4f9881a2f5edea1f30e51015 Mon Sep 17 00:00:00 2001 From: Walther Date: Wed, 3 Jul 2024 00:42:28 +0300 Subject: [PATCH] feat: add HitableTrait::centroid method --- clovers/src/bvh/hitable_trait.rs | 5 +++++ clovers/src/hitable.rs | 10 +++++++++- clovers/src/objects/boxy.rs | 5 +++++ clovers/src/objects/constant_medium.rs | 4 ++++ clovers/src/objects/gltf.rs | 10 ++++++++++ clovers/src/objects/moving_sphere.rs | 5 +++++ clovers/src/objects/quad.rs | 5 +++++ clovers/src/objects/rotate.rs | 5 +++++ clovers/src/objects/sphere.rs | 4 ++++ clovers/src/objects/stl.rs | 5 +++++ clovers/src/objects/translate.rs | 5 +++++ clovers/src/objects/triangle.rs | 5 +++++ 12 files changed, 67 insertions(+), 1 deletion(-) diff --git a/clovers/src/bvh/hitable_trait.rs b/clovers/src/bvh/hitable_trait.rs index ffd473fb..4d4cae39 100644 --- a/clovers/src/bvh/hitable_trait.rs +++ b/clovers/src/bvh/hitable_trait.rs @@ -129,4 +129,9 @@ impl<'scene> HitableTrait for BVHNode<'scene> { } } } + + // TODO: remove? + fn centroid(&self) -> Position { + self.bounding_box.centroid() + } } diff --git a/clovers/src/hitable.rs b/clovers/src/hitable.rs index 5ccd3f1d..14ba1eaf 100644 --- a/clovers/src/hitable.rs +++ b/clovers/src/hitable.rs @@ -11,7 +11,7 @@ use crate::{ objects::{Boxy, ConstantMedium, MovingSphere, Quad, RotateY, Sphere, Translate, Triangle}, ray::Ray, wavelength::Wavelength, - Direction, Displacement, Float, HitRecord, Position, + Direction, Displacement, Float, HitRecord, Position, Vec3, }; use enum_dispatch::enum_dispatch; @@ -70,6 +70,10 @@ impl HitableTrait for Empty { ) -> Float { 0.0 } + + fn centroid(&self) -> Position { + Vec3::new(0.0, 0.0, 0.0) + } } #[enum_dispatch] @@ -107,6 +111,10 @@ pub trait HitableTrait { "HitableTrait::random called for a Hitable that has no implementation for it!" ); } + + /// Returns the center point of the hitable + #[must_use] + fn centroid(&self) -> Position; } /// Returns a tuple of `(front_face, normal)`. Used in lieu of `set_face_normal` in the Ray Tracing for the Rest Of Your Life book. diff --git a/clovers/src/objects/boxy.rs b/clovers/src/objects/boxy.rs index e716066f..7092ac7a 100644 --- a/clovers/src/objects/boxy.rs +++ b/clovers/src/objects/boxy.rs @@ -129,4 +129,9 @@ impl<'scene> HitableTrait for Boxy<'scene> { sum } + + // TODO: correctness for rotations/translations? + fn centroid(&self) -> Position { + self.aabb.centroid() + } } diff --git a/clovers/src/objects/constant_medium.rs b/clovers/src/objects/constant_medium.rs index 032ded6c..16d6c7ab 100644 --- a/clovers/src/objects/constant_medium.rs +++ b/clovers/src/objects/constant_medium.rs @@ -145,4 +145,8 @@ impl<'scene> HitableTrait for ConstantMedium<'scene> { self.boundary .pdf_value(origin, direction, wavelength, time, rng) } + + fn centroid(&self) -> Position { + self.boundary.centroid() + } } diff --git a/clovers/src/objects/gltf.rs b/clovers/src/objects/gltf.rs index 3fb6c9d9..cad1edf3 100644 --- a/clovers/src/objects/gltf.rs +++ b/clovers/src/objects/gltf.rs @@ -110,6 +110,11 @@ impl<'scene> HitableTrait for GLTF<'scene> { self.bvhnode .pdf_value(origin, direction, wavelength, time, rng) } + + // TODO: correctness? + fn centroid(&self) -> Position { + self.aabb.centroid() + } } fn parse_node<'scene>( @@ -357,6 +362,11 @@ impl<'scene> HitableTrait for GLTFTriangle<'scene> { None => 0.0, } } + + // TODO: correctness + fn centroid(&self) -> Position { + self.q + (self.u / 4.0) + (self.v / 4.0) + } } #[must_use] diff --git a/clovers/src/objects/moving_sphere.rs b/clovers/src/objects/moving_sphere.rs index 99ce8e01..fccfbc6e 100644 --- a/clovers/src/objects/moving_sphere.rs +++ b/clovers/src/objects/moving_sphere.rs @@ -177,4 +177,9 @@ impl<'scene> HitableTrait for MovingSphere<'scene> { // TODO: fix 0.0 } + + fn centroid(&self) -> Position { + // TODO: proper time support + self.center(0.5) + } } diff --git a/clovers/src/objects/quad.rs b/clovers/src/objects/quad.rs index 8a3af9d8..f6a13b11 100644 --- a/clovers/src/objects/quad.rs +++ b/clovers/src/objects/quad.rs @@ -173,6 +173,11 @@ impl<'scene> HitableTrait for Quad<'scene> { + (rng.gen::() * self.v); point - origin } + + // TODO: correctness + fn centroid(&self) -> Position { + self.q + (self.u / 2.0) + (self.v / 2.0) + } } #[must_use] diff --git a/clovers/src/objects/rotate.rs b/clovers/src/objects/rotate.rs index 6847f71f..6cf6698f 100644 --- a/clovers/src/objects/rotate.rs +++ b/clovers/src/objects/rotate.rs @@ -176,4 +176,9 @@ impl<'scene> HitableTrait for RotateY<'scene> { // TODO: fix 0.0 } + + // TODO: correctness! + fn centroid(&self) -> Position { + self.object.centroid() + } } diff --git a/clovers/src/objects/sphere.rs b/clovers/src/objects/sphere.rs index 96f7729e..c51100c7 100644 --- a/clovers/src/objects/sphere.rs +++ b/clovers/src/objects/sphere.rs @@ -171,6 +171,10 @@ impl<'scene> HitableTrait for Sphere<'scene> { let vec = Unit::new_normalize(vec); *uvw.local(vec) } + + fn centroid(&self) -> Position { + self.center + } } /// Internal helper. diff --git a/clovers/src/objects/stl.rs b/clovers/src/objects/stl.rs index c2ffe62f..dbf1650c 100644 --- a/clovers/src/objects/stl.rs +++ b/clovers/src/objects/stl.rs @@ -66,6 +66,11 @@ impl<'scene> HitableTrait for STL<'scene> { fn random(&self, origin: Position, rng: &mut SmallRng) -> Displacement { self.bvhnode.random(origin, rng) } + + // TODO: correctness + fn centroid(&self) -> Position { + self.aabb.centroid() + } } /// STL structure. This gets converted into an internal representation using [Triangles](crate::objects::Triangle) diff --git a/clovers/src/objects/translate.rs b/clovers/src/objects/translate.rs index 85924cda..c94e6af8 100644 --- a/clovers/src/objects/translate.rs +++ b/clovers/src/objects/translate.rs @@ -95,4 +95,9 @@ impl<'scene> HitableTrait for Translate<'scene> { self.object .pdf_value(origin + self.offset, direction, wavelength, time, rng) } + + // TODO: correctness + fn centroid(&self) -> Position { + self.object.centroid() + self.offset + } } diff --git a/clovers/src/objects/triangle.rs b/clovers/src/objects/triangle.rs index dfa1b6a0..f62dda01 100644 --- a/clovers/src/objects/triangle.rs +++ b/clovers/src/objects/triangle.rs @@ -220,6 +220,11 @@ impl<'scene> HitableTrait for Triangle<'scene> { point - origin } + + // TODO: correctness + fn centroid(&self) -> Position { + self.q + (self.u / 4.0) + (self.v / 4.0) + } } #[must_use]