From 0ce1e8f7af54cc4dabe187384463723da3d05c1a Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Fri, 16 Jul 2021 21:06:57 -0700 Subject: [PATCH] intersections: create inters module and initial functionality. --- rtchallenge/src/intersections.rs | 64 ++++++++++++++++++++++++++++++++ rtchallenge/src/lib.rs | 2 + rtchallenge/src/spheres.rs | 5 +-- 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 rtchallenge/src/intersections.rs diff --git a/rtchallenge/src/intersections.rs b/rtchallenge/src/intersections.rs new file mode 100644 index 0000000..9e8bba6 --- /dev/null +++ b/rtchallenge/src/intersections.rs @@ -0,0 +1,64 @@ +use std::ops::Index; + +use crate::spheres::Sphere; + +pub struct Intersection<'i> { + pub t: f32, + pub object: &'i Sphere, +} + +impl<'i> Intersection<'i> { + /// Create new `Intersection` at the given `t` that hits the given `object`. + /// + /// # Examples + /// ``` + /// use rtchallenge::{intersections::Intersection, spheres::Sphere}; + /// + /// // An intersection ecapsulates t and object. + /// let s = Sphere::default(); + /// let i = Intersection::new(3.5, &s); + /// assert_eq!(i.t, 3.5); + /// assert_eq!(i.object, &s); + /// ``` + pub fn new(t: f32, object: &Sphere) -> Intersection { + Intersection { t, object } + } +} + +/// Aggregates `Intersection`s. +/// +/// # Examples +/// ``` +/// use rtchallenge::{ +/// intersections::{Intersection, Intersections}, +/// spheres::Sphere, +/// }; +/// +/// let s = Sphere::default(); +/// let i1 = Intersection::new(1., &s); +/// let i2 = Intersection::new(2., &s); +/// let xs = Intersections::new(vec![i1, i2]); +/// assert_eq!(xs.len(), 2); +/// assert_eq!(xs[0].t, 1.); +/// assert_eq!(xs[1].t, 2.); +/// ``` +pub struct Intersections<'i>(Vec>); + +impl<'i> Intersections<'i> { + pub fn new(xs: Vec>) -> Intersections { + Intersections(xs) + } + pub fn len(&self) -> usize { + self.0.len() + } + pub fn hit(&self) -> Option<&Intersection> { + todo!("Intersections::hit") + } +} + +impl<'i> Index for Intersections<'i> { + type Output = Intersection<'i>; + fn index(&self, idx: usize) -> &Self::Output { + &self.0[idx] + } +} diff --git a/rtchallenge/src/lib.rs b/rtchallenge/src/lib.rs index b443f53..5f57a92 100644 --- a/rtchallenge/src/lib.rs +++ b/rtchallenge/src/lib.rs @@ -1,6 +1,8 @@ pub mod canvas; +pub mod intersections; pub mod matrices; pub mod rays; +pub mod spheres; pub mod tuples; /// Value considered close enough for PartialEq implementations. diff --git a/rtchallenge/src/spheres.rs b/rtchallenge/src/spheres.rs index 6ed4b58..7568ebd 100644 --- a/rtchallenge/src/spheres.rs +++ b/rtchallenge/src/spheres.rs @@ -3,9 +3,9 @@ use crate::{ tuples::{dot, Tuple}, }; -#[derive(Default)] +#[derive(Default, Debug, PartialEq)] +/// Sphere represents the unit-sphere (radius of unit 1.) at the origin 0., 0., 0. pub struct Sphere {} -pub struct Intersection {} /// Intersect a ray with a sphere. /// @@ -41,7 +41,6 @@ pub struct Intersection {} /// let xs = intersect(&s, &r); /// assert_eq!(xs, vec![-6., -4.]); /// ``` - pub fn intersect(_sphere: &Sphere, ray: &Ray) -> Vec { let sphere_to_ray = ray.origin - Tuple::point(0., 0., 0.); let a = dot(ray.direction, ray.direction);