Convert an Eigen3 Transform's rotation into an AngleAxis
Categories:
Converting Eigen3 Transform Rotation to AngleAxis

Learn how to extract the rotational component from an Eigen3 Transform
object and represent it as an AngleAxis
for various computational geometry applications.
Eigen3 is a powerful C++ template library for linear algebra, matrices, and vectors. It's widely used in robotics, computer vision, and computational geometry. One common task is to work with Transform
objects, which encapsulate both rotation and translation. While the translation component is straightforward, extracting the rotation in a specific format like AngleAxis
can sometimes be tricky. This article will guide you through the process, providing clear examples and explanations.
Understanding Eigen3 Transformations
An Eigen3 Transform
object (e.g., Eigen::Isometry3d
, Eigen::Affine3d
, Eigen::Transform<double, 3, Eigen::Affine>
) represents a rigid body transformation in 3D space. It combines a rotation matrix and a translation vector. The rotation component is typically stored as a 3x3 matrix within the transformation. To convert this rotation into an AngleAxis
representation, we first need to isolate the rotation matrix.
flowchart TD A[Eigen::Transform Object] --> B{Extract Rotation Matrix} B --> C[Eigen::Matrix3d Rotation Matrix] C --> D{Construct Eigen::AngleAxis} D --> E[Eigen::AngleAxis Object] E --> F{Access Angle and Axis} F --> G[Angle (double)] F --> H[Axis (Eigen::Vector3d)]
Flowchart for converting Eigen3 Transform rotation to AngleAxis.
Extracting the Rotation Matrix
The Transform
class provides convenient methods to access its components. The rotation()
method returns the 3x3 rotation matrix. Once you have this matrix, you can use it to construct an AngleAxis
object. The AngleAxis
class in Eigen3 represents a rotation by an angle around a specific axis. This representation is often preferred for its compactness and ease of interpretation compared to a full rotation matrix or quaternion for certain applications.
#include <Eigen/Geometry>
#include <iostream>
int main() {
// 1. Define an Eigen::Transform (e.g., Isometry3d)
Eigen::Isometry3d T = Eigen::Isometry3d::Identity();
// Example: Apply a rotation of 90 degrees around the Z-axis
T.rotate(Eigen::AngleAxisd(M_PI / 2.0, Eigen::Vector3d::UnitZ()));
// Example: Apply a translation
T.translate(Eigen::Vector3d(1.0, 2.0, 3.0));
std::cout << "Original Transform T:\n" << T.matrix() << std::endl;
// 2. Extract the rotation matrix from the transform
Eigen::Matrix3d rotation_matrix = T.rotation();
std::cout << "\nExtracted Rotation Matrix:\n" << rotation_matrix << std::endl;
// 3. Construct an AngleAxis object from the rotation matrix
Eigen::AngleAxisd angle_axis(rotation_matrix);
// 4. Access the angle and axis
double angle = angle_axis.angle();
Eigen::Vector3d axis = angle_axis.axis();
std::cout << "\nAngle (radians): " << angle << std::endl;
std::cout << "Angle (degrees): " << angle * 180.0 / M_PI << std::endl;
std::cout << "Axis: " << axis.transpose() << std::endl;
// Verification: Reconstruct the rotation matrix from AngleAxis
Eigen::Matrix3d reconstructed_rotation = angle_axis.toRotationMatrix();
std::cout << "\nReconstructed Rotation Matrix from AngleAxis:\n" << reconstructed_rotation << std::endl;
return 0;
}
C++ code demonstrating the extraction of rotation matrix and conversion to AngleAxis.
Eigen::AngleAxisd
expects the angle in radians. If you're working with degrees, you'll need to convert them using M_PI / 180.0
or 180.0 / M_PI
as appropriate.Accessing Angle and Axis Components
Once you have an Eigen::AngleAxisd
object, you can easily retrieve the rotation angle using the angle()
method and the rotation axis as a Eigen::Vector3d
using the axis()
method. The axis vector will be normalized (unit length). This provides a clear and intuitive way to understand the rotational component of your transformation.
// Assuming 'angle_axis' is an Eigen::AngleAxisd object
double rotation_angle = angle_axis.angle();
Eigen::Vector3d rotation_axis = angle_axis.axis();
std::cout << "Rotation Angle: " << rotation_angle << " radians" << std::endl;
std::cout << "Rotation Axis: (" << rotation_axis.x() << ", " << rotation_axis.y() << ", " << rotation_axis.z() << ")" << std::endl;
Accessing angle and axis from an AngleAxis object.
AngleAxis
representation is particularly useful for interpolating rotations or visualizing rotations, as it directly gives the direction and magnitude of the rotation.