Loading...
Searching...
No Matches
GenericValue.h
/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
/*
* @file GenericValue.h
* @brief Wraps any type T so it can play as a Value
* @date October, 2014
* @author Michael Bosse, Abel Gawel, Renaud Dube
* based on DerivedValue.h by Duy Nguyen Ta
*/
#pragma once
#include <gtsam/base/Manifold.h>
#include <gtsam/base/types.h>
#include <gtsam/base/Value.h>
#include <boost/make_shared.hpp>
#include <boost/pool/pool_alloc.hpp>
#include <cmath>
#include <iostream>
#include <typeinfo> // operator typeid
#ifdef _WIN32
#define GENERICVALUE_VISIBILITY
#else
// This will trigger a LNKxxxx on MSVC, so disable for MSVC build
// Please refer to https://github.com/borglab/gtsam/blob/develop/Using-GTSAM-EXPORT.md
#define GENERICVALUE_VISIBILITY GTSAM_EXPORT
#endif
namespace gtsam {
template<class T>
class GenericValue: public Value {
public:
typedef T type;
protected:
T value_;
public:
// Only needed for serialization.
GenericValue(){}
GenericValue(const T& value) :
value_(value) {
}
const T& value() const {
return value_;
}
T& value() {
return value_;
}
~GenericValue() override {
}
bool equals_(const Value& p, double tol = 1e-9) const override {
// Cast the base class Value pointer to a templated generic class pointer
const GenericValue& genericValue2 = static_cast<const GenericValue&>(p);
// Return the result of using the equals traits for the derived class
return traits<T>::Equals(this->value_, genericValue2.value_, tol);
}
bool equals(const GenericValue &other, double tol = 1e-9) const {
return traits<T>::Equals(this->value(), other.value(), tol);
}
void print(const std::string& str) const override {
std::cout << "(" << demangle(typeid(T).name()) << ")\n";
traits<T>::Print(value_, str);
}
Value* clone_() const override {
GenericValue* ptr = new GenericValue(*this); // calls copy constructor to fill in
return ptr;
}
void deallocate_() const override {
delete this;
}
boost::shared_ptr<Value> clone() const override {
return boost::allocate_shared<GenericValue>(Eigen::aligned_allocator<GenericValue>(), *this);
}
Value* retract_(const Vector& delta) const override {
// Call retract on the derived class using the retract trait function
const T retractResult = traits<T>::Retract(GenericValue<T>::value(), delta);
Value* resultAsValue = new GenericValue(retractResult);
// Return the pointer to the Value base class
return resultAsValue;
}
Vector localCoordinates_(const Value& value2) const override {
// Cast the base class Value pointer to a templated generic class pointer
const GenericValue<T>& genericValue2 =
static_cast<const GenericValue<T>&>(value2);
// Return the result of calling localCoordinates trait on the derived class
return traits<T>::Local(GenericValue<T>::value(), genericValue2.value());
}
GenericValue retract(const Vector& delta) const {
return GenericValue(traits<T>::Retract(GenericValue<T>::value(), delta));
}
Vector localCoordinates(const GenericValue& value2) const {
return localCoordinates_(value2);
}
size_t dim() const override {
return traits<T>::GetDimension(value_);
}
Value& operator=(const Value& rhs) override {
// Cast the base class Value pointer to a derived class pointer
const GenericValue& derivedRhs = static_cast<const GenericValue&>(rhs);
// Do the assignment and return the result
*this = GenericValue(derivedRhs); // calls copy constructor
return *this;
}
protected:
GenericValue<T>& operator=(const GenericValue<T>& rhs) {
Value::operator=(static_cast<Value const&>(rhs));
value_ = rhs.value_;
return *this;
}
private:
friend class boost::serialization::access;
template<class ARCHIVE>
void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
ar & boost::serialization::make_nvp("GenericValue",
boost::serialization::base_object<Value>(*this));
ar & boost::serialization::make_nvp("value", value_);
}
// Alignment, see https://eigen.tuxfamily.org/dox/group__TopicStructHavingEigenMembers.html
enum { NeedsToAlign = (sizeof(T) % 16) == 0 };
public:
GTSAM_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
};
#define GTSAM_VALUE_EXPORT(Type) BOOST_CLASS_EXPORT(gtsam::GenericValue<Type>)
// traits
template <typename ValueType>
struct traits<GenericValue<ValueType> >
: public Testable<GenericValue<ValueType> > {};
// define Value::cast here since now GenericValue has been declared
template<typename ValueType>
const ValueType& Value::cast() const {
return dynamic_cast<const GenericValue<ValueType>&>(*this).value();
}
template<class T>
GenericValue<T> genericValue(const T& v) {
return GenericValue<T>(v);
}
} /* namespace gtsam */
Manifold.h
Base class and basic functions for Manifold types.
types.h
Typedefs for easier changing of types.
GTSAM_MAKE_ALIGNED_OPERATOR_NEW_IF
#define GTSAM_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
This marks a GTSAM object to require alignment.
Definition types.h:317
Value.h
The base class for any variable that can be optimized or used in a factor.
gtsam
Global functions in a separate testing namespace.
Definition chartTesting.h:28
gtsam::demangle
std::string demangle(const char *name)
Pretty print Value type name.
Definition types.cpp:37
gtsam::genericValue
GenericValue< T > genericValue(const T &v)
Functional constructor of GenericValue<T> so T can be automatically deduced.
Definition GenericValue.h:212
gtsam::traits
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition concepts.h:30
gtsam::GenericValue
Wraps any type T so it can play as a Value.
Definition GenericValue.h:47
gtsam::GenericValue::clone_
Value * clone_() const override
Create a duplicate object returned as a pointer to the generic Value interface.
Definition GenericValue.h:102
gtsam::GenericValue::value
T & value()
Return the value.
Definition GenericValue.h:72
gtsam::GenericValue::retract_
Value * retract_(const Vector &delta) const override
Generic Value interface version of retract.
Definition GenericValue.h:122
gtsam::GenericValue::operator=
Value & operator=(const Value &rhs) override
Assignment operator.
Definition GenericValue.h:158
gtsam::GenericValue::print
void print(const std::string &str) const override
Virtual print function, uses traits.
Definition GenericValue.h:94
gtsam::GenericValue::equals
bool equals(const GenericValue &other, double tol=1e-9) const
non virtual equals function, uses traits
Definition GenericValue.h:89
gtsam::GenericValue::dim
size_t dim() const override
Return run-time dimensionality.
Definition GenericValue.h:153
gtsam::GenericValue::GenericValue
GenericValue(const T &value)
Construct from value.
Definition GenericValue.h:62
gtsam::GenericValue::deallocate_
void deallocate_() const override
Destroy and deallocate this object, only if it was originally allocated using clone_().
Definition GenericValue.h:110
gtsam::GenericValue::value_
T value_
The wrapped value.
Definition GenericValue.h:55
gtsam::GenericValue::operator=
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
gtsam::GenericValue::localCoordinates
Vector localCoordinates(const GenericValue &value2) const
Non-virtual version of localCoordinates.
Definition GenericValue.h:148
gtsam::GenericValue::access
friend class boost::serialization::access
Serialization function.
Definition GenericValue.h:180
gtsam::GenericValue::equals_
bool equals_(const Value &p, double tol=1e-9) const override
equals implementing generic Value interface
Definition GenericValue.h:81
gtsam::GenericValue::value
const T & value() const
Return a constant value.
Definition GenericValue.h:67
gtsam::GenericValue::~GenericValue
~GenericValue() override
Destructor.
Definition GenericValue.h:77
gtsam::GenericValue::retract
GenericValue retract(const Vector &delta) const
Non-virtual version of retract.
Definition GenericValue.h:143
gtsam::GenericValue::localCoordinates_
Vector localCoordinates_(const Value &value2) const override
Generic Value interface version of localCoordinates.
Definition GenericValue.h:133
gtsam::GenericValue::clone
boost::shared_ptr< Value > clone() const override
Clone this value (normal clone on the heap, delete with 'delete' operator).
Definition GenericValue.h:117
gtsam::Testable
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:151
gtsam::Value
This is the base class for any type to be stored in Values.
Definition Value.h:37
gtsam::Value::cast
const ValueType & cast() const
Cast to known ValueType.
Definition GenericValue.h:205
gtsam::Value::operator=
virtual Value & operator=(const Value &)
Assignment operator.
Definition Value.h:79