李良庭
2025-04-22 5c8762f1f209e2781184a7816bbd3b6af78c0188
cloud_viewer.cpp
@@ -1,67 +1,50 @@
// cloud_viewer.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#define PCL_SILENCE_MALLOC_WARNING 1
#define PCL_SILENCE_MALLOC_WARNING 1
#include <pcl/visualization/cloud_viewer.h>
#include <iostream>
//#include <pcl/io/io.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <atomic>
#include <thread>
int user_data;
std::atomic<int> user_data(0);
void viewerOneOff(pcl::visualization::PCLVisualizer& viewer)
{
    viewer.setBackgroundColor(0, 0, 0);
    std::cout << "i only run once" << std::endl;
void viewerOneOff(pcl::visualization::PCLVisualizer& viewer) {
    viewer.setBackgroundColor(0.1, 0.1, 0.1);  // 深灰背景更易观察
    viewer.addCoordinateSystem(1.0);           // 添加坐标系
    viewer.initCameraParameters();             // 初始化相机参数
    std::cout << "Initial setup complete." << std::endl;
}
void viewerPsycho(pcl::visualization::PCLVisualizer& viewer)
{
void viewerPsycho(pcl::visualization::PCLVisualizer& viewer) {
    static unsigned count = 0;
    std::stringstream ss;
    ss << "Once per viewer loop: " << count++;
    viewer.removeShape("text", 0);
    viewer.addText(ss.str(), 200, 300, "text", 0);
    //FIXME: possible race condition here:
    user_data++;
    viewer.removeShape("text");
    viewer.addText("Frame: " + std::to_string(count++), 20, 30, 20, 1, 1, 1, "text");
    user_data.fetch_add(1, std::memory_order_relaxed);
}
int main()
{
int main() {
    // 加载点云(带错误处理)
    pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGBA>);
    pcl::io::loadPCDFile("sampledata.pcd", *cloud);
    if (pcl::io::loadPCDFile<pcl::PointXYZRGBA>("sampledata.pcd", *cloud) == -1) {
        std::cerr << "Error: Failed to load PCD file!" << std::endl;
        return -1;
    }
    pcl::visualization::CloudViewer viewer("Cloud Viewer");
    // 创建可视化器
    pcl::visualization::CloudViewer viewer("Enhanced Cloud Viewer");
    //blocks until the cloud is actually rendered
    // 显示点云
    viewer.showCloud(cloud);
    //use the following functions to get access to the underlying more advanced/powerful
    //PCLVisualizer
    //This will only get called once
    // 设置回调函数
    viewer.runOnVisualizationThreadOnce(viewerOneOff);
    //This will get called once per visualization iteration
    viewer.runOnVisualizationThread(viewerPsycho);
    while (!viewer.wasStopped())
    {
        //you can also do cool processing here
        //FIXME: Note that this is running in a separate thread from viewerPsycho
        //and you should guard against race conditions yourself...
        user_data++;
    // 主循环
    while (!viewer.wasStopped()) {
        // 示例处理逻辑(线程安全)
        user_data.fetch_add(1, std::memory_order_relaxed);
        std::this_thread::sleep_for(std::chrono::milliseconds(50));
    }
    return 0;
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧:
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
}