ft: 3: vec3 class
This commit is contained in:
252
src/vec3.rs
Normal file
252
src/vec3.rs
Normal file
@@ -0,0 +1,252 @@
|
||||
use core::f32::math::sqrt;
|
||||
use std::{
|
||||
fmt::Display,
|
||||
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign},
|
||||
};
|
||||
|
||||
pub struct Vec3 {
|
||||
r: f32,
|
||||
g: f32,
|
||||
b: f32,
|
||||
}
|
||||
|
||||
impl Vec3 {
|
||||
pub fn rgb(r: f32, g: f32, b: f32) -> Vec3 {
|
||||
return Self { r: r, g: g, b: b };
|
||||
}
|
||||
pub fn length(&self) -> f32 {
|
||||
return sqrt(self.length_squared());
|
||||
}
|
||||
|
||||
pub fn length_squared(&self) -> f32 {
|
||||
return (self.r * self.r + self.g * self.g + self.b * self.b) as f32;
|
||||
}
|
||||
|
||||
pub fn dot(self, other: Vec3) -> Vec3 {
|
||||
Vec3 {
|
||||
r: self.r * other.r,
|
||||
g: self.g * other.g,
|
||||
b: self.b * other.b,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cross(self, other: Vec3) -> Vec3 {
|
||||
Vec3 {
|
||||
r: self.g * other.b - self.b * other.g,
|
||||
g: self.b * other.r - self.r * other.b,
|
||||
b: self.r * other.g - self.g * other.r,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unit(mut self) {
|
||||
self /= self.length();
|
||||
}
|
||||
|
||||
pub fn output(self) -> image::Rgb<u8> {
|
||||
let ir = (255.599 * self.r) as u8;
|
||||
let ig = (255.599 * self.g) as u8;
|
||||
let ib = (255.599 * self.b) as u8;
|
||||
|
||||
image::Rgb([ir, ig, ib])
|
||||
}
|
||||
}
|
||||
|
||||
impl Neg for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
fn neg(self) -> Self::Output {
|
||||
Self {
|
||||
r: -self.r,
|
||||
g: -self.g,
|
||||
b: -self.b,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<Vec3> for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, _rhs: Self) -> Self {
|
||||
return Self {
|
||||
r: self.r + _rhs.r,
|
||||
g: self.g + _rhs.g,
|
||||
b: self.b + _rhs.b,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<f32> for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, f: f32) -> Self {
|
||||
return Self {
|
||||
r: self.r + f,
|
||||
g: self.g + f,
|
||||
b: self.b + f,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign<Vec3> for Vec3 {
|
||||
fn add_assign(&mut self, other: Self) {
|
||||
*self = Self {
|
||||
r: self.r + other.r,
|
||||
g: self.g + other.g,
|
||||
b: self.b + other.b,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign<f32> for Vec3 {
|
||||
fn add_assign(&mut self, f: f32) {
|
||||
*self = Self {
|
||||
r: self.r + f,
|
||||
g: self.g + f,
|
||||
b: self.b + f,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub<Vec3> for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, rhs: Self) -> Self {
|
||||
return Self {
|
||||
r: self.r - rhs.r,
|
||||
g: self.g - rhs.g,
|
||||
b: self.b - rhs.b,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub<f32> for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, f: f32) -> Self {
|
||||
return Self {
|
||||
r: self.r - f,
|
||||
g: self.g - f,
|
||||
b: self.b - f,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl SubAssign<Vec3> for Vec3 {
|
||||
fn sub_assign(&mut self, rhs: Self) {
|
||||
*self = Self {
|
||||
r: self.r - rhs.r,
|
||||
g: self.g - rhs.g,
|
||||
b: self.b - rhs.b,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl SubAssign<f32> for Vec3 {
|
||||
fn sub_assign(&mut self, f: f32) {
|
||||
*self = Self {
|
||||
r: self.r - f,
|
||||
g: self.g - f,
|
||||
b: self.b - f,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<Vec3> for Vec3 {
|
||||
type Output = Vec3;
|
||||
|
||||
fn mul(self, rhs: Self) -> Self::Output {
|
||||
Self {
|
||||
r: self.r * rhs.r,
|
||||
g: self.g * rhs.g,
|
||||
b: self.b * rhs.b,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<f32> for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, f: f32) -> Self::Output {
|
||||
Self {
|
||||
r: self.r * f,
|
||||
g: self.g * f,
|
||||
b: self.b * f,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MulAssign<Vec3> for Vec3 {
|
||||
fn mul_assign(&mut self, rhs: Self) {
|
||||
*self = Self {
|
||||
r: self.r * rhs.r,
|
||||
g: self.g * rhs.g,
|
||||
b: self.b * rhs.b,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl MulAssign<f32> for Vec3 {
|
||||
fn mul_assign(&mut self, f: f32) {
|
||||
*self = Self {
|
||||
r: self.r * f,
|
||||
g: self.g * f,
|
||||
b: self.b * f,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl Div<Vec3> for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
fn div(self, rhs: Self) -> Self::Output {
|
||||
Self {
|
||||
r: self.r / rhs.r,
|
||||
g: self.g / rhs.g,
|
||||
b: self.b / rhs.b,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Div<f32> for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
fn div(self, f: f32) -> Self::Output {
|
||||
Self {
|
||||
r: self.r / f,
|
||||
g: self.g / f,
|
||||
b: self.b / f,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DivAssign<Vec3> for Vec3 {
|
||||
fn div_assign(&mut self, rhs: Self) {
|
||||
*self = Self {
|
||||
r: self.r / rhs.r,
|
||||
g: self.g / rhs.g,
|
||||
b: self.b / rhs.b,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl DivAssign<f32> for Vec3 {
|
||||
fn div_assign(&mut self, f: f32) {
|
||||
*self = Self {
|
||||
r: self.r / f,
|
||||
g: self.g / f,
|
||||
b: self.b / f,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Vec3 {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.r == other.r && self.g == other.g && self.b == other.b
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Vec3 {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "({}, {}, {})", self.r, self.g, self.b)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user