31 #include "EXTERNAL/ceres/autodiff.h" 38 using namespace Eigen;
46 bool operator()(
const T* rotMatSerialized, T* quaternion)
const 48 typename Eigen::Matrix<T, 3, 3, Eigen::ColMajor>::ConstMapType R(rotMatSerialized);
56 quaternion[0] = (R(2,1) - R(1,2)) * t;
57 quaternion[1] = (R(0,2) - R(2,0)) * t;
58 quaternion[2] = (R(1,0) - R(0,1)) * t;
69 t = sqrt(R(i,i) - R(j,j) - R(k,k) + T(1.0));
70 quaternion[i] = T(0.5) * t;
72 quaternion[j] = (R(j,i) + R(i,j)) * t;
73 quaternion[k] = (R(k,i) + R(i,k)) * t;
74 T w = (R(k,j) - R(j,k)) * t;
78 for (
int l = 0; l < 3; ++l)
79 quaternion[l] *= T(-1);
88 for (
int k = 0; k < 100000; ++k) {
92 Vector3D rotAxisAngle = Vector3D::Random();
93 rotAxisAngle += Vector3D::Random();
94 Eigen::AngleAxisd rotation(rotAxisAngle.norm(), rotAxisAngle.normalized());
95 Matrix3D Re = rotation.toRotationMatrix();
98 Matrix<double, 3, 9, Eigen::ColMajor> dq_dR;
100 Re(0,0),Re(1,0),Re(2,0),
101 Re(0,1),Re(1,1),Re(2,1),
102 Re(0,2),Re(1,2),Re(2,2));
105 Matrix<double, 3, 9, Eigen::RowMajor> dq_dR_AD;
106 typedef ceres::internal::AutoDiff<RotationMatrix2QuaternionManifold, double, 9> AutoDiff_Dq_DR;
107 double *parameters[] = { Re.data() };
108 double *jacobians[] = { dq_dR_AD.data() };
111 AutoDiff_Dq_DR::Differentiate(rot2quat, parameters, 3, value, jacobians);
114 const double allowedDifference = 1e-6;
115 for (
int i = 0; i < dq_dR.rows(); ++i) {
116 for (
int j = 0; j < dq_dR.cols(); ++j) {
117 double d = fabs(dq_dR_AD(i,j) - dq_dR(i,j));
118 if (d > allowedDifference) {
119 cerr <<
"\ndetected difference in the Jacobians" << endl;
120 cerr << PVAR(Re) << endl << endl;
121 cerr << PVAR(dq_dR_AD) << endl << endl;
122 cerr << PVAR(dq_dR) << endl << endl;
Eigen::Matrix< double, 3, 1, Eigen::ColMajor > Vector3D
void compute_dq_dR(Eigen::Matrix< double, 3, 9, Eigen::ColMajor > &dq_dR, const double &r11, const double &r21, const double &r31, const double &r12, const double &r22, const double &r32, const double &r13, const double &r23, const double &r33)
Eigen::Matrix< double, 3, 3, Eigen::ColMajor > Matrix3D
Functor used to compute the Jacobian via AD.
bool operator()(const T *rotMatSerialized, T *quaternion) const