Home > OS >  Eigen cast AlignedBox double to AlignedBox int
Eigen cast AlignedBox double to AlignedBox int

Time:04-26

I'm trying to use Eigen AlignedBox. Specifically, I'm trying to cast a box of double into an int one, by using AlignedBox::cast

AlignedBox<double, 2> aabbox2d = AlignedBox<double, 2>(Vector2d(0.52342, 2.12315), Vector2d(3.87346, 4.72525));
aabbox2d.cast<AlignedBox<int, 2>>();
auto minx = aabbox2d.min().x();

Anyway, when the execution gets to min() I get an assert:

Assertion failed: (((SizeAtCompileTime == Dynamic && (MaxSizeAtCompileTime==Dynamic || size<=MaxSizeAtCompileTime)) || SizeAtCompileTime == size) && size>=0), function resize, file /Users/max/Developer/Stage/Workspace/AutoTools3D/dep/libigl/external/eigen/Eigen/src/Core/PlainObjectBase.h, line 312.

Note that this is different from casting a matrix scalar to another one. An object is implied. Supposedly I'm not doing the cast correctly. Does someone know the right way? Thank you

CodePudding user response:

Consulting the documentation for AlignedBox::cast shows that the template argument to cast is defined as template<typename NewScalarType> and the return value is *this with scalar type casted to NewScalarType. Thus the cast function does not modify the existing instance of the box, but returns a new one. To make your example work you need to store the returned instance like follows:

AlignedBox<double, 2> aabbox2d = AlignedBox<double, 2>(Vector2d(0.52342, 2.12315), Vector2d(3.87346, 4.72525));
AlignedBox<int, 2>  casted = aabbox2d.cast<int>();
const int minx = casted.min().x();

You can play with this here: https://godbolt.org/z/ozE4rzebb

As a side note: as the documentation states, when working with Eigen one should refrain from using auto (probably not a problem in this case though)

  • Related