Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eigen combine rotation and translation into one matrix

Tags:

c++

matrix

eigen

I have a rotation matrix rot (Eigen::Matrix3d) and a translation vector transl (Eigen::Vector3d) and I want them both together in a 4x4 transformation matrix. I just for the life of me can't figure out how to do this in Eigen. I think Affine can be used somehow but I don't understand how it works.

Essentially I want a combination of How translation a matrix(4x4) in Eigen? and Multiplying Transform and Matrix types in Eigen

My code (that doesn't compile as I don't understand how Affine works) looks like this:

Eigen::Affine3d r(rot);
Eigen::Affine3d t(transl);
Eigen::Matrix4d m = t.matrix();
m *= r.matrix();
like image 956
DaedalusAlpha Avatar asked Aug 26 '14 11:08

DaedalusAlpha


People also ask

How do you combine rotation and translation matrix?

A rotation matrix and a translation matrix can be combined into a single matrix as follows, where the r's in the upper-left 3-by-3 matrix form a rotation and p, q and r form a translation vector. This matrix represents rotations followed by a translation.

Does rotation commute with translation?

Rotations and translations do not commute. Translations and scales do not commute. Scales and rotations commute only in the special case when scaling by the same amount in all directions. In general the two operations do not commute.

What is Eigen transformation?

Eigen's Geometry module provides two different kinds of geometric transformations: Abstract transformations, such as rotations (represented by angle and axis or by a quaternion), translations, scalings.


2 Answers

Another method is to do the following:

Eigen::Matrix3d R;
// Find your Rotation Matrix
Eigen::Vector3d T;
// Find your translation Vector
Eigen::Matrix4d Trans; // Your Transformation Matrix
Trans.setIdentity();   // Set to Identity to make bottom row of Matrix 0,0,0,1
Trans.block<3,3>(0,0) = R;
Trans.block<3,1>(0,3) = T;

This method literally copies the Rotation matrix into the first 3 rows and columns and the translation vector to the 4th column. Then sets the bottom right matrix entry to 1. You final matrix will look like:

R R R T
R R R T
R R R T
0 0 0 1

where R are the corresponding values from the rotation matrix and T the values from the Translation vector.

like image 111
Timothy Murphy Avatar answered Oct 19 '22 03:10

Timothy Murphy


You didn't post the compilation errors, nor what are rot and transl. Below is a working sample showing, how you can create a 4x4 transformation matrix.

#include <Eigen/Geometry>

Eigen::Affine3d create_rotation_matrix(double ax, double ay, double az) {
  Eigen::Affine3d rx =
      Eigen::Affine3d(Eigen::AngleAxisd(ax, Eigen::Vector3d(1, 0, 0)));
  Eigen::Affine3d ry =
      Eigen::Affine3d(Eigen::AngleAxisd(ay, Eigen::Vector3d(0, 1, 0)));
  Eigen::Affine3d rz =
      Eigen::Affine3d(Eigen::AngleAxisd(az, Eigen::Vector3d(0, 0, 1)));
  return rz * ry * rx;
}

int main() {
  Eigen::Affine3d r = create_rotation_matrix(1.0, 1.0, 1.0);
  Eigen::Affine3d t(Eigen::Translation3d(Eigen::Vector3d(1,1,2)));

  Eigen::Matrix4d m = (t * r).matrix(); // Option 1

  Eigen::Matrix4d m = t.matrix(); // Option 2
  m *= r.matrix();
  return 0;
}
like image 35
Blaz Bratanic Avatar answered Oct 19 '22 03:10

Blaz Bratanic