use std::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
#[derive(Clone, Debug, PartialEq, Copy)]
pub struct Vec3(f64, f64, f64);
pub const ZERO_VEC3: Vec3 = Vec3(0.0, 0.0, 0.0);
pub const ONE_VEC3: Vec3 = Vec3(1.0, 1.0, 1.0);
impl Default for Vec3 {
fn default() -> Self {
ZERO_VEC3
}
}
impl Vec3 {
pub fn new(x: f64, y: f64, z: f64) -> Self {
Vec3(x, y, z)
}
pub fn zero() -> Self {
Vec3::default()
}
pub fn zeros(n: usize) -> Vec<Vec3> {
vec![Vec3::zero(); n]
}
pub fn x(&self) -> f64 {
self.0
}
pub fn y(&self) -> f64 {
self.1
}
pub fn z(&self) -> f64 {
self.2
}
pub fn norm(&self) -> f64 {
(self.0 * self.0 + self.1 * self.1 + self.2 * self.2).sqrt()
}
pub fn norm_2(&self) -> f64 {
self.0 * self.0 + self.1 * self.1 + self.2 * self.2
}
pub fn hat(&self) -> Vec3 {
let norm = self.norm();
Vec3(self.0 / norm, self.1 / norm, self.2 / norm)
}
pub fn to_str(&self) -> String {
format!("({:.9}, {:.9}, {:.9})", self.0, self.1, self.2)
}
pub fn to_tuple(&self) -> (f64, f64, f64) {
(self.0, self.1, self.2)
}
}
impl Add for Vec3 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Vec3(self.0 + rhs.0, self.1 + rhs.1, self.2 + rhs.2)
}
}
impl AddAssign for Vec3 {
fn add_assign(&mut self, rhs: Self) {
self.0 += rhs.0;
self.1 += rhs.1;
self.2 += rhs.2;
}
}
impl AddAssign<&Vec3> for Vec3 {
fn add_assign(&mut self, rhs: &Vec3) {
self.0 += rhs.0;
self.1 += rhs.1;
self.2 += rhs.2;
}
}
impl SubAssign for Vec3 {
fn sub_assign(&mut self, rhs: Self) {
self.0 -= rhs.0;
self.1 -= rhs.1;
self.2 -= rhs.2;
}
}
impl Sub for Vec3 {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Vec3(self.0 - rhs.0, self.1 - rhs.1, self.2 - rhs.2)
}
}
impl Mul<f64> for Vec3 {
type Output = Vec3;
fn mul(self, rhs: f64) -> Self::Output {
Vec3(self.0 * rhs, self.1 * rhs, self.2 * rhs)
}
}
impl Mul<Vec3> for f64 {
type Output = Vec3;
fn mul(self, rhs: Vec3) -> Self::Output {
Vec3(rhs.0 * self, rhs.1 * self, rhs.2 * self)
}
}
impl Mul<Vec3> for Vec3 {
type Output = Vec3;
fn mul(self, rhs: Vec3) -> Self::Output {
Vec3(rhs.0 * self.0, rhs.1 * self.1, rhs.2 * self.2)
}
}
impl Div<f64> for Vec3 {
type Output = Vec3;
fn div(self, rhs: f64) -> Self::Output {
Vec3(self.0 / rhs, self.1 / rhs, self.2 / rhs)
}
}
impl Div<Vec3> for Vec3 {
type Output = Vec3;
fn div(self, rhs: Vec3) -> Self::Output {
Vec3(self.0 / rhs.0, self.1 / rhs.1, self.2 / rhs.2)
}
}