Qt中利用PCL显示点云和面,并进行点云变换,面变换

低功耗蓝牙项目,需要一块懂省电的板

思澈 SF32LB52 芯片,BLE 协议栈深度优化,上手即开发

一、目的
1. PCL如何读取obj文件,并转换成点云数据,obj文件解析
2. 点云显示,点云变换更新显示,颜色设置
3. 面(网格)数据显示,点(网格)移动,颜色设置

二、安装背景
Qt 正确安装,且成功配置点云库
[QT中PCL配置] (https://blog.csdn.net/hss468/article/details/80487372)

三、 编写程序
1.创建简单Qt控制台程序,并配置PCL
QT中PCL配置] (https://blog.csdn.net/hss468/article/details/80487372)

2.编写程序读取点云数据(本人用到点云面显示,故数据保存在obj文件中)

    std::string fileName = "chahu.obj";//文件名称
    pcl::PolygonMesh meshData;//读取原始面、网格数据
    pcl::io::loadPolygonFile(fileName,meshData);

    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;//点云数据
    cloud.reset(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::fromPCLPointCloud2(meshData.cloud, *cloud);//将面、网格数据转换为点云数据
obj文件数据解析

这里写图片描述

3 点云显示,点云变换显示,并设置显示颜色(设置点云类型为pcl::PointXYZRGB),若不设置显示颜色(或设置点云类型pcl::PointXYZ),则默认显示白色

#include <QCoreApplication>

#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/obj_io.h>
#include <pcl/PolygonMesh.h>
#include <pcl/point_cloud.h>
#include <pcl/io/vtk_lib_io.h>//loadPolygonFileOBJ所属头文件;
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/common/transforms.h>
#include <pcl/registration/transformation_estimation_svd.h>

int main()
{
    //读取***************************************************************************************************
    std::string fileName = "chahu.obj";//文件名称
    pcl::PolygonMesh meshData;//读取原始面、网格数据
    pcl::io::loadPolygonFile(fileName,meshData);
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud;//点云数据
    cloud.reset(new pcl::PointCloud<pcl::PointXYZRGB>);
    pcl::fromPCLPointCloud2(meshData.cloud, *cloud);//将面、网格数据转换为点云数据
    for (int i=0; i<cloud->size(); i++)//设置显示颜色
    {
        cloud->points[i].r = 0;
        cloud->points[i].g = 255;
        cloud->points[i].b = 0;
    }

    //进行变换*************************************************************************************************
    Eigen::Matrix4f transform = Eigen::Matrix4f::Identity();
    float theta = M_PI/4; // The angle of rotation in radians
    transform (0,0) = cos (theta);
    transform (0,1) = -sin(theta);
    transform (1,0) = sin (theta);
    transform (1,1) = cos (theta);
    transform (0,3) = 2.5;
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr transformed_cloud (new pcl::PointCloud<pcl::PointXYZRGB> ());
    pcl::transformPointCloud (*cloud, *transformed_cloud, transform);

    // 显示结果图************************************************************************************************
    boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
    viewer->setBackgroundColor (0, 0, 0); //设置背景
    viewer->addCoordinateSystem (15.0); //设置坐标系
    viewer->initCameraParameters ();

    viewer->addPointCloud(cloud, "cloud");//显示点
    viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "cloud"); 
    int tag =0;
    while (!viewer->wasStopped())
    {
        if (tag == 0)
        {
            viewer->updatePointCloud(transformed_cloud, "cloud");
            tag = 1;
        }
        else
        {
            viewer->updatePointCloud(cloud, "cloud");
            tag = 0;
        }

        viewer->spinOnce (100);
        boost::this_thread::sleep (boost::posix_time::microseconds (100000));
    }
}

这里写图片描述

4.面显示,面变换显示,并设置显示颜色(设置点云类型为pcl::PointXYZRGB),若不设置显示颜色(或设置点云类型pcl::PointXYZ),则默认显示白色

#include <QCoreApplication>

#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/obj_io.h>
#include <pcl/PolygonMesh.h>
#include <pcl/point_cloud.h>
#include <pcl/io/vtk_lib_io.h>//loadPolygonFileOBJ所属头文件;
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/common/transforms.h>
#include <pcl/registration/transformation_estimation_svd.h>

int main()
{
    //读取***************************************************************************************************
    std::string fileName = "chahu.obj";//文件名称
    pcl::PolygonMesh meshData;//读取原始面、网格数据
    pcl::io::loadPolygonFile(fileName,meshData);
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud;//点云数据
    cloud.reset(new pcl::PointCloud<pcl::PointXYZRGB>);
    pcl::fromPCLPointCloud2(meshData.cloud, *cloud);//将面、网格数据转换为点云数据
    for (int i=0; i<cloud->size(); i++)//设置显示颜色
    {
        cloud->points[i].r = 0;
        cloud->points[i].g = 255;
        cloud->points[i].b = 0;
    }

    //进行变换*************************************************************************************************
    Eigen::Matrix4f transform = Eigen::Matrix4f::Identity();
    float theta = M_PI/4; // The angle of rotation in radians
    transform (0,0) = cos (theta);
    transform (0,1) = -sin(theta);
    transform (1,0) = sin (theta);
    transform (1,1) = cos (theta);
    transform (0,3) = 2.5;
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr transformed_cloud (new pcl::PointCloud<pcl::PointXYZRGB> ());
    pcl::transformPointCloud (*cloud, *transformed_cloud, transform);

    // 显示结果图************************************************************************************************
    boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
    viewer->setBackgroundColor (0, 0, 0); //设置背景
    viewer->addCoordinateSystem (15.0); //设置坐标系
    viewer->initCameraParameters ();

    viewer->addPolygonMesh<pcl::PointXYZRGB> (cloud, meshData.polygons, "mesh"); //显示面*****

    int tag =0;
    while (!viewer->wasStopped())
    {
        if (tag == 0)
        {
            viewer->updatePolygonMesh<pcl::PointXYZRGB>(transformed_cloud, meshData.polygons, "mesh");
            tag = 1;
        }
        else
        {
            viewer->updatePolygonMesh<pcl::PointXYZRGB>(cloud, meshData.polygons, "mesh");
            tag = 0;
        }

        viewer->spinOnce (100);
        boost::this_thread::sleep (boost::posix_time::microseconds (100000));
    }
}

这里写图片描述

低功耗蓝牙项目,需要一块懂省电的板

思澈 SF32LB52 芯片,BLE 协议栈深度优化,上手即开发

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值