Skip to content

Transform

Harumo Sasatake edited this page Oct 18, 2018 · 4 revisions

点群を移動させたり,回転させたりする関数

平行移動は下記の関数で行います.

pcl::transformPointCloud(src_cloud, dist_cloud, eigen_transform_matrix)
pcl::transformPointCloud(*src_cloud_ptr, *dist_cloud_ptr, eigen_transform_matrix)
クラス名 変数名 説明
pcl::PointCloudpcl::PointXYZ src_cloud 入力点群
pcl::PointCloudpcl::PointXYZ::Ptr src_cloud_ptr 入力点群
pcl::PointCloudpcl::PointXYZ dist_cloud 出力点群
pcl::PointCloudpcl::PointXYZ::Ptr dist_cloud_ptr 出力点群
Eigen::Matirx4f eigen_transform_matrix 同次回転行列

同次回転行列を直に作る方法

同次変換行列がわからない人は勉強してからのほうがいいかもしれない... 参考

|1 0 0 X|
|0 1 0 Y|
|0 0 1 Z|
|0 0 0 1|

みたいなやつです.上のは平行移動だけの同次行列ですね. では,回転行列を作っていきましょう.加点行列は3次元の場合,回転を表す3x3の行列と3要素のベクトルからなります.

//単位行列で回転行列を埋めます.
Eigen::Matrix4f transform = Eigen::Matrix4f::Edentity();

//3x3の回転行列の部分を埋めます.今回は90度でいきます.
double theta = M_PI/2
//transform(raw, column)
transform(0,0) = std::cos(theta);
transform(0,1) = -std::sin(theta);
transform(1.0) = sin(theta);
transform(1,1) = cos(theta);

//平行移動の部分を埋めます.今回は2.5mだけx軸に沿って移動させます.
transform(0,3) = 2.5;

//できた回転行列を出力してみます
std::cout << transform;

//移動しておわりです
pcl::transformPointCloud(src_cloud, dist_cloud, transform)

ちなみに,平行移動だけだったら,transform(0,3) = 2.5だけで,回転の方は設定しなくても大丈夫です.

アフィン変換から同次回転行列を作る方法(おすすめ)

Eigen::Affine3f = transform = Eigen::Affine3f::Identity();

//平行移動を設定
//2.5mでいきます
transform.translation() << 2.5, 0, 0; //x,y,zの順

//回転を設定
//z軸まわりに90度回します
double theta = M_PI/2;
transform.rotate(Eigen::AngleAxisf(theta, Eigen::Vector3f::UnitZ()));

//表示
std::cout << transform.matrix();

//実行
pcl::transformPointCloud(src_cloud, dist_cloud, transform);

こちらも同様に,平行移動だけだったら,transform.translation() << 2.5, 0, 0 だけで,回転の方は設定しなくても大丈夫です.

Clone this wiki locally