nalgebra/src/geometry/dual_quaternion_ops.rs

105 lines
2.3 KiB
Rust
Raw Normal View History

/*
* This file provides:
*
* NOTE: Work in progress https://github.com/dimforge/nalgebra/issues/487
*
* (Dual Quaternion)
*
* Index<usize>
* IndexMut<usize>
*
* (Assignment Operators)
*
* DualQuaternion × Scalar
* DualQuaternion × DualQuaternion
* DualQuaternion + DualQuaternion
* DualQuaternion - DualQuaternion
*
* ---
*
* References:
* Multiplication:
* - https://cs.gmu.edu/~jmlien/teaching/cs451/uploads/Main/dual-quaternion.pdf
*/
2020-12-18 23:38:19 +08:00
use crate::{DualQuaternion, SimdRealField};
2020-12-18 23:09:56 +08:00
use std::mem;
use std::ops::{Add, Index, IndexMut, Mul, Sub};
2020-12-18 23:09:56 +08:00
impl<N: SimdRealField> AsRef<[N; 8]> for DualQuaternion<N> {
#[inline]
fn as_ref(&self) -> &[N; 8] {
unsafe { mem::transmute(self) }
}
}
impl<N: SimdRealField> AsMut<[N; 8]> for DualQuaternion<N> {
#[inline]
fn as_mut(&mut self) -> &mut [N; 8] {
unsafe { mem::transmute(self) }
}
}
impl<N: SimdRealField> Index<usize> for DualQuaternion<N> {
type Output = N;
#[inline]
fn index(&self, i: usize) -> &Self::Output {
2020-12-18 23:09:56 +08:00
&self.as_ref()[i]
}
}
impl<N: SimdRealField> IndexMut<usize> for DualQuaternion<N> {
#[inline]
fn index_mut(&mut self, i: usize) -> &mut N {
2020-12-18 23:09:56 +08:00
&mut self.as_mut()[i]
}
}
impl<N: SimdRealField> Mul<DualQuaternion<N>> for DualQuaternion<N>
where
N::Element: SimdRealField,
{
type Output = DualQuaternion<N>;
fn mul(self, rhs: Self) -> Self::Output {
Self::from_real_and_dual(
2020-12-18 23:09:56 +08:00
self.real * rhs.real,
self.real * rhs.dual + self.dual * rhs.real,
)
}
}
impl<N: SimdRealField> Mul<N> for DualQuaternion<N>
where
2020-12-18 23:38:19 +08:00
N::Element: SimdRealField,
{
type Output = DualQuaternion<N>;
fn mul(self, rhs: N) -> Self::Output {
2020-12-18 23:09:56 +08:00
Self::from_real_and_dual(self.real * rhs, self.dual * rhs)
}
}
impl<N: SimdRealField> Add<DualQuaternion<N>> for DualQuaternion<N>
where
N::Element: SimdRealField,
{
type Output = DualQuaternion<N>;
fn add(self, rhs: DualQuaternion<N>) -> Self::Output {
2020-12-18 23:09:56 +08:00
Self::from_real_and_dual(self.real + rhs.real, self.dual + rhs.dual)
}
}
impl<N: SimdRealField> Sub<DualQuaternion<N>> for DualQuaternion<N>
where
N::Element: SimdRealField,
{
type Output = DualQuaternion<N>;
fn sub(self, rhs: DualQuaternion<N>) -> Self::Output {
2020-12-18 23:09:56 +08:00
Self::from_real_and_dual(self.real - rhs.real, self.dual - rhs.dual)
}
}