gtsam  4.0.0
gtsam
Factor.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 
20 // \callgraph
21 
22 #pragma once
23 
24 #include <boost/serialization/nvp.hpp>
25 
26 #include <gtsam/base/types.h>
27 #include <gtsam/base/FastVector.h>
28 #include <gtsam/inference/Key.h>
29 
30 namespace gtsam {
32 typedef FastVector<FactorIndex> FactorIndices;
34 
54  class GTSAM_EXPORT Factor
55  {
56 
57  private:
58  // These typedefs are private because they must be overridden in derived classes.
59  typedef Factor This;
60  typedef boost::shared_ptr<Factor> shared_ptr;
61 
62  public:
64  typedef KeyVector::iterator iterator;
65 
67  typedef KeyVector::const_iterator const_iterator;
68 
69  protected:
70 
73 
76 
78  Factor() {}
79 
82  template<typename CONTAINER>
83  explicit Factor(const CONTAINER& keys) : keys_(keys.begin(), keys.end()) {}
84 
87  template<typename ITERATOR>
88  Factor(ITERATOR first, ITERATOR last) : keys_(first, last) {}
89 
92  template<typename CONTAINER>
93  static Factor FromKeys(const CONTAINER& keys) {
94  return Factor(keys.begin(), keys.end()); }
95 
98  template<typename ITERATOR>
99  static Factor FromIterators(ITERATOR first, ITERATOR last) {
100  return Factor(first, last); }
101 
103 
104  public:
107 
109  Key front() const { return keys_.front(); }
110 
112  Key back() const { return keys_.back(); }
113 
115  const_iterator find(Key key) const { return std::find(begin(), end(), key); }
116 
118  const KeyVector& keys() const { return keys_; }
119 
121  const_iterator begin() const { return keys_.begin(); }
122 
124  const_iterator end() const { return keys_.end(); }
125 
129  size_t size() const { return keys_.size(); }
130 
132 
133 
136 
138  void print(const std::string& s = "Factor", const KeyFormatter& formatter = DefaultKeyFormatter) const;
139 
141  void printKeys(const std::string& s = "Factor", const KeyFormatter& formatter = DefaultKeyFormatter) const;
142 
143  protected:
145  bool equals(const This& other, double tol = 1e-9) const;
146 
148 
149  public:
152 
154  KeyVector& keys() { return keys_; }
155 
157  iterator begin() { return keys_.begin(); }
158 
160  iterator end() { return keys_.end(); }
161 
162  private:
164  friend class boost::serialization::access;
165  template<class Archive>
166  void serialize(Archive & ar, const unsigned int /*version*/) {
167  ar & BOOST_SERIALIZATION_NVP(keys_);
168  }
169 
171 
172  };
173 
174 }
This is the base class for all factor types.
Definition: Factor.h:54
static Factor FromIterators(ITERATOR first, ITERATOR last)
Construct factor from iterator keys.
Definition: Factor.h:99
A thin wrapper around std::vector that uses a custom allocator.
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:141
const_iterator find(Key key) const
find
Definition: Factor.h:115
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
KeyVector & keys()
Definition: Factor.h:154
Template to create a binary predicate.
Definition: Testable.h:110
Factor(const CONTAINER &keys)
Construct factor from container of keys.
Definition: Factor.h:83
Key front() const
First key.
Definition: Factor.h:109
iterator end()
Iterator at end of involved variable keys.
Definition: Factor.h:160
static Factor FromKeys(const CONTAINER &keys)
Construct factor from container of keys.
Definition: Factor.h:93
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
size_t size() const
Definition: Factor.h:129
iterator begin()
Iterator at beginning of involved variable keys.
Definition: Factor.h:157
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:56
Factor()
Default constructor for I/O.
Definition: Factor.h:78
Factor(ITERATOR first, ITERATOR last)
Construct factor from iterator keys.
Definition: Factor.h:88
FastVector< FactorIndex > FactorIndices
Define collection types:
Definition: Factor.h:32
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition: Factor.h:118
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
KeyVector keys_
The keys involved in this factor.
Definition: Factor.h:72
KeyVector::iterator iterator
Iterator over keys.
Definition: Factor.h:64
KeyVector::const_iterator const_iterator
Const iterator over keys.
Definition: Factor.h:67
Key back() const
Last key.
Definition: Factor.h:112
const_iterator begin() const
Iterator at beginning of involved variable keys.
Definition: Factor.h:121
const_iterator end() const
Iterator at end of involved variable keys.
Definition: Factor.h:124
Definition: FastSet.h:45
Typedefs for easier changing of types.