zl程序教程

您现在的位置是:首页 >  其它

当前栏目

CGAL 快速构建三维凸包

快速 构建 三维 凸包
2023-09-14 09:15:11 时间

一、三维凸包

  和二维凸包类似,给定一堆三维空间中的点,包含它们的最小凸多面体称为这些点的凸包。

二、代码实现

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/convex_hull_3.h>
#include <CGAL/Surface_mesh.h>       // .ply格式
#include <vector>
#include <fstream>


typedef CGAL::Exact_predicates_inexact_constructions_kernel  K;
typedef CGAL::Polyhedron_3<K>                     Polyhedron_3;
typedef K::Point_3                                Point_3;
typedef CGAL::Surface_mesh<Point_3>               Surface_mesh;


int main(int argc, char* argv[])
{
	std::string inFile("E://data//bunny.txt");
	std::string outFile = "cgal//out.ply";
	std::ifstream in(inFile);
	std::vector<Point_3> points;
	Point_3 p;
	while (in >> p) 
	{
		points.push_back(p);
	}

	// define polyhedron to hold convex hull
	Polyhedron_3 poly;

	// compute convex hull of non-collinear points
	CGAL::convex_hull_3(points.begin(), points.end(), poly);

	std::cout << "The convex hull contains " << poly.size_of_vertices() << " vertices" << std::endl;

	Surface_mesh sm;
	CGAL::convex_hull_3(points.begin(), points.end(), sm);

	std::cout << "The convex hull contains " << num_vertices(sm) << " vertices" << std::endl;
	// ---------------------------结果保存成.ply------------------------------
	std::ofstream f(outFile, std::ios_base::binary);
	CGAL::IO::set_binary_mode(f);
	CGAL::IO::write_PLY(f, sm);
	f.close();
	

	return 0;
}

三、结果展示

在这里插入图片描述

四、结论

快速构建算法确实很快,比PCL里的算法快了将近10倍!!!!