c++ - Eigen::MatrixXd 到 flann::Matrix<double> 转换

标签 c++ stl matrix nearest-neighbor eigen

假设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)在其界面中。这让我觉得我可能会遇到与上面显示的简单(但损坏的)解决方案相同的内存问题。

您认为什么可以解释这种行为?

最佳答案

我还得到了 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 of MatrixXd, 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/

相关文章:

C++:传递和返回指向数组的指针——代码不工作

带修剪的 C++ 决策树

java - 如何检查矩阵是否反对称

c - 获取错误 "passing argument 2 of ‘strcmp’ 使指针来自整数而不进行转换“比较两个字符串

c++ - openGL 片段着色器和原始纹素数据

c++ - 解析逗号分隔的 std::string

c++ - 模拟条件 back_inserter 之类的最佳方法?

c++ - C++ 标准库类型是否实现异常安全的复制赋值?

C++ 使用 STL : Stack and Queue

python - 矩阵和向量的逐元素点积