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

Imu Preintegration #10

Merged
merged 14 commits into from
Oct 4, 2024
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ We recommend you checkout the [docs](https://docs.rs/factrs/latest/factrs/) (WIP
## Example

```rust
use factrs::prelude::*;
use factrs::{
assign_symbols,
containers::{FactorBuilder, Graph, Values},
noise::GaussianNoise,
optimizers::GaussNewton,
residuals::{BetweenResidual, PriorResidual},
robust::Huber,
traits::*,
variables::SO2,
};

// Assign symbols to variable types
assign_symbols!(X: SO2);
Expand Down
2 changes: 1 addition & 1 deletion examples/g2o-rerun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use factrs::{
use rerun::{Arrows2D, Arrows3D, Points2D, Points3D};

fn main() -> Result<(), Box<dyn std::error::Error>> {
// ------------------------- Parse Arguments & Load data ------------------------- //
// ---------------------- Parse Arguments & Load data ---------------------- //
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
println!("Usage: {} <g2o file> <points/arrows>", args[0]);
Expand Down
3 changes: 1 addition & 2 deletions examples/serde.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use factrs::{
containers::{Graph, Values, X},
containers::{FactorBuilder, Graph, Values, X},
factors::Factor,
noise::GaussianNoise,
prelude::FactorBuilder,
residuals::{BetweenResidual, PriorResidual},
robust::{GemanMcClure, L2},
variables::{SE2, SO2},
Expand Down
10 changes: 9 additions & 1 deletion src/containers/factor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ use crate::{
/// [LinearFactor].
///
/// ```
/// # use factrs::prelude::*;
/// # use factrs::{
/// assign_symbols,
/// containers::FactorBuilder,
/// noise::GaussianNoise,
/// optimizers::GaussNewton,
/// residuals::{PriorResidual},
/// robust::GemanMcClure,
/// variables::VectorVar3,
/// };
/// # assign_symbols!(X: VectorVar3);
/// let prior = VectorVar3::new(1.0, 2.0, 3.0);
/// let residual = PriorResidual::new(prior);
Expand Down
9 changes: 8 additions & 1 deletion src/containers/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ use crate::{containers::Factor, dtype, linear::LinearGraph};
/// solved iteratively.
///
/// ```
/// # use factrs::prelude::*;
/// # use factrs::{
/// assign_symbols,
/// containers::{Graph, FactorBuilder},
/// residuals::PriorResidual,
/// robust::GemanMcClure,
/// traits::*,
/// variables::SO2,
/// };
/// # assign_symbols!(X: SO2);
/// # let factor = FactorBuilder::new1(PriorResidual::new(SO2::identity()), X(0)).build();
/// let mut graph = Graph::new();
Expand Down
4 changes: 2 additions & 2 deletions src/containers/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
mem::size_of,
};

use crate::prelude::VariableUmbrella;
use crate::variables::VariableUmbrella;

// Char is stored in last CHR_BITS
// Value is stored in the first IDX_BITS
Expand Down Expand Up @@ -83,7 +83,7 @@ pub trait TypedSymbol<V: VariableUmbrella>: Symbol {}
/// with the type they will be used with. This macro will create a new symbol
/// and implement all the necessary traits for it to be used as a symbol.
/// ```
/// use factrs::prelude::*;
/// use factrs::{assign_symbols, variables::{SO2, SE2}};
/// assign_symbols!(X: SO2; Y: SE2);
/// ```
#[macro_export]
Expand Down
24 changes: 18 additions & 6 deletions src/containers/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use std::{collections::hash_map::Entry, default::Default, fmt, iter::IntoIterato

use ahash::AHashMap;

use super::{Key, Symbol, TypedSymbol};
use super::{DefaultSymbol, Key, Symbol, TypedSymbol};
use crate::{
linear::LinearValues,
prelude::{DefaultSymbol, VariableUmbrella},
variables::VariableSafe,
variables::{VariableSafe, VariableUmbrella},
};

// Since we won't be passing dual numbers through any of this,
Expand All @@ -19,7 +18,11 @@ use crate::{
/// Values, it must implement [Variable](crate::variables::Variable), and then
/// will implement [VariableSafe] via a blanket implementation.
/// ```
/// # use factrs::prelude::*;
/// # use factrs::{
/// assign_symbols,
/// containers::Values,
/// variables::SO2,
/// };
/// # assign_symbols!(X: SO2);
/// let x = SO2::from_theta(0.1);
/// let mut values = Values::new();
Expand Down Expand Up @@ -81,7 +84,11 @@ impl Values {
/// symbol and as such is guaranteed to return the correct type. Returns
/// None if key isn't found.
/// ```
/// # use factrs::prelude::*;
/// # use factrs::{
/// assign_symbols,
/// containers::Values,
/// variables::SO2,
/// };
/// # assign_symbols!(X: SO2);
/// # let x = SO2::from_theta(0.1);
/// # let mut values = Values::new();
Expand Down Expand Up @@ -150,7 +157,12 @@ impl Values {
/// the values.
///
/// ```
/// # use factrs::prelude::*;
/// # use factrs::{
/// assign_symbols,
/// containers::Values,
/// traits::*,
/// variables::SO2,
/// };
/// # assign_symbols!(X: SO2);
/// # let mut values = Values::new();
/// # (0..10).for_each(|i| {values.insert(X(0), SO2::identity());} );
Expand Down
36 changes: 21 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@
//!
//! # Example
//! ```
//! use factrs::prelude::*;
//! use factrs::{
//! assign_symbols,
//! containers::{FactorBuilder, Graph, Values},
//! noise::GaussianNoise,
//! optimizers::GaussNewton,
//! residuals::{BetweenResidual, PriorResidual},
//! robust::Huber,
//! traits::*,
//! variables::SO2,
//! };
//!
//! // Assign symbols to variable types
//! assign_symbols!(X: SO2);
Expand Down Expand Up @@ -89,34 +98,31 @@ pub mod robust;
pub mod utils;
pub mod variables;

/// Untagged symnbols if `unchecked` API is desired.
/// Untagged symbols if `unchecked` API is desired.
///
/// We strongly recommend using [assign_symbols](crate::assign_symbols) to
/// create and tag symbols with the appropriate types. However, we provide a
/// number of pre-defined symbols if desired. Note this objects can't be tagged
/// number of pre-defined symbols if desired. Note these objects can't be tagged
/// due to the orphan rules.
pub mod symbols {
crate::assign_symbols!(
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
);
}

/// Helper module to import common types
/// Helper module to import common traits
///
/// This module is meant to be glob imported to make it easier to use the
/// library.
/// This module is meant to be glob imported to make it easier to use the traits
/// and functionality in the library.
/// ```
/// use factrs::prelude::*;
/// use factrs::traits::*;
/// ```
pub mod prelude {
pub mod traits {
pub use crate::{
assign_symbols,
containers::*,
noise::*,
optimizers::*,
residuals::*,
robust::*,
variables::*,
linalg::{Diff, DualConvert},
optimizers::{GraphOptimizer, Optimizer},
residuals::Residual,
variables::Variable,
};
}

Expand Down
15 changes: 14 additions & 1 deletion src/linalg/dual.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use nalgebra::{allocator::Allocator, Const};

use super::{Dim, RealField};
use super::{Dim, Matrix, RealField};
use crate::dtype;

/// Wrapper for all properties needed for dual numbers
Expand Down Expand Up @@ -28,3 +28,16 @@ impl<
> DualAllocator<N> for T
{
}

// TODO: Expand on this instead of including in Variable??
pub trait DualConvert {
type Alias<D: Numeric>;
fn dual_convert<D: Numeric>(other: &Self::Alias<dtype>) -> Self::Alias<D>;
}

impl<const R: usize, const C: usize, T: Numeric> DualConvert for Matrix<R, C, T> {
type Alias<D: Numeric> = Matrix<R, C, D>;
fn dual_convert<D: Numeric>(other: &Self::Alias<dtype>) -> Self::Alias<D> {
other.map(|x| x.into())
}
}
6 changes: 5 additions & 1 deletion src/linalg/forward_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ use crate::{
/// inputs and with vector-valued outputs.
///
/// ```
/// use factrs::{linalg::*, prelude::*};
/// use factrs::{
/// linalg::{vectorx, Const, DiffResult, ForwardProp, Numeric, VectorX},
/// traits::*,
/// variables::SO2,
/// };
///
/// fn f<D: Numeric>(x: SO2<D>, y: SO2<D>) -> VectorX<D> {
/// x.ominus(&y)
Expand Down
2 changes: 1 addition & 1 deletion src/linalg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use crate::dtype;

mod dual;
pub use dual::{DualAllocator, DualScalar, DualVector, Numeric};
pub use dual::{DualAllocator, DualConvert, DualScalar, DualVector, Numeric};
// Dual numbers
pub use num_dual::Derivative;

Expand Down
6 changes: 5 additions & 1 deletion src/linalg/numerical_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ use crate::{
/// inputs and with vector-valued outputs.
///
/// ```
/// use factrs::{linalg::*, prelude::*};
/// use factrs::{
/// linalg::{vectorx, DiffResult, NumericalDiff, VectorX},
/// traits::*,
/// variables::SO2,
/// };
///
/// // We can also be generic over Numeric as in [ForwardProp] as well if desired
/// fn f(x: SO2, y: SO2) -> VectorX {
Expand Down
3 changes: 3 additions & 0 deletions src/noise/gaussian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ tag_noise!(
GaussianNoise<10>,
GaussianNoise<11>,
GaussianNoise<12>,
GaussianNoise<13>,
GaussianNoise<14>,
GaussianNoise<15>,
);

/// A Gaussian noise model.
Expand Down
3 changes: 3 additions & 0 deletions src/noise/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ tag_noise!(
UnitNoise<10>,
UnitNoise<11>,
UnitNoise<12>,
UnitNoise<13>,
UnitNoise<14>,
UnitNoise<15>,
);

/// A unit noise model.
Expand Down
3 changes: 1 addition & 2 deletions src/optimizers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ pub mod test {

use super::*;
use crate::{
containers::{Graph, Values},
containers::{FactorBuilder, Graph, Values},
dtype,
linalg::{AllocatorBuffer, Const, DualAllocator, DualVector, VectorX},
noise::{NoiseModelSafe, UnitNoise},
prelude::FactorBuilder,
residuals::{BetweenResidual, PriorResidual, Residual, ResidualSafe},
symbols::X,
variables::VariableUmbrella,
Expand Down
Empty file.
Loading