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/scene/mod.rs | 25 ----------------- src/scene/scene.rs | 47 ------------------------------- src/scene/shapes/mod.rs | 4 --- src/scene/shapes/sphere.rs | 70 ---------------------------------------------- 4 files changed, 146 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 (limited to 'src/scene') 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); - } -} -- cgit v1.2.3