假设mat
下面是类型 Eigen::MatrixXd
并且已经包含了一些数据。为了避免重复内存,我尝试实例化一个 flann::Matrix<double>
指向 Eigen3 分配的原始内存块的指针的对象:
flann::Matrix<double> input(const_cast<double *>(mat.data(), mat.rows(), mat.cols())
然而,我的算法输出垃圾,但对于丑陋的东西来说还不错:
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);
}
我调查了子类化基类的选项 Matrix_
从 flann 输入以创建 Eigen3 矩阵的适配器。但问题是 Matrix_
依赖于 []
的实现运算符(operator)在其界面中。这让我觉得我可能会遇到与上面显示的简单(但损坏的)解决方案相同的内存问题。
您认为什么可以解释这种行为?
- 行/列主要问题
- 内部、外部步幅问题
- 内存对齐不兼容
-
Eigen::Map
很甜蜜,但不是我要找的。重新编写我的代码以使用stl::vector<std::vector<double> >
会很糟糕作为基本类型和Eigen::Map
他们到Eigen::MatrixXd
- http://nanoflann-reference.mrpt.org/svn/structnanoflann_1_1KDTreeEigenMatrixAdaptor.html不幸的是,离基本的 libflann 库太远而无法使用。
最佳答案
我还得到了 libflann 的作者 Marius Muja 的确认,即 flann::Matrix
以行优先顺序存储,而 Eigen 默认使用列优先。这是他通过电子邮件给我的答案:
The problem is most likely the fact that Eigen stores matrices in column-major order > while FLANN requires them in row-major order.
A solution would be to use
Matrix<double, Dynamic, Dynamic, RowMajor>
instead ofMatrixXd
, then FLANN and Eigen matrices can share the same memory, otherwise a copy will be needed. Marius Muja
关于c++ - Eigen::MatrixXd 到 flann::Matrix<double> 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13465890/