diff --git a/gtsam/base/DerivedValue.cpp b/gtsam/base/DerivedValue.cpp new file mode 100644 index 000000000..386919d6f --- /dev/null +++ b/gtsam/base/DerivedValue.cpp @@ -0,0 +1,21 @@ +/* + * DerivedValue.cpp + * + * Created on: Jan 26, 2012 + * Author: thduynguyen + */ + +#include "DerivedValue.h" + +namespace gtsam { + +DerivedValue::DerivedValue() { + // TODO Auto-generated constructor stub + +} + +DerivedValue::~DerivedValue() { + // TODO Auto-generated destructor stub +} + +} /* namespace gtsam */ diff --git a/gtsam/base/DerivedValue.h b/gtsam/base/DerivedValue.h new file mode 100644 index 000000000..d0608059e --- /dev/null +++ b/gtsam/base/DerivedValue.h @@ -0,0 +1,76 @@ +/* + * DerivedValue.h + * + * Created on: Jan 26, 2012 + * Author: thduynguyen + */ + +#pragma once + +#include +#include +namespace gtsam { + +template +class DerivedValue : public Value { +private: + /// Fake Tag struct for singleton pool allocator. In fact, it is never used! + struct PoolTag { }; + +public: + + DerivedValue() {} + + virtual ~DerivedValue() {} + + /** + * Create a duplicate object returned as a pointer to the generic Value interface. + * For the shake of performance, this function use singleton pool allocator instead of the normal heap allocator + */ + virtual Value* clone_() const { + void *place = boost::singleton_pool::malloc(); + DERIVED* ptr = new(place) DERIVED(static_cast(*this)); + return ptr; + } + + /** + * Create a duplicate object returned as a pointer to the generic Value interface + */ + virtual void deallocate_() const { + boost::singleton_pool::free((void*)this); + } + + /// equals implementing generic Value interface + virtual bool equals_(const Value& p, double tol = 1e-9) const { + // Cast the base class Value pointer to a derived class pointer + const DERIVED& derivedValue2 = dynamic_cast(p); + + // Return the result of calling equals on the derived class + return (static_cast(this))->equals(derivedValue2, tol); +// return CallDerivedEquals(this, p, tol); + } + + /// Generic Value interface version of retract + virtual std::auto_ptr retract_(const Vector& delta) const { + // Call retract on the derived class + const DERIVED retractResult = (static_cast(this))->retract(delta); + + // Create a Value pointer copy of the result + std::auto_ptr resultAsValue(new DERIVED(retractResult)); + + // Return the pointer to the Value base class + return resultAsValue; + } + + /// Generic Value interface version of localCoordinates + virtual Vector localCoordinates_(const Value& value2) const { + // Cast the base class Value pointer to a derived class pointer + const DERIVED& derivedValue2 = dynamic_cast(value2); + + // Return the result of calling localCoordinates on the derived class + return (static_cast(this))->localCoordinates(derivedValue2); + } + +}; + +} /* namespace gtsam */ diff --git a/gtsam/base/Value.h b/gtsam/base/Value.h index eb86e69f1..dcb4a9c16 100644 --- a/gtsam/base/Value.h +++ b/gtsam/base/Value.h @@ -102,14 +102,17 @@ namespace gtsam { public: /** Allocate and construct a clone of this value */ - virtual std::auto_ptr clone_() const = 0; + virtual Value* clone_() const = 0; - /** Print this value, for debugging and unit tests */ - virtual void print(const std::string& str = "") const = 0; + /** Deallocate a raw pointer of this value */ + virtual void deallocate_() const = 0; /** Compare this Value with another for equality. */ virtual bool equals_(const Value& other, double tol = 1e-9) const = 0; + /** Print this value, for debugging and unit tests */ + virtual void print(const std::string& str = "") const = 0; + /** Return the dimensionality of the tangent space of this value. This is * the dimensionality of \c delta passed into retract() and of the vector * returned by localCoordinates(). @@ -136,57 +139,6 @@ namespace gtsam { /** Virutal destructor */ virtual ~Value() {} - protected: - /** This is a convenience function to make it easy for you to implement the - * generic Value inferface, see the example at the top of the Value - * documentation. - * @param value1 The object on which to call equals, stored as a derived class pointer - * @param value2 The argument to pass to the derived equals function, strored as a Value reference - * @return The result of equals of the derived class - */ - template - static bool CallDerivedEquals(const Derived* value1, const Value& value2, double tol) { - // Cast the base class Value pointer to a derived class pointer - const Derived& derivedValue2 = dynamic_cast(value2); - - // Return the result of calling equals on the derived class - return value1->equals(derivedValue2, tol); - } - - /** This is a convenience function to make it easy for you to implement the - * generic Value inferface, see the example at the top of the Value - * documentation. - * @param derived A pointer to the derived class on which to call retract - * @param delta The delta vector to pass to the derived retract - * @return The result of retract on the derived class, stored as a Value pointer - */ - template - static std::auto_ptr CallDerivedRetract(const Derived* derived, const Vector& delta) { - // Call retract on the derived class - const Derived retractResult = derived->retract(delta); - - // Create a Value pointer copy of the result - std::auto_ptr resultAsValue(new Derived(retractResult)); - - // Return the pointer to the Value base class - return resultAsValue; - } - - /** This is a convenience function to make it easy for you to implement the - * generic Value inferface, see the example at the top of the Value - * documentation. - * @param value1 The object on which to call localCoordinates, stored as a derived class pointer - * @param value2 The argument to pass to the derived localCoordinates function, stored as a Value reference - * @return The result of localCoordinates of the derived class - */ - template - static Vector CallDerivedLocalCoordinates(const Derived* value1, const Value& value2) { - // Cast the base class Value pointer to a derived class pointer - const Derived& derivedValue2 = dynamic_cast(value2); - - // Return the result of calling localCoordinates on the derived class - return value1->localCoordinates(derivedValue2); - } }; } /* namespace gtsam */ diff --git a/gtsam/geometry/Rot3.h b/gtsam/geometry/Rot3.h index d2e72abeb..18f370941 100644 --- a/gtsam/geometry/Rot3.h +++ b/gtsam/geometry/Rot3.h @@ -32,7 +32,7 @@ #endif #endif -#include +#include #include #include @@ -50,7 +50,7 @@ namespace gtsam { * @ingroup geometry * \nosubgrouping */ - class Rot3 : public Value { + class Rot3 : public DerivedValue { public: static const size_t dimension = 3; @@ -178,11 +178,6 @@ namespace gtsam { static Rot3 rodriguez(double wx, double wy, double wz) { return rodriguez(Vector_(3,wx,wy,wz));} - /** - * Create a duplicate object returned as a pointer to the generic Value interface - */ - virtual std::auto_ptr clone_() const { return std::auto_ptr(new Rot3(*this)); } - /// @} /// @name Testable /// @{ @@ -193,9 +188,6 @@ namespace gtsam { /** equals with an tolerance */ bool equals(const Rot3& p, double tol = 1e-9) const; - /** equals implementing generic Value interface */ - virtual bool equals_(const Value& p, double tol = 1e-9) const { return CallDerivedEquals(this, p, tol); } - /// @} /// @name Group /// @{ @@ -247,7 +239,7 @@ namespace gtsam { static size_t Dim() { return dimension; } /// return dimensionality of tangent space, DOF = 3 - virtual size_t dim() const { return dimension; } + size_t dim() const { return dimension; } /** * The method retract() is used to map from the tangent space back to the manifold. @@ -277,12 +269,6 @@ namespace gtsam { /// Returns local retract coordinates in neighborhood around current rotation Vector localCoordinates(const Rot3& t2, Rot3::CoordinatesMode mode = ROT3_DEFAULT_COORDINATES_MODE) const; - /// Generic Value interface version of retract - virtual std::auto_ptr retract_(const Vector& omega) const { return CallDerivedRetract(this, omega); } - - /// Generic Value interface version of localCoordinates - virtual Vector localCoordinates_(const Value& value) const { return CallDerivedLocalCoordinates(this, value); } - /// @} /// @name Lie Group /// @{