From 92dbc8910e2a39607fa1129c4ba9919d815a62b6 Mon Sep 17 00:00:00 2001 From: Frank Dellaert Date: Mon, 31 Aug 2009 04:13:57 +0000 Subject: [PATCH] correct serialization of base classes moved all serialize functions to bottom of class declaration --- cpp/Cal3_S2.h | 2 +- cpp/FactorGraph.h | 17 ++++++------- cpp/NonlinearFactor.h | 49 ++++++++++++++++++++------------------ cpp/NonlinearFactorGraph.h | 24 +++++++++++-------- 4 files changed, 50 insertions(+), 42 deletions(-) diff --git a/cpp/Cal3_S2.h b/cpp/Cal3_S2.h index 26a60dd46..b5b28564d 100644 --- a/cpp/Cal3_S2.h +++ b/cpp/Cal3_S2.h @@ -89,7 +89,7 @@ namespace gtsam { /** * Check if equal up to specified tolerance */ - bool equals(const Cal3_S2& K, double tol) const; + bool equals(const Cal3_S2& K, double tol = 10e-9) const; /** friends */ friend Matrix Duncalibrate2(const Cal3_S2& K, const Point2& p); diff --git a/cpp/FactorGraph.h b/cpp/FactorGraph.h index fb900ad26..fdf935981 100644 --- a/cpp/FactorGraph.h +++ b/cpp/FactorGraph.h @@ -32,13 +32,6 @@ namespace gtsam { /** Collection of factors */ std::vector factors_; - /** Serialization function */ - friend class boost::serialization::access; - template - void serialize(Archive & ar, const unsigned int version) { - ar & BOOST_SERIALIZATION_NVP(factors_); - } - public: /** STL like, return the iterator pointing to the first factor */ @@ -109,6 +102,14 @@ namespace gtsam { fg.print(); return false; } - }; + private: + + /** Serialization function */ + friend class boost::serialization::access; + template + void serialize(Archive & ar, const unsigned int version) { + ar & BOOST_SERIALIZATION_NVP(factors_); + } + }; // FactorGraph } // namespace gtsam diff --git a/cpp/NonlinearFactor.h b/cpp/NonlinearFactor.h index 1b2e77f55..542ae31aa 100644 --- a/cpp/NonlinearFactor.h +++ b/cpp/NonlinearFactor.h @@ -33,23 +33,11 @@ namespace gtsam { //typedef boost::shared_ptr shared_ptr; /** - * Nonlinear factor which assume Gaussian noise on a measurement - * predicted by a non-linear function h + * Nonlinear factor which assumes Gaussian noise on a measurement + * predicted by a non-linear function h. */ class NonlinearFactor : public Factor { - private: - - /** Serialization function */ - friend class boost::serialization::access; - template - void serialize(Archive & ar, const unsigned int version) { -// ar & boost::serialization::base_object(*this); // TODO: needed ? - ar & BOOST_SERIALIZATION_NVP(z_); - ar & BOOST_SERIALIZATION_NVP(sigma_); - ar & BOOST_SERIALIZATION_NVP(keys_); - } - protected: Vector z_; // measurement @@ -93,13 +81,27 @@ namespace gtsam { /** dump the information of the factor into a string **/ std::string dump() const{return "";} - }; + + private: + + /** Serialization function */ + friend class boost::serialization::access; + template + void serialize(Archive & ar, const unsigned int version) { + // ar & boost::serialization::base_object(*this); // TODO: needed ? + ar & BOOST_SERIALIZATION_NVP(z_); + ar & BOOST_SERIALIZATION_NVP(sigma_); + ar & BOOST_SERIALIZATION_NVP(keys_); + } + + }; // NonlinearFactor - /* ************************************************************************* */ - /* a Gaussian nonlinear factor that takes 1 parameter */ - /* ************************************************************************* */ - + /** + * a Gaussian nonlinear factor that takes 1 parameter + * Note: cannot be serialized as contains function pointers + * Specialized derived classes could do this + */ class NonlinearFactor1 : public NonlinearFactor { public: @@ -130,10 +132,11 @@ namespace gtsam { std::string dump() const {return "";} }; - /* ************************************************************************* */ - /* A Gaussian nonlinear factor that takes 2 parameters */ - /* ************************************************************************* */ - + /** + * a Gaussian nonlinear factor that takes 2 parameters + * Note: cannot be serialized as contains function pointers + * Specialized derived classes could do this + */ class NonlinearFactor2 : public NonlinearFactor { public: diff --git a/cpp/NonlinearFactorGraph.h b/cpp/NonlinearFactorGraph.h index 8de58df57..fef282501 100644 --- a/cpp/NonlinearFactorGraph.h +++ b/cpp/NonlinearFactorGraph.h @@ -19,18 +19,11 @@ namespace gtsam { +typedef FactorGraph BaseFactorGraph; + /** Factor Graph Constsiting of non-linear factors */ -class NonlinearFactorGraph : public FactorGraph +class NonlinearFactorGraph : public BaseFactorGraph { -private: - - /** Serialization function */ - friend class boost::serialization::access; - template - void serialize(Archive & ar, const unsigned int version) { - ar & boost::serialization::base_object< FactorGraph >(*this); - } - public: // internal, exposed for testing only, doc in .cpp file FGConfig iterate(const FGConfig& config, const Ordering& ordering) const; @@ -117,5 +110,16 @@ public: // these you will probably want to use double lambda0, double lambdaFactor) const ; +private: + + /** Serialization function */ + friend class boost::serialization::access; + template + void serialize(Archive & ar, const unsigned int version) { + // do not use BOOST_SERIALIZATION_NVP for this name-value-pair ! It will crash. + ar & boost::serialization::make_nvp("BaseFactorGraph", + boost::serialization::base_object(*this)); + } + }; }