| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * ClusterTree-inl.h | 
					
						
							|  |  |  |  * Created on: July 13, 2010 | 
					
						
							|  |  |  |  * @author Kai Ni | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  * @brief: Collects factorgraph fragments defined on variable clusters, arranged in a tree | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/inference/ClusterTree.h>
 | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	using namespace std; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-15 07:48:51 +08:00
										 |  |  | 	/* ************************************************************************* *
 | 
					
						
							|  |  |  | 	 * Cluster | 
					
						
							|  |  |  | 	 * ************************************************************************* */ | 
					
						
							|  |  |  | 	template<class FG> | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	template<class Iterator> | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 	ClusterTree<FG>::Cluster::Cluster(const FG& fg, Index key, Iterator firstSeparator, Iterator lastSeparator) : | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	FG(fg), frontal(1, key), separator(firstSeparator, lastSeparator) {} | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   template<class FG> | 
					
						
							|  |  |  |   template<typename FrontalIt, typename SeparatorIt> | 
					
						
							|  |  |  |   ClusterTree<FG>::Cluster::Cluster( | 
					
						
							|  |  |  |       const FG& fg, FrontalIt firstFrontal, FrontalIt lastFrontal, SeparatorIt firstSeparator, SeparatorIt lastSeparator) : | 
					
						
							|  |  |  |       FG(fg), frontal(firstFrontal, lastFrontal), separator(firstSeparator, lastSeparator) {} | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   template<class FG> | 
					
						
							|  |  |  |   template<typename FrontalIt, typename SeparatorIt> | 
					
						
							|  |  |  |   ClusterTree<FG>::Cluster::Cluster( | 
					
						
							|  |  |  |       FrontalIt firstFrontal, FrontalIt lastFrontal, SeparatorIt firstSeparator, SeparatorIt lastSeparator) : | 
					
						
							|  |  |  |       frontal(firstFrontal, lastFrontal), separator(firstSeparator, lastSeparator) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  | 	template<class FG> | 
					
						
							|  |  |  | 	void ClusterTree<FG>::Cluster::addChild(typename ClusterTree<FG>::Cluster::shared_ptr child) { | 
					
						
							|  |  |  | 	  children_.push_back(child); | 
					
						
							| 
									
										
										
										
											2010-07-15 07:48:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* ************************************************************************* */ | 
					
						
							|  |  |  | 	template<class FG> | 
					
						
							|  |  |  | 	bool ClusterTree<FG>::Cluster::equals(const ClusterTree<FG>::Cluster& other) const { | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 		if (frontal != other.frontal) return false; | 
					
						
							|  |  |  | 		if (separator != other.separator) return false; | 
					
						
							| 
									
										
										
										
											2010-07-15 07:48:51 +08:00
										 |  |  | 		if (children_.size() != other.children_.size()) return false; | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 		typename list<shared_ptr>::const_iterator it1 = children_.begin(); | 
					
						
							|  |  |  | 		typename list<shared_ptr>::const_iterator it2 = other.children_.begin(); | 
					
						
							| 
									
										
										
										
											2010-07-15 07:48:51 +08:00
										 |  |  | 		for (; it1 != children_.end(); it1++, it2++) | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 			if (!(*it1)->equals(**it2)) return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-07-15 07:48:51 +08:00
										 |  |  | 	template<class FG> | 
					
						
							| 
									
										
										
										
											2010-07-14 12:32:58 +08:00
										 |  |  | 	void ClusterTree<FG>::Cluster::print(const string& indent) const { | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 		cout << indent; | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 		BOOST_FOREACH(const Index key, frontal) | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 						cout << key << " "; | 
					
						
							|  |  |  | 		cout << ": "; | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 		BOOST_FOREACH(const Index key, separator) | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 						cout << key << " "; | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 		cout << endl; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-07-15 07:48:51 +08:00
										 |  |  | 	template<class FG> | 
					
						
							| 
									
										
										
										
											2010-07-14 12:32:58 +08:00
										 |  |  | 	void ClusterTree<FG>::Cluster::printTree(const string& indent) const { | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 		print(indent); | 
					
						
							|  |  |  | 		BOOST_FOREACH(const shared_ptr& child, children_) | 
					
						
							| 
									
										
										
										
											2010-07-15 07:48:51 +08:00
										 |  |  | 						child->printTree(indent + "  "); | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-15 07:48:51 +08:00
										 |  |  | 	/* ************************************************************************* *
 | 
					
						
							|  |  |  | 	 * ClusterTree | 
					
						
							|  |  |  | 	 * ************************************************************************* */ | 
					
						
							|  |  |  | 	template<class FG> | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 	bool ClusterTree<FG>::equals(const ClusterTree<FG>& other, double tol) const { | 
					
						
							| 
									
										
										
										
											2010-07-15 07:48:51 +08:00
										 |  |  | 		if (!root_ && !other.root_) return true; | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 		if (!root_ || !other.root_) return false; | 
					
						
							|  |  |  | 		return root_->equals(*other.root_); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } //namespace gtsam
 |