use core::ops::Mul;
use num_traits::Zero;
use crate::{Matrix9x9, Matrix9x9Math, Vector3d};
pub type KalmanStateVector9f32 = KalmanStateVector9<f32>;
pub type KalmanStateVector9f64 = KalmanStateVector9<f64>;
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C, align(64))]
pub struct KalmanStateVector9<T> {
pub pos: Vector3d<T>,
pub vel: Vector3d<T>,
pub bias: Vector3d<T>,
}
impl<T> From<(Vector3d<T>, Vector3d<T>, Vector3d<T>)> for KalmanStateVector9<T> {
#[inline]
fn from(v: (Vector3d<T>, Vector3d<T>, Vector3d<T>)) -> Self {
Self { pos: v.0, vel: v.1, bias: v.2 }
}
}
impl Mul<f32> for KalmanStateVector9<f32> {
type Output = Self;
#[inline]
fn mul(self, rhs: f32) -> Self::Output {
Self { pos: self.pos * rhs, vel: self.vel * rhs, bias: self.bias * rhs }
}
}
impl<T> Matrix9x9<T>
where
T: Copy + Zero + Matrix9x9Math + Mul<T, Output = T>,
{
#[inline]
pub fn outer_product(col: KalmanStateVector9<T>, row: KalmanStateVector9<T>) -> Self {
let r1 = [row.pos.x, row.pos.y, row.pos.z, T::zero()];
let r2 = [row.vel.x, row.vel.y, row.vel.z, T::zero()];
let r3 = [row.bias.x, row.bias.y, row.bias.z, T::zero()];
let c = [col.pos.x, col.pos.y, col.pos.z, col.vel.x, col.vel.y, col.vel.z, col.bias.x, col.bias.y, col.bias.z];
let mut ret = Self { a: [T::zero(); 81] };
for (r_idx, &scalar) in c.iter().enumerate().take(9) {
let chunk1 = r1.map(|val| scalar * val);
let chunk2 = r2.map(|val| scalar * val);
let chunk3 = r3.map(|val| scalar * val);
let ret_row = &mut ret[r_idx * 9..(r_idx + 1) * 9];
ret_row[0..3].copy_from_slice(&chunk1[0..3]);
ret_row[3..6].copy_from_slice(&chunk2[0..3]);
ret_row[6..9].copy_from_slice(&chunk3[0..3]);
}
ret
}
}