|
| 1 | + |
| 2 | +#include <CGAL/Exact_predicates_inexact_constructions_kernel.h> |
| 3 | +#include <CGAL/Surface_mesh.h> |
| 4 | +#include <CGAL/Mean_curvature_flow_skeletonization.h> |
| 5 | + |
| 6 | +#include <OpenMesh/Core/IO/MeshIO.hh> |
| 7 | +#include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh> |
| 8 | + |
| 9 | +#include <CGAL/boost/graph/graph_traits_PolyMesh_ArrayKernelT.h> |
| 10 | + |
| 11 | +#include <fstream> |
| 12 | + |
| 13 | +typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel; |
| 14 | +typedef Kernel::Point_3 Point; |
| 15 | +typedef OpenMesh::PolyMesh_ArrayKernelT</* MyTraits*/> Triangle_mesh; |
| 16 | + |
| 17 | +typedef boost::graph_traits<Triangle_mesh>::vertex_descriptor vertex_descriptor; |
| 18 | + |
| 19 | +typedef CGAL::Mean_curvature_flow_skeletonization<Triangle_mesh> Skeletonization; |
| 20 | +typedef Skeletonization::Skeleton Skeleton; |
| 21 | + |
| 22 | +typedef Skeleton::vertex_descriptor Skeleton_vertex; |
| 23 | +typedef Skeleton::edge_descriptor Skeleton_edge; |
| 24 | + |
| 25 | +int main(int argc, char* argv[]) |
| 26 | +{ |
| 27 | + const std::string filename = (argc > 1) ? argv[1] : CGAL::data_file_path("meshes/elephant.off"); |
| 28 | + |
| 29 | + Triangle_mesh tmesh; |
| 30 | + OpenMesh::IO::read_mesh(tmesh, filename); |
| 31 | + |
| 32 | + if (!CGAL::is_triangle_mesh(tmesh)) |
| 33 | + { |
| 34 | + std::cout << "Input geometry is not triangulated." << std::endl; |
| 35 | + return EXIT_FAILURE; |
| 36 | + } |
| 37 | + |
| 38 | + Skeleton skeleton; |
| 39 | + Skeletonization mcs(tmesh); |
| 40 | + |
| 41 | + // 1. Contract the mesh by mean curvature flow. |
| 42 | + mcs.contract_geometry(); |
| 43 | + |
| 44 | + // 2. Collapse short edges and split bad triangles. |
| 45 | + mcs.collapse_edges(); |
| 46 | + mcs.split_faces(); |
| 47 | + |
| 48 | + // 3. Fix degenerate vertices. |
| 49 | + mcs.detect_degeneracies(); |
| 50 | + |
| 51 | + // Perform the above three steps in one iteration. |
| 52 | + mcs.contract(); |
| 53 | + |
| 54 | + // Iteratively apply step 1 to 3 until convergence. |
| 55 | + mcs.contract_until_convergence(); |
| 56 | + |
| 57 | + // Convert the contracted mesh into a curve skeleton and |
| 58 | + // get the correspondent surface points |
| 59 | + mcs.convert_to_skeleton(skeleton); |
| 60 | + |
| 61 | + std::cout << "Number of vertices of the skeleton: " << boost::num_vertices(skeleton) << "\n"; |
| 62 | + std::cout << "Number of edges of the skeleton: " << boost::num_edges(skeleton) << "\n"; |
| 63 | + |
| 64 | + // Output all the edges of the skeleton. |
| 65 | + std::ofstream output("skel-sm.polylines.txt"); |
| 66 | + for(Skeleton_edge e : CGAL::make_range(edges(skeleton))) |
| 67 | + { |
| 68 | + const Point& s = skeleton[source(e, skeleton)].point; |
| 69 | + const Point& t = skeleton[target(e, skeleton)].point; |
| 70 | + output << "2 "<< s << " " << t << "\n"; |
| 71 | + } |
| 72 | + output.close(); |
| 73 | + |
| 74 | + // Output skeleton points and the corresponding surface points |
| 75 | + output.open("correspondance-sm.polylines.txt"); |
| 76 | + for(Skeleton_vertex v : CGAL::make_range(vertices(skeleton))) |
| 77 | + for(vertex_descriptor vd : skeleton[v].vertices) |
| 78 | + output << "2 " << skeleton[v].point << " " << get(CGAL::vertex_point, tmesh, vd) << "\n"; |
| 79 | + |
| 80 | + return EXIT_SUCCESS; |
| 81 | +} |
0 commit comments