From 49c6adb0db70ffc30eaac33b66eacf7574b34e26 Mon Sep 17 00:00:00 2001 From: Julian T Date: Wed, 10 Feb 2021 23:06:48 +0100 Subject: Fixed most clippy warnings --- src/camera/camera.rs | 2 +- src/camera/mod.rs | 2 +- src/core/bound.rs | 2 +- src/core/vector2.rs | 2 +- src/core/vector3.rs | 6 ++-- src/lib.rs | 2 +- src/main.rs | 9 +++--- src/material/lambertian.rs | 2 +- src/sample/uniform.rs | 9 ++++-- src/scene/mod.rs | 25 ----------------- src/scene/scene.rs | 47 ------------------------------- src/scene/shapes/mod.rs | 4 --- src/scene/shapes/sphere.rs | 70 ---------------------------------------------- src/trace/mod.rs | 2 +- src/trace/pathtrace.rs | 2 +- src/world/mod.rs | 23 +++++++++++++++ src/world/scene.rs | 52 ++++++++++++++++++++++++++++++++++ src/world/shapes/mod.rs | 4 +++ src/world/shapes/sphere.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 19 files changed, 171 insertions(+), 164 deletions(-) delete mode 100644 src/scene/mod.rs delete mode 100644 src/scene/scene.rs delete mode 100644 src/scene/shapes/mod.rs delete mode 100644 src/scene/shapes/sphere.rs create mode 100644 src/world/mod.rs create mode 100644 src/world/scene.rs create mode 100644 src/world/shapes/mod.rs create mode 100644 src/world/shapes/sphere.rs diff --git a/src/camera/camera.rs b/src/camera/camera.rs index 4d05e87..241d26b 100644 --- a/src/camera/camera.rs +++ b/src/camera/camera.rs @@ -79,7 +79,7 @@ impl Camera { // Calculate translation vectors let mut forward = set.target - set.origin; - let focus = set.focus.unwrap_or(forward.len()); + let focus = set.focus.unwrap_or_else(|| forward.length()); forward.norm_in(); diff --git a/src/camera/mod.rs b/src/camera/mod.rs index 7c36307..999b5b5 100644 --- a/src/camera/mod.rs +++ b/src/camera/mod.rs @@ -7,7 +7,7 @@ pub mod film; //pub mod filter; -pub mod camera; +mod camera; pub use camera::{Camera, CameraSettings}; pub use film::Film; diff --git a/src/core/bound.rs b/src/core/bound.rs index 404424e..3978028 100644 --- a/src/core/bound.rs +++ b/src/core/bound.rs @@ -83,7 +83,7 @@ impl Bound2 { /// ``` pub fn area(&self) -> T { let diag = self.diagonal(); - return diag.x * diag.y; + diag.x * diag.y } } diff --git a/src/core/vector2.rs b/src/core/vector2.rs index 0e0165e..405b12a 100644 --- a/src/core/vector2.rs +++ b/src/core/vector2.rs @@ -112,7 +112,7 @@ impl Vector2i { } impl Vector2f { - pub fn len(&self) -> Float { + pub fn length(&self) -> Float { (self.x*self.x + self.y*self.y).sqrt() } diff --git a/src/core/vector3.rs b/src/core/vector3.rs index 24b84e9..e26c07c 100644 --- a/src/core/vector3.rs +++ b/src/core/vector3.rs @@ -117,7 +117,7 @@ impl Vector3f { self.x * self.x + self.y * self.y + self.z * self.z } - pub fn len(&self) -> Float { + pub fn length(&self) -> Float { self.len_squared().sqrt() } @@ -137,7 +137,7 @@ impl Vector3f { /// ``` pub fn norm_in(&mut self) { // TODO Experiment with checking for normality with len_squared - let len = self.len(); + let len = self.length(); if len == 0.0 { *self = Self::new(0.0); } @@ -146,7 +146,7 @@ impl Vector3f { } pub fn norm(&self) -> Self { - let mut new = self.clone(); + let mut new = *self; new.norm_in(); new } diff --git a/src/lib.rs b/src/lib.rs index f3047d6..a40e8e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ pub mod core; pub mod camera; pub mod render; -pub mod scene; +pub mod world; pub mod trace; pub mod sample; pub mod material; diff --git a/src/main.rs b/src/main.rs index 2247834..215e0d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ use rendering::camera::{Camera, Film, CameraSettings}; -use rendering::scene::{Scene, Object}; +use rendering::world::{Scene, Object, shapes::Sphere}; use rendering::trace::DefaultTracer; -use rendering::scene::shapes::Sphere; use rendering::core::{Vector2i, Vector3f, Spectrum}; use rendering::render::{RenderContext, RenderCoord}; use rendering::sample::UniformSampler; @@ -28,9 +27,9 @@ fn main() { let mut scn = Scene::new(); scn.add_objects(vec![ - Object::new(metal.clone(), Box::new(Sphere::new(0.5, Vector3f::new_xyz(0.0, 0.0, -1.0)))), - Object::new(blue.clone(), Box::new(Sphere::new(0.5, Vector3f::new_xyz(1.0, 0.0, -1.0)))), - Object::new(brown.clone(), Box::new(Sphere::new(100.0, Vector3f::new_xyz(0.0, -100.5, -1.0)))), + Object::new(metal, Box::new(Sphere::new(0.5, Vector3f::new_xyz(0.0, 0.0, -1.0)))), + Object::new(blue, Box::new(Sphere::new(0.5, Vector3f::new_xyz(1.0, 0.0, -1.0)))), + Object::new(brown, Box::new(Sphere::new(100.0, Vector3f::new_xyz(0.0, -100.5, -1.0)))), ]); let tracer = DefaultTracer::new(&scn, Some(50)); diff --git a/src/material/lambertian.rs b/src/material/lambertian.rs index 65a59cc..8ef5e50 100644 --- a/src/material/lambertian.rs +++ b/src/material/lambertian.rs @@ -32,6 +32,6 @@ impl Material for Lambertian { newray.direction = i.n; } - Some((self.color.clone(), newray)) + Some((self.color, newray)) } } diff --git a/src/sample/uniform.rs b/src/sample/uniform.rs index e2f0b7c..2cd3156 100644 --- a/src/sample/uniform.rs +++ b/src/sample/uniform.rs @@ -13,6 +13,12 @@ pub struct UniformSampler { impl UniformSampler { pub fn new() -> Self { + Self::default() + } +} + +impl Default for UniformSampler { + fn default() -> Self { Self { r: Pcg32::seed_from_u64(1), d: Uniform::new(0.0, 1.0), @@ -22,7 +28,6 @@ impl UniformSampler { impl Sampler for UniformSampler { fn get_sample(&mut self) -> Float { - let sample = self.d.sample(&mut self.r); - sample + self.d.sample(&mut self.r) } } diff --git a/src/scene/mod.rs b/src/scene/mod.rs deleted file mode 100644 index cd07236..0000000 --- a/src/scene/mod.rs +++ /dev/null @@ -1,25 +0,0 @@ -//! Defines the scene type which contains all the objects in the scene. -//! -//! Also handles finding intersections between rays and shapes -pub mod shapes; - -mod scene; -pub use scene::*; - -use std::rc::Rc; -use crate::core::Hittable; -use crate::material::Material; - -pub struct Object { - pub shape: Box, - pub mat: Rc, -} - -impl Object { - pub fn new(mat: Rc, shape: Box) -> Self { - Object { - mat, - shape, - } - } -} diff --git a/src/scene/scene.rs b/src/scene/scene.rs deleted file mode 100644 index 83a43c7..0000000 --- a/src/scene/scene.rs +++ /dev/null @@ -1,47 +0,0 @@ -use crate::core::{Ray, Intersection}; -use crate::material::Material; - -use super::Object; - -pub struct Scene { - objs: Vec, -} - -pub struct SceneIntersect<'a> { - pub mat: &'a dyn Material, - pub i: Intersection, -} - -impl Scene { - pub fn new() -> Self { - Self { - objs: Vec::new(), - } - } - - pub fn add_object(&mut self, obj: Object) { - self.objs.push(obj); - } - - pub fn add_objects(&mut self, objs: Vec) { - for obj in objs { - self.add_object(obj); - } - } - - pub fn intersect(&self, ray: &Ray) -> Option { - let mut min: Option = None; - - for obj in self.objs.iter() { - if let Some(i) = obj.shape.intersect(&ray) { - match min { - Some(ref si) if si.i.t < i.t => (), - _ => min = Some(SceneIntersect {i, mat: obj.mat.as_ref() }), - } - } - } - - min - } -} - diff --git a/src/scene/shapes/mod.rs b/src/scene/shapes/mod.rs deleted file mode 100644 index d7583ad..0000000 --- a/src/scene/shapes/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -mod sphere; - -pub use sphere::Sphere; - diff --git a/src/scene/shapes/sphere.rs b/src/scene/shapes/sphere.rs deleted file mode 100644 index eaa6625..0000000 --- a/src/scene/shapes/sphere.rs +++ /dev/null @@ -1,70 +0,0 @@ -//! Implements sphere -//! -//! Spheres are relatively easy to calculate intersections between -use crate::Float; -use crate::core::{Ray, Vector3f, Hittable, Intersection}; - -pub struct Sphere { - radius: Float, - center: Vector3f, -} - -impl Sphere { - pub fn new(radius: Float, center: Vector3f) -> Sphere { - Sphere { - radius, - center, - } - } - - fn norm_at(&self, point: &Vector3f) -> Vector3f { - let mut v = *point - self.center; - v /= self.radius; - v - } -} - -impl Hittable for Sphere { - // Implementation from ray tracing in a weekend - fn intersect(&self, ray: &Ray) -> Option { - let oc = ray.origin - self.center; - let a = ray.direction.len_squared(); - let half_b = oc.dot(&ray.direction); - let c = oc.len_squared() - self.radius * self.radius; - let disc = half_b*half_b - a*c; - - if disc < 0.0 { - return None - } else { - let distance = (-half_b - disc.sqrt()) / a; - if distance < 0.0 { - return None - } - let w = ray.at(distance); - Some(Intersection { - n: self.norm_at(&w), - p: w, - t: distance, - }) - } - - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn sphere_intersect() { - let sph = Sphere::new(2.0, Vector3f::new_xyz(2.0, 3.0, 4.0)); - - let ray = Ray { - origin: Vector3f::new_xyz(1.0, 0.0, 0.0), - direction: Vector3f::new_xyz(0.0, 1.0, 1.5).norm(), - }; - - let dist = sph.intersect(&ray).unwrap(); - assert!((dist.t - 3.28).abs() < 0.01); - } -} diff --git a/src/trace/mod.rs b/src/trace/mod.rs index 6c2c9f3..b7de476 100644 --- a/src/trace/mod.rs +++ b/src/trace/mod.rs @@ -1,4 +1,4 @@ -use crate::scene::Scene; +use crate::world::Scene; use crate::core::{Spectrum, Ray, Vector3f}; use crate::sample::Sampler; diff --git a/src/trace/pathtrace.rs b/src/trace/pathtrace.rs index af24118..47c6835 100644 --- a/src/trace/pathtrace.rs +++ b/src/trace/pathtrace.rs @@ -1,4 +1,4 @@ -use crate::scene::Scene; +use crate::world::Scene; use crate::core::{Ray, Spectrum}; use crate::sample::Sampler; use super::Tracer; diff --git a/src/world/mod.rs b/src/world/mod.rs new file mode 100644 index 0000000..ef239db --- /dev/null +++ b/src/world/mod.rs @@ -0,0 +1,23 @@ +//! Manages world objects, and implements intersection +pub mod shapes; + +mod scene; +pub use scene::*; + +use std::rc::Rc; +use crate::core::Hittable; +use crate::material::Material; + +pub struct Object { + pub shape: Box, + pub mat: Rc, +} + +impl Object { + pub fn new(mat: Rc, shape: Box) -> Self { + Object { + mat, + shape, + } + } +} diff --git a/src/world/scene.rs b/src/world/scene.rs new file mode 100644 index 0000000..444e915 --- /dev/null +++ b/src/world/scene.rs @@ -0,0 +1,52 @@ +use crate::core::{Ray, Intersection}; +use crate::material::Material; + +use super::Object; + +pub struct Scene { + objs: Vec, +} + +pub struct SceneIntersect<'a> { + pub mat: &'a dyn Material, + pub i: Intersection, +} + +impl Scene { + pub fn new() -> Self { + Self::default() + } + + pub fn add_object(&mut self, obj: Object) { + self.objs.push(obj); + } + + pub fn add_objects(&mut self, objs: Vec) { + for obj in objs { + self.add_object(obj); + } + } + + pub fn intersect(&self, ray: &Ray) -> Option { + let mut min: Option = None; + + for obj in self.objs.iter() { + if let Some(i) = obj.shape.intersect(&ray) { + match min { + Some(ref si) if si.i.t < i.t => (), + _ => min = Some(SceneIntersect {i, mat: obj.mat.as_ref() }), + } + } + } + + min + } +} + +impl Default for Scene { + fn default() -> Self { + Self { + objs: Vec::new(), + } + } +} diff --git a/src/world/shapes/mod.rs b/src/world/shapes/mod.rs new file mode 100644 index 0000000..d7583ad --- /dev/null +++ b/src/world/shapes/mod.rs @@ -0,0 +1,4 @@ +mod sphere; + +pub use sphere::Sphere; + diff --git a/src/world/shapes/sphere.rs b/src/world/shapes/sphere.rs new file mode 100644 index 0000000..8e0816b --- /dev/null +++ b/src/world/shapes/sphere.rs @@ -0,0 +1,70 @@ +//! Implements sphere +//! +//! Spheres are relatively easy to calculate intersections between +use crate::Float; +use crate::core::{Ray, Vector3f, Hittable, Intersection}; + +pub struct Sphere { + radius: Float, + center: Vector3f, +} + +impl Sphere { + pub fn new(radius: Float, center: Vector3f) -> Sphere { + Sphere { + radius, + center, + } + } + + fn norm_at(&self, point: &Vector3f) -> Vector3f { + let mut v = *point - self.center; + v /= self.radius; + v + } +} + +impl Hittable for Sphere { + // Implementation from ray tracing in a weekend + fn intersect(&self, ray: &Ray) -> Option { + let oc = ray.origin - self.center; + let a = ray.direction.len_squared(); + let half_b = oc.dot(&ray.direction); + let c = oc.len_squared() - self.radius * self.radius; + let disc = half_b*half_b - a*c; + + if disc < 0.0 { + None + } else { + let distance = (-half_b - disc.sqrt()) / a; + if distance < 0.0 { + return None + } + let w = ray.at(distance); + Some(Intersection { + n: self.norm_at(&w), + p: w, + t: distance, + }) + } + + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn sphere_intersect() { + let sph = Sphere::new(2.0, Vector3f::new_xyz(2.0, 3.0, 4.0)); + + let ray = Ray { + origin: Vector3f::new_xyz(1.0, 0.0, 0.0), + direction: Vector3f::new_xyz(0.0, 1.0, 1.5).norm(), + }; + + let dist = sph.intersect(&ray).unwrap(); + assert!((dist.t - 3.28).abs() < 0.01); + } +} -- cgit v1.2.3