impl dot + cross + length_squared with trait bounds

This commit is contained in:
Quaternions 2024-09-09 16:37:00 -07:00
parent 206e219467
commit 53bb1522eb
2 changed files with 44 additions and 0 deletions

View File

@ -97,6 +97,29 @@ macro_rules! impl_vector {
}
}
impl<const N:usize,T> Vector<N,T>
{
#[inline]
pub fn dot<U,V>(self,rhs:Vector<N,U>)->V
where
T:core::ops::Mul<U,Output=V>,
V:core::iter::Sum,
{
self.array.into_iter().zip(rhs.array).map(|(a,b)|a*b).sum()
}
}
impl<const N:usize,T,V> Vector<N,T>
where
T:core::ops::Mul<Output=V>+Copy,
V:core::iter::Sum,
{
#[inline]
pub fn length_squared(self)->V{
self.array.into_iter().map(|t|t*t).sum()
}
}
// Impl arithmetic operators
$crate::impl_vector_assign_operator!(AddAssign, add_assign );
$crate::impl_vector_operator!(Add, add );
@ -197,6 +220,22 @@ macro_rules! impl_vector_named_fields {
#[macro_export(local_inner_macros)]
macro_rules! impl_vector_3 {
()=>{
impl<T> Vector<3,T>
{
#[inline]
pub fn cross<U,V>(self,rhs:Vector<3,U>)->Vector<3,<V as core::ops::Sub>::Output>
where
T:core::ops::Mul<U,Output=V>+Copy,
U:Copy,
V:core::ops::Sub,
{
Vector::new([
self.y*rhs.z-self.z*rhs.y,
self.z*rhs.x-self.x*rhs.z,
self.x*rhs.y-self.y*rhs.x,
])
}
}
#[cfg(feature="fixed_wide")]
$crate::impl_vector_wide_3!();
}

View File

@ -8,6 +8,11 @@ fn test_bool(){
assert_eq!(Vector3::new([true,true,true]).all(),true);
}
#[test]
fn test_length_squared(){
assert_eq!(Vector3::new([1,2,3]).length_squared(),14);
}
#[test]
fn test_arithmetic(){
let a=Vector3::new([1,2,3]);