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
impl Graph
Sourcepub fn new() -> Self
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
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}
pub fn with_capacity(capacity: usize) -> Self
Sourcepub fn add_factor(&mut self, factor: Factor)
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
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}
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn error(&self, values: &Values) -> dtype
pub fn linearize(&self, values: &Values) -> LinearGraph
pub fn sparsity_pattern(&self, order: ValuesOrder) -> GraphOrder
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Graph
impl<'de> Deserialize<'de> for Graph
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Casts the value.
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
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>
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)
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)
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T
in a tonic::Request
Source§impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
Source§fn lossless_try_into(self) -> Option<Dst>
fn lossless_try_into(self) -> Option<Dst>
Performs the conversion.
Source§impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
Source§fn lossy_into(self) -> Dst
fn lossy_into(self) -> Dst
Performs the conversion.
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Casts the value.
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> Serialize for T
impl<T> Serialize for T
fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>
fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
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
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
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self
to the equivalent element of its superset.Source§impl<T> UnwrappedAs for T
impl<T> UnwrappedAs for T
Source§fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
Source§fn unwrapped_cast_from(src: Src) -> Dst
fn unwrapped_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> WithSubscriber for T
impl<T> WithSubscriber for T
Source§fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
Source§fn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Source§impl<T> WrappingAs for T
impl<T> WrappingAs for T
Source§fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
Source§fn wrapping_cast_from(src: Src) -> Dst
fn wrapping_cast_from(src: Src) -> Dst
Casts the value.