#include "stdafx.h"
#include <iostream>
#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>
/*输入输入点云,输出点云地址和分割平面方程,返回旋转完的点云,可直接xy面z轴0值分割*/
pcl::PointCloud<pcl::PointXYZRGBA> Rotate(pcl::PointCloud<pcl::PointXYZRGBA> input,double A,double B,double C, pcl::PointCloud<pcl::PointXYZRGBA> &transformed_cloud)
{
//点云旋转,基于平面标准式法向量原理
//先绕x旋转,再绕y旋转
/* 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_x = Eigen::Matrix4f::Identity();
/* x转移矩阵
|1 0 0 |
|0 cos -sin |
|0 sin cos |
*/
double cosx, sinx;
double dis = sqrt(pow(A, 2) + pow(B, 2) + pow(C, 2));
cosx = sqrt(pow(A, 2) + pow(C, 2))/dis;
sinx = B / dis;
transform_x(1, 1) = cosx;
transform_x(1, 2) = -sinx;
transform_x(2,1) = sinx;
transform_x(2,2) = cosx;
Eigen::Matrix4f transform_y = Eigen::Matrix4f::Identity();
/* y转移矩阵
|cos 0 sin |
|0 1 0 |
|-sin 0 cos |
*/
double cosy, siny;
cosy = C/dis;
siny = sqrt(pow(A,2)+pow(B,2))/dis;
// Define a rotation matrix (see https://en.wikipedia.org/wiki/Rotation_matrix)
transform_y(0, 0) = cosy;
transform_y(0, 2) = siny;
transform_y(2, 0) = -siny;
transform_y(2, 2) = cosy;
pcl::PointCloud<pcl::PointXYZRGBA> transformedx_cloud;
pcl::transformPointCloud(input,transformedx_cloud, transform_x);
pcl::transformPointCloud(transformedx_cloud, transformed_cloud, transform_y);
return transformed_cloud;
转:PCL点云旋转(平面参数) | 码农家园