【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()](CGAL 5.5 - Polygon Mesh Processing: Combinatorial Repairing)

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

测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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()一步到位。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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;
}