0
votes

Is there an easy way to wrap a double** in a c++ Eigen typ so that it can be used in Eigen expressions? As far as I know, the Eigen::Map class only supports double*. The storage where double** points to is not guaranteed to be continuous.

1
Possible duplicate of Map a Eigen Matrix to an C array - phuclv
As stated in my question I want to wrap a C matrix not a C array. The two links only show the usage of Eigen::Map with a C array. The storage where the double** points to is not guaranteed to be continuous. - Jodebo
there's no matrix type in C. If you create it your own so you have to work with them manually - phuclv
@Jodebo double** is not a matrix, assuming array decay, it's an array of arrays at best. It doesn't guarantee rectangular size, which is pretty important for a matrix, that's a bigger problem IMO. - luk32

1 Answers

0
votes

As you pointed out, you can use an Eigen::Map to wrap contiguous data:

double *data = new...
Map<MatrixXd> mappedData(data, m, n);
mappedData.transposeInPlace(); // etc.

Non-contiguous data has to be made contiguous, either as a copy in an Eigen object, or as a contiguous double* and using a Map as above.

Edit

You can't use (map/wrap) a non-contiguous section of memory as an Eigen::Matrix. If you have control of the memory allocation, you can do something like:

// make contiguous data array; better yet, make an
// Eigen::VectorXd of the same size to ensure alignment
double *actualData = new double[m*n]; 
double **columnPointers = new double[n];
for(int i = 0; i < n; ++i) columnPointers[i] = actualData + i * m;
... // do whatever you need to do with the double**
Map<MatrixXd> mappedData(actualData, m, n);
... // send to ceres, or whatever