参考学习:
http://www.pointclouds.org/documentation/tutorials/matrix_transform.php#matrix-transform
Eigen初学相关介绍:https://blog.csdn.net/yang_q_x/article/details/52383289
逐步分析教程的代码
pcl::PointCloud<pcl::PointXYZ>::Ptr source_cloud(new pcl::PointCloud<pcl::PointXYZ>());
if (pcl::io::loadPCDFile<pcl::PointXYZ>("sample.pcd", *source_cloud) == -1)
{
PCL_ERROR("Couldn't read file sample.pcd \n");
return (-1);
}
定义点云指针容器source_cloud,加载PCD文件到source_cloud指向的堆空间。
//PointXYZ是结构体,PointCloud和Ptr是类;
//PointCloud和Ptr转换
pcl::PointCloud<pcl::PointXYZ>::Ptr cloudPointer(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ> cloud;
cloud = *cloudPointer;
cloudPointer = cloud.makeShared();
/* Reminder: how transformation matrices work :
|-------> This column is the translation
| 1 0 0 x | \
| 0 1 0 y | }-> The identity 3x3 matrix (no rotation) on the left
| 0 0 1 z | /
| 0 0 0 1 | -> We do not use this line (and it has to stay 0,0,0,1)
METHOD #1: Using a Matrix4f
This is the "manual" method, perfect to understand but error prone !
*/
Eigen::Matrix4f transform_1 = Eigen::Matrix4f::Identity();
// Define a rotation matrix (see https://en.wikipedia.org/wiki/Rotation_matrix)
float theta = M_PI / 4; // The angle of rotation in radians
transform_1(0, 0) = std::cos(theta);
transform_1(0, 1) = -sin(theta);
transform_1(1, 0) = sin(theta);
transform_1(1, 1) = std::cos(theta);
// (row, column)
// Define a translation of 2.5 meters on the x axis.
transform_1(0, 3) = 2.5;
// Print the transformation
printf("Method #1: using a Matrix4f\n");
std::cout << transform_1 << std::endl;
Matrix4f 是重定义的类
使用Identity()函数的作用:在定义变量时使用Eigen::Matrix4f x = Eigen::Matrix4f::Identity();即用单位矩阵对x变量进行了初始化。
transform_1 是绕Z轴旋转45度,沿Z轴平移2.5
/* METHOD #2: Using a Affine3f
This method is easier and less error prone
*/
Eigen::Affine3f transform_2 = Eigen::Affine3f::Identity();
// Define a translation of 2.5 meters on the x axis.
transform_2.translation() << 2.5, 0.0, 0.0;
// The same rotation matrix as before; theta radians around Z axis
transform_2.rotate(Eigen::AngleAxisf(theta, Eigen::Vector3f::UnitZ()));
// Print the transformation
printf("\nMethod #2: using an Affine3f\n");
std::cout << transform_2.matrix() << std::endl;
Affine3f 和Matrix4f 同为4*4矩阵,但是Affine3f 本质是个变换矩阵,有成员函数。
// 执行变换,并将结果保存在新创建的 transformed_cloud 中
pcl::PointCloud<pcl::PointXYZ>::Ptr transformed_cloud (new pcl::PointCloud<pcl::PointXYZ> ());
// 可以使用 transform_1 或 transform_2; t它们是一样的
pcl::transformPointCloud (*source_cloud, *transformed_cloud, transform_2);
// Visualization
printf("\nPoint cloud colors : white = original point cloud\n"
" red = transformed point cloud\n");
//创建可视化窗口,名字:Matrix transformation example
pcl::visualization::PCLVisualizer viewer("Matrix transformation example");
// a specific PointCloud visualizer handler for colors
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> source_cloud_color_handler(source_cloud, 255, 255, 255);
// Add a Point Cloud (templated) to screen.使用 visualizer handler for colors
viewer.addPointCloud(source_cloud, source_cloud_color_handler, "original_cloud");
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> transformed_cloud_color_handler(transformed_cloud, 230, 20, 20); // Red
viewer.addPointCloud(transformed_cloud, transformed_cloud_color_handler, "transformed_cloud");
//Adds 3D axes describing a coordinate system to screen at 0,0,0.//
viewer.addCoordinateSystem(1.0, "cloud", 0);
//添加背景颜色
viewer.setBackgroundColor(0.05, 0.05, 0.05, 0); // Setting background to a dark grey
//设置点云渲染属性
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "original_cloud");
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "transformed_cloud");
//viewer.setPosition(400, 400); // Setting visualiser window position
while (!viewer.wasStopped()) { // 按q函数返回0,关闭窗口。否则返回1
viewer.spinOnce();//延时时间默认为1ms spinOnce(int time = 1,bool force_redraw = false)
}
return 0;
}