use std::ops::Add; //use std::ops::Div; use std::ops::Div; use std::ops::Mul; use std::ops::Neg; use std::ops::Sub; #[derive(Copy, Clone)] pub struct Vec3 { pub x: f64, pub y: f64, pub z: f64, } pub trait LengthSquared { fn length_squared(self) -> f64; } pub trait Length { fn length(self) -> f64; } impl LengthSquared for Vec3 { fn length_squared(self) -> f64 { self.x * self.x + self.y * self.y + self.z * self.z } } impl Length for Vec3 { fn length(self) -> f64 { self.length_squared().sqrt() } } impl Add for Vec3 { type Output = Vec3; fn add(self, other: Vec3) -> Vec3 { Vec3 { x: self.x + other.x, y: self.y + other.y, z: self.z + other.z, } } } impl Mul for f64 { type Output = Vec3; fn mul(self, vec: Vec3) -> Vec3 { Vec3 { x: vec.x * self, y: vec.y * self, z: vec.z * self, } } } impl Mul for Vec3 { type Output = Vec3; fn mul(self, vec: Vec3) -> Vec3 { Vec3 { x: vec.x * self.x, y: vec.y * self.y, z: vec.z * self.z, } } } impl Div for Vec3 { type Output = Vec3; fn div(self, t: f64) -> Vec3 { (1.0 / t) * self } } impl Div for f64 { type Output = Vec3; fn div(self, t: Vec3) -> Vec3 { (1.0 / self) * t } } impl Sub for Vec3 { type Output = Vec3; fn sub(self, other: Self) -> Self::Output { Self { x: self.x - other.x, y: self.y - other.y, z: self.z - other.z, } } } impl Neg for Vec3 { type Output = Self; fn neg(self) -> Self::Output { Self { x: -self.x, y: -self.y, z: -self.z, } } } #[inline] fn cross(lhs: Vec3, rhs: Vec3) -> Vec3 { Vec3 { x: lhs.y * rhs.z - lhs.z * rhs.y, y: lhs.z * rhs.x - lhs.x * rhs.z, z: lhs.x * rhs.y - lhs.y * rhs.x, } } #[inline] pub fn dot(lhs: Vec3, rhs: Vec3) -> f64 { let tmp = lhs * rhs; tmp.x + tmp.y + tmp.z } #[inline] pub fn unit_vector(v: Vec3) -> Vec3 { v / v.length() } pub type Color = Vec3;