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 
25 #include <gtsam/base/FastVector.h>
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/bind.hpp>
33 #include <boost/make_shared.hpp>
34 #include <boost/serialization/nvp.hpp>
35 #include <boost/serialization/vector.hpp>
36 
37 #include <string>
38 #include <type_traits>
39 #include <utility>
40 
41 namespace gtsam {
43 typedef FastVector<FactorIndex> FactorIndices;
44 
45 // Forward declarations
46 template <class CLIQUE>
47 class BayesTree;
48 
50 template <class C>
52  C& obj;
53 
54  public:
55  explicit CRefCallPushBack(C& obj) : obj(obj) {}
56  template <typename A>
57  void operator()(const A& a) {
58  obj.push_back(a);
59  }
60 };
61 
63 template <class C>
65  C& obj;
66 
67  public:
68  explicit RefCallPushBack(C& obj) : obj(obj) {}
69  template <typename A>
70  void operator()(A& a) {
71  obj.push_back(a);
72  }
73 };
74 
76 template <class C>
78  C& obj;
79 
80  public:
81  explicit CRefCallAddCopy(C& obj) : obj(obj) {}
82  template <typename A>
83  void operator()(const A& a) {
84  obj.addCopy(a);
85  }
86 };
87 
93 template <class FACTOR>
94 class FactorGraph {
95  public:
96  typedef FACTOR FactorType;
97  typedef boost::shared_ptr<FACTOR>
102 
103  private:
105  typedef boost::shared_ptr<This>
107 
109  template <typename DERIVEDFACTOR>
110  using IsDerived = typename std::enable_if<
112 
114  template <typename T>
115  using HasDerivedValueType = typename std::enable_if<
117 
119  template <typename T>
120  using HasDerivedElementType = typename std::enable_if<std::is_base_of<
121  FactorType, typename T::value_type::element_type>::value>::type;
122 
123  protected:
126 
127 
129 
132 
135 
137  template <typename ITERATOR>
138  FactorGraph(ITERATOR firstFactor, ITERATOR lastFactor) {
139  push_back(firstFactor, lastFactor);
140  }
141 
143  template <class CONTAINER>
144  explicit FactorGraph(const CONTAINER& factors) {
145  push_back(factors);
146  }
147 
149 
150  public:
152  // Public and virtual so boost serialization can call it.
153  virtual ~FactorGraph() = default;
154 
157 
162  void reserve(size_t size) { factors_.reserve(size); }
163 
165  template <class DERIVEDFACTOR>
166  IsDerived<DERIVEDFACTOR> push_back(boost::shared_ptr<DERIVEDFACTOR> factor) {
167  factors_.push_back(boost::shared_ptr<FACTOR>(factor));
168  }
169 
171  template <class DERIVEDFACTOR, class... Args>
173  factors_.push_back(boost::allocate_shared<DERIVEDFACTOR>(
175  std::forward<Args>(args)...));
176  }
177 
182  template <class DERIVEDFACTOR>
183  IsDerived<DERIVEDFACTOR> push_back(const DERIVEDFACTOR& factor) {
184  factors_.push_back(boost::allocate_shared<DERIVEDFACTOR>(
186  }
187 
189  template <class DERIVEDFACTOR>
190  IsDerived<DERIVEDFACTOR> add(boost::shared_ptr<DERIVEDFACTOR> factor) {
191  push_back(factor);
192  }
193 
195  template <class DERIVEDFACTOR>
196  typename std::enable_if<
198  boost::assign::list_inserter<RefCallPushBack<This>>>::type
199  operator+=(boost::shared_ptr<DERIVEDFACTOR> factor) {
200  return boost::assign::make_list_inserter(RefCallPushBack<This>(*this))(
201  factor);
202  }
203 
207 
212  template <typename ITERATOR>
214  ITERATOR lastFactor) {
215  factors_.insert(end(), firstFactor, lastFactor);
216  }
217 
219  template <typename ITERATOR>
221  ITERATOR lastFactor) {
222  for (ITERATOR f = firstFactor; f != lastFactor; ++f) push_back(*f);
223  }
224 
228 
233  template <typename CONTAINER>
234  HasDerivedElementType<CONTAINER> push_back(const CONTAINER& container) {
235  push_back(container.begin(), container.end());
236  }
237 
239  template <typename CONTAINER>
240  HasDerivedValueType<CONTAINER> push_back(const CONTAINER& container) {
241  push_back(container.begin(), container.end());
242  }
243 
248  template <class FACTOR_OR_CONTAINER>
249  void add(const FACTOR_OR_CONTAINER& factorOrContainer) {
250  push_back(factorOrContainer);
251  }
252 
257  template <class FACTOR_OR_CONTAINER>
258  boost::assign::list_inserter<CRefCallPushBack<This>> operator+=(
259  const FACTOR_OR_CONTAINER& factorOrContainer) {
260  return boost::assign::make_list_inserter(CRefCallPushBack<This>(*this))(
261  factorOrContainer);
262  }
263 
267 
273  template <class CLIQUE>
274  typename std::enable_if<
276  push_back(const BayesTree<CLIQUE>& bayesTree) {
277  bayesTree.addFactorsToGraph(this);
278  }
279 
284  template <typename CONTAINER, typename = HasDerivedElementType<CONTAINER>>
285  FactorIndices add_factors(const CONTAINER& factors,
286  bool useEmptySlots = false);
287 
291 
293  virtual void print(const std::string& s = "FactorGraph",
295 
297  bool equals(const This& fg, double tol = 1e-9) const;
299 
300  public:
303 
306  size_t size() const { return factors_.size(); }
307 
310  bool empty() const { return factors_.empty(); }
311 
315  const sharedFactor at(size_t i) const { return factors_.at(i); }
316 
320  sharedFactor& at(size_t i) { return factors_.at(i); }
321 
325  const sharedFactor operator[](size_t i) const { return at(i); }
326 
330  sharedFactor& operator[](size_t i) { return at(i); }
331 
333  const_iterator begin() const { return factors_.begin(); }
334 
336  const_iterator end() const { return factors_.end(); }
337 
339  sharedFactor front() const { return factors_.front(); }
340 
342  sharedFactor back() const { return factors_.back(); }
343 
347 
349  iterator begin() { return factors_.begin(); }
350 
352  iterator end() { return factors_.end(); }
353 
358  void resize(size_t size) { factors_.resize(size); }
359 
362  void remove(size_t i) { factors_.at(i).reset(); }
363 
365  void replace(size_t index, sharedFactor factor) { at(index) = factor; }
366 
368  iterator erase(iterator item) { return factors_.erase(item); }
369 
371  iterator erase(iterator first, iterator last) {
372  return factors_.erase(first, last);
373  }
374 
378 
380  size_t nrFactors() const;
381 
384  KeySet keys() const;
385 
389  KeyVector keyVector() const;
390 
393  inline bool exists(size_t idx) const { return idx < size() && at(idx); }
394 
395  private:
397  friend class boost::serialization::access;
398  template <class ARCHIVE>
399  void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
400  ar& BOOST_SERIALIZATION_NVP(factors_);
401  }
402 
404 }; // FactorGraph
405 } // namespace gtsam
406 
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
size_t size() const
Definition: FactorGraph.h:306
constexpr int last(int, int result)
void add(const FACTOR_OR_CONTAINER &factorOrContainer)
Definition: FactorGraph.h:249
void replace(size_t index, sharedFactor factor)
Definition: FactorGraph.h:365
Concept check for values that can be used in unit tests.
boost::assign::list_inserter< CRefCallPushBack< This > > operator+=(const FACTOR_OR_CONTAINER &factorOrContainer)
Definition: FactorGraph.h:258
void operator()(const A &a)
Definition: FactorGraph.h:57
boost::shared_ptr< This > shared_ptr
Shared pointer for this class.
Definition: FactorGraph.h:106
HasDerivedElementType< ITERATOR > push_back(ITERATOR firstFactor, ITERATOR lastFactor)
Definition: FactorGraph.h:213
FactorGraph(ITERATOR firstFactor, ITERATOR lastFactor)
Definition: FactorGraph.h:138
Definition: pytypes.h:1322
HasDerivedValueType< ITERATOR > push_back(ITERATOR firstFactor, ITERATOR lastFactor)
Push back many factors with an iterator (factors are copied)
Definition: FactorGraph.h:220
FastVector< FactorIndex > FactorIndices
Define collection types:
Definition: Factor.h:32
HasDerivedValueType< CONTAINER > push_back(const CONTAINER &container)
Push back non-pointer objects in a container (factors are copied).
Definition: FactorGraph.h:240
IsDerived< DERIVEDFACTOR > push_back(const DERIVEDFACTOR &factor)
Definition: FactorGraph.h:183
const sharedFactor operator[](size_t i) const
Definition: FactorGraph.h:325
void addFactorsToGraph(FactorGraph< FactorType > *graph) const
sharedFactor back() const
Definition: FactorGraph.h:342
GaussianFactorGraph factors(list_of(factor1)(factor2)(factor3))
typename std::enable_if< std::is_base_of< FactorType, DERIVEDFACTOR >::value >::type IsDerived
Check if a DERIVEDFACTOR is in fact derived from FactorType.
Definition: FactorGraph.h:111
#define GTSAM_CONCEPT_TESTABLE_TYPE(T)
Definition: Testable.h:175
FastVector< sharedFactor >::iterator iterator
Definition: FactorGraph.h:100
IsDerived< DERIVEDFACTOR > push_back(boost::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:166
IsDerived< DERIVEDFACTOR > add(boost::shared_ptr< DERIVEDFACTOR > factor)
add is a synonym for push_back.
Definition: FactorGraph.h:190
const_iterator begin() const
Definition: FactorGraph.h:333
FACTOR FactorType
factor type
Definition: FactorGraph.h:96
static const KeyFormatter DefaultKeyFormatter
Definition: Key.h:43
const KeyFormatter & formatter
Array33i a
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&...args)
Emplace a shared pointer to factor of given type.
Definition: FactorGraph.h:172
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
sharedFactor value_type
Definition: FactorGraph.h:99
constexpr int first(int i)
Implementation details for constexpr functions.
STL compatible allocator to use with types requiring a non standrad alignment.
Definition: Memory.h:721
sharedFactor & at(size_t i)
Definition: FactorGraph.h:320
FactorGraph(const CONTAINER &factors)
Definition: FactorGraph.h:144
boost::shared_ptr< FACTOR > sharedFactor
Shared pointer to a factor.
Definition: FactorGraph.h:98
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
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
iterator begin()
Definition: FactorGraph.h:349
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
const_iterator end() const
Definition: FactorGraph.h:336
void operator()(const A &a)
Definition: FactorGraph.h:83
sharedFactor & operator[](size_t i)
Definition: FactorGraph.h:330
Matrix< Scalar, Dynamic, Dynamic > C
Definition: bench_gemm.cpp:37
A thin wrapper around std::vector that uses a custom allocator.
traits
Definition: chartTesting.h:28
const sharedFactor at(size_t i) const
Definition: FactorGraph.h:315
typename std::enable_if< std::is_base_of< FactorType, typename T::value_type >::value >::type HasDerivedValueType
Check if T has a value_type derived from FactorType.
Definition: FactorGraph.h:116
bool empty() const
Definition: FactorGraph.h:310
FastVector< sharedFactor >::const_iterator const_iterator
Definition: FactorGraph.h:101
iterator erase(iterator first, iterator last)
Definition: FactorGraph.h:371
HasDerivedElementType< CONTAINER > push_back(const CONTAINER &container)
Definition: FactorGraph.h:234
std::enable_if< std::is_base_of< This, typename CLIQUE::FactorGraphType >::value >::type push_back(const BayesTree< CLIQUE > &bayesTree)
Definition: FactorGraph.h:276
FactorGraph< FACTOR > This
Typedef for this class.
Definition: FactorGraph.h:104
sharedFactor front() const
Definition: FactorGraph.h:339
void serialize(ARCHIVE &ar, const unsigned int)
Definition: FactorGraph.h:399
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:199
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
void resize(size_t size)
Definition: FactorGraph.h:358
void reserve(size_t size)
Definition: FactorGraph.h:162
typename std::enable_if< std::is_base_of< FactorType, typename T::value_type::element_type >::value >::type HasDerivedElementType
Check if T has a value_type derived from FactorType.
Definition: FactorGraph.h:121
const G double tol
Definition: Group.h:83
const KeyVector keys
bool exists(size_t idx) const
Definition: FactorGraph.h:393
iterator erase(iterator item)
Definition: FactorGraph.h:368


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:03