1 运行结果
生成的凸包点与原点云的可视化
2 代码实现
// convex hull
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/vtk_io.h>
#include <pcl/surface/convex_hull.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/surface/gp3.h>
#include <pcl/surface/organized_fast_mesh.h>
#include <pcl/features/normal_3d.h>
#include <pcl/filters/project_inliers.h>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in(new pcl::PointCloud<pcl::PointXYZ>); //存放读取点云的对象
pcl::PCDReader reader; //定义点云读取对象
if (reader.read(".\\input\\Kuangshan_long.pcd", *cloud_in) < 0)
{
PCL_ERROR("\a->The file does not exist.\n");
system("pause");
return -1;
}
cout << "You have loaded " << cloud_in->points.size() << " points. " << endl;
//=========================创建凸包可视化==========================
pcl::ConvexHull<pcl::PointXYZ> convex_hull;
convex_hull.setInputCloud(cloud_in);
pcl::PointCloud<pcl::PointXYZ>::Ptr hull_out(new pcl::PointCloud<pcl::PointXYZ>);
convex_hull.reconstruct(*hull_out);
pcl::visualization::PCLVisualizer viewer("ConvexHull Vis");
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> color(cloud_in, 255, 0, 0);
viewer.addPointCloud<pcl::PointXYZ>(cloud_in, color, "cloud_in");
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> color_cx(hull_out, 0, 255, 0);
viewer.addPointCloud<pcl::PointXYZ>(hull_out, color_cx, "hull_out");
for (size_t i = 0; i < hull_out->size(); ++i)
{
size_t next_index = (i + 1) % hull_out->size();
pcl::PointXYZ point1 = hull_out->points[i];
pcl::PointXYZ point2 = hull_out->points[next_index];
viewer.addLine<pcl::PointXYZ>(point1, point2, 255, 255, 255, "line" + std::to_string(i));
}
viewer.setBackgroundColor(0, 0, 0);
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud_in");
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "hull_out");
//获取当前时间
std::time_t now = std::time(nullptr);
//时间转化为本地时间
std::tm* localTime = std::localtime(&now);
int year_lt = localTime->tm_year + 1900;
int month_lt = localTime->tm_mon + 1;
int day_lt = localTime->tm_mday;
int ymd_lt = year_lt * 10000 + month_lt * 100 + day_lt;
std::string ymd_str = std::to_string(ymd_lt);
///保存为PCD格式
if (!hull_out->empty())
{
pcl::io::savePCDFile(".\\output\\0912\\ConvexHull_result" + ymd_str + ".pcd", *hull_out);
cout << "->(ASICC)The number of PointHull saved is: " << hull_out->points.size() << endl;
}
else
{
PCL_ERROR("\a->保存点云为空!\n");
system("pause");
return -1;
}
cout << "Process done!" << endl;
while (!viewer.wasStopped())
{
viewer.spinOnce();
}
return 0;
}