use crate::{BVec3, DVec3};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::ops::*;
use crate::nums::*;
use auto_ops_det::{impl_op_ex, impl_op_ex_commutative};
use core::ops;
union VecUnionCast {
v: DVec3,
uv: UnitDVec3,
}
impl DVec3 {
#[inline]
pub fn as_unit_dvec3_unchecked(self) -> UnitDVec3 {
unsafe { VecUnionCast { v: self }.uv }
}
}
impl UnitDVec3 {
#[inline]
pub fn as_dvec3(self) -> DVec3 {
unsafe { VecUnionCast { uv: self }.v }
}
}
#[derive(Clone, Copy, PartialEq)]
#[cfg_attr(not(target_arch = "spirv"), repr(C))]
#[cfg_attr(target_arch = "spirv", repr(simd))]
pub struct UnitDVec3 {
pub x: f64,
pub y: f64,
pub z: f64,
}
impl UnitDVec3 {
pub const X: Self = Self::new_unchecked(1.0_f64, 0.0_f64, 0.0_f64);
pub const Y: Self = Self::new_unchecked(0.0_f64, 1.0_f64, 0.0_f64);
pub const Z: Self = Self::new_unchecked(0.0_f64, 0.0_f64, 1.0_f64);
pub const NEG_X: Self = Self::new_unchecked(-1.0_f64, 0.0_f64, 0.0_f64);
pub const NEG_Y: Self = Self::new_unchecked(0.0_f64, -1.0_f64, 0.0_f64);
pub const NEG_Z: Self = Self::new_unchecked(0.0_f64, 0.0_f64, -1.0_f64);
pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z];
#[inline]
pub const fn new_unchecked(x: f64, y: f64, z: f64) -> Self {
Self { x, y, z }
}
#[inline]
pub fn new_normalized(x: f64, y: f64, z: f64) -> Self {
DVec3::new(x, y, z).normalize().as_unit_dvec3_unchecked()
}
#[inline]
pub fn select(mask: BVec3, if_true: Self, if_false: Self) -> DVec3 {
DVec3::select(mask, if_true.as_dvec3(), if_false.as_dvec3())
}
#[inline]
pub const fn from_array_unchecked(a: [f64; 3]) -> Self {
Self::new_unchecked(a[0], a[1], a[2])
}
#[inline]
pub const fn to_array(&self) -> [f64; 3] {
[self.x, self.y, self.z]
}
#[inline]
pub const fn from_slice_unchecked(slice: &[f64]) -> Self {
Self::new_unchecked(slice[0], slice[1], slice[2])
}
#[inline]
pub fn write_to_slice(self, slice: &mut [f64]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
}
#[inline]
pub(crate) fn dot(self, rhs: Self) -> f64 {
self.as_dvec3().dot(rhs.as_dvec3())
}
#[inline]
pub(crate) fn cross(self, rhs: Self) -> DVec3 {
self.as_dvec3().cross(rhs.as_dvec3())
}
#[inline]
pub fn min_element(self) -> f64 {
self.as_dvec3().min_element()
}
#[inline]
pub fn max_element(self) -> f64 {
self.as_dvec3().max_element()
}
#[inline]
pub fn cmpeq(self, rhs: Self) -> BVec3 {
self.as_dvec3().cmpeq(rhs.as_dvec3())
}
#[inline]
pub fn cmpne(self, rhs: Self) -> BVec3 {
self.as_dvec3().cmpne(rhs.as_dvec3())
}
#[inline]
pub fn cmpge(self, rhs: Self) -> BVec3 {
self.as_dvec3().cmpge(rhs.as_dvec3())
}
#[inline]
pub fn cmpgt(self, rhs: Self) -> BVec3 {
self.as_dvec3().cmpgt(rhs.as_dvec3())
}
#[inline]
pub fn cmple(self, rhs: Self) -> BVec3 {
self.as_dvec3().cmple(rhs.as_dvec3())
}
#[inline]
pub fn cmplt(self, rhs: Self) -> BVec3 {
self.as_dvec3().cmple(rhs.as_dvec3())
}
#[inline]
pub fn abs(self) -> Self {
self.as_dvec3().abs().as_unit_dvec3_unchecked()
}
#[inline]
pub fn signum(self) -> DVec3 {
self.as_dvec3().signum()
}
#[inline]
pub fn distance(self, rhs: Self) -> f64 {
self.as_dvec3().distance(rhs.as_dvec3())
}
#[inline]
pub fn distance_squared(self, rhs: Self) -> f64 {
self.as_dvec3().distance_squared(rhs.as_dvec3())
}
#[must_use]
#[inline]
pub fn project_onto(self, rhs: Self) -> DVec3 {
rhs.as_dvec3() * self.dot(rhs)
}
#[must_use]
#[inline]
pub fn reject_from(self, rhs: Self) -> DVec3 {
self.as_dvec3() - self.project_onto(rhs)
}
#[doc(alias = "mix")]
#[inline]
pub fn lerp(self, rhs: Self, s: f64) -> DVec3 {
self.as_dvec3().lerp(rhs.as_dvec3(), s)
}
#[inline]
pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
DVec3::abs_diff_eq(self.as_dvec3(), rhs.as_dvec3(), max_abs_diff)
}
#[inline]
pub fn mul_add(self, a: Self, b: Self) -> DVec3 {
self.as_dvec3().mul_add(a.as_dvec3(), b.as_dvec3())
}
#[inline]
pub fn angle_between(self, rhs: Self) -> f64 {
use crate::FloatEx;
self.dot(rhs).acos_approx()
}
#[inline]
pub fn any_orthogonal_vector(&self) -> Self {
let sign = 1.0_f64.copysignf(self.z);
let a = -1.0_f64 / (sign + self.z);
let b = self.x * self.y * a;
Self::new_unchecked(b, sign + self.y * self.y * a, -self.y)
}
#[inline]
pub fn any_orthogonal_pair(&self) -> (Self, Self) {
let sign = 1.0_f64.copysignf(self.z);
let a = -1.0_f64 / (sign + self.z);
let b = self.x * self.y * a;
(
Self::new_unchecked(
1.0_f64 + sign * self.x * self.x * a,
sign * b,
-sign * self.x,
),
Self::new_unchecked(b, sign + self.y * self.y * a, -self.y),
)
}
}
impl Default for UnitDVec3 {
#[inline]
fn default() -> Self {
Self::X
}
}
impl Neg for UnitDVec3 {
type Output = Self;
#[inline]
fn neg(self) -> Self {
self.as_dvec3().neg().as_unit_dvec3_unchecked()
}
}
impl_op_ex!(/ |a: &UnitDVec3, b: &UnitDVec3| -> DVec3 {
a.as_dvec3() / b.as_dvec3()
});
impl_op_ex!(/ |a: &UnitDVec3, b: &f64| -> DVec3 {
a.as_dvec3() / b
});
impl_op_ex!(/ |a: &f64, b: &UnitDVec3| -> DVec3 {
a / b.as_dvec3()
});
impl_op_ex_commutative!(/ |a: &UnitDVec3, b: &DVec3| -> DVec3 {
a.as_dvec3() / b
});
impl_op_ex!(*|a: &UnitDVec3, b: &UnitDVec3| -> DVec3 { a.as_dvec3() * b.as_dvec3() });
impl_op_ex!(*|a: &UnitDVec3, b: &f64| -> DVec3 { a.as_dvec3() * b });
impl_op_ex!(*|a: &f64, b: &UnitDVec3| -> DVec3 { a * b.as_dvec3() });
impl_op_ex_commutative!(*|a: &UnitDVec3, b: &DVec3| -> DVec3 { a.as_dvec3() * b });
impl_op_ex!(+ |a: &UnitDVec3, b: &UnitDVec3| -> DVec3 {
a.as_dvec3() + b.as_dvec3()
});
impl_op_ex!(+ |a: &UnitDVec3, b: &f64| -> DVec3 {
a.as_dvec3() + b
});
impl_op_ex!(+ |a: &f64, b: &UnitDVec3| -> DVec3 {
a + b.as_dvec3()
});
impl_op_ex_commutative!(+ |a: &UnitDVec3, b: &DVec3| -> DVec3 {
a.as_dvec3() + b
});
impl_op_ex!(-|a: &UnitDVec3, b: &UnitDVec3| -> DVec3 { a.as_dvec3() - b.as_dvec3() });
impl_op_ex!(-|a: &UnitDVec3, b: &f64| -> DVec3 { a.as_dvec3() - b });
impl_op_ex!(-|a: &f64, b: &UnitDVec3| -> DVec3 { a - b.as_dvec3() });
impl_op_ex_commutative!(-|a: &UnitDVec3, b: &DVec3| -> DVec3 { a.as_dvec3() - b });
impl_op_ex!(% |a: &UnitDVec3, b: &UnitDVec3| -> DVec3 {
a.as_dvec3() % b.as_dvec3()
});
impl_op_ex!(% |a: &UnitDVec3, b: &f64| -> DVec3 {
a.as_dvec3() % b
});
impl_op_ex!(% |a: &f64, b: &UnitDVec3| -> DVec3 {
a % b.as_dvec3()
});
impl_op_ex_commutative!(% |a: &UnitDVec3, b: &DVec3| -> DVec3 {
a.as_dvec3() % b
});
impl Index<usize> for UnitDVec3 {
type Output = f64;
#[inline]
fn index(&self, index: usize) -> &Self::Output {
match index {
0 => &self.x,
1 => &self.y,
2 => &self.z,
_ => panic!("index out of bounds"),
}
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for UnitDVec3 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for UnitDVec3 {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_tuple(stringify!(UnitDVec3))
.field(&self.x)
.field(&self.y)
.field(&self.z)
.finish()
}
}
impl From<UnitDVec3> for [f64; 3] {
#[inline]
fn from(v: UnitDVec3) -> Self {
v.as_dvec3().into()
}
}
impl From<UnitDVec3> for (f64, f64, f64) {
#[inline]
fn from(v: UnitDVec3) -> Self {
v.as_dvec3().into()
}
}
#[cfg(not(target_arch = "spirv"))]
impl AsRef<[f64; 3]> for UnitDVec3 {
#[inline]
fn as_ref(&self) -> &[f64; 3] {
unsafe { &*(self as *const UnitDVec3 as *const [f64; 3]) }
}
}