use apex_manifolds::LieGroup;
use apex_manifolds::se23::SE23;
use faer::prelude::ReborrowMut;
use nalgebra::Vector3;
use crate::core::variable::ManifoldVariable;
use crate::factors::Factor;
use crate::factors::common::validate::expect_block_sizes;
#[derive(Debug, Clone, Default)]
pub struct ZeroVelocityFactor;
impl ZeroVelocityFactor {
pub fn new() -> Self {
Self
}
}
impl Factor for ZeroVelocityFactor {
fn linearize(
&self,
params: &[&[f64]],
residual: &mut [f64],
jacobian: Option<faer::mat::MatMut<'_, f64>>,
) {
let state = SE23::from_param_slice(params[0]);
let velocity = state.velocity();
residual[..3].copy_from_slice(velocity.as_slice());
let Some(mut jac) = jacobian else { return };
let rotation = state.rotation_matrix();
for row in 0..3 {
for col in 0..9 {
*jac.rb_mut().get_mut(row, col) = 0.0;
}
for col in 0..3 {
*jac.rb_mut().get_mut(row, 6 + col) = rotation[(row, col)];
}
}
}
fn residual_dim(&self) -> usize {
3
}
fn jacobian_shape(&self) -> (usize, usize) {
(3, 9)
}
fn validate_variables(&self, variables: &[&dyn ManifoldVariable]) -> Result<(), String> {
expect_block_sizes(
variables,
&[SE23::REP_SIZE],
"ZeroVelocityFactor expects [SE23 state]",
)
}
}
#[derive(Debug, Clone)]
pub struct ZeroAngularRateFactor {
measured_rate: Vector3<f64>,
}
impl ZeroAngularRateFactor {
pub fn new(measured_rate: Vector3<f64>) -> Self {
Self { measured_rate }
}
}
impl Factor for ZeroAngularRateFactor {
fn linearize(
&self,
params: &[&[f64]],
residual: &mut [f64],
jacobian: Option<faer::mat::MatMut<'_, f64>>,
) {
let bias_gyro = Vector3::new(params[0][0], params[0][1], params[0][2]);
let r = self.measured_rate - bias_gyro;
residual[..3].copy_from_slice(r.as_slice());
let Some(mut jac) = jacobian else { return };
for row in 0..3 {
for col in 0..6 {
*jac.rb_mut().get_mut(row, col) = 0.0;
}
*jac.rb_mut().get_mut(row, row) = -1.0;
}
}
fn residual_dim(&self) -> usize {
3
}
fn jacobian_shape(&self) -> (usize, usize) {
(3, 6)
}
fn validate_variables(&self, variables: &[&dyn ManifoldVariable]) -> Result<(), String> {
expect_block_sizes(variables, &[6], "ZeroAngularRateFactor expects [bias (6D)]")
}
}