gtsam
Loading...
Searching...
No Matches
RangeFactor.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
23
24#include <type_traits>
25
26namespace gtsam {
27
28// forward declaration of Range functor, assumed partially specified
29template <typename A1, typename A2>
30struct Range;
31
37template <typename A1, typename A2 = A1, typename T = double>
39 : public NoiseModelFactorT<typename traits<T>::TangentVector, A1, A2> {
40 private:
41 typedef RangeFactor<A1, A2, T> This;
42 using ErrorVector = typename traits<T>::TangentVector;
44
45 T measured_;
46
47 public:
49 RangeFactor() = default;
50
52 RangeFactor(Key key1, Key key2, T measured, const SharedNoiseModel& model)
53 : Base(model, key1, key2), measured_(measured) {}
54
56 gtsam::NonlinearFactor::shared_ptr clone() const override {
57 return std::static_pointer_cast<gtsam::NonlinearFactor>(
58 gtsam::NonlinearFactor::shared_ptr(new This(*this)));
59 }
60
62 const T& measured() const { return measured_; }
63
65 ErrorVector evaluateError(const A1& a1, const A2& a2,
67 OptionalMatrixType H2 = OptionalNone) const override {
68 const T predicted = Range<A1, A2>()(a1, a2, H1, H2);
69 return -traits<T>::Local(predicted, measured_);
70 }
71
73 void print(const std::string& s = "",
74 const KeyFormatter& kf = DefaultKeyFormatter) const override {
75 std::cout << s << "RangeFactor" << std::endl;
76 Base::print(s, kf);
77 traits<T>::Print(measured_, " measured: ");
78 }
79
81 bool equals(const NonlinearFactor& f, double tol) const override {
82 const This* p = dynamic_cast<const This*>(&f);
83 return p != nullptr && Base::equals(f, tol) &&
84 traits<T>::Equals(measured_, p->measured_, tol);
85 }
86
87 private:
88#if GTSAM_ENABLE_BOOST_SERIALIZATION
89 friend class boost::serialization::access;
91 template <class ARCHIVE>
92 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
93 ar& boost::serialization::make_nvp(
94 "NoiseModelFactor2", boost::serialization::base_object<Base>(*this));
95 ar& BOOST_SERIALIZATION_NVP(measured_);
96 }
97#endif
98}; // \ RangeFactor
99
101template <typename A1, typename A2, typename T>
102struct traits<RangeFactor<A1, A2, T> >
103 : public Testable<RangeFactor<A1, A2, T> > {};
104
109template <typename A1, typename A2 = A1,
110 typename T = typename Range<A1, A2>::result_type>
112 : public NoiseModelFactorT<typename traits<T>::TangentVector, A1, A2> {
113 private:
116 using ErrorVector = typename traits<T>::TangentVector;
117
118 T measured_;
119 A1 body_T_sensor_;
120
121 public:
124
127 const SharedNoiseModel& model,
128 const A1& body_T_sensor)
129 : Base(model, key1, key2),
130 measured_(measured),
131 body_T_sensor_(body_T_sensor) {}
132
135
137 gtsam::NonlinearFactor::shared_ptr clone() const override {
138 return std::static_pointer_cast<gtsam::NonlinearFactor>(
139 gtsam::NonlinearFactor::shared_ptr(new This(*this)));
140 }
141
143 const T& measured() const { return measured_; }
144
146 ErrorVector evaluateError(const A1& a1, const A2& a2,
148 OptionalMatrixType H2 = OptionalNone) const override {
149 Matrix HposeCompose;
150 const A1 nav_T_sensor =
151 a1.compose(body_T_sensor_, H1 ? &HposeCompose : nullptr);
152 const T predicted = Range<A1, A2>()(nav_T_sensor, a2, H1, H2);
153 if (H1) *H1 *= HposeCompose;
154 return -traits<T>::Local(predicted, measured_);
155 }
156
157 // An evaluateError overload to accept matrices (Matrix&) and pass it to the
158 // OptionalMatrixType evaluateError overload
159 Vector evaluateError(const A1& a1, const A2& a2, Matrix& H1,
160 Matrix& H2) const {
161 return evaluateError(a1, a2, &H1, &H2);
162 }
163
165 void print(
166 const std::string& s = "",
167 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
168 std::cout << s << "RangeFactorWithTransform" << std::endl;
169 this->body_T_sensor_.print(" sensor pose in body frame: ");
170 Base::print(s, keyFormatter);
171 traits<T>::Print(measured_, " measured: ");
172 }
173
175 bool equals(const NonlinearFactor& f, double tol) const override {
176 const This* p = dynamic_cast<const This*>(&f);
177 return p != nullptr && Base::equals(f, tol) &&
178 traits<T>::Equals(measured_, p->measured_, tol) &&
179 traits<A1>::Equals(body_T_sensor_, p->body_T_sensor_, tol);
180 }
181
182 private:
183#if GTSAM_ENABLE_BOOST_SERIALIZATION
184 friend class boost::serialization::access;
186 template <typename ARCHIVE>
187 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
188 ar& boost::serialization::make_nvp(
189 "NoiseModelFactor2", boost::serialization::base_object<Base>(*this));
190 ar& BOOST_SERIALIZATION_NVP(measured_);
191 ar& BOOST_SERIALIZATION_NVP(body_T_sensor_);
192 }
193#endif
194}; // \ RangeFactorWithTransform
195
197template <typename A1, typename A2, typename T>
199 : public Testable<RangeFactorWithTransform<A1, A2, T> > {};
200
206template <typename A1, typename A2 = A1,
207 typename T = typename Range<A1, A2>::result_type>
209 : public NoiseModelFactorT<typename traits<T>::TangentVector, A1, A2, T> {
210 private:
213 using ErrorVector = typename traits<T>::TangentVector;
214
215 T measured_;
216 A1 body_T_sensor_;
217
218 public:
221
225 const A1& body_T_sensor)
226 : Base(noiseModel, key1, key2, key3),
227 measured_(measured),
228 body_T_sensor_(body_T_sensor) {}
229
231 ~RangeFactorWithTransformBias() override = default;
232
234 gtsam::NonlinearFactor::shared_ptr clone() const override {
235 return std::static_pointer_cast<gtsam::NonlinearFactor>(
236 gtsam::NonlinearFactor::shared_ptr(new This(*this)));
237 }
238
240 const T& measured() const { return measured_; }
241
243 ErrorVector evaluateError(const A1& a1, const A2& a2, const T& bias,
246 OptionalMatrixType H3 = OptionalNone) const override {
247 Matrix HposeCompose;
248 const A1 nav_T_sensor =
249 a1.compose(body_T_sensor_, H1 ? &HposeCompose : nullptr);
250 const T predictedRange = Range<A1, A2>()(nav_T_sensor, a2, H1, H2);
251 if (H1) *H1 *= HposeCompose;
252 if (H3) {
253 if constexpr (std::is_same_v<T, double>) {
254 *H3 = Matrix1::Identity();
255 } else {
256 const int dimension = traits<T>::GetDimension(measured_);
257 *H3 = Matrix::Identity(dimension, dimension);
258 }
259 }
260 return -traits<T>::Local(predictedRange + bias, measured_);
261 }
262
264 Vector evaluateError(const A1& a1, const A2& a2, const T& bias, Matrix& H1,
265 Matrix& H2, Matrix& H3) const {
266 return evaluateError(a1, a2, bias, &H1, &H2, &H3);
267 }
268
270 void print(
271 const std::string& s = "",
272 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
273 std::cout << s << "RangeFactorWithTransformBias" << std::endl;
274 body_T_sensor_.print(" sensor pose in body frame: ");
275 Base::print(s, keyFormatter);
276 traits<T>::Print(measured_, " measured: ");
277 }
278
280 bool equals(const NonlinearFactor& f, double tol) const override {
281 const This* p = dynamic_cast<const This*>(&f);
282 return p != nullptr && Base::equals(f, tol) &&
283 traits<T>::Equals(measured_, p->measured_, tol) &&
284 traits<A1>::Equals(body_T_sensor_, p->body_T_sensor_, tol);
285 }
286
287 private:
288#if GTSAM_ENABLE_BOOST_SERIALIZATION
289 friend class boost::serialization::access;
291 template <typename ARCHIVE>
292 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
293 ar& boost::serialization::make_nvp(
294 "NoiseModelFactor3", boost::serialization::base_object<Base>(*this));
295 ar& BOOST_SERIALIZATION_NVP(measured_);
296 ar& BOOST_SERIALIZATION_NVP(body_T_sensor_);
297 }
298#endif
299}; // \ RangeFactorWithTransformBias
300
302template <typename A1, typename A2, typename T>
304 : public Testable<RangeFactorWithTransformBias<A1, A2, T> > {};
305
306} // namespace gtsam
Arbitrary-arity Jacobian factor with compile-time block dimensions.
Base class for noise model factors with N variables.
#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
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
All noise models live in the noiseModel namespace.
Definition LossFunctions.cpp:33
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
Definition BearingRange.h:42
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
Nonlinear factor base class.
Definition NonlinearFactor.h:70
Binary factor for a range measurement Works for any two types A1,A2 for which the functor Range<A1,...
Definition RangeFactor.h:39
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition RangeFactor.h:56
bool equals(const NonlinearFactor &f, double tol) const override
Check equality up to a tolerance.
Definition RangeFactor.h:81
const T & measured() const
Definition RangeFactor.h:62
ErrorVector evaluateError(const A1 &a1, const A2 &a2, OptionalMatrixType H1=OptionalNone, OptionalMatrixType H2=OptionalNone) const override
Evaluate the unwhitened range error and optional Jacobians.
Definition RangeFactor.h:65
void print(const std::string &s="", const KeyFormatter &kf=DefaultKeyFormatter) const override
print
Definition RangeFactor.h:73
RangeFactor(Key key1, Key key2, T measured, const SharedNoiseModel &model)
Construct from keys, a range measurement, and a noise model.
Definition RangeFactor.h:52
RangeFactor()=default
default constructor
Binary factor for a range measurement, with a transform applied.
Definition RangeFactor.h:112
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition RangeFactor.h:137
RangeFactorWithTransform()=default
Default constructor.
RangeFactorWithTransform(Key key1, Key key2, T measured, const SharedNoiseModel &model, const A1 &body_T_sensor)
Construct from keys, a range measurement, a noise model, and transform.
Definition RangeFactor.h:126
ErrorVector evaluateError(const A1 &a1, const A2 &a2, OptionalMatrixType H1=OptionalNone, OptionalMatrixType H2=OptionalNone) const override
Evaluate the unwhitened range error and optional Jacobians.
Definition RangeFactor.h:146
~RangeFactorWithTransform() override
Destroy this factor.
Definition RangeFactor.h:134
bool equals(const NonlinearFactor &f, double tol) const override
Check equality up to a tolerance.
Definition RangeFactor.h:175
const T & measured() const
Definition RangeFactor.h:143
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print contents
Definition RangeFactor.h:165
Ternary factor for a range measurement with a fixed sensor transform and an additive bias term.
Definition RangeFactor.h:209
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition RangeFactor.h:234
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Print contents.
Definition RangeFactor.h:270
ErrorVector evaluateError(const A1 &a1, const A2 &a2, const T &bias, OptionalMatrixType H1=OptionalNone, OptionalMatrixType H2=OptionalNone, OptionalMatrixType H3=OptionalNone) const override
Evaluate the unwhitened range error and optional Jacobians.
Definition RangeFactor.h:243
RangeFactorWithTransformBias()=default
Default constructor.
RangeFactorWithTransformBias(Key key1, Key key2, Key key3, T measured, const SharedNoiseModel &noiseModel, const A1 &body_T_sensor)
Construct from keys, a range measurement, a noise model, and transform.
Definition RangeFactor.h:223
~RangeFactorWithTransformBias() override=default
Destroy this factor.
Vector evaluateError(const A1 &a1, const A2 &a2, const T &bias, Matrix &H1, Matrix &H2, Matrix &H3) const
Matrix-reference overload for evaluateError.
Definition RangeFactor.h:264
const T & measured() const
Definition RangeFactor.h:240
bool equals(const NonlinearFactor &f, double tol) const override
Check equality up to a tolerance.
Definition RangeFactor.h:280