Struct Graph

Source
pub struct Graph { /* private fields */ }
Expand description

Structure to represent a nonlinear factor graph

Main usage will be via add_factor to add new factors to the graph. Also of note is the linearize function that returns a linear (aka Gaussian) factor graph.

Since the graph represents a nonlinear least-squares problem, during optimization it will be iteratively linearized about a set of variables and solved iteratively.

   assign_symbols,
   containers::{Graph, FactorBuilder},
   residuals::PriorResidual,
   robust::GemanMcClure,
   traits::*,
   variables::SO2,
};
let mut graph = Graph::new();
graph.add_factor(factor);

Implementations§

Source§

impl Graph

Source

pub fn new() -> Self

Examples found in repository?
examples/readme.rs (line 21)
11fn main() {
12    // Make all the values
13    let mut values = Values::new();
14
15    let x = SO2::from_theta(1.0);
16    let y = SO2::from_theta(2.0);
17    values.insert(X(0), SO2::identity());
18    values.insert(X(1), SO2::identity());
19
20    // Make the factors & insert into graph
21    let mut graph = Graph::new();
22    let res = PriorResidual::new(x.clone());
23    let factor = fac![res, X(0)];
24    graph.add_factor(factor);
25
26    let res = BetweenResidual::new(y.minus(&x));
27    let factor = fac![res, (X(0), X(1)), 0.1 as std, Huber::default()];
28    graph.add_factor(factor);
29
30    // Optimize!
31    let mut opt: GaussNewton = GaussNewton::new(graph);
32    let result = opt.optimize(values).unwrap();
33    println!("Results {:#}", result);
34}
More examples
Hide additional examples
examples/serde.rs (line 31)
7fn main() {
8    // ------------------------- Serialize values ------------------------- //
9    let x = SO2::from_theta(0.6);
10    let y = SE2::new(1.0, 2.0, 0.3);
11    let mut values = Values::new();
12    values.insert(X(0), x.clone());
13    values.insert(Y(1), y.clone());
14
15    println!("------ Serializing Values ------");
16
17    let serialized = serde_json::to_string_pretty(&values).unwrap();
18    println!("serialized = {}", serialized);
19
20    // Convert the JSON string back to a Point.
21    let deserialized: Values = serde_json::from_str(&serialized).unwrap();
22    println!("deserialized = {:#}", deserialized);
23
24    // ------------------------- Serialize graph ------------------------- //
25    let prior = PriorResidual::new(x);
26    let bet = BetweenResidual::new(y);
27
28    let prior = fac![prior, X(0), 0.1 as cov, GemanMcClure::default()];
29    let bet = fac![bet, (Y(0), Y(1)), 10.0 as cov];
30
31    let mut graph = Graph::new();
32    graph.add_factor(prior);
33    graph.add_factor(bet);
34
35    println!("\n\n------ Serializing Graph ------");
36
37    let serialized = serde_json::to_string_pretty(&graph).unwrap();
38    println!("serialized = {}", serialized);
39
40    let deserialized: Graph = serde_json::from_str(&serialized).unwrap();
41    println!("deserialized = {:#?}", deserialized);
42}
examples/gps.rs (line 88)
87fn main() {
88    let mut graph = Graph::new();
89
90    // Add odometry factors
91    let res = BetweenResidual::new(SE2::new(0.0, 2.0, 0.0));
92    let odometry_01 = fac![res.clone(), (X(0), X(1)), (0.1, 0.2) as cov];
93    let odometry_12 = fac![res, (X(1), X(2)), (0.1, 0.2) as cov];
94    graph.add_factor(odometry_01);
95    graph.add_factor(odometry_12);
96
97    // Add gps factors
98    let g0 = fac![GpsResidual::new(0.0, 0.0), X(0), 1.0 as std];
99    let g1 = fac![GpsResidual::new(2.0, 0.0), X(1), 1.0 as std];
100    let g2 = fac![GpsResidual::new(4.0, 0.0), X(2), 1.0 as std];
101    graph.add_factor(g0);
102    graph.add_factor(g1);
103    graph.add_factor(g2);
104
105    // Make values
106    let mut values = Values::new();
107    values.insert(X(0), SE2::new(1.0, 2.0, 3.0));
108    values.insert(X(1), SE2::identity());
109    values.insert(X(2), SE2::identity());
110
111    // // These will all compile-time error
112    // // mismatched symbol-variable types
113    // values.insert(X(5), VectorVar2::identity());
114    // // wrong number of keys
115    // let f = fac![GpsResidual::new(0.0, 0.0), (X(0), X(1))];
116    // // wrong noise-model dimension
117    // let n = factrs::noise::GaussianNoise::<5>::from_scalar_sigma(0.1);
118    // let f = fac![GpsResidual::new(0.0, 0.0), X(0), n];
119    // // mismatched symbol-variable types
120    // assign_symbols!(Y : VectorVar2);
121    // let f = fac![GpsResidual::new(0.0, 0.0), Y(0), 0.1 as std];
122
123    // optimize
124    let mut opt: GaussNewton = GaussNewton::new(graph);
125    let result = opt.optimize(values).expect("Optimization failed");
126
127    println!("Final Result: {:#?}", result);
128}
Source

pub fn with_capacity(capacity: usize) -> Self

Source

pub fn add_factor(&mut self, factor: Factor)

Examples found in repository?
examples/readme.rs (line 24)
11fn main() {
12    // Make all the values
13    let mut values = Values::new();
14
15    let x = SO2::from_theta(1.0);
16    let y = SO2::from_theta(2.0);
17    values.insert(X(0), SO2::identity());
18    values.insert(X(1), SO2::identity());
19
20    // Make the factors & insert into graph
21    let mut graph = Graph::new();
22    let res = PriorResidual::new(x.clone());
23    let factor = fac![res, X(0)];
24    graph.add_factor(factor);
25
26    let res = BetweenResidual::new(y.minus(&x));
27    let factor = fac![res, (X(0), X(1)), 0.1 as std, Huber::default()];
28    graph.add_factor(factor);
29
30    // Optimize!
31    let mut opt: GaussNewton = GaussNewton::new(graph);
32    let result = opt.optimize(values).unwrap();
33    println!("Results {:#}", result);
34}
More examples
Hide additional examples
examples/serde.rs (line 32)
7fn main() {
8    // ------------------------- Serialize values ------------------------- //
9    let x = SO2::from_theta(0.6);
10    let y = SE2::new(1.0, 2.0, 0.3);
11    let mut values = Values::new();
12    values.insert(X(0), x.clone());
13    values.insert(Y(1), y.clone());
14
15    println!("------ Serializing Values ------");
16
17    let serialized = serde_json::to_string_pretty(&values).unwrap();
18    println!("serialized = {}", serialized);
19
20    // Convert the JSON string back to a Point.
21    let deserialized: Values = serde_json::from_str(&serialized).unwrap();
22    println!("deserialized = {:#}", deserialized);
23
24    // ------------------------- Serialize graph ------------------------- //
25    let prior = PriorResidual::new(x);
26    let bet = BetweenResidual::new(y);
27
28    let prior = fac![prior, X(0), 0.1 as cov, GemanMcClure::default()];
29    let bet = fac![bet, (Y(0), Y(1)), 10.0 as cov];
30
31    let mut graph = Graph::new();
32    graph.add_factor(prior);
33    graph.add_factor(bet);
34
35    println!("\n\n------ Serializing Graph ------");
36
37    let serialized = serde_json::to_string_pretty(&graph).unwrap();
38    println!("serialized = {}", serialized);
39
40    let deserialized: Graph = serde_json::from_str(&serialized).unwrap();
41    println!("deserialized = {:#?}", deserialized);
42}
examples/gps.rs (line 94)
87fn main() {
88    let mut graph = Graph::new();
89
90    // Add odometry factors
91    let res = BetweenResidual::new(SE2::new(0.0, 2.0, 0.0));
92    let odometry_01 = fac![res.clone(), (X(0), X(1)), (0.1, 0.2) as cov];
93    let odometry_12 = fac![res, (X(1), X(2)), (0.1, 0.2) as cov];
94    graph.add_factor(odometry_01);
95    graph.add_factor(odometry_12);
96
97    // Add gps factors
98    let g0 = fac![GpsResidual::new(0.0, 0.0), X(0), 1.0 as std];
99    let g1 = fac![GpsResidual::new(2.0, 0.0), X(1), 1.0 as std];
100    let g2 = fac![GpsResidual::new(4.0, 0.0), X(2), 1.0 as std];
101    graph.add_factor(g0);
102    graph.add_factor(g1);
103    graph.add_factor(g2);
104
105    // Make values
106    let mut values = Values::new();
107    values.insert(X(0), SE2::new(1.0, 2.0, 3.0));
108    values.insert(X(1), SE2::identity());
109    values.insert(X(2), SE2::identity());
110
111    // // These will all compile-time error
112    // // mismatched symbol-variable types
113    // values.insert(X(5), VectorVar2::identity());
114    // // wrong number of keys
115    // let f = fac![GpsResidual::new(0.0, 0.0), (X(0), X(1))];
116    // // wrong noise-model dimension
117    // let n = factrs::noise::GaussianNoise::<5>::from_scalar_sigma(0.1);
118    // let f = fac![GpsResidual::new(0.0, 0.0), X(0), n];
119    // // mismatched symbol-variable types
120    // assign_symbols!(Y : VectorVar2);
121    // let f = fac![GpsResidual::new(0.0, 0.0), Y(0), 0.1 as std];
122
123    // optimize
124    let mut opt: GaussNewton = GaussNewton::new(graph);
125    let result = opt.optimize(values).expect("Optimization failed");
126
127    println!("Final Result: {:#?}", result);
128}
Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn error(&self, values: &Values) -> dtype

Source

pub fn linearize(&self, values: &Values) -> LinearGraph

Source

pub fn sparsity_pattern(&self, order: ValuesOrder) -> GraphOrder

Trait Implementations§

Source§

impl Clone for Graph

Source§

fn clone(&self) -> Graph

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Graph

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Graph

Source§

fn default() -> Graph

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Graph

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Graph

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Graph

§

impl !RefUnwindSafe for Graph

§

impl !Send for Graph

§

impl !Sync for Graph

§

impl Unpin for Graph

§

impl !UnwindSafe for Graph

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

Source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
Source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

Source§

fn lossy_into(self) -> Dst

Performs the conversion.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> Serialize for T
where T: Serialize + ?Sized,

Source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>

Source§

fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>

Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,