gtsam  4.0.0
gtsam
FactorGraph-inst.h
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 
22 #pragma once
23 
25 
26 #include <boost/bind.hpp>
27 
28 #include <stdio.h>
29 #include <sstream>
30 #include <iostream> // for cout :-(
31 
32 namespace gtsam {
33 
34  /* ************************************************************************* */
35  template<class FACTOR>
36  void FactorGraph<FACTOR>::print(const std::string& s, const KeyFormatter& formatter) const {
37  std::cout << s << std::endl;
38  std::cout << "size: " << size() << std::endl;
39  for (size_t i = 0; i < factors_.size(); i++) {
40  std::stringstream ss;
41  ss << "factor " << i << ": ";
42  if (factors_[i])
43  factors_[i]->print(ss.str(), formatter);
44  }
45  }
46 
47  /* ************************************************************************* */
48  template<class FACTOR>
49  bool FactorGraph<FACTOR>::equals(const This& fg, double tol) const {
51  if (factors_.size() != fg.size()) return false;
52 
54  for (size_t i = 0; i < factors_.size(); i++) {
55  // TODO: Doesn't this force order of factor insertion?
56  sharedFactor f1 = factors_[i], f2 = fg.factors_[i];
57  if (f1 == NULL && f2 == NULL) continue;
58  if (f1 == NULL || f2 == NULL) return false;
59  if (!f1->equals(*f2, tol)) return false;
60  }
61  return true;
62  }
63 
64  /* ************************************************************************* */
65  template<class FACTOR>
67  size_t size_ = 0;
68  for(const sharedFactor& factor: factors_)
69  if (factor) size_++;
70  return size_;
71  }
72 
73  /* ************************************************************************* */
74  template<class FACTOR>
76  KeySet keys;
77  for(const sharedFactor& factor: this->factors_) {
78  if(factor)
79  keys.insert(factor->begin(), factor->end());
80  }
81  return keys;
82  }
83 
84  /* ************************************************************************* */
85  template <class FACTOR>
87  KeyVector keys;
88  keys.reserve(2 * size()); // guess at size
89  for (const sharedFactor& factor: factors_)
90  if (factor)
91  keys.insert(keys.end(), factor->begin(), factor->end());
92  std::sort(keys.begin(), keys.end());
93  auto last = std::unique(keys.begin(), keys.end());
94  keys.erase(last, keys.end());
95  return keys;
96  }
97 
98  /* ************************************************************************* */
99 } // namespace gtsam
void print(const std::string &s="FactorGraph", const KeyFormatter &formatter=DefaultKeyFormatter) const
print out graph
Definition: FactorGraph-inst.h:36
KeyVector keyVector() const
Potentially slow function to return all keys involved, sorted, as a vector.
Definition: FactorGraph-inst.h:86
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
size_t nrFactors() const
return the number of non-null factors
Definition: FactorGraph-inst.h:66
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:56
boost::shared_ptr< NonlinearFactor > sharedFactor
Shared pointer to a factor.
Definition: FactorGraph.h:88
bool equals(const This &fg, double tol=1e-9) const
Check equality.
Definition: FactorGraph-inst.h:49
FastVector< sharedFactor > factors_
concept check, makes sure FACTOR defines print and equals
Definition: FactorGraph.h:102
KeySet keys() const
Potentially slow function to return all keys involved, sorted, as a set.
Definition: FactorGraph-inst.h:75
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
Factor Graph Base Class.
size_t size() const
return the number of factors (including any null factors set by remove() ).
Definition: FactorGraph.h:275