Eigen學習筆記(8)-原生緩存的接口:Map類

原文:Eigen官網-Interfacing with raw buffers: the Map class

1. 引言

本篇文章將介紹Eigen如何與原生raw C/C++ 數組混合編程。當你從其他庫中導入vectors或matrices時,這將會很有用。

你偶爾也許會想將預先定義的一個數組在Eigen中作爲vector或matrix進行使用,相比copy數據,更一般的方式是複用數據的內存,將它們轉變爲Eigen類型。Map類很好地實現了這個功能。

2. Map類型和Map變量聲明

Map的定義:

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

在這種默認的定義中,Map只需要一個模板參數-Matrix。
爲了構造Map變量,需要另兩個信息:一個指針,該指針指向用於定義數組元素的內存區域;另一個是希望得到的matrix或vector的形狀。

比如:定義一個float類型、動態尺寸大小的matrix:

Map<MatrixXf> mf(pf,rows,columns);

其中pf是一個float *類型的指向數組內存的指針。

定義一個固定大小的、整形的、只讀vector:

Map<const Vector4i> mi(pi);

其中,pi是一個int *類型的指針。在這個例子中不需要傳遞尺寸大小給構造函數,因爲尺寸大小已經由Matrix/Array類型確定了。

注意:Map沒有默認的構造函數,你需要傳遞一個指針來初始化對象。

Map能夠足夠靈活地去容納多種不同的數據表示,其他的兩個模板參數:

Map<typename MatrixType,
    int MapOptions,
    typename StrideType>
  • MapOptions標識指針是否是對齊的(Aligned or Unaligned),默認是Unaligned。
  • 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

3. 使用Map變量

可以像使用任何一種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

Eigen提供的函數都兼容Map對象。

4. 修改Map數組

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

儘管外觀如此,這不會調用內存分配器,因爲語法指定了存儲結果的位置。

此語法使得在不知道映射數組在內存中的位置的情況下聲明映射對象成爲可能:

Map<Matrix3f> A(NULL);  // don't try to use this matrix yet!
VectorXf b(n_matrices);
for (int i = 0; i < n_matrices; i++)
{
  new (&A) Map<Matrix3f>(get_matrix_pointer(i));
  b(i) = A.trace();
}

參考:
“Eigen教程(8)”

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章