Skip to content

Commit

Permalink
refactored into modules
Browse files Browse the repository at this point in the history
  • Loading branch information
scott223 committed Nov 12, 2023
1 parent 1e91f40 commit 9b0ae10
Show file tree
Hide file tree
Showing 18 changed files with 53 additions and 37 deletions.
2 changes: 1 addition & 1 deletion benches/default_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use rand_distr::Uniform;
use raytracer::bvh::aabb::Aabb;

use raytracer::interval::Interval;
use raytracer::render::ray::Ray;
use raytracer::linalg::vec3::Vec3;
use raytracer::render::ray::Ray;

pub fn criterion_benchmark(c: &mut Criterion) {
let bbox: Aabb = Aabb::new_from_points(Vec3::new(1.0, 1.0, 1.0), Vec3::new(2.0, 2.0, 2.0));
Expand Down
4 changes: 2 additions & 2 deletions src/bvh/aabb.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::elements::Hittable;
use crate::interval::Interval;
use crate::render::Ray;
use crate::linalg::Vec3;
use crate::render::Ray;

use std::fmt;
use std::ops::Index;
Expand Down Expand Up @@ -172,8 +172,8 @@ impl Index<usize> for Aabb {
mod tests {
use crate::bvh::aabb::Aabb;
use crate::interval::Interval;
use crate::render::Ray;
use crate::linalg::Vec3;
use crate::render::Ray;
use assert_approx_eq::assert_approx_eq;

// Test creating a new aabb, taking intervals as an argument
Expand Down
2 changes: 1 addition & 1 deletion src/bvh/bvh_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use super::aabb::Aabb;

use crate::elements::*;
use crate::interval::Interval;
use crate::render::Ray;
use crate::linalg::Vec3;
use crate::render::Ray;

use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
Expand Down
2 changes: 1 addition & 1 deletion src/bvh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ mod aabb;
pub use aabb::Aabb;

mod bvh_node;
pub use bvh_node::BVHNode;
pub use bvh_node::BVHNode;
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::render::camera::Camera;
use crate::elements::*;
use crate::interval::Interval;
use crate::render::camera::Camera;
use crate::render::Ray;
use crate::{render::camera::JSONCamera, color::Color};
use crate::{color::Color, render::camera::JSONCamera};

use serde::{Deserialize, Serialize};

Expand Down
11 changes: 7 additions & 4 deletions src/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ use serde::{Deserialize, Serialize};
extern crate wavefront_obj;
use wavefront_obj::obj;

use crate::linalg::{Vec3, Onb, mat4::{Mat4, Vec4}};
use crate::{bvh::Aabb, materials::*};
use crate::render::Ray;
use crate::interval::Interval;
use crate::linalg::{
mat4::{Mat4, Vec4},
Onb, Vec3,
};
use crate::render::Ray;
use crate::{bvh::Aabb, materials::*};

// hitrecord gets returned on a hit, containting the point on the ray, the point in the global coordinate system, the normal and the material for the hit
#[derive(Debug)]
Expand Down Expand Up @@ -822,8 +825,8 @@ impl JSONObj {
mod tests {
use crate::color::Color;
use crate::elements::*;
use crate::render::Ray;
use crate::linalg::Vec3;
use crate::render::Ray;
use assert_approx_eq::assert_approx_eq;

#[test_log::test]
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub mod render;
pub mod bvh;
pub mod color;
pub mod config;
pub mod elements;
pub mod interval;
pub mod linalg;
pub mod materials;
pub mod materials;
pub mod render;
5 changes: 4 additions & 1 deletion src/linalg/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,10 @@ impl Div<f64> for Mat4 {

#[cfg(test)]
mod tests {
use crate::linalg::{vec3::Vec3, mat4::{Mat4, Vec4}};
use crate::linalg::{
mat4::{Mat4, Vec4},
vec3::Vec3,
};
use assert_approx_eq::assert_approx_eq;

// Test creating a new vector with three rows, taking floats as an argument
Expand Down
2 changes: 1 addition & 1 deletion src/linalg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ pub use vec3::Vec3;
pub mod mat4;

mod onb;
pub use onb::Onb;
pub use onb::Onb;
10 changes: 6 additions & 4 deletions src/materials/dielectric.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use rand::Rng;
use serde::{Deserialize, Serialize};

use crate::{color::Color, render::Ray, elements::HitRecord, linalg::Vec3};
use crate::{color::Color, elements::HitRecord, linalg::Vec3, render::Ray};

use super::{Refracts, RefractRecord};
use super::{RefractRecord, Refracts};

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Dielectric {
Expand Down Expand Up @@ -42,7 +42,9 @@ impl Refracts for Dielectric {

let cannot_refract: bool = refraction_ratio * sin_theta > 1.0;

if cannot_refract || super::metal::reflectance(cos_theta, refraction_ratio) > rng.gen::<f64>() {
if cannot_refract
|| super::metal::reflectance(cos_theta, refraction_ratio) > rng.gen::<f64>()
{
let direction: Vec3 = super::metal::reflect_vector(&unit_direction, &hit_record.normal);
let reflected_ray: Ray = Ray::new(hit_record.point, direction);
let refract_record: RefractRecord = RefractRecord {
Expand All @@ -61,4 +63,4 @@ impl Refracts for Dielectric {
return Some(refract_record);
}
}
}
}
4 changes: 2 additions & 2 deletions src/materials/diffuse_light.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

use crate::{elements::HitRecord, color::Color, render::Ray};
use crate::{color::Color, elements::HitRecord, render::Ray};

use super::Emmits;

Expand All @@ -26,4 +26,4 @@ impl Emmits for DiffuseLight {
None
}
}
}
}
10 changes: 7 additions & 3 deletions src/materials/lambertian.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use rand::Rng;
use serde::{Deserialize, Serialize};

use crate::{color::Color, render::{Ray, Pdf, CosinePDF}, elements::HitRecord};
use crate::{
color::Color,
elements::HitRecord,
render::{CosinePDF, Pdf, Ray},
};

use super::{Scatterable, ScatterRecord};
use super::{ScatterRecord, Scatterable};

// Lambertian (diffuse) material, that scatters rays in a semi-random direction (lambertian distribution = more concentrated around the normal)
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
Expand Down Expand Up @@ -36,4 +40,4 @@ impl Scatterable for Lambertian {

Some(scatter)
}
}
}
6 changes: 3 additions & 3 deletions src/materials/metal.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use rand::Rng;
use serde::{Deserialize, Serialize};

use crate::{color::Color, render::Ray, elements::HitRecord, linalg::Vec3};
use crate::{color::Color, elements::HitRecord, linalg::Vec3, render::Ray};

use super::{Reflects, ReflectRecord};
use super::{ReflectRecord, Reflects};

// Metal material, with a fuzz factor. Metal reflects all rays in a predictable way (normal reflection)
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
Expand Down Expand Up @@ -73,4 +73,4 @@ pub fn reflectance(cosine: f64, ref_idx: f64) -> f64 {
let mut r0 = (1.0 - ref_idx) / (1.0 + ref_idx);
r0 = r0 * r0;
r0 + (1.0 - r0) * (1.0 - cosine).powi(5)
}
}
10 changes: 7 additions & 3 deletions src/materials/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ mod dielectric;
pub use dielectric::Dielectric;

use rand::Rng;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

use crate::{elements::HitRecord, render::{Ray, Pdf}, color::Color};
use crate::{
color::Color,
elements::HitRecord,
render::{Pdf, Ray},
};

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum Material {
Expand Down Expand Up @@ -128,4 +132,4 @@ impl Emmits for Material {
_ => None,
}
}
}
}
2 changes: 1 addition & 1 deletion src/render/camera.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rand::Rng;
use serde::{Deserialize, Serialize};

use crate::{config::Config, render::ray::Ray, linalg::Vec3};
use crate::{config::Config, linalg::Vec3, render::ray::Ray};

#[derive(Serialize, Deserialize, Debug)]
pub struct JSONCamera {
Expand Down
4 changes: 2 additions & 2 deletions src/render/integrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use rayon::prelude::*;

use crate::{
bvh::BVHNode,
render::camera::Camera,
color::Color,
config::{Config, JSONScene},
elements::{Element, Hittable, JSONElement},
interval::Interval,
materials::{Emmits, Reflects, Refracts, Scatterable},
render::camera::Camera,
render::pdf::{HittablePDF, MixedPDF, PDFTrait, Pdf},
render::ray::Ray,
};
Expand Down Expand Up @@ -274,4 +274,4 @@ mod tests {
//render(scene, config)?;
Ok(())
}
}
}
6 changes: 3 additions & 3 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub mod integrator;
pub mod camera;
pub mod integrator;

mod pdf;
pub use pdf::{Pdf, PDFTrait, CosinePDF, MixedPDF, HittablePDF};
pub use pdf::{CosinePDF, HittablePDF, MixedPDF, PDFTrait, Pdf};

mod ray;
pub use ray::Ray;
pub use ray::Ray;
2 changes: 1 addition & 1 deletion src/render/ray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ impl fmt::Display for Ray {

#[cfg(test)]
mod tests {
use crate::render::ray::Ray;
use crate::linalg::Vec3;
use crate::render::ray::Ray;
use assert_approx_eq::assert_approx_eq;

#[test_log::test]
Expand Down

0 comments on commit 9b0ae10

Please sign in to comment.