Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
aarestad committed Aug 12, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent a5134a0 commit d983da0
Showing 3 changed files with 38 additions and 25 deletions.
45 changes: 26 additions & 19 deletions src/data/vector3.rs
Original file line number Diff line number Diff line change
@@ -34,11 +34,7 @@ pub fn rand_range(min: f64, max: f64) -> Vector {

let dist = Uniform::new_inclusive(min, max);

Vector3::new(
rng.sample(dist),
rng.sample(dist),
rng.sample(dist),
)
Vector3::new(rng.sample(dist), rng.sample(dist), rng.sample(dist))
}

pub fn random_in_unit_cube() -> Vector {
@@ -63,22 +59,21 @@ pub fn refract(vec: &Vector, normal: &Vector, etai_over_etat: f64) -> Vector {

#[cfg(test)]
mod test {
use approx::assert_abs_diff_eq;
use super::EPSILON;
use super::Vector;
use super::random_in_unit_sphere;
use super::random_in_unit_disk;
use super::near_zero;
use super::rand_range;
use super::random_in_unit_cube;
use super::random_in_unit_disk;
use super::random_in_unit_sphere;
use super::reflect;
use super::refract;

use super::Vector;
use super::EPSILON;
use approx::assert_abs_diff_eq;

#[test]
fn accessors() {
let v = Vector::new(1.0, 2.0, 3.0);

assert_eq!(v.x, 1.0);
assert_eq!(v.y, 2.0);
assert_eq!(v.z, 3.0);
@@ -91,18 +86,26 @@ mod test {
#[test]
fn unary_methods() {
let v = Vector::new(1.0, 2.0, 3.0);

assert_eq!(-v, Vector::new(-1.0, -2.0, -3.0));
assert_abs_diff_eq!(v.magnitude(), 3.7416573867, epsilon = EPSILON);
assert_abs_diff_eq!(v.normalize(), Vector::new(0.2672612419, 0.53452248, 0.80178372), epsilon=EPSILON);
assert_abs_diff_eq!(
v.normalize(),
Vector::new(0.2672612419, 0.53452248, 0.80178372),
epsilon = EPSILON
);
}

#[test]
fn vector_scalar_methods() {
let v = Vector::new(1.0, 2.0, 3.0);

assert_eq!(v * 3.0, Vector::new(3.0, 6.0, 9.0));
assert_abs_diff_eq!(v / 3.0, Vector::new(0.33333333, 0.66666666, 1.0), epsilon=EPSILON);
assert_abs_diff_eq!(
v / 3.0,
Vector::new(0.33333333, 0.66666666, 1.0),
epsilon = EPSILON
);
}

#[test]
@@ -114,13 +117,13 @@ mod test {
assert_eq!(v1 - v2, Vector::new(-3.0, -3.0, -3.0));
assert_eq!(v1.component_mul(&v2), Vector::new(4.0, 10.0, 18.0));
assert_eq!(v1.dot(&v2), 32.0);
assert_eq!(v1.cross(&v2), Vector::new(-3.0, 6.0,-3.0));
assert_eq!(v1.cross(&v2), Vector::new(-3.0, 6.0, -3.0));
}

#[test]
fn custom_methods() {
for _ in 0..1000 {
assert_abs_diff_eq!(random_in_unit_sphere().magnitude(), 1.0, epsilon=EPSILON);
assert_abs_diff_eq!(random_in_unit_sphere().magnitude(), 1.0, epsilon = EPSILON);

let random_disk_v = random_in_unit_disk();
assert_eq!(random_disk_v.z, 0.0);
@@ -147,6 +150,10 @@ mod test {
let v2 = Vector::new(4.0, 5.0, 6.0);

assert_abs_diff_eq!(reflect(&v1, &v2), Vector::new(-255.0, -318.0, -381.0));
assert_abs_diff_eq!(refract(&v1, &v2, 0.5), Vector::new(-6.556601564455425, -7.820751955569282, -9.084902346683137), epsilon=EPSILON);
assert_abs_diff_eq!(
refract(&v1, &v2, 0.5),
Vector::new(-6.556601564455425, -7.820751955569282, -9.084902346683137),
epsilon = EPSILON
);
}
}
}
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod args;
pub mod worlds;
mod ppm;
pub mod worlds;

pub const EPSILON: f64 = 1e-8;
16 changes: 11 additions & 5 deletions src/util/ppm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use image::RgbImage;
use std::io;
use std::io::Write;
use image::RgbImage;

pub fn image_to_ppm(image: &RgbImage, file: &mut dyn Write) -> io::Result<()> {
file.write_all("P3\n".as_bytes())?;
@@ -26,11 +26,17 @@ pub fn main() -> io::Result<()> {
let g = j as f64 / ((image_height - 1) as f64);
let b = 0.0;

img.put_pixel(i, j, image::Rgb::from([(r * 255.0).floor() as u8,
(g * 255.0f64).floor() as u8,
(b * 255.0f64).floor() as u8]));
img.put_pixel(
i,
j,
image::Rgb::from([
(r * 255.0).floor() as u8,
(g * 255.0f64).floor() as u8,
(b * 255.0f64).floor() as u8,
]),
);
}
}

image_to_ppm(&img, &mut io::stdout())
}
}

0 comments on commit d983da0

Please sign in to comment.