gtsam 4.1.1
gtsam
VariableIndex.h
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2
3 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4 * Atlanta, Georgia 30332-0415
5 * All Rights Reserved
6 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7
8 * See LICENSE for the license information
9
10 * -------------------------------------------------------------------------- */
11
18#pragma once
19
21#include <gtsam/inference/Key.h>
22#include <gtsam/base/FastMap.h>
24#include <gtsam/dllexport.h>
25
26#include <boost/optional/optional.hpp>
27#include <boost/smart_ptr/shared_ptr.hpp>
28
29#include <cassert>
30#include <stdexcept>
31
32namespace gtsam {
33
43class GTSAM_EXPORT VariableIndex {
44 public:
45 typedef boost::shared_ptr<VariableIndex> shared_ptr;
46 typedef FactorIndices::iterator Factor_iterator;
47 typedef FactorIndices::const_iterator Factor_const_iterator;
48
49 protected:
51 KeyMap index_;
52 size_t nFactors_; // Number of factors in the original factor graph.
53 size_t nEntries_; // Sum of involved variable counts of each factor.
54
55 public:
56 typedef KeyMap::const_iterator const_iterator;
57 typedef KeyMap::const_iterator iterator;
58 typedef KeyMap::value_type value_type;
59
62
64 VariableIndex() : nFactors_(0), nEntries_(0) {}
65
70 template <class FG>
71 explicit VariableIndex(const FG& factorGraph) : nFactors_(0), nEntries_(0) {
72 augment(factorGraph);
73 }
74
78
80 size_t size() const { return index_.size(); }
81
83 size_t nFactors() const { return nFactors_; }
84
86 size_t nEntries() const { return nEntries_; }
87
89 const FactorIndices& operator[](Key variable) const {
90 KeyMap::const_iterator item = index_.find(variable);
91 if(item == index_.end())
92 throw std::invalid_argument("Requested non-existent variable from VariableIndex");
93 else
94 return item->second;
95 }
96
98 bool empty(Key variable) const {
99 return (*this)[variable].empty();
100 }
101
105
107 bool equals(const VariableIndex& other, double tol=0.0) const;
108
110 void print(const std::string& str = "VariableIndex: ",
111 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
112
117 void outputMetisFormat(std::ostream& os) const;
118
119
123
128 template<class FG>
129 void augment(const FG& factors, boost::optional<const FactorIndices&> newFactorIndices = boost::none);
130
136 void augmentExistingFactor(const FactorIndex factorIndex, const KeySet & newKeys);
137
148 template<typename ITERATOR, class FG>
149 void remove(ITERATOR firstFactor, ITERATOR lastFactor, const FG& factors);
150
152 template<typename ITERATOR>
153 void removeUnusedVariables(ITERATOR firstKey, ITERATOR lastKey);
154
156 const_iterator begin() const { return index_.begin(); }
157
159 const_iterator end() const { return index_.end(); }
160
162 const_iterator find(Key key) const { return index_.find(key); }
163
164protected:
165 Factor_iterator factorsBegin(Key variable) { return internalAt(variable).begin(); }
166 Factor_iterator factorsEnd(Key variable) { return internalAt(variable).end(); }
167
168 Factor_const_iterator factorsBegin(Key variable) const { return internalAt(variable).begin(); }
169 Factor_const_iterator factorsEnd(Key variable) const { return internalAt(variable).end(); }
170
172 const FactorIndices& internalAt(Key variable) const {
173 const KeyMap::const_iterator item = index_.find(variable);
174 assert(item != index_.end());
175 return item->second;
176 }
177
180 const KeyMap::iterator item = index_.find(variable);
181 assert(item != index_.end());
182 return item->second;
183 }
184
185private:
187 friend class boost::serialization::access;
188 template<class ARCHIVE>
189 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
190 ar & BOOST_SERIALIZATION_NVP(index_);
191 ar & BOOST_SERIALIZATION_NVP(nFactors_);
192 ar & BOOST_SERIALIZATION_NVP(nEntries_);
193 }
194
196};
197
199template<>
200struct traits<VariableIndex> : public Testable<VariableIndex> {
201};
202
203} //\ namespace gtsam
204
A thin wrapper around std::vector that uses a custom allocator.
A thin wrapper around std::map that uses boost's fast_pool_allocator.
The base class for all factors.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
std::uint64_t FactorIndex
Integer nonlinear factor index type.
Definition: types.h:72
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:112
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:155
FastVector< FactorIndex > FactorIndices
Define collection types:
Definition: Factor.h:33
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:69
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
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
Template to create a binary predicate.
Definition: Testable.h:111
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:151
The VariableIndex class computes and stores the block column structure of a factor graph.
Definition: VariableIndex.h:43
const FactorIndices & internalAt(Key variable) const
Internal version of 'at' that asserts existence.
Definition: VariableIndex.h:172
size_t nEntries() const
The number of nonzero blocks, i.e. the number of variable-factor entries.
Definition: VariableIndex.h:86
const_iterator begin() const
Iterator to the first variable entry.
Definition: VariableIndex.h:156
const_iterator find(Key key) const
Find the iterator for the requested variable entry.
Definition: VariableIndex.h:162
const FactorIndices & operator[](Key variable) const
Access a list of factors by variable.
Definition: VariableIndex.h:89
VariableIndex(const FG &factorGraph)
Create a VariableIndex that computes and stores the block column structure of a factor graph.
Definition: VariableIndex.h:71
bool empty(Key variable) const
Return true if no factors associated with a variable.
Definition: VariableIndex.h:98
size_t size() const
The number of variable entries. This is equal to the number of unique variable Keys.
Definition: VariableIndex.h:80
FactorIndices & internalAt(Key variable)
Internal version of 'at' that asserts existence.
Definition: VariableIndex.h:179
const_iterator end() const
Iterator to the first variable entry.
Definition: VariableIndex.h:159
VariableIndex()
Default constructor, creates an empty VariableIndex.
Definition: VariableIndex.h:64
size_t nFactors() const
The number of factors in the original factor graph.
Definition: VariableIndex.h:83