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/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 
46  typedef boost::shared_ptr<VariableIndex> shared_ptr;
47  typedef FactorIndices Factors;
48  typedef Factors::iterator Factor_iterator;
49  typedef Factors::const_iterator Factor_const_iterator;
50 
51 protected:
53  KeyMap index_;
54  size_t nFactors_; // Number of factors in the original factor graph.
55  size_t nEntries_; // Sum of involved variable counts of each factor.
56 
57 public:
58  typedef KeyMap::const_iterator const_iterator;
59  typedef KeyMap::const_iterator iterator;
60  typedef KeyMap::value_type value_type;
61 
62 public:
63 
66 
68  VariableIndex() : nFactors_(0), nEntries_(0) {}
69 
74  template<class FG>
75  VariableIndex(const FG& factorGraph) : nFactors_(0), nEntries_(0) { augment(factorGraph); }
76 
80 
82  size_t size() const { return index_.size(); }
83 
85  size_t nFactors() const { return nFactors_; }
86 
88  size_t nEntries() const { return nEntries_; }
89 
91  const Factors& operator[](Key variable) const {
92  KeyMap::const_iterator item = index_.find(variable);
93  if(item == index_.end())
94  throw std::invalid_argument("Requested non-existent variable from VariableIndex");
95  else
96  return item->second;
97  }
98 
102 
104  bool equals(const VariableIndex& other, double tol=0.0) const;
105 
107  void print(const std::string& str = "VariableIndex: ",
108  const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
109 
114  void outputMetisFormat(std::ostream& os) const;
115 
116 
120 
125  template<class FG>
126  void augment(const FG& factors, boost::optional<const FactorIndices&> newFactorIndices = boost::none);
127 
138  template<typename ITERATOR, class FG>
139  void remove(ITERATOR firstFactor, ITERATOR lastFactor, const FG& factors);
140 
142  template<typename ITERATOR>
143  void removeUnusedVariables(ITERATOR firstKey, ITERATOR lastKey);
144 
146  const_iterator begin() const { return index_.begin(); }
147 
149  const_iterator end() const { return index_.end(); }
150 
152  const_iterator find(Key key) const { return index_.find(key); }
153 
154 protected:
155  Factor_iterator factorsBegin(Key variable) { return internalAt(variable).begin(); }
156  Factor_iterator factorsEnd(Key variable) { return internalAt(variable).end(); }
157 
158  Factor_const_iterator factorsBegin(Key variable) const { return internalAt(variable).begin(); }
159  Factor_const_iterator factorsEnd(Key variable) const { return internalAt(variable).end(); }
160 
162  const Factors& internalAt(Key variable) const {
163  const KeyMap::const_iterator item = index_.find(variable);
164  assert(item != index_.end());
165  return item->second; }
166 
168  Factors& internalAt(Key variable) {
169  const KeyMap::iterator item = index_.find(variable);
170  assert(item != index_.end());
171  return item->second; }
172 
174 };
175 
177 template<>
178 struct traits<VariableIndex> : public Testable<VariableIndex> {
179 };
180 
181 } //\ namespace gtsam
182 
const Factors & operator[](Key variable) const
Access a list of factors by variable.
Definition: VariableIndex.h:91
A thin wrapper around std::vector that uses a custom allocator.
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:141
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
const_iterator find(Key key) const
Find the iterator for the requested variable entry.
Definition: VariableIndex.h:152
Template to create a binary predicate.
Definition: Testable.h:110
The VariableIndex class computes and stores the block column structure of a factor graph.
Definition: VariableIndex.h:43
const Factors & internalAt(Key variable) const
Internal version of 'at' that asserts existence.
Definition: VariableIndex.h:162
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
size_t nEntries() const
The number of nonzero blocks, i.e. the number of variable-factor entries.
Definition: VariableIndex.h:88
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
VariableIndex()
Default constructor, creates an empty VariableIndex.
Definition: VariableIndex.h:68
A thin wrapper around std::map that uses boost's fast_pool_allocator.
size_t nFactors() const
The number of factors in the original factor graph.
Definition: VariableIndex.h:85
size_t size() const
The number of variable entries. This is equal to the number of unique variable Keys.
Definition: VariableIndex.h:82
Factors & internalAt(Key variable)
Internal version of 'at' that asserts existence.
Definition: VariableIndex.h:168
VariableIndex(const FG &factorGraph)
Create a VariableIndex that computes and stores the block column structure of a factor graph.
Definition: VariableIndex.h:75
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
FastVector< FactorIndex > FactorIndices
Define collection types:
Definition: Factor.h:32
The base class for all factors.
const_iterator end() const
Iterator to the first variable entry.
Definition: VariableIndex.h:149
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
const_iterator begin() const
Iterator to the first variable entry.
Definition: VariableIndex.h:146