【CGAL_IO】读取并显示STL模型

前言

CGAL确实实现了对于STL文件的读写接口,但是都是以polygon soup的形式读入的。polygon soup是一个没有全局信息的多边形集合,分别存放在两个容器中:一个存储点(PointRange),一个存储由点索引组成的面(PolygonRange)。

image-20220810214137472

若要让读入的STL模型数据能够供CGAL算法使用,一般需要将polygon soup转换为polygon mesh,后者具有一致性和可定向性。

而这个转换过程一版会用到两个函数:

orient_polygon_soup()

将多边形的方向统一

polygon_soup_to_polygon_mesh())

将方向统一后的polygon soup转化为polygon mesh

测试代码

#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Polygon_mesh_processing.h>
#include <CGAL/IO/STL.h>
#include <CGAL/draw_polyhedron.h>
using namespace std;
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron_3;

int main()
{
	std::string stl_file_name = "data/simple80_complete.stl";

	Polyhedron_3 poly_Partition;
	std::vector<CGAL::cpp11::array<double, 3> > points;
	std::vector<CGAL::cpp11::array<int, 3> > triangles;

	CGAL::IO::read_STL(stl_file_name, points, triangles);

	cout << "number of points in soup : " << points.size() << "\n";
	cout << "number of triangles in soup : " << triangles.size() << "\n";

	CGAL::Polygon_mesh_processing::orient_polygon_soup(points, triangles);
	CGAL::Polygon_mesh_processing::polygon_soup_to_polygon_mesh(points, triangles, poly_Partition);
	
	cout << "number of points : " << points.size() << "\n";
	cout << "number of triangles : " << triangles.size() << "\n";
	cout << "number of facets : " << poly_Partition.size_of_facets() << "\n";

	CGAL::draw(poly_Partition);

	return EXIT_SUCCESS;
}

image-20220810203125774

image-20220810203154591

可以看到,经过转换,模型中顶点的数目增加了。我的理解是其将原本的非流形表面转化为了流形表面,所以三角面片数量不变,但顶点变多了。

一步到位

image-20220810213948145

上述是CGAL官方文档的描述,为了方便,可以使用 CGAL::Polygon_mesh_processing::IO::read_polygon_mesh()一步到位。

#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/draw_polyhedron.h>
#include <CGAL/Polygon_mesh_processing/IO/polygon_mesh_io.h>
using namespace std;
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron_3;

int main()
{
	std::string stl_file_name = "data/simple80_complete.stl";

	Polyhedron_3 poly_Partition;
	CGAL::Polygon_mesh_processing::IO::read_polygon_mesh(stl_file_name, poly_Partition);

	CGAL::draw(poly_Partition);

	return EXIT_SUCCESS;
}
  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CGAL(Computational Geometry Algorithms Library)是一个用于计算几何学算法的开源库,而PCL(Point Cloud Library)是一个用于点云处理的开源库。要在CGAL读取PCL库中的点云数据,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了CGAL和PCL库,并且配置正确。 2. 在你的代码中引入CGAL和PCL的相关头文件。例如: ```cpp #include <CGAL/Exact_predicates_inexact_constructions_kernel.h> #include <CGAL/point_generators_3.h> #include <CGAL/Polygon_mesh_processing/measure.h> #include <pcl/io/pcd_io.h> ``` 3. 使用PCL的点云读取函数加载点云数据。例如,使用`pcl::io::loadPCDFile`函数加载一个PCD文件: ```cpp pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); pcl::io::loadPCDFile("path/to/your/pointcloud.pcd", *cloud); ``` 这将把读取的点云数据存储在`cloud`变量中。 4. 将PCL的点云数据转换为CGAL的点类型。CGAL使用自定义的点类型来表示点云数据。你需要将PCL的点云数据转换为CGAL的点类型以便进行进一步的计算。例如,假设你使用的是三维点云数据: ```cpp typedef CGAL::Exact_predicates_inexact_constructions_kernel K; typedef K::Point_3 CGAL_Point; std::vector<CGAL_Point> cgal_points; for (const pcl::PointXYZ& pcl_point : cloud->points) { CGAL_Point cgal_point(pcl_point.x, pcl_point.y, pcl_point.z); cgal_points.push_back(cgal_point); } ``` 这将把PCL的点云数据转换为CGAL的点类型,并存储在`cgal_points`向量中。 现在你可以使用CGAL的算法对点云数据进行处理了。请注意,上述代码只是一个简单的示例,具体的实现可能因你的需求而有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值