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;
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 
324  Filtered<Value>
325  filter(const boost::function<bool(Key)>& filterFcn);
326 
346  template<class ValueType>
347  Filtered<ValueType>
348  filter(const boost::function<bool(Key)>& filterFcn = &_truePredicate<Key>);
349 
363  ConstFiltered<Value>
364  filter(const boost::function<bool(Key)>& filterFcn) const;
365 
384  template<class ValueType>
385  ConstFiltered<ValueType>
386  filter(const boost::function<bool(Key)>& filterFcn = &_truePredicate<Key>) const;
387 
388  // Count values of given type \c ValueType
389  template<class ValueType>
390  size_t count() const {
391  size_t i = 0;
392  for (const auto& key_value : *this) {
393  if (dynamic_cast<const GenericValue<ValueType>*>(&key_value.value))
394  ++i;
395  }
396  return i;
397  }
398 
399  private:
400  // Filters based on ValueType (if not Value) and also based on the user-
401  // supplied \c filter function.
402  template<class ValueType>
403  static bool filterHelper(const boost::function<bool(Key)> filter, const ConstKeyValuePair& key_value) {
404  BOOST_STATIC_ASSERT((!boost::is_same<ValueType, Value>::value));
405  // Filter and check the type
406  return filter(key_value.key) && (dynamic_cast<const GenericValue<ValueType>*>(&key_value.value));
407  }
408 
410  friend class boost::serialization::access;
411  template<class ARCHIVE>
412  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
413  ar & BOOST_SERIALIZATION_NVP(values_);
414  }
415 
416  static ConstKeyValuePair make_const_deref_pair(const KeyValueMap::const_iterator::value_type& key_value) {
417  return ConstKeyValuePair(key_value.first, *key_value.second); }
418 
419  static KeyValuePair make_deref_pair(const KeyValueMap::iterator::value_type& key_value) {
420  return KeyValuePair(key_value.first, *key_value.second); }
421 
422  };
423 
424  /* ************************************************************************* */
425  class GTSAM_EXPORT ValuesKeyAlreadyExists : public std::exception {
426  protected:
427  const Key key_;
428 
429  private:
430  mutable std::string message_;
431 
432  public:
435  key_(key) {}
436 
437  virtual ~ValuesKeyAlreadyExists() throw() {}
438 
440  Key key() const throw() { return key_; }
441 
443  virtual const char* what() const throw();
444  };
445 
446  /* ************************************************************************* */
447  class GTSAM_EXPORT ValuesKeyDoesNotExist : public std::exception {
448  protected:
449  const char* operation_;
450  const Key key_;
451 
452  private:
453  mutable std::string message_;
454 
455  public:
457  ValuesKeyDoesNotExist(const char* operation, Key key) throw() :
458  operation_(operation), key_(key) {}
459 
460  virtual ~ValuesKeyDoesNotExist() throw() {}
461 
463  Key key() const throw() { return key_; }
464 
466  virtual const char* what() const throw();
467  };
468 
469  /* ************************************************************************* */
470  class GTSAM_EXPORT ValuesIncorrectType : public std::exception {
471  protected:
472  const Key key_;
473  const std::type_info& storedTypeId_;
474  const std::type_info& requestedTypeId_;
475 
476  private:
477  mutable std::string message_;
478 
479  public:
482  const std::type_info& storedTypeId, const std::type_info& requestedTypeId) throw() :
483  key_(key), storedTypeId_(storedTypeId), requestedTypeId_(requestedTypeId) {}
484 
485  virtual ~ValuesIncorrectType() throw() {}
486 
488  Key key() const throw() { return key_; }
489 
491  const std::type_info& storedTypeId() const { return storedTypeId_; }
492 
494  const std::type_info& requestedTypeId() const { return requestedTypeId_; }
495 
497  virtual const char* what() const throw();
498  };
499 
500  /* ************************************************************************* */
501  class GTSAM_EXPORT DynamicValuesMismatched : public std::exception {
502 
503  public:
504  DynamicValuesMismatched() throw() {}
505 
506  virtual ~DynamicValuesMismatched() throw() {}
507 
508  virtual const char* what() const throw() {
509  return "The Values 'this' and the argument passed to Values::localCoordinates have mismatched keys and values";
510  }
511  };
512 
513  /* ************************************************************************* */
514  class GTSAM_EXPORT NoMatchFoundForFixed: public std::exception {
515 
516  protected:
517  const size_t M1_, N1_;
518  const size_t M2_, N2_;
519 
520  private:
521  mutable std::string message_;
522 
523  public:
524  NoMatchFoundForFixed(size_t M1, size_t N1, size_t M2, size_t N2) throw () :
525  M1_(M1), N1_(N1), M2_(M2), N2_(N2) {
526  }
527 
528  virtual ~NoMatchFoundForFixed() throw () {
529  }
530 
531  virtual const char* what() const throw ();
532  };
533 
534  /* ************************************************************************* */
536  template<>
537  struct traits<Values> : public Testable<Values> {
538  };
539 
540 } //\ namespace gtsam
541 
542 
543 #include <gtsam/nonlinear/Values-inl.h>
Key key() const
The key that was attempted to be accessed that does not exist.
Definition: Values.h:488
const Value & value
The value.
Definition: Values.h:111
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:70
virtual void deallocate_() const =0
Deallocate a raw pointer of this value.
boost::transform_iterator< boost::function1< ConstKeyValuePair, const ConstKeyValuePtrPair & >, KeyValueMap::const_iterator > const_iterator
Const forward iterator, with value type ConstKeyValuePair.
Definition: Values.h:123
void insertDouble(Key j, double c)
version for double
Definition: Values.h:266
Values()
Default constructor creates an empty Values class.
Definition: Values.h:144
This is the base class for any type to be stored in Values.
Definition: Value.h:36
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:141
A filtered view of a Values, returned from Values::filter.
Definition: Values-inl.h:93
Definition: Values.h:470
boost::transform_iterator< boost::function1< KeyValuePair, const KeyValuePtrPair & >, KeyValueMap::reverse_iterator > reverse_iterator
Mutable reverse iterator, with value type KeyValuePair.
Definition: Values.h:127
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
ValuesKeyAlreadyExists(Key key)
Construct with the key-value pair attempted to be added.
Definition: Values.h:434
const_iterator upper_bound(Key j) const
Find the lowest-ordered element greater than the specified key.
Definition: Values.h:225
Template to create a binary predicate.
Definition: Testable.h:110
const Key key_
The key that already existed.
Definition: Values.h:427
double atDouble(size_t key) const
version for double
Definition: Values.h:186
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
Value & value
The value.
Definition: Values.h:103
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
boost::shared_ptr< Values > shared_ptr
A shared_ptr to this class.
Definition: Values.h:95
A key-value pair, which you get by dereferencing iterators.
Definition: Values.h:109
void clear()
Remove all variables from the config.
Definition: Values.h:303
Wraps any type T so it can play as a Value.
Definition: GenericValue.h:38
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
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
Definition: Values.h:54
boost::transform_iterator< boost::function1< KeyValuePair, const KeyValuePtrPair & >, KeyValueMap::iterator > iterator
Mutable forward iterator, with value type KeyValuePair.
Definition: Values.h:119
boost::shared_ptr< const Values > const_shared_ptr
A const shared_ptr to this class.
Definition: Values.h:98
A filtered view of a const Values, returned from Values::filter.
Definition: Values-inl.h:167
iterator find(Key j)
Find an element by key, returning an iterator, or end() if the key was not found.
Definition: Values.h:209
This class represents a collection of vector-valued variables associated each with a unique integer i...
Definition: VectorValues.h:73
Definition: Values.h:514
Definition: Values.h:447
const Key key
The key.
Definition: Values.h:102
A key-value pair, which you get by dereferencing iterators.
Definition: Values.h:101
const Key key_
The key requested.
Definition: Values.h:472
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:56
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
const char * operation_
The operation that attempted to access the key.
Definition: Values.h:449
Definition: Values.h:501
Definition: Values.h:425
virtual Value * clone_() const =0
Clone this value in a special memory pool, must be deleted with Value::deallocate_,...
void swap(Values &other)
Swap the contents of two Values without copying data.
Definition: Values.h:300
Key key() const
The duplicate key that was attempted to be added.
Definition: Values.h:440
Key key() const
The key that was attempted to be accessed that does not exist.
Definition: Values.h:463
ValuesKeyDoesNotExist(const char *operation, Key key)
Construct with the key that does not exist in the values.
Definition: Values.h:457
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
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:481
iterator upper_bound(Key j)
Find the lowest-ordered element greater than the specified key.
Definition: Values.h:222
const Key key_
The key that does not exist.
Definition: Values.h:450
const Key key
The key.
Definition: Values.h:110
iterator lower_bound(Key j)
Find the element greater than or equal to the specified key.
Definition: Values.h:216
const std::type_info & storedTypeId() const
The typeid of the value stores in the Values.
Definition: Values.h:491
const std::type_info & requestedTypeId() const
The requested typeid.
Definition: Values.h:494
bool empty() const
whether the config is empty
Definition: Values.h:231
size_t size() const
The number of variables in this config.
Definition: Values.h:228
const_iterator lower_bound(Key j) const
Find the element greater than or equal to the specified key.
Definition: Values.h:219