GenericValue.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 
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/types.h>
24 #include <gtsam/base/Value.h>
25 
26 #include <cmath>
27 #include <iostream>
28 #include <typeinfo> // operator typeid
29 
30 #ifdef _WIN32
31 #define GENERICVALUE_VISIBILITY
32 #else
33 // This will trigger a LNKxxxx on MSVC, so disable for MSVC build
34 // Please refer to https://github.com/borglab/gtsam/blob/develop/Using-GTSAM-EXPORT.md
35 #define GENERICVALUE_VISIBILITY GTSAM_EXPORT
36 #endif
37 
38 namespace gtsam {
39 
43 template<class T>
44 class GenericValue: public Value {
45 
46 public:
47 
48  typedef T type;
49 
50 protected:
51 
53 
54 public:
55  // Only needed for serialization.
57 
59  GenericValue(const T& value) :
60  value_(value) {
61  }
62 
64  const T& value() const {
65  return value_;
66  }
67 
69  T& value() {
70  return value_;
71  }
72 
74  ~GenericValue() override {
75  }
76 
78  bool equals_(const Value& p, double tol = 1e-9) const override {
79  // Cast the base class Value pointer to a templated generic class pointer
80  const GenericValue& genericValue2 = static_cast<const GenericValue&>(p);
81  // Return the result of using the equals traits for the derived class
82  return traits<T>::Equals(this->value_, genericValue2.value_, tol);
83  }
84 
86  bool equals(const GenericValue &other, double tol = 1e-9) const {
87  return traits<T>::Equals(this->value(), other.value(), tol);
88  }
89 
91  void print(const std::string& str) const override {
92  std::cout << "(" << demangle(typeid(T).name()) << ")\n";
93  traits<T>::Print(value_, str);
94  }
95 
99  Value* clone_() const override {
100  GenericValue* ptr = new GenericValue(*this); // calls copy constructor to fill in
101  return ptr;
102  }
103 
107  void deallocate_() const override {
108  delete this;
109  }
110 
114  std::shared_ptr<Value> clone() const override {
115  return std::allocate_shared<GenericValue>(Eigen::aligned_allocator<GenericValue>(), *this);
116  }
117 
119  Value* retract_(const Vector& delta) const override {
120  // Call retract on the derived class using the retract trait function
121  const T retractResult = traits<T>::Retract(GenericValue<T>::value(), delta);
122 
123  Value* resultAsValue = new GenericValue(retractResult);
124 
125  // Return the pointer to the Value base class
126  return resultAsValue;
127  }
128 
130  Vector localCoordinates_(const Value& value2) const override {
131  // Cast the base class Value pointer to a templated generic class pointer
132  const GenericValue<T>& genericValue2 =
133  static_cast<const GenericValue<T>&>(value2);
134 
135  // Return the result of calling localCoordinates trait on the derived class
136  return traits<T>::Local(GenericValue<T>::value(), genericValue2.value());
137  }
138 
142  }
143 
145  Vector localCoordinates(const GenericValue& value2) const {
146  return localCoordinates_(value2);
147  }
148 
150  size_t dim() const override {
151  return traits<T>::GetDimension(value_);
152  }
153 
155  Value& operator=(const Value& rhs) override {
156  // Cast the base class Value pointer to a derived class pointer
157  const GenericValue& derivedRhs = static_cast<const GenericValue&>(rhs);
158 
159  // Do the assignment and return the result
160  *this = GenericValue(derivedRhs); // calls copy constructor
161  return *this;
162  }
163 
164  protected:
165 
169  Value::operator=(static_cast<Value const&>(rhs));
170  value_ = rhs.value_;
171  return *this;
172  }
173 
174  private:
175 
176 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
177 
178  friend class boost::serialization::access;
179  template<class ARCHIVE>
180  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
181  ar & boost::serialization::make_nvp("GenericValue",
182  boost::serialization::base_object<Value>(*this));
183  ar & boost::serialization::make_nvp("value", value_);
184  }
185 #endif
186 
187 
188  // Alignment, see https://eigen.tuxfamily.org/dox/group__TopicStructHavingEigenMembers.html
189  enum { NeedsToAlign = (sizeof(T) % 16) == 0 };
190 public:
192 };
193 
195 #define GTSAM_VALUE_EXPORT(Type) BOOST_CLASS_EXPORT(gtsam::GenericValue<Type>)
196 
197 // traits
198 template <typename ValueType>
199 struct traits<GenericValue<ValueType> >
200  : public Testable<GenericValue<ValueType> > {};
201 
202 // define Value::cast here since now GenericValue has been declared
203 template<typename ValueType>
204 const ValueType& Value::cast() const {
205  return dynamic_cast<const GenericValue<ValueType>&>(*this).value();
206 }
207 
210 template<class T>
212  return GenericValue<T>(v);
213 }
214 
215 
216 } /* namespace gtsam */
Vector localCoordinates(const GenericValue &value2) const
Non-virtual version of localCoordinates.
Definition: GenericValue.h:145
Typedefs for easier changing of types.
GenericValue retract(const Vector &delta) const
Non-virtual version of retract.
Definition: GenericValue.h:140
std::shared_ptr< Value > clone() const override
Definition: GenericValue.h:114
std::string serialize(const T &input)
serializes to a string
T value_
The wrapped value.
Definition: GenericValue.h:52
void print(const std::string &str) const override
Virtual print function, uses traits.
Definition: GenericValue.h:91
~GenericValue() override
Destructor.
Definition: GenericValue.h:74
Value * clone_() const override
Definition: GenericValue.h:99
GenericValue(const T &value)
Construct from value.
Definition: GenericValue.h:59
#define GTSAM_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
Definition: types.h:293
void deallocate_() const override
Definition: GenericValue.h:107
Eigen::VectorXd Vector
Definition: Vector.h:38
STL compatible allocator to use with types requiring a non standrad alignment.
Definition: Memory.h:878
Definition: pytypes.h:1403
Value & operator=(const Value &rhs) override
Assignment operator.
Definition: GenericValue.h:155
Base class and basic functions for Manifold types.
Value * retract_(const Vector &delta) const override
Generic Value interface version of retract.
Definition: GenericValue.h:119
Array< int, Dynamic, 1 > v
Eigen::Triplet< double > T
virtual Value & operator=(const Value &)
Definition: Value.h:81
Array< double, 1, 3 > e(1./3., 0.5, 2.)
traits
Definition: chartTesting.h:28
const ValueType & cast() const
Definition: GenericValue.h:204
GenericValue< T > genericValue(const T &v)
Definition: GenericValue.h:211
std::string demangle(const char *name)
Pretty print Value type name.
Definition: types.cpp:37
bool equals_(const Value &p, double tol=1e-9) const override
equals implementing generic Value interface
Definition: GenericValue.h:78
bool equals(const GenericValue &other, double tol=1e-9) const
non virtual equals function, uses traits
Definition: GenericValue.h:86
float * p
Vector localCoordinates_(const Value &value2) const override
Generic Value interface version of localCoordinates.
Definition: GenericValue.h:130
The base class for any variable that can be optimized or used in a factor.
const G double tol
Definition: Group.h:86
T & value()
Return the value.
Definition: GenericValue.h:69
void Print(const CONTAINER &keys, const string &s, const KeyFormatter &keyFormatter)
Definition: Key.cpp:62
size_t dim() const override
Return run-time dimensionality.
Definition: GenericValue.h:150
const T & value() const
Return a constant value.
Definition: GenericValue.h:64
GenericValue< T > & operator=(const GenericValue< T > &rhs)
Definition: GenericValue.h:168


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:17