From da1c3949a449f3fafe579c62ff6b14ffd993a197 Mon Sep 17 00:00:00 2001 From: Julian T Date: Sun, 21 Feb 2021 18:01:56 +0100 Subject: Add 3d bounding box and merged SceneIntersection and Intersection --- src/world/shapes/sphere.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/world/shapes') diff --git a/src/world/shapes/sphere.rs b/src/world/shapes/sphere.rs index 8e0816b..55fc8b4 100644 --- a/src/world/shapes/sphere.rs +++ b/src/world/shapes/sphere.rs @@ -2,7 +2,8 @@ //! //! Spheres are relatively easy to calculate intersections between use crate::Float; -use crate::core::{Ray, Vector3f, Hittable, Intersection}; +use crate::core::{Ray, Vector3f, Bound3f}; +use crate::world::{Hittable, Intersection}; pub struct Sphere { radius: Float, @@ -45,10 +46,31 @@ impl Hittable for Sphere { n: self.norm_at(&w), p: w, t: distance, + m: None, }) } } + + /// Box containing the circle + /// + /// # Examples + /// + /// ``` + /// use rendering::core::{Vector3f, Hittable}; + /// use rendering::world::shapes::Sphere; + /// + /// let sph = Sphere::new(1.0, Vector3f::new(0.0)); + /// let b = sph.bounding_box().unwrap(); + /// + /// assert!(b.min.x == -1.0 && b.min.y == -1.0 && b.min.z == -1.0); + /// assert!(b.max.x == 1.0 && b.max.y == 1.0 && b.max.z == 1.0); + fn bounding_box(&self) -> Option { + let offset = Vector3f::new(self.radius); + Some( + Bound3f::new(self.center - offset, self.center + offset) + ) + } } #[cfg(test)] -- cgit v1.2.3