planar_face_traversal.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include <boost/graph/planar_face_traversal.hpp>
  16. #include <boost/graph/boyer_myrvold_planar_test.hpp>
  17. using namespace boost;
  18. // Some planar face traversal visitors that will
  19. // print the vertices and edges on the faces
  20. struct output_visitor : public planar_face_traversal_visitor
  21. {
  22. void begin_face() { std::cout << "New face: "; }
  23. void end_face() { std::cout << std::endl; }
  24. };
  25. struct vertex_output_visitor : public output_visitor
  26. {
  27. template <typename Vertex>
  28. void next_vertex(Vertex v)
  29. {
  30. std::cout << v << " ";
  31. }
  32. };
  33. struct edge_output_visitor : public output_visitor
  34. {
  35. template <typename Edge>
  36. void next_edge(Edge e)
  37. {
  38. std::cout << e << " ";
  39. }
  40. };
  41. int main(int argc, char** argv)
  42. {
  43. typedef adjacency_list
  44. < vecS,
  45. vecS,
  46. undirectedS,
  47. property<vertex_index_t, int>,
  48. property<edge_index_t, int>
  49. >
  50. graph;
  51. // Create a graph - this is a biconnected, 3 x 3 grid.
  52. // It should have four small (four vertex/four edge) faces and
  53. // one large face that contains all but the interior vertex
  54. graph g(9);
  55. add_edge(0,1,g);
  56. add_edge(1,2,g);
  57. add_edge(3,4,g);
  58. add_edge(4,5,g);
  59. add_edge(6,7,g);
  60. add_edge(7,8,g);
  61. add_edge(0,3,g);
  62. add_edge(3,6,g);
  63. add_edge(1,4,g);
  64. add_edge(4,7,g);
  65. add_edge(2,5,g);
  66. add_edge(5,8,g);
  67. // Initialize the interior edge index
  68. property_map<graph, edge_index_t>::type e_index = get(edge_index, g);
  69. graph_traits<graph>::edges_size_type edge_count = 0;
  70. graph_traits<graph>::edge_iterator ei, ei_end;
  71. for(boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
  72. put(e_index, *ei, edge_count++);
  73. // Test for planarity - we know it is planar, we just want to
  74. // compute the planar embedding as a side-effect
  75. typedef std::vector< graph_traits<graph>::edge_descriptor > vec_t;
  76. std::vector<vec_t> embedding(num_vertices(g));
  77. if (boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g,
  78. boyer_myrvold_params::embedding =
  79. &embedding[0]
  80. )
  81. )
  82. std::cout << "Input graph is planar" << std::endl;
  83. else
  84. std::cout << "Input graph is not planar" << std::endl;
  85. std::cout << std::endl << "Vertices on the faces: " << std::endl;
  86. vertex_output_visitor v_vis;
  87. planar_face_traversal(g, &embedding[0], v_vis);
  88. std::cout << std::endl << "Edges on the faces: " << std::endl;
  89. edge_output_visitor e_vis;
  90. planar_face_traversal(g, &embedding[0], e_vis);
  91. return 0;
  92. }