42 #include <glog/logging.h>
50 double c1 = std::cos(phi);
51 double c2 = std::cos(theta);
52 double c3 = std::cos(psi);
53 double s1 = std::sin(phi);
54 double s2 = std::sin(theta);
55 double s3 = std::sin(psi);
59 R(0, 1) = c3*s1*s2 - c1*s3;
60 R(0, 2) = s1*s3 + c1*c3*s2;
62 R(1, 1) = c1*c3 + s1*s2*s3;
63 R(1, 2) = c1*s2*s3 - c3*s1;
83 if (std::abs(R.determinant() - 1) > 1e-4) {
84 LOG(WARNING) <<
"R does not have a determinant of 1.";
85 return Vector3d::Zero();
88 double theta = -std::asin(R(2, 0));
90 if (std::abs(cos(theta)) < 1e-8) {
91 LOG(WARNING) <<
"Theta is approximately +/- PI/2, which yields a "
92 "singularity. Cannot decompose matrix into Euler angles.";
93 return Vector3d(theta, 0.0, 0.0);
96 double phi = std::atan2(R(2, 1), R(2, 2));
97 double psi = std::atan2(R(1, 0) / std::cos(theta), R(0, 0) / std::cos(theta));
99 return Vector3d(phi, theta, psi);
104 double Roll(
const Matrix3d& R) {
105 double theta = -std::asin(R(2, 0));
106 if (std::abs(std::cos(theta)) < 1e-8)
108 return std::atan2(R(2, 1), R(2, 2));
114 return -std::asin(R(2, 0));
119 double Yaw(
const Matrix3d& R) {
120 double theta = -std::asin(R(2, 0));
121 if (std::abs(std::cos(theta)) < 1e-8)
123 return std::atan2(R(1, 0) / std::cos(theta), R(0, 0) / std::cos(theta));
128 angle = fmod(angle, 2.0 * M_PI);
136 angle = fmod(angle + M_PI, 2.0 * M_PI);
153 double D2R(
double angle) {
154 return angle * M_PI / 180.0;
158 double R2D(
double angle) {
159 return angle * 180.0 / M_PI;
163 double SO3Error(
const Matrix3d& R1,
const Matrix3d& R2) {
164 const Matrix3d R_error = R1.transpose()*R2 - R2.transpose()*R1;
168 const Vector3d R_vee(R_error(2,1), R_error(0,2), R_error(1,0));
169 return (0.5 * R_vee).norm();
double Normalize(double angle)
double Roll(const Matrix3d &R)
double S1Distance(double from, double to)
Vector3d MatrixToEulerAngles(const Matrix3d &R)
double Unroll(double angle)
double Pitch(const Matrix3d &R)
double Yaw(const Matrix3d &R)
double SO3Error(const Matrix3d &R1, const Matrix3d &R2)
Matrix3d EulerAnglesToMatrix(double phi, double theta, double psi)