2010/8/20
<lfrfly@xxxxxxxxxxx>
I want to store the vertices of a convex polygon in an Eigen::Matrix<double, 7,2>, and then find the area of the polygon using the method of determinants. In other words, I need to make a series of mat.block<2,2>(n,0).det() calls. However, for this to work, the vertices must be in order (clockwise or ccw doesn't matter). So I figured I'd use a convex hull algorithm to reorder them.
Well, I already have a dependency on OpenCV, so I thought I'd use their convexHull() method. However, to do this I must either reinterpret or copy the contents of the Eigen::Matrix into a cv::Mat_<cv::Vec2f>(7,1). The internal format of this type is straightforward----I can just write floats in rowmajor order using std::transform() and it will be fine.
The tricky part is figuring how what is the best way to get at that data in the Eigen::Matrix. Curiously, upon examining the internals from data() in the debugger, the data appears to be in column-major order even though I didn't specify that (is it the default for some reason?). I know I can use the rowwise() method, but it isn't obvious to me if it's possible to get any kind of iterator from that, or if there's some other way to do the copy cleanly.
Eigen defaults to column-major.
If you want row-major with Eigen you can do:
typedef Matrix<float,2,2,RowMajor> MyRowMajorMatrixType;
or you can even do:
#define EIGEN_DEFAULT_TO_ROW_MAJOR
before including eigen headers.
Benoit
Of course I could simply loop over the thing, but I strive for more elegant solutions when possible.