1

I have a transformation matrix of type Eigen::Matrix4d. And I would like to get its inverse. I write a function my self to compute it by the following formular.

enter image description here.

And here is my code:

Eigen::Matrix4d inverseTransformation(Eigen::Matrix4d T)
{
    Eigen::MatrixX3d R;
    Eigen::Vector3d t;
    R = T.block<3, 3>(0, 0);
    t = T.block<3, 1>(0, 3);
    Eigen::Matrix4d result;
    result.setIdentity();
    result.block<3, 3>(0, 0) = R.transpose();
    result.block<3, 1>(0, 3) = -R.transpose() * t;
    return result;
}



    std::cout<<"Input transformation matrix is:\n" << T << std::endl;
    std::cout << "inverse of T , my implementation:\n" << inverseTransformation(T) << std::endl << std::endl;
    std::cout << "inverse of T , Eigen implementation::\n" << T.inverse() << std::endl << std::endl;
    std::cout << "T * T^(-1), my implementation\n " << T * inverseTransformation(T) << std::endl << std::endl;
    std::cout << "T * T^(-1), eigen's implementation\n " << T * T.inverse() << std::endl << std::endl;

Ideally, T * T^(1) should be I. However, there is some error in my result (the red part in the following picture.)

enter image description here

By contrast, the result from T * T.inverse() is much more accurate.

Could someone please tell me why? Thanks a lot in advance!

Update: Inverse of a rotation matrix is its transpose. The result will be more accurate if I replace R.tranpose() with R.inverse().

KillerBee
  • 11
  • 3
  • 2
    It has to do with floating point precission, which is not that precise. There are complete univeristy courses on how to minimize errors in outcomes devoted to this. But it all starts here : [is floating point math broken](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Pepijn Kramer Dec 08 '22 at 11:45
  • in general, differnt algorithms yield different results with different precision, that should not be a surprise. Consider you would have a solution only accurate up to 2 digits, then you could use the result of `T * T.inverse()` to improve the result, you could do this iteratively to get incrementally better precision. Now it only depends at what point you stop that iteration. And eventually you are limited by precision of `double` – 463035818_is_not_an_ai Dec 08 '22 at 11:48
  • According to the formula you posted, The top left part of the matrix should be R^(-1) (i.e. R inverse), not R transposed as done your code. Same goes regarding -R^(-1)*d. – wohlstad Dec 08 '22 at 11:48
  • @wohlstad Maybe `R` is a rotation matrix, whose inverse is equal to its transpose. But it would be good if this were clarified in the question. – Thomas Dec 08 '22 at 11:53
  • @Thomas I see, missed that. I believe these lines of code should have a comment stating that, for clarity. – wohlstad Dec 08 '22 at 11:55
  • 2
    @wohlstad, Hi I just added that in the question. And I found that If I use R.inverse() instead of R.transpose(), the result will be much more accurate. Now I believe that the rotation matrix expressed in the floating system is not accurate enough. Hence, I cannot replace R.inverse() with R.transpose() – KillerBee Dec 08 '22 at 12:02
  • @PepijnKramer Hi, thank you for your hint. That makes sense. Theoretically, R.inverse() equals to R.tranpose(). But this is not correct in floating point system – KillerBee Dec 08 '22 at 12:05

1 Answers1

0

Thanks to the comments. Since I would like to close this question, I will answer it my self.

Theoretically, R.inverse() equals to R.tranpose(). But this is not correct in floating point system. Once I use R.inverse(), the result is more accurate.

KillerBee
  • 11
  • 3