Goosefoot Mesher - CGAL
copy_polyhedron.h
1 #ifndef COPY_POLYHEDRON_H
2 #define COPY_POLYHEDRON_H
3 
4 #include <CGAL/Polyhedron_incremental_builder_3.h>
5 
6 // See : http://cgal-discuss.949826.n4.nabble.com/Example-Convert-polyhedron-from-one-kernel-to-another-td4514497.html
7 // Can be used to convert polyhedron from exact to inexact and vice-versa
8 template <class Polyhedron_input,
9 class Polyhedron_output>
11  : public CGAL::Modifier_base<typename Polyhedron_output::HalfedgeDS>
12 {
13  Copy_polyhedron_to(std::shared_ptr<Polyhedron_input> in_poly)
14  : in_poly(in_poly) {}
15 
16  void operator()(typename Polyhedron_output::HalfedgeDS& out_hds)
17  {
18  typedef typename Polyhedron_output::HalfedgeDS Output_HDS;
19 
20  CGAL::Polyhedron_incremental_builder_3<Output_HDS> builder(out_hds);
21 
22  typedef typename Polyhedron_input::Vertex_const_iterator Vertex_const_iterator;
23  typedef typename Polyhedron_input::Facet_const_iterator Facet_const_iterator;
24  typedef typename Polyhedron_input::Halfedge_around_facet_const_circulator HFCC;
25 
26  builder.begin_surface(in_poly->size_of_vertices(),
27  in_poly->size_of_facets(),
28  in_poly->size_of_halfedges());
29 
30  for(Vertex_const_iterator
31  vi = in_poly->vertices_begin(), end = in_poly->vertices_end();
32  vi != end ; ++vi)
33  {
34  typename Polyhedron_output::Point_3 p(::CGAL::to_double( vi->point().x()),
35  ::CGAL::to_double( vi->point().y()),
36  ::CGAL::to_double( vi->point().z()));
37  builder.add_vertex(p);
38  }
39 
40  typedef CGAL::Inverse_index<Vertex_const_iterator> Index;
41  Index index( in_poly->vertices_begin(), in_poly->vertices_end());
42 
43  for(Facet_const_iterator
44  fi = in_poly->facets_begin(), end = in_poly->facets_end();
45  fi != end; ++fi)
46  {
47  HFCC hc = fi->facet_begin();
48  HFCC hc_end = hc;
49  builder.begin_facet ();
50  do {
51  builder.add_vertex_to_facet(index[hc->vertex()]);
52  ++hc;
53  } while( hc != hc_end);
54  builder.end_facet();
55  }
56  builder.end_surface();
57  } // end operator()(..)
58 private:
59  std::shared_ptr<Polyhedron_input> in_poly;
60 }; // end Copy_polyhedron_to<>
61 
62 template <class Poly_B, class Poly_A>
63 void poly_copy(std::shared_ptr<Poly_B> poly_b, std::shared_ptr<Poly_A> poly_a)
64 {
65  poly_b->clear();
66  Copy_polyhedron_to<Poly_A, Poly_B> modifier(poly_a);
67  poly_b->delegate(modifier);
68 }
69 
70 #endif
Definition: copy_polyhedron.h:10