use core::ops::{Add, Div, Mul, Sub};
use crate::{
algebra::{Dot, Length, LengthSquared, Lerp, Normalize},
numeric::Sqrt,
vector::Vector,
};
crate::impl_approx_eq_wrapper!(
[T, const N: usize],
impl: Vector<T, N>,
item: T,
field: data,
);
impl<T, const N: usize> Vector<T, N>
where
T: Copy + Mul<Output = T> + Add<Output = T>,
{
#[inline]
pub fn length_squared(self) -> T {
self.into_tuple().length_squared()
}
#[inline]
pub fn length(self) -> T
where
T: Sqrt,
{
self.length_squared().sqrt()
}
#[inline]
pub fn normalize(self) -> Self
where
T: Sqrt + Div<Output = T>,
{
self / self.length()
}
}
impl<T, const N: usize> LengthSquared for Vector<T, N>
where
T: Copy + Mul<Output = T> + Add<Output = T>,
{
type Output = T;
#[inline]
fn length_squared(self) -> Self::Output {
Vector::length_squared(self)
}
}
impl<T, const N: usize> Length for Vector<T, N>
where
T: Copy + Mul<Output = T> + Add<Output = T> + Sqrt,
{
#[inline]
fn length(self) -> Self::Output {
Vector::length(self)
}
}
impl<T, const N: usize> Normalize for Vector<T, N>
where
T: Copy + Mul<Output = T> + Add<Output = T> + Div<Output = T> + Sqrt,
{
#[inline]
fn normalize(self) -> Self {
Vector::normalize(self)
}
}
impl<T, const N: usize> Vector<T, N>
where
T: Copy + Mul<Output = T> + Add<Output = T>,
{
#[inline]
pub fn dot(self, rhs: Self) -> T {
self.into_tuple().dot(rhs.into_tuple())
}
}
impl<T, const N: usize> Dot for Vector<T, N>
where
T: Copy + Mul<Output = T> + Add<Output = T>,
{
type Output = T;
#[inline]
fn dot(self, rhs: Self) -> Self::Output {
Vector::dot(self, rhs)
}
}
impl<T, const N: usize> Vector<T, N>
where
T: Copy + Sub<Output = T> + Mul<Output = T> + Add<Output = T>,
{
#[inline]
fn lerp(self, rhs: Self, t: T) -> Self {
self + (rhs - self) * t
}
}
impl<T, const N: usize> Lerp for Vector<T, N>
where
T: Copy + Sub<Output = T> + Mul<Output = T> + Add<Output = T>,
{
type Scalar = T;
#[inline]
fn lerp(self, rhs: Self, t: Self::Scalar) -> Self {
Vector::lerp(self, rhs, t)
}
}