gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VariableSlots.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 
19 #pragma once
20 
21 #include <gtsam/global_includes.h>
22 #include <gtsam/base/FastMap.h>
23 #include <gtsam/base/FastVector.h>
24 #include <gtsam/base/timing.h>
25 
26 #include <iostream>
27 
28 #include <boost/foreach.hpp>
29 #include <boost/tuple/tuple.hpp>
30 
31 #include <string>
32 
33 namespace gtsam {
34 
53 class VariableSlots : public FastMap<Key, FastVector<size_t> > {
54 
55 public:
56 
58  GTSAM_EXPORT static const size_t Empty;
59 
62 
68  template<class FG>
69  VariableSlots(const FG& factorGraph);
70 
72 
75 
77  GTSAM_EXPORT void print(const std::string& str = "VariableSlots: ") const;
78 
80  GTSAM_EXPORT bool equals(const VariableSlots& rhs, double tol = 0.0) const;
81 
83 };
84 
85 /* ************************************************************************* */
86 template<class FG>
87 VariableSlots::VariableSlots(const FG& factorGraph)
88 {
89  gttic(VariableSlots_constructor);
90  static const bool debug = false;
91 
92  // Compute a mapping (called variableSlots) *from* each involved
93  // variable that will be in the new joint factor *to* the slot in each
94  // removed factor in which that variable appears. For each variable,
95  // this is stored as a vector of slot numbers, stored in order of the
96  // removed factors. The slot number is the max integer value if the
97  // factor does not involve that variable.
98  size_t jointFactorPos = 0;
99  BOOST_FOREACH(const typename FG::sharedFactor& factor, factorGraph) {
100  assert(factor);
101  size_t factorVarSlot = 0;
102  BOOST_FOREACH(const Key involvedVariable, *factor) {
103  // Set the slot in this factor for this variable. If the
104  // variable was not already discovered, create an array for it
105  // that we'll fill with the slot indices for each factor that
106  // we're combining. Initially we put the max integer value in
107  // the array entry for each factor that will indicate the factor
108  // does not involve the variable.
109  iterator thisVarSlots; bool inserted;
110  boost::tie(thisVarSlots, inserted) = this->insert(std::make_pair(involvedVariable, FastVector<size_t>()));
111  if(inserted)
112  thisVarSlots->second.resize(factorGraph.size(), Empty);
113  thisVarSlots->second[jointFactorPos] = factorVarSlot;
114  if(debug) std::cout << " var " << involvedVariable << " rowblock " << jointFactorPos << " comes from factor's slot " << factorVarSlot << std::endl;
115  ++ factorVarSlot;
116  }
117  ++ jointFactorPos;
118  }
119 }
120 
121 }
Definition: FastMap.h:37
GTSAM_EXPORT void print(const std::string &str="VariableSlots: ") const
print
Definition: VariableSlots.cpp:30
A combined factor is assembled as one block of rows for each component factor.
Definition: VariableSlots.h:53
Included from all GTSAM files.
Timing utilities.
A thin wrapper around std::map that uses boost's fast_pool_allocator.
GTSAM_EXPORT bool equals(const VariableSlots &rhs, double tol=0.0) const
equals
Definition: VariableSlots.cpp:53
VariableSlots(const FG &factorGraph)
Constructor from a set of factors to be combined.
Definition: VariableSlots.h:87
size_t Key
Integer nonlinear key type.
Definition: types.h:59
A thin wrapper around std::vector that uses boost's pool_allocator.