Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Eigen
3.
Geometrical Transformations
4.
Transformation Types
5.
Standard APIs Across Transformation Types
6.
Affine Transformations
7.
Component Accessors
8.
Transformation Creation
9.
Euler Angles
9.1.
Sample Program in C++
10.
Frequently Asked Questions
10.1.
What are some common operations that can be performed using the Eigen Library?
10.2.
What are the primary fields where the Eigen Library is used?
10.3.
What are Euler Angles?
10.4.
What is OpenGL?
10.5.
What are Affine Transformations?
11.
Conclusion
Last Updated: Mar 27, 2024
Medium

Eigen's Geometry

Introduction

You must have thought about why we must manually do all the tough mathematical calculations, as it is sometimes very boring. They invented computers to make things easier, did they not?

Well, someone else thought it too, and Eigen the beautiful thing that came out of their mind.

Eigen's Geometry

What is Eigen

Eigen has a wide range of usages and can help perform complex mathematical operations. These operations are Dot Product, Cross Product, Vector Addition, Vector Subtraction, Multiplication, and other algebraic operations.

Eigen’s library is extremely useful for people involved in robotics. It makes a lot of complicated tasks possible relatively quickly. It has many applications in robotics and helps perform different operations.

Geometrical Transformations

Eigen’s Geometry Module offers many methods to deal with things like 2D/3D rotations and Projective or Affine Transformations.

Eigen’s Geometry Module offers transformations that are summarised into two broad categories:

  1. Abstract Transformations like Rotations, Translations, and Scaling
  2. Projective or Affine Transformation Matrices


For example

A Transform for an abstract Transformation can be defined as

Transform t(AngleAxis(angle, axis));
You can also try this code with Online C++ Compiler
Run Code


Or 

Like this:

Transform t;
T = AngleAxis(angle, axis)
You can also try this code with Online C++ Compiler
Run Code

Transformation Types

Let us look at some common transformation types and their initialization code:

Transformation Types Typical Initialization Code
2D Rotation From an Angle
Rotation2D<float> rotation_angle(angle_in_radian);
You can also try this code with Online C++ Compiler
Run Code
3D Rotation as an angle + axis
AngleAxis<float>angleAxis(angle_in_radian, Vector3f(ax,ay, az)); 
The Axis Vector must Be Normalized
You can also try this code with Online C++ Compiler
Run Code
3D Rotation as a Quaternion
Quaternion<float> Quat;  
Quat= AngleAxis<float>(angle_in_radian, axis);
You can also try this code with Online C++ Compiler
Run Code
N-D Scaling 
Scaling(sx, sy)
Scaling(sx, sy, sz)
Scaling(s)
Scaling(vecN)
You can also try this code with Online C++ Compiler
Run Code
N-D Translation
Translation<float,2>(tx, ty)
Translation<float,3>(tx, ty, tz)
Translation<float,N>(_s)
Translation<float,N>(vectorN)
You can also try this code with Online C++ Compiler
Run Code
N-DAffine Transformation
Transform<float,N,Affine> tr = concatenationOfAnyTransformations;
Transform<float,3,Affine> tr = Translation3f(p) * AngleAxisf(a,axis) * Scaling(s);
You can also try this code with Online C++ Compiler
Run Code

N-D Linear transformations

(pure rotations,

scaling, etc.)

Matrix<float, N> mat = concatenationOfRotationsAndScalings;
Matrix<float,2> mat = Rotation2Df(a) * Scaling(s);
Matrix<float,3> mat = AngleAxisf(a, axis) * Scaling(s);
You can also try this code with Online C++ Compiler
Run Code

Notes on Rotations:

Rotation Matrices are preferred representations to convert more than a single vector. Quaternion represents a choice for other uses as they are fast, compact, and stable.

Finally, Rotation2D and Angle Axis are suitable for creating other rotation objects.

Notes on Translation and Scaling:

These classes, like AngleAxis, were created to simplify the design and definition of linear matrices and Affine transformations.  

However, unlike AngleAxis, which is weak, these classes might still be interested in writing generic and efficient algorithms taking any transformations as input.

Any of the above Transformations can be converted into others of the exact nature or to a more generic type. Here are some examples

Rotation2Df rot;  rot  = Matrix2f(..);       // assumes a pure rotation matrix
AngleAxisf angleAxis;  angleAxis = Quaternionf(..);
AngleAxisf angleAxis;  angleAxis = Matrix3f(..);       // assumes a pure rotation matrix
Matrix2f matrix2F;     matrix2F  = Rotation2Df(..);
Matrix3f matrix3F;     matrix3F  = Quaternionf(..);       
Matrix3f matrix2F;   matrix2F = Scaling(..);
Affine3f affine3F;     affine3F  = AngleAxis3f(..);       
Affine3f affine3F;   affine3F = Scaling(..);
Affine3f affine3F;     affine3F  = Translation3f(..);     
Affine3f affine3F;   affine3F = Matrix3f(..)
You can also try this code with Online C++ Compiler
Run Code

Standard APIs Across Transformation Types

Eigen’s Geometry Module allows you to write generic algorithms that work on various transformation representations.

Concatenation of two transformations
tran1 * tran2;
You can also try this code with Online C++ Compiler
Run Code
Apply the transformation to a vector
vector2 = tran1 * vector1;
You can also try this code with Online C++ Compiler
Run Code
Get the inverse of the transformation
tran2 = tran1.inverse();
You can also try this code with Online C++ Compiler
Run Code
Spherical interpolation
(Rotation 2D and Quaternion Only)
rotation3 = rotation1.slerp(alpha, rotation2);
You can also try this code with Online C++ Compiler
Run Code

Affine Transformations

The Affine Transformations that are generic are represented by the class called Transform Class. Transform Class is internally a (Dim +1) ^ 2 matrix (here, Dim is the Dimension of the Matrix). 

In Eigen’s Geometry Module, Vectors and Points are declared as same; Displacement Vectors from the origin represent all points. As they are the same, the actual points and vectors can be told apart when the transformation is applied.

Apply Transformation to a Point
VectorNf vector1, vector2;
vector2 = t * vector1;
You can also try this code with Online C++ Compiler
Run Code
Applying Transformation to some Vector
VectorNf vector1, vector2;
vector2 = t.linear() * vector1;
You can also try this code with Online C++ Compiler
Run Code
Apply a General Transformation to some Normal Vector
VectorNf vector1, vector2;
MatrixNf normalMatrix = tr.linear().inverse().transpose();
normalV2 = (normalMatrix * normalV1).normalized();
You can also try this code with Online C++ Compiler
Run Code
Apply Transformation with Pure Rotation to a Normal Vector
normalV2 = tran.linear() * normalV1;
You can also try this code with Online C++ Compiler
Run Code
OpenGL Compatibility 3D
glLoadMatrixf(tr.data());
You can also try this code with Online C++ Compiler
Run Code
OpenGL Compatibility 2D
Affine3f affineX(Affine3f::Identity());
affineX.linear().topLeftCorner<2,2>() = tr.linear();
affineX.translation().start<2>() = tr.translation();
glLoadMatrixf(affineX.data());
You can also try this code with Online C++ Compiler
Run Code

Component Accessors

Let us look at some component accessors:

Full Read-Write Access to the Internal Matrix
tr.matrix() = matrixN1xN1;    // N1 means N+1
matrixN1xN1 = tr.matrix();
You can also try this code with Online C++ Compiler
Run Code
Coefficient Accessors
tr(i,j) = scalar;   <=>   tr.matrix()(i,j) = scalar;
scalar = tr(i,j);   <=>   scalar = tr.matrix()(i,j);
You can also try this code with Online C++ Compiler
Run Code
Translation Part
tr.translation() = vectorN;
vectorN = tr.translation();
You can also try this code with Online C++ Compiler
Run Code
Linear Part
tr.linear() = matrixNxN;
matrixNxN = tr.linear();
You can also try this code with Online C++ Compiler
Run Code
Extract The Rotation Matrix
matrixNxN = tr.rotation();
You can also try this code with Online C++ Compiler
Run Code

Transformation Creation

It should be noted that in both of the APIs said above. Different transformations can be joined into a single expression, as mentioned in the examples below:

t.pretranslate(..).rotate(..).translate(..).scale(..);
t = Translation_(..) * t * RotationType(..) * Translation_(..) * Scaling(..);
Transformation Creation Procedural API Equivalent Natural API
Translation
tr.translate(Vector_(tx,ty,..));
tr.pretranslate(Vector_(tx,ty,..));
You can also try this code with Online C++ Compiler
Run Code
tr = Translation_(tx,ty,..) * t;
tr *= Translation_(tx,ty,..);
You can also try this code with Online C++ Compiler
Run Code

Rotation 

(in 2 Dimensions and for procedural API, anyRotation can also be an angle in radian)

tr.rotate(anyRotation);
tr.prerotate(anyRotation);
You can also try this code with Online C++ Compiler
Run Code
tr = anyRotation * tr;
tr *= anyRotation;
You can also try this code with Online C++ Compiler
Run Code
Scaling
tr.scale(Vector_(sx,sy,..));
tr.scale(sc);
tr.prescale(Vector_(sx,sy,..));
tr.prescale(sc);
You can also try this code with Online C++ Compiler
Run Code
tr.scale(Vector_(sx,sy,..));
tr.scale(sc);
tr.prescale(Vector_(sx,sy,..));
tr.prescale(sc);
You can also try this code with Online C++ Compiler
Run Code
Shear Transformation 
(2D Only)
t.shear(sx,sy);
t.preshear(sx,sy);
You can also try this code with Online C++ Compiler
Run Code
-

Also see, Application of Oops

Euler Angles

The Euler Angles prove to be helpful for creating different rotation objects. But since there are 24 different ways, they could be more clear. 

The example below shows a method to develop a rotation matrix following the 2-1-2 way:

Matrix3f m;
m = AngleAxisf(angle1, Vector3f::UnitZ())
   * AngleAxisf(angle2, Vector3f::UnitY())
   * AngleAxisf(angle3, Vector3f::UnitZ());
You can also try this code with Online C++ Compiler
Run Code

Sample Program in C++

#include<iostream>
#include<Eigen/Dense>
using namespace std;

int main(){
    Eigen::Matrix3f m;
    m<<1.1,3.4,11.1,
    14, 12 ,8,
    0, 0.5, 3;
    cout<<"Before rotation\n";
    cout<<m<<'\n';
    m = Eigen::AngleAxisf(45, Eigen::Vector3f::UnitZ())
        * Eigen::AngleAxisf(90, Eigen::Vector3f::UnitY())
        * Eigen::AngleAxisf(45, Eigen::Vector3f::UnitZ());
    cout<<"\n\nAfter Rotation:\n"<<m<<'\n';
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code


Output:

Output

Try and compile by yourself with the help of online C++ Compiler for better understanding.

Frequently Asked Questions

What are some common operations that can be performed using the Eigen Library?

Functions such as the definition of vectors, matrices, and quaternions, along with some mathematical functions such as addition/subtraction of vectors, dot product, and cross product, can also be completed using the Eigen’s Geometry Library.

What are the primary fields where the Eigen Library is used?

The Eigen’s Geometry Library is very useful for Robotics and other real-world applications. Eigen’s Library is used to achieve many algebraic computations quickly.

What are Euler Angles?

Euler Rotations are the set of three angles about which the matrix can be rotated. Euler Angles represent a rotation in three dimensions space as three Euler Angles.

What is OpenGL?

OpenGL is a cross-platform and cross-interface library that renders 2D and 3D vector graphics. The OpenGL API is typically used to interact with the graphics processing unit to achieve hardware-accelerated rendering.

What are Affine Transformations?

Affine transformation is a linear method that preserves mathematical entities such as points, straight lines, and planes. It can also be described as a transformation that preserves co-linearity and distance ratios.

Conclusion

In this article, we briefly discussed what Eigen’s Library is and what its usages are. 

We discussed the Geometry Methods/Transformations offered in Eigen (Eigen’s Geometry) at length and how to implement them.

For more information on Eigen and related topics, refer to the following articles:


Also, check out some of the Guided Paths available on Coding Ninjas Studio on topics such as the Basics of C++Operating Systems, and Computer Networks, along with some Interview Experiences and Interview Bundles for placement preparation.

Live masterclass