use core::ops::{Add, Neg, Sub};
use crate::linear_algebra::Vector;
use crate::scalar::Numeric;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Twist<T: Numeric> {
linear: Vector<3, T>,
angular: Vector<3, T>,
}
impl<T: Numeric> Twist<T> {
#[inline]
pub fn new(linear: Vector<3, T>, angular: Vector<3, T>) -> Self {
Twist { linear, angular }
}
#[inline]
pub fn zeros() -> Self {
Twist {
linear: Vector::zeros(),
angular: Vector::zeros(),
}
}
#[inline]
pub fn from_array(a: [T; 6]) -> Self {
Twist {
linear: Vector::new([a[0], a[1], a[2]]),
angular: Vector::new([a[3], a[4], a[5]]),
}
}
#[inline]
pub fn as_array(self) -> [T; 6] {
let v = self.linear;
let w = self.angular;
[v[0], v[1], v[2], w[0], w[1], w[2]]
}
#[inline]
pub fn from_vector(v: Vector<6, T>) -> Self {
Self::from_array(v.into_array())
}
#[inline]
pub fn to_vector(self) -> Vector<6, T> {
Vector::new(self.as_array())
}
#[inline]
pub fn linear(self) -> Vector<3, T> {
self.linear
}
#[inline]
pub fn angular(self) -> Vector<3, T> {
self.angular
}
#[inline]
pub fn scale(self, scalar: T) -> Self {
Twist {
linear: self.linear.scale(scalar),
angular: self.angular.scale(scalar),
}
}
}
impl<T: Numeric> Add for Twist<T> {
type Output = Self;
#[inline]
fn add(self, rhs: Self) -> Self {
Twist {
linear: self.linear + rhs.linear,
angular: self.angular + rhs.angular,
}
}
}
impl<T: Numeric> Sub for Twist<T> {
type Output = Self;
#[inline]
fn sub(self, rhs: Self) -> Self {
Twist {
linear: self.linear - rhs.linear,
angular: self.angular - rhs.angular,
}
}
}
impl<T: Numeric> Neg for Twist<T> {
type Output = Self;
#[inline]
fn neg(self) -> Self {
Twist {
linear: -self.linear,
angular: -self.angular,
}
}
}
impl<T: Numeric> From<Vector<6, T>> for Twist<T> {
#[inline]
fn from(v: Vector<6, T>) -> Self {
Self::from_vector(v)
}
}
impl<T: Numeric> From<Twist<T>> for Vector<6, T> {
#[inline]
fn from(t: Twist<T>) -> Self {
t.to_vector()
}
}