#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Polygon_mesh_processing/triangulate_faces.h>
#include <CGAL/Polygon_mesh_processing/remesh.h>
#include <CGAL/Polygon_mesh_processing/IO/polygon_mesh_io.h>
namespace PMP = CGAL::Polygon_mesh_processing;
typedef CGAL::Surface_mesh<K::Point_3> Mesh;
typedef Mesh::Vertex_index vertex_descriptor;
typedef Mesh::Face_index face_descriptor;
// 创建简单的平面网格并添加孔洞
Mesh create_plane_with_holes(double width, double height, int subdivisions)
{
Mesh mesh;
// 创建顶点
std::vector<vertex_descriptor> vertices;
double step_x = width / subdivisions;
double step_y = height / subdivisions;
for (int i = 0; i <= subdivisions; ++i) {
for (int j = 0; j <= subdivisions; ++j) {
double x = j * step_x - width / 2.0;
double y = i * step_y - height / 2.0;
vertices.push_back(mesh.add_vertex(K::Point_3(x, y, 0.0)));
}
}
// 创建面(除了中心区域)
int hole_start = subdivisions / 3;
int hole_end = 2 * subdivisions / 3;
for (int i = 0; i < subdivisions; ++i) {
for (int j = 0; j < subdivisions; ++j) {
// 跳过中心区域以创建孔洞
if (i >= hole_start && i < hole_end &&
j >= hole_start && j < hole_end) {
continue;
}
int v1 = i * (subdivisions + 1) + j;
int v2 = v1 + 1;
int v3 = (i + 1) * (subdivisions + 1) + j + 1;
int v4 = (i + 1) * (subdivisions + 1) + j;
mesh.add_face(vertices[v1], vertices[v2], vertices[v3]);
mesh.add_face(vertices[v1], vertices[v3], vertices[v4]);
}
}
return mesh;
}
int main()
{
// 创建带孔洞的网格
Mesh mesh = create_plane_with_holes(10.0, 10.0, 20);
std::cout << "Original mesh: " << std::endl;
std::cout << "Vertices: " << mesh.number_of_vertices() << std::endl;
std::cout << "Faces: " << mesh.number_of_faces() << std::endl;
// 三角化所有面
PMP::triangulate_faces(mesh);
std::cout << "After triangulation: " << std::endl;
std::cout << "Faces: " << mesh.number_of_faces() << std::endl;
// 保存结果
CGAL::IO::write_polygon_mesh("plane_with_holes.off", mesh);