use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
pub struct Point3 {
pub x: f64,
pub y: f64,
pub z: f64,
}
impl Point3 {
pub const ZERO: Point3 = Point3 {
x: 0.0,
y: 0.0,
z: 0.0,
};
pub const ORIGIN: Point3 = Self::ZERO;
pub const fn new(x: f64, y: f64, z: f64) -> Self {
Self { x, y, z }
}
pub const fn xy(x: f64, y: f64) -> Self {
Self { x, y, z: 0.0 }
}
pub const fn to_array(self) -> [f64; 3] {
[self.x, self.y, self.z]
}
}
impl From<[f64; 3]> for Point3 {
fn from([x, y, z]: [f64; 3]) -> Self {
Self::new(x, y, z)
}
}
impl From<[f64; 2]> for Point3 {
fn from([x, y]: [f64; 2]) -> Self {
Self::xy(x, y)
}
}
impl From<(f64, f64, f64)> for Point3 {
fn from((x, y, z): (f64, f64, f64)) -> Self {
Self::new(x, y, z)
}
}
impl From<(f64, f64)> for Point3 {
fn from((x, y): (f64, f64)) -> Self {
Self::xy(x, y)
}
}
impl Add<Vec3> for Point3 {
type Output = Point3;
fn add(self, rhs: Vec3) -> Self::Output {
Self::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
}
}
impl AddAssign<Vec3> for Point3 {
fn add_assign(&mut self, rhs: Vec3) {
self.x += rhs.x;
self.y += rhs.y;
self.z += rhs.z;
}
}
impl Sub<Vec3> for Point3 {
type Output = Point3;
fn sub(self, rhs: Vec3) -> Self::Output {
Self::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
}
}
impl SubAssign<Vec3> for Point3 {
fn sub_assign(&mut self, rhs: Vec3) {
self.x -= rhs.x;
self.y -= rhs.y;
self.z -= rhs.z;
}
}
impl Sub<Point3> for Point3 {
type Output = Vec3;
fn sub(self, rhs: Point3) -> Self::Output {
Vec3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
pub struct Vec3 {
pub x: f64,
pub y: f64,
pub z: f64,
}
impl Vec3 {
pub const ZERO: Vec3 = Vec3 {
x: 0.0,
y: 0.0,
z: 0.0,
};
pub const ONE: Vec3 = Vec3 {
x: 1.0,
y: 1.0,
z: 1.0,
};
pub const X: Vec3 = Vec3 {
x: 1.0,
y: 0.0,
z: 0.0,
};
pub const Y: Vec3 = Vec3 {
x: 0.0,
y: 1.0,
z: 0.0,
};
pub const Z: Vec3 = Vec3 {
x: 0.0,
y: 0.0,
z: 1.0,
};
pub const fn new(x: f64, y: f64, z: f64) -> Self {
Self { x, y, z }
}
pub const fn to_array(self) -> [f64; 3] {
[self.x, self.y, self.z]
}
}
impl From<[f64; 3]> for Vec3 {
fn from([x, y, z]: [f64; 3]) -> Self {
Self::new(x, y, z)
}
}
impl From<(f64, f64, f64)> for Vec3 {
fn from((x, y, z): (f64, f64, f64)) -> Self {
Self::new(x, y, z)
}
}
impl Add for Vec3 {
type Output = Vec3;
fn add(self, rhs: Vec3) -> Self::Output {
Self::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
}
}
impl AddAssign for Vec3 {
fn add_assign(&mut self, rhs: Vec3) {
self.x += rhs.x;
self.y += rhs.y;
self.z += rhs.z;
}
}
impl Sub for Vec3 {
type Output = Vec3;
fn sub(self, rhs: Vec3) -> Self::Output {
Self::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
}
}
impl SubAssign for Vec3 {
fn sub_assign(&mut self, rhs: Vec3) {
self.x -= rhs.x;
self.y -= rhs.y;
self.z -= rhs.z;
}
}
impl Mul<f64> for Vec3 {
type Output = Vec3;
fn mul(self, rhs: f64) -> Self::Output {
Self::new(self.x * rhs, self.y * rhs, self.z * rhs)
}
}
impl MulAssign<f64> for Vec3 {
fn mul_assign(&mut self, rhs: f64) {
self.x *= rhs;
self.y *= rhs;
self.z *= rhs;
}
}
impl Mul<Vec3> for f64 {
type Output = Vec3;
fn mul(self, rhs: Vec3) -> Self::Output {
Vec3::new(self * rhs.x, self * rhs.y, self * rhs.z)
}
}
impl Neg for Vec3 {
type Output = Vec3;
fn neg(self) -> Self::Output {
Self::new(-self.x, -self.y, -self.z)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct BBox {
pub min: Point3,
pub max: Point3,
}