gtsam  4.0.0
gtsam
Values.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 
25 #pragma once
26 
27 #include <gtsam/base/GenericValue.h>
28 #include <gtsam/base/VectorSpace.h>
29 #include <gtsam/inference/Key.h>
30 #include <boost/iterator/transform_iterator.hpp>
31 #include <boost/iterator/filter_iterator.hpp>
32 #ifdef __GNUC__
33 #pragma GCC diagnostic push
34 #pragma GCC diagnostic ignored "-Wunused-variable"
35 #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
36 #endif
37 #include <boost/bind.hpp>
38 #ifdef __GNUC__
39 #pragma GCC diagnostic pop
40 #endif
41 #include <boost/ptr_container/serialize_ptr_map.hpp>
42 
43 #include <string>
44 #include <utility>
45 
46 namespace gtsam {
47 
48  // Forward declarations / utilities
49  class VectorValues;
50  class ValueAutomaticCasting;
51  template<typename T> static bool _truePredicate(const T&) { return true; }
52 
53  /* ************************************************************************* */
54  class GTSAM_EXPORT ValueCloneAllocator {
55  public:
56  static Value* allocate_clone(const Value& a) { return a.clone_(); }
57  static void deallocate_clone(const Value* a) { a->deallocate_(); }
59  };
60 
70  class GTSAM_EXPORT Values {
71 
72  private:
73 
74  // Internally we store a boost ptr_map, with a ValueCloneAllocator (defined
75  // below) to clone and deallocate the Value objects, and a boost
76  // fast_pool_allocator to allocate map nodes. In this way, all memory is
77  // allocated in a boost memory pool.
78  typedef boost::ptr_map<
79  Key,
80  Value,
81  std::less<Key>,
83  boost::fast_pool_allocator<std::pair<const Key, void*> > > KeyValueMap;
84 
85  // The member to store the values, see just above
86  KeyValueMap values_;
87 
88  // Types obtained by iterating
89  typedef KeyValueMap::const_iterator::value_type ConstKeyValuePtrPair;
90  typedef KeyValueMap::iterator::value_type KeyValuePtrPair;
91 
92  public:
93 
95  typedef boost::shared_ptr<Values> shared_ptr;
96 
98  typedef boost::shared_ptr<const Values> const_shared_ptr;
99 
101  struct GTSAM_EXPORT KeyValuePair {
102  const Key key;
103  Value& value;
104 
105  KeyValuePair(Key _key, Value& _value) : key(_key), value(_value) {}
106  };
107 
109  struct GTSAM_EXPORT ConstKeyValuePair {
110  const Key key;
111  const Value& value;
112 
113  ConstKeyValuePair(Key _key, const Value& _value) : key(_key), value(_value) {}
114  ConstKeyValuePair(const KeyValuePair& kv) : key(kv.key), value(kv.value) {}
115  };
116 
118  typedef boost::transform_iterator<
119  boost::function1<KeyValuePair, const KeyValuePtrPair&>, KeyValueMap::iterator> iterator;
120 
122  typedef boost::transform_iterator<
123  boost::function1<ConstKeyValuePair, const ConstKeyValuePtrPair&>, KeyValueMap::const_iterator> const_iterator;
124 
126  typedef boost::transform_iterator<
127  boost::function1<KeyValuePair, const KeyValuePtrPair&>, KeyValueMap::reverse_iterator> reverse_iterator;
128 
130  typedef boost::transform_iterator<
131  boost::function1<ConstKeyValuePair, const ConstKeyValuePtrPair&>, KeyValueMap::const_reverse_iterator> const_reverse_iterator;
132 
133  typedef KeyValuePair value_type;
134 
136  template<class ValueType = Value>
137  class Filtered;
138 
140  template<class ValueType = Value>
141  class ConstFiltered;
142 
144  Values() {}
145 
147  Values(const Values& other);
148 
150  Values(Values&& other);
151 
153  Values(const Values& other, const VectorValues& delta);
154 
156  template<class ValueType>
157  Values(const Filtered<ValueType>& view);
158 
160  template<class ValueType>
161  Values(const ConstFiltered<ValueType>& view);
162 
165 
167  void print(const std::string& str = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
168 
170  bool equals(const Values& other, double tol=1e-9) const;
171 
173 
182  template<typename ValueType>
183  ValueType at(Key j) const;
184 
186  double atDouble(size_t key) const { return at<double>(key);}
187 
193  const Value& at(Key j) const;
194 
198  bool exists(Key j) const;
199 
204  template<typename ValueType>
205  boost::optional<const ValueType&> exists(Key j) const;
206 
209  iterator find(Key j) { return boost::make_transform_iterator(values_.find(j), &make_deref_pair); }
210 
213  const_iterator find(Key j) const { return boost::make_transform_iterator(values_.find(j), &make_const_deref_pair); }
214 
216  iterator lower_bound(Key j) { return boost::make_transform_iterator(values_.lower_bound(j), &make_deref_pair); }
217 
219  const_iterator lower_bound(Key j) const { return boost::make_transform_iterator(values_.lower_bound(j), &make_const_deref_pair); }
220 
222  iterator upper_bound(Key j) { return boost::make_transform_iterator(values_.upper_bound(j), &make_deref_pair); }
223 
225  const_iterator upper_bound(Key j) const { return boost::make_transform_iterator(values_.upper_bound(j), &make_const_deref_pair); }
226 
228  size_t size() const { return values_.size(); }
229 
231  bool empty() const { return values_.empty(); }
232 
233  const_iterator begin() const { return boost::make_transform_iterator(values_.begin(), &make_const_deref_pair); }
234  const_iterator end() const { return boost::make_transform_iterator(values_.end(), &make_const_deref_pair); }
235  iterator begin() { return boost::make_transform_iterator(values_.begin(), &make_deref_pair); }
236  iterator end() { return boost::make_transform_iterator(values_.end(), &make_deref_pair); }
237  const_reverse_iterator rbegin() const { return boost::make_transform_iterator(values_.rbegin(), &make_const_deref_pair); }
238  const_reverse_iterator rend() const { return boost::make_transform_iterator(values_.rend(), &make_const_deref_pair); }
239  reverse_iterator rbegin() { return boost::make_transform_iterator(values_.rbegin(), &make_deref_pair); }
240  reverse_iterator rend() { return boost::make_transform_iterator(values_.rend(), &make_deref_pair); }
241 
244 
246  Values retract(const VectorValues& delta) const;
247 
249  VectorValues localCoordinates(const Values& cp) const;
250 
252 
254  void insert(Key j, const Value& val);
255 
257  void insert(const Values& values);
258 
262  template <typename ValueType>
263  void insert(Key j, const ValueType& val);
264 
266  void insertDouble(Key j, double c) { insert<double>(j,c); }
267 
272  std::pair<iterator, bool> tryInsert(Key j, const Value& value);
273 
275  void update(Key j, const Value& val);
276 
281  template <typename T>
282  void update(Key j, const T& val);
283 
285  void update(const Values& values);
286 
288  void erase(Key j);
289 
294  KeyVector keys() const;
295 
297  Values& operator=(const Values& rhs);
298 
300  void swap(Values& other) { values_.swap(other.values_); }
301 
303  void clear() { values_.clear(); }
304 
306  size_t dim() const;
307 
309  VectorValues zeroVectors() const;
310 
325  filter(const boost::function<bool(Key)>& filterFcn);
326 
346  template<class ValueType>
348  filter(const boost::function<bool(Key)>& filterFcn = &_truePredicate<Key>);
349 
364  filter(const boost::function<bool(Key)>& filterFcn) const;
365 
384  template<class ValueType>
386  filter(const boost::function<bool(Key)>& filterFcn = &_truePredicate<Key>) const;
387 
388  private:
389  // Filters based on ValueType (if not Value) and also based on the user-
390  // supplied \c filter function.
391  template<class ValueType>
392  static bool filterHelper(const boost::function<bool(Key)> filter, const ConstKeyValuePair& key_value) {
393  BOOST_STATIC_ASSERT((!boost::is_same<ValueType, Value>::value));
394  // Filter and check the type
395  return filter(key_value.key) && (dynamic_cast<const GenericValue<ValueType>*>(&key_value.value));
396  }
397 
399  friend class boost::serialization::access;
400  template<class ARCHIVE>
401  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
402  ar & BOOST_SERIALIZATION_NVP(values_);
403  }
404 
405  static ConstKeyValuePair make_const_deref_pair(const KeyValueMap::const_iterator::value_type& key_value) {
406  return ConstKeyValuePair(key_value.first, *key_value.second); }
407 
408  static KeyValuePair make_deref_pair(const KeyValueMap::iterator::value_type& key_value) {
409  return KeyValuePair(key_value.first, *key_value.second); }
410 
411  };
412 
413  /* ************************************************************************* */
414  class GTSAM_EXPORT ValuesKeyAlreadyExists : public std::exception {
415  protected:
416  const Key key_;
417 
418  private:
419  mutable std::string message_;
420 
421  public:
424  key_(key) {}
425 
426  virtual ~ValuesKeyAlreadyExists() throw() {}
427 
429  Key key() const throw() { return key_; }
430 
432  virtual const char* what() const throw();
433  };
434 
435  /* ************************************************************************* */
436  class GTSAM_EXPORT ValuesKeyDoesNotExist : public std::exception {
437  protected:
438  const char* operation_;
439  const Key key_;
440 
441  private:
442  mutable std::string message_;
443 
444  public:
446  ValuesKeyDoesNotExist(const char* operation, Key key) throw() :
447  operation_(operation), key_(key) {}
448 
449  virtual ~ValuesKeyDoesNotExist() throw() {}
450 
452  Key key() const throw() { return key_; }
453 
455  virtual const char* what() const throw();
456  };
457 
458  /* ************************************************************************* */
459  class GTSAM_EXPORT ValuesIncorrectType : public std::exception {
460  protected:
461  const Key key_;
462  const std::type_info& storedTypeId_;
463  const std::type_info& requestedTypeId_;
464 
465  private:
466  mutable std::string message_;
467 
468  public:
471  const std::type_info& storedTypeId, const std::type_info& requestedTypeId) throw() :
472  key_(key), storedTypeId_(storedTypeId), requestedTypeId_(requestedTypeId) {}
473 
474  virtual ~ValuesIncorrectType() throw() {}
475 
477  Key key() const throw() { return key_; }
478 
480  const std::type_info& storedTypeId() const { return storedTypeId_; }
481 
483  const std::type_info& requestedTypeId() const { return requestedTypeId_; }
484 
486  virtual const char* what() const throw();
487  };
488 
489  /* ************************************************************************* */
490  class GTSAM_EXPORT DynamicValuesMismatched : public std::exception {
491 
492  public:
493  DynamicValuesMismatched() throw() {}
494 
495  virtual ~DynamicValuesMismatched() throw() {}
496 
497  virtual const char* what() const throw() {
498  return "The Values 'this' and the argument passed to Values::localCoordinates have mismatched keys and values";
499  }
500  };
501 
502  /* ************************************************************************* */
503  class GTSAM_EXPORT NoMatchFoundForFixed: public std::exception {
504 
505  protected:
506  const size_t M1_, N1_;
507  const size_t M2_, N2_;
508 
509  private:
510  mutable std::string message_;
511 
512  public:
513  NoMatchFoundForFixed(size_t M1, size_t N1, size_t M2, size_t N2) throw () :
514  M1_(M1), N1_(N1), M2_(M2), N2_(N2) {
515  }
516 
517  virtual ~NoMatchFoundForFixed() throw () {
518  }
519 
520  virtual const char* what() const throw ();
521  };
522 
523  /* ************************************************************************* */
525  template<>
526  struct traits<Values> : public Testable<Values> {
527  };
528 
529 } //\ namespace gtsam
530 
531 
532 #include <gtsam/nonlinear/Values-inl.h>
Definition: Values.h:436
Definition: Values.h:459
const char * operation_
The operation that attempted to access the key.
Definition: Values.h:438
A filtered view of a Values, returned from Values::filter.
Definition: Values-inl.h:94
const Key key_
The key that already existed.
Definition: Values.h:416
const_iterator lower_bound(Key j) const
Find the element greater than or equal to the specified key.
Definition: Values.h:219
Value & value
The value.
Definition: Values.h:103
iterator find(Key j)
Find an element by key, returning an iterator, or end() if the key was not found. ...
Definition: Values.h:209
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:140
boost::transform_iterator< boost::function1< KeyValuePair, const KeyValuePtrPair & >, KeyValueMap::reverse_iterator > reverse_iterator
Mutable reverse iterator, with value type KeyValuePair.
Definition: Values.h:127
const Key key_
The key that does not exist.
Definition: Values.h:439
const Key key
The key.
Definition: Values.h:110
Wraps any type T so it can play as a Value.
Definition: GenericValue.h:38
Definition: Values.h:414
const_iterator upper_bound(Key j) const
Find the lowest-ordered element greater than the specified key.
Definition: Values.h:225
boost::transform_iterator< boost::function1< ConstKeyValuePair, const ConstKeyValuePtrPair & >, KeyValueMap::const_iterator > const_iterator
Const forward iterator, with value type ConstKeyValuePair.
Definition: Values.h:123
virtual void deallocate_() const =0
Deallocate a raw pointer of this value.
A filtered view of a const Values, returned from Values::filter.
Definition: Values-inl.h:168
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:70
boost::shared_ptr< const Values > const_shared_ptr
A const shared_ptr to this class.
Definition: Values.h:98
Key key() const
The key that was attempted to be accessed that does not exist.
Definition: Values.h:452
size_t size() const
The number of variables in this config.
Definition: Values.h:228
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
const std::type_info & requestedTypeId() const
The requested typeid.
Definition: Values.h:483
double atDouble(size_t key) const
version for double
Definition: Values.h:186
void insertDouble(Key j, double c)
version for double
Definition: Values.h:266
Template to create a binary predicate.
Definition: Testable.h:110
virtual Value * clone_() const =0
Clone this value in a special memory pool, must be deleted with Value::deallocate_, not with the &#39;delete&#39; operator.
const_iterator find(Key j) const
Find an element by key, returning an iterator, or end() if the key was not found. ...
Definition: Values.h:213
ValuesKeyAlreadyExists(Key key)
Construct with the key-value pair attempted to be added.
Definition: Values.h:423
Definition: Values.h:54
void swap(Values &other)
Swap the contents of two Values without copying data.
Definition: Values.h:300
This class represents a collection of vector-valued variables associated each with a unique integer i...
Definition: VectorValues.h:90
Values()
Default constructor creates an empty Values class.
Definition: Values.h:144
iterator upper_bound(Key j)
Find the lowest-ordered element greater than the specified key.
Definition: Values.h:222
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
ValuesIncorrectType(Key key, const std::type_info &storedTypeId, const std::type_info &requestedTypeId)
Construct with the key that does not exist in the values.
Definition: Values.h:470
Key key() const
The duplicate key that was attempted to be added.
Definition: Values.h:429
const Key key_
The key requested.
Definition: Values.h:461
const std::type_info & storedTypeId() const
The typeid of the value stores in the Values.
Definition: Values.h:480
A key-value pair, which you get by dereferencing iterators.
Definition: Values.h:101
Key key() const
The key that was attempted to be accessed that does not exist.
Definition: Values.h:477
const Value & value
The value.
Definition: Values.h:111
ValuesKeyDoesNotExist(const char *operation, Key key)
Construct with the key that does not exist in the values.
Definition: Values.h:446
iterator lower_bound(Key j)
Find the element greater than or equal to the specified key.
Definition: Values.h:216
boost::shared_ptr< Values > shared_ptr
A shared_ptr to this class.
Definition: Values.h:95
boost::transform_iterator< boost::function1< ConstKeyValuePair, const ConstKeyValuePtrPair & >, KeyValueMap::const_reverse_iterator > const_reverse_iterator
Const reverse iterator, with value type ConstKeyValuePair.
Definition: Values.h:131
void clear()
Remove all variables from the config.
Definition: Values.h:303
Definition: Values.h:503
const Key key
The key.
Definition: Values.h:102
This is the interface class for any value that may be used as a variable assignment in a factor graph...
Definition: Value.h:83
Definition: Values.h:490
boost::transform_iterator< boost::function1< KeyValuePair, const KeyValuePtrPair & >, KeyValueMap::iterator > iterator
Mutable forward iterator, with value type KeyValuePair.
Definition: Values.h:119
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
A key-value pair, which you get by dereferencing iterators.
Definition: Values.h:109
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
bool empty() const
whether the config is empty
Definition: Values.h:231