gtsam  4.0.0
gtsam
GenericValue.h
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 
12 /*
13  * @file GenericValue.h
14  * @brief Wraps any type T so it can play as a Value
15  * @date October, 2014
16  * @author Michael Bosse, Abel Gawel, Renaud Dube
17  * based on DerivedValue.h by Duy Nguyen Ta
18  */
19 
20 #pragma once
21 
22 #include <gtsam/base/Manifold.h>
23 #include <gtsam/base/Value.h>
24 
25 #include <boost/make_shared.hpp>
26 #include <boost/pool/pool_alloc.hpp>
27 
28 #include <cmath>
29 #include <iostream>
30 #include <typeinfo> // operator typeid
31 
32 namespace gtsam {
33 
37 template<class T>
38 class GenericValue: public Value {
39 
40 public:
41 
42  typedef T type;
43 
44 protected:
45 
46  T value_;
47 
48 public:
49  // Only needed for serialization.
50  GenericValue(){}
51 
53  GenericValue(const T& value) :
54  value_(value) {
55  }
56 
58  const T& value() const {
59  return value_;
60  }
61 
63  T& value() {
64  return value_;
65  }
66 
68  virtual ~GenericValue() {
69  }
70 
72  virtual bool equals_(const Value& p, double tol = 1e-9) const {
73  // Cast the base class Value pointer to a templated generic class pointer
74  const GenericValue& genericValue2 = static_cast<const GenericValue&>(p);
75  // Return the result of using the equals traits for the derived class
76  return traits<T>::Equals(this->value_, genericValue2.value_, tol);
77  }
78 
80  bool equals(const GenericValue &other, double tol = 1e-9) const {
81  return traits<T>::Equals(this->value(), other.value(), tol);
82  }
83 
85  virtual void print(const std::string& str) const {
86  std::cout << "(" << typeid(T).name() << ") ";
88  }
89 
95  virtual Value* clone_() const {
96  void *place = boost::singleton_pool<PoolTag, sizeof(GenericValue)>::malloc();
97  GenericValue* ptr = new (place) GenericValue(*this); // calls copy constructor to fill in
98  return ptr;
99  }
100 
104  virtual void deallocate_() const {
105  this->~GenericValue(); // Virtual destructor cleans up the derived object
106  boost::singleton_pool<PoolTag, sizeof(GenericValue)>::free((void*) this); // Release memory from pool
107  }
108 
112  virtual boost::shared_ptr<Value> clone() const {
113  return boost::allocate_shared<GenericValue>(Eigen::aligned_allocator<GenericValue>(), *this);
114  }
115 
117  virtual Value* retract_(const Vector& delta) const {
118  // Call retract on the derived class using the retract trait function
119  const T retractResult = traits<T>::Retract(GenericValue<T>::value(), delta);
120 
121  // Create a Value pointer copy of the result
122  void* resultAsValuePlace =
123  boost::singleton_pool<PoolTag, sizeof(GenericValue)>::malloc();
124  Value* resultAsValue = new (resultAsValuePlace) GenericValue(retractResult);
125 
126  // Return the pointer to the Value base class
127  return resultAsValue;
128  }
129 
131  virtual Vector localCoordinates_(const Value& value2) const {
132  // Cast the base class Value pointer to a templated generic class pointer
133  const GenericValue<T>& genericValue2 =
134  static_cast<const GenericValue<T>&>(value2);
135 
136  // Return the result of calling localCoordinates trait on the derived class
137  return traits<T>::Local(GenericValue<T>::value(), genericValue2.value());
138  }
139 
141  GenericValue retract(const Vector& delta) const {
143  }
144 
146  Vector localCoordinates(const GenericValue& value2) const {
147  return localCoordinates_(value2);
148  }
149 
151  virtual size_t dim() const {
153  }
154 
156  virtual Value& operator=(const Value& rhs) {
157  // Cast the base class Value pointer to a derived class pointer
158  const GenericValue& derivedRhs = static_cast<const GenericValue&>(rhs);
159 
160  // Do the assignment and return the result
161  *this = GenericValue(derivedRhs); // calls copy constructor
162  return *this;
163  }
164 
165  protected:
166 
170  Value::operator=(static_cast<Value const&>(rhs));
171  value_ = rhs.value_;
172  return *this;
173  }
174 
175  private:
176 
178  struct PoolTag {
179  };
180 
181  private:
182 
185  template<class ARCHIVE>
186  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
187  ar & boost::serialization::make_nvp("GenericValue",
188  boost::serialization::base_object<Value>(*this));
189  ar & boost::serialization::make_nvp("value", value_);
190  }
191 
192 
193  // Alignment, see https://eigen.tuxfamily.org/dox/group__TopicStructHavingEigenMembers.html
194  enum { NeedsToAlign = (sizeof(T) % 16) == 0 };
195 public:
196  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
197 };
198 
200 #define GTSAM_VALUE_EXPORT(Type) BOOST_CLASS_EXPORT(gtsam::GenericValue<Type>)
201 
202 // traits
203 template <typename ValueType>
204 struct traits<GenericValue<ValueType> >
205  : public Testable<GenericValue<ValueType> > {};
206 
207 // define Value::cast here since now GenericValue has been declared
208 template<typename ValueType>
209 const ValueType& Value::cast() const {
210  return dynamic_cast<const GenericValue<ValueType>&>(*this).value();
211 }
212 
213 } /* namespace gtsam */
The base class for any variable that can be optimized or used in a factor.
virtual Value * clone_() const
Create a duplicate object returned as a pointer to the generic Value interface.
Definition: GenericValue.h:95
Base class and basic functions for Manifold types.
Vector localCoordinates(const GenericValue &value2) const
Non-virtual version of localCoordinates.
Definition: GenericValue.h:146
virtual void print(const std::string &str) const
Virtual print function, uses traits.
Definition: GenericValue.h:85
This is the base class for any type to be stored in Values.
Definition: Value.h:36
bool equals(const GenericValue &other, double tol=1e-9) const
non virtual equals function, uses traits
Definition: GenericValue.h:80
const T & value() const
Return a constant value.
Definition: GenericValue.h:58
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
const ValueType & cast() const
Cast to known ValueType.
Definition: GenericValue.h:209
virtual void deallocate_() const
Destroy and deallocate this object, only if it was originally allocated using clone_().
Definition: GenericValue.h:104
GenericValue retract(const Vector &delta) const
Non-virtual version of retract.
Definition: GenericValue.h:141
Wraps any type T so it can play as a Value.
Definition: GenericValue.h:38
virtual size_t dim() const
Return run-time dimensionality.
Definition: GenericValue.h:151
virtual Value & operator=(const Value &)
Assignment operator.
Definition: Value.h:78
virtual ~GenericValue()
Destructor.
Definition: GenericValue.h:68
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
T value_
The wrapped value.
Definition: GenericValue.h:46
friend class boost::serialization::access
Serialization function.
Definition: GenericValue.h:184
GenericValue(const T &value)
Construct from value.
Definition: GenericValue.h:53
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
virtual bool equals_(const Value &p, double tol=1e-9) const
equals implementing generic Value interface
Definition: GenericValue.h:72
virtual Value * retract_(const Vector &delta) const
Generic Value interface version of retract.
Definition: GenericValue.h:117
GenericValue< T > & operator=(const GenericValue< T > &rhs)
Assignment operator, protected because only the Value or DERIVED assignment operators should be used.
Definition: GenericValue.h:169
virtual Vector localCoordinates_(const Value &value2) const
Generic Value interface version of localCoordinates.
Definition: GenericValue.h:131
virtual Value & operator=(const Value &rhs)
Assignment operator.
Definition: GenericValue.h:156
virtual boost::shared_ptr< Value > clone() const
Clone this value (normal clone on the heap, delete with 'delete' operator)
Definition: GenericValue.h:112