gtsam
Loading...
Searching...
No Matches
RotateFactor.h
1/*
2 * @file RotateFactor.cpp
3 * @brief RotateFactor class
4 * @author Frank Dellaert
5 * @date December 17, 2013
6 */
7
8#pragma once
9
12#include <gtsam/geometry/Rot3.h>
13
14namespace gtsam {
15
24class RotateFactor: public NoiseModelFactorN<Rot3> {
25
26 Point3 p_, z_;
27
28 typedef NoiseModelFactorN<Rot3> Base;
29 typedef RotateFactor This;
30
31public:
32
33 // Provide access to the Matrix& version of evaluateError:
35
37 RotateFactor(Key key, const Rot3& P, const Rot3& Z,
38 const SharedNoiseModel& model) :
39 Base(model, key), p_(Rot3::Logmap(P)), z_(Rot3::Logmap(Z)) {
40 }
41
43 gtsam::NonlinearFactor::shared_ptr clone() const override {
44 return std::static_pointer_cast<gtsam::NonlinearFactor>(
45 gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
46
48 void print(const std::string& s = "",
49 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
50 Base::print(s);
51 std::cout << "RotateFactor:]\n";
52 std::cout << "p: " << p_.transpose() << std::endl;
53 std::cout << "z: " << z_.transpose() << std::endl;
54 }
55
57 Vector evaluateError(const Rot3& R, OptionalMatrixType H) const override {
58 // predict p_ as q = R*z_, derivative H will be filled if not none
59 Point3 q = R.rotate(z_,H);
60 // error is just difference, and note derivative of that wrpt q is I3
61 return Vector{{q.x() - p_.x(), q.y() - p_.y(), q.z() - p_.z()}};
62 }
63
64};
65
71
72 Unit3 i_p_, c_z_;
73
74 typedef NoiseModelFactorN<Rot3> Base;
75 typedef RotateDirectionsFactor This;
76
77public:
78
79 // Provide access to the Matrix& version of evaluateError:
81
83 RotateDirectionsFactor(Key key, const Unit3& i_p, const Unit3& c_z,
84 const SharedNoiseModel& model) :
85 Base(model, key), i_p_(i_p), c_z_(c_z) {
86 }
87
89 static Rot3 Initialize(const Unit3& i_p, const Unit3& c_z) {
90 gtsam::Quaternion iRc;
91 // setFromTwoVectors sets iRc to (a) quaternion which transform c_z into i_p
92 iRc.setFromTwoVectors(c_z.unitVector(), i_p.unitVector());
93 return Rot3(iRc);
94 }
95
97 gtsam::NonlinearFactor::shared_ptr clone() const override {
98 return std::static_pointer_cast<gtsam::NonlinearFactor>(
99 gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
100
102 void print(const std::string& s = "",
103 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
104 Base::print(s);
105 std::cout << "RotateDirectionsFactor:" << std::endl;
106 i_p_.print("p");
107 c_z_.print("z");
108 }
109
111 Vector evaluateError(const Rot3& iRc, OptionalMatrixType H) const override {
112 Unit3 i_q = iRc * c_z_;
113 Vector error = i_p_.errorVector(i_q, {}, H);
114 if (H) {
115 Matrix DR;
116 iRc.rotate(c_z_, DR);
117 *H = (*H) * DR;
118 }
119 return error;
120 }
121};
122} // namespace gtsam
123
3D rotation represented as a rotation matrix or quaternion
Base class for noise model factors with N variables.
Non-linear factor base classes.
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
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
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
@ Logmap
Use the SE_2(3) NavState Logmap for every backend.
Definition PreintegrationParams.h:32
noiseModel::Base::shared_ptr SharedNoiseModel
Aliases.
Definition NoiseModel.h:846
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
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
Vector3 unitVector(OptionalJacobian< 3, 2 > H={}) const
Return unit-norm Vector.
Definition Unit3.cpp:151
virtual void print(const std::string &s="Factor", const KeyFormatter &formatter=DefaultKeyFormatter) const
print
Definition Factor.cpp:29
virtual Vector evaluateError(const ValueTypes &... x, OptionalMatrixTypeT< ValueTypes >... H) const=0
Key key() const
Definition NoiseModelFactorN.h:307
double error(const Values &c) const override
Calculate the error of the factor.
Definition NonlinearFactor.cpp:146
RotateFactor(Key key, const Rot3 &P, const Rot3 &Z, const SharedNoiseModel &model)
Constructor.
Definition RotateFactor.h:37
Vector evaluateError(const Rot3 &R, OptionalMatrixType H) const override
vector of errors returns 2D vector
Definition RotateFactor.h:57
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition RotateFactor.h:48
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition RotateFactor.h:43
Vector evaluateError(const Rot3 &iRc, OptionalMatrixType H) const override
vector of errors returns 2D vector
Definition RotateFactor.h:111
RotateDirectionsFactor(Key key, const Unit3 &i_p, const Unit3 &c_z, const SharedNoiseModel &model)
Constructor.
Definition RotateFactor.h:83
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition RotateFactor.h:102
static Rot3 Initialize(const Unit3 &i_p, const Unit3 &c_z)
Initialize rotation iRc such that i_p = iRc * c_z.
Definition RotateFactor.h:89
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition RotateFactor.h:97