前言
CGAL确实实现了对于STL文件的读写接口,但是都是以polygon soup
的形式读入的。polygon soup
是一个没有全局信息的多边形集合,分别存放在两个容器中:一个存储点(PointRange
),一个存储由点索引组成的面(PolygonRange
)。
若要让读入的STL模型数据能够供CGAL算法使用,一般需要将polygon soup
转换为polygon mesh
,后者具有一致性和可定向性。
而这个转换过程一版会用到两个函数:
将多边形的方向统一
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;
}
可以看到,经过转换,模型中顶点的数目增加了。我的理解是其将原本的非流形表面转化为了流形表面,所以三角面片数量不变,但顶点变多了。
一步到位
上述是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;
}