gtsam  4.0.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/Key.h>
21 #include <gtsam/base/FastMap.h>
22 #include <gtsam/base/FastVector.h>
23 #include <gtsam/dllexport.h>
24 
25 #include <boost/optional/optional.hpp>
26 #include <boost/smart_ptr/shared_ptr.hpp>
27 
28 #include <cassert>
29 #include <stdexcept>
30 
31 namespace gtsam {
32 
42 class GTSAM_EXPORT VariableIndex {
43 public:
44 
45  typedef boost::shared_ptr<VariableIndex> shared_ptr;
47  typedef Factors::iterator Factor_iterator;
48  typedef Factors::const_iterator Factor_const_iterator;
49 
50 protected:
52  KeyMap index_;
53  size_t nFactors_; // Number of factors in the original factor graph.
54  size_t nEntries_; // Sum of involved variable counts of each factor.
55 
56 public:
57  typedef KeyMap::const_iterator const_iterator;
58  typedef KeyMap::const_iterator iterator;
59  typedef KeyMap::value_type value_type;
60 
61 public:
62 
65 
67  VariableIndex() : nFactors_(0), nEntries_(0) {}
68 
73  template<class FG>
74  VariableIndex(const FG& factorGraph) : nFactors_(0), nEntries_(0) { augment(factorGraph); }
75 
79 
84  size_t size() const { return index_.size(); }
85 
87  size_t nFactors() const { return nFactors_; }
88 
90  size_t nEntries() const { return nEntries_; }
91 
93  const Factors& operator[](Key variable) const {
94  KeyMap::const_iterator item = index_.find(variable);
95  if(item == index_.end())
96  throw std::invalid_argument("Requested non-existent variable from VariableIndex");
97  else
98  return item->second;
99  }
100 
104 
106  bool equals(const VariableIndex& other, double tol=0.0) const;
107 
109  void print(const std::string& str = "VariableIndex: ",
110  const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
111 
116  void outputMetisFormat(std::ostream& os) const;
117 
118 
122 
127  template<class FG>
128  void augment(const FG& factors, boost::optional<const FastVector<size_t>&> newFactorIndices = boost::none);
129 
140  template<typename ITERATOR, class FG>
141  void remove(ITERATOR firstFactor, ITERATOR lastFactor, const FG& factors);
142 
144  template<typename ITERATOR>
145  void removeUnusedVariables(ITERATOR firstKey, ITERATOR lastKey);
146 
148  const_iterator begin() const { return index_.begin(); }
149 
151  const_iterator end() const { return index_.end(); }
152 
154  const_iterator find(Key key) const { return index_.find(key); }
155 
156 protected:
157  Factor_iterator factorsBegin(Key variable) { return internalAt(variable).begin(); }
158  Factor_iterator factorsEnd(Key variable) { return internalAt(variable).end(); }
159 
160  Factor_const_iterator factorsBegin(Key variable) const { return internalAt(variable).begin(); }
161  Factor_const_iterator factorsEnd(Key variable) const { return internalAt(variable).end(); }
162 
164  const Factors& internalAt(Key variable) const {
165  const KeyMap::const_iterator item = index_.find(variable);
166  assert(item != index_.end());
167  return item->second; }
168 
170  Factors& internalAt(Key variable) {
171  const KeyMap::iterator item = index_.find(variable);
172  assert(item != index_.end());
173  return item->second; }
174 
176 };
177 
179 template<>
180 struct traits<VariableIndex> : public Testable<VariableIndex> {
181 };
182 
183 } //\ namespace gtsam
184 
const_iterator find(Key key) const
Find the iterator for the requested variable entry.
Definition: VariableIndex.h:154
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:140
size_t nFactors() const
The number of factors in the original factor graph.
Definition: VariableIndex.h:87
const_iterator end() const
Iterator to the first variable entry.
Definition: VariableIndex.h:151
const_iterator begin() const
Iterator to the first variable entry.
Definition: VariableIndex.h:148
const Factors & operator[](Key variable) const
Access a list of factors by variable.
Definition: VariableIndex.h:93
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
The VariableIndex class computes and stores the block column structure of a factor graph...
Definition: VariableIndex.h:42
Template to create a binary predicate.
Definition: Testable.h:110
VariableIndex(const FG &factorGraph)
Create a VariableIndex that computes and stores the block column structure of a factor graph...
Definition: VariableIndex.h:74
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
const Factors & internalAt(Key variable) const
Internal version of &#39;at&#39; that asserts existence.
Definition: VariableIndex.h:164
size_t nEntries() const
The number of nonzero blocks, i.e.
Definition: VariableIndex.h:90
size_t size() const
The number of variable entries.
Definition: VariableIndex.h:84
VariableIndex()
Default constructor, creates an empty VariableIndex.
Definition: VariableIndex.h:67
A thin wrapper around std::vector that uses boost&#39;s pool_allocator.
A thin wrapper around std::map that uses boost&#39;s fast_pool_allocator.
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
Factors & internalAt(Key variable)
Internal version of &#39;at&#39; that asserts existence.
Definition: VariableIndex.h:170
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
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:33