gtsam
Loading...
Searching...
No Matches
MagFactor.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
18
19#pragma once
20
22#include <gtsam/geometry/Rot2.h>
23#include <gtsam/geometry/Rot3.h>
26
27namespace gtsam {
28
35class MagFactor: public NoiseModelFactorN<Rot2> {
36
37 const Point3 measured_;
38 const Point3 nM_;
39 const Point3 bias_;
40
41public:
42
43 // Provide access to Matrix& version of evaluateError:
44 using NoiseModelFactor1<Rot2>::evaluateError;
45
55 MagFactor(Key key, const Point3& measured, double scale,
56 const Unit3& direction, const Point3& bias,
57 const SharedNoiseModel& model) :
58 NoiseModelFactorN<Rot2>(model, key), //
59 measured_(measured), nM_(scale * direction), bias_(bias) {
60 }
61
63 NonlinearFactor::shared_ptr clone() const override {
64 return std::static_pointer_cast<NonlinearFactor>(
65 NonlinearFactor::shared_ptr(new MagFactor(*this)));
66 }
67
68 static Point3 unrotate(const Rot2& R, const Point3& p,
70 Point3 q = Rot3::Yaw(R.theta()).unrotate(p, HR, {});
71 if (HR) {
72 // assign to temporary first to avoid error in Win-Debug mode
73 Matrix H = HR->col(2);
74 *HR = H;
75 }
76 return q;
77 }
78
82 Vector evaluateError(const Rot2& nRb, OptionalMatrixType H) const override {
83 // measured bM = nRb� * nM + b
84 Point3 hx = unrotate(nRb, nM_, H) + bias_;
85 return (hx - measured_);
86 }
87};
88
94class MagFactor1: public NoiseModelFactorN<Rot3> {
95
96 const Point3 measured_;
97 const Point3 nM_;
98 const Point3 bias_;
99
100public:
101
102 // Provide access to Matrix& version of evaluateError:
103 using NoiseModelFactor1<Rot3>::evaluateError;
104
105
107 MagFactor1(Key key, const Point3& measured, double scale,
108 const Unit3& direction, const Point3& bias,
109 const SharedNoiseModel& model) :
110 NoiseModelFactorN<Rot3>(model, key), //
111 measured_(measured), nM_(scale * direction), bias_(bias) {
112 }
113
115 NonlinearFactor::shared_ptr clone() const override {
116 return std::static_pointer_cast<NonlinearFactor>(
117 NonlinearFactor::shared_ptr(new MagFactor1(*this)));
118 }
119
123 Vector evaluateError(const Rot3& nRb, OptionalMatrixType H) const override {
124 // measured bM = nRb� * nM + b
125 Point3 hx = nRb.unrotate(nM_, H, OptionalNone) + bias_;
126 return (hx - measured_);
127 }
128};
129
135class MagFactor2 : public NoiseModelFactorT<Vector3, Point3, Point3> {
136
137 const Point3 measured_;
138 const Rot3 bRn_;
139
140public:
141
142 // Provide access to Matrix& version of evaluateError:
145
146
148 MagFactor2(Key key1, Key key2, const Point3& measured, const Rot3& nRb,
149 const SharedNoiseModel& model) :
150 Base(model, key1, key2), //
151 measured_(measured), bRn_(nRb.inverse()) {
152 }
153
155 NonlinearFactor::shared_ptr clone() const override {
156 return std::static_pointer_cast<NonlinearFactor>(
157 NonlinearFactor::shared_ptr(new MagFactor2(*this)));
158 }
159
165 Vector3 evaluateError(const Point3& nM, const Point3& bias,
167 OptionalMatrixType H2) const override {
168 // measured bM = nRb� * nM + b, where b is unknown bias
169 Point3 hx = bRn_.rotate(nM, OptionalNone, H1) + bias;
170 if (H2)
171 *H2 = I_3x3;
172 return (hx - measured_);
173 }
174};
175
182 : public NoiseModelFactorT<Vector3, double, Unit3, Point3> {
183
184 const Point3 measured_;
185 const Rot3 bRn_;
186
187public:
188
189 // Provide access to Matrix& version of evaluateError:
192
193
195 MagFactor3(Key key1, Key key2, Key key3, const Point3& measured,
196 const Rot3& nRb, const SharedNoiseModel& model) :
197 Base(model, key1, key2, key3), //
198 measured_(measured), bRn_(nRb.inverse()) {
199 }
200
202 NonlinearFactor::shared_ptr clone() const override {
203 return std::static_pointer_cast<NonlinearFactor>(
204 NonlinearFactor::shared_ptr(new MagFactor3(*this)));
205 }
206
212 Vector3 evaluateError(const double& scale, const Unit3& direction,
213 const Point3& bias, OptionalMatrixType H1,
214 OptionalMatrixType H2, OptionalMatrixType H3) const override {
215 // measured bM = nRb� * nM + b, where b is unknown bias
216 Unit3 rotated = bRn_.rotate(direction, OptionalNone, H2);
217 Point3 hx = scale * rotated.point3() + bias;
218 if (H1)
219 *H1 = rotated.point3();
220 if (H2) // H2 is 2*2, but we need 3*2
221 {
222 Matrix H;
223 rotated.point3(H);
224 *H2 = scale * H * (*H2);
225 }
226 if (H3)
227 *H3 = I_3x3;
228 return (hx - measured_);
229 }
230};
231
232}
Macros for Matrix constants to avoid excessive template instantiation.
2D rotation
3D rotation represented as a rotation matrix or quaternion
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
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
Vector3 Point3
As of GTSAM 4, in order to make GTSAM more lean, it is now possible to just typedef Point3 to Vector3...
Definition Point3.h:38
noiseModel::Base::shared_ptr SharedNoiseModel
Aliases.
Definition NoiseModel.h:846
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
Rotation matrix NOTE: the angle theta is in radians unless explicitly stated.
Definition Rot2.h:40
Rot3 is a 3D rotation represented as a rotation matrix if the preprocessor symbol GTSAM_USE_QUATERNIO...
Definition Rot3.h:65
static Rot3 Yaw(double t)
Positive yaw is to right (as in aircraft heading). See ypr.
Definition Rot3.h:190
Point3 unrotate(const Point3 &p, OptionalJacobian< 3, 3 > H1={}, OptionalJacobian< 3, 3 > H2={}) const
rotate point from world to rotated frame
Definition Rot3.cpp:143
Represents a 3D point on a unit sphere.
Definition Unit3.h:44
Point3 point3(OptionalJacobian< 3, 2 > H={}) const
Return unit-norm Point3.
Definition Unit3.cpp:144
Vector evaluateError(const Rot2 &nRb, OptionalMatrixType H) const override
vector of errors
Definition MagFactor.h:82
NonlinearFactor::shared_ptr clone() const override
Definition MagFactor.h:63
MagFactor(Key key, const Point3 &measured, double scale, const Unit3 &direction, const Point3 &bias, const SharedNoiseModel &model)
Constructor of factor that estimates nav to body rotation bRn.
Definition MagFactor.h:55
MagFactor1(Key key, const Point3 &measured, double scale, const Unit3 &direction, const Point3 &bias, const SharedNoiseModel &model)
Constructor.
Definition MagFactor.h:107
Vector evaluateError(const Rot3 &nRb, OptionalMatrixType H) const override
vector of errors
Definition MagFactor.h:123
NonlinearFactor::shared_ptr clone() const override
Definition MagFactor.h:115
NonlinearFactor::shared_ptr clone() const override
Definition MagFactor.h:155
MagFactor2(Key key1, Key key2, const Point3 &measured, const Rot3 &nRb, const SharedNoiseModel &model)
Constructor.
Definition MagFactor.h:148
Vector3 evaluateError(const Point3 &nM, const Point3 &bias, OptionalMatrixType H1, OptionalMatrixType H2) const override
vector of errors
Definition MagFactor.h:165
Vector3 evaluateError(const double &scale, const Unit3 &direction, const Point3 &bias, OptionalMatrixType H1, OptionalMatrixType H2, OptionalMatrixType H3) const override
vector of errors
Definition MagFactor.h:212
MagFactor3(Key key1, Key key2, Key key3, const Point3 &measured, const Rot3 &nRb, const SharedNoiseModel &model)
Constructor.
Definition MagFactor.h:195
NonlinearFactor::shared_ptr clone() const override
Definition MagFactor.h:202
NoiseModelFactorT()
Definition NoiseModelFactorN.h:257
virtual Vector3 evaluateError(const ValueTypes &... x, OptionalMatrixTypeT< ValueTypes >... H) const=0
Key key() const
Definition NoiseModelFactorN.h:307