一、目的
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));
}
}


623

被折叠的 条评论
为什么被折叠?



