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