use std::fmt;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use crate::EPS;
#[derive(Clone, Copy, PartialEq)]
#[repr(C)]
pub struct Vec3 {
pub x: f64,
pub y: f64,
pub z: f64,
}
impl Vec3 {
pub const ZERO: Self = Self {
x: 0.0,
y: 0.0,
z: 0.0,
};
pub const X: Self = Self {
x: 1.0,
y: 0.0,
z: 0.0,
};
pub const Y: Self = Self {
x: 0.0,
y: 1.0,
z: 0.0,
};
pub const Z: Self = Self {
x: 0.0,
y: 0.0,
z: 1.0,
};
#[inline]
pub const fn new(x: f64, y: f64, z: f64) -> Self {
Self { x, y, z }
}
#[inline]
pub fn length_sq(self) -> f64 {
self.dot(self)
}
#[inline]
pub fn length(self) -> f64 {
self.length_sq().sqrt()
}
#[inline]
pub fn dot(self, rhs: Self) -> f64 {
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
}
#[inline]
pub fn cross(self, rhs: Self) -> Self {
Self {
x: self.y * rhs.z - self.z * rhs.y,
y: self.z * rhs.x - self.x * rhs.z,
z: self.x * rhs.y - self.y * rhs.x,
}
}
#[inline]
pub fn try_normalize(self) -> Option<Self> {
let len = self.length();
if len < EPS {
None
} else {
Some(self / len)
}
}
#[inline]
pub fn normalize(self) -> Self {
self.try_normalize()
.expect("Vec3::normalize called on zero vector")
}
#[inline]
pub fn abs(self) -> Self {
Self::new(self.x.abs(), self.y.abs(), self.z.abs())
}
#[inline]
pub fn reflect(self, n: Self) -> Self {
self - n * (2.0 * self.dot(n))
}
pub fn any_perp(self) -> Self {
if self.x.abs() <= self.y.abs() && self.x.abs() <= self.z.abs() {
self.cross(Self::X)
} else if self.y.abs() <= self.z.abs() {
self.cross(Self::Y)
} else {
self.cross(Self::Z)
}
}
#[inline]
pub fn lerp(self, rhs: Self, t: f64) -> Self {
self + (rhs - self) * t
}
}
impl Add for Vec3 {
type Output = Self;
#[inline]
fn add(self, r: Self) -> Self {
Self::new(self.x + r.x, self.y + r.y, self.z + r.z)
}
}
impl Sub for Vec3 {
type Output = Self;
#[inline]
fn sub(self, r: Self) -> Self {
Self::new(self.x - r.x, self.y - r.y, self.z - r.z)
}
}
impl Neg for Vec3 {
type Output = Self;
#[inline]
fn neg(self) -> Self {
Self::new(-self.x, -self.y, -self.z)
}
}
impl Mul<f64> for Vec3 {
type Output = Self;
#[inline]
fn mul(self, s: f64) -> Self {
Self::new(self.x * s, self.y * s, self.z * s)
}
}
impl Mul<Vec3> for f64 {
type Output = Vec3;
#[inline]
fn mul(self, v: Vec3) -> Vec3 {
v * self
}
}
impl Div<f64> for Vec3 {
type Output = Self;
#[inline]
fn div(self, s: f64) -> Self {
self * (1.0 / s)
}
}
impl AddAssign for Vec3 {
#[inline]
fn add_assign(&mut self, r: Self) {
*self = *self + r;
}
}
impl SubAssign for Vec3 {
#[inline]
fn sub_assign(&mut self, r: Self) {
*self = *self - r;
}
}
impl MulAssign<f64> for Vec3 {
#[inline]
fn mul_assign(&mut self, s: f64) {
*self = *self * s;
}
}
impl DivAssign<f64> for Vec3 {
#[inline]
fn div_assign(&mut self, s: f64) {
*self = *self / s;
}
}
impl fmt::Debug for Vec3 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Vec3({:.6}, {:.6}, {:.6})", self.x, self.y, self.z)
}
}
impl fmt::Display for Vec3 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({:.4}, {:.4}, {:.4})", self.x, self.y, self.z)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cross_product() {
let x = Vec3::X;
let y = Vec3::Y;
let z = x.cross(y);
assert!((z - Vec3::Z).length() < 1e-12);
}
#[test]
fn normalize_unit_length() {
let v = Vec3::new(3.0, 4.0, 0.0).normalize();
assert!((v.length() - 1.0).abs() < 1e-12);
}
#[test]
fn reflect() {
let v = Vec3::new(1.0, -1.0, 0.0);
let n = Vec3::Y;
let r = v.reflect(n);
assert!((r - Vec3::new(1.0, 1.0, 0.0)).length() < 1e-12);
}
}