Assume that mat
below is of type Eigen::MatrixXd
and already contains some data. In an attempt to avoid duplicating memory, I tried to instantiate a flann::Matrix<double>
object from the pointer to the raw memory chunk allocated by Eigen3:
flann::Matrix<double> input(const_cast<double *>(mat.data(), mat.rows(), mat.cols())
However, my algorithm outputs garbage, but is just fine with the ugly:
flann::Matrix<double> input(new double[mat.rows()*mat.cols()], mat.rows(), mat.cols());
for (int i = 0; i < mat.rows(); i++) {
for (int j = 0; j < mat.cols(); j++) {
input[i][j] = mat(i, j);
}
}
I investigated the option to subclass the base Matrix_
type from flann to create an adaptor to Eigen3 matrices. The problem though is that Matrix_
relies on the implementation of the []
operator in its interace. It makes me feel that I might encounter the same memory issue than in the simple (but broken) solution shown above.
What do you think could explain such behaviour ?
- Row/column-major issue
- Inner, outer stride issue
- Memory alignment incompatibilities
Eigen::Map
is sweet, but not what I'm looking for. It would suck to re-write my code to usestl::vector<std::vector<double> >
as base types andEigen::Map
them toEigen::MatrixXd
- http://nanoflann-reference.mrpt.org/svn/structnanoflann_1_1KDTreeEigenMatrixAdaptor.html unfortunately too far from the base libflann library to be usable.