31 #include <boost/serialization/nvp.hpp> 32 #include <boost/serialization/vector.hpp> 33 #include <boost/assign/list_inserter.hpp> 34 #include <boost/bind.hpp> 35 #include <boost/make_shared.hpp> 37 #include <type_traits> 43 template<
class CLIQUE>
class BayesTree;
53 void operator()(
const A& a) { obj.push_back(a); }
64 void operator()(A& a) { obj.push_back(a); }
75 void operator()(
const A& a) { obj.addCopy(a); }
83 template<
class FACTOR>
90 typedef typename FastVector<sharedFactor>::iterator iterator;
91 typedef typename FastVector<sharedFactor>::const_iterator const_iterator;
95 typedef boost::shared_ptr<This> shared_ptr;
99 GTSAM_CONCEPT_TESTABLE_TYPE(FACTOR)
111 template<
typename ITERATOR>
115 template<
class CONTAINER>
157 template<
class DERIVEDFACTOR>
158 typename std::enable_if<std::is_base_of<FactorType, DERIVEDFACTOR>::value>::type
160 factors_.push_back(boost::shared_ptr<FACTOR>(factor)); }
167 template<
class DERIVEDFACTOR,
class... Args>
168 typename std::enable_if<std::is_base_of<FactorType, DERIVEDFACTOR>::value>::type
170 factors_.push_back(boost::allocate_shared<DERIVEDFACTOR>(Eigen::aligned_allocator<DERIVEDFACTOR>(), std::forward<Args>(args)...));
174 template<
typename ITERATOR>
175 typename std::enable_if<std::is_base_of<FactorType, typename ITERATOR::value_type::element_type>::value>::type
180 template<
typename CONTAINER>
181 typename std::enable_if<std::is_base_of<FactorType, typename CONTAINER::value_type::element_type>::value>::type
183 push_back(container.begin(), container.end());
188 template<
class CLIQUE>
189 typename std::enable_if<std::is_base_of<This, typename CLIQUE::FactorGraphType>::value>::type
197 template<
class DERIVEDFACTOR>
198 typename std::enable_if<std::is_base_of<FactorType, DERIVEDFACTOR>::value>::type
200 factors_.push_back(boost::allocate_shared<DERIVEDFACTOR>(Eigen::aligned_allocator<DERIVEDFACTOR>(), factor));
205 template<
typename ITERATOR>
206 typename std::enable_if<std::is_base_of<FactorType, typename ITERATOR::value_type>::value>::type
208 for (ITERATOR f = firstFactor; f != lastFactor; ++f)
213 template<
typename CONTAINER>
214 typename std::enable_if<std::is_base_of<FactorType, typename CONTAINER::value_type>::value>::type
216 push_back(container.begin(), container.end());
220 template<
class DERIVEDFACTOR>
221 typename std::enable_if<std::is_base_of<FactorType, DERIVEDFACTOR>::value,
222 boost::assign::list_inserter<RefCallPushBack<This> > >::type
228 boost::assign::list_inserter<CRefCallPushBack<This> >
234 template<
class FACTOR_OR_CONTAINER>
235 boost::assign::list_inserter<CRefCallPushBack<This> >
241 template<
class DERIVEDFACTOR>
242 typename std::enable_if<std::is_base_of<FactorType, DERIVEDFACTOR>::value>::type
243 add(boost::shared_ptr<DERIVEDFACTOR> factor) {
253 template<
class FACTOR_OR_CONTAINER>
254 void add(
const FACTOR_OR_CONTAINER& factorOrContainer) {
263 void print(
const std::string& s =
"FactorGraph",
264 const KeyFormatter& formatter = DefaultKeyFormatter)
const;
267 bool equals(
const This& fg,
double tol = 1e-9)
const;
338 iterator
erase(iterator first, iterator last) {
return factors_.erase(first, last); }
354 inline bool exists(
size_t idx)
const {
return idx <
size() &&
at(idx); }
359 friend class boost::serialization::access;
360 template<
class ARCHIVE>
361 void serialize(ARCHIVE & ar,
const unsigned int ) {
362 ar & BOOST_SERIALIZATION_NVP(
factors_);
371 #include <gtsam/inference/FactorGraph-inst.h> void print(const std::string &s="FactorGraph", const KeyFormatter &formatter=DefaultKeyFormatter) const
print out graph
Definition: FactorGraph-inst.h:36
boost::assign::list_inserter< CRefCallPushBack< This > > operator+=(const sharedFactor &factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:229
std::enable_if< std::is_base_of< FactorType, typename ITERATOR::value_type >::value >::type push_back(ITERATOR firstFactor, ITERATOR lastFactor)
push back many factors with an iterator over plain factors (factors are copied)
Definition: FactorGraph.h:207
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:354
A thin wrapper around std::vector that uses a custom allocator.
iterator end()
non-const STL-style end()
Definition: FactorGraph.h:320
void resize(size_t size)
Directly resize the number of factors in the graph.
Definition: FactorGraph.h:326
std::enable_if< std::is_base_of< This, typename CLIQUE::FactorGraphType >::value >::type push_back(const BayesTree< CLIQUE > &bayesTree)
push back a BayesTree as a collection of factors.
Definition: FactorGraph.h:190
sharedFactor back() const
Get the last factor.
Definition: FactorGraph.h:310
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:236
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:298
void replace(size_t index, sharedFactor factor)
replace a factor by index
Definition: FactorGraph.h:332
sharedFactor front() const
Get the first factor.
Definition: FactorGraph.h:307
void addFactorsToGraph(FactorGraph< FactorType > &graph) const
Add all cliques in this BayesTree to the specified factor graph.
Definition: BayesTree-inst.h:157
bool empty() const
Check if the graph is empty (null factors set by remove() will cause this to return false).
Definition: FactorGraph.h:278
FactorGraph(ITERATOR firstFactor, ITERATOR lastFactor)
Constructor from iterator over factors (shared_ptr or plain objects)
Definition: FactorGraph.h:112
Definition: BayesTree.h:64
std::enable_if< std::is_base_of< FactorType, DERIVEDFACTOR >::value >::type emplace_shared(Args &&... args)
Emplace a factor.
Definition: FactorGraph.h:169
KeyVector keyVector() const
Potentially slow function to return all keys involved, sorted, as a vector.
Definition: FactorGraph-inst.h:86
const_iterator begin() const
Iterator to beginning of factors.
Definition: FactorGraph.h:301
Helper.
Definition: FactorGraph.h:58
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:33
FACTOR FactorType
factor type
Definition: FactorGraph.h:87
Helper.
Definition: FactorGraph.h:47
FactorGraph(const CONTAINER &factors)
Construct from container of factors (shared_ptr or plain objects)
Definition: FactorGraph.h:116
size_t nrFactors() const
return the number of non-null factors
Definition: FactorGraph-inst.h:66
void remove(size_t i)
delete factor without re-arranging indexes by inserting a NULL pointer
Definition: FactorGraph.h:329
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:56
A factor graph is a bipartite graph with factor nodes connected to variable nodes.
Definition: BayesTree.h:32
boost::shared_ptr< FACTOR > sharedFactor
Shared pointer to a factor.
Definition: FactorGraph.h:88
bool equals(const This &fg, double tol=1e-9) const
Check equality.
Definition: FactorGraph-inst.h:49
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:152
void add(const sharedFactor &factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:248
iterator erase(iterator item)
Erase factor and rearrange other factors to take up the empty space.
Definition: FactorGraph.h:335
Helper.
Definition: FactorGraph.h:69
FastVector< sharedFactor > factors_
concept check, makes sure FACTOR defines print and equals
Definition: FactorGraph.h:102
std::enable_if< std::is_base_of< FactorType, DERIVEDFACTOR >::value >::type 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:199
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:293
const_iterator end() const
Iterator to end of factors.
Definition: FactorGraph.h:304
std::enable_if< std::is_base_of< FactorType, typename ITERATOR::value_type::element_type >::value >::type push_back(ITERATOR firstFactor, ITERATOR lastFactor)
push back many factors with an iterator over shared_ptr (factors are not copied)
Definition: FactorGraph.h:176
sharedFactor & at(size_t i)
Get a specific factor by index (this checks array bounds and may throw an exception,...
Definition: FactorGraph.h:288
KeySet keys() const
Potentially slow function to return all keys involved, sorted, as a set.
Definition: FactorGraph-inst.h:75
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
std::enable_if< std::is_base_of< FactorType, typename CONTAINER::value_type::element_type >::value >::type push_back(const CONTAINER &container)
push back many factors as shared_ptr's in a container (factors are not copied)
Definition: FactorGraph.h:182
Concept check for values that can be used in unit tests.
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:283
void push_back(const sharedFactor &factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:163
std::enable_if< std::is_base_of< FactorType, DERIVEDFACTOR >::value, boost::assign::list_inserter< RefCallPushBack< This > > >::type operator+=(boost::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:223
std::enable_if< std::is_base_of< FactorType, DERIVEDFACTOR >::value >::type add(boost::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:243
iterator begin()
non-const STL-style begin()
Definition: FactorGraph.h:317
std::enable_if< std::is_base_of< FactorType, typename CONTAINER::value_type >::value >::type push_back(const CONTAINER &container)
push back many factors as non-pointer objects in a container (factors are copied)
Definition: FactorGraph.h:215
void add(const FACTOR_OR_CONTAINER &factorOrContainer)
Add a factor or container of factors, including STL collections, BayesTrees, etc.
Definition: FactorGraph.h:254
iterator erase(iterator first, iterator last)
Erase factors and rearrange other factors to take up the empty space.
Definition: FactorGraph.h:338
std::enable_if< std::is_base_of< FactorType, DERIVEDFACTOR >::value >::type push_back(boost::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:159
size_t size() const
return the number of factors (including any null factors set by remove() ).
Definition: FactorGraph.h:275