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>
45template <
class CLIQUE>
56 void operator()(
const A& a) {
69 void operator()(A& a) {
82 void operator()(
const A& a) {
92template <
class FACTOR>
96 typedef boost::shared_ptr<FACTOR>
99 typedef typename FastVector<sharedFactor>::iterator iterator;
100 typedef typename FastVector<sharedFactor>::const_iterator const_iterator;
104 typedef boost::shared_ptr<This>
108 template <
typename DERIVEDFACTOR>
109 using IsDerived =
typename std::enable_if<
110 std::is_base_of<FactorType, DERIVEDFACTOR>::value>::type;
113 template <
typename T>
114 using HasDerivedValueType =
typename std::enable_if<
115 std::is_base_of<FactorType, typename T::value_type>::value>::type;
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;
124 GTSAM_CONCEPT_TESTABLE_TYPE(FACTOR)
136 template <
typename ITERATOR>
142 template <
class CONTAINER>
164 template <
class DERIVEDFACTOR>
165 IsDerived<DERIVEDFACTOR>
push_back(boost::shared_ptr<DERIVEDFACTOR> factor) {
166 factors_.push_back(boost::shared_ptr<FACTOR>(factor));
170 template <
class DERIVEDFACTOR,
class... Args>
172 factors_.push_back(boost::allocate_shared<DERIVEDFACTOR>(
173 Eigen::aligned_allocator<DERIVEDFACTOR>(),
174 std::forward<Args>(args)...));
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));
188 template <
class DERIVEDFACTOR>
189 IsDerived<DERIVEDFACTOR>
add(boost::shared_ptr<DERIVEDFACTOR> factor) {
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
211 template <
typename ITERATOR>
212 HasDerivedElementType<ITERATOR>
push_back(ITERATOR firstFactor,
213 ITERATOR lastFactor) {
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);
232 template <
typename CONTAINER>
233 HasDerivedElementType<CONTAINER>
push_back(
const CONTAINER& container) {
234 push_back(container.begin(), container.end());
238 template <
typename CONTAINER>
239 HasDerivedValueType<CONTAINER>
push_back(
const CONTAINER& container) {
240 push_back(container.begin(), container.end());
247 template <
class FACTOR_OR_CONTAINER>
248 void add(
const FACTOR_OR_CONTAINER& factorOrContainer) {
256 template <
class FACTOR_OR_CONTAINER>
257 boost::assign::list_inserter<CRefCallPushBack<This>>
operator+=(
258 const FACTOR_OR_CONTAINER& factorOrContainer) {
272 template <
class CLIQUE>
273 typename std::enable_if<
274 std::is_base_of<This, typename CLIQUE::FactorGraphType>::value>::type
283 template <
typename CONTAINER,
typename = HasDerivedElementType<CONTAINER>>
285 bool useEmptySlots =
false);
292 virtual void print(
const std::string& s =
"FactorGraph",
293 const KeyFormatter& formatter = DefaultKeyFormatter)
const;
296 bool equals(
const This& fg,
double tol = 1e-9)
const;
370 iterator
erase(iterator first, iterator last) {
392 inline bool exists(
size_t idx)
const {
return idx <
size() &&
at(idx); }
397 template <
class ARCHIVE>
398 void serialize(ARCHIVE& ar,
const unsigned int ) {
399 ar& BOOST_SERIALIZATION_NVP(
factors_);
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