| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 22:39:48 +08:00
										 |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation, | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  |  * 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  BetweenFactorEM.h | 
					
						
							|  |  |  |  *  @author Vadim Indelman | 
					
						
							|  |  |  |  **/ | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <ostream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam/base/Testable.h>
 | 
					
						
							|  |  |  | #include <gtsam/base/Lie.h>
 | 
					
						
							|  |  |  | #include <gtsam/nonlinear/NonlinearFactor.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/GaussianFactor.h>
 | 
					
						
							| 
									
										
										
										
											2014-07-01 01:43:25 +08:00
										 |  |  | #include <gtsam/nonlinear/Marginals.h>
 | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * A class for a measurement predicted by "between(config[key1],config[key2])" | 
					
						
							|  |  |  |  * @tparam VALUE the Value type | 
					
						
							|  |  |  |  * @addtogroup SLAM | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | template<class VALUE> | 
					
						
							|  |  |  | class BetweenFactorEM: public NonlinearFactor { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   typedef VALUE T; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   typedef BetweenFactorEM<VALUE> This; | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |   typedef NonlinearFactor Base; | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |   Key key1_; | 
					
						
							|  |  |  |   Key key2_; | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   VALUE measured_; /** The measurement */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SharedGaussian model_inlier_; | 
					
						
							|  |  |  |   SharedGaussian model_outlier_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   double prior_inlier_; | 
					
						
							|  |  |  |   double prior_outlier_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   bool flag_bump_up_near_zero_probs_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** concept check by type */ | 
					
						
							|  |  |  |   GTSAM_CONCEPT_LIE_TYPE(T)GTSAM_CONCEPT_TESTABLE_TYPE(T) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // shorthand for a smart pointer to a factor
 | 
					
						
							|  |  |  |   typedef typename boost::shared_ptr<BetweenFactorEM> shared_ptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** default constructor - only use for serialization */ | 
					
						
							|  |  |  |   BetweenFactorEM() { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Constructor */ | 
					
						
							|  |  |  |   BetweenFactorEM(Key key1, Key key2, const VALUE& measured, | 
					
						
							|  |  |  |       const SharedGaussian& model_inlier, const SharedGaussian& model_outlier, | 
					
						
							|  |  |  |       const double prior_inlier, const double prior_outlier, | 
					
						
							|  |  |  |       const bool flag_bump_up_near_zero_probs = false) : | 
					
						
							|  |  |  |       Base(cref_list_of<2>(key1)(key2)), key1_(key1), key2_(key2), measured_( | 
					
						
							|  |  |  |           measured), model_inlier_(model_inlier), model_outlier_(model_outlier), prior_inlier_( | 
					
						
							|  |  |  |           prior_inlier), prior_outlier_(prior_outlier), flag_bump_up_near_zero_probs_( | 
					
						
							|  |  |  |           flag_bump_up_near_zero_probs) { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   virtual ~BetweenFactorEM() { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** implement functions needed for Testable */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** print */ | 
					
						
							|  |  |  |   virtual void print(const std::string& s, const KeyFormatter& keyFormatter = | 
					
						
							|  |  |  |       DefaultKeyFormatter) const { | 
					
						
							|  |  |  |     std::cout << s << "BetweenFactorEM(" << keyFormatter(key1_) << "," | 
					
						
							|  |  |  |         << keyFormatter(key2_) << ")\n"; | 
					
						
							|  |  |  |     measured_.print("  measured: "); | 
					
						
							|  |  |  |     model_inlier_->print("  noise model inlier: "); | 
					
						
							|  |  |  |     model_outlier_->print("  noise model outlier: "); | 
					
						
							|  |  |  |     std::cout << "(prior_inlier, prior_outlier_) = (" << prior_inlier_ << "," | 
					
						
							|  |  |  |         << prior_outlier_ << ")\n"; | 
					
						
							|  |  |  |     //      Base::print(s, keyFormatter);
 | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** equals */ | 
					
						
							|  |  |  |   virtual bool equals(const NonlinearFactor& f, double tol = 1e-9) const { | 
					
						
							|  |  |  |     const This *t = dynamic_cast<const This*>(&f); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (t && Base::equals(f)) | 
					
						
							|  |  |  |       return key1_ == t->key1_ && key2_ == t->key2_ | 
					
						
							|  |  |  |           && | 
					
						
							|  |  |  |           //            model_inlier_->equals(t->model_inlier_ ) && // TODO: fix here
 | 
					
						
							|  |  |  |           //            model_outlier_->equals(t->model_outlier_ ) &&
 | 
					
						
							|  |  |  |           prior_outlier_ == t->prior_outlier_ | 
					
						
							|  |  |  |           && prior_inlier_ == t->prior_inlier_ && measured_.equals(t->measured_); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** implement functions needed to derive from Factor */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |   virtual double error(const Values& x) const { | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     return whitenedError(x).squaredNorm(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  |   /**
 | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |    * Linearize a non-linearFactorN to get a GaussianFactor, | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |    * \f$ Ax-b \approx h(x+\delta x)-z = h(x) + A \delta x - z \f$ | 
					
						
							|  |  |  |    * Hence \f$ b = z - h(x) = - \mathtt{error\_vector}(x) \f$ | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |   /* This version of linearize recalculates the noise model each time */ | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |   virtual boost::shared_ptr<GaussianFactor> linearize( | 
					
						
							|  |  |  |       const Values& x) const { | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     // Only linearize if the factor is active
 | 
					
						
							|  |  |  |     if (!this->active(x)) | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |       return boost::shared_ptr<JacobianFactor>(); | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     //std::cout<<"About to linearize"<<std::endl;
 | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |     Matrix A1, A2; | 
					
						
							|  |  |  |     std::vector<Matrix> A(this->size()); | 
					
						
							|  |  |  |     Vector b = -whitenedError(x, A); | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     A1 = A[0]; | 
					
						
							|  |  |  |     A2 = A[1]; | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |     return GaussianFactor::shared_ptr( | 
					
						
							|  |  |  |         new JacobianFactor(key1_, A1, key2_, A2, b, | 
					
						
							|  |  |  |             noiseModel::Unit::Create(b.size()))); | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |   /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |   Vector whitenedError(const Values& x, | 
					
						
							|  |  |  |       boost::optional<std::vector<Matrix>&> H = boost::none) const { | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     bool debug = true; | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     const T& p1 = x.at<T>(key1_); | 
					
						
							|  |  |  |     const T& p2 = x.at<T>(key2_); | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     Matrix H1, H2; | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     T hx = p1.between(p2, H1, H2); // h(x)
 | 
					
						
							|  |  |  |     // manifold equivalent of h(x)-z -> log(z,h(x))
 | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     Vector err = measured_.localCoordinates(hx); | 
					
						
							| 
									
										
										
										
											2013-08-23 23:22:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     // Calculate indicator probabilities (inlier and outlier)
 | 
					
						
							|  |  |  |     Vector p_inlier_outlier = calcIndicatorProb(x); | 
					
						
							|  |  |  |     double p_inlier = p_inlier_outlier[0]; | 
					
						
							|  |  |  |     double p_outlier = p_inlier_outlier[1]; | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     Vector err_wh_inlier = model_inlier_->whiten(err); | 
					
						
							|  |  |  |     Vector err_wh_outlier = model_outlier_->whiten(err); | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     Matrix invCov_inlier = model_inlier_->R().transpose() * model_inlier_->R(); | 
					
						
							|  |  |  |     Matrix invCov_outlier = model_outlier_->R().transpose() | 
					
						
							|  |  |  |         * model_outlier_->R(); | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     Vector err_wh_eq; | 
					
						
							|  |  |  |     err_wh_eq.resize(err_wh_inlier.rows() * 2); | 
					
						
							|  |  |  |     err_wh_eq << sqrt(p_inlier) * err_wh_inlier.array(), sqrt(p_outlier) | 
					
						
							|  |  |  |         * err_wh_outlier.array(); | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     if (H) { | 
					
						
							|  |  |  |       // stack Jacobians for the two indicators for each of the key
 | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |       Matrix H1_inlier = sqrt(p_inlier) * model_inlier_->Whiten(H1); | 
					
						
							|  |  |  |       Matrix H1_outlier = sqrt(p_outlier) * model_outlier_->Whiten(H1); | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |       Matrix H1_aug = stack(2, &H1_inlier, &H1_outlier); | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |       Matrix H2_inlier = sqrt(p_inlier) * model_inlier_->Whiten(H2); | 
					
						
							|  |  |  |       Matrix H2_outlier = sqrt(p_outlier) * model_outlier_->Whiten(H2); | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |       Matrix H2_aug = stack(2, &H2_inlier, &H2_outlier); | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |       (*H)[0].resize(H1_aug.rows(), H1_aug.cols()); | 
					
						
							|  |  |  |       (*H)[1].resize(H2_aug.rows(), H2_aug.cols()); | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |       (*H)[0] = H1_aug; | 
					
						
							|  |  |  |       (*H)[1] = H2_aug; | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     if (debug) { | 
					
						
							|  |  |  |       //        std::cout<<"unwhitened error: "<<err[0]<<" "<<err[1]<<" "<<err[2]<<std::endl;
 | 
					
						
							|  |  |  |       //        std::cout<<"err_wh_inlier: "<<err_wh_inlier[0]<<" "<<err_wh_inlier[1]<<" "<<err_wh_inlier[2]<<std::endl;
 | 
					
						
							|  |  |  |       //        std::cout<<"err_wh_outlier: "<<err_wh_outlier[0]<<" "<<err_wh_outlier[1]<<" "<<err_wh_outlier[2]<<std::endl;
 | 
					
						
							|  |  |  |       //
 | 
					
						
							|  |  |  |       //        std::cout<<"p_inlier, p_outlier, sumP: "<<p_inlier<<" "<<p_outlier<<" " << sumP << std::endl;
 | 
					
						
							|  |  |  |       //
 | 
					
						
							|  |  |  |       //        std::cout<<"prior_inlier_, prior_outlier_: "<<prior_inlier_<<" "<<prior_outlier_<< std::endl;
 | 
					
						
							|  |  |  |       //
 | 
					
						
							|  |  |  |       //        double s_inl  = -0.5 * err_wh_inlier.dot(err_wh_inlier);
 | 
					
						
							|  |  |  |       //        double s_outl = -0.5 * err_wh_outlier.dot(err_wh_outlier);
 | 
					
						
							|  |  |  |       //        std::cout<<"s_inl, s_outl: "<<s_inl<<" "<<s_outl<<std::endl;
 | 
					
						
							|  |  |  |       //
 | 
					
						
							|  |  |  |       //        std::cout<<"norm of invCov_inlier, invCov_outlier: "<<invCov_inlier.norm()<<" "<<invCov_outlier.norm()<<std::endl;
 | 
					
						
							|  |  |  |       //        double q_inl  = invCov_inlier.norm() * exp( -0.5 * err_wh_inlier.dot(err_wh_inlier) );
 | 
					
						
							|  |  |  |       //        double q_outl = invCov_outlier.norm() * exp( -0.5 * err_wh_outlier.dot(err_wh_outlier) );
 | 
					
						
							|  |  |  |       //        std::cout<<"q_inl, q_outl: "<<q_inl<<" "<<q_outl<<std::endl;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       //        Matrix Cov_inlier  = invCov_inlier.inverse();
 | 
					
						
							|  |  |  |       //        Matrix Cov_outlier = invCov_outlier.inverse();
 | 
					
						
							|  |  |  |       //        std::cout<<"Cov_inlier: "<<std::endl<<
 | 
					
						
							|  |  |  |       //            Cov_inlier(0,0) << " " << Cov_inlier(0,1) << " " << Cov_inlier(0,2) <<std::endl<<
 | 
					
						
							|  |  |  |       //            Cov_inlier(1,0) << " " << Cov_inlier(1,1) << " " << Cov_inlier(1,2) <<std::endl<<
 | 
					
						
							|  |  |  |       //            Cov_inlier(2,0) << " " << Cov_inlier(2,1) << " " << Cov_inlier(2,2) <<std::endl;
 | 
					
						
							|  |  |  |       //        std::cout<<"Cov_outlier: "<<std::endl<<
 | 
					
						
							|  |  |  |       //                    Cov_outlier(0,0) << " " << Cov_outlier(0,1) << " " << Cov_outlier(0,2) <<std::endl<<
 | 
					
						
							|  |  |  |       //                    Cov_outlier(1,0) << " " << Cov_outlier(1,1) << " " << Cov_outlier(1,2) <<std::endl<<
 | 
					
						
							|  |  |  |       //                    Cov_outlier(2,0) << " " << Cov_outlier(2,1) << " " << Cov_outlier(2,2) <<std::endl;
 | 
					
						
							|  |  |  |       //        std::cout<<"===="<<std::endl;
 | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     return err_wh_eq; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |   /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |   Vector calcIndicatorProb(const Values& x) const { | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     bool debug = false; | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     Vector err = unwhitenedError(x); | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     // Calculate indicator probabilities (inlier and outlier)
 | 
					
						
							|  |  |  |     Vector err_wh_inlier = model_inlier_->whiten(err); | 
					
						
							|  |  |  |     Vector err_wh_outlier = model_outlier_->whiten(err); | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     Matrix invCov_inlier = model_inlier_->R().transpose() * model_inlier_->R(); | 
					
						
							|  |  |  |     Matrix invCov_outlier = model_outlier_->R().transpose() | 
					
						
							|  |  |  |         * model_outlier_->R(); | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     double p_inlier = prior_inlier_ * std::sqrt(invCov_inlier.determinant()) | 
					
						
							|  |  |  |         * exp(-0.5 * err_wh_inlier.dot(err_wh_inlier)); | 
					
						
							|  |  |  |     double p_outlier = prior_outlier_ * std::sqrt(invCov_outlier.determinant()) | 
					
						
							|  |  |  |         * exp(-0.5 * err_wh_outlier.dot(err_wh_outlier)); | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     if (debug) { | 
					
						
							|  |  |  |       std::cout << "in calcIndicatorProb. err_unwh: " << err[0] << ", " | 
					
						
							|  |  |  |           << err[1] << ", " << err[2] << std::endl; | 
					
						
							|  |  |  |       std::cout << "in calcIndicatorProb. err_wh_inlier: " << err_wh_inlier[0] | 
					
						
							|  |  |  |           << ", " << err_wh_inlier[1] << ", " << err_wh_inlier[2] << std::endl; | 
					
						
							|  |  |  |       std::cout << "in calcIndicatorProb. err_wh_inlier.dot(err_wh_inlier): " | 
					
						
							|  |  |  |           << err_wh_inlier.dot(err_wh_inlier) << std::endl; | 
					
						
							|  |  |  |       std::cout << "in calcIndicatorProb. err_wh_outlier.dot(err_wh_outlier): " | 
					
						
							|  |  |  |           << err_wh_outlier.dot(err_wh_outlier) << std::endl; | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |       std::cout | 
					
						
							|  |  |  |           << "in calcIndicatorProb. p_inlier, p_outlier before normalization: " | 
					
						
							|  |  |  |           << p_inlier << ", " << p_outlier << std::endl; | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     double sumP = p_inlier + p_outlier; | 
					
						
							|  |  |  |     p_inlier /= sumP; | 
					
						
							|  |  |  |     p_outlier /= sumP; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (flag_bump_up_near_zero_probs_) { | 
					
						
							|  |  |  |       // Bump up near-zero probabilities (as in linerFlow.h)
 | 
					
						
							|  |  |  |       double minP = 0.05; // == 0.1 / 2 indicator variables
 | 
					
						
							|  |  |  |       if (p_inlier < minP || p_outlier < minP) { | 
					
						
							|  |  |  |         if (p_inlier < minP) | 
					
						
							|  |  |  |           p_inlier = minP; | 
					
						
							|  |  |  |         if (p_outlier < minP) | 
					
						
							|  |  |  |           p_outlier = minP; | 
					
						
							|  |  |  |         sumP = p_inlier + p_outlier; | 
					
						
							|  |  |  |         p_inlier /= sumP; | 
					
						
							|  |  |  |         p_outlier /= sumP; | 
					
						
							| 
									
										
										
										
											2013-08-08 23:12:21 +08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2014-07-01 01:43:25 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-23 08:35:27 +08:00
										 |  |  |     return (Vector(2) << p_inlier, p_outlier).finished(); | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |   Vector unwhitenedError(const Values& x) const { | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const T& p1 = x.at<T>(key1_); | 
					
						
							|  |  |  |     const T& p2 = x.at<T>(key2_); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Matrix H1, H2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     T hx = p1.between(p2, H1, H2); // h(x)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return measured_.localCoordinates(hx); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   void set_flag_bump_up_near_zero_probs(bool flag) { | 
					
						
							|  |  |  |     flag_bump_up_near_zero_probs_ = flag; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   bool get_flag_bump_up_near_zero_probs() const { | 
					
						
							|  |  |  |     return flag_bump_up_near_zero_probs_; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   SharedGaussian get_model_inlier() const { | 
					
						
							|  |  |  |     return model_inlier_; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   SharedGaussian get_model_outlier() const { | 
					
						
							|  |  |  |     return model_outlier_; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   Matrix get_model_inlier_cov() const { | 
					
						
							|  |  |  |     return (model_inlier_->R().transpose() * model_inlier_->R()).inverse(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   Matrix get_model_outlier_cov() const { | 
					
						
							|  |  |  |     return (model_outlier_->R().transpose() * model_outlier_->R()).inverse(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |   void updateNoiseModels(const Values& values, | 
					
						
							|  |  |  |       const NonlinearFactorGraph& graph) { | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     /* Update model_inlier_ and model_outlier_ to account for uncertainty in robot trajectories
 | 
					
						
							|  |  |  |      * (note these are given in the E step, where indicator probabilities are calculated). | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Principle: R += [H1 H2] * joint_cov12 * [H1 H2]', where H1, H2 are Jacobians of the | 
					
						
							|  |  |  |      * unwhitened error w.r.t. states, and R is the measurement covariance (inlier or outlier modes). | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * TODO: improve efficiency (info form) | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     // get joint covariance of the involved states
 | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |     KeyVector Keys; | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     Keys.push_back(key1_); | 
					
						
							|  |  |  |     Keys.push_back(key2_); | 
					
						
							|  |  |  |     Marginals marginals(graph, values, Marginals::QR); | 
					
						
							|  |  |  |     JointMarginal joint_marginal12 = marginals.jointMarginalCovariance(Keys); | 
					
						
							|  |  |  |     Matrix cov1 = joint_marginal12(key1_, key1_); | 
					
						
							|  |  |  |     Matrix cov2 = joint_marginal12(key2_, key2_); | 
					
						
							|  |  |  |     Matrix cov12 = joint_marginal12(key1_, key2_); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     updateNoiseModels_givenCovs(values, cov1, cov2, cov12); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |   void updateNoiseModels_givenCovs(const Values& values, | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |       const Matrix& cov1, const Matrix& cov2, const Matrix& cov12) { | 
					
						
							|  |  |  |     /* Update model_inlier_ and model_outlier_ to account for uncertainty in robot trajectories
 | 
					
						
							|  |  |  |      * (note these are given in the E step, where indicator probabilities are calculated). | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Principle: R += [H1 H2] * joint_cov12 * [H1 H2]', where H1, H2 are Jacobians of the | 
					
						
							|  |  |  |      * unwhitened error w.r.t. states, and R is the measurement covariance (inlier or outlier modes). | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * TODO: improve efficiency (info form) | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2013-08-07 23:25:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     const T& p1 = values.at<T>(key1_); | 
					
						
							|  |  |  |     const T& p2 = values.at<T>(key2_); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Matrix H1, H2; | 
					
						
							|  |  |  |     p1.between(p2, H1, H2); // h(x)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Matrix H; | 
					
						
							|  |  |  |     H.resize(H1.rows(), H1.rows() + H2.rows()); | 
					
						
							|  |  |  |     H << H1, H2; // H = [H1 H2]
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Matrix joint_cov; | 
					
						
							|  |  |  |     joint_cov.resize(cov1.rows() + cov2.rows(), cov1.cols() + cov2.cols()); | 
					
						
							|  |  |  |     joint_cov << cov1, cov12, cov12.transpose(), cov2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Matrix cov_state = H * joint_cov * H.transpose(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //       model_inlier_->print("before:");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // update inlier and outlier noise models
 | 
					
						
							|  |  |  |     Matrix covRinlier = | 
					
						
							|  |  |  |         (model_inlier_->R().transpose() * model_inlier_->R()).inverse(); | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |     model_inlier_ = noiseModel::Gaussian::Covariance( | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |         covRinlier + cov_state); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Matrix covRoutlier = | 
					
						
							|  |  |  |         (model_outlier_->R().transpose() * model_outlier_->R()).inverse(); | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  |     model_outlier_ = noiseModel::Gaussian::Covariance( | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |         covRoutlier + cov_state); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //       model_inlier_->print("after:");
 | 
					
						
							|  |  |  |     //       std::cout<<"covRinlier + cov_state: "<<covRinlier + cov_state<<std::endl;
 | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   /** return the measured */ | 
					
						
							|  |  |  |   const VALUE& measured() const { | 
					
						
							|  |  |  |     return measured_; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** number of variables attached to this factor */ | 
					
						
							|  |  |  |   std::size_t size() const { | 
					
						
							|  |  |  |     return 2; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   virtual size_t dim() const { | 
					
						
							|  |  |  |     return model_inlier_->R().rows() + model_inlier_->R().cols(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Serialization function */ | 
					
						
							|  |  |  |   friend class boost::serialization::access; | 
					
						
							|  |  |  |   template<class ARCHIVE> | 
					
						
							| 
									
										
										
										
											2015-03-06 23:12:09 +08:00
										 |  |  |   void serialize(ARCHIVE & ar, const unsigned int /*version*/) { | 
					
						
							| 
									
										
										
										
											2014-11-10 23:00:44 +08:00
										 |  |  |     ar | 
					
						
							|  |  |  |         & boost::serialization::make_nvp("NonlinearFactor", | 
					
						
							|  |  |  |             boost::serialization::base_object<Base>(*this)); | 
					
						
							|  |  |  |     ar & BOOST_SERIALIZATION_NVP(measured_); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | // \class BetweenFactorEM
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 13:58:50 +08:00
										 |  |  | }  // namespace gtsam
 |