基础理论专栏目录 - 知乎 (zhihu.com)
凸包问题——概述 - 知乎 (zhihu.com)
1、安装PCL
安装pcl,我的是window10,vs2019。我安装的是1.13
win10系统下 VS2019点云库PCL1.12.0的安装与配置_windows 10使用pcl-CSDN博客
照着上述博客进行配置,再结合这个设置环境变量pcl1.8.0+vs2013环境配置(详细)_pcl:1.8.0 vs:2013 qt:5.9.0-CSDN博客
我对bat进行了修改,寻找lib文件,排除d.lib。旨在寻找release的lib
@echo off
(for %%I in (*.lib) do (
echo %%I | findstr /i /c:"d.lib" >nul
if errorlevel 1 echo %%I
)) >> release.txtpause
我这边做了dll环境变量配置,但找不到dll,我就直接配置在项目的生成路径中了,然后就可以找到,不清楚啥原因,反正先学再说
测试代码
#include <pcl/visualization/cloud_viewer.h>
#include <iostream>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <iostream>
#include <pcl/filters/filter.h>
int user_data;
void
viewerOneOff(pcl::visualization::PCLVisualizer& viewer)
{
viewer.setBackgroundColor(1.0, 0.5, 1.0);
pcl::PointXYZ o;
o.x = 1.0;
o.y = 0;
o.z = 0;
viewer.addSphere(o, 0.25, "sphere", 0);
std::cout << "i only run once" << std::endl;
}
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++;
}
int
main()
{
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGBA>);
pcl::io::loadPCDFile("test_cloud.pcd", *cloud);
pcl::visualization::CloudViewer viewer("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())
{
std::cout << cloud->width << endl;
std::cout << cloud->height << endl;
//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++;
}
return 0;
}
2、三维凸包
用不严谨的话来讲,给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边形,它能包含点集中所有的点。
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/convex_hull_3.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron_3;
int main() {
// 加载三维PCD点云数据
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ>(R"(C:\chenqi\CGAL\MeshSimplification\data\chef.pcd)", *cloud) == -1) {
PCL_ERROR("Could not read file\n");
return -1;
}
// 转换为CGAL支持的格式
std::vector<Kernel::Point_3> points;
for (const auto& point : cloud->points) {
points.push_back(Kernel::Point_3(point.x, point.y, point.z));
}
// 计算三维凸包
Polyhedron_3 poly;
CGAL::convex_hull_3(points.begin(), points.end(), poly);
// 输出凸包的顶点数量
std::cout << poly.size_of_vertices() << " points on the convex hull" << std::endl;
// 打开文件用于写入
std::ofstream out_file(R"(C:\chenqi\CGAL\MeshSimplification\data\chef.xyz)");
if (!out_file) {
std::cerr << "Failed to open output file." << std::endl;
return -1;
}
// 遍历Polyhedron_3中的每个顶点
for (Polyhedron_3::Vertex_const_iterator it = poly.vertices_begin(); it != poly.vertices_end(); ++it) {
// 获取当前顶点的坐标
Kernel::Point_3 p = it->point();
// 将顶点坐标写入文件
out_file << p.x() << " " << p.y() << " " << p.z() << std::endl;
}
// 关闭文件
out_file.close();
std::cout << "Convex hull vertices saved to 'convex_hull_vertices.xyz'" << std::endl;
return 0;
}
CGAL 二维点集的凸包提取_二维凸包提取-CSDN博客
CGAL 快速构建三维凸包_bcn三元凸包图-CSDN博客