56

我以前见过它,但我不记得如何用相同长度的 a 有效地初始化Eigen::Vector已知长度的 a std::vector。这是一个很好的例子:

std::vector<double> v1 = {1.0, 2.0, 3.0};

Eigen::Vector3d v2; // Do I put it like this in here: v2(v1) ?
v2 << v1[0], v1[1], v1[2]; // I know you can do it like this but 
                           // I am sure i have seen a one liner.

我已经仔细阅读有关高级矩阵初始化的页面,但没有明确说明执行此操作的方法。

4

5 回答 5

56

根据 Eigen Doc,Vector是 Matrix 的 typedef,而 Matrix 有一个具有以下签名的构造函数:

Matrix (const Scalar *data)

构造一个使用从 data 开始的系数初始化的固定大小的矩阵。

向量参考定义std::vector::data为:

std::vector::data

T* data();
const T* data() const;

返回指向用作元素存储的基础数组的指针。指针使得范围[data(); data() + size())始终是有效范围,即使容器为空。

因此,您可以将向量的数据作为Vector3d构造函数参数传递:

Eigen::Vector3d v2(v1.data());

此外,从 Eigen 3.2.8 开始,上述构造函数定义为:

template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
inline Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
  ::Matrix(const Scalar *data)
{
  this->_set_noalias(Eigen::Map<const Matrix>(data));
}

如您所见,它也使用Eigen::Map,正如@ggael 和@gongzhitaao 所指出的那样。

于 2013-06-11T06:22:15.213 回答
32

只是为了扩展@ggael的答案,以防其他人没有注意到:

来自快速参考指南:映射外部数组

float data[] = {1,2,3,4};
Map<Vector3f> v1(data);       // uses v1 as a Vector3f object
Map<ArrayXf>  v2(data,3);     // uses v2 as a ArrayXf object
Map<Array22f> m1(data);       // uses m1 as a Array22f object
Map<MatrixXf> m2(data,2,2);   // uses m2 as a MatrixXf object
于 2013-11-14T00:10:48.153 回答
28

以下单行应该更正确:

#include <Eigen/Dense>
#include <Eigen/Core>

std::vector<double> a = {1, 2, 3, 4};
Eigen::VectorXd b = Eigen::Map<Eigen::VectorXd, Eigen::Unaligned>(a.data(), a.size());
于 2017-06-18T08:17:09.963 回答
22

我通过此链接找到了更好的答案:

https://forum.kde.org/viewtopic.php?f=74&t=94839

基本上首先创建一个指向 std 向量的指针,然后使用 Map 将指针和长度传递给构造函数。

此方法适用于 Eigen 中的动态 Vector 对象。当我尝试使用 std 向量中的 .data() 函数作为第一个答案建议时,它给了我一个错误:静态断言失败:YOU_CALLED_A_FIXED_SIZE_METHOD_ON_A_DYNAMIC_SIZE_MATRIX_OR_VECTOR

但是使用这种方法它可以工作!

我只是从此处的链接中复制并粘贴相关代码:

std::vector<double> v(4, 100.0);
double* ptr = &v[0];
Eigen::Map<Eigen::VectorXd> my_vect(ptr, 4);
于 2016-08-26T03:27:25.543 回答
-4

这是我在数组/向量和 Eigen::Vector/Eigen::Matrix 之间转换的简单代码。

#include <iostream>
#include <vector>
#include <Eigen/Dense>

using namespace std;
using namespace Eigen;

void array2eigenMat();
void eigenMat2array();

void array2eigenVec();
void eigenVec2array();

void vector2eigenMat();
void eigenMat2vector();

void vector2eigenVec();
void eigenVec2vector();
int main()
{
    cout << "hello world" <<endl;
    array2eigenMat();
    eigenMat2array();

    array2eigenVec();
    eigenVec2array();

    vector2eigenMat();
    eigenMat2vector();

    vector2eigenVec();
    eigenVec2vector();
    return 0;
}

void array2eigenMat()
{
    cout << "-------------------------- array2eigenMat  --------------------------" << endl;

    int array[9];
    for (int i = 0; i < 9; ++i) array[i] = i;
    cout << "array = [ "; for (int i = 0; i < 9; ++i) cout << array[i] << " "; cout << "]" << endl;

    cout << "colMajor matrix = \n" << Map<Matrix3i>(array) << endl;                     // map a contiguous array as a column-major matrix
    cout << "rowMajor matrix = \n" << Map<Matrix<int, 3, 3, RowMajor>>(array) << endl;  // map a contiguous array as a row-major matrix


    cout << "stride matrix = \n" << Map<MatrixXi, 0, OuterStride<>>(array, 3, 3, OuterStride<>(2)) << endl;
    //mapping an array while specifying an outer stride. Here, since we're mapping as a column-major matrix, 
    // 'outer stride' means the pointer increment between two consecutive columns


    Map<MatrixXi> eigMat1(array, 3, 3); 
    MatrixXi      eigMat2 = Map<MatrixXi>(array, 3, 3);
    array[0] = 9;

    cout << "eigMat1 matrix = \n"; cout << eigMat1 << endl;
    cout << "eigMat2 matrix = \n"; cout << eigMat2 << endl;
    cout << "---------------------------------------------------------------------" << endl;

}
void eigenMat2array()
{
    cout << "-------------------------- eigenMat2array  --------------------------" << endl;
    Matrix3d eigMat;
    eigMat << 
        1, 2, 3,
        4, 5, 6,
        7, 8, 9;
    cout << "init eigMat = \n";    cout << eigMat << endl;

    double* eigMatptr = eigMat.data();
    cout << "array = [ "; for (int i = 0; i < 9; ++i) cout << eigMatptr[i] << " "; cout << "]" << endl;

    eigMat(0, 0) = 9;
    cout << "array = [ "; for (int i = 0; i < 9; ++i) cout << eigMatptr[i] << " "; cout << "]" << endl;


    double *eigMatptrnew = new double[eigMat.size()];
    Map<MatrixXd>(eigMatptrnew, eigMat.rows(), eigMat.cols()) = eigMat;

    eigMat(2, 2) = 0;
    cout << "init matrix = \n"; cout << eigMat << endl;
    cout << "array = [ "; for (int i = 0; i < 9; ++i) cout << eigMatptr[i] << " "; cout << "]" << endl;
    cout << "---------------------------------------------------------------------" << endl;
}

void array2eigenVec()
{
    cout << "-------------------------- array2eigenVec  --------------------------" << endl;

    int array[9];
    for (int i = 0; i < 9; ++i) array[i] = i;
    cout << "data array = [ "; for (int i = 0; i < 9; ++i) cout << array[i] << " "; cout << "]" << endl;

    Map<VectorXi> eigVec(array, 5);
    cout << "eigen  vector transpose = " << eigVec.transpose() << endl;
    cout << "stride vector transpose = " << Map<VectorXi, 0, InnerStride<2> >(array, 4).transpose() << endl;
    // map an array as a vector, specifying an inner stride, that is, the pointer increment between two consecutive coefficients

    array[0] = 9;
    cout << "eigen  vector transpose = " << eigVec.transpose() << endl;
    cout << "stride vector transpose = " << Map<VectorXi, 0, InnerStride<2> >(array, 4).transpose() << endl;

    cout << "---------------------------------------------------------------------" << endl;
}
void eigenVec2array()
{
    cout << "-------------------------- eigenVec2array  --------------------------" << endl;
    VectorXf eigvec(5);
    eigvec << 0, 1, 2, 3, 4;
    cout << "eigen  vector transpose = " << eigvec.transpose() << endl;

    float *array = new float;
    array = eigvec.data();
    cout << "data array = [ "; for (int i = 0; i < eigvec.size(); ++i) cout << array[i] << " "; cout << "]" << endl;

    eigvec(0) = 9;
    cout << "data array = [ "; for (int i = 0; i < eigvec.size(); ++i) cout << array[i] << " "; cout << "]" << endl;

    array[0] = 5;
    cout << "eigen  vector transpose = " << eigvec.transpose() << endl;

    cout << "---------------------------------------------------------------------" << endl;
}

void vector2eigenMat()
{
    cout << "-------------------------- vector2eigenMat --------------------------" << endl;
    vector<int> stdvec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    Map<Matrix<int, 3, 3, RowMajor>> eigMat1(stdvec.data());
    MatrixXi                         eigMat2 = Map<Matrix<int, 3, 3, RowMajor>>(stdvec.data());

    cout << "eigMat1 matrix = \n"; cout << eigMat1 << endl;
    cout << "eigMat2 matrix = \n"; cout << eigMat2 << endl;

    stdvec[0] = 9;
    cout << "eigMat1 matrix = \n"; cout << eigMat1 << endl;
    cout << "eigMat2 matrix = \n"; cout << eigMat2 << endl;

    cout << "---------------------------------------------------------------------" << endl;
}
void eigenMat2vector()
{
    cout << "-------------------------- eigenMat2vector --------------------------" << endl;
    Matrix3d eigMatCol;
    eigMatCol << 
        1, 2, 3,
        4, 5, 6,
        7, 8, 9;
    cout << "eigen matrix col = \n";    cout << eigMatCol << endl;
    vector<double> stdvec1(eigMatCol.data(), eigMatCol.data() + eigMatCol.size());
    cout << "std   vector1 = ["; for (int i = 0; i < stdvec1.size(); ++i) cout << stdvec1[i] << " "; cout << "]" << endl;

    Matrix<double, 3, 3, RowMajor> eigMatRow = eigMatCol;
    cout << "eigen matrix row = \n";    cout << eigMatCol << endl;
    vector<double> stdvec2(eigMatRow.data(), eigMatRow.data() + eigMatRow.size());
    cout << "std   vector2 = ["; for (int i = 0; i < stdvec2.size(); ++i) cout << stdvec2[i] << " "; cout << "]" << endl;

    cout << "---------------------------------------------------------------------" << endl;
}

void vector2eigenVec()
{
    cout << "-------------------------- vector2eigenVec --------------------------" << endl;
    vector<int> stdvec{ 1, 2, 3, 4, 5 };
    cout << "std   vector = ["; for (int i = 0; i < stdvec.size(); ++i) cout << stdvec[i] << " "; cout << "]" << endl;

    Map<VectorXi> eigVec1(stdvec.data(), stdvec.size());
    VectorXi eigVec2 = Map<VectorXi>(stdvec.data(), stdvec.size());
    cout << "eigen  vector1 transpose = " << eigVec1.transpose() << endl;
    cout << "eigen  vector2 transpose = " << eigVec2.transpose() << endl;
    cout << "stride vector  transpose = " << Map<VectorXi, 0, InnerStride<2> >(stdvec.data(), 2).transpose() << endl;


    stdvec[0] = 9;
    cout << "eigen  vector1 transpose = " << eigVec1.transpose() << endl;
    cout << "eigen  vector2 transpose = " << eigVec2.transpose() << endl;

    cout << "stride vector  transpose = " << Map<VectorXi, 0, InnerStride<2> >(stdvec.data(), 2).transpose() << endl;

    cout << "---------------------------------------------------------------------" << endl;
}
void eigenVec2vector()
{
    cout << "-------------------------- eigenVec2vector --------------------------" << endl;
    VectorXf eigvec(5);
    eigvec << 0, 1, 2, 3, 4;
    cout << "eigen  vector transpose = " << eigvec.transpose() << endl;

    vector<float> stdvec(eigvec.data(), eigvec.data() + eigvec.size());
    cout << "std   vector = ["; for (int i = 0; i < stdvec.size(); ++i) cout << stdvec[i] << " "; cout << "]" << endl;

    eigvec(0) = 5;
    cout << "std   vector = ["; for (int i = 0; i < stdvec.size(); ++i) cout << stdvec[i] << " "; cout << "]" << endl;
    cout << "---------------------------------------------------------------------" << endl;
}

更多细节在这里

于 2018-12-04T06:45:17.000 回答