4
#include <CGAL/Simple_cartesian.h>
5
#include <CGAL/Polyhedron_3.h>
6
#include <CGAL/IO/Polyhedron_iostream.h>
8
// Adaptor for Polyhedron_3
9
#include <CGAL/Surface_mesh_simplification/HalfedgeGraph_Polyhedron_3.h>
11
// Simplification function
12
#include <CGAL/Surface_mesh_simplification/edge_collapse.h>
14
// Stop-condition policy
15
#include <CGAL/Surface_mesh_simplification/Policies/Edge_collapse/Count_stop_predicate.h>
17
// Map used to mark edges as fixed
18
#include <CGAL/Unique_hash_map.h>
20
typedef CGAL::Simple_cartesian<double> Kernel;
21
typedef CGAL::Polyhedron_3<Kernel> Surface;
23
namespace SMS = CGAL::Surface_mesh_simplification ;
26
// BGL property map which indicates whether an edge is border OR is marked as non-removable
28
class Constrains_map : public boost::put_get_helper<bool,Constrains_map>
32
typedef boost::readable_property_map_tag category;
33
typedef bool value_type;
34
typedef bool reference;
35
typedef boost::graph_traits<Surface const>::edge_descriptor key_type;
37
Constrains_map() : mConstrains(false) {}
39
reference operator[](key_type const& e) const { return e->is_border() || is_constrained(e) ; }
41
void set_is_constrained ( key_type const& e, bool is ) { mConstrains[e]=is; }
43
bool is_constrained( key_type const& e ) const { return mConstrains.is_defined(e) ? mConstrains[e] : false ; }
47
CGAL::Unique_hash_map<key_type,bool> mConstrains ;
51
int main( int argc, char** argv )
55
std::ifstream is(argv[1]) ; is >> surface ;
57
// This is a stop predicate (defines when the algorithm terminates).
58
// In this example, the simplification stops when the number of undirected edges
59
// left in the surface drops below the specified number (1000)
60
SMS::Count_stop_predicate<Surface> stop(10);
62
Constrains_map constrains_map ;
65
// This example marks ALL edges as non-removable, but a real world application would mark only selected ones.
66
for( Surface::Halfedge_iterator eb = surface.halfedges_begin(), ee = surface.halfedges_end() ; eb != ee ; ++ eb )
67
constrains_map.set_is_constrained(eb,true);
69
// This the actual call to the simplification algorithm.
70
// The surface and stop conditions are mandatory arguments.
71
// The index maps are needed because the vertices and edges
72
// of this surface lack an "id()" field.
73
int r = SMS::edge_collapse
76
,CGAL::vertex_index_map(boost::get(CGAL::vertex_external_index,surface))
77
.edge_index_map (boost::get(CGAL::edge_external_index ,surface))
78
.edge_is_border_map(constrains_map)
81
std::cout << "\nFinished...\n" << r << " edges removed.\n"
82
<< (surface.size_of_halfedges()/2) << " final edges.\n" ;
84
std::ofstream os( argc > 2 ? argv[2] : "out.off" ) ; os << surface ;