From 8c527d34fc9729bb4ff9a6e8664106cb0fcf5e73 Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Thu, 30 Jan 2020 17:01:23 -0500 Subject: [PATCH] Add Primitive trait --- src/main.rs | 14 +++++++------- src/raycasting/mod.rs | 2 ++ src/raycasting/plane.rs | 6 +++--- src/raycasting/sphere.rs | 4 +++- src/raycasting/triangle.rs | 4 +++- src/scene.rs | 6 ++++-- 6 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6d66786..08cd592 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,7 @@ use vanrijn::colour::{ColourRgbF, NamedColour}; use vanrijn::image::{ClampingToneMapper, ImageRgbF, ImageRgbU8, ToneMapper}; use vanrijn::materials::{LambertianMaterial, PhongMaterial, ReflectiveMaterial}; use vanrijn::mesh::load_obj; -use vanrijn::raycasting::{Intersect, Plane, Sphere}; +use vanrijn::raycasting::{Primitive, Plane, Sphere}; use vanrijn::scene::Scene; fn update_texture(image: &ImageRgbU8, texture: &mut Texture) { @@ -75,17 +75,17 @@ pub fn main() -> Result<(), Box> { ) .unwrap() .into_iter() - .map(|triangle| Box::new(triangle) as Box>) + .map(|triangle| Arc::new(triangle) as Arc>) .chain(vec![ - Box::new(Plane::new( + Arc::new(Plane::new( Vector3::new(0.0, 1.0, 0.0), -2.0, Arc::new(LambertianMaterial { colour: ColourRgbF::new(0.55, 0.27, 0.04), diffuse_strength: 0.1, }), - )) as Box>, - Box::new(Sphere::new( + )) as Arc>, + Arc::new(Sphere::new( Point3::new(-6.25, -0.5, 1.0), 1.0, Arc::new(LambertianMaterial { @@ -93,7 +93,7 @@ pub fn main() -> Result<(), Box> { diffuse_strength: 0.1, }), )), - Box::new(Sphere::new( + Arc::new(Sphere::new( Point3::new(-4.25, -0.5, 2.0), 1.0, Arc::new(ReflectiveMaterial { @@ -102,7 +102,7 @@ pub fn main() -> Result<(), Box> { reflection_strength: 0.99, }), )), - Box::new(Sphere::new( + Arc::new(Sphere::new( Point3::new(-5.0, 1.5, 1.0), 1.0, Arc::new(PhongMaterial { diff --git a/src/raycasting/mod.rs b/src/raycasting/mod.rs index 4dac05d..eda0bb5 100644 --- a/src/raycasting/mod.rs +++ b/src/raycasting/mod.rs @@ -64,6 +64,8 @@ pub trait HasBoundingBox: Send + Sync { fn bounding_box(&self) -> BoundingBox; } +pub trait Primitive: Intersect + HasBoundingBox {} + #[cfg(test)] mod tests { use quickcheck_macros::quickcheck; diff --git a/src/raycasting/plane.rs b/src/raycasting/plane.rs index d9bb235..fd8ebda 100644 --- a/src/raycasting/plane.rs +++ b/src/raycasting/plane.rs @@ -2,7 +2,7 @@ use nalgebra::{convert, Point3, RealField, Vector3}; use crate::materials::Material; -use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Ray}; +use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Primitive, Ray}; use std::sync::Arc; @@ -87,6 +87,8 @@ impl HasBoundingBox for Plane { } } +impl Primitive for Plane {} + #[cfg(test)] mod tests { use nalgebra::Point3; @@ -274,6 +276,4 @@ mod tests { assert!(bb.contains_point(Point3::new(-2000.0, 2.0, 2.0))); assert!(bb.contains_point(Point3::new(3.0, 2.0, 3.0))); } - - } diff --git a/src/raycasting/sphere.rs b/src/raycasting/sphere.rs index 1872923..7fd7e3e 100644 --- a/src/raycasting/sphere.rs +++ b/src/raycasting/sphere.rs @@ -2,7 +2,7 @@ use nalgebra::{convert, Point3, RealField, Vector3}; use crate::materials::Material; -use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Ray}; +use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Primitive, Ray}; use std::sync::Arc; @@ -84,6 +84,8 @@ impl HasBoundingBox for Sphere { } } +impl Primitive for Sphere {} + #[cfg(test)] mod tests { use quickcheck::TestResult; diff --git a/src/raycasting/triangle.rs b/src/raycasting/triangle.rs index 04c49d5..d38f92a 100644 --- a/src/raycasting/triangle.rs +++ b/src/raycasting/triangle.rs @@ -1,6 +1,6 @@ use crate::materials::Material; -use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Ray}; +use super::{BoundingBox, HasBoundingBox, Intersect, IntersectionInfo, Primitive, Ray}; use nalgebra::{Point3, RealField, Vector2, Vector3}; use std::sync::Arc; @@ -83,6 +83,8 @@ impl HasBoundingBox for Triangle { } } +impl Primitive for Triangle {} + fn indices_with_index_of_largest_element_last(v: &Vector3) -> [usize; 3] { if v.x > v.y { if v.z > v.x { diff --git a/src/scene.rs b/src/scene.rs index b569050..d11d826 100644 --- a/src/scene.rs +++ b/src/scene.rs @@ -1,8 +1,10 @@ use nalgebra::{Point3, RealField}; -use crate::raycasting::Intersect; +use crate::raycasting::Primitive; + +use std::sync::Arc; pub struct Scene { pub camera_location: Point3, - pub objects: Vec>>, + pub objects: Vec>>, }