From b89e01107d18e952786ce7bcc11b7f14aaba46c3 Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Tue, 7 Apr 2020 23:01:56 +0200 Subject: [PATCH] library: unnest scene module to please clippy warning: module has the same name as its containing module --> pathtracer/src/scene/mod.rs:12:1 | 12 | pub mod scene; | ^^^^^^^^^^^^^^ | = note: `#[warn(clippy::module_inception)]` on by default --- pathtracer/src/scene/mod.rs | 149 +++++++++++++++++++++++++++++++++- pathtracer/src/scene/scene.rs | 149 ---------------------------------- 2 files changed, 147 insertions(+), 151 deletions(-) delete mode 100644 pathtracer/src/scene/scene.rs diff --git a/pathtracer/src/scene/mod.rs b/pathtracer/src/scene/mod.rs index 24d58d9..7d4e0af 100644 --- a/pathtracer/src/scene/mod.rs +++ b/pathtracer/src/scene/mod.rs @@ -1,5 +1,10 @@ //! Desciption of the scene. +use beevee::bvh::BVH; +use serde::Deserialize; + +use crate::core::{Camera, LinearColor}; + pub mod light_aggregate; pub use light_aggregate::*; @@ -9,5 +14,145 @@ pub use mesh::*; pub mod object; pub use object::*; -pub mod scene; -pub use scene::*; +/// Represent the scene being rendered. +#[serde(from = "SerializedScene")] +#[derive(Debug, PartialEq, Deserialize)] +pub struct Scene { + pub(crate) camera: Camera, + pub(crate) lights: LightAggregate, + pub(crate) objects: Vec, + pub(crate) bvh: BVH, + pub(crate) background: LinearColor, + pub(crate) shot_rays: u32, + pub(crate) reflection_limit: u32, + pub(crate) diffraction_index: f32, +} + +impl Scene { + /// Creates a new `Scene`. + /// + /// # Examples + /// + /// ``` + /// # use pathtracer::core::{Camera, LightProperties, LinearColor}; + /// # use pathtracer::material::UniformMaterial; + /// # use pathtracer::scene::{LightAggregate, Object, Scene}; + /// # use pathtracer::shape::Sphere; + /// # use pathtracer::texture::UniformTexture; + /// # use pathtracer::Point; + /// # + /// let scene = Scene::new( + /// Camera::default(), + /// LightAggregate::empty(), + /// vec![ + /// Object::new( + /// Sphere::new(Point::origin(), 1.0).into(), + /// UniformMaterial::new( + /// LightProperties::new( + /// LinearColor::new(1.0, 0.0, 0.0), // diffuse component + /// LinearColor::new(0.0, 0.0, 0.0), // specular component + /// None, + /// LinearColor::black(), // Emitted light + /// ), + /// ).into(), + /// UniformTexture::new(LinearColor::new(0.5, 0.5, 0.5)).into(), + /// ), + /// ], + /// LinearColor::black(), // Background color + /// 5, // amount of rays shot per pixel + /// 3, // reflection recursion limit + /// 0.0, // diffraction index + /// ); + /// ``` + pub fn new( + camera: Camera, + lights: LightAggregate, + mut objects: Vec, + background: LinearColor, + shot_rays: u32, + reflection_limit: u32, + diffraction_index: f32, + ) -> Self { + let bvh = BVH::build(&mut objects); + Scene { + camera, + lights, + objects, + bvh, + background, + shot_rays, + reflection_limit, + diffraction_index, + } + } +} + +#[derive(Debug, PartialEq, Deserialize)] +#[serde(deny_unknown_fields)] +struct SerializedScene { + camera: Camera, + #[serde(default)] + lights: LightAggregate, + #[serde(default)] + objects: Vec, + #[serde(default)] + meshes: Vec, + #[serde(default)] + background: LinearColor, + #[serde(default)] + shot_rays: u32, + #[serde(default)] + reflection_limit: u32, + #[serde(default = "crate::serialize::default_identity")] + starting_diffraction: f32, +} + +impl From for Scene { + fn from(mut scene: SerializedScene) -> Self { + let mut flattened_meshes: Vec = scene + .meshes + .into_iter() + .map(|m| m.shapes) + .flatten() + .collect(); + scene.objects.append(&mut flattened_meshes); + + Scene::new( + scene.camera, + scene.lights, + scene.objects, + scene.background, + scene.shot_rays, + scene.reflection_limit, + scene.starting_diffraction, + ) + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn deserialization_works() { + let yaml = std::include_str!("../../examples/scene.yaml"); + let _: Scene = serde_yaml::from_str(yaml).unwrap(); + // FIXME: actually test the equality ? + } + + #[test] + fn empty_scene() { + use crate::core::Camera; + use crate::scene::{LightAggregate, Scene}; + + let _scene = Scene::new( + Camera::default(), + LightAggregate::empty(), + Vec::new(), // Objects list + LinearColor::black(), // Background color + 5, // aliasing limit + 3, // reflection recursion limit + 0.0, // diffraction index + ); + } +} diff --git a/pathtracer/src/scene/scene.rs b/pathtracer/src/scene/scene.rs deleted file mode 100644 index c4165d2..0000000 --- a/pathtracer/src/scene/scene.rs +++ /dev/null @@ -1,149 +0,0 @@ -//! Scene representation. - -use super::{LightAggregate, Mesh, Object}; -use crate::core::{Camera, LinearColor}; -use beevee::bvh::BVH; -use serde::Deserialize; - -/// Represent the scene being rendered. -#[serde(from = "SerializedScene")] -#[derive(Debug, PartialEq, Deserialize)] -pub struct Scene { - pub(crate) camera: Camera, - pub(crate) lights: LightAggregate, - pub(crate) objects: Vec, - pub(crate) bvh: BVH, - pub(crate) background: LinearColor, - pub(crate) shot_rays: u32, - pub(crate) reflection_limit: u32, - pub(crate) diffraction_index: f32, -} - -impl Scene { - /// Creates a new `Scene`. - /// - /// # Examples - /// - /// ``` - /// # use pathtracer::core::{Camera, LightProperties, LinearColor}; - /// # use pathtracer::material::UniformMaterial; - /// # use pathtracer::scene::{LightAggregate, Object, Scene}; - /// # use pathtracer::shape::Sphere; - /// # use pathtracer::texture::UniformTexture; - /// # use pathtracer::Point; - /// # - /// let scene = Scene::new( - /// Camera::default(), - /// LightAggregate::empty(), - /// vec![ - /// Object::new( - /// Sphere::new(Point::origin(), 1.0).into(), - /// UniformMaterial::new( - /// LightProperties::new( - /// LinearColor::new(1.0, 0.0, 0.0), // diffuse component - /// LinearColor::new(0.0, 0.0, 0.0), // specular component - /// None, - /// LinearColor::black(), // Emitted light - /// ), - /// ).into(), - /// UniformTexture::new(LinearColor::new(0.5, 0.5, 0.5)).into(), - /// ), - /// ], - /// LinearColor::black(), // Background color - /// 5, // amount of rays shot per pixel - /// 3, // reflection recursion limit - /// 0.0, // diffraction index - /// ); - /// ``` - pub fn new( - camera: Camera, - lights: LightAggregate, - mut objects: Vec, - background: LinearColor, - shot_rays: u32, - reflection_limit: u32, - diffraction_index: f32, - ) -> Self { - let bvh = BVH::build(&mut objects); - Scene { - camera, - lights, - objects, - bvh, - background, - shot_rays, - reflection_limit, - diffraction_index, - } - } -} - -#[derive(Debug, PartialEq, Deserialize)] -#[serde(deny_unknown_fields)] -struct SerializedScene { - camera: Camera, - #[serde(default)] - lights: LightAggregate, - #[serde(default)] - objects: Vec, - #[serde(default)] - meshes: Vec, - #[serde(default)] - background: LinearColor, - #[serde(default)] - shot_rays: u32, - #[serde(default)] - reflection_limit: u32, - #[serde(default = "crate::serialize::default_identity")] - starting_diffraction: f32, -} - -impl From for Scene { - fn from(mut scene: SerializedScene) -> Self { - let mut flattened_meshes: Vec = scene - .meshes - .into_iter() - .map(|m| m.shapes) - .flatten() - .collect(); - scene.objects.append(&mut flattened_meshes); - - Scene::new( - scene.camera, - scene.lights, - scene.objects, - scene.background, - scene.shot_rays, - scene.reflection_limit, - scene.starting_diffraction, - ) - } -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - fn deserialization_works() { - let yaml = std::include_str!("../../examples/scene.yaml"); - let _: Scene = serde_yaml::from_str(yaml).unwrap(); - // FIXME: actually test the equality ? - } - - #[test] - fn empty_scene() { - use crate::core::Camera; - use crate::scene::{LightAggregate, Scene}; - - let _scene = Scene::new( - Camera::default(), - LightAggregate::empty(), - Vec::new(), // Objects list - LinearColor::black(), // Background color - 5, // aliasing limit - 3, // reflection recursion limit - 0.0, // diffraction index - ); - } -}