• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  //=======================================================================
2  // Copyright 2007 Aaron Windsor
3  //
4  // Distributed under the Boost Software License, Version 1.0. (See
5  // accompanying file LICENSE_1_0.txt or copy at
6  // http://www.boost.org/LICENSE_1_0.txt)
7  //=======================================================================
8  #include <iostream>
9  #include <boost/graph/adjacency_list.hpp>
10  #include <boost/graph/properties.hpp>
11  #include <boost/graph/graph_traits.hpp>
12  #include <boost/property_map/property_map.hpp>
13  #include <boost/ref.hpp>
14  #include <vector>
15  
16  #include <boost/graph/planar_face_traversal.hpp>
17  #include <boost/graph/boyer_myrvold_planar_test.hpp>
18  
19  using namespace boost;
20  
21  // Some planar face traversal visitors that will
22  // print the vertices and edges on the faces
23  
24  struct output_visitor : public planar_face_traversal_visitor
25  {
begin_faceoutput_visitor26      void begin_face() { std::cout << "New face: "; }
end_faceoutput_visitor27      void end_face() { std::cout << std::endl; }
28  };
29  
30  struct vertex_output_visitor : public output_visitor
31  {
next_vertexvertex_output_visitor32      template < typename Vertex > void next_vertex(Vertex v)
33      {
34          std::cout << v << " ";
35      }
36  };
37  
38  struct edge_output_visitor : public output_visitor
39  {
next_edgeedge_output_visitor40      template < typename Edge > void next_edge(Edge e) { std::cout << e << " "; }
41  };
42  
main(int argc,char ** argv)43  int main(int argc, char** argv)
44  {
45  
46      typedef adjacency_list< vecS, vecS, undirectedS,
47          property< vertex_index_t, int >, property< edge_index_t, int > >
48          graph;
49  
50      // Create a graph - this is a biconnected, 3 x 3 grid.
51      // It should have four small (four vertex/four edge) faces and
52      // one large face that contains all but the interior vertex
53      graph g(9);
54  
55      add_edge(0, 1, g);
56      add_edge(1, 2, g);
57  
58      add_edge(3, 4, g);
59      add_edge(4, 5, g);
60  
61      add_edge(6, 7, g);
62      add_edge(7, 8, g);
63  
64      add_edge(0, 3, g);
65      add_edge(3, 6, g);
66  
67      add_edge(1, 4, g);
68      add_edge(4, 7, g);
69  
70      add_edge(2, 5, g);
71      add_edge(5, 8, g);
72  
73      // Initialize the interior edge index
74      property_map< graph, edge_index_t >::type e_index = get(edge_index, g);
75      graph_traits< graph >::edges_size_type edge_count = 0;
76      graph_traits< graph >::edge_iterator ei, ei_end;
77      for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
78          put(e_index, *ei, edge_count++);
79  
80      // Test for planarity - we know it is planar, we just want to
81      // compute the planar embedding as a side-effect
82      typedef std::vector< graph_traits< graph >::edge_descriptor > vec_t;
83      std::vector< vec_t > embedding(num_vertices(g));
84      if (boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g,
85              boyer_myrvold_params::embedding = &embedding[0]))
86          std::cout << "Input graph is planar" << std::endl;
87      else
88          std::cout << "Input graph is not planar" << std::endl;
89  
90      std::cout << std::endl << "Vertices on the faces: " << std::endl;
91      vertex_output_visitor v_vis;
92      planar_face_traversal(g, &embedding[0], v_vis);
93  
94      std::cout << std::endl << "Edges on the faces: " << std::endl;
95      edge_output_visitor e_vis;
96      planar_face_traversal(g, &embedding[0], e_vis);
97  
98      return 0;
99  }
100