Derived value

release/4.3a0
Duy-Nguyen Ta 2012-01-26 23:10:55 +00:00
parent a7a845a803
commit 9e00963d54
4 changed files with 106 additions and 71 deletions

View File

@ -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 */

76
gtsam/base/DerivedValue.h Normal file
View File

@ -0,0 +1,76 @@
/*
* DerivedValue.h
*
* Created on: Jan 26, 2012
* Author: thduynguyen
*/
#pragma once
#include <boost/pool/singleton_pool.hpp>
#include <gtsam/base/Value.h>
namespace gtsam {
template<class DERIVED>
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<PoolTag, sizeof(DERIVED)>::malloc();
DERIVED* ptr = new(place) DERIVED(static_cast<const DERIVED&>(*this));
return ptr;
}
/**
* Create a duplicate object returned as a pointer to the generic Value interface
*/
virtual void deallocate_() const {
boost::singleton_pool<PoolTag, sizeof(DERIVED)>::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<const DERIVED&>(p);
// Return the result of calling equals on the derived class
return (static_cast<const DERIVED*>(this))->equals(derivedValue2, tol);
// return CallDerivedEquals(this, p, tol);
}
/// Generic Value interface version of retract
virtual std::auto_ptr<Value> retract_(const Vector& delta) const {
// Call retract on the derived class
const DERIVED retractResult = (static_cast<const DERIVED*>(this))->retract(delta);
// Create a Value pointer copy of the result
std::auto_ptr<Value> 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<const DERIVED&>(value2);
// Return the result of calling localCoordinates on the derived class
return (static_cast<const DERIVED*>(this))->localCoordinates(derivedValue2);
}
};
} /* namespace gtsam */

View File

@ -102,14 +102,17 @@ namespace gtsam {
public:
/** Allocate and construct a clone of this value */
virtual std::auto_ptr<Value> 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<class Derived>
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<const Derived&>(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<class Derived>
static std::auto_ptr<Value> 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<Value> 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<class Derived>
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<const Derived&>(value2);
// Return the result of calling localCoordinates on the derived class
return value1->localCoordinates(derivedValue2);
}
};
} /* namespace gtsam */

View File

@ -32,7 +32,7 @@
#endif
#endif
#include <gtsam/base/Value.h>
#include <gtsam/base/DerivedValue.h>
#include <gtsam/geometry/Point3.h>
#include <gtsam/3rdparty/Eigen/Eigen/Geometry>
@ -50,7 +50,7 @@ namespace gtsam {
* @ingroup geometry
* \nosubgrouping
*/
class Rot3 : public Value {
class Rot3 : public DerivedValue<Rot3> {
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<Value> clone_() const { return std::auto_ptr<Value>(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<Value> 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
/// @{