From ae460c3f34838e3baf0ffafe4ccbcf1fbfe03095 Mon Sep 17 00:00:00 2001 From: Julian T Date: Sat, 31 Jul 2021 18:27:56 +0200 Subject: Remove dynamic boxes from object and list implementation --- src/main.rs | 8 ++++---- src/world/container/list.rs | 6 +++--- src/world/mod.rs | 11 ++++++----- src/world/scene.rs | 2 +- src/world/shapes/mod.rs | 26 ++++++++++++++++++++++++++ 5 files changed, 40 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index dc9ea43..5f336c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,10 +28,10 @@ fn main() { let mut scn = Scene::new(); scn.add_objects(vec![ - Object::new(metal, Box::new(Sphere::new(0.2, 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)))), - Object::new(sun, Box::new(Sphere::new(0.4, Vector3f::new_xyz(-1.0, 0.7, 0.0)))), + Object::new(metal, Sphere::new(0.2, Vector3f::new_xyz(0.0, 0.0, -1.0))), + Object::new(blue, Sphere::new(0.5, Vector3f::new_xyz(1.0, 0.0, -1.0))), + Object::new(brown, Sphere::new(100.0, Vector3f::new_xyz(0.0, -100.5, -1.0))), + Object::new(sun, Sphere::new(0.4, Vector3f::new_xyz(-1.0, 0.7, 0.0))), ]); let tracer = DefaultTracer::new(&scn, Some(50), diff --git a/src/world/container/list.rs b/src/world/container/list.rs index c8e3fdd..22b6d88 100644 --- a/src/world/container/list.rs +++ b/src/world/container/list.rs @@ -1,9 +1,9 @@ -use crate::world::{Hittable, Intersection}; +use crate::world::{Object, Hittable, Intersection}; use crate::core::{Bound3f, Ray}; pub struct HittableList { - elems: Vec>, + elems: Vec, } impl HittableList { @@ -11,7 +11,7 @@ impl HittableList { Self::default() } - pub fn add(&mut self, h: Box) { + pub fn add(&mut self, h: Object) { self.elems.push(h); } } diff --git a/src/world/mod.rs b/src/world/mod.rs index 3a09522..cba6ddc 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -6,21 +6,22 @@ pub mod container; mod hittable; pub use scene::*; pub use hittable::{Intersection, Hittable}; +pub use shapes::Shape; use std::sync::Arc; use crate::material::Material; use crate::core::{Bound3f, Ray}; pub struct Object { - pub shape: Box, - pub mat: Arc, + pub shape: Shape, + pub mat: Arc, } impl Object { - pub fn new(mat: Arc, shape: Box) -> Self { + pub fn new>(mat: Arc, shape: T) -> Self { Object { mat, - shape, + shape: shape.into(), } } } @@ -29,7 +30,7 @@ impl Hittable for Object { fn intersect(&self, ray: &Ray) -> Option { if let Some(mut inter) = self.shape.intersect(ray) { inter.add_material_if_none(self.mat.as_ref()); - Some(inter) + Some(inter) } else { None } diff --git a/src/world/scene.rs b/src/world/scene.rs index 6d15fc1..87bec1f 100644 --- a/src/world/scene.rs +++ b/src/world/scene.rs @@ -14,7 +14,7 @@ impl Scene { } pub fn add_object(&mut self, obj: Object) { - self.content.add(Box::new(obj)); + self.content.add(obj); } pub fn add_objects(&mut self, objs: Vec) { diff --git a/src/world/shapes/mod.rs b/src/world/shapes/mod.rs index d7583ad..a11df5d 100644 --- a/src/world/shapes/mod.rs +++ b/src/world/shapes/mod.rs @@ -2,3 +2,29 @@ mod sphere; pub use sphere::Sphere; +use crate::world::{Hittable, Intersection}; +use crate::core::{Bound3f, Ray}; + +pub enum Shape { + Sphere(Sphere), +} + +impl Hittable for Shape { + fn intersect(&self, ray: &Ray) -> Option { + match self { + Self::Sphere(sph) => sph.intersect(ray) + } + } + + fn bounding_box(&self) -> Bound3f { + match self { + Self::Sphere(sph) => sph.bounding_box() + } + } +} + +impl From for Shape { + fn from(s: Sphere) -> Self { + Self::Sphere(s) + } +} -- cgit v1.2.3