aboutsummaryrefslogtreecommitdiff
path: root/src/world/shapes/sphere.rs
blob: 5f32c7f091e5d2423633e8e0f761686df9643467 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//! 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, DynHittable, Intersection, Instancable};

pub struct Sphere {
    radius: Float,
}

impl Sphere {
    pub fn new(radius: Float) -> Sphere {
        Sphere {
            radius,
        }
    }

    fn norm_at(&self, point: &Vector3f) -> Vector3f {
        *point / self.radius
    }
}

impl Hittable for Sphere {
    // Implementation from ray tracing in a weekend
    fn intersect(&self, ray: &Ray) -> Option<Intersection> {
        let a = ray.direction.len_squared();
        let half_b = ray.origin.dot(&ray.direction);
        let c = ray.origin.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);
    /// 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(-offset, offset)
    }
}

impl Instancable for Sphere {}

impl Into<DynHittable> for Sphere {
    fn into(self) -> DynHittable {
        DynHittable::new(Box::new(self))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sphere_intersect() {
        let sph = Sphere::new(2.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 - 1.732).abs() < 0.01);
    }
}