/* ---------------------------------------------------------------------------- * 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 * -------------------------------------------------------------------------- */ /** * @file VariableIndex.h * @brief * @author Richard Roberts * @created Sep 12, 2010 */ #pragma once #include #include #include #include #include #include #include namespace gtsam { class Inference; /** * The VariableIndex class stores the information necessary to perform * elimination, including an index of factors involving each variable and * the structure of the intermediate joint factors that develop during * elimination. This maps variables to collections of factor indices. */ class VariableIndex { public: typedef boost::shared_ptr shared_ptr; typedef FastList Factors; typedef typename Factors::iterator Factor_iterator; typedef typename Factors::const_iterator Factor_const_iterator; protected: std::vector indexUnpermuted_; Permuted, Factors&> index_; size_t nFactors_; size_t nEntries_; // Sum of involved variable counts of each factor public: VariableIndex() : index_(indexUnpermuted_), nFactors_(0), nEntries_(0) {} template VariableIndex(const FactorGraph& factorGraph, Index nVariables); template VariableIndex(const FactorGraph& factorGraph); Index size() const { return index_.size(); } size_t nFactors() const { return nFactors_; } size_t nEntries() const { return nEntries_; } const Factors& operator[](Index variable) const { checkVar(variable); return index_[variable]; } Factors& operator[](Index variable) { checkVar(variable); return index_[variable]; } void permute(const Permutation& permutation); template void augment(const FactorGraph& factorGraph); void rebaseFactors(ptrdiff_t baseIndexChange); bool equals(const VariableIndex& other, double tol=0.0) const; void print(const std::string& str = "VariableIndex: ") const; protected: VariableIndex(size_t nVars) : indexUnpermuted_(nVars), index_(indexUnpermuted_), nFactors_(0), nEntries_(0) {} void checkVar(Index variable) const { assert(variable < index_.size()); } template void fill(const FactorGraph& factorGraph); Factor_iterator factorsBegin(Index variable) { checkVar(variable); return index_[variable].begin(); } Factor_const_iterator factorsBegin(Index variable) const { checkVar(variable); return index_[variable].begin(); } Factor_iterator factorsEnd(Index variable) { checkVar(variable); return index_[variable].end(); } Factor_const_iterator factorsEnd(Index variable) const { checkVar(variable); return index_[variable].end(); } }; /* ************************************************************************* */ template void VariableIndex::fill(const FactorGraph& factorGraph) { // Build index mapping from variable id to factor index for(size_t fi=0; fikeys()) { if(key < index_.size()) { index_[key].push_back(fi); ++ nEntries_; } } ++ nFactors_; } } /* ************************************************************************* */ template VariableIndex::VariableIndex(const FactorGraph& factorGraph) : index_(indexUnpermuted_), nFactors_(0), nEntries_(0) { // If the factor graph is empty, return an empty index because inside this // if block we assume at least one factor. if(factorGraph.size() > 0) { // Find highest-numbered variable Index maxVar = 0; BOOST_FOREACH(const typename FactorGraph::sharedFactor& factor, factorGraph) { if(factor) { BOOST_FOREACH(const Index key, factor->keys()) { if(key > maxVar) maxVar = key; } } } // Allocate array index_.container().resize(maxVar+1); index_.permutation() = Permutation::Identity(maxVar+1); fill(factorGraph); } } /* ************************************************************************* */ template VariableIndex::VariableIndex(const FactorGraph& factorGraph, Index nVariables) : indexUnpermuted_(nVariables), index_(indexUnpermuted_), nFactors_(0), nEntries_(0) { fill(factorGraph); } /* ************************************************************************* */ template void VariableIndex::augment(const FactorGraph& factorGraph) { // If the factor graph is empty, return an empty index because inside this // if block we assume at least one factor. if(factorGraph.size() > 0) { // Find highest-numbered variable Index maxVar = 0; BOOST_FOREACH(const typename FactorGraph::sharedFactor& factor, factorGraph) { if(factor) { BOOST_FOREACH(const Index key, factor->keys()) { if(key > maxVar) maxVar = key; } } } // Allocate index Index originalSize = index_.size(); index_.container().resize(std::max(index_.size(), maxVar+1)); index_.permutation().resize(index_.container().size()); for(Index var=originalSize; varkeys()) { index_[key].push_back(orignFactors + fi); ++ nEntries_; } ++ nFactors_; } } } }