gtsam
Loading...
Searching...
No Matches
PartialPriorFactor.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
17
18#pragma once
19
20#include <gtsam/config.h>
21
22#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
23
24#include <gtsam/base/Lie.h>
26
27#include <cassert>
28
29namespace gtsam {
30
63template <class VALUE>
64class PartialPriorFactor : public NoiseModelFactorN<VALUE> {
65 public:
66 typedef VALUE T;
67
68 protected:
69 // Concept checks on the variable type - currently requires Lie
70 GTSAM_CONCEPT_LIE_TYPE(VALUE)
71
72 typedef NoiseModelFactorN<VALUE> Base;
73 typedef PartialPriorFactor<VALUE> This;
74 typedef FunctorizedFactor<Vector, VALUE> InternalFactor;
75
76 Vector prior_;
77 std::vector<size_t> indices_;
78 InternalFactor internalFactor_;
79
81 static InternalFactor MakeInternalFactor(
82 Key key, const Vector& prior, const SharedNoiseModel& model,
83 const std::vector<size_t>& indices) {
84 auto projection =
85 [indices](const T& value,
86 OptionalJacobian<Eigen::Dynamic, Eigen::Dynamic> H = {}) {
87 constexpr int Dim = traits<T>::dimension;
88 Eigen::Matrix<double, Dim, Dim> Hlocal;
89
90// Rot3's Cayley chart cannot provide the Logmap Jacobian.
91#ifdef GTSAM_ROT3_EXPMAP
92 const Vector fullTangent = traits<T>::Local(
93 traits<T>::Identity(), value, {}, H ? &Hlocal : nullptr);
94#else
95 const Vector fullTangent =
96 traits<T>::Logmap(value, H ? &Hlocal : nullptr);
97#endif
98
99 if (H) {
100 H->setZero(indices.size(), Dim);
101 for (size_t i = 0; i < indices.size(); ++i) {
102 H->row(i) = Hlocal.row(indices.at(i));
103 }
104 }
105
106 Vector partialTangent(indices.size());
107 for (size_t i = 0; i < indices.size(); ++i) {
108 partialTangent(i) = fullTangent(indices.at(i));
109 }
110 return partialTangent;
111 };
112 return MakeFunctorizedFactor<T>(key, prior, model, projection);
113 }
114
116 void initializeInternalFactor() {
117 internalFactor_ =
118 MakeInternalFactor(this->key1(), prior_, this->noiseModel(), indices_);
119 }
120
125 PartialPriorFactor(Key key, const SharedNoiseModel& model)
126 : Base(model, key),
127 internalFactor_(MakeInternalFactor(key, prior_, model, indices_)) {}
128
129 public:
130
131 // Provide access to the Matrix& version of evaluateError:
132 using Base::evaluateError;
133
135 PartialPriorFactor() {}
136
138 PartialPriorFactor(Key key, size_t idx, double prior,
139 const SharedNoiseModel& model)
140 : Base(model, key),
141 prior_(Vector{{prior}}),
142 indices_(1, idx),
143 internalFactor_(MakeInternalFactor(key, prior_, model, indices_)) {
144 assert(model->dim() == 1);
145 }
146
148 PartialPriorFactor(Key key, const std::vector<size_t>& indices, const Vector& prior,
149 const SharedNoiseModel& model) :
150 Base(model, key),
151 prior_(prior),
152 indices_(indices),
153 internalFactor_(MakeInternalFactor(key, prior_, model, indices_)) {
154 assert((size_t)prior_.size() == indices_.size());
155 assert(model->dim() == (size_t)prior.size());
156 }
157
158 ~PartialPriorFactor() override {}
159
161 gtsam::NonlinearFactor::shared_ptr clone() const override {
162 return std::static_pointer_cast<gtsam::NonlinearFactor>(
163 gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
164
166
168 void print(const std::string& s, const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
169 Base::print(s, keyFormatter);
170 gtsam::print(prior_, "Prior: ");
171 std::cout << "Indices: ";
172 for (const int i : indices_) {
173 std::cout << i << " ";
174 }
175 std::cout << std::endl;
176 }
177
179 bool equals(const NonlinearFactor& expected, double tol=1e-9) const override {
180 const This *e = dynamic_cast<const This*> (&expected);
181 return e != nullptr && Base::equals(*e, tol) &&
182 gtsam::equal_with_abs_tol(this->prior_, e->prior_, tol) &&
183 this->indices_ == e->indices_;
184 }
185
187
189 Vector evaluateError(const T& p, OptionalMatrixType H) const override {
190 return internalFactor_.evaluateError(p, H);
191 }
192
193 // access
194 const Vector& prior() const { return prior_; }
195 const std::vector<size_t>& indices() const { return indices_; }
196
197 private:
198#if GTSAM_ENABLE_BOOST_SERIALIZATION
200 friend class boost::serialization::access;
201 template <class ARCHIVE>
202 void save(ARCHIVE& ar, const unsigned int /*version*/) const {
203 // NoiseModelFactor1 instead of NoiseModelFactorN for backward compatibility
204 ar & boost::serialization::make_nvp("NoiseModelFactor1",
205 boost::serialization::base_object<Base>(*this));
206 ar & BOOST_SERIALIZATION_NVP(prior_);
207 ar & BOOST_SERIALIZATION_NVP(indices_);
208 }
209
210 template <class ARCHIVE>
211 void load(ARCHIVE& ar, const unsigned int /*version*/) {
212 // NoiseModelFactor1 instead of NoiseModelFactorN for backward compatibility
213 ar & boost::serialization::make_nvp("NoiseModelFactor1",
214 boost::serialization::base_object<Base>(*this));
215 ar & BOOST_SERIALIZATION_NVP(prior_);
216 ar & BOOST_SERIALIZATION_NVP(indices_);
217 initializeInternalFactor();
218 }
219
220 BOOST_SERIALIZATION_SPLIT_MEMBER()
221#endif
222 }; // \class PartialPriorFactor
223
224}
225
226#endif // GTSAM_ALLOW_DEPRECATED_SINCE_V43
Base class and basic functions for Lie types.
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
void save(const Matrix &A, const string &s, const string &filename)
save a matrix to file, which can be loaded by matlab
Definition Matrix.cpp:154
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
Template to create a binary predicate.
Definition Testable.h:112
Nonlinear factor base class.
Definition NonlinearFactor.h:70