| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * 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 <gtsam/nonlinear/NonlinearFactor.h>
 | 
					
						
							|  |  |  | #include <gtsam/geometry/Pose3.h>
 | 
					
						
							|  |  |  | #include <gtsam/geometry/Cal3_S2.h>
 | 
					
						
							| 
									
										
										
										
											2014-09-21 17:33:53 +08:00
										 |  |  | #include <gtsam/slam/GeneralSFMFactor.h>
 | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  | #include <gtsam/inference/Key.h>
 | 
					
						
							| 
									
										
										
										
											2014-09-19 06:33:11 +08:00
										 |  |  | #include <gtsam/base/Testable.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/make_shared.hpp>
 | 
					
						
							| 
									
										
										
										
											2014-09-22 00:54:01 +08:00
										 |  |  | #include <boost/foreach.hpp>
 | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-21 22:30:30 +08:00
										 |  |  | namespace gtsam { | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  | ///-----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | /// Expression node. The superclass for objects that do the heavy lifting
 | 
					
						
							|  |  |  | /// An Expression<T> has a pointer to an ExpressionNode<T> underneath
 | 
					
						
							|  |  |  | /// allowing Expressions to have polymorphic behaviour even though they
 | 
					
						
							|  |  |  | /// are passed by value. This is the same way boost::function works.
 | 
					
						
							|  |  |  | /// http://loki-lib.sourceforge.net/html/a00652.html
 | 
					
						
							|  |  |  | template<class T> | 
					
						
							|  |  |  | class ExpressionNode { | 
					
						
							|  |  |  |  public: | 
					
						
							|  |  |  |   ExpressionNode(){} | 
					
						
							|  |  |  |   virtual ~ExpressionNode(){} | 
					
						
							|  |  |  |   virtual void getKeys(std::set<Key>& keys) const = 0; | 
					
						
							|  |  |  |   virtual T value(const Values& values, | 
					
						
							|  |  |  |                   boost::optional<std::map<Key, Matrix>&> = boost::none) const = 0; | 
					
						
							|  |  |  |   virtual ExpressionNode<T>* clone() const = 0; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-21 22:30:30 +08:00
										 |  |  | /// Constant Expression
 | 
					
						
							|  |  |  | template<class T> | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  | class ConstantExpression : public ExpressionNode<T> { | 
					
						
							| 
									
										
										
										
											2014-09-21 22:30:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   T value_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |  public: | 
					
						
							| 
									
										
										
										
											2014-09-21 22:30:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-21 23:37:09 +08:00
										 |  |  |   typedef T type; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-21 22:30:30 +08:00
										 |  |  |   /// Constructor with a value, yielding a constant
 | 
					
						
							|  |  |  |   ConstantExpression(const T& value) : | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |     value_(value) { | 
					
						
							| 
									
										
										
										
											2014-09-21 22:30:30 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   virtual ~ConstantExpression(){} | 
					
						
							| 
									
										
										
										
											2014-09-21 22:30:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   virtual void getKeys(std::set<Key>& /* keys */) const {} | 
					
						
							|  |  |  |   virtual T value(const Values& values, | 
					
						
							|  |  |  |                   boost::optional<std::map<Key, Matrix>&> jacobians = boost::none) const { | 
					
						
							| 
									
										
										
										
											2014-09-21 22:30:30 +08:00
										 |  |  |     return value_; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   virtual ExpressionNode<T>* clone() const { return new ConstantExpression(*this); } | 
					
						
							| 
									
										
										
										
											2014-09-21 22:30:30 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-21 23:37:09 +08:00
										 |  |  | //-----------------------------------------------------------------------------
 | 
					
						
							| 
									
										
										
										
											2014-09-21 22:30:30 +08:00
										 |  |  | /// Leaf Expression
 | 
					
						
							|  |  |  | template<class T> | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  | class LeafExpression : public ExpressionNode<T> { | 
					
						
							| 
									
										
										
										
											2014-09-21 22:30:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Key key_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |  public: | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-21 23:37:09 +08:00
										 |  |  |   typedef T type; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  |   /// Constructor with a single key
 | 
					
						
							| 
									
										
										
										
											2014-09-21 23:37:09 +08:00
										 |  |  |   LeafExpression(Key key) : | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |     key_(key) { | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   virtual ~LeafExpression(){} | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   virtual void getKeys(std::set<Key>& keys) const { keys.insert(key_); } | 
					
						
							|  |  |  |   virtual T value(const Values& values, | 
					
						
							|  |  |  |                   boost::optional<std::map<Key, Matrix>&> jacobians = boost::none) const { | 
					
						
							| 
									
										
										
										
											2014-09-22 00:22:28 +08:00
										 |  |  |     const T& value = values.at<T>(key_); | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |     if( jacobians ) { | 
					
						
							|  |  |  |       std::map<Key, Matrix>::iterator it = jacobians->find(key_); | 
					
						
							|  |  |  |       if(it != jacobians->end()) { | 
					
						
							|  |  |  |         it->second += Eigen::MatrixXd::Identity(value.dim(), value.dim()); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         (*jacobians)[key_] = Eigen::MatrixXd::Identity(value.dim(), value.dim()); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return value; | 
					
						
							| 
									
										
										
										
											2014-09-22 00:22:28 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   virtual ExpressionNode<T>* clone() const { return new LeafExpression(*this); } | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-21 23:43:47 +08:00
										 |  |  | //-----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | /// Unary Expression
 | 
					
						
							|  |  |  | template<class T, class E> | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  | class UnaryExpression : public ExpressionNode<T> { | 
					
						
							| 
									
										
										
										
											2014-09-21 23:43:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |  public: | 
					
						
							| 
									
										
										
										
											2014-09-21 23:43:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   typedef T (*function)(const E&, boost::optional<Matrix&>); | 
					
						
							| 
									
										
										
										
											2014-09-21 23:43:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |  private: | 
					
						
							| 
									
										
										
										
											2014-09-21 23:43:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   boost::shared_ptr< ExpressionNode<E> > expression_; | 
					
						
							| 
									
										
										
										
											2014-09-21 23:43:47 +08:00
										 |  |  |   function f_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |  public: | 
					
						
							| 
									
										
										
										
											2014-09-21 23:43:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   typedef T type; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Constructor with a single key
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   UnaryExpression(function f, const ExpressionNode<E>& expression) : | 
					
						
							|  |  |  |     expression_(expression.clone()), f_(f) { | 
					
						
							| 
									
										
										
										
											2014-09-21 23:43:47 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   virtual ~UnaryExpression(){} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   virtual void getKeys(std::set<Key>& keys) const{ expression_->getKeys(keys); } | 
					
						
							|  |  |  |   virtual T value(const Values& values, | 
					
						
							|  |  |  |                   boost::optional<std::map<Key, Matrix>&> jacobians = boost::none) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     T value; | 
					
						
							|  |  |  |     if(jacobians) { | 
					
						
							|  |  |  |       Eigen::MatrixXd H; | 
					
						
							|  |  |  |       value = f_(expression_->value(values, jacobians), H); | 
					
						
							|  |  |  |       std::map<Key, Matrix>::iterator it = jacobians->begin(); | 
					
						
							|  |  |  |       for( ; it != jacobians->end(); ++it) { | 
					
						
							|  |  |  |         it->second = H * it->second; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       value = f_(expression_->value(values), boost::none); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return value; | 
					
						
							| 
									
										
										
										
											2014-09-21 23:43:47 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-09-22 00:22:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   virtual ExpressionNode<T>* clone() const { return new UnaryExpression(*this); } | 
					
						
							| 
									
										
										
										
											2014-09-21 23:43:47 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-21 23:37:09 +08:00
										 |  |  | //-----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | /// Binary Expression
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-21 23:37:09 +08:00
										 |  |  | template<class T, class E1, class E2> | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  | class BinaryExpression : public ExpressionNode<T> { | 
					
						
							| 
									
										
										
										
											2014-09-21 23:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |  public: | 
					
						
							| 
									
										
										
										
											2014-09-21 23:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   typedef T (*function)(const E1&, const E2&, | 
					
						
							| 
									
										
										
										
											2014-09-22 00:36:19 +08:00
										 |  |  |       boost::optional<Matrix&>, boost::optional<Matrix&>); | 
					
						
							| 
									
										
										
										
											2014-09-21 23:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |  private: | 
					
						
							| 
									
										
										
										
											2014-09-21 23:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   boost::shared_ptr< ExpressionNode<E1> > expression1_; | 
					
						
							|  |  |  |   boost::shared_ptr< ExpressionNode<E2> > expression2_; | 
					
						
							| 
									
										
										
										
											2014-09-21 23:37:09 +08:00
										 |  |  |   function f_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |  public: | 
					
						
							| 
									
										
										
										
											2014-09-21 23:37:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   typedef T type; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Constructor with a single key
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   BinaryExpression(function f, const ExpressionNode<E1>& expression1, const ExpressionNode<E2>& expression2) : | 
					
						
							|  |  |  |     expression1_(expression1.clone()), expression2_(expression2.clone()), f_(f) { | 
					
						
							| 
									
										
										
										
											2014-09-21 23:37:09 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   virtual ~BinaryExpression(){} | 
					
						
							| 
									
										
										
										
											2014-09-21 23:37:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   virtual void getKeys(std::set<Key>& keys) const{ | 
					
						
							|  |  |  |     expression1_->getKeys(keys); | 
					
						
							|  |  |  |     expression2_->getKeys(keys); | 
					
						
							| 
									
										
										
										
											2014-09-21 23:37:09 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   virtual T value(const Values& values, | 
					
						
							|  |  |  |                   boost::optional<std::map<Key, Matrix>&> jacobians = boost::none) const { | 
					
						
							|  |  |  |     T val; | 
					
						
							|  |  |  |     if(jacobians) { | 
					
						
							|  |  |  |       std::map<Key, Matrix> terms1; | 
					
						
							|  |  |  |       std::map<Key, Matrix> terms2; | 
					
						
							|  |  |  |       Matrix H1, H2; | 
					
						
							|  |  |  |       val = f_(expression1_->value(values, terms1), expression2_->value(values, terms2), H1, H2); | 
					
						
							|  |  |  |       // TODO: both Jacobians and terms are sorted. There must be a simple
 | 
					
						
							|  |  |  |       //       but fast algorithm that does this.
 | 
					
						
							|  |  |  |       typedef std::pair<Key, Matrix> Pair; | 
					
						
							|  |  |  |       BOOST_FOREACH(const Pair& term, terms1) { | 
					
						
							|  |  |  |         std::map<Key, Matrix>::iterator it = jacobians->find(term.first); | 
					
						
							|  |  |  |         if(it != jacobians->end()) { | 
					
						
							|  |  |  |           it->second += H1 * term.second; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           (*jacobians)[term.first] = H1 * term.second; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       BOOST_FOREACH(const Pair& term, terms2) { | 
					
						
							|  |  |  |         std::map<Key, Matrix>::iterator it = jacobians->find(term.first); | 
					
						
							|  |  |  |         if(it != jacobians->end()) { | 
					
						
							|  |  |  |           it->second += H2 * term.second; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           (*jacobians)[term.first] = H2 * term.second; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       val = f_(expression1_->value(values), expression2_->value(values), | 
					
						
							|  |  |  |                boost::none, boost::none); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return val; | 
					
						
							| 
									
										
										
										
											2014-09-22 00:22:28 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   virtual ExpressionNode<T>* clone() const { return new BinaryExpression(*this); } | 
					
						
							| 
									
										
										
										
											2014-09-21 23:37:09 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  | template<typename T> | 
					
						
							|  |  |  | class Expression { | 
					
						
							|  |  |  |  public: | 
					
						
							|  |  |  |   Expression(const ExpressionNode<T>& root) { | 
					
						
							|  |  |  |     root_.reset(root.clone()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Initialize a constant expression
 | 
					
						
							|  |  |  |   Expression(const T& value) : | 
					
						
							|  |  |  |     root_(new ConstantExpression<T>(value)){ } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Initialize a leaf expression
 | 
					
						
							|  |  |  |   Expression(const Key& key) : | 
					
						
							|  |  |  |     root_(new LeafExpression<T>(key)) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Initialize a unary expression
 | 
					
						
							|  |  |  |   template<typename E> | 
					
						
							|  |  |  |   Expression(typename UnaryExpression<T,E>::function f, | 
					
						
							|  |  |  |              const Expression<E>& expression) { | 
					
						
							|  |  |  |     // TODO Assert that root of expression is not null.
 | 
					
						
							|  |  |  |     root_.reset(new UnaryExpression<T,E>(f, *expression.root())); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Initialize a binary expression
 | 
					
						
							|  |  |  |   template<typename E1, typename E2> | 
					
						
							|  |  |  |   Expression(typename BinaryExpression<T,E1,E2>::function f, | 
					
						
							|  |  |  |              const Expression<E1>& expression1, | 
					
						
							|  |  |  |              const Expression<E2>& expression2) { | 
					
						
							|  |  |  |     // TODO Assert that root of expressions 1 and 2 are not null.
 | 
					
						
							|  |  |  |     root_.reset(new BinaryExpression<T,E1,E2>(f, *expression1.root(), | 
					
						
							|  |  |  |                                               *expression2.root())); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 20:08:31 +08:00
										 |  |  |   void getKeys(std::set<Key>& keys) const { root_->getKeys(keys); } | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   T value(const Values& values, | 
					
						
							|  |  |  |           boost::optional<std::map<Key, Matrix>&> jacobians = boost::none) const { | 
					
						
							|  |  |  |     return root_->value(values, jacobians); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const boost::shared_ptr<ExpressionNode<T> >& root() const{ return root_; } | 
					
						
							|  |  |  |  private: | 
					
						
							|  |  |  |   boost::shared_ptr<ExpressionNode<T> > root_; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-09-22 00:22:28 +08:00
										 |  |  | //-----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-22 00:36:19 +08:00
										 |  |  | void printPair(std::pair<Key, Matrix> pair) { | 
					
						
							| 
									
										
										
										
											2014-09-22 00:22:28 +08:00
										 |  |  |   std::cout << pair.first << ": " << pair.second << std::endl; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-09-22 00:54:01 +08:00
										 |  |  | // usage: std::for_each(terms.begin(), terms.end(), printPair);
 | 
					
						
							| 
									
										
										
										
											2014-09-22 00:22:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-21 23:37:09 +08:00
										 |  |  | //-----------------------------------------------------------------------------
 | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  | /// AD Factor
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  | template<class T> | 
					
						
							| 
									
										
										
										
											2014-09-19 06:33:11 +08:00
										 |  |  | class BADFactor: NonlinearFactor { | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-21 22:30:30 +08:00
										 |  |  |   const T measurement_; | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   const Expression<T> expression_; | 
					
						
							| 
									
										
										
										
											2014-09-21 22:30:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-21 23:13:25 +08:00
										 |  |  |   /// get value from expression and calculate error with respect to measurement
 | 
					
						
							|  |  |  |   Vector unwhitenedError(const Values& values) const { | 
					
						
							|  |  |  |     const T& value = expression_.value(values); | 
					
						
							| 
									
										
										
										
											2014-09-22 00:54:01 +08:00
										 |  |  |     return value.localCoordinates(measurement_); | 
					
						
							| 
									
										
										
										
											2014-09-21 23:13:25 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |  public: | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /// Constructor
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   BADFactor(const T& measurement, const Expression<T>& expression) : | 
					
						
							|  |  |  |     measurement_(measurement), expression_(expression) { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   /// Constructor
 | 
					
						
							|  |  |  |   BADFactor(const T& measurement, const ExpressionNode<T>& expression) : | 
					
						
							|  |  |  |     measurement_(measurement), expression_(expression) { | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-09-19 06:33:11 +08:00
										 |  |  |   /**
 | 
					
						
							| 
									
										
										
										
											2014-09-21 23:13:25 +08:00
										 |  |  |    * Calculate the error of the factor. | 
					
						
							|  |  |  |    * This is the log-likelihood, e.g. \f$ 0.5(h(x)-z)^2/\sigma^2 \f$ in case of Gaussian. | 
					
						
							|  |  |  |    * In this class, we take the raw prediction error \f$ h(x)-z \f$, ask the noise model | 
					
						
							|  |  |  |    * to transform it to \f$ (h(x)-z)^2/\sigma^2 \f$, and then multiply by 0.5. | 
					
						
							| 
									
										
										
										
											2014-09-19 06:33:11 +08:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2014-09-21 23:13:25 +08:00
										 |  |  |   virtual double error(const Values& values) const { | 
					
						
							|  |  |  |     if (this->active(values)) { | 
					
						
							|  |  |  |       const Vector e = unwhitenedError(values); | 
					
						
							| 
									
										
										
										
											2014-09-21 23:59:34 +08:00
										 |  |  |       return 0.5 * e.squaredNorm(); | 
					
						
							| 
									
										
										
										
											2014-09-21 23:13:25 +08:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       return 0.0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-09-19 06:33:11 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// get the dimension of the factor (number of rows on linearization)
 | 
					
						
							|  |  |  |   size_t dim() const { | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// linearize to a GaussianFactor
 | 
					
						
							| 
									
										
										
										
											2014-09-21 22:30:30 +08:00
										 |  |  |   boost::shared_ptr<GaussianFactor> linearize(const Values& values) const { | 
					
						
							| 
									
										
										
										
											2014-09-21 19:11:55 +08:00
										 |  |  |     // We will construct an n-ary factor below, where  terms is a container whose
 | 
					
						
							|  |  |  |     // value type is std::pair<Key, Matrix>, specifying the
 | 
					
						
							|  |  |  |     // collection of keys and matrices making up the factor.
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |     std::map<Key, Matrix> terms; | 
					
						
							|  |  |  |     expression_.value(values, terms); | 
					
						
							| 
									
										
										
										
											2014-09-21 23:13:25 +08:00
										 |  |  |     Vector b = unwhitenedError(values); | 
					
						
							| 
									
										
										
										
											2014-09-21 19:11:55 +08:00
										 |  |  |     SharedDiagonal model = SharedDiagonal(); | 
					
						
							| 
									
										
										
										
											2014-09-21 22:30:30 +08:00
										 |  |  |     return boost::shared_ptr<JacobianFactor>( | 
					
						
							|  |  |  |         new JacobianFactor(terms, b, model)); | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-09-21 22:30:30 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-22 00:22:28 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-09-21 23:59:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-22 00:36:19 +08:00
										 |  |  | Point3 transformTo(const Pose3& x, const Point3& p, | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |                    boost::optional<Matrix&> Dpose, boost::optional<Matrix&> Dpoint) { | 
					
						
							| 
									
										
										
										
											2014-09-22 00:36:19 +08:00
										 |  |  |   return x.transform_to(p, Dpose, Dpoint); | 
					
						
							| 
									
										
										
										
											2014-09-21 23:59:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-22 00:36:19 +08:00
										 |  |  | Point2 project(const Point3& p, boost::optional<Matrix&> Dpoint) { | 
					
						
							|  |  |  |   return PinholeCamera<Cal3_S2>::project_to_camera(p, Dpoint); | 
					
						
							| 
									
										
										
										
											2014-09-21 23:59:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<class CAL> | 
					
						
							| 
									
										
										
										
											2014-09-22 00:36:19 +08:00
										 |  |  | Point2 uncalibrate(const CAL& K, const Point2& p, boost::optional<Matrix&> Dcal, | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |                    boost::optional<Matrix&> Dp) { | 
					
						
							| 
									
										
										
										
											2014-09-22 00:36:19 +08:00
										 |  |  |   return K.uncalibrate(p, Dcal, Dp); | 
					
						
							| 
									
										
										
										
											2014-09-21 23:59:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST(BAD, test) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-21 17:33:53 +08:00
										 |  |  |   // Create some values
 | 
					
						
							|  |  |  |   Values values; | 
					
						
							| 
									
										
										
										
											2014-09-21 22:30:30 +08:00
										 |  |  |   values.insert(1, Pose3()); | 
					
						
							|  |  |  |   values.insert(2, Point3(0, 0, 1)); | 
					
						
							|  |  |  |   values.insert(3, Cal3_S2()); | 
					
						
							| 
									
										
										
										
											2014-09-21 17:33:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create old-style factor to create expected value and derivatives
 | 
					
						
							| 
									
										
										
										
											2014-09-21 23:59:34 +08:00
										 |  |  |   Point2 measured(-17, 30); | 
					
						
							| 
									
										
										
										
											2014-09-21 17:33:53 +08:00
										 |  |  |   SharedNoiseModel model = noiseModel::Unit::Create(2); | 
					
						
							|  |  |  |   GeneralSFMFactor2<Cal3_S2> old(measured, model, 1, 2, 3); | 
					
						
							| 
									
										
										
										
											2014-09-21 23:13:25 +08:00
										 |  |  |   double expected_error = old.error(values); | 
					
						
							| 
									
										
										
										
											2014-09-21 17:33:53 +08:00
										 |  |  |   GaussianFactor::shared_ptr expected = old.linearize(values); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  |   // Create leaves
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   Expression<Pose3> x(1); | 
					
						
							|  |  |  |   Expression<Point3> p(2); | 
					
						
							|  |  |  |   Expression<Cal3_S2> K(3); | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create expression tree
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   Expression<Point3> p_cam(transformTo, x, p); | 
					
						
							|  |  |  |   Expression<Point2> projection(project, p_cam); | 
					
						
							|  |  |  |   Expression<Point2> uv_hat(uncalibrate, K, projection); | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-27 20:08:31 +08:00
										 |  |  |   // Check getKeys
 | 
					
						
							|  |  |  |   std::set<Key> keys; | 
					
						
							|  |  |  |   uv_hat.getKeys(keys); | 
					
						
							|  |  |  |   EXPECT_LONGS_EQUAL(3, keys.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  |   // Create factor
 | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  |   BADFactor<Point2> f(measured, uv_hat); | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Check value
 | 
					
						
							| 
									
										
										
										
											2014-09-21 23:13:25 +08:00
										 |  |  |   EXPECT_DOUBLES_EQUAL(expected_error, f.error(values), 1e-9); | 
					
						
							| 
									
										
										
										
											2014-09-19 06:33:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Check dimension
 | 
					
						
							|  |  |  |   EXPECT_LONGS_EQUAL(0, f.dim()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Check linearization
 | 
					
						
							|  |  |  |   boost::shared_ptr<GaussianFactor> gf = f.linearize(values); | 
					
						
							| 
									
										
										
										
											2014-09-21 17:33:53 +08:00
										 |  |  |   EXPECT( assert_equal(*expected, *gf, 1e-9)); | 
					
						
							| 
									
										
										
										
											2014-09-27 17:39:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-19 06:10:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { | 
					
						
							|  |  |  |   TestResult tr; | 
					
						
							|  |  |  |   return TestRegistry::runAllTests(tr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | 
 |