use rand::prelude::*; use std::fmt::{Display, Formatter, Result}; use std::ops::{Add, AddAssign, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Neg, Range, Sub}; use rand::rngs::ThreadRng; #[derive(Clone, Copy, Debug, Default)] pub struct Vec3 { pub x: f64, pub y: f64, pub z: f64, } pub type Point3 = Vec3; impl Vec3 { pub fn new(x: f64, y: f64, z: f64) -> Self { Self { x, y, z } } pub fn length(self) -> f64 { self.length_squared().sqrt() } pub fn length_squared(self) -> f64 { let Self { x, y, z } = self; x * x + y * y + z * z } fn random(range: Range, rng: &mut ThreadRng) -> Vec3 { Vec3::new( rng.random_range(range.clone()), rng.random_range(range.clone()), rng.random_range(range), ) } pub fn random_unit(rng: &mut ThreadRng) -> Vec3 { loop { let vec = Self::random(-1.0..1.0, rng); let length_squared = vec.length_squared(); if 1e-160 < length_squared && length_squared <= 1.0 { return vec / length_squared.sqrt(); } } } pub fn random_on_hemisphere(normal: Vec3, rng: &mut ThreadRng) -> Vec3 { let on_unit_sphere = Self::random_unit(rng); if dot(on_unit_sphere, normal) > 0.0 { on_unit_sphere } else { -on_unit_sphere } } pub fn random_unit_disk(rng: &mut ThreadRng) -> Point3 { loop { let p = Point3::new( rng.random_range(-1.0..1.0), rng.random_range(-1.0..1.0), 0.0, ); if p.length_squared() < 1.0 { return p; } } } pub fn near_zero(self) -> bool { let Self { x, y, z } = self; const S: f64 = 1e-8; x.abs() < S && y.abs() < S && z.abs() < S } pub fn reflect(v: Self, n: Self) -> Self { v - (2.0 * dot(v, n) * n) } pub fn refract(uv: Self, n: Self, etai_over_etat: f64) -> Self { let cos_theta = dot(-uv, n).min(1.0); let r_out_perpendicular = etai_over_etat * (uv + cos_theta * n); let r_out_parralel = -((1.0 - r_out_perpendicular.length_squared()).sqrt()) * n; r_out_perpendicular + r_out_parralel } } impl Neg for Vec3 { type Output = Self; fn neg(self) -> Self::Output { let Self { x, y, z } = self; Self::new(-x, -y, -z) } } impl Index for Vec3 { type Output = f64; fn index(&self, i: usize) -> &Self::Output { match i { 0 => &self.x, 1 => &self.y, 2 => &self.z, _ => panic!("Vec3 you fool."), } } } impl IndexMut for Vec3 { fn index_mut(&mut self, i: usize) -> &mut Self::Output { match i { 0 => &mut self.x, 1 => &mut self.y, 2 => &mut self.z, _ => panic!("Vec3 you fool."), } } } impl AddAssign for Vec3 { fn add_assign(&mut self, v: Self) { self.x += v.x; self.y += v.y; self.z += v.z; } } impl MulAssign for Vec3 { fn mul_assign(&mut self, t: f64) { self.x *= t; self.y *= t; self.z *= t; } } impl DivAssign for Vec3 { fn div_assign(&mut self, t: f64) { *self *= 1.0 / t; } } impl Display for Vec3 { fn fmt(&self, f: &mut Formatter<'_>) -> Result { let Self { x, y, z } = &self; write!(f, "{} {} {}", x, y, z) } } impl Add for Vec3 { type Output = Self; fn add(self, v: Self) -> Self { let Self { x, y, z } = &self; Self::new(x + v.x, y + v.y, z + v.z) } } impl Sub for Vec3 { type Output = Self; fn sub(self, v: Self) -> Self { let Self { x, y, z } = &self; Self::new(x - v.x, y - v.y, z - v.z) } } impl Mul for Vec3 { type Output = Self; fn mul(self, v: Self) -> Self { let Self { x, y, z } = &self; Self::new(x * v.x, y * v.y, z * v.z) } } impl Mul for Vec3 { type Output = Self; fn mul(self, t: f64) -> Self { let Self { x, y, z } = self; Self::new(x * t, y * t, z * t) } } impl Mul for f64 { type Output = Vec3; fn mul(self, v: Vec3) -> Vec3 { v * self } } impl Div for Vec3 { type Output = Self; fn div(self, t: f64) -> Self { (1.0 / t) * self } } pub fn dot(u: Vec3, v: Vec3) -> f64 { u.x * v.x + u.y * v.y + u.z * v.z } pub fn cross(u: Vec3, v: Vec3) -> Vec3 { Vec3::new( u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z, u.x * v.y - u.y * v.x, ) } pub fn unit_vector(v: Vec3) -> Vec3 { v / v.length() }