Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags. |
Using Eigen
Description: There are quite a few tricks you should be aware of when using eigen...Keywords: ecl eigen linear algebra
Tutorial Level: INTERMEDIATE
Next Tutorial: Eigen Extensions
Contents
Using Eigen
Documentation
The following are some important links for us back to the eigen documentation:
Some important points to note (more detailed notes regarding some eigen specific issues further below):
- Matrices/Vectors of all types are built from the one base class.
- Typedefs exist for all common forms of matrices and vectors (important for vectors as there is no Vector class).
The ecl supplements the eigen library with a few extras features.
Issues
Alignment
You have to be very careful with eigen to make sure alignment works properly. This is a bit of a pain in the bum, but will result in hardware speedups. If you see the following error message:
1 /opt/ros/ecl/ecl_core/ecl_linear_algebra/include/ecl/Eigen2/src/Core/MatrixStorage.h:44:Eigen::ei_matrix_array<T, Size, MatrixOptions, Align>::ei_matrix_array() [with T = double, int Size = 4, int MatrixOptions = 2, bool Align = true]: Assertion `(reinterpret_cast<size_t>(array) & 0xf) == 0 && "this assertion is explained here:
2 http://eigen.tuxfamily.org/dox/UnalignedArrayAssert.html **** READ THIS WEB PAGE !!! ****"' failed.
3
its probably because you need to fix your storage mechanism for classes with eigen vectors inside them. See that web page for more detail and/or run gdb on your program to see where the compile time assert triggered.
Usual scenario - you have used an eigen type of fixed size as a storage container embeddded inside a class or struct, or inherited a class that does so.
- only for fixed size eigen types
- fixed size eigen type is inside the class or inside an inherited class
In this situation, you need to add the following macro to the public section of the class <i>as well as</i> each class that inherits this class.
1 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Std Vector
If always calling linear_algebra.hpp to access eigen, it will bring in Eigen's NewStdVector implementation. This will use it for eigen types and default to the original std vector for other types, but there's one caveat. For eigen to distinguish, you need to specify the vector allocator.
1 #include <ecl/linear_algebra.hpp>
2
3 int main(int argc, char **argv) {
4
5 std::vector< Eigen::Vector2d, Eigen::aligned_allocator<Eigen::Vector2d> > vv; // eigen's std vector implementation
6 std::vector<double> vd; // std's vector implementation
7
8 Eigen::Vector2d v; v << 1,2;
9 vv.push_back(v); vv.push_back(v);
10 std::cout << vv[0] << std::endl;
11
12 return 0;
13 }
If you do not include it, you will see the usual compile time alignment warning that Eigen loves.