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