#include <iostream>
#include <fstream>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/boost/graph/graph_traits_Surface_mesh.h>
#include <CGAL/Surface_mesh_simplification/edge_collapse.h>
#include <CGAL/Surface_mesh_simplification/Edge_collapse_visitor_base.h>
#include <CGAL/Surface_mesh_simplification/Policies/Edge_collapse/Count_ratio_stop_predicate.h>
#include <CGAL/Surface_mesh_simplification/Policies/Edge_collapse/Midpoint_and_length.h>
typedef CGAL::Surface_mesh<Point_3> Surface_mesh;
typedef boost::graph_traits<Surface_mesh>::halfedge_descriptor halfedge_descriptor ;
typedef boost::graph_traits<Surface_mesh>::vertex_descriptor vertex_descriptor;
namespace SMS = CGAL::Surface_mesh_simplification ;
typedef SMS::Edge_profile<Surface_mesh> Profile ;
struct Stats
{
Stats()
: collected(0)
, processed(0)
, collapsed(0)
, non_collapsable(0)
, cost_uncomputable(0)
, placement_uncomputable(0)
{}
std::size_t collected ;
std::size_t processed ;
std::size_t collapsed ;
std::size_t non_collapsable ;
std::size_t cost_uncomputable ;
std::size_t placement_uncomputable ;
} ;
struct My_visitor : SMS::Edge_collapse_visitor_base<Surface_mesh>
{
My_visitor( Stats* s) : stats(s){}
void OnCollected( Profile const&, boost::optional<double> const& )
{
++ stats->collected ;
std::cerr << "\rEdges collected: " << stats->collected << std::flush ;
}
void OnSelected(Profile const&
,boost::optional<double> cost
,std::size_t initial
,std::size_t current
)
{
++ stats->processed ;
if ( !cost )
++ stats->cost_uncomputable ;
if ( current == initial )
std::cerr << "\n" << std::flush ;
std::cerr << "\r" << current << std::flush ;
}
void OnCollapsing(Profile const&
,boost::optional<Point> placement
)
{
if ( !placement )
++ stats->placement_uncomputable ;
}
void OnNonCollapsable( Profile const& )
{
++ stats->non_collapsable;
}
void OnCollapsed( Profile const&, vertex_descriptor )
{
++ stats->collapsed;
}
Stats* stats ;
} ;
int main( int argc, char** argv )
{
Surface_mesh surface_mesh;
std::ifstream is(argv[1]) ; is >> surface_mesh ;
SMS::Count_ratio_stop_predicate<Surface_mesh> stop(0.1);
Stats stats ;
My_visitor vis(&stats) ;
(surface_mesh
,stop
,CGAL::get_cost (SMS::Edge_length_cost <Surface_mesh>())
.get_placement(SMS::Midpoint_placement<Surface_mesh>())
.visitor (vis)
);
std::cout << "\nEdges collected: " << stats.collected
<< "\nEdges proccessed: " << stats.processed
<< "\nEdges collapsed: " << stats.collapsed
<< std::endl
<< "\nEdges not collapsed due to topological constraints: " << stats.non_collapsable
<< "\nEdge not collapsed due to cost computation constraints: " << stats.cost_uncomputable
<< "\nEdge not collapsed due to placement computation constraints: " << stats.placement_uncomputable
<< std::endl ;
std::cout << "\nFinished...\n" << r << " edges removed.\n"
<< num_edges(surface_mesh) << " final edges.\n" ;
std::ofstream os( argc > 2 ? argv[2] : "out.off" ) ; os << surface_mesh ;
return 0 ;
}