use crate::nums::*;
use crate::{BVec3x4, Vec3x4};
use crate::UnitVec3;
#[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: Vec3x4,
uv: UnitVec3x4,
}
impl Vec3x4 {
#[inline]
pub fn as_unit_vec3x4_unchecked(self) -> UnitVec3x4 {
unsafe { VecUnionCast { v: self }.uv }
}
}
impl UnitVec3x4 {
#[inline]
pub fn as_vec3x4(self) -> Vec3x4 {
unsafe { VecUnionCast { uv: self }.v }
}
}
#[derive(Clone, Copy, PartialEq)]
#[repr(C)]
pub struct UnitVec3x4 {
pub x: f32x4,
pub y: f32x4,
pub z: f32x4,
}
impl UnitVec3x4 {
pub const X: Self = Self::new_unchecked(f32x4::ONE, f32x4::ZERO, f32x4::ZERO);
pub const Y: Self = Self::new_unchecked(f32x4::ZERO, f32x4::ONE, f32x4::ZERO);
pub const Z: Self = Self::new_unchecked(f32x4::ZERO, f32x4::ZERO, f32x4::ONE);
pub const NEG_X: Self = Self::new_unchecked(f32x4::NEG_ONE, f32x4::ZERO, f32x4::ZERO);
pub const NEG_Y: Self = Self::new_unchecked(f32x4::ZERO, f32x4::NEG_ONE, f32x4::ZERO);
pub const NEG_Z: Self = Self::new_unchecked(f32x4::ZERO, f32x4::ZERO, f32x4::NEG_ONE);
pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z];
#[inline]
pub const fn new_unchecked(x: f32x4, y: f32x4, z: f32x4) -> Self {
Self { x, y, z }
}
#[inline]
pub fn new_normalized(x: f32x4, y: f32x4, z: f32x4) -> Self {
Vec3x4::new(x, y, z).normalize().as_unit_vec3x4_unchecked()
}
#[inline]
pub fn select(mask: BVec3x4, if_true: Self, if_false: Self) -> Vec3x4 {
Vec3x4::select(mask, if_true.as_vec3x4(), if_false.as_vec3x4())
}
#[inline]
pub const fn from_array_unchecked(a: [f32x4; 3]) -> Self {
Self::new_unchecked(a[0], a[1], a[2])
}
#[inline]
pub const fn to_array(&self) -> [f32x4; 3] {
[self.x, self.y, self.z]
}
#[inline]
pub const fn from_slice_unchecked(slice: &[f32x4]) -> Self {
Self::new_unchecked(slice[0], slice[1], slice[2])
}
#[inline]
pub fn write_to_slice(self, slice: &mut [f32x4]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
}
#[inline]
pub(crate) fn dot(self, rhs: Self) -> f32x4 {
self.as_vec3x4().dot(rhs.as_vec3x4())
}
#[inline]
pub(crate) fn cross(self, rhs: Self) -> Vec3x4 {
self.as_vec3x4().cross(rhs.as_vec3x4())
}
#[inline]
pub fn min_element(self) -> f32x4 {
self.as_vec3x4().min_element()
}
#[inline]
pub fn max_element(self) -> f32x4 {
self.as_vec3x4().max_element()
}
#[inline]
pub fn cmpeq(self, rhs: Self) -> BVec3x4 {
self.as_vec3x4().cmpeq(rhs.as_vec3x4())
}
#[inline]
pub fn cmpne(self, rhs: Self) -> BVec3x4 {
self.as_vec3x4().cmpne(rhs.as_vec3x4())
}
#[inline]
pub fn cmpge(self, rhs: Self) -> BVec3x4 {
self.as_vec3x4().cmpge(rhs.as_vec3x4())
}
#[inline]
pub fn cmpgt(self, rhs: Self) -> BVec3x4 {
self.as_vec3x4().cmpgt(rhs.as_vec3x4())
}
#[inline]
pub fn cmple(self, rhs: Self) -> BVec3x4 {
self.as_vec3x4().cmple(rhs.as_vec3x4())
}
#[inline]
pub fn cmplt(self, rhs: Self) -> BVec3x4 {
self.as_vec3x4().cmple(rhs.as_vec3x4())
}
#[inline]
pub fn abs(self) -> Self {
self.as_vec3x4().abs().as_unit_vec3x4_unchecked()
}
#[inline]
pub fn signum(self) -> Vec3x4 {
self.as_vec3x4().signum()
}
#[inline]
pub fn distance(self, rhs: Self) -> f32x4 {
self.as_vec3x4().distance(rhs.as_vec3x4())
}
#[inline]
pub fn distance_squared(self, rhs: Self) -> f32x4 {
self.as_vec3x4().distance_squared(rhs.as_vec3x4())
}
#[inline]
pub fn lane_select(cond: bool32x4, if_true: Self, if_false: Self) -> Self {
Self {
x: if_true.x.select(cond, if_false.x),
y: if_true.y.select(cond, if_false.y),
z: if_true.z.select(cond, if_false.z),
}
}
#[inline]
pub fn splat_soa(v: UnitVec3) -> Self {
Self {
x: f32x4::splat(v.x),
y: f32x4::splat(v.y),
z: f32x4::splat(v.z),
}
}
#[inline]
pub fn extract_lane(&self, i: usize) -> UnitVec3 {
UnitVec3::new_unchecked(self.x.extract(i), self.y.extract(i), self.z.extract(i))
}
#[inline]
pub fn replace_lane(&mut self, i: usize, v: UnitVec3) {
let mut nv = self.as_vec3x4();
nv.replace_lane(i, v.as_vec3());
*self = nv.as_unit_vec3x4_unchecked();
}
#[inline]
pub fn split_soa(self) -> [UnitVec3; 4] {
[
self.extract_lane(0),
self.extract_lane(1),
self.extract_lane(2),
self.extract_lane(3),
]
}
#[inline]
pub fn compose_soa(a: &[UnitVec3; 4]) -> UnitVec3x4 {
let mut v = Self::default();
v.replace_lane(0, a[0]);
v.replace_lane(1, a[1]);
v.replace_lane(2, a[2]);
v.replace_lane(3, a[3]);
v
}
#[must_use]
#[inline]
pub fn project_onto(self, rhs: Self) -> Vec3x4 {
rhs.as_vec3x4() * self.dot(rhs)
}
#[must_use]
#[inline]
pub fn reject_from(self, rhs: Self) -> Vec3x4 {
self.as_vec3x4() - self.project_onto(rhs)
}
#[doc(alias = "mix")]
#[inline]
pub fn lerp(self, rhs: Self, s: f32x4) -> Vec3x4 {
self.as_vec3x4().lerp(rhs.as_vec3x4(), s)
}
#[inline]
pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32x4) -> bool32x4 {
Vec3x4::abs_diff_eq(self.as_vec3x4(), rhs.as_vec3x4(), max_abs_diff)
}
#[inline]
pub fn mul_add(self, a: Self, b: Self) -> Vec3x4 {
self.as_vec3x4().mul_add(a.as_vec3x4(), b.as_vec3x4())
}
#[inline]
pub fn angle_between(self, rhs: Self) -> f32x4 {
use crate::FloatEx;
self.dot(rhs).acos_approx()
}
#[inline]
pub fn any_orthogonal_vector(&self) -> Self {
let sign = f32x4::ONE.copysignf(self.z);
let a = f32x4::NEG_ONE / (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 = f32x4::ONE.copysignf(self.z);
let a = f32x4::NEG_ONE / (sign + self.z);
let b = self.x * self.y * a;
(
Self::new_unchecked(
f32x4::ONE + 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 UnitVec3x4 {
#[inline]
fn default() -> Self {
Self::X
}
}
impl Neg for UnitVec3x4 {
type Output = Self;
#[inline]
fn neg(self) -> Self {
self.as_vec3x4().neg().as_unit_vec3x4_unchecked()
}
}
impl_op_ex!(/ |a: &UnitVec3x4, b: &UnitVec3x4| -> Vec3x4 {
a.as_vec3x4() / b.as_vec3x4()
});
impl_op_ex!(/ |a: &UnitVec3x4, b: &f32x4| -> Vec3x4 {
a.as_vec3x4() / b
});
impl_op_ex!(/ |a: &f32x4, b: &UnitVec3x4| -> Vec3x4 {
a / b.as_vec3x4()
});
impl_op_ex_commutative!(/ |a: &UnitVec3x4, b: &Vec3x4| -> Vec3x4 {
a.as_vec3x4() / b
});
impl_op_ex!(*|a: &UnitVec3x4, b: &UnitVec3x4| -> Vec3x4 { a.as_vec3x4() * b.as_vec3x4() });
impl_op_ex!(*|a: &UnitVec3x4, b: &f32x4| -> Vec3x4 { a.as_vec3x4() * b });
impl_op_ex!(*|a: &f32x4, b: &UnitVec3x4| -> Vec3x4 { a * b.as_vec3x4() });
impl_op_ex_commutative!(*|a: &UnitVec3x4, b: &Vec3x4| -> Vec3x4 { a.as_vec3x4() * b });
impl_op_ex!(+ |a: &UnitVec3x4, b: &UnitVec3x4| -> Vec3x4 {
a.as_vec3x4() + b.as_vec3x4()
});
impl_op_ex!(+ |a: &UnitVec3x4, b: &f32x4| -> Vec3x4 {
a.as_vec3x4() + b
});
impl_op_ex!(+ |a: &f32x4, b: &UnitVec3x4| -> Vec3x4 {
a + b.as_vec3x4()
});
impl_op_ex_commutative!(+ |a: &UnitVec3x4, b: &Vec3x4| -> Vec3x4 {
a.as_vec3x4() + b
});
impl_op_ex!(-|a: &UnitVec3x4, b: &UnitVec3x4| -> Vec3x4 { a.as_vec3x4() - b.as_vec3x4() });
impl_op_ex!(-|a: &UnitVec3x4, b: &f32x4| -> Vec3x4 { a.as_vec3x4() - b });
impl_op_ex!(-|a: &f32x4, b: &UnitVec3x4| -> Vec3x4 { a - b.as_vec3x4() });
impl_op_ex_commutative!(-|a: &UnitVec3x4, b: &Vec3x4| -> Vec3x4 { a.as_vec3x4() - b });
impl_op_ex!(% |a: &UnitVec3x4, b: &UnitVec3x4| -> Vec3x4 {
a.as_vec3x4() % b.as_vec3x4()
});
impl_op_ex!(% |a: &UnitVec3x4, b: &f32x4| -> Vec3x4 {
a.as_vec3x4() % b
});
impl_op_ex!(% |a: &f32x4, b: &UnitVec3x4| -> Vec3x4 {
a % b.as_vec3x4()
});
impl_op_ex_commutative!(% |a: &UnitVec3x4, b: &Vec3x4| -> Vec3x4 {
a.as_vec3x4() % b
});
impl Index<usize> for UnitVec3x4 {
type Output = f32x4;
#[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 UnitVec3x4 {
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 UnitVec3x4 {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_tuple(stringify!(UnitVec3x4))
.field(&self.x)
.field(&self.y)
.field(&self.z)
.finish()
}
}
impl From<UnitVec3x4> for [f32x4; 3] {
#[inline]
fn from(v: UnitVec3x4) -> Self {
v.as_vec3x4().into()
}
}
impl From<UnitVec3x4> for (f32x4, f32x4, f32x4) {
#[inline]
fn from(v: UnitVec3x4) -> Self {
v.as_vec3x4().into()
}
}
#[cfg(not(target_arch = "spirv"))]
impl AsRef<[f32x4; 3]> for UnitVec3x4 {
#[inline]
fn as_ref(&self) -> &[f32x4; 3] {
unsafe { &*(self as *const UnitVec3x4 as *const [f32x4; 3]) }
}
}