如何将OpenMesh中的网格结构转化为CGAL中的网格结构(复杂版)
背景:以OpenMesh为主要数据结构开发的项目需要用到CGAL封装的算法。
关键函数:头文件CGAL/boost/graph/copy_face_graph.h下的copy_face_graph。
环境准备:OpenMesh,CGAL,windows10 SDK。
难点:函数copy_face_graph的第三个参数(类型是CGAL自定义的 Named Parameters )
1 #include <OpenMesh/Core/Mesh/DefaultTriMesh.hh> 2 #include <OpenMesh/Core/IO/MeshIO.hh> 3 #include <CGAL/boost/graph/copy_face_graph.h> 4 #include <CGAL/boost/graph/graph_traits_TriMesh_ArrayKernelT.h> 5 #include <CGAL/boost/graph/graph_traits_PolyMesh_ArrayKernelT.h> 6 #include <boost/unordered_map.hpp> 7 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h> 8 #include <CGAL/Polyhedron_3.h> 9 #include <iterator> 10 11 typedef OpenMesh::TriMesh TriMesh; 12 typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;//inexact 13 typedef CGAL::Polyhedron_3<Kernel> PolyMesh;//inexact 14 15 typedef boost::graph_traits<TriMesh>::vertex_descriptor tm_vertex_descriptor; 16 typedef boost::graph_traits<TriMesh>::halfedge_descriptor tm_halfedge_descriptor; 17 typedef boost::graph_traits<TriMesh>::face_descriptor tm_face_descriptor; 18 19 typedef boost::graph_traits<PolyMesh>::vertex_descriptor pm_vertex_descriptor; 20 typedef boost::graph_traits<PolyMesh>::halfedge_descriptor pm_halfedge_descriptor; 21 typedef boost::graph_traits<PolyMesh>::face_descriptor pm_face_descriptor; 22 23 //用无序map来追踪元素 24 boost::unordered_map<tm_vertex_descriptor, pm_vertex_descriptor> v2v; 25 boost::unordered_map<tm_halfedge_descriptor, pm_halfedge_descriptor> h2h; 26 boost::unordered_map<tm_face_descriptor, pm_face_descriptor> f2f; 27 28 int main(int argc, char* argv[]) { 29 30 TriMesh OM_mesh; 31 PolyMesh CGAL_mesh; 32 33 if (OpenMesh::IO::read_mesh(OM_mesh, "E:\\3d_model_files\\sphere0.stl")) { 34 std::clog << "顺利读入模型文件到OM_mesh中" << std::endl; 35 } 36 37 std::clog << CGAL::is_valid_polygon_mesh(OM_mesh) << std::endl; 38 39 CGAL::copy_face_graph(OM_mesh,CGAL_mesh,//复制的方向是从OM_mesh到CGAL_mesh 40 CGAL::parameters::vertex_to_vertex_map(boost::make_assoc_property_map(v2v)). 41 halfedge_to_halfedge_output_iterator(std::inserter(h2h, h2h.end())). 42 face_to_face_map(boost::make_assoc_property_map(f2f))); 43 44 std::clog << CGAL::is_valid_polygon_mesh(CGAL_mesh) << std::endl; 45 std::clog << vertices(CGAL_mesh).size() << std::endl; 46 std::clog << halfedges(CGAL_mesh).size() << std::endl; 47 std::clog << faces(CGAL_mesh).size() << std::endl; 48 49 return 0; 50 }