文档说明:
该函数可以实现点云的变换(包括平移变换和旋转变换)
具体操作如下:
1.需要传入的文件类型是.pcd或者.ply的文件
2.在命令行窗口运行:
需要在命令行窗口输入6个参数:前3个参数是平移参数,后三个参数是旋转参数:
{ 参数1:沿x轴平移} {参数2:沿y轴平移} { 参数3:沿z轴平移}
{ 参数4:绕x轴旋转} { 参数5: 绕y轴旋转} {参数6:绕z轴旋转}
该图就是绕x轴旋转0度,绕y轴旋转45度,绕z轴旋转45度;然后沿着x轴平移20个单位,沿着y轴平移20个单位,沿着z轴平移0个单位;
该图就是按照上述参数变换后的的展示。
#代码如图所示
#include <iostream>
// TODO: 在此处引用程序需要的其他标头。
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_cloud.h>
#include <pcl/console/parse.h>
#include <pcl/common/transforms.h>
#include <pcl/visualization/pcl_visualizer.h>
#include<stdlib.h>
using namespace pcl;
// This is the main function
int main(int argc,char**argv)
{
float a, b, c, d, e, f;
a = atoi(argv[1]);
b = atoi(argv[2]);
c = atoi(argv[3]);
d = atoi(argv[4]);
e = atoi(argv[5]);
f = atoi(argv[6]);
// Load file | Works with PCD and PLY files
PointCloud<PointXYZ>::Ptr source_cloud(new pcl::PointCloud<pcl::PointXYZ>());
char strfilepath[256] = "E:\\PCL\\my_pcl\\x64\\Debug\\rabbit.pcd";
pcl::io::loadPCDFile(strfilepath, *source_cloud);
// Define a rotation matrix (see https://en.wikipedia.org/wiki/Rotation_matrix)
float theta = (M_PI / 180)*d; // The angle of rotation in radians
float bata = (M_PI / 180)*e; // The angle of rotation in radians
float gama = (M_PI / 180)*f; // The angle of rotation in radians
/* METHOD #2: Using a Affine3f
This method is easier and less error prone
*/
Eigen::Affine3d transform_2 = Eigen::Affine3d::Identity();
// Define a translation of 2.5 meters on the x axis.
//从后往前旋转,先绕着X轴逆时针theta, 再绕着Y逆时针bata,再绕着X逆时针gamma
//Eigen::Vector3d(1,0,0)和Eigen::Vector3d::UnitX()一样
transform_2 = Eigen::AngleAxisd(gama, Eigen::Vector3d(0, 0, 1)) *
Eigen::AngleAxisd(bata, Eigen::Vector3d::UnitY()) *
Eigen::AngleAxisd(theta, Eigen::Vector3d::UnitX());
transform_2.translation() << a, b, c;
// Print the transformation
printf("\nMethod : using an Affine3d\n");
std::cout << transform_2.matrix() << std::endl;
// Executing the transformation
pcl::PointCloud<pcl::PointXYZ>::Ptr transformed_cloud(new pcl::PointCloud<pcl::PointXYZ>());
// You can either apply transform_1 or transform_2; they are the same
pcl::transformPointCloud(*source_cloud, *transformed_cloud, transform_2);
// Visualization
printf("\nPoint cloud colors : white = original point cloud\n"
" red = transformed point cloud\n");
pcl::visualization::PCLVisualizer viewer("Matrix transformation example");
// Define R,G,B colors for the point cloud
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> source_cloud_color_handler(source_cloud, 255, 255, 255);
// We add the point cloud to the viewer and pass the color handler
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");
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(800, 200); // Setting visualiser window position
while (!viewer.wasStopped()) { // Display the visualiser until 'q' key is pressed
viewer.spinOnce();
}
return 0;
}