diff --git a/src/camera.rs b/src/camera.rs index 7b109d1..41beaa8 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,6 +1,5 @@ -use crate::vec3::Vec3; -use crate::ray::Ray; +use crate::primitives::{Vec3, Ray}; use crate::degrees_to_radians; use rand::rngs::SmallRng; diff --git a/src/hittable.rs b/src/hittable.rs index 990e21f..988589b 100644 --- a/src/hittable.rs +++ b/src/hittable.rs @@ -1,6 +1,5 @@ -use crate::vec3::Vec3; -use crate::ray::Ray; +use crate::primitives::{Vec3, Ray}; use crate::material::Material; pub struct HitRecord{ diff --git a/src/main.rs b/src/main.rs index adbc408..395e5db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,11 @@ -mod vec3; -mod ray; +mod primitives; mod camera; mod material; mod hittable; mod thread_utils; -use crate::vec3::Vec3; -use crate::ray::Ray; +use crate::primitives::{Vec3, Ray}; use crate::hittable::Hittable; use crate::material::Material; use crate::camera::Camera; diff --git a/src/material.rs b/src/material.rs index f9ff8ce..4e7c0d2 100644 --- a/src/material.rs +++ b/src/material.rs @@ -1,8 +1,6 @@ -use crate::ray::Ray; +use crate::primitives::{Vec3, Ray}; use crate::hittable::HitRecord; -use crate::vec3; -use crate::vec3::Vec3; use rand::Rng; use rand::rngs::SmallRng; @@ -62,7 +60,7 @@ impl Material { let refraction_ratio = if rec.front_face { 1.0 / index_refraction } else { *index_refraction }; let unit_direction = Vec3::as_unit(ray_in.dir); - let cos_theta = vec3::min(Vec3::dot(-unit_direction, rec.normal), 1.0); + let cos_theta = Vec3::dot(-unit_direction, rec.normal).min(1.0); let sin_theta = (1.0 - cos_theta * cos_theta).sqrt(); let cannot_refract = refraction_ratio * sin_theta > 1.0; diff --git a/src/vec3.rs b/src/primitives.rs similarity index 94% rename from src/vec3.rs rename to src/primitives.rs index 3ab8a3e..da54ce0 100644 --- a/src/vec3.rs +++ b/src/primitives.rs @@ -97,9 +97,9 @@ impl Vec3{ let g = (self.y * scale).sqrt(); let b = (self.z * scale).sqrt(); - let ir = (clamp(r, 0.0, 0.999) * 256.0) as i32; - let ig = (clamp(g, 0.0, 0.999) * 256.0) as i32; - let ib = (clamp(b, 0.0, 0.999) * 256.0) as i32; + let ir = (r.clamp( 0.0, 0.999) * 256.0) as i32; + let ig = (g.clamp( 0.0, 0.999) * 256.0) as i32; + let ib = (b.clamp( 0.0, 0.999) * 256.0) as i32; format!("{} {} {}", ir, ig, ib) } @@ -116,7 +116,7 @@ impl Vec3{ } pub fn refract(uv: Vec3, n: Vec3, etai_over_etat: f32) -> Vec3 { - let cos_theta = min(Vec3::dot(-uv, n), 1.0); + let cos_theta = Vec3::dot(-uv, n).min(1.0); let r_out_perp = (uv + n * cos_theta) * etai_over_etat; let r_out_parallel = n * -(1.0 - r_out_perp.length_squared()).abs().sqrt(); r_out_perp + r_out_parallel @@ -288,18 +288,6 @@ impl Display for Vec3 { } } -pub fn clamp(input: f32, lower: f32, upper: f32) -> f32 { - min(max(input, lower), upper) -} - -pub fn min(a: f32, b: f32) -> f32 { - if a < b { a } else { b } -} - -pub fn max(a: f32, b: f32) -> f32 { - if a > b { a } else { b } -} - #[cfg(test)] mod test{ use super::*; @@ -561,3 +549,32 @@ mod test{ } } +#[derive(Copy, Clone)] +pub struct Ray{ + pub orig: Vec3, + pub dir: Vec3, +} + +impl Ray{ + pub fn at(&self, t: f32) -> Vec3 { + self.orig + self.dir*t + } +} + +#[cfg(test)] +mod test{ + use super::*; + + #[test] + fn check_lerp(){ + let ray = Ray{ + orig: Vec3::new(0.0, 0.0, 0.0), + dir: Vec3::new(1.0, 1.0, 0.0) + }; + let half = ray.at(0.5); + assert_eq!( + half, + Vec3::new(0.5, 0.5, 0.0) + ); + } +} diff --git a/src/ray.rs b/src/ray.rs deleted file mode 100644 index f6eb722..0000000 --- a/src/ray.rs +++ /dev/null @@ -1,32 +0,0 @@ - -use crate::vec3::Vec3; - -#[derive(Copy, Clone)] -pub struct Ray{ - pub orig: Vec3, - pub dir: Vec3, -} - -impl Ray{ - pub fn at(&self, t: f32) -> Vec3 { - self.orig + self.dir*t - } -} - -#[cfg(test)] -mod test{ - use super::*; - - #[test] - fn check_lerp(){ - let ray = Ray{ - orig: Vec3::new(0.0, 0.0, 0.0), - dir: Vec3::new(1.0, 1.0, 0.0) - }; - let half = ray.at(0.5); - assert_eq!( - half, - Vec3::new(0.5, 0.5, 0.0) - ); - } -}