GTSAM  4.0.2
C++ library for smoothing and mapping (SAM)
PriorFactor.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 
16 #pragma once
17 
19 #include <gtsam/base/Testable.h>
20 
21 #include <string>
22 
23 namespace gtsam {
24 
29  template<class VALUE>
30  class PriorFactor: public NoiseModelFactorN<VALUE> {
31 
32  public:
33  typedef VALUE T;
34 
35  // Provide access to the Matrix& version of evaluateError:
36  using NoiseModelFactor1<VALUE>::evaluateError;
37 
38 
39  private:
40 
42 
43  VALUE prior_;
46  GTSAM_CONCEPT_TESTABLE_TYPE(T)
47 
48  public:
49 
51  typedef typename std::shared_ptr<PriorFactor<VALUE> > shared_ptr;
52 
55 
58 
59  ~PriorFactor() override {}
60 
62  PriorFactor(Key key, const VALUE& prior, const SharedNoiseModel& model = nullptr) :
63  Base(model, key), prior_(prior) {
64  }
65 
67  PriorFactor(Key key, const VALUE& prior, const Matrix& covariance) :
68  Base(noiseModel::Gaussian::Covariance(covariance), key), prior_(prior) {
69  }
70 
72  gtsam::NonlinearFactor::shared_ptr clone() const override {
73  return std::static_pointer_cast<gtsam::NonlinearFactor>(
74  gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
75 
79  void print(const std::string& s,
80  const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
81  std::cout << s << "PriorFactor on " << keyFormatter(this->key()) << "\n";
82  traits<T>::Print(prior_, " prior mean: ");
83  if (this->noiseModel_)
84  this->noiseModel_->print(" noise model: ");
85  else
86  std::cout << "no noise model" << std::endl;
87  }
88 
90  bool equals(const NonlinearFactor& expected, double tol=1e-9) const override {
91  const This* e = dynamic_cast<const This*> (&expected);
92  return e != nullptr && Base::equals(*e, tol) && traits<T>::Equals(prior_, e->prior_, tol);
93  }
94 
98  Vector evaluateError(const T& x, OptionalMatrixType H) const override {
99  if (H) (*H) = Matrix::Identity(traits<T>::GetDimension(x),traits<T>::GetDimension(x));
100  // manifold equivalent of z-x -> Local(x,z)
101  return -traits<T>::Local(x, prior_);
102  }
103 
104  const VALUE & prior() const { return prior_; }
105 
106  private:
107 
108 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
109 
110  friend class boost::serialization::access;
111  template<class ARCHIVE>
112  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
113  // NoiseModelFactor1 instead of NoiseModelFactorN for backward compatibility
114  ar & boost::serialization::make_nvp("NoiseModelFactor1",
115  boost::serialization::base_object<Base>(*this));
116  ar & BOOST_SERIALIZATION_NVP(prior_);
117  }
118 #endif
119 
120  // Alignment, see https://eigen.tuxfamily.org/dox/group__TopicStructHavingEigenMembers.html
121  enum { NeedsToAlign = (sizeof(T) % 16) == 0 };
122  public:
124  };
125 
127  template<class VALUE>
128  struct traits<PriorFactor<VALUE> > : public Testable<PriorFactor<VALUE> > {};
129 
130 
131 }
Vector evaluateError(const T &x, OptionalMatrixType H) const override
Definition: PriorFactor.h:98
Definition: NonlinearFactor.h:431
bool equals(const NonlinearFactor &f, double tol=1e-9) const override
Concept check for values that can be used in unit tests.
PriorFactor(Key key, const VALUE &prior, const Matrix &covariance)
Definition: PriorFactor.h:67
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:113
Definition: Factor.h:69
Definition: Testable.h:152
Definition: Group.h:43
Key key() const
Definition: NonlinearFactor.h:582
Definition: NonlinearFactor.h:68
PriorFactor(Key key, const VALUE &prior, const SharedNoiseModel &model=nullptr)
Definition: PriorFactor.h:62
#define GTSAM_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
Definition: types.h:293
PriorFactor()
Definition: PriorFactor.h:57
const SharedNoiseModel & noiseModel() const
access to the noise model
Definition: NonlinearFactor.h:245
Matrix * OptionalMatrixType
Definition: NonlinearFactor.h:55
Definition: PriorFactor.h:30
std::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
Definition: chartTesting.h:28
Non-linear factor base classes.
bool equals(const NonlinearFactor &expected, double tol=1e-9) const override
Definition: PriorFactor.h:90
PriorFactor< VALUE > This
Typedef to this class.
Definition: PriorFactor.h:54
std::shared_ptr< PriorFactor< VALUE > > shared_ptr
shorthand for a smart pointer to a factor
Definition: PriorFactor.h:51
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition: PriorFactor.h:72
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
void print(const std::string &s, const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Definition: PriorFactor.h:79
noiseModel::Base::shared_ptr SharedNoiseModel
Definition: NoiseModel.h:741