Skip to content

Commit

Permalink
feat: add HitableTrait::centroid method
Browse files Browse the repository at this point in the history
  • Loading branch information
Walther committed Jul 2, 2024
1 parent 8250498 commit 5ec5060
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 1 deletion.
5 changes: 5 additions & 0 deletions clovers/src/bvh/hitable_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,9 @@ impl<'scene> HitableTrait for BVHNode<'scene> {
}
}
}

// TODO: remove?
fn centroid(&self) -> Position {
self.bounding_box.centroid()
}
}
10 changes: 9 additions & 1 deletion clovers/src/hitable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions clovers/src/objects/boxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,9 @@ impl<'scene> HitableTrait for Boxy<'scene> {

sum
}

// TODO: correctness for rotations/translations?
fn centroid(&self) -> Position {
self.aabb.centroid()
}
}
4 changes: 4 additions & 0 deletions clovers/src/objects/constant_medium.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
10 changes: 10 additions & 0 deletions clovers/src/objects/gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
Expand Down Expand Up @@ -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]
Expand Down
5 changes: 5 additions & 0 deletions clovers/src/objects/moving_sphere.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
5 changes: 5 additions & 0 deletions clovers/src/objects/quad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ impl<'scene> HitableTrait for Quad<'scene> {
+ (rng.gen::<Float>() * self.v);
point - origin
}

// TODO: correctness
fn centroid(&self) -> Position {
self.q + (self.u / 2.0) + (self.v / 2.0)
}
}

#[must_use]
Expand Down
5 changes: 5 additions & 0 deletions clovers/src/objects/rotate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,9 @@ impl<'scene> HitableTrait for RotateY<'scene> {
// TODO: fix
0.0
}

// TODO: correctness!
fn centroid(&self) -> Position {
self.object.centroid()
}
}
4 changes: 4 additions & 0 deletions clovers/src/objects/sphere.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions clovers/src/objects/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions clovers/src/objects/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
5 changes: 5 additions & 0 deletions clovers/src/objects/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 5ec5060

Please sign in to comment.