From 052574178c357f74161332eb4a1f00c353a17ae2 Mon Sep 17 00:00:00 2001 From: Easton Potokar Date: Mon, 9 Dec 2024 10:50:51 -0500 Subject: [PATCH] Clean up fac! noise as literal --- README.md | 4 ++-- factrs-proc/src/fac.rs | 12 ++++++++++-- src/residuals/imu_preint/residual.rs | 6 +++--- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e56e391..43aee6c 100644 --- a/README.md +++ b/README.md @@ -53,10 +53,10 @@ let factor = fac![res, X(0)]; graph.add_factor(factor); let res = BetweenResidual::new(y.minus(&x)); -let noise = GaussianNoise::from_scalar_sigma(0.1); let robust = Huber::default(); -let factor = fac![res, (X(0), X(1)), noise, robust]; +let factor = fac![res, (X(0), X(1)), 0.1 as std, robust]; // The same as above, but verbose +// let noise = GaussianNoise::from_scalar_sigma(0.1); // let factor = FactorBuilder::new2(res, X(0), X(1)) // .noise(noise) // .robust(robust) diff --git a/factrs-proc/src/fac.rs b/factrs-proc/src/fac.rs index d89d899..237b60f 100644 --- a/factrs-proc/src/fac.rs +++ b/factrs-proc/src/fac.rs @@ -1,7 +1,9 @@ use proc_macro2::Span; use proc_macro2::TokenStream as TokenStream2; use quote::quote; +use quote::ToTokens; use syn::parse_quote; +use syn::ExprCast; use syn::{parse::Parse, punctuated::Punctuated, Expr, Ident, Token}; pub struct Factor { @@ -76,10 +78,16 @@ impl Parse for Factor { // Then the noise let noise = if input.len() >= 3 { + let m = quote!(factrs::noise); match &input[2] { - Expr::Lit(l) => { - Some(parse_quote!(factrs::noise::GaussianNoise::from_scalar_sigma(#l))) + Expr::Cast(ExprCast { expr, ty, .. }) => { + match ty.to_token_stream().to_string().as_str() { + "std" => Some(parse_quote!(#m::GaussianNoise::from_scalar_sigma(#expr))), + "cov" => Some(parse_quote!(#m::GaussianNoise::from_scalar_cov(#expr))), + _ => return Err(syn::Error::new_spanned(ty, "Unknown cast for noise")), + } } + Expr::Infer(_) => Some(parse_quote!(#m::UnitNoise)), _ => Some(input[2].clone()), } } else { diff --git a/src/residuals/imu_preint/residual.rs b/src/residuals/imu_preint/residual.rs index 8382f1d..f1ffa41 100644 --- a/src/residuals/imu_preint/residual.rs +++ b/src/residuals/imu_preint/residual.rs @@ -396,9 +396,9 @@ mod test { // Build factor and graph let mut graph = Graph::new(); let factor = preint.build(X(0), V(0), B(0), X(1), V(1), B(1)); - let prior_x0 = fac!(PriorResidual::new(x0.clone()), X(0), 1e-3); - let prior_v0 = fac!(PriorResidual::new(v0.clone()), V(0), 1e-3); - let prior_b0 = fac!(PriorResidual::new(b0.clone()), B(0), 1e-3); + let prior_x0 = fac!(PriorResidual::new(x0.clone()), X(0), 1e-3 as cov); + let prior_v0 = fac!(PriorResidual::new(v0.clone()), V(0), 1e-3 as cov); + let prior_b0 = fac!(PriorResidual::new(b0.clone()), B(0), 1e-3 as cov); graph.add_factor(factor); graph.add_factor(prior_x0); graph.add_factor(prior_v0);