5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Eigen 動的 Matrix 型に std::vector などから値をセットする

Last updated at Posted at 2020-03-20

背景

std::vector やポインタから Eigen::MatrixXd などの動的サイズの Matrix に変換したい.

方法

Eigen::Map を使う.

引数のサイズは, MatrixXd で個数なので, out-of-bounds アクセスにならないように気を付ける.
(map に指定できるのは入力側ではポインタのアドレスだけっぽい)

std::vector<double> pts(3 * 100);

Eigen::Map<Eigen::MatrixXd> p(pts.data(), 3, 100);

Row major or column major?

メモリの格納レイアウト.

Eigen では default は column-major である(OpenGL と同じ).
三次元座標データだと, std::vector では XYZXYZXYZ... と格納されているのを想定していると解釈されるとの理解でよいかと思います.

TODO

  • Stride や alignment を考慮した指定方法を確認する.
  • row major, column major 切り替えの方法を探す.
  • memcpy 的な方法を探す(デフォルトのコンストラクタが memcpy 的かな?)
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?