From 7a64e2e54698e90cd2afc0538060ed754e85974b Mon Sep 17 00:00:00 2001 From: dellaert Date: Sun, 21 Sep 2014 16:30:30 +0200 Subject: [PATCH] Class hierarchy --- gtsam_unstable/base/tests/testBAD.cpp | 107 ++++++++++++++++++-------- 1 file changed, 73 insertions(+), 34 deletions(-) diff --git a/gtsam_unstable/base/tests/testBAD.cpp b/gtsam_unstable/base/tests/testBAD.cpp index 5ed02edd0..e97b8c6f3 100644 --- a/gtsam_unstable/base/tests/testBAD.cpp +++ b/gtsam_unstable/base/tests/testBAD.cpp @@ -27,55 +27,90 @@ #include -using namespace std; -using namespace gtsam; +namespace gtsam { -/// This class might have to become a class hierarchy ? +/// Base class template class Expression { public: - /// Constructor with a single key - Expression(Key key) { - } + typedef Expression This; + typedef boost::shared_ptr shared_ptr; + + virtual T value(const Values& values) const = 0; +}; + +/// Constant Expression +template +class ConstantExpression: public Expression { + + T value_; + +public: /// Constructor with a value, yielding a constant - Expression(const T& t) { + ConstantExpression(const T& value) : + value_(value) { + } + + T value(const Values& values) const { + return value_; + } +}; + +/// Leaf Expression +template +class LeafExpression: public Expression { + + Key key_; + +public: + + /// Constructor with a single key + LeafExpression(Key key) { + } + + T value(const Values& values) const { + return values.at(key_); } }; /// Expression version of transform -Expression transformTo(const Expression& x, +LeafExpression transformTo(const Expression& x, const Expression& p) { - return Expression(0); + return LeafExpression(0); } /// Expression version of project -Expression project(const Expression& p) { - return Expression(0); +LeafExpression project(const Expression& p) { + return LeafExpression(0); } /// Expression version of uncalibrate -Expression uncalibrate(const Expression& K, +LeafExpression uncalibrate(const Expression& K, const Expression& p) { - return Expression(0); + return LeafExpression(0); } /// Expression version of Point2.sub -Expression operator -(const Expression& p, +LeafExpression operator -(const Expression& p, const Expression& q) { - return Expression(0); + return LeafExpression(0); } /// AD Factor -template +template class BADFactor: NonlinearFactor { + const T measurement_; + const E expression_; + public: /// Constructor - BADFactor(const Expression& t) { + BADFactor(const T& measurement, const E& expression) : + measurement_(measurement), expression_(expression) { } /** @@ -92,17 +127,23 @@ public: } /// linearize to a GaussianFactor - boost::shared_ptr linearize(const Values& c) const { + boost::shared_ptr linearize(const Values& values) const { // We will construct an n-ary factor below, where terms is a container whose // value type is std::pair, specifying the // collection of keys and matrices making up the factor. - std::map terms; - Vector b; + std::map terms; + const T& value = expression_.value(values); + Vector b = measurement_.localCoordinates(value); SharedDiagonal model = SharedDiagonal(); - return boost::shared_ptr(new JacobianFactor(terms,b,model)); + return boost::shared_ptr( + new JacobianFactor(terms, b, model)); } }; +} + +using namespace std; +using namespace gtsam; /* ************************************************************************* */ @@ -110,30 +151,28 @@ TEST(BAD, test) { // Create some values Values values; - values.insert(1,Pose3()); - values.insert(2,Point3(0,0,1)); - values.insert(3,Cal3_S2()); + values.insert(1, Pose3()); + values.insert(2, Point3(0, 0, 1)); + values.insert(3, Cal3_S2()); // Create old-style factor to create expected value and derivatives - Point2 measured(0,1); + Point2 measured(0, 1); SharedNoiseModel model = noiseModel::Unit::Create(2); GeneralSFMFactor2 old(measured, model, 1, 2, 3); GaussianFactor::shared_ptr expected = old.linearize(values); // Create leaves - Expression x(1); - Expression p(2); - Expression K(3); - Expression uv(measured); + LeafExpression x(1); + LeafExpression p(2); + LeafExpression K(3); // Create expression tree - Expression p_cam = transformTo(x, p); - Expression projection = project(p_cam); - Expression uv_hat = uncalibrate(K, projection); - Expression e = uv - uv_hat; + LeafExpression p_cam = transformTo(x, p); + LeafExpression projection = project(p_cam); + LeafExpression uv_hat = uncalibrate(K, projection); // Create factor - BADFactor f(e); + BADFactor > f(measured, uv_hat); // Check value EXPECT_DOUBLES_EQUAL(old.error(values), f.error(values), 1e-9);