| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * ClusterTree.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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <gtsam/base/types.h>
 | 
					
						
							|  |  |  | #include <gtsam/base/Testable.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | #include <boost/shared_ptr.hpp>
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <boost/weak_ptr.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <list>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-14 12:32:58 +08:00
										 |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * A cluster-tree is associated with a factor graph and is defined as in Koller-Friedman: | 
					
						
							|  |  |  | 	 * each node k represents a subset C_k \sub X, and the tree is family preserving, in that | 
					
						
							|  |  |  | 	 * each factor f_i is associated with a single cluster and scope(f_i) \sub C_k. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 	template <class FG> | 
					
						
							|  |  |  | 	class ClusterTree : public Testable<ClusterTree<FG> > { | 
					
						
							| 
									
										
										
										
											2010-07-14 12:32:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-15 07:48:51 +08:00
										 |  |  | 	protected: | 
					
						
							| 
									
										
										
										
											2010-07-14 12:32:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 		// the class for subgraphs that also include the pointers to the parents and two children
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 		class Cluster : public FG { | 
					
						
							|  |  |  | 		public: | 
					
						
							| 
									
										
										
										
											2010-07-14 12:32:58 +08:00
										 |  |  | 			typedef typename boost::shared_ptr<Cluster> shared_ptr; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 			typedef typename boost::weak_ptr<Cluster> weak_ptr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |       const std::vector<Index> frontal;                   // the frontal variables
 | 
					
						
							|  |  |  |       const std::vector<Index> separator;                // the separator variables
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		protected: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			weak_ptr parent_;                      // the parent cluster
 | 
					
						
							|  |  |  | 			std::list<shared_ptr> children_;     // the child clusters
 | 
					
						
							|  |  |  | 			const typename FG::sharedFactor eliminated_; // the eliminated factor to pass on to the parent
 | 
					
						
							| 
									
										
										
										
											2010-07-14 12:32:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 		public: | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-15 07:48:51 +08:00
										 |  |  | 			// Construct empty clique
 | 
					
						
							| 
									
										
										
										
											2010-07-14 12:32:58 +08:00
										 |  |  | 			Cluster() {} | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-15 07:48:51 +08:00
										 |  |  | 			/* Create a node with a single frontal variable */ | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 			template<typename Iterator> | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 			Cluster(const FG& fg, Index key, Iterator firstSeparator, Iterator lastSeparator); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       /* Create a node with several frontal variables */ | 
					
						
							|  |  |  |       template<typename FrontalIt, typename SeparatorIt> | 
					
						
							|  |  |  |       Cluster(const FG& fg, FrontalIt firstFrontal, FrontalIt lastFrontal, SeparatorIt firstSeparator, SeparatorIt lastSeparator); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       /* Create a node with several frontal variables */ | 
					
						
							|  |  |  |       template<typename FrontalIt, typename SeparatorIt> | 
					
						
							|  |  |  |       Cluster(FrontalIt firstFrontal, FrontalIt lastFrontal, SeparatorIt firstSeparator, SeparatorIt lastSeparator); | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// print the object
 | 
					
						
							|  |  |  | 			void print(const std::string& indent) const; | 
					
						
							|  |  |  | 			void printTree(const std::string& indent) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// check equality
 | 
					
						
							| 
									
										
										
										
											2010-07-14 12:32:58 +08:00
										 |  |  | 			bool equals(const Cluster& other) const; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// get or set the parent
 | 
					
						
							|  |  |  | 			weak_ptr& parent() { return parent_; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// get a reference to the children
 | 
					
						
							|  |  |  | 			const std::list<shared_ptr>& children() const { return children_; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// add a child
 | 
					
						
							|  |  |  | 			void addChild(shared_ptr child); | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-14 12:32:58 +08:00
										 |  |  | 		// typedef for shared pointers to clusters
 | 
					
						
							|  |  |  | 		typedef typename Cluster::shared_ptr sharedCluster; | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-14 12:32:58 +08:00
										 |  |  | 		// Root cluster
 | 
					
						
							|  |  |  | 		sharedCluster root_; | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	public: | 
					
						
							| 
									
										
										
										
											2010-07-15 07:48:51 +08:00
										 |  |  | 		// constructor of empty tree
 | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 		ClusterTree() {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-14 12:32:58 +08:00
										 |  |  | 		// return the root cluster
 | 
					
						
							|  |  |  | 		sharedCluster root() const { return root_; } | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// print the object
 | 
					
						
							|  |  |  | 		void print(const std::string& str) const { | 
					
						
							| 
									
										
										
										
											2010-07-14 12:32:58 +08:00
										 |  |  | 			std::cout << str << std::endl; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 			if (root_) root_->printTree(""); | 
					
						
							| 
									
										
										
										
											2010-07-14 06:03:18 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** check equality */ | 
					
						
							|  |  |  | 		bool equals(const ClusterTree<FG>& other, double tol = 1e-9) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	}; // ClusterTree
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // namespace gtsam
 |