gtsam
Loading...
Searching...
No Matches
ExtendedPriorFactor.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
21
22#include <optional>
23
24namespace gtsam {
25
41template <class VALUE>
43 public:
44 typedef VALUE T;
45
47 using NoiseModelFactor1<VALUE>::evaluateError;
48
49 protected:
50 typedef NoiseModelFactorN<VALUE> Base;
51
52 VALUE origin_;
53 std::optional<Vector>
55
57 GTSAM_CONCEPT_TESTABLE_TYPE(T)
58
59 public:
62
65
68
70 ExtendedPriorFactor(Key key, const T& origin, const SharedNoiseModel& model)
71 : Base(model, key), origin_(origin) {}
72
74 ExtendedPriorFactor(Key key, const T& origin, const Vector& mean,
75 const SharedNoiseModel& model)
76 : Base(model, key), origin_(origin), mean_(mean) {
77 if (mean.size() != static_cast<Eigen::Index>(model->dim()))
78 throw std::invalid_argument(
79 "ExtendedPriorFactor: mean dimension does not match noise model");
80 }
81
83 ExtendedPriorFactor(Key key, const T& origin, const Matrix& covariance)
84 : Base(noiseModel::Gaussian::Covariance(covariance), key),
85 origin_(origin) {}
86
88 ExtendedPriorFactor(Key key, const T& origin, const Vector& mean,
89 const Matrix& covariance)
90 : Base(noiseModel::Gaussian::Covariance(covariance), key),
91 origin_(origin),
92 mean_(mean) {
93 if (mean.size() != covariance.rows() ||
94 covariance.rows() != covariance.cols())
95 throw std::invalid_argument(
96 "ExtendedPriorFactor: mean and covariance dimensions do not match");
97 }
98
102
103 ~ExtendedPriorFactor() override {}
104
108
110 void print(const std::string& s, const KeyFormatter& keyFormatter =
111 DefaultKeyFormatter) const override {
112 std::cout << s << "ExtendedPriorFactor on " << keyFormatter(this->key())
113 << "\n";
114 traits<T>::Print(origin_, " origin: ");
115 if (mean_) {
116 gtsam::print(*mean_, " tangent space mean: ");
117 }
118 if (this->noiseModel_)
119 this->noiseModel_->print(" noise model: ");
120 else
121 std::cout << "no noise model" << std::endl;
122 }
123
125 bool equals(const NonlinearFactor& expected,
126 double tol = 1e-9) const override {
127 const This* e = dynamic_cast<const This*>(&expected);
128 if (!e) return false;
129 bool mean_equals =
130 (!mean_ && !e->mean_) ||
131 (mean_ && e->mean_ && equal_with_abs_tol(*mean_, *e->mean_, tol));
132 return Base::equals(*e, tol) &&
133 traits<T>::Equals(origin_, e->origin_, tol) && mean_equals;
134 }
135
139
141 gtsam::NonlinearFactor::shared_ptr clone() const override {
142 return std::static_pointer_cast<gtsam::NonlinearFactor>(
143 gtsam::NonlinearFactor::shared_ptr(new This(*this)));
144 }
145
147 Vector evaluateError(const T& x, OptionalMatrixType H) const override {
148 // manifold equivalent of z-x -> Local(x,z)
149 Vector error;
150#ifdef GTSAM_SLOW_BUT_CORRECT_BETWEENFACTOR
152 if (H) {
153 error = -traits<T>::Local(x, origin_, H, OptionalNone);
154 *H *= -1.0;
155 } else {
156 error = -traits<T>::Local(x, origin_);
157 }
158 } else
159#endif
160 {
161 if (H) {
162 *H = Matrix::Identity(traits<T>::GetDimension(x),
164 }
165 error = -traits<T>::Local(x, origin_);
166 }
167 if (mean_) {
168 return error - *mean_;
169 }
170 return error;
171 }
172
174 using Base::error;
175
177 double error(const T& x) const {
178 return this->noiseModel_->loss(evaluateError(x));
179 }
180
182 double likelihood(const T& x) const { return exp(-error(x)); }
183
187
188 const VALUE& origin() const { return origin_; }
189 const std::optional<Vector>& mean() const { return mean_; }
190
192 noiseModel::Gaussian::shared_ptr gaussianModel(
193 const std::string& method = "<unknown>",
194 bool throwOnFailure = false) const {
196 const auto& model = this->noiseModel();
197 if (!model) {
198 if (throwOnFailure) {
199 throw std::runtime_error(method + " requires a noise model");
200 }
201 return nullptr;
202 }
203 auto g = std::dynamic_pointer_cast<Gaussian>(model);
204 if (!g) {
205 if (throwOnFailure) {
206 throw std::runtime_error(method +
207 " is only implemented for Gaussian noise "
208 "models. The noise model used is of type " +
209 std::string(typeid(*model).name()));
210 }
211 return nullptr;
212 }
213 return g;
214 }
215
217 std::optional<Matrix> covariance(const std::string& method = "<unknown>",
218 bool throwOnFailure = false) const {
219 auto gaussian = gaussianModel(method, throwOnFailure);
220 if (!gaussian) return std::nullopt;
221 return gaussian->covariance();
222 }
223
225 using Gaussian = std::pair<Vector, Matrix>; // mean, covariance
226
228 std::optional<Gaussian> gaussian(const std::string& method = "<unknown>",
229 bool throwOnFailure = false) const {
230 auto cov = covariance(method, throwOnFailure);
231 if (!cov) return std::nullopt;
232 return Gaussian{this->mean_.value_or(Vector::Zero(this->dim())), *cov};
233 }
234
236
237 private:
238#if GTSAM_ENABLE_BOOST_SERIALIZATION
240 friend class boost::serialization::access;
241 template <class ARCHIVE>
242 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
243 // NoiseModelFactor1 instead of NoiseModelFactorN for backward compatibility
244 ar& boost::serialization::make_nvp(
245 "NoiseModelFactor1", boost::serialization::base_object<Base>(*this));
246 ar& BOOST_SERIALIZATION_NVP(origin_);
247 ar& BOOST_SERIALIZATION_NVP(mean_);
248 }
249#endif
250};
251
253template <class VALUE>
255 : public Testable<ExtendedPriorFactor<VALUE> > {};
256
257} // namespace gtsam
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
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
Detect whether a traits type provides Local with Jacobians.
Definition Manifold.h:145
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
bool equals(const This &other, double tol=1e-9) const
check equality
Definition Factor.cpp:42
virtual double error(const HybridValues &hybridValues) const
All factor types need to implement an error function.
Definition Factor.cpp:47
Gaussian implements the mathematical model |R*x|^2 = |y|^2 with R'*R=inv(Sigma) where y = whiten(x) =...
Definition NoiseModel.h:192
A class for a soft prior on any Value type, but with a non-zero mean in the tangent space.
Definition ExtendedPriorFactor.h:42
Vector evaluateError(const T &x, OptionalMatrixType H) const override
vector of errors
Definition ExtendedPriorFactor.h:147
noiseModel::Gaussian::shared_ptr gaussianModel(const std::string &method="<unknown>", bool throwOnFailure=false) const
Get the Gaussian noise model, or return nullopt/throw if not Gaussian.
Definition ExtendedPriorFactor.h:192
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition ExtendedPriorFactor.h:141
double error(const T &x) const
Definition ExtendedPriorFactor.h:177
void print(const std::string &s, const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition ExtendedPriorFactor.h:110
ExtendedPriorFactor(Key key, const T &origin, const SharedNoiseModel &model)
Constructor with noise model and optional mean in tangent space.
Definition ExtendedPriorFactor.h:70
ExtendedPriorFactor< T > This
Definition ExtendedPriorFactor.h:61
std::optional< Vector > mean_
Definition ExtendedPriorFactor.h:54
ExtendedPriorFactor(Key key, const T &origin, const Matrix &covariance)
Constructor with covariance matrix (zero mean in tangent space).
Definition ExtendedPriorFactor.h:83
ExtendedPriorFactor(Key key, const T &origin, const Vector &mean, const SharedNoiseModel &model)
Constructor with noise model and optional mean in tangent space.
Definition ExtendedPriorFactor.h:74
bool equals(const NonlinearFactor &expected, double tol=1e-9) const override
equals
Definition ExtendedPriorFactor.h:125
std::optional< Matrix > covariance(const std::string &method="<unknown>", bool throwOnFailure=false) const
Definition ExtendedPriorFactor.h:217
ExtendedPriorFactor(Key key, const T &origin, const Vector &mean, const Matrix &covariance)
Constructor with mean (in tangent space) and covariance matrix.
Definition ExtendedPriorFactor.h:88
double likelihood(const T &x) const
Compute the likelihood of a given value.
Definition ExtendedPriorFactor.h:182
std::optional< Gaussian > gaussian(const std::string &method="<unknown>", bool throwOnFailure=false) const
Definition ExtendedPriorFactor.h:228
std::pair< Vector, Matrix > Gaussian
Definition ExtendedPriorFactor.h:225
ExtendedPriorFactor()
default constructor - only use for serialization
Definition ExtendedPriorFactor.h:67
Key key() const
Definition NoiseModelFactorN.h:307
Nonlinear factor base class.
Definition NonlinearFactor.h:70
size_t dim() const override
get the dimension of the factor (number of rows on linearization)
Definition NonlinearFactor.h:251