use crate::{BVec2, Vec2};
#[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: Vec2,
uv: UnitVec2,
}
impl Vec2 {
#[inline]
pub fn as_unit_vec2_unchecked(self) -> UnitVec2 {
unsafe { VecUnionCast { v: self }.uv }
}
}
impl UnitVec2 {
#[inline]
pub fn as_vec2(self) -> Vec2 {
unsafe { VecUnionCast { uv: self }.v }
}
}
#[derive(Clone, Copy, PartialEq)]
#[cfg_attr(feature = "cuda", repr(align(8)))]
#[cfg_attr(not(target_arch = "spirv"), repr(C))]
#[cfg_attr(target_arch = "spirv", repr(simd))]
pub struct UnitVec2 {
pub x: f32,
pub y: f32,
}
impl UnitVec2 {
pub const X: Self = Self::new_unchecked(1.0_f32, 0.0_f32);
pub const Y: Self = Self::new_unchecked(0.0_f32, 1.0_f32);
pub const NEG_X: Self = Self::new_unchecked(-1.0_f32, 0.0_f32);
pub const NEG_Y: Self = Self::new_unchecked(0.0_f32, -1.0_f32);
pub const AXES: [Self; 2] = [Self::X, Self::Y];
#[inline]
pub const fn new_unchecked(x: f32, y: f32) -> Self {
Self { x, y }
}
#[inline]
pub fn new_normalized(x: f32, y: f32) -> Self {
Vec2::new(x, y).normalize().as_unit_vec2_unchecked()
}
#[inline]
pub fn select(mask: BVec2, if_true: Self, if_false: Self) -> Vec2 {
Vec2::select(mask, if_true.as_vec2(), if_false.as_vec2())
}
#[inline]
pub const fn from_array_unchecked(a: [f32; 2]) -> Self {
Self::new_unchecked(a[0], a[1])
}
#[inline]
pub const fn to_array(&self) -> [f32; 2] {
[self.x, self.y]
}
#[inline]
pub const fn from_slice_unchecked(slice: &[f32]) -> Self {
Self::new_unchecked(slice[0], slice[1])
}
#[inline]
pub fn write_to_slice(self, slice: &mut [f32]) {
slice[0] = self.x;
slice[1] = self.y;
}
#[inline]
pub(crate) fn dot(self, rhs: Self) -> f32 {
self.as_vec2().dot(rhs.as_vec2())
}
#[inline]
pub fn min_element(self) -> f32 {
self.as_vec2().min_element()
}
#[inline]
pub fn max_element(self) -> f32 {
self.as_vec2().max_element()
}
#[inline]
pub fn cmpeq(self, rhs: Self) -> BVec2 {
self.as_vec2().cmpeq(rhs.as_vec2())
}
#[inline]
pub fn cmpne(self, rhs: Self) -> BVec2 {
self.as_vec2().cmpne(rhs.as_vec2())
}
#[inline]
pub fn cmpge(self, rhs: Self) -> BVec2 {
self.as_vec2().cmpge(rhs.as_vec2())
}
#[inline]
pub fn cmpgt(self, rhs: Self) -> BVec2 {
self.as_vec2().cmpgt(rhs.as_vec2())
}
#[inline]
pub fn cmple(self, rhs: Self) -> BVec2 {
self.as_vec2().cmple(rhs.as_vec2())
}
#[inline]
pub fn cmplt(self, rhs: Self) -> BVec2 {
self.as_vec2().cmple(rhs.as_vec2())
}
#[inline]
pub fn abs(self) -> Self {
self.as_vec2().abs().as_unit_vec2_unchecked()
}
#[inline]
pub fn signum(self) -> Vec2 {
self.as_vec2().signum()
}
#[inline]
pub fn distance(self, rhs: Self) -> f32 {
self.as_vec2().distance(rhs.as_vec2())
}
#[inline]
pub fn distance_squared(self, rhs: Self) -> f32 {
self.as_vec2().distance_squared(rhs.as_vec2())
}
#[must_use]
#[inline]
pub fn project_onto(self, rhs: Self) -> Vec2 {
rhs.as_vec2() * self.dot(rhs)
}
#[must_use]
#[inline]
pub fn reject_from(self, rhs: Self) -> Vec2 {
self.as_vec2() - self.project_onto(rhs)
}
#[doc(alias = "mix")]
#[inline]
pub fn lerp(self, rhs: Self, s: f32) -> Vec2 {
self.as_vec2().lerp(rhs.as_vec2(), s)
}
#[inline]
pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
Vec2::abs_diff_eq(self.as_vec2(), rhs.as_vec2(), max_abs_diff)
}
#[inline]
pub fn mul_add(self, a: Self, b: Self) -> Vec2 {
self.as_vec2().mul_add(a.as_vec2(), b.as_vec2())
}
#[inline]
pub fn from_angle(angle: f32) -> Self {
Vec2::from_angle(angle).as_unit_vec2_unchecked()
}
#[inline]
pub fn angle_between(self, rhs: Self) -> f32 {
use crate::FloatEx;
let angle = self.dot(rhs).acos_approx();
angle * self.perp_dot(rhs).signumf()
}
#[inline]
pub fn perp(self) -> Self {
self.as_vec2().perp().as_unit_vec2_unchecked()
}
#[doc(alias = "wedge")]
#[doc(alias = "cross")]
#[doc(alias = "determinant")]
#[inline]
pub fn perp_dot(self, rhs: Self) -> f32 {
self.as_vec2().perp_dot(rhs.as_vec2())
}
#[must_use]
#[inline]
pub fn rotate(self, rhs: Self) -> Self {
self.as_vec2()
.rotate(rhs.as_vec2())
.as_unit_vec2_unchecked()
}
}
impl Default for UnitVec2 {
#[inline]
fn default() -> Self {
Self::X
}
}
impl Neg for UnitVec2 {
type Output = Self;
#[inline]
fn neg(self) -> Self {
self.as_vec2().neg().as_unit_vec2_unchecked()
}
}
impl_op_ex!(/ |a: &UnitVec2, b: &UnitVec2| -> Vec2 {
a.as_vec2() / b.as_vec2()
});
impl_op_ex!(/ |a: &UnitVec2, b: &f32| -> Vec2 {
a.as_vec2() / b
});
impl_op_ex!(/ |a: &f32, b: &UnitVec2| -> Vec2 {
a / b.as_vec2()
});
impl_op_ex_commutative!(/ |a: &UnitVec2, b: &Vec2| -> Vec2 {
a.as_vec2() / b
});
impl_op_ex!(*|a: &UnitVec2, b: &UnitVec2| -> Vec2 { a.as_vec2() * b.as_vec2() });
impl_op_ex!(*|a: &UnitVec2, b: &f32| -> Vec2 { a.as_vec2() * b });
impl_op_ex!(*|a: &f32, b: &UnitVec2| -> Vec2 { a * b.as_vec2() });
impl_op_ex_commutative!(*|a: &UnitVec2, b: &Vec2| -> Vec2 { a.as_vec2() * b });
impl_op_ex!(+ |a: &UnitVec2, b: &UnitVec2| -> Vec2 {
a.as_vec2() + b.as_vec2()
});
impl_op_ex!(+ |a: &UnitVec2, b: &f32| -> Vec2 {
a.as_vec2() + b
});
impl_op_ex!(+ |a: &f32, b: &UnitVec2| -> Vec2 {
a + b.as_vec2()
});
impl_op_ex_commutative!(+ |a: &UnitVec2, b: &Vec2| -> Vec2 {
a.as_vec2() + b
});
impl_op_ex!(-|a: &UnitVec2, b: &UnitVec2| -> Vec2 { a.as_vec2() - b.as_vec2() });
impl_op_ex!(-|a: &UnitVec2, b: &f32| -> Vec2 { a.as_vec2() - b });
impl_op_ex!(-|a: &f32, b: &UnitVec2| -> Vec2 { a - b.as_vec2() });
impl_op_ex_commutative!(-|a: &UnitVec2, b: &Vec2| -> Vec2 { a.as_vec2() - b });
impl_op_ex!(% |a: &UnitVec2, b: &UnitVec2| -> Vec2 {
a.as_vec2() % b.as_vec2()
});
impl_op_ex!(% |a: &UnitVec2, b: &f32| -> Vec2 {
a.as_vec2() % b
});
impl_op_ex!(% |a: &f32, b: &UnitVec2| -> Vec2 {
a % b.as_vec2()
});
impl_op_ex_commutative!(% |a: &UnitVec2, b: &Vec2| -> Vec2 {
a.as_vec2() % b
});
impl Index<usize> for UnitVec2 {
type Output = f32;
#[inline]
fn index(&self, index: usize) -> &Self::Output {
match index {
0 => &self.x,
1 => &self.y,
_ => panic!("index out of bounds"),
}
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for UnitVec2 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}, {}]", self.x, self.y)
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for UnitVec2 {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_tuple(stringify!(UnitVec2))
.field(&self.x)
.field(&self.y)
.finish()
}
}
impl From<UnitVec2> for [f32; 2] {
#[inline]
fn from(v: UnitVec2) -> Self {
v.as_vec2().into()
}
}
impl From<UnitVec2> for (f32, f32) {
#[inline]
fn from(v: UnitVec2) -> Self {
v.as_vec2().into()
}
}
#[cfg(not(target_arch = "spirv"))]
impl AsRef<[f32; 2]> for UnitVec2 {
#[inline]
fn as_ref(&self) -> &[f32; 2] {
unsafe { &*(self as *const UnitVec2 as *const [f32; 2]) }
}
}