Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up fac! noise as literal #21

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading