gtsam
Loading...
Searching...
No Matches
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
19
20// \callgraph
21
22#pragma once
23
24#if GTSAM_ENABLE_BOOST_SERIALIZATION
25#include <boost/serialization/nvp.hpp>
26#endif
28#include <gtsam/base/types.h>
29#include <gtsam/inference/Key.h>
30
31#include <algorithm>
32#include <memory>
33
34namespace gtsam {
35
38 typedef FastSet<FactorIndex> FactorIndexSet;
39
40 class HybridValues; // forward declaration of a Value type for error.
41
70 class GTSAM_EXPORT Factor
71 {
72
73 private:
74 // These typedefs are private because they must be overridden in derived classes.
75 typedef Factor This;
76 typedef std::shared_ptr<Factor> shared_ptr;
77
78 public:
80 typedef KeyVector::iterator iterator;
81
83 typedef KeyVector::const_iterator const_iterator;
84
85 protected:
86
89
92
94 Factor() {}
95
98 template<typename CONTAINER>
99 explicit Factor(const CONTAINER& keys) : keys_(keys.begin(), keys.end()) {}
100
103 template<typename ITERATOR>
104 Factor(ITERATOR first, ITERATOR last) : keys_(first, last) {}
105
108 template<typename CONTAINER>
109 static Factor FromKeys(const CONTAINER& keys) {
110 return Factor(keys.begin(), keys.end()); }
111
114 template<typename ITERATOR>
115 static Factor FromIterators(ITERATOR first, ITERATOR last) {
116 return Factor(first, last); }
117
119
120 public:
122 // public since it is required for boost serialization and static methods.
123 // virtual since it is public.
124 // http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-dtor-virtual
125 virtual ~Factor() = default;
126
129
131 bool empty() const { return keys_.empty(); }
132
134 Key front() const { return keys_.front(); }
135
137 Key back() const { return keys_.back(); }
138
140 const_iterator find(Key key) const { return std::find(begin(), end(), key); }
141
143 const KeyVector& keys() const { return keys_; }
144
146 const_iterator begin() const { return keys_.begin(); }
147
149 const_iterator end() const { return keys_.end(); }
150
155 virtual double error(const HybridValues& hybridValues) const;
156
160 size_t size() const { return keys_.size(); }
161
163
166
168 virtual void print(
169 const std::string& s = "Factor",
170 const KeyFormatter& formatter = DefaultKeyFormatter) const;
171
173 virtual void printKeys(
174 const std::string& s = "Factor",
175 const KeyFormatter& formatter = DefaultKeyFormatter) const;
176
178 bool equals(const This& other, double tol = 1e-9) const;
179
183
185 KeyVector& keys() { return keys_; }
186
188 iterator begin() { return keys_.begin(); }
189
191 iterator end() { return keys_.end(); }
192
194
195 private:
196#if GTSAM_ENABLE_BOOST_SERIALIZATION
199
200 friend class boost::serialization::access;
201 template<class Archive>
202 void serialize(Archive & ar, const unsigned int /*version*/) {
203 ar & BOOST_SERIALIZATION_NVP(keys_);
204 }
205#endif
206
208
209 };
210
211} // \namespace gtsam
A thin wrapper around std::vector that uses a custom allocator.
Typedefs for easier changing of types.
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
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
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
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
FastSet is a thin wrapper around std::set that uses the boost fast_pool_allocator instead of the defa...
Definition FastSet.h:54
Template to create a binary predicate.
Definition Testable.h:112
HybridValues represents a collection of DiscreteValues and VectorValues.
Definition HybridValues.h:37
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition Factor.h:143
KeyVector keys_
The keys involved in this factor.
Definition Factor.h:88
const_iterator find(Key key) const
find
Definition Factor.h:140
const_iterator begin() const
Iterator at beginning of involved variable keys.
Definition Factor.h:146
Factor()
Default constructor for I/O.
Definition Factor.h:94
iterator end()
Iterator at end of involved variable keys.
Definition Factor.h:191
bool empty() const
Whether the factor is empty (involves zero variables).
Definition Factor.h:131
KeyVector & keys()
Definition Factor.h:185
static Factor FromIterators(ITERATOR first, ITERATOR last)
Construct factor from iterator keys.
Definition Factor.h:115
static Factor FromKeys(const CONTAINER &keys)
Construct factor from container of keys.
Definition Factor.h:109
iterator begin()
Iterator at beginning of involved variable keys.
Definition Factor.h:188
virtual ~Factor()=default
Default destructor.
Factor(ITERATOR first, ITERATOR last)
Construct factor from iterator keys.
Definition Factor.h:104
KeyVector::const_iterator const_iterator
Const iterator over keys.
Definition Factor.h:83
const_iterator end() const
Iterator at end of involved variable keys.
Definition Factor.h:149
Factor(const CONTAINER &keys)
Construct factor from container of keys.
Definition Factor.h:99
KeyVector::iterator iterator
Iterator over keys.
Definition Factor.h:80
Key back() const
Last key.
Definition Factor.h:137
Key front() const
First key.
Definition Factor.h:134
size_t size() const
Definition Factor.h:160