use crate::{BVec3, Vec3};
#[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: Vec3,
uv: UnitVec3,
}
impl Vec3 {
#[inline]
pub fn as_unit_vec3_unchecked(self) -> UnitVec3 {
unsafe { VecUnionCast { v: self }.uv }
}
}
impl UnitVec3 {
#[inline]
pub fn as_vec3(self) -> Vec3 {
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 UnitVec3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
impl UnitVec3 {
pub const X: Self = Self::new_unchecked(1.0_f32, 0.0_f32, 0.0_f32);
pub const Y: Self = Self::new_unchecked(0.0_f32, 1.0_f32, 0.0_f32);
pub const Z: Self = Self::new_unchecked(0.0_f32, 0.0_f32, 1.0_f32);
pub const NEG_X: Self = Self::new_unchecked(-1.0_f32, 0.0_f32, 0.0_f32);
pub const NEG_Y: Self = Self::new_unchecked(0.0_f32, -1.0_f32, 0.0_f32);
pub const NEG_Z: Self = Self::new_unchecked(0.0_f32, 0.0_f32, -1.0_f32);
pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z];
#[inline]
pub const fn new_unchecked(x: f32, y: f32, z: f32) -> Self {
Self { x, y, z }
}
#[inline]
pub fn new_normalized(x: f32, y: f32, z: f32) -> Self {
Vec3::new(x, y, z).normalize().as_unit_vec3_unchecked()
}
#[inline]
pub fn select(mask: BVec3, if_true: Self, if_false: Self) -> Vec3 {
Vec3::select(mask, if_true.as_vec3(), if_false.as_vec3())
}
#[inline]
pub const fn from_array_unchecked(a: [f32; 3]) -> Self {
Self::new_unchecked(a[0], a[1], a[2])
}
#[inline]
pub const fn to_array(&self) -> [f32; 3] {
[self.x, self.y, self.z]
}
#[inline]
pub const fn from_slice_unchecked(slice: &[f32]) -> Self {
Self::new_unchecked(slice[0], slice[1], slice[2])
}
#[inline]
pub fn write_to_slice(self, slice: &mut [f32]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
}
#[inline]
pub(crate) fn dot(self, rhs: Self) -> f32 {
self.as_vec3().dot(rhs.as_vec3())
}
#[inline]
pub(crate) fn cross(self, rhs: Self) -> Vec3 {
self.as_vec3().cross(rhs.as_vec3())
}
#[inline]
pub fn min_element(self) -> f32 {
self.as_vec3().min_element()
}
#[inline]
pub fn max_element(self) -> f32 {
self.as_vec3().max_element()
}
#[inline]
pub fn cmpeq(self, rhs: Self) -> BVec3 {
self.as_vec3().cmpeq(rhs.as_vec3())
}
#[inline]
pub fn cmpne(self, rhs: Self) -> BVec3 {
self.as_vec3().cmpne(rhs.as_vec3())
}
#[inline]
pub fn cmpge(self, rhs: Self) -> BVec3 {
self.as_vec3().cmpge(rhs.as_vec3())
}
#[inline]
pub fn cmpgt(self, rhs: Self) -> BVec3 {
self.as_vec3().cmpgt(rhs.as_vec3())
}
#[inline]
pub fn cmple(self, rhs: Self) -> BVec3 {
self.as_vec3().cmple(rhs.as_vec3())
}
#[inline]
pub fn cmplt(self, rhs: Self) -> BVec3 {
self.as_vec3().cmple(rhs.as_vec3())
}
#[inline]
pub fn abs(self) -> Self {
self.as_vec3().abs().as_unit_vec3_unchecked()
}
#[inline]
pub fn signum(self) -> Vec3 {
self.as_vec3().signum()
}
#[inline]
pub fn distance(self, rhs: Self) -> f32 {
self.as_vec3().distance(rhs.as_vec3())
}
#[inline]
pub fn distance_squared(self, rhs: Self) -> f32 {
self.as_vec3().distance_squared(rhs.as_vec3())
}
#[must_use]
#[inline]
pub fn project_onto(self, rhs: Self) -> Vec3 {
rhs.as_vec3() * self.dot(rhs)
}
#[must_use]
#[inline]
pub fn reject_from(self, rhs: Self) -> Vec3 {
self.as_vec3() - self.project_onto(rhs)
}
#[doc(alias = "mix")]
#[inline]
pub fn lerp(self, rhs: Self, s: f32) -> Vec3 {
self.as_vec3().lerp(rhs.as_vec3(), s)
}
#[inline]
pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
Vec3::abs_diff_eq(self.as_vec3(), rhs.as_vec3(), max_abs_diff)
}
#[inline]
pub fn mul_add(self, a: Self, b: Self) -> Vec3 {
self.as_vec3().mul_add(a.as_vec3(), b.as_vec3())
}
#[inline]
pub fn angle_between(self, rhs: Self) -> f32 {
use crate::FloatEx;
self.dot(rhs).acos_approx()
}
#[inline]
pub fn any_orthogonal_vector(&self) -> Self {
let sign = 1.0_f32.copysignf(self.z);
let a = -1.0_f32 / (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_f32.copysignf(self.z);
let a = -1.0_f32 / (sign + self.z);
let b = self.x * self.y * a;
(
Self::new_unchecked(
1.0_f32 + 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 UnitVec3 {
#[inline]
fn default() -> Self {
Self::X
}
}
impl Neg for UnitVec3 {
type Output = Self;
#[inline]
fn neg(self) -> Self {
self.as_vec3().neg().as_unit_vec3_unchecked()
}
}
impl_op_ex!(/ |a: &UnitVec3, b: &UnitVec3| -> Vec3 {
a.as_vec3() / b.as_vec3()
});
impl_op_ex!(/ |a: &UnitVec3, b: &f32| -> Vec3 {
a.as_vec3() / b
});
impl_op_ex!(/ |a: &f32, b: &UnitVec3| -> Vec3 {
a / b.as_vec3()
});
impl_op_ex_commutative!(/ |a: &UnitVec3, b: &Vec3| -> Vec3 {
a.as_vec3() / b
});
impl_op_ex!(*|a: &UnitVec3, b: &UnitVec3| -> Vec3 { a.as_vec3() * b.as_vec3() });
impl_op_ex!(*|a: &UnitVec3, b: &f32| -> Vec3 { a.as_vec3() * b });
impl_op_ex!(*|a: &f32, b: &UnitVec3| -> Vec3 { a * b.as_vec3() });
impl_op_ex_commutative!(*|a: &UnitVec3, b: &Vec3| -> Vec3 { a.as_vec3() * b });
impl_op_ex!(+ |a: &UnitVec3, b: &UnitVec3| -> Vec3 {
a.as_vec3() + b.as_vec3()
});
impl_op_ex!(+ |a: &UnitVec3, b: &f32| -> Vec3 {
a.as_vec3() + b
});
impl_op_ex!(+ |a: &f32, b: &UnitVec3| -> Vec3 {
a + b.as_vec3()
});
impl_op_ex_commutative!(+ |a: &UnitVec3, b: &Vec3| -> Vec3 {
a.as_vec3() + b
});
impl_op_ex!(-|a: &UnitVec3, b: &UnitVec3| -> Vec3 { a.as_vec3() - b.as_vec3() });
impl_op_ex!(-|a: &UnitVec3, b: &f32| -> Vec3 { a.as_vec3() - b });
impl_op_ex!(-|a: &f32, b: &UnitVec3| -> Vec3 { a - b.as_vec3() });
impl_op_ex_commutative!(-|a: &UnitVec3, b: &Vec3| -> Vec3 { a.as_vec3() - b });
impl_op_ex!(% |a: &UnitVec3, b: &UnitVec3| -> Vec3 {
a.as_vec3() % b.as_vec3()
});
impl_op_ex!(% |a: &UnitVec3, b: &f32| -> Vec3 {
a.as_vec3() % b
});
impl_op_ex!(% |a: &f32, b: &UnitVec3| -> Vec3 {
a % b.as_vec3()
});
impl_op_ex_commutative!(% |a: &UnitVec3, b: &Vec3| -> Vec3 {
a.as_vec3() % b
});
impl Index<usize> for UnitVec3 {
type Output = f32;
#[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 UnitVec3 {
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 UnitVec3 {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_tuple(stringify!(UnitVec3))
.field(&self.x)
.field(&self.y)
.field(&self.z)
.finish()
}
}
impl From<UnitVec3> for [f32; 3] {
#[inline]
fn from(v: UnitVec3) -> Self {
v.as_vec3().into()
}
}
impl From<UnitVec3> for (f32, f32, f32) {
#[inline]
fn from(v: UnitVec3) -> Self {
v.as_vec3().into()
}
}
#[cfg(not(target_arch = "spirv"))]
impl AsRef<[f32; 3]> for UnitVec3 {
#[inline]
fn as_ref(&self) -> &[f32; 3] {
unsafe { &*(self as *const UnitVec3 as *const [f32; 3]) }
}
}