| 
									
										
										
										
											2010-10-14 12:54:38 +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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    Permutation.cpp | 
					
						
							|  |  |  |  * @brief    | 
					
						
							|  |  |  |  * @author  Richard Roberts | 
					
						
							|  |  |  |  * @created Oct 9, 2010 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam/inference/Permutation.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | Permutation Permutation::Identity(Index nVars) { | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  |   Permutation ret(nVars); | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   for(Index i=0; i<nVars; ++i) | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  |     ret[i] = i; | 
					
						
							|  |  |  |   return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | Permutation Permutation::PullToFront(const vector<Index>& toFront, size_t size) { | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Permutation ret(size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Mask of which variables have been pulled, used to reorder
 | 
					
						
							|  |  |  |   vector<bool> pulled(size, false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  |   // Put the pulled variables at the front of the permutation and set up the
 | 
					
						
							|  |  |  |   // pulled flags.
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   for(Index j=0; j<toFront.size(); ++j) { | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  |     ret[j] = toFront[j]; | 
					
						
							|  |  |  |     pulled[toFront[j]] = true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  |   // Fill in the rest of the variables
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   Index nextVar = toFront.size(); | 
					
						
							|  |  |  |   for(Index j=0; j<size; ++j) | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  |     if(!pulled[j]) | 
					
						
							|  |  |  |       ret[nextVar++] = j; | 
					
						
							|  |  |  |   assert(nextVar == size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | Permutation Permutation::PushToBack(const std::vector<Index>& toBack, size_t size) { | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Permutation ret(size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Mask of which variables have been pushed, used to reorder
 | 
					
						
							|  |  |  |   vector<bool> pushed(size, false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Put the pushed variables at the back of the permutation and set up the
 | 
					
						
							|  |  |  |   // pushed flags;
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   Index nextVar = size - toBack.size(); | 
					
						
							|  |  |  |   for(Index j=0; j<toBack.size(); ++j) { | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  |     ret[nextVar++] = toBack[j]; | 
					
						
							|  |  |  |     pushed[toBack[j]] = true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   assert(nextVar == size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Fill in the rest of the variables
 | 
					
						
							|  |  |  |   nextVar = 0; | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   for(Index j=0; j<size; ++j) | 
					
						
							| 
									
										
										
										
											2010-10-12 00:02:01 +08:00
										 |  |  |     if(!pushed[j]) | 
					
						
							|  |  |  |       ret[nextVar++] = j; | 
					
						
							|  |  |  |   assert(nextVar == size - toBack.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | Permutation::shared_ptr Permutation::permute(const Permutation& permutation) const { | 
					
						
							|  |  |  |   const size_t nVars = permutation.size(); | 
					
						
							|  |  |  |   Permutation::shared_ptr result(new Permutation(nVars)); | 
					
						
							|  |  |  |   for(size_t j=0; j<nVars; ++j) { | 
					
						
							|  |  |  |     assert(permutation[j] < rangeIndices_.size()); | 
					
						
							|  |  |  |     (*result)[j] = operator[](permutation[j]); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | Permutation::shared_ptr Permutation::partialPermutation( | 
					
						
							|  |  |  |     const Permutation& selector, const Permutation& partialPermutation) const { | 
					
						
							|  |  |  |   assert(selector.size() == partialPermutation.size()); | 
					
						
							|  |  |  |   Permutation::shared_ptr result(new Permutation(*this)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   for(Index subsetPos=0; subsetPos<selector.size(); ++subsetPos) | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  |     (*result)[selector[subsetPos]] = (*this)[selector[partialPermutation[subsetPos]]]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | Permutation::shared_ptr Permutation::inverse() const { | 
					
						
							|  |  |  |   Permutation::shared_ptr result(new Permutation(this->size())); | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   for(Index i=0; i<this->size(); ++i) { | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  |     assert((*this)[i] < this->size()); | 
					
						
							|  |  |  |     (*result)[(*this)[i]] = i; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void Permutation::print(const std::string& str) const { | 
					
						
							|  |  |  |   std::cout << str; | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   BOOST_FOREACH(Index s, rangeIndices_) { std::cout << s << " "; } | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  |   std::cout << std::endl; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |