use core::{
fmt::{self, Display, Formatter},
ops::Mul,
};
use nalgebra::Vector6;
use crate::{Real, Vector};
use super::se3;
#[derive(Debug)]
pub struct Vec6<T> {
pub(crate) val: Vector6<T>,
}
impl<T> Display for Vec6<T>
where
T: Real + Display,
{
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.val.fmt(f)
}
}
impl<T> Mul<T> for Vec6<T>
where
T: Real,
{
type Output = Self;
fn mul(self, rhs: T) -> Self::Output {
Self {
val: self.val * rhs,
}
}
}
impl<T> Mul<T> for &Vec6<T>
where
T: Real,
{
type Output = Vec6<T>;
fn mul(self, rhs: T) -> Self::Output {
Vec6 {
val: self.val * rhs,
}
}
}
impl<T> Mul<&T> for Vec6<T>
where
T: Real,
{
type Output = Vec6<T>;
fn mul(self, rhs: &T) -> Self::Output {
Vec6 {
val: self.val * *rhs,
}
}
}
impl<T> Mul<&T> for &Vec6<T>
where
T: Real,
{
type Output = Vec6<T>;
fn mul(self, rhs: &T) -> Self::Output {
Vec6 {
val: self.val * *rhs,
}
}
}
impl<T> Vec6<T>
where
T: Copy,
{
pub fn new(r: [T; 3], p: [T; 3]) -> Self {
Self {
val: Vector6::new(r[0], r[1], r[2], p[0], p[1], p[2]),
}
}
pub fn as_array(&self) -> [T; 6] {
[
self.val[0],
self.val[1],
self.val[2],
self.val[3],
self.val[4],
self.val[5],
]
}
pub fn as_slice(&self) -> &[T] {
self.val.as_slice()
}
pub fn r(&self) -> [T; 3] {
[self.val[0], self.val[1], self.val[2]]
}
pub fn p(&self) -> [T; 3] {
[self.val[3], self.val[4], self.val[5]]
}
}
impl<T> Vector for Vec6<T>
where
T: Copy,
{
type Algebra = se3<T>;
fn hat(&self) -> Self::Algebra {
Self::Algebra { val: self.val }
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_new() {
let r = [1.0, 2.0, 3.0];
let v = [4.0, 5.0, 6.0];
let vec = Vec6::new(r, v);
assert_eq!(vec.val, Vector6::new(1.0, 2.0, 3.0, 4.0, 5.0, 6.0));
}
#[test]
fn test_r() {
let r = [1.0, 2.0, 3.0];
let v = [4.0, 5.0, 6.0];
let vec = Vec6::new(r, v);
assert_eq!(vec.r(), [1.0, 2.0, 3.0]);
}
#[test]
fn test_p() {
let r = [1.0, 2.0, 3.0];
let v = [4.0, 5.0, 6.0];
let vec = Vec6::new(r, v);
assert_eq!(vec.p(), [4.0, 5.0, 6.0]);
}
#[test]
fn test_hat() {
let r = [1.0, 2.0, 3.0];
let v = [4.0, 5.0, 6.0];
let vec = Vec6::new(r, v);
let algebra = vec.hat();
assert_eq!(algebra.val, vec.val);
}
}