gtsam
Loading...
Searching...
No Matches
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
14#include <gtsam/base/std_optional_serialization.h>
18
19#include <optional>
20
21namespace gtsam {
22
31template <class POSE>
32class MagPoseFactor: public NoiseModelFactorN<POSE> {
33 private:
34 using This = MagPoseFactor<POSE>;
35 using Base = NoiseModelFactorN<POSE>;
36 using Point = typename POSE::Translation;
37 using Rot = typename POSE::Rotation;
38
39 const Point measured_;
40 const Point nM_;
41 const Point bias_;
42 std::optional<POSE> body_P_sensor_;
43
44 static const int MeasDim = Point::RowsAtCompileTime;
45 static const int PoseDim = traits<POSE>::dimension;
46 static const int RotDim = traits<Rot>::dimension;
47
49 using shared_ptr = std::shared_ptr<MagPoseFactor<POSE>>;
50
52 GTSAM_CONCEPT_TESTABLE_TYPE(POSE)
53 GTSAM_CONCEPT_POSE_TYPE(POSE)
54
55 public:
56
57 // Provide access to the Matrix& version of evaluateError:
59
60 ~MagPoseFactor() override {}
61
64
76 const Point& measured,
77 double scale,
78 const Point& direction,
79 const Point& bias,
80 const SharedNoiseModel& model,
81 const std::optional<POSE>& body_P_sensor = {})
82 : Base(model, pose_key),
83 measured_(body_P_sensor ? body_P_sensor->rotation() * measured : measured),
84 nM_(scale * direction.normalized()),
85 bias_(body_P_sensor ? body_P_sensor->rotation() * bias : bias),
86 body_P_sensor_(body_P_sensor) {}
87
89 NonlinearFactor::shared_ptr clone() const override {
90 return std::static_pointer_cast<NonlinearFactor>(
91 NonlinearFactor::shared_ptr(new This(*this)));
92 }
93
95
96 // Print out the factor.
97 void print(const std::string& s = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
98 Base::print(s, keyFormatter);
99 gtsam::print(Vector(nM_), "local field (nM): ");
100 gtsam::print(Vector(measured_), "measured field (bM): ");
101 gtsam::print(Vector(bias_), "magnetometer bias: ");
102 }
103
105 bool equals(const NonlinearFactor& expected, double tol=1e-9) const override {
106 const This *e = dynamic_cast<const This*> (&expected);
107 return e != nullptr && Base::equals(*e, tol) &&
108 gtsam::equal_with_abs_tol(this->measured_, e->measured_, tol) &&
109 gtsam::equal_with_abs_tol(this->nM_, e->nM_, tol) &&
110 gtsam::equal_with_abs_tol(this->bias_, e->bias_, tol);
111 }
112
114
119 Vector evaluateError(const POSE& nPb, OptionalMatrixType H) const override {
120 // Predict the measured magnetic field h(x) in the *body* frame.
121 // If body_P_sensor was given, bias_ will have been rotated into the body frame.
122 Matrix H_rot = Matrix::Zero(MeasDim, RotDim);
123 const Point hx = nPb.rotation().unrotate(nM_, H_rot, OptionalNone) + bias_;
124
125 if (H) {
126 // Fill in the relevant part of the Jacobian (just rotation columns).
127 *H = Matrix::Zero(MeasDim, PoseDim);
128 const size_t rot_col0 = nPb.rotationInterval().first;
129 (*H).block(0, rot_col0, MeasDim, RotDim) = H_rot;
130 }
131
132 return (hx - measured_);
133 }
134
135 private:
136#if GTSAM_ENABLE_BOOST_SERIALIZATION
138 friend class boost::serialization::access;
139 template<class ARCHIVE>
140 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
141 // NoiseModelFactor1 instead of NoiseModelFactorN for backward compatibility
142 ar & boost::serialization::make_nvp("NoiseModelFactor1",
143 boost::serialization::base_object<Base>(*this));
144 ar & BOOST_SERIALIZATION_NVP(measured_);
145 ar & BOOST_SERIALIZATION_NVP(nM_);
146 ar & BOOST_SERIALIZATION_NVP(bias_);
147 ar & BOOST_SERIALIZATION_NVP(body_P_sensor_);
148 }
149#endif
150}; // \class MagPoseFactor
151
152}
Base class for noise model factors with N variables.
Non-linear factor base classes.
#define OptionalNone
These typedefs and aliases will help with making the evaluateError interface independent of boost TOD...
Definition NonlinearFactor.h:51
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
Matrix * OptionalMatrixType
This typedef will be used everywhere boost::optional<Matrix&> reference was used previously.
Definition NonlinearFactor.h:57
NoiseModelFactorT< Vector, ValueTypes... > NoiseModelFactorN
Noise model factor with N value types and dynamic-sized error vector.
Definition NoiseModelFactorN.h:561
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
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
noiseModel::Base::shared_ptr SharedNoiseModel
Aliases.
Definition NoiseModel.h:846
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
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:81
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
virtual void print(const std::string &s="Factor", const KeyFormatter &formatter=DefaultKeyFormatter) const
print
Definition Factor.cpp:29
bool equals(const This &other, double tol=1e-9) const
check equality
Definition Factor.cpp:42
MagPoseFactor(Key pose_key, const Point &measured, double scale, const Point &direction, const Point &bias, const SharedNoiseModel &model, const std::optional< POSE > &body_P_sensor={})
Construct the factor.
Definition MagPoseFactor.h:75
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Implement functions needed for Testable.
Definition MagPoseFactor.h:97
Vector evaluateError(const POSE &nPb, OptionalMatrixType H) const override
Implement functions needed to derive from Factor.
Definition MagPoseFactor.h:119
bool equals(const NonlinearFactor &expected, double tol=1e-9) const override
Equals function.
Definition MagPoseFactor.h:105
NonlinearFactor::shared_ptr clone() const override
Definition MagPoseFactor.h:89
MagPoseFactor()
Default constructor - only use for serialization.
Definition MagPoseFactor.h:63
virtual Vector evaluateError(const ValueTypes &... x, OptionalMatrixTypeT< ValueTypes >... H) const=0
Nonlinear factor base class.
Definition NonlinearFactor.h:70
Concept-checking macros for geometric objects Each macro instantiates a concept check structure,...