32#if GTSAM_ENABLE_BOOST_SERIALIZATION
33#include <boost/serialization/nvp.hpp>
34#include <boost/serialization/vector.hpp>
47template <
class CLIQUE>
57template <
class FACTOR>
61 typedef std::shared_ptr<FACTOR>
69 typedef std::shared_ptr<This>
73 template <
typename DERIVEDFACTOR>
74 using IsDerived =
typename std::enable_if<
75 std::is_base_of<FactorType, DERIVEDFACTOR>::value>::type;
79 using HasDerivedValueType =
typename std::enable_if<
80 std::is_base_of<FactorType, typename T::value_type>::value>::type;
84 using HasDerivedElementType =
typename std::enable_if<std::is_base_of<
85 FactorType,
typename T::value_type::element_type>::value>::type;
89 GTSAM_CONCEPT_TESTABLE_TYPE(FACTOR)
106 template <
typename ITERATOR>
112 template <
class CONTAINER>
131 template <
class DERIVEDFACTOR,
typename = IsDerived<DERIVEDFACTOR>>
132 FactorGraph(std::initializer_list<std::shared_ptr<DERIVEDFACTOR>> sharedFactors)
146 template <
class DERIVEDFACTOR>
147 IsDerived<DERIVEDFACTOR>
push_back(std::shared_ptr<DERIVEDFACTOR> factor) {
148 factors_.push_back(std::shared_ptr<FACTOR>(factor));
152 template <
class DERIVEDFACTOR,
class... Args>
154 factors_.push_back(std::allocate_shared<DERIVEDFACTOR>(
155 Eigen::aligned_allocator<DERIVEDFACTOR>(),
156 std::forward<Args>(args)...));
163 template <
class DERIVEDFACTOR>
164 IsDerived<DERIVEDFACTOR>
push_back(
const DERIVEDFACTOR& factor) {
165 factors_.push_back(std::allocate_shared<DERIVEDFACTOR>(
166 Eigen::aligned_allocator<DERIVEDFACTOR>(), factor));
170 template <
class DERIVEDFACTOR>
171 IsDerived<DERIVEDFACTOR>
add(std::shared_ptr<DERIVEDFACTOR> factor) {
176 template <
class DERIVEDFACTOR>
177 typename std::enable_if<std::is_base_of<FactorType, DERIVEDFACTOR>::value,
189 template <
class DERIVEDFACTOR>
190 typename std::enable_if<std::is_base_of<FactorType, DERIVEDFACTOR>::value, This>::type&
operator,(
191 std::shared_ptr<DERIVEDFACTOR> factor) {
204 template <
typename ITERATOR>
205 HasDerivedElementType<ITERATOR>
push_back(ITERATOR firstFactor,
206 ITERATOR lastFactor) {
211 template <
typename ITERATOR>
212 HasDerivedValueType<ITERATOR>
push_back(ITERATOR firstFactor,
213 ITERATOR lastFactor) {
214 for (ITERATOR f = firstFactor; f != lastFactor; ++f)
push_back(*f);
225 template <
typename CONTAINER>
226 HasDerivedElementType<CONTAINER>
push_back(
const CONTAINER& container) {
227 push_back(container.begin(), container.end());
231 template <
typename CONTAINER>
232 HasDerivedValueType<CONTAINER>
push_back(
const CONTAINER& container) {
233 push_back(container.begin(), container.end());
240 template <
class FACTOR_OR_CONTAINER>
241 void add(
const FACTOR_OR_CONTAINER& factorOrContainer) {
249 template <
class FACTOR_OR_CONTAINER>
250 This&
operator+=(
const FACTOR_OR_CONTAINER& factorOrContainer) {
264 template <
class CLIQUE>
265 typename std::enable_if<
266 std::is_base_of<This, typename CLIQUE::FactorGraphType>::value>::type
275 template <
typename CONTAINER,
typename = HasDerivedElementType<CONTAINER>>
277 bool useEmptySlots =
false);
284 virtual void print(
const std::string& s =
"FactorGraph",
288 bool equals(
const This& fg,
double tol = 1e-9)
const;
317 template <
typename F>
318 std::shared_ptr<F>
at(
size_t i) {
319 return std::dynamic_pointer_cast<F>(
factors_.at(i));
323 template <
typename F>
324 const std::shared_ptr<F>
at(
size_t i)
const {
325 return std::dynamic_pointer_cast<F>(
factors_.at(i));
380 iterator
erase(iterator first, iterator last) {
389 void dot(std::ostream& os,
420 inline bool exists(
size_t idx)
const {
return idx <
size() &&
at(idx); }
423#if GTSAM_ENABLE_BOOST_SERIALIZATION
425 friend class boost::serialization::access;
426 template <
class ARCHIVE>
427 void serialize(ARCHIVE& ar,
const unsigned int ) {
428 ar& BOOST_SERIALIZATION_NVP(
factors_);
A thin wrapper around std::vector that uses a custom allocator.
Concept check for values that can be used in unit tests.
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:33
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:91
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
FastVector< FactorIndex > FactorIndices
Define collection types:
Definition Factor.h:37
HybridValues represents a collection of DiscreteValues and VectorValues.
Definition HybridValues.h:37
virtual void print(const std::string &s="FactorGraph", const KeyFormatter &formatter=DefaultKeyFormatter) const
Print out graph to std::cout, with optional key formatter.
Definition FactorGraph-inst.h:37
bool isEqual(const FactorGraph &other) const
Definition FactorGraph.h:95
KeySet keys() const
Potentially slow function to return all keys involved, sorted, as a set.
Definition FactorGraph-inst.h:85
bool empty() const
Check if the graph is empty (null factors set by remove() will cause this to return false).
Definition FactorGraph.h:301
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:109
void dot(std::ostream &os, const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DotWriter &writer=DotWriter()) const
Output to graphviz format, stream version.
Definition FactorGraph-inst.h:141
iterator erase(iterator item)
Erase factor and rearrange other factors to take up the empty space.
Definition FactorGraph.h:377
void add(const FACTOR_OR_CONTAINER &factorOrContainer)
Add a factor or container of factors, including STL collections, BayesTrees, etc.
Definition FactorGraph.h:241
iterator erase(iterator first, iterator last)
Erase factors and rearrange other factors to take up the empty space.
Definition FactorGraph.h:380
FactorGraph(std::initializer_list< std::shared_ptr< DERIVEDFACTOR > > sharedFactors)
Constructor that takes an initializer list of shared pointers.
Definition FactorGraph.h:132
sharedFactor back() const
Get the last factor.
Definition FactorGraph.h:348
FactorGraph(const CONTAINER &factors)
Construct from container of factors (shared_ptr or plain objects).
Definition FactorGraph.h:113
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition FactorGraph.h:147
void remove(size_t i)
delete factor without re-arranging indexes by inserting a nullptr pointer
Definition FactorGraph.h:371
KeyVector keyVector() const
Potentially slow function to return all keys involved, sorted, as a vector.
Definition FactorGraph-inst.h:95
HasDerivedValueType< CONTAINER > push_back(const CONTAINER &container)
Push back non-pointer objects in a container (factors are copied).
Definition FactorGraph.h:232
FactorGraph()
Default constructor.
Definition FactorGraph.h:103
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
Definition FactorGraph.h:153
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:336
sharedFactor & at(size_t i)
Get a specific factor by index (this checks array bounds and may throw an exception,...
Definition FactorGraph.h:311
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:267
double error(const HybridValues &values) const
Add error for all factors.
Definition FactorGraph-inst.h:66
FactorGraph(ITERATOR firstFactor, ITERATOR lastFactor)
Constructor from iterator over factors (shared_ptr or plain objects).
Definition FactorGraph.h:107
std::shared_ptr< F > at(size_t i)
Get a specific factor by index and typecast to factor type F (this checks array bounds and may throw ...
Definition FactorGraph.h:318
std::shared_ptr< DiscreteFactor > sharedFactor
Definition FactorGraph.h:62
virtual void resize(size_t size)
Directly resize the number of factors in the graph.
Definition FactorGraph.h:367
size_t nrFactors() const
return the number of non-null factors
Definition FactorGraph-inst.h:76
size_t size() const
Definition FactorGraph.h:297
HasDerivedValueType< ITERATOR > push_back(ITERATOR firstFactor, ITERATOR lastFactor)
Push back many factors with an iterator (factors are copied).
Definition FactorGraph.h:212
sharedFactor front() const
Get the first factor.
Definition FactorGraph.h:345
std::enable_if< std::is_base_of< FactorType, DERIVEDFACTOR >::value, This >::type & operator,(std::shared_ptr< DERIVEDFACTOR > factor)
Overload comma operator to allow for append chaining.
Definition FactorGraph.h:190
const_iterator end() const
Iterator to end of factors.
Definition FactorGraph.h:342
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:226
DiscreteFactor FactorType
Definition FactorGraph.h:60
void replace(size_t index, sharedFactor factor)
replace a factor by index
Definition FactorGraph.h:374
bool equals(const This &fg, double tol=1e-9) const
Check equality up to tolerance.
Definition FactorGraph-inst.h:50
const std::shared_ptr< F > at(size_t i) const
Const version of templated at method.
Definition FactorGraph.h:324
iterator begin()
non-const STL-style begin()
Definition FactorGraph.h:358
This & operator+=(const FACTOR_OR_CONTAINER &factorOrContainer)
Add a factor or container of factors, including STL collections, BayesTrees, etc.
Definition FactorGraph.h:250
const_iterator begin() const
Iterator to beginning of factors.
Definition FactorGraph.h:339
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:205
std::enable_if< std::is_base_of< FactorType, DERIVEDFACTOR >::value, This >::type & operator+=(std::shared_ptr< DERIVEDFACTOR > factor)
Append factor to factor graph.
Definition FactorGraph.h:179
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:420
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:331
std::string dot(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DotWriter &writer=DotWriter()) const
Output to graphviz format string.
Definition FactorGraph-inst.h:168
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:164
iterator end()
non-const STL-style end()
Definition FactorGraph.h:361
FastVector< sharedFactor > factors_
Definition FactorGraph.h:92
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:306
IsDerived< DERIVEDFACTOR > add(std::shared_ptr< DERIVEDFACTOR > factor)
add is a synonym for push_back.
Definition FactorGraph.h:171
void saveGraph(const std::string &filename, const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DotWriter &writer=DotWriter()) const
output to file with graphviz format.
Definition FactorGraph-inst.h:177
virtual ~FactorGraph()=default
Default destructor Public and virtual so boost serialization can call it.
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:143
Bayes tree.
Definition BayesTree.h:77
void addFactorsToGraph(FactorGraph< FactorType > *graph) const
Add all cliques in this BayesTree to the specified factor graph.
Definition BayesTree-inst.h:171
DotWriter is a helper class for writing graphviz .dot files.
Definition DotWriter.h:36