cgal/BGL/examples/BGL_surface_mesh/shortest_path.cpp

81 lines
2.3 KiB
C++

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/boost/graph/graph_traits_Surface_mesh.h>
#include <CGAL/boost/graph/shortest_path.h>
#include <CGAL/IO/polygon_mesh_io.h>
#include <string>
#include <vector>
#include <fstream>
#include <exception>
#include <algorithm>
using K = CGAL::Exact_predicates_inexact_constructions_kernel;
using Point = K::Point_3;
using Mesh = CGAL::Surface_mesh<Point>;
using vertex_descriptor = boost::graph_traits<Mesh>::vertex_descriptor;
using edge_descriptor = boost::graph_traits<Mesh>::edge_descriptor;
using halfedge_descriptor = boost::graph_traits<Mesh>::halfedge_descriptor;
namespace PMP = CGAL::Polygon_mesh_processing;
namespace params = CGAL::parameters;
// Example main
int main(int argc, char** argv)
{
const std::string filename = (argc > 1) ? argv[1] : CGAL::data_file_path("meshes/elephant.off");
// Try building a surface_mesh
Mesh sm;
bool ok = CGAL::IO::read_polygon_mesh(filename, sm);
if (!ok || !sm.is_valid() || sm.is_empty())
{
std::cerr << "Error: Invalid facegraph" << std::endl;
std::cerr << "Filename = " << filename << std::endl;
return EXIT_FAILURE;
}
const std::size_t i0 = 0;
const std::size_t i1 = num_vertices(sm) / 2;
// Get the vertex descriptors of the source and target vertices
const vertex_descriptor vs = *vertices(sm).first;
vertex_descriptor vt;
std::size_t vid = 0;
for (const vertex_descriptor v : vertices(sm))
{
if (vid++ == i1)
{
vt = v;
break;
}
}
std::vector<halfedge_descriptor> halfedge_sequence;
CGAL::shortest_path_between_two_vertices(vs, vt, sm,
std::back_inserter(halfedge_sequence));
// dump
std::cout << "Shortest path between vertices " << i0 << " and " << i1
<< " is made of " << halfedge_sequence.size() << " halfedges." << std::endl;
// Get the property map of the points of the mesh
auto vpmap = get(CGAL::vertex_point, sm);
std::ofstream out("shortest_path.polylines.txt");
for (const halfedge_descriptor he : halfedge_sequence)
{
const vertex_descriptor v0 = source(he, sm);
const vertex_descriptor v1 = target(he, sm);
out << "2 " << get(vpmap, v0) << " " << get(vpmap, v1) << std::endl;
}
out.close();
return EXIT_SUCCESS;
}