From 5e7139f0ba62eecbdf2d62e7be91af20b5acf12a Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Fri, 24 Jun 2022 14:54:27 -0700 Subject: [PATCH] rtchallenge: Address cargo clippy. --- rtchallenge/examples/glass.rs | 2 +- rtchallenge/src/camera.rs | 4 ++-- rtchallenge/src/canvas.rs | 2 +- rtchallenge/src/intersections.rs | 3 +++ rtchallenge/src/shapes.rs | 23 +++++++++++------------ rtchallenge/src/world.rs | 11 +++-------- 6 files changed, 21 insertions(+), 24 deletions(-) diff --git a/rtchallenge/examples/glass.rs b/rtchallenge/examples/glass.rs index 150b01f..03967b7 100644 --- a/rtchallenge/examples/glass.rs +++ b/rtchallenge/examples/glass.rs @@ -8,7 +8,7 @@ use rtchallenge::prelude::*; use rtchallenge::{ camera::RenderStrategy, float::consts::PI, - patterns::{test_pattern, BLACK_PAT, WHITE_PAT}, + patterns::{BLACK_PAT, WHITE_PAT}, WHITE, }; diff --git a/rtchallenge/src/camera.rs b/rtchallenge/src/camera.rs index dcc42ad..c9bd0fe 100644 --- a/rtchallenge/src/camera.rs +++ b/rtchallenge/src/camera.rs @@ -41,7 +41,7 @@ impl Default for RenderStrategy { impl FromStr for RenderStrategy { type Err = serde_json::error::Error; fn from_str(s: &str) -> Result { - Ok(serde_json::from_str(&format!("\"{}\"", s))?) + serde_json::from_str(&format!("\"{}\"", s)) } } @@ -327,7 +327,7 @@ impl Camera { let color = self .supersample_rays_for_pixel(x, y, self.samples_per_pixel) .iter() - .map(|ray| w.color_at(&ray, MAX_DEPTH_RECURSION)) + .map(|ray| w.color_at(ray, MAX_DEPTH_RECURSION)) .fold(BLACK, |acc, c| acc + c); color / self.samples_per_pixel as Float } else { diff --git a/rtchallenge/src/canvas.rs b/rtchallenge/src/canvas.rs index 46f0c99..2ec4f23 100644 --- a/rtchallenge/src/canvas.rs +++ b/rtchallenge/src/canvas.rs @@ -48,7 +48,7 @@ impl Canvas { { let path = Path::new(path.as_ref()); let file = File::create(path)?; - let ref mut w = BufWriter::new(file); + let w = &mut BufWriter::new(file); let mut encoder = png::Encoder::new(w, self.width as u32, self.height as u32); encoder.set_color(png::ColorType::RGB); diff --git a/rtchallenge/src/intersections.rs b/rtchallenge/src/intersections.rs index e238920..542cd6f 100644 --- a/rtchallenge/src/intersections.rs +++ b/rtchallenge/src/intersections.rs @@ -40,6 +40,9 @@ impl<'i> Intersections<'i> { pub fn new(xs: Vec>) -> Intersections { Intersections(xs) } + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } pub fn len(&self) -> usize { self.0.len() } diff --git a/rtchallenge/src/shapes.rs b/rtchallenge/src/shapes.rs index 0f1428d..83e6405 100644 --- a/rtchallenge/src/shapes.rs +++ b/rtchallenge/src/shapes.rs @@ -5,7 +5,7 @@ use derive_builder::Builder; use crate::{ intersections::Intersections, materials::{Material, MaterialBuilder}, - matrices::{identity, Matrix4x4}, + matrices::Matrix4x4, rays::Ray, tuples::Tuple, }; @@ -131,7 +131,7 @@ impl ShapeBuilder { let mut s = Shape::default(); if let Some(transform) = &self.transform { - s.set_transform(transform.clone()); + s.set_transform(*transform); } if let Some(material) = &self.material { s.material = material.clone(); @@ -139,10 +139,10 @@ impl ShapeBuilder { if let Some(geometry) = &self.geometry { s.geometry = geometry.clone(); }; - let transform = s.transform().clone(); + let transform = s.transform(); if let Geometry::Group(ref mut children) = s.geometry { children - .into_iter() + .iter_mut() .for_each(|c| c.set_transform(transform * c.transform())); } Ok(s) @@ -242,7 +242,7 @@ pub fn intersect<'s>(shape: &'s Shape, ray: &Ray) -> Intersections<'s> { Geometry::Plane => plane::intersect(shape, &local_ray), Geometry::TestShape(_) => test_shape::intersect(shape, &local_ray), Geometry::Cube => cube::intersect(shape, &local_ray), - Geometry::Group(_) => group::intersect(shape, &ray), + Geometry::Group(_) => group::intersect(shape, ray), } } @@ -279,8 +279,8 @@ mod sphere { return Intersections::default(); } Intersections::new(vec![ - Intersection::new((-b - discriminant.sqrt()) / (2. * a), &shape), - Intersection::new((-b + discriminant.sqrt()) / (2. * a), &shape), + Intersection::new((-b - discriminant.sqrt()) / (2. * a), shape), + Intersection::new((-b + discriminant.sqrt()) / (2. * a), shape), ]) } } @@ -298,7 +298,7 @@ mod plane { } Intersections::new(vec![Intersection::new( -ray.origin.y / ray.direction.y, - &shape, + shape, )]) } } @@ -343,8 +343,8 @@ mod cube { return Intersections::default(); } Intersections::new(vec![ - Intersection::new(tmin, &shape), - Intersection::new(tmax, &shape), + Intersection::new(tmin, shape), + Intersection::new(tmax, shape), ]) } pub fn local_normal_at(point: Tuple) -> Tuple { @@ -435,8 +435,7 @@ mod group { if let Geometry::Group(children) = &shape.geometry { let mut intersections: Vec<_> = children .iter() - .map(|c| super::intersect(c, ray)) - .flatten() + .flat_map(|c| super::intersect(c, ray)) .collect(); intersections.sort_by(|a, b| { a.t.partial_cmp(&b.t) diff --git a/rtchallenge/src/world.rs b/rtchallenge/src/world.rs index 7369df2..f489c1b 100644 --- a/rtchallenge/src/world.rs +++ b/rtchallenge/src/world.rs @@ -40,12 +40,7 @@ impl World { /// Intersects the ray with this world. pub fn intersect(&self, r: &Ray) -> Intersections { - let mut xs: Vec<_> = self - .objects - .iter() - .map(|o| intersect(&o, &r)) - .flatten() - .collect(); + let mut xs: Vec<_> = self.objects.iter().flat_map(|o| intersect(o, r)).collect(); xs.sort_by(|i1, i2| { i1.t.partial_cmp(&i2.t) .expect("an intersection has a t value that is NaN") @@ -62,7 +57,7 @@ impl World { let shadowed = self.is_shadowed(comps.over_point, light); let surface = lighting( &comps.object.material, - &comps.object, + comps.object, light, comps.over_point, comps.eyev, @@ -86,7 +81,7 @@ impl World { let xs = self.intersect(r); match xs.hit() { Some(hit) => { - let comps = prepare_computations(&hit, r, &xs); + let comps = prepare_computations(hit, r, &xs); self.shade_hit(&comps, remaining) } None => BLACK,