I want to extract the first three values โโof type Vector4 in Eigen, into type Vector3. While I'm doing it in a loop. Is there a smarter way to do this?
The member function .head() returns the first n elements of the vector. If n is a compile-time constant, you can use the template option (as in the example below), and the Eigen library will automatically expand the loop.
.head()
Eigen::Vector4f vec4; // initialize vec4 Eigen::Vector3f vec3 = vec4.head<3>();
In the Eigen documentation, see Block operations for introduction to similar operations to extract parts of vectors and matrices and DenseBase :: head () for a specific function.
Yes, because you know that the size is static (3 elements), you have to unroll the loop and copy them explicitly. This optimization can already be done by the compiler, but in fact it will not hurt to do it yourself.