convert Eigen::Matrix to flann::Matrix

The following functions convert (note the matrix size!!)

  • a column-major NxM Eigen::Matrix to a row-major MxN flann::Matrix.
  • a row-major NxM Eigen::Matrix to a row-major NxM flann::Matrix.
template <typename ValueType>
flann::Matrix<ValueType> convertEigen2Flann(
const Eigen::Matrix<ValueType, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>&amp; mEigen
)
{
flann::Matrix<ValueType>mFlann(new ValueType[mEigen.size()], mEigen.rows(), mEigen.cols());

for(size_t n = 0; n < mEigen.size(); ++n)
{
*(mFlann.ptr()+n) = *(mEigen.data()+n);
}
return mFlann;
}

template <typename ValueType>
flann::Matrix<ValueType> convertEigen2Flann(
const Eigen::Matrix<ValueType, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>&amp; _mEigen
)
{
Eigen::Matrix<ValueType, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> mEigen = _mEigen.transpose();
return convertEigen2Flann(mEigen);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s