From fb744258b2df3122f6ab8021004b8f158b5337d3 Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Fri, 8 Oct 2021 09:23:16 -0400 Subject: [PATCH] Numerous small changes suggested by clippy --- src/accumulation_buffer.rs | 2 +- src/camera.rs | 2 +- src/colour/photon.rs | 2 +- src/image.rs | 5 ++--- src/integrators/simple_random_integrator.rs | 4 ++-- src/integrators/whitted_integrator.rs | 10 +++++----- src/math/vec3.rs | 10 ++++------ src/mesh.rs | 6 +++--- src/random_distributions/cosine_weighted_hemisphere.rs | 1 + src/random_distributions/sky_light_pdf.rs | 6 ++++++ src/random_distributions/uniform_hemisphere.rs | 1 + src/random_distributions/unit_disc.rs | 6 ++++++ src/raycasting/bounding_volume_hierarchy.rs | 10 +++++----- src/raycasting/mod.rs | 2 +- src/raycasting/plane.rs | 2 +- src/raycasting/sphere.rs | 2 +- src/raycasting/triangle.rs | 4 ++-- src/raycasting/vec_aggregate.rs | 8 ++++---- src/sampler.rs | 2 +- src/util/binary_tree.rs | 6 ++---- src/util/polyhedra.rs | 4 ++-- 21 files changed, 52 insertions(+), 43 deletions(-) diff --git a/src/accumulation_buffer.rs b/src/accumulation_buffer.rs index 2ae697c..2d1db3c 100644 --- a/src/accumulation_buffer.rs +++ b/src/accumulation_buffer.rs @@ -47,7 +47,7 @@ impl AccumulationBuffer { let buffer_colour_bias = &mut self.colour_bias_buffer[row][column]; let buffer_weight = &mut self.weight_buffer[row][column]; let buffer_weight_bias = &mut self.weight_bias_buffer[row][column]; - let photon_colour = ColourXyz::from_photon(&photon); + let photon_colour = ColourXyz::from_photon(photon); let weight_sum_y = weight - *buffer_weight_bias; let weight_sum_t = *buffer_weight + weight_sum_y; *buffer_weight_bias = (weight_sum_t - *buffer_weight) - weight_sum_y; diff --git a/src/camera.rs b/src/camera.rs index 5b0dcee..5665cdb 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -101,7 +101,7 @@ pub fn partial_render_scene( let mut output_image_tile = AccumulationBuffer::new(tile.width(), tile.height()); let image_sampler = ImageSampler::new(width, height, scene.camera_location); let integrator = SimpleRandomIntegrator {}; - let sampler = Sampler { scene: &scene }; + let sampler = Sampler { scene }; for column in 0..tile.width() { for row in 0..tile.height() { let ray = image_sampler.ray_for_pixel(tile.start_row + row, tile.start_column + column); diff --git a/src/colour/photon.rs b/src/colour/photon.rs index 7077010..5026181 100644 --- a/src/colour/photon.rs +++ b/src/colour/photon.rs @@ -37,7 +37,7 @@ impl Photon { pub fn set_intensity(&self, intensity: f64) -> Photon { Photon { wavelength: self.wavelength, - intensity: intensity, + intensity, } } } diff --git a/src/image.rs b/src/image.rs index a4c62ed..65958d9 100644 --- a/src/image.rs +++ b/src/image.rs @@ -1,4 +1,3 @@ -use std::convert::TryInto; use std::fs::File; use std::io::BufWriter; use std::path::Path; @@ -20,7 +19,7 @@ impl ImageRgbU8 { pub fn get_colour(&self, row: usize, column: usize) -> ColourRgbU8 { ColourRgbU8 { - values: self.data[row][column].try_into().expect("Wrong length."), + values: self.data[row][column], } } @@ -52,7 +51,7 @@ impl ImageRgbU8 { pub fn write_png(&self, filename: &Path) -> Result<(), std::io::Error> { let file = File::create(filename)?; - let ref mut file_buffer = BufWriter::new(file); + let file_buffer = &mut BufWriter::new(file); let mut encoder = png::Encoder::new( file_buffer, diff --git a/src/integrators/simple_random_integrator.rs b/src/integrators/simple_random_integrator.rs index b24863c..4e65fba 100644 --- a/src/integrators/simple_random_integrator.rs +++ b/src/integrators/simple_random_integrator.rs @@ -34,7 +34,7 @@ impl Integrator for SimpleRandomIntegrator { let MaterialSampleResult { direction: w_o, pdf: w_o_pdf, - } = info.material.sample(&w_i, &photon); + } = info.material.sample(&w_i, photon); let world_space_w_o = bsdf_to_world_space * w_o; info.material.bsdf()( &w_o, @@ -45,7 +45,7 @@ impl Integrator for SimpleRandomIntegrator { photon.wavelength, )), Some(recursive_hit) => { - self.integrate(&sampler, &recursive_hit, &photon, recursion_limit - 1) + self.integrate(sampler, &recursive_hit, photon, recursion_limit - 1) } } .scale_intensity(w_o_pdf) diff --git a/src/integrators/whitted_integrator.rs b/src/integrators/whitted_integrator.rs index 3875c26..144f5ea 100644 --- a/src/integrators/whitted_integrator.rs +++ b/src/integrators/whitted_integrator.rs @@ -35,13 +35,13 @@ impl Integrator for WhittedIntegrator { .iter() .map(|light| { match sampler.sample(&Ray::new(info.location, light.direction).bias(0.000_000_1)) { - Some(_) => self.ambient_light.emit_photon(&photon), + Some(_) => self.ambient_light.emit_photon(photon), None => info.material.bsdf()( &(world_to_bsdf_space * info.retro), &(world_to_bsdf_space * light.direction), &light .spectrum - .emit_photon(&photon) + .emit_photon(photon) .scale_intensity(light.direction.dot(&info.normal).abs()), ), } @@ -49,7 +49,7 @@ impl Integrator for WhittedIntegrator { .chain( [info .material - .sample(&(world_to_bsdf_space * info.retro), &photon)] + .sample(&(world_to_bsdf_space * info.retro), photon)] .iter() .map(|MaterialSampleResult { direction, pdf: _ }| { let world_space_direction = bsdf_to_world_space * direction; @@ -62,9 +62,9 @@ impl Integrator for WhittedIntegrator { &(world_to_bsdf_space * info.retro), direction, &self.integrate( - &sampler, + sampler, &recursive_hit, - &photon, + photon, recursion_limit - 1, ), ); diff --git a/src/math/vec3.rs b/src/math/vec3.rs index 60cdcad..3914f49 100644 --- a/src/math/vec3.rs +++ b/src/math/vec3.rs @@ -93,7 +93,7 @@ impl Vec3 { } pub fn norm_squared(&self) -> f64 { - self.dot(&self) + self.dot(self) } pub fn norm(&self) -> f64 { @@ -119,12 +119,10 @@ impl Vec3 { } else { 2 } + } else if y < z { + 1 } else { - if y < z { - 1 - } else { - 2 - } + 2 } } diff --git a/src/mesh.rs b/src/mesh.rs index b065758..504d497 100644 --- a/src/mesh.rs +++ b/src/mesh.rs @@ -47,16 +47,16 @@ mod wavefront_obj { ) -> Vec { if let Some(v0_index) = polygon.iter().next() { let (v0_vertex, v0_normal) = - get_vertex_and_normal(v0_index, &vertex_positions, &normal_positions); + get_vertex_and_normal(v0_index, &vertex_positions, normal_positions); polygon .iter() .skip(1) .zip(polygon.iter().skip(2)) .map(|(v1_index, v2_index)| { let (v1_vertex, v1_normal) = - get_vertex_and_normal(v1_index, &vertex_positions, &normal_positions); + get_vertex_and_normal(v1_index, vertex_positions, normal_positions); let (v2_vertex, v2_normal) = - get_vertex_and_normal(v2_index, &vertex_positions, &normal_positions); + get_vertex_and_normal(v2_index, vertex_positions, normal_positions); let vertices = [v0_vertex, v1_vertex, v2_vertex]; let normals = [v0_normal, v1_normal, v2_normal]; Triangle { diff --git a/src/random_distributions/cosine_weighted_hemisphere.rs b/src/random_distributions/cosine_weighted_hemisphere.rs index 5e51ee7..b278831 100644 --- a/src/random_distributions/cosine_weighted_hemisphere.rs +++ b/src/random_distributions/cosine_weighted_hemisphere.rs @@ -4,6 +4,7 @@ use crate::math::Vec3; use super::{RandomDistribution, UnitDisc}; +#[derive(Default)] pub struct CosineWeightedHemisphere { unit_disc: UnitDisc, } diff --git a/src/random_distributions/sky_light_pdf.rs b/src/random_distributions/sky_light_pdf.rs index c1ac891..93b1f59 100644 --- a/src/random_distributions/sky_light_pdf.rs +++ b/src/random_distributions/sky_light_pdf.rs @@ -18,6 +18,12 @@ impl SkyLightPdf { } } +impl Default for SkyLightPdf { + fn default() -> SkyLightPdf { + SkyLightPdf::new() + } +} + impl RandomDistribution for SkyLightPdf { fn value(&self) -> Vec3 { let mut rng = thread_rng(); diff --git a/src/random_distributions/uniform_hemisphere.rs b/src/random_distributions/uniform_hemisphere.rs index 8fc7572..5cf6f63 100644 --- a/src/random_distributions/uniform_hemisphere.rs +++ b/src/random_distributions/uniform_hemisphere.rs @@ -7,6 +7,7 @@ use crate::math::Vec3; use super::RandomDistribution; +#[derive(Default)] pub struct UniformHemisphere {} impl UniformHemisphere { diff --git a/src/random_distributions/unit_disc.rs b/src/random_distributions/unit_disc.rs index 7513b1c..3556687 100644 --- a/src/random_distributions/unit_disc.rs +++ b/src/random_distributions/unit_disc.rs @@ -9,6 +9,12 @@ pub struct UnitDisc { square_distribution: UniformSquare, } +impl Default for UnitDisc { + fn default() -> UnitDisc { + UnitDisc::new() + } +} + impl UnitDisc { pub fn new() -> UnitDisc { let square_distribution = UniformSquare::new(Vec2::new(-1.0, -1.0), 2.0); diff --git a/src/raycasting/bounding_volume_hierarchy.rs b/src/raycasting/bounding_volume_hierarchy.rs index 37c6bb3..35e33b2 100644 --- a/src/raycasting/bounding_volume_hierarchy.rs +++ b/src/raycasting/bounding_volume_hierarchy.rs @@ -92,24 +92,24 @@ fn closest_intersection( } impl Intersect for BoundingVolumeHierarchy { - fn intersect<'a>(&'a self, ray: &Ray) -> Option { + fn intersect(&self, ray: &Ray) -> Option { match self { BoundingVolumeHierarchy::Node { bounds, left, right, } => { - if bounds.intersect(&ray) { - closest_intersection(left.intersect(&ray), right.intersect(&ray)) + if bounds.intersect(ray) { + closest_intersection(left.intersect(ray), right.intersect(ray)) } else { None } } BoundingVolumeHierarchy::Leaf { bounds, primitives } => { - if bounds.intersect(&ray) { + if bounds.intersect(ray) { primitives .iter() - .map(|elem| elem.intersect(&ray)) + .map(|elem| elem.intersect(ray)) .fold(None, closest_intersection) } else { None diff --git a/src/raycasting/mod.rs b/src/raycasting/mod.rs index 52cb8bd..97d6301 100644 --- a/src/raycasting/mod.rs +++ b/src/raycasting/mod.rs @@ -100,7 +100,7 @@ pub struct IntersectionInfo { /// intersected with a [Ray](Ray) pub trait Intersect: Send + Sync { /// Test if the ray intersects the object, and return information about the object and intersection. - fn intersect<'a>(&'a self, ray: &Ray) -> Option; + fn intersect(&self, ray: &Ray) -> Option; } /// A geometric object that can be intersected with a ray diff --git a/src/raycasting/plane.rs b/src/raycasting/plane.rs index b4d8ffa..9a0af24 100644 --- a/src/raycasting/plane.rs +++ b/src/raycasting/plane.rs @@ -46,7 +46,7 @@ impl Plane { }*/ impl Intersect for Plane { - fn intersect<'a>(&'a self, ray: &Ray) -> Option { + fn intersect(&self, ray: &Ray) -> Option { let ray_direction_dot_plane_normal = ray.direction.dot(&self.normal); let point_on_plane = self.normal * self.distance_from_origin; let point_on_plane_minus_ray_origin_dot_normal = diff --git a/src/raycasting/sphere.rs b/src/raycasting/sphere.rs index 76bf2f4..608722a 100644 --- a/src/raycasting/sphere.rs +++ b/src/raycasting/sphere.rs @@ -37,7 +37,7 @@ impl Sphere { }*/ impl Intersect for Sphere { - fn intersect<'a>(&'a self, ray: &Ray) -> Option { + fn intersect<'a>(&'_ self, ray: &Ray) -> Option { let r_o = ray.origin; let centre_coords = self.centre; let a = ray diff --git a/src/raycasting/triangle.rs b/src/raycasting/triangle.rs index 193dcd9..506d574 100644 --- a/src/raycasting/triangle.rs +++ b/src/raycasting/triangle.rs @@ -33,7 +33,7 @@ pub struct Triangle { }*/ impl Intersect for Triangle { - fn intersect<'a>(&'a self, ray: &Ray) -> Option { + fn intersect(&self, ray: &Ray) -> Option { let translation = -ray.origin; let indices = indices_with_index_of_largest_element_last(&ray.direction); let permuted_ray_direction = permute_vector_elements(&ray.direction, &indices); @@ -126,7 +126,7 @@ fn is_valid_permutation(indices: &[usize; 3]) -> bool { } fn permute_vector_elements(v: &Vec3, indices: &[usize; 3]) -> Vec3 { - debug_assert!(is_valid_permutation(&indices)); + debug_assert!(is_valid_permutation(indices)); Vec3::new(v[indices[0]], v[indices[1]], v[indices[2]]) } diff --git a/src/raycasting/vec_aggregate.rs b/src/raycasting/vec_aggregate.rs index 2e578c8..27786f4 100644 --- a/src/raycasting/vec_aggregate.rs +++ b/src/raycasting/vec_aggregate.rs @@ -9,9 +9,9 @@ impl HasBoundingBox for Vec> { } impl Intersect for Vec> { - fn intersect<'a>(&'a self, ray: &Ray) -> Option { + fn intersect(&self, ray: &Ray) -> Option { self.iter() - .flat_map(|primitive| primitive.intersect(&ray)) + .flat_map(|primitive| primitive.intersect(ray)) .min_by( |a, b| match PartialOrd::partial_cmp(&a.distance, &b.distance) { None => std::cmp::Ordering::Less, @@ -32,9 +32,9 @@ impl HasBoundingBox for Vec> { } impl Intersect for Vec> { - fn intersect<'a>(&'a self, ray: &Ray) -> Option { + fn intersect(&self, ray: &Ray) -> Option { self.iter() - .flat_map(|aggregate| aggregate.intersect(&ray)) + .flat_map(|aggregate| aggregate.intersect(ray)) .min_by( |a, b| match PartialOrd::partial_cmp(&a.distance, &b.distance) { None => std::cmp::Ordering::Less, diff --git a/src/sampler.rs b/src/sampler.rs index 30dc8d2..ed92b99 100644 --- a/src/sampler.rs +++ b/src/sampler.rs @@ -10,7 +10,7 @@ impl<'a> Sampler<'a> { self.scene .objects .iter() - .flat_map(|object| object.intersect(&ray)) + .flat_map(|object| object.intersect(ray)) .min_by( |a, b| match PartialOrd::partial_cmp(&a.distance, &b.distance) { None => std::cmp::Ordering::Less, diff --git a/src/util/binary_tree.rs b/src/util/binary_tree.rs index c073980..85e76a5 100644 --- a/src/util/binary_tree.rs +++ b/src/util/binary_tree.rs @@ -1,5 +1,4 @@ -pub enum BinaryTree -{ +pub enum BinaryTree { Branch { value: Value, left: Box, @@ -11,8 +10,7 @@ pub enum BinaryTree None, } -impl BinaryTree -{ +impl BinaryTree { pub fn count_leaves(&self) -> usize { match self { Self::Branch { diff --git a/src/util/polyhedra.rs b/src/util/polyhedra.rs index 2182303..198830c 100644 --- a/src/util/polyhedra.rs +++ b/src/util/polyhedra.rs @@ -7,7 +7,7 @@ use crate::raycasting::{Primitive, Triangle}; use std::sync::Arc; pub fn triangulate_polygon( - vertices: &Vec, + vertices: &[Vec3], normal: &Vec3, material: Arc, ) -> Vec> { @@ -124,7 +124,7 @@ pub fn generate_dodecahedron( .iter() .flat_map(|face| { let normal = (face[1] - face[0]).cross(&(face[2] - face[1])); - let transformed_face = face.iter().map(|v| centre + v * scale).collect(); + let transformed_face: Vec<_> = face.iter().map(|v| centre + v * scale).collect(); triangulate_polygon(&transformed_face, &normal, Arc::clone(&material)) }) .collect()