26#include <gtsam/base/concepts.h>
27#include <gtsam/config.h>
30#include <gtsam/geometry/Unit3.h>
37#ifndef ROT3_DEFAULT_COORDINATES_MODE
38 #ifdef GTSAM_USE_QUATERNIONS
40 #define ROT3_DEFAULT_COORDINATES_MODE Rot3::EXPMAP
43 #ifndef GTSAM_ROT3_EXPMAP
45 #define ROT3_DEFAULT_COORDINATES_MODE Rot3::CAYLEY
47 #define ROT3_DEFAULT_COORDINATES_MODE Rot3::EXPMAP
67 static constexpr size_t MatrixM = 3;
70#ifdef GTSAM_USE_QUATERNIONS
72 gtsam::Quaternion quaternion_;
93 Rot3(
double R11,
double R12,
double R13,
94 double R21,
double R22,
double R23,
95 double R31,
double R32,
double R33);
104 template <
typename Derived>
105#ifdef GTSAM_USE_QUATERNIONS
106 explicit Rot3(
const Eigen::MatrixBase<Derived>& R) {
107 quaternion_ = Matrix3(R);
110 explicit Rot3(
const Eigen::MatrixBase<Derived>& R) : rot_(R) {
118#ifdef GTSAM_USE_QUATERNIONS
119 explicit Rot3(
const Matrix3& R) : quaternion_(R) {}
121 explicit Rot3(
const Matrix3& R) : rot_(R) {}
127#ifdef GTSAM_USE_QUATERNIONS
128 explicit Rot3(
const SO3& R) : quaternion_(R.matrix()) {}
130 explicit Rot3(
const SO3& R) : rot_(R) {}
137 Rot3(
const Quaternion& q);
138 Rot3(
double w,
double x,
double y,
double z) :
Rot3(Quaternion(w, x, y, z)) {}
146 static Rot3 Random(std::mt19937 & rng);
152 static bool IsValid(
const Matrix3& R,
double tol = 1e-9);
157 static Rot3 Rx(
double t);
160 static Rot3 Ry(
double t);
163 static Rot3 Rz(
double t);
166 static Rot3 RzRyRx(
double x,
double y,
double z,
168 OptionalJacobian<3, 1> Hy = {},
169 OptionalJacobian<3, 1> Hz = {});
175 if (xyz.size() != 3) {
182 out = RzRyRx(xyz(0), xyz(1), xyz(2), Hx, Hy, Hz);
185 out = RzRyRx(xyz(0), xyz(1), xyz(2));
215 OptionalJacobian<3, 1> Hr = {}) {
216 return RzRyRx(r, p, y, Hr, Hp, Hy);
221 gtsam::Quaternion q(w, x, y, z);
234#ifdef GTSAM_USE_QUATERNIONS
235 return gtsam::Quaternion(Eigen::AngleAxis<double>(angle, unitAxis));
299 Rot3 normalized()
const;
306 void print(
const std::string& s=
"")
const;
309 bool equals(
const Rot3& p,
double tol = 1e-9)
const;
325#ifdef GTSAM_USE_QUATERNIONS
326 return Rot3(quaternion_.inverse());
328 return Rot3(rot_.matrix().transpose());
339 return cRb * (*this) * cRb.
inverse();
357#ifndef GTSAM_USE_QUATERNIONS
362#ifndef GTSAM_USE_QUATERNIONS
372 return compose(CayleyChart::Retract(omega));
377 return CayleyChart::Local(between(other));
386 using LieAlgebra = Matrix3;
398 static Vector3 Logmap(
const Rot3& R, OptionalJacobian<3,3> H = {});
401 static Matrix3 ExpmapDerivative(
const Vector3& x);
404 static Matrix3 LogmapDerivative(
const Vector3& x);
414 static Rot3 Retract(
const Vector3& v, ChartJacobian H = {});
415 static Vector3 Local(
const Rot3& r, ChartJacobian H = {});
421 static inline Matrix3
Hat(
const Vector3& xi) {
return SO3::Hat(xi); }
424 static inline Vector3
Vee(
const Matrix3& X) {
return SO3::Vee(X); }
434 OptionalJacobian<3,3> H2 = {})
const;
440 Point3 unrotate(
const Point3& p, OptionalJacobian<3,3> H1 = {},
441 OptionalJacobian<3,3> H2={})
const;
448 Unit3
rotate(
const Unit3& p, OptionalJacobian<2,3> HR = {},
449 OptionalJacobian<2,2> Hp = {})
const;
452 Unit3 unrotate(
const Unit3& p, OptionalJacobian<2,3> HR = {},
453 OptionalJacobian<2,2> Hp = {})
const;
463 Matrix3 matrix()
const;
468 Matrix3 transpose()
const;
478 Vector3 xyz(OptionalJacobian<3, 3> H = {})
const;
484 Vector3 ypr(OptionalJacobian<3, 3> H = {})
const;
490 Vector3 rpy(OptionalJacobian<3, 3> H = {})
const;
498 double roll(OptionalJacobian<1, 3> H = {})
const;
506 double pitch(OptionalJacobian<1, 3> H = {})
const;
514 double yaw(OptionalJacobian<1, 3> H = {})
const;
528 std::pair<Unit3, double> axisAngle()
const;
533 gtsam::Quaternion toQuaternion()
const;
540 Rot3 slerp(
double t,
const Rot3& other)
const;
546 GTSAM_EXPORT
friend std::ostream &operator<<(std::ostream &os,
const Rot3& p);
552#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
554 Point3 column(
int index)
const;
560#if GTSAM_ENABLE_BOOST_SERIALIZATION
562 friend class boost::serialization::access;
563 template <
class ARCHIVE>
564 void serialize(ARCHIVE& ar,
const unsigned int ) {
565#ifndef GTSAM_USE_QUATERNIONS
567 ar& boost::serialization::make_nvp(
"rot11", M(0, 0));
568 ar& boost::serialization::make_nvp(
"rot12", M(0, 1));
569 ar& boost::serialization::make_nvp(
"rot13", M(0, 2));
570 ar& boost::serialization::make_nvp(
"rot21", M(1, 0));
571 ar& boost::serialization::make_nvp(
"rot22", M(1, 1));
572 ar& boost::serialization::make_nvp(
"rot23", M(1, 2));
573 ar& boost::serialization::make_nvp(
"rot31", M(2, 0));
574 ar& boost::serialization::make_nvp(
"rot32", M(2, 1));
575 ar& boost::serialization::make_nvp(
"rot33", M(2, 2));
577 ar& boost::serialization::make_nvp(
"w", quaternion_.w());
578 ar& boost::serialization::make_nvp(
"x", quaternion_.x());
579 ar& boost::serialization::make_nvp(
"y", quaternion_.y());
580 ar& boost::serialization::make_nvp(
"z", quaternion_.z());
587 using Rot3Vector = std::vector<Rot3, Eigen::aligned_allocator<Rot3>>;
599 GTSAM_EXPORT std::pair<Matrix3, Vector3>
RQ(
617 if constexpr (D == 1) {
618 const Matrix3 R = value.
matrix();
621 X.bottomRows(9) = Eigen::Map<const Matrix>(R.data(), 9, 1);
623 }
else if constexpr (D >= 3) {
624 Matrix X = Matrix::Zero(3, D);
625 X.template leftCols<3>() = value.
matrix().transpose();
628 throw std::invalid_argument(
629 "traits<Rot3>::QcqpValue only supports D=1 and D>=3.");
642 if constexpr (D == 1) {
645 std::vector<std::pair<Matrix, double>> constraints;
646 constraints.reserve(10);
648 Matrix A = Matrix::Zero(10, 10);
652 constraints.emplace_back(A, 1.0);
662 constraints.emplace_back(A, 0.0);
671 constraints.emplace_back(A, 0.0);
680 constraints.emplace_back(A, 0.0);
687 constraints.emplace_back(A, 1.0);
696 constraints.emplace_back(A, 0.0);
705 constraints.emplace_back(A, 0.0);
711 constraints.emplace_back(A, 1.0);
720 constraints.emplace_back(A, 0.0);
726 constraints.emplace_back(A, 1.0);
729 }
else if constexpr (D >= 3) {
730 std::vector<std::pair<Matrix, double>> constraints;
731 constraints.reserve(6);
734 for (
int r = 0; r < 3; ++r) {
735 Matrix A = Matrix::Zero(3, 3);
737 constraints.emplace_back(A, 1.0);
741 for (
int r1 = 0; r1 < 3; ++r1) {
742 for (
int r2 = r1 + 1; r2 < 3; ++r2) {
743 Matrix A = Matrix::Zero(3, 3);
746 constraints.emplace_back(A, 0.0);
751 throw std::invalid_argument(
752 "traits<Rot3>::QcqpConstraints only supports D=1 and D>=3.");
765 if constexpr (D == 1) {
767 std::abs(X(0, 0)) < 1e-9) {
768 throw std::invalid_argument(
769 "traits<Rot3>::FromQcqpValue requires a 10-by-1 vector with a "
770 "nonzero homogenization entry.");
772 const Vector x = X.col(0) / X(0, 0);
774 R.col(0) = x.segment<3>(1);
775 R.col(1) = x.segment<3>(4);
776 R.col(2) = x.segment<3>(7);
779 static_assert(D >= 3,
780 "traits<Rot3>::FromQcqpValue requires D >= 3.");
781 if (X.rows() != 3 || X.cols() != D) {
782 throw std::invalid_argument(
783 "traits<Rot3>::FromQcqpValue requires a 3-by-D matrix.");
805 static constexpr bool available =
true;
806 static constexpr bool expmapAvailable =
true;
816 const Vector3& omega,
const Vector3& v,
819#ifdef GTSAM_USE_QUATERNIONS
822 const Rot3 rotation(local.expmap());
824 const Vector3 transported =
825 local.tangentExpmap(v, rotation.matrix(), derivative);
826 return {rotation, transported};
829 static Matrix6 rightJacobian(
const Vector3& omega,
const Vector3& v) {
831 expmap(omega, v, derivative);
P rotate(const T &r, const P &pt)
rotation functions
Definition lieProxies.h:47
Lie Group wrapper for Eigen Quaternions.
3*3 matrix representation of SO(3)
Global functions in a separate testing namespace.
Definition chartTesting.h:28
Expression< T > expmap(const Expression< T > &origin, const Expression< typename traits< T >::TangentVector > &tangent)
Apply an exponential-map increment to a Lie-group expression.
Definition expressions.h:43
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
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition Point2.h:52
std::vector< Rot3, Eigen::aligned_allocator< Rot3 > > Rot3Vector
std::vector of Rot3s, used in Matlab wrapper
Definition Rot3.h:587
pair< Matrix3, Vector3 > RQ(const Matrix3 &A, OptionalJacobian< 3, 9 > H)
[RQ] receives a 3 by 3 matrix and returns an upper triangular matrix R and 3 rotation angles correspo...
Definition Rot3.cpp:255
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 CRTP helper class that implements Lie group methods Prerequisites: methods operator*,...
Definition Lie.h:114
A CRTP helper class that implements matrix Lie group methods.
Definition MatrixLieGroup.h:51
Both LieGroupTraits and Testable.
Definition MatrixLieGroup.h:350
OptionalJacobian is an Eigen::Ref like class that can take be constructed using either a fixed size o...
Definition OptionalJacobian.h:40
Optional closed-form kernels used by TangentLieGroup::Expmap() and its private rightJacobian() helper...
Definition TangentLieGroup.h:51
Template to create a binary predicate.
Definition Testable.h:112
Rot3 is a 3D rotation represented as a rotation matrix if the preprocessor symbol GTSAM_USE_QUATERNIO...
Definition Rot3.h:65
static Rot3 Identity()
identity rotation for group operation
Definition Rot3.h:316
Vector9 vec(OptionalJacobian< 9, 3 > H={}) const
Vee maps from Lie algebra to tangent vector.
Definition Rot3.h:543
static Vector3 Vee(const Matrix3 &X)
Vee maps from Lie algebra to tangent vector.
Definition Rot3.h:424
static Rot3 Roll(double t)
Positive roll is to right (increasing yaw in aircraft).
Definition Rot3.h:196
Rot3 retractCayley(const Vector &omega) const
Retraction from R^3 to Rot3 manifold using the Cayley transform.
Definition Rot3.h:371
Matrix3 AdjointMap() const
Calculate Adjoint map.
Definition Rot3.h:407
static Rot3 ClosestTo(const Matrix3 &M)
Static, named constructor that finds Rot3 element closest to M in Frobenius norm.
Definition Rot3.h:287
virtual ~Rot3()
Virtual destructor.
Definition Rot3.h:149
static Rot3 Yaw(double t)
Positive yaw is to right (as in aircraft heading). See ypr.
Definition Rot3.h:190
static Rot3 Expmap(const Vector3 &v, OptionalJacobian< 3, 3 > H={})
Exponential map - create a rotation from canonical coordinates using Rodrigues' formula.
Definition Rot3M.cpp:173
static Rot3 AxisAngle(const Unit3 &axis, double angle)
Convert from axis/angle representation.
Definition Rot3.h:247
static Matrix3 Hat(const Vector3 &xi)
Hat maps from tangent vector to Lie algebra.
Definition Rot3.h:421
Rot3(const Matrix3 &R)
Constructor from a rotation matrix Overload version for Matrix3 to avoid casting in quaternion mode.
Definition Rot3.h:121
static Matrix3 adjointMap(const Vector3 &xi)
Matrix representation of the Lie-algebra adjoint operator ad_xi on so(3).
Definition Rot3.h:410
static Rot3 Rodrigues(const Vector3 &w)
Rodrigues' formula to compute an incremental rotation.
Definition Rot3.h:256
Rot3()
default constructor, unit rotation
Definition Rot3M.cpp:52
static Rot3 RzRyRx(const Vector &xyz, OptionalJacobian< 3, 3 > H={})
Rotations around Z, Y, then X axes as in http://en.wikipedia.org/wiki/Rotation_matrix,...
Definition Rot3.h:172
static Rot3 Ry(double t)
Rotation around Y axis as in http://en.wikipedia.org/wiki/Rotation_matrix, counterclockwise when look...
Definition Rot3M.cpp:82
static Rot3 AxisAngle(const Point3 &axis, double angle)
Convert from axis/angle representation.
Definition Rot3.h:231
Vector3 xyz(OptionalJacobian< 3, 3 > H={}) const
Use RQ to calculate xyz angle representation.
Definition Rot3.cpp:169
static Rot3 Pitch(double t)
Positive pitch is up (increasing aircraft altitude).See ypr.
Definition Rot3.h:193
Vector3 localCayley(const Rot3 &other) const
Inverse of retractCayley.
Definition Rot3.h:376
static Rot3 Quaternion(double w, double x, double y, double z)
Create from Quaternion coefficients.
Definition Rot3.h:220
static Rot3 Rx(double t)
Rotation around X axis as in http://en.wikipedia.org/wiki/Rotation_matrix, counterclockwise when look...
Definition Rot3M.cpp:73
Rot3 conjugate(const Rot3 &cRb) const
Conjugation: given a rotation acting in frame B, compute rotation c1Rc2 acting in a frame C.
Definition Rot3.h:337
static Rot3 Rodrigues(double wx, double wy, double wz)
Rodrigues' formula to compute an incremental rotation.
Definition Rot3.h:267
Rot3 inverse() const
inverse of a rotation
Definition Rot3.h:324
Rot3(const SO3 &R)
Constructor from an SO3 instance.
Definition Rot3.h:130
static Rot3 Ypr(double y, double p, double r, OptionalJacobian< 3, 1 > Hy={}, OptionalJacobian< 3, 1 > Hp={}, OptionalJacobian< 3, 1 > Hr={})
Returns rotation nRb from body to nav frame.
Definition Rot3.h:212
static Rot3 Rz(double t)
Rotation around Z axis as in http://en.wikipedia.org/wiki/Rotation_matrix, counterclockwise when look...
Definition Rot3M.cpp:91
CoordinatesMode
The method retract() is used to map from the tangent space back to the manifold.
Definition Rot3.h:355
@ CAYLEY
Retract and localCoordinates using the Cayley transform.
Definition Rot3.h:358
@ EXPMAP
Use the Lie group exponential map to retract.
Definition Rot3.h:356
Rot3(const Eigen::MatrixBase< Derived > &R)
Constructor from a rotation matrix Version for generic matrices.
Definition Rot3.h:110
Matrix3 matrix() const
return 3*3 rotation matrix
Definition Rot3M.cpp:261
static constexpr int QcqpVectorDim
Dimension of the D=1 homogenized QCQP vector.
Definition Rot3.h:605
static std::vector< std::pair< Matrix, double > > QcqpConstraints()
Return row-space QCQP equality constraints A, b such that trace(X' A X) = b.
Definition Rot3.h:641
static Matrix QcqpValue(const Rot3 &value)
Return a matrix-valued QCQP variable for Rot3.
Definition Rot3.h:616
static Rot3 FromQcqpValue(const Matrix &X)
Project a D=1 vector or canonical 3-by-D lift back to Rot3.
Definition Rot3.h:764
static std::pair< Rot3, Vector3 > expmap(const Vector3 &omega, const Vector3 &v, OptionalJacobian< 6, 6 > derivative={})
Evaluate the complete TSO(3) exponential from one SO(3) kernel.
Definition Rot3.h:815
Functor that implements Exponential map and its derivatives Math extends Ethan theme of elegant I + a...
Definition SO3.h:184
static TangentVector Vee(const MatrixNN &X)
MatrixNN matrix_
Rotation matrix.
Definition SOn.h:66
static SO AxisAngle(const Vector3 &axis, double theta)
Definition SO3.cpp:254
static MatrixNN Hat(const TangentVector &xi)
static SO ClosestTo(const MatrixNN &M)
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