gtsam  4.0.0
gtsam
PriorFactor.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 
16 #pragma once
17 
19 #include <gtsam/base/Testable.h>
20 
21 namespace gtsam {
22 
27  template<class VALUE>
28  class PriorFactor: public NoiseModelFactor1<VALUE> {
29 
30  public:
31  typedef VALUE T;
32 
33  private:
34 
36 
37  VALUE prior_;
40  GTSAM_CONCEPT_TESTABLE_TYPE(T)
41 
42  public:
43 
45  typedef typename boost::shared_ptr<PriorFactor<VALUE> > shared_ptr;
46 
49 
52 
53  virtual ~PriorFactor() {}
54 
56  PriorFactor(Key key, const VALUE& prior, const SharedNoiseModel& model = nullptr) :
57  Base(model, key), prior_(prior) {
58  }
59 
61  PriorFactor(Key key, const VALUE& prior, const Matrix& covariance) :
62  Base(noiseModel::Gaussian::Covariance(covariance), key), prior_(prior) {
63  }
64 
66  virtual gtsam::NonlinearFactor::shared_ptr clone() const {
67  return boost::static_pointer_cast<gtsam::NonlinearFactor>(
68  gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
69 
73  virtual void print(const std::string& s, const KeyFormatter& keyFormatter = DefaultKeyFormatter) const {
74  std::cout << s << "PriorFactor on " << keyFormatter(this->key()) << "\n";
75  traits<T>::Print(prior_, " prior mean: ");
76  this->noiseModel_->print(" noise model: ");
77  }
78 
80  virtual bool equals(const NonlinearFactor& expected, double tol=1e-9) const {
81  const This* e = dynamic_cast<const This*> (&expected);
82  return e != NULL && Base::equals(*e, tol) && traits<T>::Equals(prior_, e->prior_, tol);
83  }
84 
88  Vector evaluateError(const T& x, boost::optional<Matrix&> H = boost::none) const {
89  if (H) (*H) = Matrix::Identity(traits<T>::GetDimension(x),traits<T>::GetDimension(x));
90  // manifold equivalent of z-x -> Local(x,z)
91  // TODO(ASL) Add Jacobians.
92  return -traits<T>::Local(x, prior_);
93  }
94 
95  const VALUE & prior() const { return prior_; }
96 
97  private:
98 
101  template<class ARCHIVE>
102  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
103  ar & boost::serialization::make_nvp("NoiseModelFactor1",
104  boost::serialization::base_object<Base>(*this));
105  ar & BOOST_SERIALIZATION_NVP(prior_);
106  }
107 
108  // Alignment, see https://eigen.tuxfamily.org/dox/group__TopicStructHavingEigenMembers.html
109  enum { NeedsToAlign = (sizeof(T) % 16) == 0 };
110  public:
111  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
112  };
113 
114 }
This is the base class for all factor types.
Definition: Factor.h:54
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
virtual bool equals(const NonlinearFactor &expected, double tol=1e-9) const
equals
Definition: PriorFactor.h:80
Definition: PriorFactor.h:28
PriorFactor(Key key, const VALUE &prior, const Matrix &covariance)
Convenience constructor that takes a full covariance argument.
Definition: PriorFactor.h:61
Vector evaluateError(const T &x, boost::optional< Matrix & > H=boost::none) const
implement functions needed to derive from Factor
Definition: PriorFactor.h:88
friend class boost::serialization::access
Serialization function.
Definition: PriorFactor.h:100
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
A convenient base class for creating your own NoiseModelFactor with 1 variable.
Definition: NonlinearFactor.h:276
Nonlinear factor base class.
Definition: NonlinearFactor.h:50
const SharedNoiseModel & noiseModel() const
access to the noise model
Definition: NonlinearFactor.h:210
virtual gtsam::NonlinearFactor::shared_ptr clone() const
Definition: PriorFactor.h:66
virtual void print(const std::string &s, const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
implement functions needed for Testable
Definition: PriorFactor.h:73
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
Non-linear factor base classes.
boost::shared_ptr< PriorFactor< VALUE > > shared_ptr
The measurement.
Definition: PriorFactor.h:45
PriorFactor()
default constructor - only use for serialization
Definition: PriorFactor.h:51
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
Concept check for values that can be used in unit tests.
noiseModel::Base::shared_ptr SharedNoiseModel
Note, deliberately not in noiseModel namespace.
Definition: NoiseModel.h:1072
PriorFactor< VALUE > This
Typedef to this class.
Definition: PriorFactor.h:48
virtual bool equals(const NonlinearFactor &f, double tol=1e-9) const
Check if two factors are equal.
Definition: NonlinearFactor.cpp:71
PriorFactor(Key key, const VALUE &prior, const SharedNoiseModel &model=nullptr)
Constructor.
Definition: PriorFactor.h:56