我有一个点云,已知包含地面。这个点云的方向未知,且不在原点(0,0,0)上。我需要:
- 将
floor_plane
移动到XY平面上,让其位于XY平面上。 - 将
floor_plane
的质心移动到原点(0,0,0)
。
- 使用RANSAC算法获取
floor_plane
的平面系数。前三个系数对应于floor_plane
的法线。 - 生成XY平面的法向量。它的x=0,y=0,z=1。
- 计算地面平面的法向量和XY平面的法向量的叉积,得到旋转矢量(轴),即一个单位向量。
- 计算旋转角度。两个平面之间的夹角等于两个法向量之间的夹角。从点积的定义中,我们可以提取出两个法向量之间的角度。在XY平面的情况下,它等于
theta=acos(C/sqrt(A^2+B^2+C^2)
,其中A、B、C是floor_plane
的前三个系数。 - 生成旋转矩阵(3x3)或四元数。在维基百科中查找公式。
- 找到
floor_plane
的质心。取负以生成平移向量。 - 使用transformPointCloud(cloud,transformed,transformationMatrix)简单地应用变换。
// Find the planar coefficients for floor plane
pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
pcl::PointIndices::Ptr floor_inliers (new pcl::PointIndices);
pcl::SACSegmentation<pcl::PointXYZRGB> seg;
seg.setOptimizeCoefficients (true);
seg.setModelType (pcl::SACMODEL_PLANE);
seg.setMethodType (pcl::SAC_RANSAC);
seg.setDistanceThreshold (0.01);
seg.setInputCloud (floor_plane);
seg.segment (*floor_inliers, *coefficients);
std::cerr << "Floor Plane Model coefficients: " << coefficients->values[0] << " "
<< coefficients->values[1] << " "
<< coefficients->values[2] << " "
<< coefficients->values[3] << std::endl;
Eigen::Matrix<float, 1, 3> floor_plane_normal_vector, xy_plane_normal_vector, rotation_vector;
floor_plane_normal_vector[0] = coefficients->values[0];
floor_plane_normal_vector[1] = coefficients->values[1];
floor_plane_normal_vector[2] = coefficients->values[2];
std::cout << floor_plane_normal_vector << std::endl;
xy_plane_normal_vector[0] = 0.0;
xy_plane_normal_vector[1] = 0.0;
xy_plane_normal_vector[2] = 1.0;
std::cout << xy_plane_normal_vector << std::endl;
rotation_vector = xy_plane_normal_vector.cross (floor_plane_normal_vector);
std::cout << "Rotation Vector: "<< rotation_vector << std::endl;
float theta = acos(floor_plane_normal_vector.dot(xy_plane_normal_vector)/sqrt( pow(coefficients->values[0],2)+ pow(coefficients->values[1],2) + pow(coefficients->values[2],2)));
Eigen::Affine3f transform_2 = Eigen::Affine3f::Identity();
transform_2.translation() << 0, 0, 30;
transform_2.rotate (Eigen::AngleAxisf (theta, rotation_vector));
std::cout << "Transformation matrix: " << std::endl << transform_2.matrix() << std::endl;
pcl::transformPointCloud (*cloud, *centeredCloud, transform_2);