GTSAM  4.0.2
C++ library for smoothing and mapping (SAM)
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/inference/Key.h>
29 #include <gtsam/base/GenericValue.h>
30 #include <gtsam/base/VectorSpace.h>
31 
32 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
33 #include <boost/serialization/unique_ptr.hpp>
34 #endif
35 
36 
37 #include <memory>
38 #include <string>
39 #include <utility>
40 
41 namespace gtsam {
42 
43  // Forward declarations / utilities
44  class VectorValues;
45  class ValueAutomaticCasting;
46  template<typename T> static bool _truePredicate(const T&) { return true; }
47 
48  /* ************************************************************************* */
49  class GTSAM_EXPORT ValueCloneAllocator {
50  public:
51  static Value* allocate_clone(const Value& a) { return a.clone_(); }
52  static void deallocate_clone(const Value* a) { a->deallocate_(); }
54  };
55 
65  class GTSAM_EXPORT Values {
66 
67  private:
68  // Internally we store a boost ptr_map, with a ValueCloneAllocator (defined
69  // below) to clone and deallocate the Value objects, and our compile-flag-
70  // dependent FastDefaultAllocator to allocate map nodes. In this way, the
71  // user defines the allocation details (i.e. optimize for memory pool/arenas
72  // concurrency).
73  typedef internal::FastDefaultAllocator<typename std::pair<const Key, void*>>::type KeyValuePtrPairAllocator;
74  using KeyValueMap =
75  std::map<Key, std::unique_ptr<Value>, std::less<Key>,
76  std::allocator<std::pair<const Key, std::unique_ptr<Value>>>>;
77 
78  // The member to store the values, see just above
79  KeyValueMap values_;
80 
81  public:
82 
84  typedef std::shared_ptr<Values> shared_ptr;
85 
87  typedef std::shared_ptr<const Values> const_shared_ptr;
88 
90  struct GTSAM_EXPORT KeyValuePair {
91  const Key key;
93 
94  KeyValuePair(Key _key, Value& _value) : key(_key), value(_value) {}
95  };
96 
98  struct GTSAM_EXPORT ConstKeyValuePair {
99  const Key key;
100  const Value& value;
101 
102  ConstKeyValuePair(Key _key, const Value& _value) : key(_key), value(_value) {}
103  ConstKeyValuePair(const KeyValuePair& kv) : key(kv.key), value(kv.value) {}
104  };
105 
106  typedef KeyValuePair value_type;
107 
110 
112  Values() = default;
113 
115  Values(const Values& other);
116 
118  Values(Values&& other);
119 
125  Values(std::initializer_list<ConstKeyValuePair> init);
126 
128  Values(const Values& other, const VectorValues& delta);
129 
133 
135  void print(const std::string& str = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
136 
138  bool equals(const Values& other, double tol=1e-9) const;
139 
143 
152  template <typename ValueType>
153  const ValueType at(Key j) const;
154 
156  double atDouble(size_t key) const { return at<double>(key);}
157 
163  const Value& at(Key j) const;
164 
168  bool exists(Key j) const;
169 
174  template<typename ValueType>
175  const ValueType * exists(Key j) const;
176 
178  size_t size() const { return values_.size(); }
179 
181  bool empty() const { return values_.empty(); }
182 
186 
187  struct deref_iterator {
188  using const_iterator_type = typename KeyValueMap::const_iterator;
189  const_iterator_type it_;
190  deref_iterator(const_iterator_type it) : it_(it) {}
191  ConstKeyValuePair operator*() const { return {it_->first, *(it_->second)}; }
192  std::unique_ptr<ConstKeyValuePair> operator->() {
193  return std::make_unique<ConstKeyValuePair>(it_->first, *(it_->second));
194  }
195  bool operator==(const deref_iterator& other) const {
196  return it_ == other.it_;
197  }
198  bool operator!=(const deref_iterator& other) const { return it_ != other.it_; }
199  deref_iterator& operator++() {
200  ++it_;
201  return *this;
202  }
203  };
204 
205  deref_iterator begin() const { return deref_iterator(values_.begin()); }
206  deref_iterator end() const { return deref_iterator(values_.end()); }
207 
210  deref_iterator find(Key j) const { return deref_iterator(values_.find(j)); }
211 
213  deref_iterator lower_bound(Key j) const { return deref_iterator(values_.lower_bound(j)); }
214 
216  deref_iterator upper_bound(Key j) const { return deref_iterator(values_.upper_bound(j)); }
217 
221 
223  Values retract(const VectorValues& delta) const;
224 
229  void retractMasked(const VectorValues& delta, const KeySet& mask);
230 
232  VectorValues localCoordinates(const Values& cp) const;
233 
235 
237  void insert(Key j, const Value& val);
238 
240  void insert(const Values& values);
241 
245  template <typename ValueType>
246  void insert(Key j, const ValueType& val);
247 
249  void insertDouble(Key j, double c) { insert<double>(j,c); }
250 
252  void update(Key j, const Value& val);
253 
258  template <typename T>
259  void update(Key j, const T& val);
260 
262  void update(const Values& values);
263 
265  void insert_or_assign(Key j, const Value& val);
266 
271  void insert_or_assign(const Values& values);
272 
274  template <typename ValueType>
275  void insert_or_assign(Key j, const ValueType& val);
276 
278  void erase(Key j);
279 
284  KeyVector keys() const;
285 
289  KeySet keySet() const;
290 
292  Values& operator=(const Values& rhs);
293 
295  void swap(Values& other) { values_.swap(other.values_); }
296 
298  void clear() { values_.clear(); }
299 
301  size_t dim() const;
302 
304  std::map<Key,size_t> dims() const;
305 
307  VectorValues zeroVectors() const;
308 
309  // Count values of given type \c ValueType
310  template <class ValueType>
311  size_t count() const;
312 
332  template <class ValueType>
333  std::map<Key, ValueType> // , std::less<Key>, Eigen::aligned_allocator<ValueType>
334  extract(const std::function<bool(Key)>& filterFcn = &_truePredicate<Key>) const;
335 
336  private:
337  // Filters based on ValueType (if not Value) and also based on the user-
338  // supplied \c filter function.
339  template<class ValueType>
340  static bool filterHelper(const std::function<bool(Key)> filter, const ConstKeyValuePair& key_value) {
341  // static_assert if ValueType is type: Value
342  static_assert(!std::is_same<Value, ValueType>::value, "ValueType must not be type: Value to use this filter");
343  // Filter and check the type
344  return filter(key_value.key) && (dynamic_cast<const GenericValue<ValueType>*>(&key_value.value));
345  }
346 
347 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
348 
349  friend class boost::serialization::access;
350  template<class ARCHIVE>
351  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
352  ar & BOOST_SERIALIZATION_NVP(values_);
353  }
354 #endif
355 
356  };
357 
358  /* ************************************************************************* */
359  class ValuesKeyAlreadyExists : public std::exception {
360  protected:
361  const Key key_;
362 
363  private:
364  mutable std::string message_;
365 
366  public:
368  ValuesKeyAlreadyExists(Key key) noexcept :
369  key_(key) {}
370 
371  ~ValuesKeyAlreadyExists() noexcept override {}
372 
374  Key key() const noexcept { return key_; }
375 
377  GTSAM_EXPORT const char* what() const noexcept override;
378  };
379 
380  /* ************************************************************************* */
381  class ValuesKeyDoesNotExist : public std::exception {
382  protected:
383  const char* operation_;
384  const Key key_;
385 
386  private:
387  mutable std::string message_;
388 
389  public:
391  ValuesKeyDoesNotExist(const char* operation, Key key) noexcept :
392  operation_(operation), key_(key) {}
393 
394  ~ValuesKeyDoesNotExist() noexcept override {}
395 
397  Key key() const noexcept { return key_; }
398 
400  GTSAM_EXPORT const char* what() const noexcept override;
401  };
402 
403  /* ************************************************************************* */
404  class ValuesIncorrectType : public std::exception {
405  protected:
406  const Key key_;
407  const std::type_info& storedTypeId_;
408  const std::type_info& requestedTypeId_;
409 
410  private:
411  mutable std::string message_;
412 
413  public:
416  const std::type_info& storedTypeId, const std::type_info& requestedTypeId) noexcept :
417  key_(key), storedTypeId_(storedTypeId), requestedTypeId_(requestedTypeId) {}
418 
419  ~ValuesIncorrectType() noexcept override {}
420 
422  Key key() const noexcept { return key_; }
423 
425  const std::type_info& storedTypeId() const { return storedTypeId_; }
426 
428  const std::type_info& requestedTypeId() const { return requestedTypeId_; }
429 
431  GTSAM_EXPORT const char* what() const noexcept override;
432  };
433 
434  /* ************************************************************************* */
435  class DynamicValuesMismatched : public std::exception {
436 
437  public:
438  DynamicValuesMismatched() noexcept {}
439 
440  ~DynamicValuesMismatched() noexcept override {}
441 
442  const char* what() const noexcept override {
443  return "The Values 'this' and the argument passed to Values::localCoordinates have mismatched keys and values";
444  }
445  };
446 
447  /* ************************************************************************* */
448  class NoMatchFoundForFixed: public std::exception {
449 
450  protected:
451  const size_t M1_, N1_;
452  const size_t M2_, N2_;
453 
454  private:
455  mutable std::string message_;
456 
457  public:
458  NoMatchFoundForFixed(size_t M1, size_t N1, size_t M2, size_t N2) noexcept :
459  M1_(M1), N1_(N1), M2_(M2), N2_(N2) {
460  }
461 
462  ~NoMatchFoundForFixed() noexcept override {
463  }
464 
465  GTSAM_EXPORT const char* what() const noexcept override;
466  };
467 
468  /* ************************************************************************* */
470  template<>
471  struct traits<Values> : public Testable<Values> {
472  };
473 
474 } //\ namespace gtsam
475 
476 
477 #include <gtsam/nonlinear/Values-inl.h>
void clear()
Definition: Values.h:298
Definition: Values.h:448
bool operator!=(const Matrix &A, const Matrix &B)
Definition: Matrix.h:106
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:113
deref_iterator lower_bound(Key j) const
Definition: Values.h:213
A key-value pair, which you get by dereferencing iterators.
Definition: Values.h:90
Definition: Testable.h:152
Key key() const noexcept
The key that was attempted to be accessed that does not exist.
Definition: Values.h:422
std::shared_ptr< const Values > const_shared_ptr
A const shared_ptr to this class.
Definition: Values.h:87
Definition: Group.h:43
virtual Value * clone_() const =0
Key key() const noexcept
The duplicate key that was attempted to be added.
Definition: Values.h:374
Definition: Values.h:404
bool operator==(const Matrix &A, const Matrix &B)
Definition: Matrix.h:99
deref_iterator find(Key j) const
Definition: Values.h:210
virtual void deallocate_() const =0
Default allocator for list, map, and set types.
Definition: FastDefaultAllocator.h:49
ValuesKeyDoesNotExist(const char *operation, Key key) noexcept
Construct with the key that does not exist in the values.
Definition: Values.h:391
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:52
Definition: GenericValue.h:44
A key-value pair, which you get by dereferencing iterators.
Definition: Values.h:98
Definition: VectorValues.h:74
const std::type_info & storedTypeId() const
The typeid of the value stores in the Values.
Definition: Values.h:425
const Value & value
The value.
Definition: Values.h:100
const char * operation_
The operation that attempted to access the key.
Definition: Values.h:383
Definition: Testable.h:112
Definition: Values.h:187
const std::type_info & requestedTypeId() const
The requested typeid.
Definition: Values.h:428
const Key key
The key.
Definition: Values.h:91
GTSAM_EXPORT void print(const Matrix &A, const std::string &s, std::ostream &stream)
size_t size() const
Definition: Values.h:178
Definition: Values.h:65
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
double atDouble(size_t key) const
version for double
Definition: Values.h:156
ValuesKeyAlreadyExists(Key key) noexcept
Construct with the key-value pair attempted to be added.
Definition: Values.h:368
Definition: chartTesting.h:28
Definition: Value.h:39
const Key key
The key.
Definition: Values.h:99
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
void swap(Values &other)
Definition: Values.h:295
const Key key_
The key that already existed.
Definition: Values.h:361
Definition: Values.h:381
const Key key_
The key requested.
Definition: Values.h:406
ValuesIncorrectType(Key key, const std::type_info &storedTypeId, const std::type_info &requestedTypeId) noexcept
Construct with the key that does not exist in the values.
Definition: Values.h:415
Value & value
The value.
Definition: Values.h:92
deref_iterator upper_bound(Key j) const
Definition: Values.h:216
Definition: Values.h:359
void insertDouble(Key j, double c)
version for double
Definition: Values.h:249
An easy way to control which allocator is used for Fast* collections.
std::shared_ptr< Values > shared_ptr
A shared_ptr to this class.
Definition: Values.h:84
Definition: Values.h:435
bool empty() const
Definition: Values.h:181
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
Definition: Values.h:49
Key key() const noexcept
The key that was attempted to be accessed that does not exist.
Definition: Values.h:397
const Key key_
The key that does not exist.
Definition: Values.h:384