生成CGAL闭合曲面例子可参见
一个可能遇到的问题是,生成的曲面法向方向错误,期望法向为曲面→曲面外侧,实际为曲面→曲面内侧,此时需要进行法向翻转。
其中一种方法是,计算曲面上一点法向,计算曲面质心指向该点向量,通过判断法向和该向量的夹角来决定是否进行法向翻转。部分代码如下:
#include <CGAL/Polygon_mesh_processing/compute_normal.h>
#include <CGAL/Polygon_mesh_processing/orientation.h>
typedef boost::graph_traits<Mesh>::vertex_descriptor vertex_descriptor;
typedef Mesh::template Property_map<vertex_descriptor, Vector> VNMap;
Mesh output_mesh; // 假设已得到生成的Mesh
// 曲面法向计算
VNMap vnormals = output_mesh.add_property_map<vertex_descriptor, Vector>("v:normal", CGAL::NULL_VECTOR).first;
CGAL::Polygon_mesh_processing::compute_vertex_normals(output_mesh, vnormals);
// 取出曲面上一点及其法向
auto pt = output_mesh.point(*output_mesh.vertices_begin());
auto normal = vnormals[*output_mesh.vertices_begin()];
// 构造质心指向曲面上一点的向量
CGAL::Vector_3 vec = pt - centroid;
// 若两向量夹角超过90°,则翻转法线
Kernel::FT dot_product_result = vec * normal;
if (dot_product_result < 0)
{
CGAL::Polygon_mesh_processing::reverse_face_orientations(output_mesh);
}