#include<Eigen/Dense>// Current stateEigen::Vector4dpoint(1,2,3,4);Eigen::Vector2dv1=point.block<2,1>(0,0);// get x and y coordinate of point in a new vectorEigen::Vector2dv2=point.block<2,1>(2,0);// get z and w coordinate of point in a new vector// ImprovementEigen::Vector2dv3=point.xy();// easier to read - shorter to writeEigen::Vector2dv4=point.zw();// less prone to typing errorsstd::cout<<v1<<std::endl;// output 1, 2std::cout<<v2<<std::endl;// output 3, 4std::cout<<v3<<std::endl;// output 1, 2std::cout<<v4<<std::endl;// output 3, 4
When I tried the first time to get the z and w coordinate of a 4D vector I typed the following code:
The above code is incorrect, but that is hard to see. Writing .zw() would fix those issues.
In our codebase, we use 76 times the .block member method. I am quite sure that sometimes the wrong elements are accessed. We moved from a library that supported swizzle operators to Eigen and replaced all occurrences of swizzles with the .block member function - but we are not happy with that situation.
If you try to access an invalid member as shown in the example bellow you get an error (OUT_OF_RANGE_ACCESS) during compiling.
I recommend accepting my pull request to improve this situation.
Added read-only access swizzle operators to DenseCoeffsBase class.