| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    NonlinearFactor.cpp | 
					
						
							|  |  |  |  * @brief   nonlinear factor versions which assume a Gaussian noise on a measurement  | 
					
						
							|  |  |  |  * @brief   predicted by a non-linear function h nonlinearFactor | 
					
						
							|  |  |  |  * @author  Kai Ni | 
					
						
							|  |  |  |  * @author  Carlos Nieto | 
					
						
							|  |  |  |  * @author  Christian Potthast | 
					
						
							|  |  |  |  * @author  Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | #include "NonlinearFactor.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | NonlinearFactor1::NonlinearFactor1(const Vector& z,		                     | 
					
						
							|  |  |  |                                    const double sigma,	                        | 
					
						
							|  |  |  |                                    Vector (*h)(const Vector&),                       | 
					
						
							|  |  |  |                                    const string& key1,                    | 
					
						
							|  |  |  |                                    Matrix (*H)(const Vector&))  | 
					
						
							| 
									
										
										
										
											2009-10-25 04:01:47 +08:00
										 |  |  |   : NonlinearFactor<VectorConfig>(z, sigma), h_(h), key_(key1), H_(H) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   keys_.push_front(key1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void NonlinearFactor1::print(const string& s) const { | 
					
						
							| 
									
										
										
										
											2009-11-13 00:47:12 +08:00
										 |  |  | 	cout << "NonlinearFactor1 " << s << endl; | 
					
						
							| 
									
										
										
										
											2009-10-25 04:01:47 +08:00
										 |  |  |   cout << "h  : " << (void*)h_ << endl; | 
					
						
							|  |  |  |   cout << "key: " << key_      << endl; | 
					
						
							|  |  |  |   cout << "H  : " << (void*)H_ << endl; | 
					
						
							|  |  |  | 	NonlinearFactor<VectorConfig>::print("parent"); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | GaussianFactor::shared_ptr NonlinearFactor1::linearize(const VectorConfig& c) const { | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	// get argument 1 from config
 | 
					
						
							| 
									
										
										
										
											2009-10-25 04:01:47 +08:00
										 |  |  | 	Vector x1 = c[key_]; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	// Jacobian A = H(x1)/sigma
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 	Matrix A = H_(x1); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	// Right-hand-side b = error(c) = (z - h(x1))/sigma
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 	Vector b = (z_ - h_(x1)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactor::shared_ptr p(new GaussianFactor(key_, A, b, sigma_)); | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	return p; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | /** http://artis.imag.fr/~Xavier.Decoret/resources/C++/operator==.html       */ | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-10-23 01:23:24 +08:00
										 |  |  | bool NonlinearFactor1::equals(const Factor<VectorConfig>& f, double tol) const { | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	const NonlinearFactor1* p = dynamic_cast<const NonlinearFactor1*> (&f); | 
					
						
							|  |  |  | 	if (p == NULL) return false; | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | 	return NonlinearFactor<VectorConfig>::equals(*p, tol) | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	&& (h_   == p->h_)  | 
					
						
							| 
									
										
										
										
											2009-10-25 04:01:47 +08:00
										 |  |  | 	&& (key_ == p->key_) | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	&& (H_   == p->H_); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | NonlinearFactor2::NonlinearFactor2(const Vector& z, | 
					
						
							|  |  |  | 		const double sigma, | 
					
						
							|  |  |  | 		Vector (*h)(const Vector&, const Vector&), | 
					
						
							|  |  |  | 		const string& key1, | 
					
						
							|  |  |  | 		Matrix (*H1)(const Vector&, const Vector&), | 
					
						
							|  |  |  | 		const string& key2, | 
					
						
							|  |  |  | 		Matrix (*H2)(const Vector&, const Vector&) | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | : NonlinearFactor<VectorConfig>(z, sigma), h_(h), key1_(key1), H1_(H1), key2_(key2), H2_(H2) | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	keys_.push_front(key1); | 
					
						
							|  |  |  | 	keys_.push_front(key2); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void NonlinearFactor2::print(const string& s) const { | 
					
						
							| 
									
										
										
										
											2009-11-13 00:47:12 +08:00
										 |  |  | 	cout << "NonlinearFactor2 " << s << endl; | 
					
						
							| 
									
										
										
										
											2009-10-25 04:01:47 +08:00
										 |  |  |   cout << "h   : " << (void*)h_  << endl; | 
					
						
							|  |  |  |   cout << "key1: " << key1_      << endl; | 
					
						
							|  |  |  |   cout << "H2  : " << (void*)H2_ << endl; | 
					
						
							|  |  |  |   cout << "key2: " << key2_      << endl; | 
					
						
							|  |  |  |   cout << "H1  : " << (void*)H1_ << endl; | 
					
						
							|  |  |  | 	NonlinearFactor<VectorConfig>::print("parent"); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | GaussianFactor::shared_ptr NonlinearFactor2::linearize(const VectorConfig& c) const { | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	// get arguments from config
 | 
					
						
							|  |  |  | 	Vector x1 = c[key1_]; | 
					
						
							|  |  |  | 	Vector x2 = c[key2_]; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	// Jacobian A = H(x)/sigma
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 	Matrix A1 = H1_(x1, x2); | 
					
						
							|  |  |  | 	Matrix A2 = H2_(x1, x2); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	// Right-hand-side b = (z - h(x))/sigma
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 	Vector b = (z_ - h_(x1, x2)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | 	GaussianFactor::shared_ptr p(new GaussianFactor(key1_, A1, key2_, A2, b, sigma_)); | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	return p; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-10-23 01:23:24 +08:00
										 |  |  | bool NonlinearFactor2::equals(const Factor<VectorConfig>& f, double tol) const { | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	const NonlinearFactor2* p = dynamic_cast<const NonlinearFactor2*> (&f); | 
					
						
							|  |  |  | 	if (p == NULL) return false; | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | 	return NonlinearFactor<VectorConfig>::equals(*p, tol) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     && (h_    == p->h_) | 
					
						
							|  |  |  |     && (key1_ == p->key1_) | 
					
						
							| 
									
										
										
										
											2009-10-25 04:01:47 +08:00
										 |  |  |     && (H1_   == p->H1_) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     && (key2_ == p->key2_) | 
					
						
							| 
									
										
										
										
											2009-10-25 04:01:47 +08:00
										 |  |  |     && (H2_   == p->H2_); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ |