gtsam 4.1.1
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/types.h>
24#include <gtsam/base/Value.h>
25
26#include <boost/make_shared.hpp>
27#include <boost/pool/pool_alloc.hpp>
28
29#include <cmath>
30#include <iostream>
31#include <typeinfo> // operator typeid
32
33#ifdef _WIN32
34#define GENERICVALUE_VISIBILITY
35#else
36// This will trigger a LNKxxxx on MSVC, so disable for MSVC build
37// Please refer to https://github.com/borglab/gtsam/blob/develop/Using-GTSAM-EXPORT.md
38#define GENERICVALUE_VISIBILITY GTSAM_EXPORT
39#endif
40
41namespace gtsam {
42
46template<class T>
47class GenericValue: public Value {
48
49public:
50
51 typedef T type;
52
53protected:
54
56
57public:
58 // Only needed for serialization.
59 GenericValue(){}
60
62 GenericValue(const T& value) :
63 value_(value) {
64 }
65
67 const T& value() const {
68 return value_;
69 }
70
72 T& value() {
73 return value_;
74 }
75
77 ~GenericValue() override {
78 }
79
81 bool equals_(const Value& p, double tol = 1e-9) const override {
82 // Cast the base class Value pointer to a templated generic class pointer
83 const GenericValue& genericValue2 = static_cast<const GenericValue&>(p);
84 // Return the result of using the equals traits for the derived class
85 return traits<T>::Equals(this->value_, genericValue2.value_, tol);
86 }
87
89 bool equals(const GenericValue &other, double tol = 1e-9) const {
90 return traits<T>::Equals(this->value(), other.value(), tol);
91 }
92
94 void print(const std::string& str) const override {
95 std::cout << "(" << demangle(typeid(T).name()) << ")\n";
97 }
98
102 Value* clone_() const override {
103 GenericValue* ptr = new GenericValue(*this); // calls copy constructor to fill in
104 return ptr;
105 }
106
110 void deallocate_() const override {
111 delete this;
112 }
113
117 boost::shared_ptr<Value> clone() const override {
118 return boost::allocate_shared<GenericValue>(Eigen::aligned_allocator<GenericValue>(), *this);
119 }
120
122 Value* retract_(const Vector& delta) const override {
123 // Call retract on the derived class using the retract trait function
124 const T retractResult = traits<T>::Retract(GenericValue<T>::value(), delta);
125
126 Value* resultAsValue = new GenericValue(retractResult);
127
128 // Return the pointer to the Value base class
129 return resultAsValue;
130 }
131
133 Vector localCoordinates_(const Value& value2) const override {
134 // Cast the base class Value pointer to a templated generic class pointer
135 const GenericValue<T>& genericValue2 =
136 static_cast<const GenericValue<T>&>(value2);
137
138 // Return the result of calling localCoordinates trait on the derived class
139 return traits<T>::Local(GenericValue<T>::value(), genericValue2.value());
140 }
141
143 GenericValue retract(const Vector& delta) const {
145 }
146
148 Vector localCoordinates(const GenericValue& value2) const {
149 return localCoordinates_(value2);
150 }
151
153 size_t dim() const override {
155 }
156
158 Value& operator=(const Value& rhs) override {
159 // Cast the base class Value pointer to a derived class pointer
160 const GenericValue& derivedRhs = static_cast<const GenericValue&>(rhs);
161
162 // Do the assignment and return the result
163 *this = GenericValue(derivedRhs); // calls copy constructor
164 return *this;
165 }
166
167 protected:
168
172 Value::operator=(static_cast<Value const&>(rhs));
173 value_ = rhs.value_;
174 return *this;
175 }
176
177 private:
178
181 template<class ARCHIVE>
182 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
183 ar & boost::serialization::make_nvp("GenericValue",
184 boost::serialization::base_object<Value>(*this));
185 ar & boost::serialization::make_nvp("value", value_);
186 }
187
188
189 // Alignment, see https://eigen.tuxfamily.org/dox/group__TopicStructHavingEigenMembers.html
190 enum { NeedsToAlign = (sizeof(T) % 16) == 0 };
191public:
193};
194
196#define GTSAM_VALUE_EXPORT(Type) BOOST_CLASS_EXPORT(gtsam::GenericValue<Type>)
197
198// traits
199template <typename ValueType>
200struct traits<GenericValue<ValueType> >
201 : public Testable<GenericValue<ValueType> > {};
202
203// define Value::cast here since now GenericValue has been declared
204template<typename ValueType>
205const ValueType& Value::cast() const {
206 return dynamic_cast<const GenericValue<ValueType>&>(*this).value();
207}
208
211template<class T>
213 return GenericValue<T>(v);
214}
215
216
217} /* namespace gtsam */
Typedefs for easier changing of types.
#define GTSAM_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
This marks a GTSAM object to require alignment.
Definition: types.h:286
The base class for any variable that can be optimized or used in a factor.
Base class and basic functions for Manifold types.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
std::string demangle(const char *name)
Pretty print Value type name.
Definition: types.cpp:37
GenericValue< T > genericValue(const T &v)
Functional constructor of GenericValue<T> so T can be automatically deduced.
Definition: GenericValue.h:212
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
Wraps any type T so it can play as a Value.
Definition: GenericValue.h:47
Value * clone_() const override
Create a duplicate object returned as a pointer to the generic Value interface.
Definition: GenericValue.h:102
Value * retract_(const Vector &delta) const override
Generic Value interface version of retract.
Definition: GenericValue.h:122
Value & operator=(const Value &rhs) override
Assignment operator.
Definition: GenericValue.h:158
void print(const std::string &str) const override
Virtual print function, uses traits.
Definition: GenericValue.h:94
bool equals(const GenericValue &other, double tol=1e-9) const
non virtual equals function, uses traits
Definition: GenericValue.h:89
size_t dim() const override
Return run-time dimensionality.
Definition: GenericValue.h:153
GenericValue(const T &value)
Construct from value.
Definition: GenericValue.h:62
void deallocate_() const override
Destroy and deallocate this object, only if it was originally allocated using clone_().
Definition: GenericValue.h:110
T value_
The wrapped value.
Definition: GenericValue.h:55
GenericValue< T > & operator=(const GenericValue< T > &rhs)
Assignment operator, protected because only the Value or DERIVED assignment operators should be used.
Definition: GenericValue.h:171
Vector localCoordinates(const GenericValue &value2) const
Non-virtual version of localCoordinates.
Definition: GenericValue.h:148
friend class boost::serialization::access
Serialization function.
Definition: GenericValue.h:180
bool equals_(const Value &p, double tol=1e-9) const override
equals implementing generic Value interface
Definition: GenericValue.h:81
const T & value() const
Return a constant value.
Definition: GenericValue.h:67
~GenericValue() override
Destructor.
Definition: GenericValue.h:77
GenericValue retract(const Vector &delta) const
Non-virtual version of retract.
Definition: GenericValue.h:143
Vector localCoordinates_(const Value &value2) const override
Generic Value interface version of localCoordinates.
Definition: GenericValue.h:133
boost::shared_ptr< Value > clone() const override
Clone this value (normal clone on the heap, delete with 'delete' operator)
Definition: GenericValue.h:117
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:151
This is the base class for any type to be stored in Values.
Definition: Value.h:36
const ValueType & cast() const
Cast to known ValueType.
Definition: GenericValue.h:205
virtual Value & operator=(const Value &)
Assignment operator.
Definition: Value.h:78