diff --git a/.cproject b/.cproject index c459bcfa2..7e077ef47 100644 --- a/.cproject +++ b/.cproject @@ -300,6 +300,7 @@ make + install true true @@ -307,6 +308,7 @@ make + check true true @@ -314,7 +316,6 @@ make - check true true @@ -322,6 +323,7 @@ make + testSimpleCamera.run true true @@ -337,7 +339,6 @@ make - testVSLAMFactor.run true true @@ -345,6 +346,7 @@ make + testCalibratedCamera.run true true @@ -352,7 +354,6 @@ make - testConditionalGaussian.run true true @@ -360,6 +361,7 @@ make + testPose2.run true true @@ -375,6 +377,7 @@ make + testRot3.run true true @@ -382,7 +385,6 @@ make - testNonlinearOptimizer.run true true @@ -390,6 +392,7 @@ make + testLinearFactor.run true true @@ -397,6 +400,7 @@ make + testConstrainedNonlinearFactorGraph.run true true @@ -404,6 +408,7 @@ make + testLinearFactorGraph.run true true @@ -411,7 +416,6 @@ make - testNonlinearFactorGraph.run true true @@ -419,6 +423,7 @@ make + testPose3.run true true @@ -426,7 +431,6 @@ make - testConstrainedLinearFactorGraph.run true true @@ -434,7 +438,6 @@ make - testVectorConfig.run true true @@ -442,15 +445,21 @@ make - testPoint2.run true true true + +make + +testNonlinearFactor.run +true +true +true + make - install true true @@ -458,7 +467,6 @@ make - clean true true @@ -466,7 +474,6 @@ make - check true true diff --git a/cpp/NonlinearFactor.cpp b/cpp/NonlinearFactor.cpp index 76d9b372d..7caaae1eb 100644 --- a/cpp/NonlinearFactor.cpp +++ b/cpp/NonlinearFactor.cpp @@ -19,7 +19,7 @@ NonlinearFactor1::NonlinearFactor1(const Vector& z, Vector (*h)(const Vector&), const string& key1, Matrix (*H)(const Vector&)) - : NonlinearFactor(z, sigma), h_(h), key1_(key1), H_(H) + : NonlinearFactor(z, sigma), h_(h), key_(key1), H_(H) { keys_.push_front(key1); } @@ -27,13 +27,16 @@ NonlinearFactor1::NonlinearFactor1(const Vector& z, /* ************************************************************************* */ void NonlinearFactor1::print(const string& s) const { cout << "NonLinearFactor1 " << s << endl; - NonlinearFactor::print(s); + cout << "h : " << (void*)h_ << endl; + cout << "key: " << key_ << endl; + cout << "H : " << (void*)H_ << endl; + NonlinearFactor::print("parent"); } /* ************************************************************************* */ LinearFactor::shared_ptr NonlinearFactor1::linearize(const VectorConfig& c) const { // get argument 1 from config - Vector x1 = c[key1_]; + Vector x1 = c[key_]; // Jacobian A = H(x1)/sigma Matrix A = H_(x1) / sigma_; @@ -41,7 +44,7 @@ LinearFactor::shared_ptr NonlinearFactor1::linearize(const VectorConfig& c) cons // Right-hand-side b = error(c) = (z - h(x1))/sigma Vector b = (z_ - h_(x1)) / sigma_; - LinearFactor::shared_ptr p(new LinearFactor(key1_, A, b)); + LinearFactor::shared_ptr p(new LinearFactor(key_, A, b)); return p; } @@ -53,7 +56,7 @@ bool NonlinearFactor1::equals(const Factor& f, double tol) const { if (p == NULL) return false; return NonlinearFactor::equals(*p, tol) && (h_ == p->h_) - && (key1_== p->key1_) + && (key_ == p->key_) && (H_ == p->H_); } @@ -75,7 +78,12 @@ NonlinearFactor2::NonlinearFactor2(const Vector& z, /* ************************************************************************* */ void NonlinearFactor2::print(const string& s) const { cout << "NonLinearFactor2 " << s << endl; - NonlinearFactor::print(s); + cout << "h : " << (void*)h_ << endl; + cout << "key1: " << key1_ << endl; + cout << "H2 : " << (void*)H2_ << endl; + cout << "key2: " << key2_ << endl; + cout << "H1 : " << (void*)H1_ << endl; + NonlinearFactor::print("parent"); } /* ************************************************************************* */ @@ -102,9 +110,9 @@ bool NonlinearFactor2::equals(const Factor& f, double tol) const { return NonlinearFactor::equals(*p, tol) && (h_ == p->h_) && (key1_ == p->key1_) - && (H2_ == p->H1_) + && (H1_ == p->H1_) && (key2_ == p->key2_) - && (H1_ == p->H2_); + && (H2_ == p->H2_); } /* ************************************************************************* */ diff --git a/cpp/NonlinearFactor.h b/cpp/NonlinearFactor.h index 4f52ff1d6..ba7f6d7e1 100644 --- a/cpp/NonlinearFactor.h +++ b/cpp/NonlinearFactor.h @@ -72,7 +72,7 @@ namespace gtsam { } /** Check if two NonlinearFactor objects are equal */ - bool equals(const Factor& f, double tol) const { + bool equals(const Factor& f, double tol=1e-9) const { const NonlinearFactor* p = dynamic_cast*> (&f); if (p == NULL) return false; return equal_with_abs_tol(z_,p->z_,tol) && fabs(sigma_ - p->sigma_)<=tol; @@ -123,7 +123,7 @@ namespace gtsam { class NonlinearFactor1 : public NonlinearFactor { private: - std::string key1_; + std::string key_; public: @@ -144,7 +144,7 @@ namespace gtsam { /** error function */ inline Vector error_vector(const VectorConfig& c) const { - return z_ - h_(c[key1_]); + return z_ - h_(c[key_]); } /** linearize a non-linearFactor1 to get a linearFactor1 */ @@ -172,8 +172,8 @@ namespace gtsam { Matrix (*H2_)(const Vector&, const Vector&); /** Constructor */ - NonlinearFactor2(const Vector& z, // the measurement - const double sigma, // the variance + NonlinearFactor2(const Vector& z, // the measurement + const double sigma, // the variance Vector (*h)(const Vector&, const Vector&), // the measurement function const std::string& key1, // key of the first variable Matrix (*H1)(const Vector&, const Vector&), // derivative of h in first variable diff --git a/cpp/smallExample.cpp b/cpp/smallExample.cpp index 8e0f7af0a..d1b73924f 100644 --- a/cpp/smallExample.cpp +++ b/cpp/smallExample.cpp @@ -6,9 +6,6 @@ * @author Frank dellaert */ - - -/*STL/C++*/ #include #include diff --git a/cpp/testNonlinearFactor.cpp b/cpp/testNonlinearFactor.cpp index a747fbfb5..f7917d9fd 100644 --- a/cpp/testNonlinearFactor.cpp +++ b/cpp/testNonlinearFactor.cpp @@ -13,12 +13,46 @@ #include "Matrix.h" #include "smallExample.h" +#include "Simulated2DMeasurement.h" using namespace std; using namespace gtsam; typedef boost::shared_ptr > shared_nlf; +/* ************************************************************************* */ +TEST( NonLinearFactor, equals ) +{ + double sigma = 1.0; + + // create two nonlinear2 factors + Vector z3(2); z3(0) = 0. ; z3(1) = -1.; + Simulated2DMeasurement f0(z3, sigma, "x1", "l1"); + + // measurement between x2 and l1 + Vector z4(2); z4(0)= -1.5 ; z4(1) = -1.; + Simulated2DMeasurement f1(z4, sigma, "x2", "l1"); + + CHECK(assert_equal(f0,f0)); + CHECK(f0.equals(f0)); + CHECK(!f0.equals(f1)); + CHECK(!f1.equals(f0)); +} + +/* ************************************************************************* */ +TEST( NonLinearFactor, equals2 ) +{ + // create a non linear factor graph + ExampleNonlinearFactorGraph fg = createNonlinearFactorGraph(); + + // get two factors + shared_nlf f0 = fg[0], f1 = fg[1]; + + CHECK(f0->equals(*f0)); + CHECK(!f0->equals(*f1)); + CHECK(!f1->equals(*f0)); +} + /* ************************************************************************* */ TEST( NonLinearFactor, NonlinearFactor ) {