From f15fa9dd4895f1fb44dc19de21bd0643c29488da Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sun, 23 Jun 2024 15:53:41 -0400 Subject: [PATCH] Fix warnings --- ray-tracer/src/types/intersections.rs | 4 ++++ ray-tracer/src/types/ray.rs | 26 +++++++++++------------ ray-tracer/src/types/sphere.rs | 22 +++++++++++--------- ray-tracer/src/types/tuple.rs | 30 ++------------------------- 4 files changed, 31 insertions(+), 51 deletions(-) diff --git a/ray-tracer/src/types/intersections.rs b/ray-tracer/src/types/intersections.rs index 62480ba..591591a 100644 --- a/ray-tracer/src/types/intersections.rs +++ b/ray-tracer/src/types/intersections.rs @@ -10,6 +10,10 @@ pub struct Intersection<'a> { pub struct Intersections<'a>(Vec>); impl<'a> Intersections<'a> { + pub fn is_empty(&'a self) -> bool { + self.0.is_empty() + } + pub fn len(&'a self) -> usize { self.0.len() } diff --git a/ray-tracer/src/types/ray.rs b/ray-tracer/src/types/ray.rs index dde0176..b4d65e9 100644 --- a/ray-tracer/src/types/ray.rs +++ b/ray-tracer/src/types/ray.rs @@ -30,8 +30,8 @@ impl Ray { let t2 = (-b + discriminant.sqrt()) / (2. * a); vec![ - Intersection { t: t1, object: &s }, - Intersection { t: t2, object: &s }, + Intersection { t: t1, object: s }, + Intersection { t: t2, object: s }, ] .into() } @@ -61,7 +61,7 @@ mod tests { #[test] fn ray_intersects_sphere_at_two_points() { let r = Ray::new(Point::new(0., 0., -5.), Vector::new(0., 0., 1.)); - let s = Sphere::new(); + let s = Sphere::default(); let xs = r.intersect(&s); assert_eq!(xs.len(), 2); assert_eq!(xs[0].t, 4.); @@ -73,7 +73,7 @@ mod tests { #[test] fn ray_tangents_sphere_at_one_point() { let r = Ray::new(Point::new(0., 1., -5.), Vector::new(0., 0., 1.)); - let s = Sphere::new(); + let s = Sphere::default(); let xs = r.intersect(&s); assert_eq!(xs.len(), 2); assert_eq!(xs[0].t, 5.); @@ -83,7 +83,7 @@ mod tests { #[test] fn ray_misses_the_sphere() { let r = Ray::new(Point::new(0., 2., -5.), Vector::new(0., 0., 1.)); - let s = Sphere::new(); + let s = Sphere::default(); let xs = r.intersect(&s); assert_eq!(xs.len(), 0); } @@ -91,7 +91,7 @@ mod tests { #[test] fn ray_originates_inside_the_sphere() { let r = Ray::new(Point::new(0., 0., 0.), Vector::new(0., 0., 1.)); - let s = Sphere::new(); + let s = Sphere::default(); let xs = r.intersect(&s); assert_eq!(xs.len(), 2); assert_eq!(xs[0].t, -1.); @@ -101,7 +101,7 @@ mod tests { #[test] fn sphere_is_behind_the_ray() { let r = Ray::new(Point::new(0., 0., 5.), Vector::new(0., 0., 1.)); - let s = Sphere::new(); + let s = Sphere::default(); let xs = r.intersect(&s); assert_eq!(xs.len(), 2); assert_eq!(xs[0].t, -6.); @@ -110,7 +110,7 @@ mod tests { #[test] fn hit_all_intersections_are_positive() { - let s = Sphere::new(); + let s = Sphere::default(); let i1 = Intersection { t: 1., object: &s }; let i2 = Intersection { t: 2., object: &s }; let xs = Intersections::from(vec![i1.clone(), i2.clone()]); @@ -120,7 +120,7 @@ mod tests { #[test] fn hit_some_intersections_are_negative() { - let s = Sphere::new(); + let s = Sphere::default(); let i1 = Intersection { t: -1., object: &s }; let i2 = Intersection { t: 1., object: &s }; let xs = Intersections::from(vec![i1, i2.clone()]); @@ -130,7 +130,7 @@ mod tests { #[test] fn hit_all_intersections_are_negative() { - let s = Sphere::new(); + let s = Sphere::default(); let i1 = Intersection { t: -2., object: &s }; let i2 = Intersection { t: -1., object: &s }; let xs = Intersections::from(vec![i1, i2]); @@ -140,7 +140,7 @@ mod tests { #[test] fn hit_is_always_lowest_nonnegative() { - let s = Sphere::new(); + let s = Sphere::default(); let i1 = Intersection { t: 5., object: &s }; let i2 = Intersection { t: 7., object: &s }; let i3 = Intersection { t: -3., object: &s }; @@ -171,7 +171,7 @@ mod tests { #[test] fn intersect_scaled_sphere_with_ray() { let r = Ray::new(Point::new(0., 0., -5.), Vector::new(0., 0., 1.)); - let mut s = Sphere::new(); + let mut s = Sphere::default(); s.set_transformation(scaling(2., 2., 2.)); let xs = r.intersect(&s); assert_eq!(xs.len(), 2); @@ -182,7 +182,7 @@ mod tests { #[test] fn intersect_translated_sphere_with_ray() { let r = Ray::new(Point::new(0., 0., -5.), Vector::new(0., 0., 1.)); - let mut s = Sphere::new(); + let mut s = Sphere::default(); s.set_transformation(translation(5., 0., 0.)); let xs = r.intersect(&s); assert_eq!(xs.len(), 0); diff --git a/ray-tracer/src/types/sphere.rs b/ray-tracer/src/types/sphere.rs index 153046d..07b1afe 100644 --- a/ray-tracer/src/types/sphere.rs +++ b/ray-tracer/src/types/sphere.rs @@ -7,13 +7,6 @@ pub struct Sphere { } impl Sphere { - pub fn new() -> Self { - Self { - origin: Point::new(0., 0., 0.), - transformation: Matrix::identity(), - } - } - pub fn transformation(&self) -> &Matrix { &self.transformation } @@ -23,20 +16,29 @@ impl Sphere { } } +impl Default for Sphere { + fn default() -> Self { + Self { + origin: Point::new(0., 0., 0.), + transformation: Matrix::identity(), + } + } +} + #[cfg(test)] mod test { - use crate::transforms::translation; use super::*; + use crate::transforms::translation; #[test] fn sphere_has_default_transformation() { - let s = Sphere::new(); + let s = Sphere::default(); assert_eq!(s.transformation(), &Matrix::identity()); } #[test] fn change_a_spheres_transformation() { - let mut s = Sphere::new(); + let mut s = Sphere::default(); let t = translation(2., 3., 4.); s.set_transformation(t.clone()); assert_eq!(*s.transformation(), t); diff --git a/ray-tracer/src/types/tuple.rs b/ray-tracer/src/types/tuple.rs index 0a45320..1082d14 100644 --- a/ray-tracer/src/types/tuple.rs +++ b/ray-tracer/src/types/tuple.rs @@ -34,20 +34,6 @@ impl std::ops::Add<&Tuple> for &Tuple { } } -impl std::ops::Add for &Tuple { - type Output = Tuple; - fn add(self, r: Tuple) -> Self::Output { - self + &r - } -} - -impl std::ops::Add<&Tuple> for Tuple { - type Output = Tuple; - fn add(self, r: &Tuple) -> Self::Output { - &self + r - } -} - impl std::ops::Add for Tuple { type Output = Tuple; fn add(self, r: Tuple) -> Self::Output { @@ -62,20 +48,6 @@ impl std::ops::Sub<&Tuple> for &Tuple { } } -impl std::ops::Sub for &Tuple { - type Output = Tuple; - fn sub(self, r: Tuple) -> Self::Output { - self - &r - } -} - -impl std::ops::Sub<&Tuple> for Tuple { - type Output = Tuple; - fn sub(self, r: &Tuple) -> Self::Output { - &self - r - } -} - impl std::ops::Sub for Tuple { type Output = Tuple; fn sub(self, r: Tuple) -> Self::Output { @@ -111,6 +83,7 @@ impl std::ops::Mul for &Tuple { impl std::ops::Mul for Tuple { type Output = Tuple; + #[allow(clippy::op_ref)] fn mul(self, scalar: f64) -> Self::Output { &self * scalar } @@ -130,6 +103,7 @@ impl std::ops::Div for &Tuple { impl std::ops::Div for Tuple { type Output = Tuple; + #[allow(clippy::op_ref)] fn div(self, scalar: f64) -> Self::Output { &self / scalar }