如何控制Eigen Precision

时间:2018-04-23 11:22:35

标签: c++ eigen quaternions

我正在尝试使用此代码将Eigen 3x3旋转矩阵转换为四元数:

//m_labelMatrix : raw data of vtk4x4 matrix4d.
//m_transformationMatrix : Eigen4x4 matrix4d. 
m_transformationMatrix = Eigen::Map<Eigen::Matrix4d>(m_labelMatrix);        
m_transformationMatrix.transposeInPlace();
//m_affinedMatrix : affine3d matrix. 
m_affinedMatrix = m_transformationMatrix;
auto label_pos = m_affinedMatrix.translation();
auto rotationMatrix = m_affinedMatrix.linear();
auto scaleX = rotationMatrix.col(0).norm();
auto scaleY = rotationMatrix.col(1).norm();
auto scaleZ = rotationMatrix.col(2).norm();

// Make my rotation matrix orthogonal.
rotationMatrix.col(0).normalize();
rotationMatrix.col(1).normalize();
rotationMatrix.col(2) = rotationMatrix.col(0).cross(rotationMatrix.col(1));
rotationMatrix.col(2).normalize();
rotationMatrix.col(0) = rotationMatrix.col(1).cross(rotationMatrix.col(2));
rotationMatrix.col(0).normalize();


Eigen::Quaterniond q(rotationMatrix);

但是,当我尝试转换回旋转矩阵时,我得到了具有一些不同值的相同矩阵(我认为这是一个特征舍入问题):

  rotationMatrix = q.normalized().matrix();
  /*3.02303  0.484642 -0.124911  
  -0.559522   2.94976 -0.217941 
  0.259569   0.71415  0.984962  */ 

1 个答案:

答案 0 :(得分:0)

有一种用旋转矩阵转换噪声的方法 https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Conversion_to_and_from_the_matrix_representation

“拟合四元数”

与Eigen,它看起来像

Eigen::Matrix<Scalar, 4, 4> c;
c(0, 0) = xx - yy - zz; c(0, 1) = yx + xy; c(0, 2) = zx + xz; c(0, 3) = yz - zy;
c(1, 0) = yx + xy; c(1, 1) = yy - xx - zz; c(1, 2) = zy + yz; c(1, 3) = zx - xz;
c(2, 0) = zx + xz; c(2, 1) = zy + yz; c(2, 2) = zz - xx - yy; c(2, 3) = xy - yx;
c(3, 0) = yz - zy; c(3, 1) = zx - xz; c(3, 2) = xy - yx; c(3, 3) = xx + yy + zz;
c /= 3.0;

Eigen::SelfAdjointEigenSolver<Matrix44> es(c);
typename Vector4::Index maxIdx;
es.eigenvalues().maxCoeff(&maxIdx);
Vector4 xyzw = es.eigenvectors().col(maxIdx);