gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 <boost/optional.hpp>
28 #include <boost/pool/pool_alloc.hpp>
29 #include <boost/ptr_container/ptr_map.hpp>
30 #include <boost/iterator/transform_iterator.hpp>
31 #include <boost/iterator/filter_iterator.hpp>
32 #include <boost/function.hpp>
33 #ifdef __GNUC__
34 #pragma GCC diagnostic push
35 #pragma GCC diagnostic ignored "-Wunused-variable"
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 #include <boost/iterator_adaptors.hpp>
43 
44 #include <string>
45 #include <utility>
46 
47 #include <gtsam/base/Value.h>
48 #include <gtsam/base/FastMap.h>
49 #include <gtsam/inference/Key.h>
50 
51 namespace gtsam {
52 
53  // Forward declarations / utilities
54  class VectorValues;
55  class ValueAutomaticCasting;
56  template<typename T> static bool _truePredicate(const T&) { return true; }
57 
58  /* ************************************************************************* */
59  class GTSAM_EXPORT ValueCloneAllocator {
60  public:
61  static Value* allocate_clone(const Value& a) { return a.clone_(); }
62  static void deallocate_clone(const Value* a) { a->deallocate_(); }
64  };
65 
75  class GTSAM_EXPORT Values {
76 
77  private:
78 
79  // Internally we store a boost ptr_map, with a ValueCloneAllocator (defined
80  // below) to clone and deallocate the Value objects, and a boost
81  // fast_pool_allocator to allocate map nodes. In this way, all memory is
82  // allocated in a boost memory pool.
83  typedef boost::ptr_map<
84  Key,
85  Value,
86  std::less<Key>,
88  boost::fast_pool_allocator<std::pair<const Key, void*> > > KeyValueMap;
89 
90  // The member to store the values, see just above
91  KeyValueMap values_;
92 
93  // Types obtained by iterating
94  typedef KeyValueMap::const_iterator::value_type ConstKeyValuePtrPair;
95  typedef KeyValueMap::iterator::value_type KeyValuePtrPair;
96 
97  public:
98 
100  typedef boost::shared_ptr<Values> shared_ptr;
101 
103  typedef boost::shared_ptr<const Values> const_shared_ptr;
104 
106  struct GTSAM_EXPORT KeyValuePair {
107  const Key key;
109 
110  KeyValuePair(Key _key, Value& _value) : key(_key), value(_value) {}
111  };
112 
114  struct GTSAM_EXPORT ConstKeyValuePair {
115  const Key key;
116  const Value& value;
117 
118  ConstKeyValuePair(Key _key, const Value& _value) : key(_key), value(_value) {}
119  ConstKeyValuePair(const KeyValuePair& kv) : key(kv.key), value(kv.value) {}
120  };
121 
123  typedef boost::transform_iterator<
124  boost::function1<KeyValuePair, const KeyValuePtrPair&>, KeyValueMap::iterator> iterator;
125 
127  typedef boost::transform_iterator<
128  boost::function1<ConstKeyValuePair, const ConstKeyValuePtrPair&>, KeyValueMap::const_iterator> const_iterator;
129 
131  typedef boost::transform_iterator<
132  boost::function1<KeyValuePair, const KeyValuePtrPair&>, KeyValueMap::reverse_iterator> reverse_iterator;
133 
135  typedef boost::transform_iterator<
136  boost::function1<ConstKeyValuePair, const ConstKeyValuePtrPair&>, KeyValueMap::const_reverse_iterator> const_reverse_iterator;
137 
138  typedef KeyValuePair value_type;
139 
141  template<class ValueType = Value>
142  class Filtered;
143 
145  template<class ValueType = Value>
146  class ConstFiltered;
147 
149  Values() {}
150 
152  Values(const Values& other);
153 
155  template<class ValueType>
156  Values(const Filtered<ValueType>& view);
157 
159  template<class ValueType>
160  Values(const ConstFiltered<ValueType>& view);
161 
164 
166  void print(const std::string& str = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
167 
169  bool equals(const Values& other, double tol=1e-9) const;
170 
172 
180  template<typename ValueType>
181  const ValueType& at(Key j) const;
182 
188  const Value& at(Key j) const;
189 
193  bool exists(Key j) const;
194 
199  template<typename ValueType>
200  boost::optional<const ValueType&> exists(Key j) const;
201 
204  iterator find(Key j) { return boost::make_transform_iterator(values_.find(j), &make_deref_pair); }
205 
208  const_iterator find(Key j) const { return boost::make_transform_iterator(values_.find(j), &make_const_deref_pair); }
209 
211  iterator lower_bound(Key j) { return boost::make_transform_iterator(values_.lower_bound(j), &make_deref_pair); }
212 
214  const_iterator lower_bound(Key j) const { return boost::make_transform_iterator(values_.lower_bound(j), &make_const_deref_pair); }
215 
217  iterator upper_bound(Key j) { return boost::make_transform_iterator(values_.upper_bound(j), &make_deref_pair); }
218 
220  const_iterator upper_bound(Key j) const { return boost::make_transform_iterator(values_.upper_bound(j), &make_const_deref_pair); }
221 
223  size_t size() const { return values_.size(); }
224 
226  bool empty() const { return values_.empty(); }
227 
228  const_iterator begin() const { return boost::make_transform_iterator(values_.begin(), &make_const_deref_pair); }
229  const_iterator end() const { return boost::make_transform_iterator(values_.end(), &make_const_deref_pair); }
230  iterator begin() { return boost::make_transform_iterator(values_.begin(), &make_deref_pair); }
231  iterator end() { return boost::make_transform_iterator(values_.end(), &make_deref_pair); }
232  const_reverse_iterator rbegin() const { return boost::make_transform_iterator(values_.rbegin(), &make_const_deref_pair); }
233  const_reverse_iterator rend() const { return boost::make_transform_iterator(values_.rend(), &make_const_deref_pair); }
234  reverse_iterator rbegin() { return boost::make_transform_iterator(values_.rbegin(), &make_deref_pair); }
235  reverse_iterator rend() { return boost::make_transform_iterator(values_.rend(), &make_deref_pair); }
236 
239 
241  Values retract(const VectorValues& delta) const;
242 
244  VectorValues localCoordinates(const Values& cp) const;
245 
247 
249  void insert(Key j, const Value& val);
250 
252  void insert(const Values& values);
253 
258  std::pair<iterator, bool> tryInsert(Key j, const Value& value);
259 
261  void update(Key j, const Value& val);
262 
264  void update(const Values& values);
265 
267  void erase(Key j);
268 
273  KeyList keys() const;
274 
276  Values& operator=(const Values& rhs);
277 
279  void swap(Values& other) { values_.swap(other.values_); }
280 
282  void clear() { values_.clear(); }
283 
285  size_t dim() const;
286 
288  VectorValues zeroVectors() const;
289 
303  Filtered<Value>
304  filter(const boost::function<bool(Key)>& filterFcn);
305 
325  template<class ValueType>
326  Filtered<ValueType>
327  filter(const boost::function<bool(Key)>& filterFcn = &_truePredicate<Key>);
328 
342  ConstFiltered<Value>
343  filter(const boost::function<bool(Key)>& filterFcn) const;
344 
363  template<class ValueType>
364  ConstFiltered<ValueType>
365  filter(const boost::function<bool(Key)>& filterFcn = &_truePredicate<Key>) const;
366 
367  private:
368  // Filters based on ValueType (if not Value) and also based on the user-
369  // supplied \c filter function.
370  template<class ValueType>
371  static bool filterHelper(const boost::function<bool(Key)> filter, const ConstKeyValuePair& key_value) {
372  // Filter and check the type
373  return filter(key_value.key) && (typeid(ValueType) == typeid(key_value.value) || typeid(ValueType) == typeid(Value));
374  }
375 
376  // Cast to the derived ValueType
377  template<class ValueType, class CastedKeyValuePairType, class KeyValuePairType>
378  static CastedKeyValuePairType castHelper(KeyValuePairType key_value) {
379  // Static cast because we already checked the type during filtering
380  return CastedKeyValuePairType(key_value.key, static_cast<ValueType&>(key_value.value));
381  }
382 
384  friend class boost::serialization::access;
385  template<class ARCHIVE>
386  void serialize(ARCHIVE & ar, const unsigned int version) {
387  ar & BOOST_SERIALIZATION_NVP(values_);
388  }
389 
390  static ConstKeyValuePair make_const_deref_pair(const KeyValueMap::const_iterator::value_type& key_value) {
391  return ConstKeyValuePair(key_value.first, *key_value.second); }
392 
393  static KeyValuePair make_deref_pair(const KeyValueMap::iterator::value_type& key_value) {
394  return KeyValuePair(key_value.first, *key_value.second); }
395 
396  };
397 
398  /* ************************************************************************* */
399  class GTSAM_EXPORT ValuesKeyAlreadyExists : public std::exception {
400  protected:
401  const Key key_;
402 
403  private:
404  mutable std::string message_;
405 
406  public:
409  key_(key) {}
410 
411  virtual ~ValuesKeyAlreadyExists() throw() {}
412 
414  Key key() const throw() { return key_; }
415 
417  virtual const char* what() const throw();
418  };
419 
420  /* ************************************************************************* */
421  class GTSAM_EXPORT ValuesKeyDoesNotExist : public std::exception {
422  protected:
423  const char* operation_;
424  const Key key_;
425 
426  private:
427  mutable std::string message_;
428 
429  public:
431  ValuesKeyDoesNotExist(const char* operation, Key key) throw() :
432  operation_(operation), key_(key) {}
433 
434  virtual ~ValuesKeyDoesNotExist() throw() {}
435 
437  Key key() const throw() { return key_; }
438 
440  virtual const char* what() const throw();
441  };
442 
443  /* ************************************************************************* */
444  class GTSAM_EXPORT ValuesIncorrectType : public std::exception {
445  protected:
446  const Key key_;
447  const std::type_info& storedTypeId_;
448  const std::type_info& requestedTypeId_;
449 
450  private:
451  mutable std::string message_;
452 
453  public:
456  const std::type_info& storedTypeId, const std::type_info& requestedTypeId) throw() :
457  key_(key), storedTypeId_(storedTypeId), requestedTypeId_(requestedTypeId) {}
458 
459  virtual ~ValuesIncorrectType() throw() {}
460 
462  Key key() const throw() { return key_; }
463 
465  const std::type_info& storedTypeId() const { return storedTypeId_; }
466 
468  const std::type_info& requestedTypeId() const { return requestedTypeId_; }
469 
471  virtual const char* what() const throw();
472  };
473 
474  /* ************************************************************************* */
475  class GTSAM_EXPORT DynamicValuesMismatched : public std::exception {
476 
477  public:
478  DynamicValuesMismatched() throw() {}
479 
480  virtual ~DynamicValuesMismatched() throw() {}
481 
482  virtual const char* what() const throw() {
483  return "The Values 'this' and the argument passed to Values::localCoordinates have mismatched keys and values";
484  }
485  };
486 
487 }
488 
489 #include <gtsam/nonlinear/Values-inl.h>
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:208
ValuesKeyDoesNotExist(const char *operation, Key key)
Construct with the key that does not exist in the values.
Definition: Values.h:431
Key key() const
The key that was attempted to be accessed that does not exist.
Definition: Values.h:462
const Key key_
The key that does not exist.
Definition: Values.h:424
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:455
A filtered view of a const Values, returned from Values::filter.
Definition: Values-inl.h:125
virtual Value * clone_() const =0
Clone this value in a special memory pool, must be deleted with Value::deallocate_, not with the 'delete' operator.
const std::type_info & storedTypeId() const
The typeid of the value stores in the Values.
Definition: Values.h:465
void clear()
Remove all variables from the config.
Definition: Values.h:282
const Key key_
The key that already existed.
Definition: Values.h:401
This is the interface class for any value that may be used as a variable assignment in a factor graph...
Definition: Value.h:81
const Key key_
The key requested.
Definition: Values.h:446
const char * operation_
The operation that attempted to access the key.
Definition: Values.h:423
Definition: Values.h:421
Definition: Values.h:444
const Value & value
The value.
Definition: Values.h:116
boost::shared_ptr< const Values > const_shared_ptr
A const shared_ptr to this class.
Definition: Values.h:103
Value & value
The value.
Definition: Values.h:108
boost::shared_ptr< Values > shared_ptr
A shared_ptr to this class.
Definition: Values.h:100
size_t dim(const Vector &v)
dimensionality == size
Definition: Vector.h:90
Definition: Values.h:399
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:75
void print(const Matrix &A, const string &s, ostream &stream)
print a matrix
Definition: Matrix.cpp:183
A thin wrapper around std::map that uses boost's fast_pool_allocator.
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:136
Key key() const
The key that was attempted to be accessed that does not exist.
Definition: Values.h:437
Key key() const
The duplicate key that was attempted to be added.
Definition: Values.h:414
const_iterator lower_bound(Key j) const
Find the element greater than or equal to the specified key.
Definition: Values.h:214
iterator lower_bound(Key j)
Find the element greater than or equal to the specified key.
Definition: Values.h:211
size_t size() const
The number of variables in this config.
Definition: Values.h:223
virtual void deallocate_() const =0
Deallocate a raw pointer of this value.
iterator find(Key j)
Find an element by key, returning an iterator, or end() if the key was not found. ...
Definition: Values.h:204
Template to create a binary predicate.
Definition: Testable.h:102
boost::transform_iterator< boost::function1< KeyValuePair, const KeyValuePtrPair & >, KeyValueMap::reverse_iterator > reverse_iterator
Mutable reverse iterator, with value type KeyValuePair.
Definition: Values.h:132
ValuesKeyAlreadyExists(Key key)
Construct with the key-value pair attempted to be added.
Definition: Values.h:408
size_t Key
Integer nonlinear key type.
Definition: types.h:59
The interface class for any variable that can be optimized or used in a factor.
A filtered view of a Values, returned from Values::filter.
Definition: Values-inl.h:57
const Key key
The key.
Definition: Values.h:115
This class represents a collection of vector-valued variables associated each with a unique integer i...
Definition: VectorValues.h:89
const_iterator upper_bound(Key j) const
Find the lowest-ordered element greater than the specified key.
Definition: Values.h:220
A key-value pair, which you get by dereferencing iterators.
Definition: Values.h:114
Definition: Values.h:475
FastList< Key > KeyList
Useful typedefs for operations with Values - allow for matlab interfaces.
Definition: Key.h:44
Definition: Values.h:59
Values()
Default constructor creates an empty Values class.
Definition: Values.h:149
void swap(Values &other)
Swap the contents of two Values without copying data.
Definition: Values.h:279
const std::type_info & requestedTypeId() const
The requested typeid.
Definition: Values.h:468
A key-value pair, which you get by dereferencing iterators.
Definition: Values.h:106
Vector delta(size_t n, size_t i, double value)
Create basis vector of dimension n, with a constant in spot i.
Definition: Vector.cpp:53
boost::transform_iterator< boost::function1< ConstKeyValuePair, const ConstKeyValuePtrPair & >, KeyValueMap::const_iterator > const_iterator
Const forward iterator, with value type ConstKeyValuePair.
Definition: Values.h:128
iterator upper_bound(Key j)
Find the lowest-ordered element greater than the specified key.
Definition: Values.h:217
boost::transform_iterator< boost::function1< KeyValuePair, const KeyValuePtrPair & >, KeyValueMap::iterator > iterator
Mutable forward iterator, with value type KeyValuePair.
Definition: Values.h:124
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: types.h:62
bool empty() const
whether the config is empty
Definition: Values.h:226
const Key key
The key.
Definition: Values.h:107