| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |  * @file    VectorConfig.cpp | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  * @brief   Factor Graph Configuration | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |  * @brief   VectorConfig | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  * @author  Carlos Nieto | 
					
						
							|  |  |  |  * @author  Christian Potthast | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							|  |  |  | #include <boost/tuple/tuple.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | #include "VectorConfig.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // trick from some reading group
 | 
					
						
							|  |  |  | #define FOREACH_PAIR( KEY, VAL, COL) BOOST_FOREACH (boost::tie(KEY,VAL),COL) 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | using namespace std; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void check_size(const string& key, const Vector & vj, const Vector & dj) { | 
					
						
							|  |  |  |   if (dj.size()!=vj.size()) { | 
					
						
							|  |  |  |     cout << "For key \"" << key << "\"" << endl; | 
					
						
							|  |  |  |     cout << "vj.size = " << vj.size() << endl; | 
					
						
							|  |  |  |     cout << "dj.size = " << dj.size() << endl; | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |     throw(std::invalid_argument("VectorConfig::+ mismatched dimensions")); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-20 13:10:55 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | VectorConfig VectorConfig::scale(double gain) { | 
					
						
							|  |  |  | 	VectorConfig scaled; | 
					
						
							|  |  |  | 	string key; Vector val; | 
					
						
							|  |  |  | 	FOREACH_PAIR(key, val, values) { | 
					
						
							|  |  |  | 		scaled.insert(key, gain*val); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return scaled; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | VectorConfig VectorConfig::exmap(const VectorConfig & delta) const | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | 	VectorConfig newConfig; | 
					
						
							| 
									
										
										
										
											2009-11-29 05:44:07 +08:00
										 |  |  | 	for (const_iterator it = values.begin(); it!=values.end(); it++) { | 
					
						
							|  |  |  | 		string j = it->first; | 
					
						
							|  |  |  | 		const Vector &vj = it->second; | 
					
						
							|  |  |  | 		if (delta.contains(j)) { | 
					
						
							|  |  |  | 			const Vector& dj = delta[j]; | 
					
						
							|  |  |  | 			check_size(j,vj,dj); | 
					
						
							|  |  |  | 			newConfig.insert(j, vj + dj); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			newConfig.insert(j, vj); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return newConfig; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | Vector VectorConfig::get(const std::string& name) const { | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   const_iterator it = values.find(name); | 
					
						
							|  |  |  |   if (it==values.end()) { | 
					
						
							|  |  |  |     print(); | 
					
						
							|  |  |  |     cout << "asked for key " << name << endl; | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |     throw(std::invalid_argument("VectorConfig::[] invalid key")); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   return it->second; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | void VectorConfig::print(const std::string& name) const { | 
					
						
							|  |  |  |   odprintf("VectorConfig %s\n", name.c_str()); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   odprintf("size: %d\n", values.size()); | 
					
						
							|  |  |  |   string j; Vector v; | 
					
						
							|  |  |  |   FOREACH_PAIR(j, v, values) { | 
					
						
							|  |  |  |     odprintf("%s:", j.c_str()); | 
					
						
							|  |  |  |     gtsam::print(v); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | bool VectorConfig::equals(const VectorConfig& expected, double tol) const { | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   string j; Vector vActual; | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   if( values.size() != expected.size() ) return false; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // iterate over all nodes
 | 
					
						
							|  |  |  |   FOREACH_PAIR(j, vActual, values) { | 
					
						
							|  |  |  |     Vector vExpected = expected[j]; | 
					
						
							|  |  |  |     if(!equal_with_abs_tol(vExpected,vActual,tol))  | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |     	return false; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | 
 |