aboutsummaryrefslogtreecommitdiff
path: root/src/world/shapes
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2021-08-05 15:44:40 +0200
committerJulian T <julian@jtle.dk>2021-08-05 15:44:40 +0200
commit3ef8f4d918406eec6bdc29e0ebd883fabfac9b2e (patch)
treeaa4b1aac1e165821c16f222ebfb9212a9740e98b /src/world/shapes
parent45119506c0293fdde6cef35f6e6f82d4055b46b6 (diff)
Add picture for c5505ab84820248c6dba35fc06aef9e0ced183derendered
Diffstat (limited to 'src/world/shapes')
-rw-r--r--src/world/shapes/mod.rs30
-rw-r--r--src/world/shapes/sphere.rs98
2 files changed, 0 insertions, 128 deletions
diff --git a/src/world/shapes/mod.rs b/src/world/shapes/mod.rs
deleted file mode 100644
index a11df5d..0000000
--- a/src/world/shapes/mod.rs
+++ /dev/null
@@ -1,30 +0,0 @@
-mod sphere;
-
-pub use sphere::Sphere;
-
-use crate::world::{Hittable, Intersection};
-use crate::core::{Bound3f, Ray};
-
-pub enum Shape {
- Sphere(Sphere),
-}
-
-impl Hittable for Shape {
- fn intersect(&self, ray: &Ray) -> Option<Intersection> {
- match self {
- Self::Sphere(sph) => sph.intersect(ray)
- }
- }
-
- fn bounding_box(&self) -> Bound3f {
- match self {
- Self::Sphere(sph) => sph.bounding_box()
- }
- }
-}
-
-impl From<Sphere> for Shape {
- fn from(s: Sphere) -> Self {
- Self::Sphere(s)
- }
-}
diff --git a/src/world/shapes/sphere.rs b/src/world/shapes/sphere.rs
deleted file mode 100644
index 1df9c35..0000000
--- a/src/world/shapes/sphere.rs
+++ /dev/null
@@ -1,98 +0,0 @@
-//! Implements sphere
-//!
-//! Spheres are relatively easy to calculate intersections between
-use crate::{Float, NEAR_ZERO};
-use crate::core::{Ray, Vector3f, Bound3f};
-use crate::world::{Hittable, Intersection};
-
-pub struct Sphere {
- radius: Float,
- center: Vector3f,
-}
-
-impl Sphere {
- pub fn new(radius: Float, center: Vector3f) -> Sphere {
- Sphere {
- radius,
- center,
- }
- }
-
- fn norm_at(&self, point: &Vector3f) -> Vector3f {
- let mut v = *point - self.center;
- v /= self.radius;
- v
- }
-}
-
-impl Hittable for Sphere {
- // Implementation from ray tracing in a weekend
- fn intersect(&self, ray: &Ray) -> Option<Intersection> {
- let oc = ray.origin - self.center;
- let a = ray.direction.len_squared();
- let half_b = oc.dot(&ray.direction);
- let c = oc.len_squared() - self.radius * self.radius;
- let disc = half_b*half_b - a*c;
-
- if disc < 0.0 {
- None
- } else {
- let disc_sqrt = disc.sqrt();
-
- let mut distance = -half_b - disc_sqrt;
- if distance <= NEAR_ZERO {
- distance = -half_b + disc_sqrt;
- }
- if distance <= NEAR_ZERO {
- return None;
- }
-
- distance /= a;
- let w = ray.at(distance);
- Some(Intersection::new(
- self.norm_at(&w),
- w,
- ray,
- distance,
- ))
- }
-
- }
-
- /// Box containing the circle
- ///
- /// # Examples
- ///
- /// ```
- /// use rendering::core::Vector3f;
- /// use rendering::world::{Hittable, shapes::Sphere};
- ///
- /// let sph = Sphere::new(1.0, Vector3f::new(0.0));
- /// let b = sph.bounding_box();
- ///
- /// 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) -> Bound3f {
- let offset = Vector3f::new(self.radius);
-
- Bound3f::new(self.center - offset, self.center + offset)
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn sphere_intersect() {
- let sph = Sphere::new(2.0, Vector3f::new_xyz(2.0, 3.0, 4.0));
-
- let ray = Ray {
- origin: Vector3f::new_xyz(1.0, 0.0, 0.0),
- direction: Vector3f::new_xyz(0.0, 1.0, 1.5).norm(),
- };
-
- let dist = sph.intersect(&ray).unwrap();
- assert!((dist.t - 3.28).abs() < 0.01);
- }
-}