#include <iostream>
#include <fstream>
#include <map>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/boost/graph/graph_traits_Polyhedron_3.h>
#include <CGAL/Surface_mesh_simplification/edge_collapse.h>
#include <CGAL/Surface_mesh_simplification/Policies/Edge_collapse/Midpoint_placement.h>
#include <CGAL/Surface_mesh_simplification/Policies/Edge_collapse/Constrained_placement.h>
#include <CGAL/Surface_mesh_simplification/Policies/Edge_collapse/Count_stop_predicate.h>
namespace SMS = CGAL::Surface_mesh_simplification ;
struct Border_is_constrained_edge_map{
const Surface_mesh* sm_ptr;
typedef boost::graph_traits<Surface_mesh>::edge_descriptor key_type;
typedef bool value_type;
typedef value_type reference;
typedef boost::readable_property_map_tag category;
Border_is_constrained_edge_map()
{}
Border_is_constrained_edge_map(const Surface_mesh& sm)
: sm_ptr(&sm)
{}
friend bool get(Border_is_constrained_edge_map m, const key_type& edge) {
return CGAL::is_border(edge, *m.sm_ptr);
}
};
typedef SMS::Constrained_placement<SMS::Midpoint_placement<Surface_mesh>,
Border_is_constrained_edge_map > Placement;
int main( int argc, char** argv )
{
Surface_mesh surface_mesh;
if (argc!=2){
std::cerr<< "Usage: " << argv[0] << " input.off\n";
return 1;
}
std::ifstream is(argv[1]);
if(!is){
std::cerr<< "Filename provided is invalid\n";
return 1;
}
is >> surface_mesh ;
std::map<Surface_mesh::Halfedge_handle,std::pair<Point_3, Point_3> >constrained_edges;
std::size_t nb_border_edges=0;
for (Surface_mesh::Halfedge_iterator hit=surface_mesh.halfedges_begin(),
hit_end=surface_mesh.halfedges_end();
hit!=hit_end; ++hit )
{
if ( hit->is_border() ){
constrained_edges[hit]=std::make_pair( hit->opposite()->vertex()->point(),
hit->vertex()->point() );
++nb_border_edges;
}
}
SMS::Count_stop_predicate<Surface_mesh> stop(0);
(surface_mesh
,stop
,CGAL::vertex_index_map(get(CGAL::vertex_external_index,surface_mesh))
.halfedge_index_map (get(CGAL::halfedge_external_index ,surface_mesh))
.edge_is_constrained_map(Border_is_constrained_edge_map(surface_mesh))
.get_placement(Placement())
);
std::cout << "\nFinished...\n" << r << " edges removed.\n"
<< (surface_mesh.size_of_halfedges()/2) << " final edges.\n" ;
std::ofstream os( argc > 2 ? argv[2] : "out.off" ) ; os << surface_mesh ;
for (Surface_mesh::Halfedge_iterator hit=surface_mesh.halfedges_begin(),
hit_end=surface_mesh.halfedges_end();
hit!=hit_end; ++hit )
{
if (hit->is_border()){
--nb_border_edges;
assert( constrained_edges[hit] ==
std::make_pair( hit->opposite()->vertex()->point(),
hit->vertex()->point() ) );
}
}
assert( nb_border_edges==0 );
return 0 ;
}