diff --git a/ray-tracer/src/types/intersection.rs b/ray-tracer/src/types/intersection.rs deleted file mode 100644 index e69de29..0000000 diff --git a/ray-tracer/src/types/intersections.rs b/ray-tracer/src/types/intersections.rs new file mode 100644 index 0000000..62480ba --- /dev/null +++ b/ray-tracer/src/types/intersections.rs @@ -0,0 +1,35 @@ +use std::cmp::Ordering; +use super::Sphere; + +#[derive(Clone, Debug, PartialEq)] +pub struct Intersection<'a> { + pub t: f64, + pub object: &'a Sphere, +} + +pub struct Intersections<'a>(Vec>); + +impl<'a> Intersections<'a> { + pub fn len(&'a self) -> usize { + self.0.len() + } + + pub fn hit(&'a self) -> Option<&Intersection<'a>> { + self.0.iter().find(|i| i.t >= 0.) + } +} + +impl<'a> std::ops::Index for Intersections<'a> { + type Output = Intersection<'a>; + fn index(&self, idx: usize) -> &Intersection<'a> { + &self.0[idx] + } +} + +impl<'a> From>> for Intersections<'a> { + fn from(mut v: Vec>) -> Self { + v.sort_by(|l, r| l.t.partial_cmp(&r.t).unwrap_or(Ordering::Equal)); + Self(v) + } +} + diff --git a/ray-tracer/src/types/mod.rs b/ray-tracer/src/types/mod.rs index 4b05a25..e2a03b0 100644 --- a/ray-tracer/src/types/mod.rs +++ b/ray-tracer/src/types/mod.rs @@ -1,5 +1,6 @@ mod canvas; mod color; +mod intersections; mod matrix; mod point; mod ray; @@ -9,6 +10,7 @@ mod vector; pub use canvas::Canvas; pub use color::Color; +pub use intersections::{Intersections, Intersection}; pub use matrix::Matrix; pub use point::Point; pub use ray::Ray; diff --git a/ray-tracer/src/types/ray.rs b/ray-tracer/src/types/ray.rs index 322f5f0..dde0176 100644 --- a/ray-tracer/src/types/ray.rs +++ b/ray-tracer/src/types/ray.rs @@ -1,37 +1,4 @@ -use crate::types::{Matrix, Point, Sphere, Vector}; -use std::cmp::Ordering; - -#[derive(Clone, Debug, PartialEq)] -pub struct Intersection<'a> { - t: f64, - object: &'a Sphere, -} - -pub struct Intersections<'a>(Vec>); - -impl<'a> Intersections<'a> { - pub fn len(&'a self) -> usize { - self.0.len() - } - - pub fn hit(&'a self) -> Option<&Intersection<'a>> { - self.0.iter().find(|i| i.t >= 0.) - } -} - -impl<'a> std::ops::Index for Intersections<'a> { - type Output = Intersection<'a>; - fn index(&self, idx: usize) -> &Intersection<'a> { - &self.0[idx] - } -} - -impl<'a> From>> for Intersections<'a> { - fn from(mut v: Vec>) -> Self { - v.sort_by(|l, r| l.t.partial_cmp(&r.t).unwrap_or(Ordering::Equal)); - Self(v) - } -} +use super::{Intersection, Intersections, Matrix, Point, Sphere, Vector}; pub struct Ray { origin: Point, @@ -72,7 +39,7 @@ impl Ray { pub fn transform(&self, m: Matrix) -> Self { Self { origin: &m * self.origin, - direction: m * self.direction, + direction: m * self.direction, } } }