gtsam 4.1.1
gtsam
FactorGraph.h
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2
3 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4 * Atlanta, Georgia 30332-0415
5 * All Rights Reserved
6 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7
8 * See LICENSE for the license information
9
10 * -------------------------------------------------------------------------- */
11
21// \callgraph
22
23#pragma once
24
26#include <gtsam/base/Testable.h>
27#include <gtsam/inference/Key.h>
28
29#include <Eigen/Core> // for Eigen::aligned_allocator
30
31#include <boost/assign/list_inserter.hpp>
32#include <boost/make_shared.hpp>
33#include <boost/serialization/nvp.hpp>
34#include <boost/serialization/vector.hpp>
35
36#include <string>
37#include <type_traits>
38#include <utility>
39
40namespace gtsam {
42typedef FastVector<FactorIndex> FactorIndices;
43
44// Forward declarations
45template <class CLIQUE>
46class BayesTree;
47
49template <class C>
51 C& obj;
52
53 public:
54 explicit CRefCallPushBack(C& obj) : obj(obj) {}
55 template <typename A>
56 void operator()(const A& a) {
57 obj.push_back(a);
58 }
59};
60
62template <class C>
64 C& obj;
65
66 public:
67 explicit RefCallPushBack(C& obj) : obj(obj) {}
68 template <typename A>
69 void operator()(A& a) {
70 obj.push_back(a);
71 }
72};
73
75template <class C>
77 C& obj;
78
79 public:
80 explicit CRefCallAddCopy(C& obj) : obj(obj) {}
81 template <typename A>
82 void operator()(const A& a) {
83 obj.addCopy(a);
84 }
85};
86
92template <class FACTOR>
94 public:
95 typedef FACTOR FactorType;
96 typedef boost::shared_ptr<FACTOR>
98 typedef sharedFactor value_type;
99 typedef typename FastVector<sharedFactor>::iterator iterator;
100 typedef typename FastVector<sharedFactor>::const_iterator const_iterator;
101
102 private:
103 typedef FactorGraph<FACTOR> This;
104 typedef boost::shared_ptr<This>
105 shared_ptr;
106
108 template <typename DERIVEDFACTOR>
109 using IsDerived = typename std::enable_if<
110 std::is_base_of<FactorType, DERIVEDFACTOR>::value>::type;
111
113 template <typename T>
114 using HasDerivedValueType = typename std::enable_if<
115 std::is_base_of<FactorType, typename T::value_type>::value>::type;
116
118 template <typename T>
119 using HasDerivedElementType = typename std::enable_if<std::is_base_of<
120 FactorType, typename T::value_type::element_type>::value>::type;
121
122 protected:
124 GTSAM_CONCEPT_TESTABLE_TYPE(FACTOR)
125
126
128
131
134
136 template <typename ITERATOR>
137 FactorGraph(ITERATOR firstFactor, ITERATOR lastFactor) {
138 push_back(firstFactor, lastFactor);
139 }
140
142 template <class CONTAINER>
143 explicit FactorGraph(const CONTAINER& factors) {
144 push_back(factors);
145 }
146
148
149 public:
151 // Public and virtual so boost serialization can call it.
152 virtual ~FactorGraph() = default;
153
156
161 void reserve(size_t size) { factors_.reserve(size); }
162
164 template <class DERIVEDFACTOR>
165 IsDerived<DERIVEDFACTOR> push_back(boost::shared_ptr<DERIVEDFACTOR> factor) {
166 factors_.push_back(boost::shared_ptr<FACTOR>(factor));
167 }
168
170 template <class DERIVEDFACTOR, class... Args>
171 IsDerived<DERIVEDFACTOR> emplace_shared(Args&&... args) {
172 factors_.push_back(boost::allocate_shared<DERIVEDFACTOR>(
173 Eigen::aligned_allocator<DERIVEDFACTOR>(),
174 std::forward<Args>(args)...));
175 }
176
181 template <class DERIVEDFACTOR>
182 IsDerived<DERIVEDFACTOR> push_back(const DERIVEDFACTOR& factor) {
183 factors_.push_back(boost::allocate_shared<DERIVEDFACTOR>(
184 Eigen::aligned_allocator<DERIVEDFACTOR>(), factor));
185 }
186
188 template <class DERIVEDFACTOR>
189 IsDerived<DERIVEDFACTOR> add(boost::shared_ptr<DERIVEDFACTOR> factor) {
190 push_back(factor);
191 }
192
194 template <class DERIVEDFACTOR>
195 typename std::enable_if<
196 std::is_base_of<FactorType, DERIVEDFACTOR>::value,
197 boost::assign::list_inserter<RefCallPushBack<This>>>::type
198 operator+=(boost::shared_ptr<DERIVEDFACTOR> factor) {
199 return boost::assign::make_list_inserter(RefCallPushBack<This>(*this))(
200 factor);
201 }
202
206
211 template <typename ITERATOR>
212 HasDerivedElementType<ITERATOR> push_back(ITERATOR firstFactor,
213 ITERATOR lastFactor) {
214 factors_.insert(end(), firstFactor, lastFactor);
215 }
216
218 template <typename ITERATOR>
219 HasDerivedValueType<ITERATOR> push_back(ITERATOR firstFactor,
220 ITERATOR lastFactor) {
221 for (ITERATOR f = firstFactor; f != lastFactor; ++f) push_back(*f);
222 }
223
227
232 template <typename CONTAINER>
233 HasDerivedElementType<CONTAINER> push_back(const CONTAINER& container) {
234 push_back(container.begin(), container.end());
235 }
236
238 template <typename CONTAINER>
239 HasDerivedValueType<CONTAINER> push_back(const CONTAINER& container) {
240 push_back(container.begin(), container.end());
241 }
242
247 template <class FACTOR_OR_CONTAINER>
248 void add(const FACTOR_OR_CONTAINER& factorOrContainer) {
249 push_back(factorOrContainer);
250 }
251
256 template <class FACTOR_OR_CONTAINER>
257 boost::assign::list_inserter<CRefCallPushBack<This>> operator+=(
258 const FACTOR_OR_CONTAINER& factorOrContainer) {
259 return boost::assign::make_list_inserter(CRefCallPushBack<This>(*this))(
260 factorOrContainer);
261 }
262
266
272 template <class CLIQUE>
273 typename std::enable_if<
274 std::is_base_of<This, typename CLIQUE::FactorGraphType>::value>::type
275 push_back(const BayesTree<CLIQUE>& bayesTree) {
276 bayesTree.addFactorsToGraph(this);
277 }
278
283 template <typename CONTAINER, typename = HasDerivedElementType<CONTAINER>>
284 FactorIndices add_factors(const CONTAINER& factors,
285 bool useEmptySlots = false);
286
290
292 virtual void print(const std::string& s = "FactorGraph",
293 const KeyFormatter& formatter = DefaultKeyFormatter) const;
294
296 bool equals(const This& fg, double tol = 1e-9) const;
298
299 public:
302
305 size_t size() const { return factors_.size(); }
306
309 bool empty() const { return factors_.empty(); }
310
314 const sharedFactor at(size_t i) const { return factors_.at(i); }
315
319 sharedFactor& at(size_t i) { return factors_.at(i); }
320
324 const sharedFactor operator[](size_t i) const { return at(i); }
325
329 sharedFactor& operator[](size_t i) { return at(i); }
330
332 const_iterator begin() const { return factors_.begin(); }
333
335 const_iterator end() const { return factors_.end(); }
336
338 sharedFactor front() const { return factors_.front(); }
339
341 sharedFactor back() const { return factors_.back(); }
342
346
348 iterator begin() { return factors_.begin(); }
349
351 iterator end() { return factors_.end(); }
352
357 void resize(size_t size) { factors_.resize(size); }
358
361 void remove(size_t i) { factors_.at(i).reset(); }
362
364 void replace(size_t index, sharedFactor factor) { at(index) = factor; }
365
367 iterator erase(iterator item) { return factors_.erase(item); }
368
370 iterator erase(iterator first, iterator last) {
371 return factors_.erase(first, last);
372 }
373
377
379 size_t nrFactors() const;
380
383 KeySet keys() const;
384
388 KeyVector keyVector() const;
389
392 inline bool exists(size_t idx) const { return idx < size() && at(idx); }
393
394 private:
397 template <class ARCHIVE>
398 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
399 ar& BOOST_SERIALIZATION_NVP(factors_);
400 }
401
403}; // FactorGraph
404} // namespace gtsam
405
406#include <gtsam/inference/FactorGraph-inst.h>
A thin wrapper around std::vector that uses a custom allocator.
Concept check for values that can be used in unit tests.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
FastVector< FactorIndex > FactorIndices
Define collection types:
Definition: Factor.h:33
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:35
A factor graph is a bipartite graph with factor nodes connected to variable nodes.
Definition: FactorGraph.h:93
virtual void print(const std::string &s="FactorGraph", const KeyFormatter &formatter=DefaultKeyFormatter) const
print out graph
Definition: FactorGraph-inst.h:36
KeySet keys() const
Potentially slow function to return all keys involved, sorted, as a set.
Definition: FactorGraph-inst.h:74
bool empty() const
Check if the graph is empty (null factors set by remove() will cause this to return false).
Definition: FactorGraph.h:309
FactorIndices add_factors(const CONTAINER &factors, bool useEmptySlots=false)
Add new factors to a factor graph and returns a list of new factor indices, optionally finding and re...
Definition: FactorGraph-inst.h:98
IsDerived< DERIVEDFACTOR > push_back(boost::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:165
iterator erase(iterator item)
Erase factor and rearrange other factors to take up the empty space.
Definition: FactorGraph.h:367
void resize(size_t size)
Directly resize the number of factors in the graph.
Definition: FactorGraph.h:357
void add(const FACTOR_OR_CONTAINER &factorOrContainer)
Add a factor or container of factors, including STL collections, BayesTrees, etc.
Definition: FactorGraph.h:248
iterator erase(iterator first, iterator last)
Erase factors and rearrange other factors to take up the empty space.
Definition: FactorGraph.h:370
sharedFactor back() const
Get the last factor.
Definition: FactorGraph.h:341
FactorGraph(const CONTAINER &factors)
Construct from container of factors (shared_ptr or plain objects)
Definition: FactorGraph.h:143
void remove(size_t i)
delete factor without re-arranging indexes by inserting a nullptr pointer
Definition: FactorGraph.h:361
KeyVector keyVector() const
Potentially slow function to return all keys involved, sorted, as a vector.
Definition: FactorGraph-inst.h:84
HasDerivedValueType< CONTAINER > push_back(const CONTAINER &container)
Push back non-pointer objects in a container (factors are copied).
Definition: FactorGraph.h:239
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
Definition: FactorGraph.h:171
sharedFactor & operator[](size_t i)
Get a specific factor by index (this does not check array bounds, as opposed to at() which does).
Definition: FactorGraph.h:329
sharedFactor & at(size_t i)
Get a specific factor by index (this checks array bounds and may throw an exception,...
Definition: FactorGraph.h:319
std::enable_if< std::is_base_of< This, typenameCLIQUE::FactorGraphType >::value >::type push_back(const BayesTree< CLIQUE > &bayesTree)
Push back a BayesTree as a collection of factors.
Definition: FactorGraph.h:275
FactorGraph(ITERATOR firstFactor, ITERATOR lastFactor)
Constructor from iterator over factors (shared_ptr or plain objects)
Definition: FactorGraph.h:137
size_t nrFactors() const
return the number of non-null factors
Definition: FactorGraph-inst.h:65
size_t size() const
return the number of factors (including any null factors set by remove() ).
Definition: FactorGraph.h:305
HasDerivedValueType< ITERATOR > push_back(ITERATOR firstFactor, ITERATOR lastFactor)
Push back many factors with an iterator (factors are copied)
Definition: FactorGraph.h:219
sharedFactor front() const
Get the first factor.
Definition: FactorGraph.h:338
const_iterator end() const
Iterator to end of factors.
Definition: FactorGraph.h:335
HasDerivedElementType< CONTAINER > push_back(const CONTAINER &container)
Push back many factors as shared_ptr's in a container (factors are not copied)
Definition: FactorGraph.h:233
FACTOR FactorType
factor type
Definition: FactorGraph.h:95
void replace(size_t index, sharedFactor factor)
replace a factor by index
Definition: FactorGraph.h:364
boost::assign::list_inserter< CRefCallPushBack< This > > operator+=(const FACTOR_OR_CONTAINER &factorOrContainer)
Add a factor or container of factors, including STL collections, BayesTrees, etc.
Definition: FactorGraph.h:257
bool equals(const This &fg, double tol=1e-9) const
Check equality.
Definition: FactorGraph-inst.h:49
std::enable_if< std::is_base_of< FactorType, DERIVEDFACTOR >::value, boost::assign::list_inserter< RefCallPushBack< This > > >::type operator+=(boost::shared_ptr< DERIVEDFACTOR > factor)
+= works well with boost::assign list inserter.
Definition: FactorGraph.h:198
iterator begin()
non-const STL-style begin()
Definition: FactorGraph.h:348
const_iterator begin() const
Iterator to beginning of factors.
Definition: FactorGraph.h:332
HasDerivedElementType< ITERATOR > push_back(ITERATOR firstFactor, ITERATOR lastFactor)
Push back many factors with an iterator over shared_ptr (factors are not copied)
Definition: FactorGraph.h:212
IsDerived< DERIVEDFACTOR > add(boost::shared_ptr< DERIVEDFACTOR > factor)
add is a synonym for push_back.
Definition: FactorGraph.h:189
friend class boost::serialization::access
Serialization function.
Definition: FactorGraph.h:396
boost::shared_ptr< FACTOR > sharedFactor
Shared pointer to a factor.
Definition: FactorGraph.h:97
bool exists(size_t idx) const
MATLAB interface utility: Checks whether a factor index idx exists in the graph and is a live pointer...
Definition: FactorGraph.h:392
const sharedFactor operator[](size_t i) const
Get a specific factor by index (this does not check array bounds, as opposed to at() which does).
Definition: FactorGraph.h:324
IsDerived< DERIVEDFACTOR > push_back(const DERIVEDFACTOR &factor)
Add a factor by value, will be copy-constructed (use push_back with a shared_ptr to avoid the copy).
Definition: FactorGraph.h:182
iterator end()
non-const STL-style end()
Definition: FactorGraph.h:351
FastVector< sharedFactor > factors_
concept check, makes sure FACTOR defines print and equals
Definition: FactorGraph.h:127
const sharedFactor at(size_t i) const
Get a specific factor by index (this checks array bounds and may throw an exception,...
Definition: FactorGraph.h:314
virtual ~FactorGraph()=default
Default destructor.
void reserve(size_t size)
Reserve space for the specified number of factors if you know in advance how many there will be (work...
Definition: FactorGraph.h:161
Definition: BayesTree.h:67
void addFactorsToGraph(FactorGraph< FactorType > *graph) const
Add all cliques in this BayesTree to the specified factor graph.
Definition: BayesTree-inst.h:151
Helper.
Definition: FactorGraph.h:50
Helper.
Definition: FactorGraph.h:63
Helper.
Definition: FactorGraph.h:76