all_planar_input_files_test.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. /*
  9. This test looks in the directory "planar_input_graphs" for any files
  10. of the form *.dimacs. Each such file is used to create an input graph
  11. and test the input graph for planarity. If the graph is planar, a
  12. straight line drawing is generated and verified. If the graph isn't
  13. planar, a kuratowski subgraph is isolated and verified.
  14. This test needs to be linked against Boost.Filesystem.
  15. */
  16. #define BOOST_FILESYSTEM_VERSION 3
  17. #include <iostream>
  18. #include <fstream>
  19. #include <vector>
  20. #include <string>
  21. #include <utility>
  22. #include <boost/property_map/property_map.hpp>
  23. #include <boost/lexical_cast.hpp>
  24. #include <boost/tuple/tuple.hpp>
  25. #include <boost/filesystem.hpp>
  26. #include <boost/algorithm/string.hpp>
  27. #include <boost/test/minimal.hpp>
  28. #include <boost/graph/adjacency_list.hpp>
  29. #include <boost/graph/depth_first_search.hpp>
  30. #include <boost/graph/properties.hpp>
  31. #include <boost/graph/graph_traits.hpp>
  32. #include <boost/graph/planar_canonical_ordering.hpp>
  33. #include <boost/graph/make_connected.hpp>
  34. #include <boost/graph/make_biconnected_planar.hpp>
  35. #include <boost/graph/make_maximal_planar.hpp>
  36. #include <boost/graph/is_straight_line_drawing.hpp>
  37. #include <boost/graph/is_kuratowski_subgraph.hpp>
  38. #include <boost/graph/chrobak_payne_drawing.hpp>
  39. #include <boost/graph/boyer_myrvold_planar_test.hpp>
  40. #include <boost/graph/planar_detail/add_edge_visitors.hpp>
  41. using namespace boost;
  42. struct coord_t
  43. {
  44. std::size_t x;
  45. std::size_t y;
  46. };
  47. template <typename Graph>
  48. void read_dimacs(Graph& g, const std::string& filename)
  49. {
  50. typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
  51. std::vector<vertex_t> vertices_by_index;
  52. std::ifstream in(filename.c_str());
  53. while (!in.eof())
  54. {
  55. char buffer[256];
  56. in.getline(buffer, 256);
  57. std::string s(buffer);
  58. if (s.size() == 0)
  59. continue;
  60. std::vector<std::string> v;
  61. split(v, buffer, is_any_of(" \t\n"));
  62. if (v[0] == "p")
  63. {
  64. //v[1] == "edge"
  65. g = Graph(boost::lexical_cast<std::size_t>(v[2].c_str()));
  66. std::copy(vertices(g).first,
  67. vertices(g).second,
  68. std::back_inserter(vertices_by_index)
  69. );
  70. }
  71. else if (v[0] == "e")
  72. {
  73. add_edge(vertices_by_index
  74. [boost::lexical_cast<std::size_t>(v[1].c_str())],
  75. vertices_by_index
  76. [boost::lexical_cast<std::size_t>(v[2].c_str())],
  77. g);
  78. }
  79. }
  80. }
  81. int test_graph(const std::string& dimacs_filename)
  82. {
  83. typedef adjacency_list<listS,
  84. vecS,
  85. undirectedS,
  86. property<vertex_index_t, int>,
  87. property<edge_index_t, int> > graph;
  88. typedef graph_traits<graph>::edge_descriptor edge_t;
  89. typedef graph_traits<graph>::edge_iterator edge_iterator_t;
  90. typedef graph_traits<graph>::vertex_iterator vertex_iterator_t;
  91. typedef graph_traits<graph>::edges_size_type e_size_t;
  92. typedef graph_traits<graph>::vertex_descriptor vertex_t;
  93. typedef edge_index_update_visitor<property_map<graph, edge_index_t>::type>
  94. edge_visitor_t;
  95. vertex_iterator_t vi, vi_end;
  96. edge_iterator_t ei, ei_end;
  97. graph g;
  98. read_dimacs(g, dimacs_filename);
  99. // Initialize the interior edge index
  100. property_map<graph, edge_index_t>::type e_index = get(edge_index, g);
  101. e_size_t edge_count = 0;
  102. for(boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
  103. put(e_index, *ei, edge_count++);
  104. // Initialize the interior vertex index - not needed if the vertices
  105. // are stored with a vecS
  106. /*
  107. property_map<graph, vertex_index_t>::type v_index = get(vertex_index, g);
  108. v_size_t vertex_count = 0;
  109. for(boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  110. put(v_index, *vi, vertex_count++);
  111. */
  112. // This edge_updater will automatically update the interior edge
  113. // index of the graph as edges are created.
  114. edge_visitor_t edge_updater(get(edge_index, g), num_edges(g));
  115. // The input graph may not be maximal planar, but the Chrobak-Payne straight
  116. // line drawing needs a maximal planar graph as input. So, we make a copy of
  117. // the original graph here, then add edges to the graph to make it maximal
  118. // planar. When we're done creating a drawing of the maximal planar graph,
  119. // we can use the same mapping of vertices to points on the grid to embed the
  120. // original, non-maximal graph.
  121. graph g_copy(g);
  122. // Add edges to make g connected, if it isn't already
  123. make_connected(g, get(vertex_index, g), edge_updater);
  124. std::vector<graph_traits<graph>::edge_descriptor> kuratowski_edges;
  125. typedef std::vector< std::vector<edge_t> > edge_permutation_storage_t;
  126. typedef boost::iterator_property_map
  127. < edge_permutation_storage_t::iterator,
  128. property_map<graph, vertex_index_t>::type
  129. >
  130. edge_permutation_t;
  131. edge_permutation_storage_t edge_permutation_storage(num_vertices(g));
  132. edge_permutation_t perm(edge_permutation_storage.begin(),
  133. get(vertex_index,g)
  134. );
  135. // Test for planarity, computing the planar embedding or the kuratowski
  136. // subgraph.
  137. if (!boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g,
  138. boyer_myrvold_params::embedding = perm,
  139. boyer_myrvold_params::kuratowski_subgraph
  140. = std::back_inserter(kuratowski_edges)
  141. )
  142. )
  143. {
  144. std::cout << "Not planar. ";
  145. BOOST_REQUIRE(is_kuratowski_subgraph(g,
  146. kuratowski_edges.begin(),
  147. kuratowski_edges.end()
  148. )
  149. );
  150. return 0;
  151. }
  152. // If we get this far, we have a connected planar graph.
  153. make_biconnected_planar(g, perm, get(edge_index, g), edge_updater);
  154. // Compute the planar embedding of the (now) biconnected planar graph
  155. BOOST_CHECK (boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g,
  156. boyer_myrvold_params::embedding =
  157. perm
  158. )
  159. );
  160. // If we get this far, we have a biconnected planar graph
  161. make_maximal_planar(g, perm, get(vertex_index,g), get(edge_index,g),
  162. edge_updater
  163. );
  164. // Now the graph is triangulated - we can compute the final planar embedding
  165. BOOST_CHECK (boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g,
  166. boyer_myrvold_params::embedding =
  167. perm
  168. )
  169. );
  170. // Compute a planar canonical ordering of the vertices
  171. std::vector<vertex_t> ordering;
  172. planar_canonical_ordering(g, perm, std::back_inserter(ordering));
  173. BOOST_CHECK(ordering.size() == num_vertices(g));
  174. typedef std::vector< coord_t > drawing_storage_t;
  175. typedef boost::iterator_property_map
  176. < drawing_storage_t::iterator, property_map<graph, vertex_index_t>::type >
  177. drawing_map_t;
  178. drawing_storage_t drawing_vector(num_vertices(g));
  179. drawing_map_t drawing(drawing_vector.begin(), get(vertex_index,g));
  180. // Compute a straight line drawing
  181. chrobak_payne_straight_line_drawing(g,
  182. perm,
  183. ordering.begin(),
  184. ordering.end(),
  185. drawing
  186. );
  187. std::cout << "Planar. ";
  188. BOOST_REQUIRE (is_straight_line_drawing(g, drawing));
  189. return 0;
  190. }
  191. int test_main(int argc, char* argv[])
  192. {
  193. std::string input_directory_str = "planar_input_graphs";
  194. if (argc > 1)
  195. {
  196. input_directory_str = std::string(argv[1]);
  197. }
  198. std::cout << "Reading planar input files from " << input_directory_str
  199. << std::endl;
  200. filesystem::path input_directory =
  201. filesystem::system_complete(filesystem::path(input_directory_str));
  202. const std::string dimacs_extension = ".dimacs";
  203. filesystem::directory_iterator dir_end;
  204. for( filesystem::directory_iterator dir_itr(input_directory);
  205. dir_itr != dir_end; ++dir_itr)
  206. {
  207. if (dir_itr->path().extension() != dimacs_extension)
  208. continue;
  209. std::cout << "Testing " << dir_itr->path().leaf() << "... ";
  210. BOOST_REQUIRE (test_graph(dir_itr->path().string()) == 0);
  211. std::cout << std::endl;
  212. }
  213. return 0;
  214. }