一个模板函数,用于将一串数据映射到一个矩阵或者向量中。
模板是
template<typename PlainObjectType, int MapOptions, typename StrideType>
class Eigen::Map< PlainObjectType, MapOptions, StrideType >
PlainObjectType:指定想要生成的矩阵或者向量的类型,比如Matrix3i,VectorXi,RowVectorXi
MapOptions:以字节为单位指定指针对齐方式,可以为Aligned8,Aligned16....
StrideType:指定“步幅”的种类,可以为外部或者内部,分别为OuterStride或者InnerStride,默认为是连续的,也就是没有步幅。所谓的OuterStride指的是,如果我们构造的矩阵为列优先(column-major),那么每两个连续的列之间的指针就会跳跃一个步幅,如果为InnerStride,那么矩阵或者向量内部每两个连续的元素之间就会跳跃一个步幅。
Map有三种构造函数:
Map (PointerArgType dataPtr, const StrideType &stride=StrideType())
Map (PointerArgType dataPtr, Index size, const StrideType &stride=StrideType())
Map (PointerArgType dataPtr, Index rows, Index cols, const StrideType &stride=StrideType())
dataPtr:指向数据的指针
StrideType:步幅的类型
size:用于构造向量时指定大小
rows,cols:几行几列
下面看例程:
int array[9];
for(int i = 0; i < 9; ++i) array[i] = i;
cout << Map<Matrix3i>(array) << endl;
output:
0 3 6
1 4 7
2 5 8
带StrideType的:
int array[12];
for(int i = 0; i < 12; ++i) array[i] = i;
cout << Map<VectorXi, 0, InnerStride<2> >
(array, 6) // the inner stride has already been passed as template parameter
<< endl;
output:
0
2
4
6
8
10
int array[12];
for(int i = 0; i < 12; ++i) array[i] = i;
cout << Map<MatrixXi, 0, OuterStride<> >(array, 3, 3, OuterStride<>(4)) << endl;
output:
0 4 8
1 5 9
2 6 10
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";
output:
The mapped vector v is: 1 2 3 4
Now v is: 5 6 7 8 9