Skip to content

Commit

Permalink
Merge pull request #21 from rpl-cmu/easton/fac_std_cov
Browse files Browse the repository at this point in the history
Clean up fac! noise as literal
  • Loading branch information
contagon authored Dec 9, 2024
2 parents 6ca8eca + 0525741 commit 99e4f57
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions factrs-proc/src/fac.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/residuals/imu_preint/residual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 99e4f57

Please sign in to comment.