make_maximal_planar.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/make_biconnected_planar.hpp>
  16. #include <boost/graph/make_maximal_planar.hpp>
  17. #include <boost/graph/planar_face_traversal.hpp>
  18. #include <boost/graph/boyer_myrvold_planar_test.hpp>
  19. // This example shows how to start with a connected planar graph
  20. // and add edges to make the graph maximal planar (triangulated.)
  21. // Any maximal planar simple graph on n vertices has 3n - 6 edges and
  22. // 2n - 4 faces, a consequence of Euler's formula.
  23. using namespace boost;
  24. // This visitor is passed to planar_face_traversal to count the
  25. // number of faces.
  26. struct face_counter : public planar_face_traversal_visitor
  27. {
  28. face_counter() : count(0) {}
  29. void begin_face() { ++count; }
  30. int count;
  31. };
  32. int main(int argc, char** argv)
  33. {
  34. typedef adjacency_list
  35. < vecS,
  36. vecS,
  37. undirectedS,
  38. property<vertex_index_t, int>,
  39. property<edge_index_t, int>
  40. >
  41. graph;
  42. // Create the graph - a straight line
  43. graph g(10);
  44. add_edge(0,1,g);
  45. add_edge(1,2,g);
  46. add_edge(2,3,g);
  47. add_edge(3,4,g);
  48. add_edge(4,5,g);
  49. add_edge(5,6,g);
  50. add_edge(6,7,g);
  51. add_edge(7,8,g);
  52. add_edge(8,9,g);
  53. std::cout << "Since the input graph is planar with " << num_vertices(g)
  54. << " vertices," << std::endl
  55. << "The output graph should be planar with "
  56. << 3*num_vertices(g) - 6 << " edges and "
  57. << 2*num_vertices(g) - 4 << " faces." << std::endl;
  58. //Initialize the interior edge index
  59. property_map<graph, edge_index_t>::type e_index = get(edge_index, g);
  60. graph_traits<graph>::edges_size_type edge_count = 0;
  61. graph_traits<graph>::edge_iterator ei, ei_end;
  62. for(boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
  63. put(e_index, *ei, edge_count++);
  64. //Test for planarity; compute the planar embedding as a side-effect
  65. typedef std::vector< graph_traits<graph>::edge_descriptor > vec_t;
  66. std::vector<vec_t> embedding(num_vertices(g));
  67. if (boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g,
  68. boyer_myrvold_params::embedding =
  69. &embedding[0]
  70. )
  71. )
  72. std::cout << "Input graph is planar" << std::endl;
  73. else
  74. std::cout << "Input graph is not planar" << std::endl;
  75. make_biconnected_planar(g, &embedding[0]);
  76. // Re-initialize the edge index, since we just added a few edges
  77. edge_count = 0;
  78. for(boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
  79. put(e_index, *ei, edge_count++);
  80. //Test for planarity again; compute the planar embedding as a side-effect
  81. if (boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g,
  82. boyer_myrvold_params::embedding =
  83. &embedding[0]
  84. )
  85. )
  86. std::cout << "After calling make_biconnected, the graph is still planar"
  87. << std::endl;
  88. else
  89. std::cout << "After calling make_biconnected, the graph is not planar"
  90. << std::endl;
  91. make_maximal_planar(g, &embedding[0]);
  92. // Re-initialize the edge index, since we just added a few edges
  93. edge_count = 0;
  94. for(boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
  95. put(e_index, *ei, edge_count++);
  96. // Test for planarity one final time; compute the planar embedding as a
  97. // side-effect
  98. std::cout << "After calling make_maximal_planar, the final graph ";
  99. if (boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g,
  100. boyer_myrvold_params::embedding =
  101. &embedding[0]
  102. )
  103. )
  104. std::cout << "is planar." << std::endl;
  105. else
  106. std::cout << "is not planar." << std::endl;
  107. std::cout << "The final graph has " << num_edges(g)
  108. << " edges." << std::endl;
  109. face_counter count_visitor;
  110. planar_face_traversal(g, &embedding[0], count_visitor);
  111. std::cout << "The final graph has " << count_visitor.count << " faces."
  112. << std::endl;
  113. return 0;
  114. }