Loading...
Searching...
No Matches
VariableIndex.h
Go to the documentation of this file.
/* ----------------------------------------------------------------------------
* 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
* -------------------------------------------------------------------------- */
#pragma once
#include <gtsam/inference/Factor.h>
#include <gtsam/inference/Key.h>
#include <gtsam/base/FastMap.h>
#include <gtsam/base/FastVector.h>
#include <gtsam/dllexport.h>
#include <boost/optional/optional.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <cassert>
#include <stdexcept>
namespace gtsam {
class GTSAM_EXPORT VariableIndex {
public:
typedef boost::shared_ptr<VariableIndex> shared_ptr;
typedef FactorIndices::iterator Factor_iterator;
typedef FactorIndices::const_iterator Factor_const_iterator;
protected:
typedef FastMap<Key, FactorIndices> KeyMap;
KeyMap index_;
size_t nFactors_; // Number of factors in the original factor graph.
size_t nEntries_; // Sum of involved variable counts of each factor.
public:
typedef KeyMap::const_iterator const_iterator;
typedef KeyMap::const_iterator iterator;
typedef KeyMap::value_type value_type;
VariableIndex() : nFactors_(0), nEntries_(0) {}
template <class FG>
explicit VariableIndex(const FG& factorGraph) : nFactors_(0), nEntries_(0) {
augment(factorGraph);
}
size_t size() const { return index_.size(); }
size_t nFactors() const { return nFactors_; }
size_t nEntries() const { return nEntries_; }
const FactorIndices& operator[](Key variable) const {
KeyMap::const_iterator item = index_.find(variable);
if(item == index_.end())
throw std::invalid_argument("Requested non-existent variable from VariableIndex");
else
return item->second;
}
bool empty(Key variable) const {
return (*this)[variable].empty();
}
bool equals(const VariableIndex& other, double tol=0.0) const;
void print(const std::string& str = "VariableIndex: ",
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
void outputMetisFormat(std::ostream& os) const;
template<class FG>
void augment(const FG& factors, boost::optional<const FactorIndices&> newFactorIndices = boost::none);
void augmentExistingFactor(const FactorIndex factorIndex, const KeySet & newKeys);
template<typename ITERATOR, class FG>
void remove(ITERATOR firstFactor, ITERATOR lastFactor, const FG& factors);
template<typename ITERATOR>
void removeUnusedVariables(ITERATOR firstKey, ITERATOR lastKey);
const_iterator begin() const { return index_.begin(); }
const_iterator end() const { return index_.end(); }
const_iterator find(Key key) const { return index_.find(key); }
protected:
Factor_iterator factorsBegin(Key variable) { return internalAt(variable).begin(); }
Factor_iterator factorsEnd(Key variable) { return internalAt(variable).end(); }
Factor_const_iterator factorsBegin(Key variable) const { return internalAt(variable).begin(); }
Factor_const_iterator factorsEnd(Key variable) const { return internalAt(variable).end(); }
const FactorIndices& internalAt(Key variable) const {
const KeyMap::const_iterator item = index_.find(variable);
assert(item != index_.end());
return item->second;
}
FactorIndices& internalAt(Key variable) {
const KeyMap::iterator item = index_.find(variable);
assert(item != index_.end());
return item->second;
}
private:
friend class boost::serialization::access;
template<class ARCHIVE>
void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
ar & BOOST_SERIALIZATION_NVP(index_);
ar & BOOST_SERIALIZATION_NVP(nFactors_);
ar & BOOST_SERIALIZATION_NVP(nEntries_);
}
};
template<>
struct traits<VariableIndex> : public Testable<VariableIndex> {
};
} //\ namespace gtsam
#include <gtsam/inference/VariableIndex-inl.h>
FastMap.h
A thin wrapper around std::map that uses boost's fast_pool_allocator.
FastVector.h
A thin wrapper around std::vector that uses a custom allocator.
Key.h
VariableIndex-inl.h
Factor.h
The base class for all factors.
gtsam
Global functions in a separate testing namespace.
Definition chartTesting.h:28
gtsam::FactorIndex
std::uint64_t FactorIndex
Integer nonlinear factor index type.
Definition types.h:103
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:156
gtsam::KeyFormatter
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition Key.h:35
gtsam::FactorIndices
FastVector< FactorIndex > FactorIndices
Define collection types:
Definition Factor.h:34
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:100
gtsam::traits
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition concepts.h:30
gtsam::FastMap
FastMap is a thin wrapper around std::map that uses the boost fast_pool_allocator instead of the defa...
Definition FastMap.h:38
gtsam::equals
Template to create a binary predicate.
Definition Testable.h:111
gtsam::Testable
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:151
gtsam::VariableIndex
The VariableIndex class computes and stores the block column structure of a factor graph.
Definition VariableIndex.h:43
gtsam::VariableIndex::internalAt
const FactorIndices & internalAt(Key variable) const
Internal version of 'at' that asserts existence.
Definition VariableIndex.h:172
gtsam::VariableIndex::nEntries
size_t nEntries() const
The number of nonzero blocks, i.e. the number of variable-factor entries.
Definition VariableIndex.h:86
gtsam::VariableIndex::begin
const_iterator begin() const
Iterator to the first variable entry.
Definition VariableIndex.h:156
gtsam::VariableIndex::find
const_iterator find(Key key) const
Find the iterator for the requested variable entry.
Definition VariableIndex.h:162
gtsam::VariableIndex::operator[]
const FactorIndices & operator[](Key variable) const
Access a list of factors by variable.
Definition VariableIndex.h:89
gtsam::VariableIndex::VariableIndex
VariableIndex(const FG &factorGraph)
Create a VariableIndex that computes and stores the block column structure of a factor graph.
Definition VariableIndex.h:71
gtsam::VariableIndex::empty
bool empty(Key variable) const
Return true if no factors associated with a variable.
Definition VariableIndex.h:98
gtsam::VariableIndex::size
size_t size() const
The number of variable entries. This is equal to the number of unique variable Keys.
Definition VariableIndex.h:80
gtsam::VariableIndex::internalAt
FactorIndices & internalAt(Key variable)
Internal version of 'at' that asserts existence.
Definition VariableIndex.h:179
gtsam::VariableIndex::end
const_iterator end() const
Iterator to the first variable entry.
Definition VariableIndex.h:159
gtsam::VariableIndex::augment
void augment(const FG &factors, boost::optional< const FactorIndices & > newFactorIndices=boost::none)
Augment the variable index with new factors.
Definition VariableIndex-inl.h:27
gtsam::VariableIndex::VariableIndex
VariableIndex()
Default constructor, creates an empty VariableIndex.
Definition VariableIndex.h:64
gtsam::VariableIndex::nFactors
size_t nFactors() const
The number of factors in the original factor graph.
Definition VariableIndex.h:83