g2o
Classes | Functions
test_mat2quat_jacobian.cpp File Reference
#include <iostream>
#include "dquat2mat.h"
#include "isometry3d_mappings.h"
#include "g2o/stuff/macros.h"
#include "EXTERNAL/ceres/autodiff.h"
#include <cstdio>
Include dependency graph for test_mat2quat_jacobian.cpp:

Go to the source code of this file.

Classes

struct  RotationMatrix2QuaternionManifold
 Functor used to compute the Jacobian via AD. More...
 

Functions

int main (int, char **)
 

Function Documentation

int main ( int  ,
char **   
)

Definition at line 86 of file test_mat2quat_jacobian.cpp.

References g2o::internal::compute_dq_dR().

87 {
88  for (int k = 0; k < 100000; ++k) {
89 
90  // create a random rotation matrix by sampling a random 3d vector
91  // that will be used in axis-angle representation to create the matrix
92  Vector3D rotAxisAngle = Vector3D::Random();
93  rotAxisAngle += Vector3D::Random();
94  Eigen::AngleAxisd rotation(rotAxisAngle.norm(), rotAxisAngle.normalized());
95  Matrix3D Re = rotation.toRotationMatrix();
96 
97  // our analytic function which we want to evaluate
98  Matrix<double, 3, 9, Eigen::ColMajor> dq_dR;
99  compute_dq_dR (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));
103 
104  // compute the Jacobian using AD
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() };
109  double value[3];
111  AutoDiff_Dq_DR::Differentiate(rot2quat, parameters, 3, value, jacobians);
112 
113  // compare the two 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;
123  return 1;
124  }
125  }
126  }
127  cerr << "+";
128 
129  }
130  return 0;
131 }
Eigen::Matrix< double, 3, 1, Eigen::ColMajor > Vector3D
Definition: eigen_types.h:46
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)
Definition: dquat2mat.cpp:42
Eigen::Matrix< double, 3, 3, Eigen::ColMajor > Matrix3D
Definition: eigen_types.h:61
Functor used to compute the Jacobian via AD.