/* ---------------------------------------------------------------------------- * 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 testBAD.cpp * @date September 18, 2014 * @author Frank Dellaert * @brief unit tests for Block Automatic Differentiation */ #include #include #include #include #include #include #include #include using namespace std; using namespace gtsam; /// This class might have to become a class hierarchy ? template class Expression { public: /// Constructor with a single key Expression(Key key) { } /// Constructor with a value, yielding a constant Expression(const T& t) { } }; /// Expression version of transform Expression transformTo(const Expression& x, const Expression& p) { return Expression(0); } /// Expression version of project Expression project(const Expression& p) { return Expression(0); } /// Expression version of uncalibrate Expression uncalibrate(const Expression& K, const Expression& p) { return Expression(0); } /// Expression version of Point2.sub Expression operator -(const Expression& p, const Expression& q) { return Expression(0); } /// AD Factor template class BADFactor: NonlinearFactor { public: /// Constructor BADFactor(const Expression& t) { } /** * Calculate the error of the factor * This is typically equal to log-likelihood, e.g. \f$ 0.5(h(x)-z)^2/sigma^2 \f$ */ double error(const Values& c) const { return 0; } /// get the dimension of the factor (number of rows on linearization) size_t dim() const { return 0; } /// linearize to a GaussianFactor boost::shared_ptr linearize(const Values& c) 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; SharedDiagonal model = SharedDiagonal(); return boost::shared_ptr(new JacobianFactor(terms,b,model)); } }; /* ************************************************************************* */ TEST(BAD, test) { // Create some values Values values; 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); 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); // 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; // Create factor BADFactor f(e); // Check value EXPECT_DOUBLES_EQUAL(old.error(values), f.error(values), 1e-9); // Check dimension EXPECT_LONGS_EQUAL(0, f.dim()); // Check linearization boost::shared_ptr gf = f.linearize(values); EXPECT( assert_equal(*expected, *gf, 1e-9)); } /* ************************************************************************* */ int main() { TestResult tr; return TestRegistry::runAllTests(tr); } /* ************************************************************************* */