1. 从点云创建深度图

#include <pcl/range_image/range_image.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/range_image_visualizer.h>

int main(int argc, char** argv){
    pcl::PointCloud<pcl::PointXYZ>::Ptr PointCloudPtr(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::PointCloud<pcl::PointXYZ> &PointCloud = *PointCloudPtr;

    pcl::io::loadPCDFile("../bunny.pcd", PointCloud); // 加载数据

    float angular_resolution = (float)(1.0f * (M_PI / 180.0f) ); // 深度传感器的角度分辨率,即深度图像中一个像素对应的角度
    float maxAngleWidth = (float)(360.0f * (M_PI / 180.0f)); // 水平最大采样角度宽度
    float maxAngleHeight = (float)(180.0f * (M_PI / 180.0f)); // 垂直最大采样角度宽度

    Eigen::Affine3f sensorPose = (Eigen::Affine3f) Eigen::Translation3f(0.0f, 0.0f, 0.0f); // 深度传感器的采集位置

    // 深度传感器遵循的坐标系统
    pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::CAMERA_FRAME; // 深度传感器遵循的坐标系统
    float noiseLevel = 0.00; // 获取深度图像时,近邻点对查询距离值的影响水平
    float minRange = 0.0f; // 深度图像中,距离小于该值的点将被忽略
    int borderSize = 1; // 深度图像边界大小

    // boost::shared_ptr<pcl::RangeImage> range_image_ptr(new pcl::RangeImage); // 可视化
    const std::shared_ptr<pcl::RangeImage> range_image_ptr(new pcl::RangeImage);
    pcl::RangeImage &rangeImage = *range_image_ptr;

    rangeImage.createFromPointCloud(PointCloud, angular_resolution, maxAngleWidth, maxAngleHeight, sensorPose, coordinate_frame, noiseLevel, minRange, borderSize);

    // -------------------------------------------------------------------------
    // 输出
    std::cout << rangeImage << std::endl;
    pcl::visualization::PCLVisualizer viewer("3D Viewer");
    viewer.setBackgroundColor(1, 1, 1);

    // 添加深度点云
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointWithRange> range_image_color_handler(range_image_ptr, 0, 0, 0);

    viewer.addPointCloud(range_image_ptr, range_image_color_handler, "range image");
    viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 4, "range image");

    // 添加原始点云
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> org_image_color_handler(PointCloudPtr, 255, 100, 0);
    viewer.addPointCloud(PointCloudPtr,org_image_color_handler, "org image");
    viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "org image");

    viewer.initCameraParameters();
    viewer.addCoordinateSystem(1.0);

    while (!viewer.wasStopped()) {
         viewer.spin(); 
         pcl_sleep(0.01);
    }

    return 0;
}