gtsam  4.1.0
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 
20 #include <gtsam/inference/Factor.h>
21 #include <gtsam/inference/Key.h>
22 #include <gtsam/base/FastMap.h>
23 #include <gtsam/base/FastVector.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 
32 namespace gtsam {
33 
43 class 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 
164 protected:
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 
186 };
187 
189 template<>
190 struct traits<VariableIndex> : public Testable<VariableIndex> {
191 };
192 
193 } //\ namespace gtsam
194 
FastVector.h
A thin wrapper around std::vector that uses a custom allocator.
gtsam::equals
Template to create a binary predicate.
Definition: Testable.h:110
gtsam::VariableIndex::nEntries
size_t nEntries() const
The number of nonzero blocks, i.e. the number of variable-factor entries.
Definition: VariableIndex.h:86
VariableIndex-inl.h
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::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61
gtsam::VariableIndex::operator[]
const FactorIndices & operator[](Key variable) const
Access a list of factors by variable.
Definition: VariableIndex.h:89
gtsam::VariableIndex::empty
bool empty(Key variable) const
Return true if no factors associated with a variable.
Definition: VariableIndex.h:98
FastMap.h
A thin wrapper around std::map that uses boost's fast_pool_allocator.
gtsam::VariableIndex::nFactors
size_t nFactors() const
The number of factors in the original factor graph.
Definition: VariableIndex.h:83
gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
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::print
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:155
gtsam::Testable
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
gtsam::FactorIndex
std::uint64_t FactorIndex
Integer nonlinear factor index type.
Definition: types.h:64
Factor.h
The base class for all factors.
gtsam::FactorIndices
FastVector< FactorIndex > FactorIndices
Define collection types:
Definition: Factor.h:32
Key.h
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::VariableIndex
VariableIndex()
Default constructor, creates an empty VariableIndex.
Definition: VariableIndex.h:64
gtsam::FastSet< Key >
gtsam::VariableIndex::begin
const_iterator begin() const
Iterator to the first variable entry.
Definition: VariableIndex.h:156
gtsam::KeyFormatter
boost::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::VariableIndex::end
const_iterator end() const
Iterator to the first variable entry.
Definition: VariableIndex.h:159
gtsam::FastMap< Key, FactorIndices >
gtsam::VariableIndex::internalAt
const FactorIndices & internalAt(Key variable) const
Internal version of 'at' that asserts existence.
Definition: VariableIndex.h:172
gtsam::VariableIndex::internalAt
FactorIndices & internalAt(Key variable)
Internal version of 'at' that asserts existence.
Definition: VariableIndex.h:179
gtsam::VariableIndex
The VariableIndex class computes and stores the block column structure of a factor graph.
Definition: VariableIndex.h:43
gtsam::VariableIndex::find
const_iterator find(Key key) const
Find the iterator for the requested variable entry.
Definition: VariableIndex.h:162