gtsam
Loading...
Searching...
No Matches
AttitudeFactor.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#pragma once
19
21#include <gtsam/geometry/Gal3.h>
23#include <gtsam/geometry/Unit3.h>
26
27#include <type_traits>
28
29namespace gtsam {
30
31namespace detail {
32
33// Type trait to detect ExtendedPose3 instantiations
34template <typename T>
35struct is_extended_pose3 : std::false_type {};
36
37template <int K, class Derived>
38struct is_extended_pose3<gtsam::ExtendedPose3<K, Derived>> : std::true_type {};
39
40template <typename T>
41constexpr bool is_extended_pose3_v = is_extended_pose3<T>::value;
42template <class VALUE>
43inline std::string attitudeFactorName() {
44 if constexpr (std::is_same_v<VALUE, Rot3>) {
45 return "AttitudeFactorRot3";
46 } else if constexpr (std::is_same_v<VALUE, Pose3>) {
47 return "AttitudeFactorPose3";
48 } else if constexpr (std::is_same_v<VALUE, NavState>) {
49 return "AttitudeFactorNavState";
50 } else if constexpr (std::is_same_v<VALUE, Gal3>) {
51 return "AttitudeFactorGal3";
52 } else if constexpr (std::is_same_v<VALUE, Se23>) {
53 return "AttitudeFactorSe23";
54 } else if constexpr (is_extended_pose3_v<VALUE>) {
55 constexpr int K = std::remove_reference_t<VALUE>::K;
56 const std::string valueName = (K == Eigen::Dynamic)
57 ? "ExtendedPose3d"
58 : "ExtendedPose3" + std::to_string(K);
59 return "AttitudeFactor" + valueName;
60 } else {
61 return "AttitudeFactor";
62 }
63}
64
65} // namespace detail
66
82template <class VALUE>
83class GTSAM_EXPORT AttitudeFactor : public NoiseModelFactorN<VALUE> {
84 public:
85 typedef AttitudeFactor<VALUE> This;
86 typedef NoiseModelFactorN<VALUE> Base;
87
89
91 typedef std::shared_ptr<This> shared_ptr;
92
93 protected:
94 Unit3 nRef_, bMeasured_;
95
96 public:
99
108 AttitudeFactor(Key key, const Unit3& nRef, const SharedNoiseModel& model,
109 const Unit3& bMeasured = Unit3(0, 0, 1))
110 : Base(model, key), nRef_(nRef), bMeasured_(bMeasured) {}
111
112 ~AttitudeFactor() override {}
113
115 Vector attitudeError(const Rot3& nRb, OptionalJacobian<2, 3> H = {}) const {
116 if (H) {
117 Matrix23 D_nMeasured_R;
118 const Unit3 nMeasured = nRb.rotate(bMeasured_, D_nMeasured_R);
119 Matrix22 D_e_nMeasured;
120 const Vector error = nRef_.errorVector(nMeasured, {}, D_e_nMeasured);
121 *H = D_e_nMeasured * D_nMeasured_R;
122 return error;
123 } else {
124 return nRef_.errorVector(nRb * bMeasured_);
125 }
126 }
127
128 const Unit3& nRef() const { return nRef_; }
129 const Unit3& bMeasured() const { return bMeasured_; }
130
131#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
132 [[deprecated("Use nRef() instead")]]
133 const Unit3& nZ() const {
134 return nRef_;
135 }
136
137 [[deprecated("Use bMeasured() instead")]]
138 const Unit3& bRef() const {
139 return bMeasured_;
140 }
141#endif
142
144 gtsam::NonlinearFactor::shared_ptr clone() const override {
145 return std::static_pointer_cast<gtsam::NonlinearFactor>(
146 gtsam::NonlinearFactor::shared_ptr(new This(*this)));
147 }
148
150 void print(
151 const std::string& s = "",
152 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
153 std::cout << (s.empty() ? "" : s + " ")
154 << detail::attitudeFactorName<VALUE>() << " on "
155 << keyFormatter(this->key()) << "\n";
156 nRef_.print(" reference direction in nav frame: ");
157 bMeasured_.print(" measured direction in body frame: ");
158 this->noiseModel_->print(" noise model: ");
159 }
160
162 bool equals(const NonlinearFactor& expected,
163 double tol = 1e-9) const override {
164 const This* e = dynamic_cast<const This*>(&expected);
165 return e != nullptr && Base::equals(*e, tol) &&
166 nRef_.equals(e->nRef_, tol) && bMeasured_.equals(e->bMeasured_, tol);
167 }
168
170 Vector evaluateError(const VALUE& value,
171 OptionalMatrixType H) const override {
172 if constexpr (std::is_same_v<VALUE, Rot3>) {
173 return attitudeError(value, H);
174 } else {
175 if (H) {
176 Matrix H_rotation_value;
177 const Rot3 nRb = value.rotation(H_rotation_value);
178 Matrix23 H_error_rotation;
179 const Vector error = attitudeError(nRb, H_error_rotation);
180 *H = H_error_rotation * H_rotation_value;
181 return error;
182 } else {
183 return attitudeError(value.rotation());
184 }
185 }
186 }
187
188 private:
189#if GTSAM_ENABLE_BOOST_SERIALIZATION
191 friend class boost::serialization::access;
192 template <class ARCHIVE>
193 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
194 ar& boost::serialization::make_nvp(
195 "NoiseModelFactor1", boost::serialization::base_object<Base>(*this));
196 ar& boost::serialization::make_nvp("nRef_", nRef_);
197 ar& boost::serialization::make_nvp("bMeasured_", bMeasured_);
198 }
199#endif
200};
201
202template <class VALUE>
203struct traits<AttitudeFactor<VALUE>> : public Testable<AttitudeFactor<VALUE>> {
204};
205
206} // namespace gtsam
3D Galilean Group SGal(3) state (attitude, position, velocity, time)
Extended pose Lie group SE_k(3), with static or dynamic k.
3D Pose manifold SO(3) x R^3 and group SE(3)
Navigation state composing of attitude, position, and velocity.
Base class for noise model factors with N variables.
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
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
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
OptionalJacobian is an Eigen::Ref like class that can take be constructed using either a fixed size o...
Definition OptionalJacobian.h:40
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
Lie group SE_k(3): semidirect product of SO(3) with k copies of R^3.
Definition ExtendedPose3.h:52
Rot3 is a 3D rotation represented as a rotation matrix if the preprocessor symbol GTSAM_USE_QUATERNIO...
Definition Rot3.h:65
Point3 rotate(const Point3 &p, OptionalJacobian< 3, 3 > H1={}, OptionalJacobian< 3, 3 > H2={}) const
rotate point from rotated coordinate frame to world
Definition Rot3M.cpp:165
Represents a 3D point on a unit sphere.
Definition Unit3.h:44
Vector2 errorVector(const Unit3 &q, OptionalJacobian< 2, 2 > H_p={}, OptionalJacobian< 2, 2 > H_q={}) const
Signed, vector-valued error between two directions NOTE(hayk): This method has zero derivatives if th...
Definition Unit3.cpp:242
bool equals(const This &other, double tol=1e-9) const
check equality
Definition Factor.cpp:42
Definition AttitudeFactor.h:35
Unary factor that constrains the rotation component of a value.
Definition AttitudeFactor.h:83
Vector attitudeError(const Rot3 &nRb, OptionalJacobian< 2, 3 > H={}) const
vector of errors
Definition AttitudeFactor.h:115
AttitudeFactor()
default constructor - only use for serialization
Definition AttitudeFactor.h:98
AttitudeFactor(Key key, const Unit3 &nRef, const SharedNoiseModel &model, const Unit3 &bMeasured=Unit3(0, 0, 1))
Constructor.
Definition AttitudeFactor.h:108
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition AttitudeFactor.h:144
Vector evaluateError(const VALUE &value, OptionalMatrixType H) const override
vector of errors
Definition AttitudeFactor.h:170
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition AttitudeFactor.h:150
std::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
Definition AttitudeFactor.h:91
bool equals(const NonlinearFactor &expected, double tol=1e-9) const override
equals
Definition AttitudeFactor.h:162
virtual Vector evaluateError(const ValueTypes &... x, OptionalMatrixTypeT< ValueTypes >... H) const=0
Key key() const
Definition NoiseModelFactorN.h:307
Nonlinear factor base class.
Definition NonlinearFactor.h:70
double error(const Values &c) const override
Calculate the error of the factor.
Definition NonlinearFactor.cpp:146