gtsam 4.1.1
gtsam
MagPoseFactor.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
12#pragma once
13
16
17namespace gtsam {
18
27template <class POSE>
28class MagPoseFactor: public NoiseModelFactor1<POSE> {
29 private:
32 using Point = typename POSE::Translation;
33 using Rot = typename POSE::Rotation;
34
35 const Point measured_;
36 const Point nM_;
37 const Point bias_;
38 boost::optional<POSE> body_P_sensor_;
39
40 static const int MeasDim = Point::RowsAtCompileTime;
41 static const int PoseDim = traits<POSE>::dimension;
42 static const int RotDim = traits<Rot>::dimension;
43
45 using shared_ptr = boost::shared_ptr<MagPoseFactor<POSE>>;
46
48 GTSAM_CONCEPT_TESTABLE_TYPE(POSE)
49 GTSAM_CONCEPT_POSE_TYPE(POSE)
50
51 public:
52 ~MagPoseFactor() override {}
53
56
68 const Point& measured,
69 double scale,
70 const Point& direction,
71 const Point& bias,
72 const SharedNoiseModel& model,
73 const boost::optional<POSE>& body_P_sensor)
74 : Base(model, pose_key),
75 measured_(body_P_sensor ? body_P_sensor->rotation() * measured : measured),
76 nM_(scale * direction.normalized()),
77 bias_(body_P_sensor ? body_P_sensor->rotation() * bias : bias),
78 body_P_sensor_(body_P_sensor) {}
79
81 NonlinearFactor::shared_ptr clone() const override {
82 return boost::static_pointer_cast<NonlinearFactor>(
83 NonlinearFactor::shared_ptr(new This(*this)));
84 }
85
87
88 // Print out the factor.
89 void print(const std::string& s = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
90 Base::print(s, keyFormatter);
91 gtsam::print(Vector(nM_), "local field (nM): ");
92 gtsam::print(Vector(measured_), "measured field (bM): ");
93 gtsam::print(Vector(bias_), "magnetometer bias: ");
94 }
95
97 bool equals(const NonlinearFactor& expected, double tol=1e-9) const override {
98 const This *e = dynamic_cast<const This*> (&expected);
99 return e != nullptr && Base::equals(*e, tol) &&
100 gtsam::equal_with_abs_tol(this->measured_, e->measured_, tol) &&
101 gtsam::equal_with_abs_tol(this->nM_, e->nM_, tol) &&
102 gtsam::equal_with_abs_tol(this->bias_, e->bias_, tol);
103 }
104
106
111 Vector evaluateError(const POSE& nPb, boost::optional<Matrix&> H = boost::none) const override {
112 // Predict the measured magnetic field h(x) in the *body* frame.
113 // If body_P_sensor was given, bias_ will have been rotated into the body frame.
114 Matrix H_rot = Matrix::Zero(MeasDim, RotDim);
115 const Point hx = nPb.rotation().unrotate(nM_, H_rot, boost::none) + bias_;
116
117 if (H) {
118 // Fill in the relevant part of the Jacobian (just rotation columns).
119 *H = Matrix::Zero(MeasDim, PoseDim);
120 const size_t rot_col0 = nPb.rotationInterval().first;
121 (*H).block(0, rot_col0, MeasDim, RotDim) = H_rot;
122 }
123
124 return (hx - measured_);
125 }
126
127 private:
130 template<class ARCHIVE>
131 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
132 ar & boost::serialization::make_nvp("NoiseModelFactor1",
133 boost::serialization::base_object<Base>(*this));
134 ar & BOOST_SERIALIZATION_NVP(measured_);
135 ar & BOOST_SERIALIZATION_NVP(nM_);
136 ar & BOOST_SERIALIZATION_NVP(bias_);
137 ar & BOOST_SERIALIZATION_NVP(body_P_sensor_);
138 }
139}; // \class MagPoseFactor
140
141}
Non-linear factor base classes.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:155
noiseModel::Base::shared_ptr SharedNoiseModel
Note, deliberately not in noiseModel namespace.
Definition: NoiseModel.h:736
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:69
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
bool equal_with_abs_tol(const Eigen::DenseBase< MATRIX > &A, const Eigen::DenseBase< MATRIX > &B, double tol=1e-9)
equals with a tolerance
Definition: Matrix.h:84
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
This is the base class for all factor types.
Definition: Factor.h:56
Factor to estimate rotation of a Pose2 or Pose3 given a magnetometer reading.
Definition: MagPoseFactor.h:28
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Implement functions needed for Testable.
Definition: MagPoseFactor.h:89
~MagPoseFactor() override
Concept check by type.
Definition: MagPoseFactor.h:52
bool equals(const NonlinearFactor &expected, double tol=1e-9) const override
Equals function.
Definition: MagPoseFactor.h:97
NonlinearFactor::shared_ptr clone() const override
Definition: MagPoseFactor.h:81
friend class boost::serialization::access
Serialization function.
Definition: MagPoseFactor.h:129
MagPoseFactor(Key pose_key, const Point &measured, double scale, const Point &direction, const Point &bias, const SharedNoiseModel &model, const boost::optional< POSE > &body_P_sensor)
Construct the factor.
Definition: MagPoseFactor.h:67
Vector evaluateError(const POSE &nPb, boost::optional< Matrix & > H=boost::none) const override
Implement functions needed to derive from Factor.
Definition: MagPoseFactor.h:111
MagPoseFactor()
Default constructor - only use for serialization.
Definition: MagPoseFactor.h:55
Nonlinear factor base class.
Definition: NonlinearFactor.h:43
bool equals(const NonlinearFactor &f, double tol=1e-9) const override
Check if two factors are equal.
Definition: NonlinearFactor.cpp:71
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Print.
Definition: NonlinearFactor.cpp:63
A convenient base class for creating your own NoiseModelFactor with 1 variable.
Definition: NonlinearFactor.h:285
Concept-checking macros for geometric objects Each macro instantiates a concept check structure,...