Eigen与数组之间的转换

Map类型

T

Map<Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime> >

默认情况下,Map只需要一个模板参数。

为了构建Map变量,我们需要其余的两个信息:一个指向元素数组的指针,Matrix/vector的尺寸。定义一个float类型的矩阵: Map<MatrixXf> mf(pf,rows,columns); pf是一个数组指针float *。

固定尺寸的整形vector声明: Map<const Vector4i> mi(pi);

注意:Map没有默认的构造函数,你需要传递一个指针来初始化对象。

Mat是灵活地足够去容纳多种不同的数据表示,其他的两个模板参数:

Map<typename MatrixType,
    int MapOptions,
    typename StrideType>

其中MapOptions标识指针是否是对齐的(Aligned),默认是Unaligned, 也就是按列对齐,如果需要按行对齐,则使用RowMajor。

StrideType表示内存数组的组织方式:行列的步长。

int array[8];
for(int i = 0; i < 8; ++i) array[i] = i;
cout << "Column-major:\n" << Map<Matrix<int,2,4> >(array) << endl;
cout << "Row-major:\n" << Map<Matrix<int,2,4,RowMajor> >(array) << endl;
cout << "Row-major using stride:\n" <<
  Map<Matrix<int,2,4>, Unaligned, Stride<1,4> >(array) << endl;

Column-major:
0 2 4 6
1 3 5 7
Row-major:
0 1 2 3
4 5 6 7
Row-major using stride:
0 1 2 3
4 5 6 7

W

可以像Eigen的其他类型一样来使用Map类型。

typedef Matrix<float,1,Dynamic> MatrixType;
typedef Map<MatrixType> MapType;
typedef Map<const MatrixType> MapTypeConst;   // a read-only map
const int n_dims = 5;
  
MatrixType m1(n_dims), m2(n_dims);
m1.setRandom();
m2.setRandom();
float *p = &m2(0);  // get the address storing the data for m2
MapType m2map(p,m2.size());   // m2map shares data with m2
MapTypeConst m2mapconst(p,m2.size());  // a read-only accessor for m2
cout << "m1: " << m1 << endl;
cout << "m2: " << m2 << endl;
cout << "Squared euclidean distance: " << (m1-m2).squaredNorm() << endl;
cout << "Squared euclidean distance, using map: " <<
  (m1-m2map).squaredNorm() << endl;
m2map(3) = 7;   // this will change m2, since they share the same array
cout << "Updated m2: " << m2 << endl;
cout << "m2 coefficient 2, constant accessor: " << m2mapconst(2) << endl;
/* m2mapconst(2) = 5; */   // this yields a compile-time error

m1:   0.68 -0.211  0.566  0.597  0.823
m2: -0.605  -0.33  0.536 -0.444  0.108
Squared euclidean distance: 3.26
Squared euclidean distance, using map: 3.26
Updated m2: -0.605  -0.33  0.536      7  0.108
m2 coefficient 2, constant accessor: 0.536

更多实例针对动态数组

///c数组转Eigen::MatrixXd
    double vec[6] = {1, 2, 3, 4, 5, 6};
    std::cout << Eigen::Map<Eigen::VectorXd>(&vec[0], sizeof (vec) / sizeof(double)) << "\n";
    double arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    std::cout << Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(&arr[0][0], 3, 3) << "\n";
///std::vector<double>转Eigen::MatrixXd
    std::vector<int> vec2{1, 2, 3, 4, 5, 6};
    std::cout << Eigen::Map<Eigen::VectorXi>(vec2.data(), 6) << "\n";
    Eigen::Matrix3d mat2 = Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(&arr[0][0], 3, 3);
    std::cout << mat2 << "\n";
///Eigen::MatrixXd转std::vector<double>
    Eigen::Isometry3d vRes = Eigen::Isometry3d::Identity();
    Eigen::Matrix<double, 4, 4, Eigen::RowMajor> res = vRes.matrix();
    auto resa = std::vector<double>(res.data(), res.data() + res.size());

    Eigen::Matrix<float, 4, 4, Eigen::RowMajor> A;
    A << 0.814723686393179,   0.632359246225410,   0.957506835434298,   0.957166948242946,
            0.905791937075619,   0.097540404999410,   0.964888535199277,   0.485375648722841,
            0.126986816293506,   0.278498218867048,   0.157613081677548,   0.800280468888800,
            0.913375856139019,   0.546881519204984,   0.970592781760616,   0.141886338627215;
    Eigen::Matrix<float, 4, 4, Eigen::RowMajor> B;
    B << 0.421761282626275,   0.655740699156587,   0.678735154857773,   0.655477890177557,
            0.915735525189067,   0.035711678574190,   0.757740130578333,   0.171186687811562,
            0.792207329559554,   0.849129305868777,   0.743132468124916,   0.706046088019609,
            0.959492426392903,   0.933993247757551,   0.392227019534168,   0.031832846377421;
    Eigen::Matrix<float, 4, 4, Eigen::RowMajor> C;
    C = A * B;
    std::cout << "\n" << C << std::endl;
    std::cout << "\n";
    float A1[4][4];
    float B1[4][4];
    float C1[4][4];
    Eigen::Map<Eigen::Matrix<float, 4, 4, Eigen::RowMajor>>(&A1[0][0], 4, 4) = A;
    Eigen::Map<Eigen::Matrix<float, 4, 4, Eigen::RowMajor>>(&B1[0][0], 4, 4) = B;
    for (auto & i : B1)
    {
        for (float j : i)
        {
            std::cout << j << " ";
        }
        std::cout << "\n";
    }
    auto A2 = Eigen::Map<Eigen::Matrix<float, 4, 4, Eigen::RowMajor>>(&A1[0][0], 4, 4);
    auto B2 = Eigen::Map<Eigen::Matrix<float, 4, 4, Eigen::RowMajor>>(&B1[0][0], 4, 4);
    std::cout << "\n" << A2 << std::endl;
    std::cout << "\n" << B2 << std::endl;
    float V[4] = {1,2,3,4};
    auto V1 = Eigen::Map<Eigen::Vector4f>(&V[0], 4, 1);
    std::cout << V1.transpose() << std::endl;
    float V2[4];
    Eigen::Map<Eigen::Vector4f>(&V2[0], 4, 1) = V1;
    for (auto & i : V2)
    {
        std::cout << i << " ";
    }
    std::cout << "\n";

实际使用,如按配置文件生成障碍物的位置,半径
yaml文件

CircleObs:               [  0.0,   0.0,   3.0,
                            6.0,   6.0,   2.0,
                           -6.0,  -6.0,   2.0]
nh_priv.getParam("CircleObs", circleObsVec);
mcircleObs=Eigen::Map<Eigen::Matrix<double, 3, 3, Eigen::RowMajor>>(circleObsVec.data());
        ROS_INFO_STREAM(mcircleObs);

Eigen提供的函数都兼容Map对象。

改变mapped数组

Map对象声明后,可以通过C++的placement new语法来改变Map的数组。

int data[] = {1,2,3,4,5,6,7,8,9};
Map<RowVectorXi> v(data,4);
cout << "The mapped vector v is: " << v << "\n";
new (&v) Map<RowVectorXi>(data+4,5);
cout << "Now v is: " << v << "\n";

The mapped vector v is: 1 2 3 4
Now v is: 5 6 7 8 9

Eigen并没有为matrix提供直接的Reshape和Slicing的API,但是这些特性可以通过Map类来实现。

reshape;

.size()

.data()

innerstride<2>

ref

https://blog.csdn.net/qq_27806947/category_9806222.html

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值