https://github.com/MNewBie/PCL-Notes/blob/master/chapter9.md

    表面法线是几何体表面的重要属性,属于特征描述范畴,由于下章关键点需要此操作,所以提前给出用法。

    class pcl::NormalEstimation< PointInT, PointOutT >
    NormalEstimation estimates local surface properties (surface normals and curvatures)at each 3D point. More…
    1. #include <pcl/io/pcd_io.h>
    2. #include <pcl/point_types.h>
    3. // 包含相关头文件
    4. #include <pcl/features/normal_3d.h>
    5. #include <pcl/visualization/pcl_visualizer.h>
    6. typedef pcl::PointXYZ PointT;
    7. typedef pcl::PointNormal PointNT; // 也可以pcl::Normal,但无法用PCLVisualizer显示。
    8. int main(int argc, char** argv)
    9. {
    10. // 读取点云
    11. pcl::PointCloud<PointT>::Ptr cloud(new pcl::PointCloud<PointT>);
    12. pcl::io::loadPCDFile(argv[1], *cloud);
    13. // 计算法向量
    14. pcl::NormalEstimation<PointT, PointNT> nest;
    15. //nest.setRadiusSearch(0.01); // 设置拟合时邻域搜索半径,最好用模型分辨率的倍数
    16. nest.setKSearch(50); // 设置拟合时采用的点数
    17. nest.setInputCloud(cloud);
    18. pcl::PointCloud<PointNT>::Ptr normals(new pcl::PointCloud<PointNT>);
    19. nest.compute(*normals);
    20. for (size_t i = 0; i < cloud->points.size(); ++i)
    21. { // 生成时只生成了法向量,没有将原始点云信息拷贝,为了显示需要复制原信息
    22. // 也可用其他方法进行连接,如:pcl::concatenateFields
    23. normals->points[i].x = cloud->points[i].x;
    24. normals->points[i].y = cloud->points[i].y;
    25. normals->points[i].z = cloud->points[i].z;
    26. }
    27. // 显示
    28. pcl::visualization::PCLVisualizer viewer;
    29. viewer.addPointCloud(cloud, "cloud");
    30. int level = 100; // 多少条法向量集合显示成一条
    31. float scale = 0.01; // 法向量长度
    32. viewer.addPointCloudNormals<PointNT>(normals, level, scale, "normals");
    33. viewer.spin();
    34. system("pause");
    35. return 0;
    36. }