PointCloudLibrary / pcl

Point Cloud Library (PCL)
https://pointclouds.org/
Other
9.64k stars 4.59k forks source link

[custom] chatGPT code on pcl visualization : different behavior between pcl::visualization::PCLVisualizer::Ptr and pcl::visualization::PCLVisualizer #6027

Closed hitbuyi closed 1 month ago

hitbuyi commented 1 month ago

I use chatGPT to generate PCL code on show, my task is very simple, to visualize a lidar cloud point,the color is according to lidar's intensity. Below two versions of code are generated by chatGPT, my enviroments are

OS: ubuntu20.04
PCL:1.14(built from source)
VTK: 9.1.0

v1: use pcl::visualization::PCLVisualizer::Ptr, it can display lidar points in white-black, without color

    //create point cloud,the two versions are the same 
    pcl::PointCloud<pcl::PointXYZI>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZI>);
    float x, y, z, intensity;
    while (input_file.read(reinterpret_cast<char*>(&x), sizeof(float)) &&
           input_file.read(reinterpret_cast<char*>(&y), sizeof(float)) &&
           input_file.read(reinterpret_cast<char*>(&z), sizeof(float)) &&
           input_file.read(reinterpret_cast<char*>(&intensity), sizeof(float))) {
        pcl::PointXYZI point;
        point.x = x;
        point.y = y;
        point.z = z;
        point.intensity = intensity;
        cloud->push_back(point);
    }
    input_file.close();

    // Create PCL visualizer
    pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("Cloud Viewer"));
    viewer->setBackgroundColor(0, 0, 0);
    viewer->addPointCloud<pcl::PointXYZI>(cloud, "cloud");
    // Colorize points based on intensity
    pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZI> intensity_color(cloud, "intensity");
    viewer->addPointCloud<pcl::PointXYZI>(cloud, intensity_color, "cloud");
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud");

    // Run visualization
    while (!viewer->wasStopped()) {
        viewer->spinOnce(100);
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
    return 0;

the result is v1

v2: use,pcl::visualization::PCLVisualizer, it can display lidar points with color

    //create point cloud,the two versions are the same 
    pcl::PointCloud<pcl::PointXYZI>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZI>);
    float x, y, z, intensity;
    while (input_file.read(reinterpret_cast<char*>(&x), sizeof(float)) &&
           input_file.read(reinterpret_cast<char*>(&y), sizeof(float)) &&
           input_file.read(reinterpret_cast<char*>(&z), sizeof(float)) &&
           input_file.read(reinterpret_cast<char*>(&intensity), sizeof(float))) {
        pcl::PointXYZI point;
        point.x = x;
        point.y = y;
        point.z = z;
        point.intensity = intensity;
        cloud->push_back(point);
    }
    input_file.close();
   // Step 2: Assign colors based on intensity
    pcl::visualization::PCLVisualizer viewer("Cloud Viewer");
    viewer.setBackgroundColor(0, 0, 0);
    pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZI> intensity_color(cloud, "intensity");
    viewer.addPointCloud<pcl::PointXYZI>(cloud, intensity_color, "cloud");
    viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud");

    // Step 3: Visualize the point cloud
    while (!viewer.wasStopped()) {
        viewer.spinOnce(100);
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
    return 0;

the result is v2

Why this happen? I think they are the same, just one use pointer, the other not.

mvieth commented 1 month ago

@hitbuyi The first version calls addPointCloud twice, the first time without the handler. The second call to addPointCloud is ignored because the id "cloud" is the same. You actually get a warning from PCL: [addPointCloud] The id <cloud> already exists! Please choose a different id and retry. Please be aware that chatGPT often produces complete garbage. Do not blindly trust it, double check everything. We can not provide any support for chatGPT generated code.

hitbuyi commented 1 month ago

@hitbuyi The first version calls addPointCloud twice, the first time without the handler. The second call to addPointCloud is ignored because the id "cloud" is the same. You actually get a warning from PCL: [addPointCloud] The id <cloud> already exists! Please choose a different id and retry. Please be aware that chatGPT often produces complete garbage. Do not blindly trust it, double check everything. We can not provide any support for chatGPT generated code.

It solved my problem