straight_line_drawing.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <vector>
  14. #include <boost/graph/planar_canonical_ordering.hpp>
  15. #include <boost/graph/is_straight_line_drawing.hpp>
  16. #include <boost/graph/chrobak_payne_drawing.hpp>
  17. #include <boost/graph/boyer_myrvold_planar_test.hpp>
  18. using namespace boost;
  19. //a class to hold the coordinates of the straight line embedding
  20. struct coord_t
  21. {
  22. std::size_t x;
  23. std::size_t y;
  24. };
  25. int main(int argc, char** argv)
  26. {
  27. typedef adjacency_list
  28. < vecS,
  29. vecS,
  30. undirectedS,
  31. property<vertex_index_t, int>
  32. > graph;
  33. //Define the storage type for the planar embedding
  34. typedef std::vector< std::vector< graph_traits<graph>::edge_descriptor > >
  35. embedding_storage_t;
  36. typedef boost::iterator_property_map
  37. < embedding_storage_t::iterator,
  38. property_map<graph, vertex_index_t>::type
  39. >
  40. embedding_t;
  41. // Create the graph - a maximal planar graph on 7 vertices. The functions
  42. // planar_canonical_ordering and chrobak_payne_straight_line_drawing both
  43. // require a maximal planar graph. If you start with a graph that isn't
  44. // maximal planar (or you're not sure), you can use the functions
  45. // make_connected, make_biconnected_planar, and make_maximal planar in
  46. // sequence to add a set of edges to any undirected planar graph to make
  47. // it maximal planar.
  48. graph g(7);
  49. add_edge(0,1,g);
  50. add_edge(1,2,g);
  51. add_edge(2,3,g);
  52. add_edge(3,0,g);
  53. add_edge(3,4,g);
  54. add_edge(4,5,g);
  55. add_edge(5,6,g);
  56. add_edge(6,3,g);
  57. add_edge(0,4,g);
  58. add_edge(1,3,g);
  59. add_edge(3,5,g);
  60. add_edge(2,6,g);
  61. add_edge(1,4,g);
  62. add_edge(1,5,g);
  63. add_edge(1,6,g);
  64. // Create the planar embedding
  65. embedding_storage_t embedding_storage(num_vertices(g));
  66. embedding_t embedding(embedding_storage.begin(), get(vertex_index,g));
  67. boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g,
  68. boyer_myrvold_params::embedding = embedding
  69. );
  70. // Find a canonical ordering
  71. std::vector<graph_traits<graph>::vertex_descriptor> ordering;
  72. planar_canonical_ordering(g, embedding, std::back_inserter(ordering));
  73. //Set up a property map to hold the mapping from vertices to coord_t's
  74. typedef std::vector< coord_t > straight_line_drawing_storage_t;
  75. typedef boost::iterator_property_map
  76. < straight_line_drawing_storage_t::iterator,
  77. property_map<graph, vertex_index_t>::type
  78. >
  79. straight_line_drawing_t;
  80. straight_line_drawing_storage_t straight_line_drawing_storage
  81. (num_vertices(g));
  82. straight_line_drawing_t straight_line_drawing
  83. (straight_line_drawing_storage.begin(),
  84. get(vertex_index,g)
  85. );
  86. // Compute the straight line drawing
  87. chrobak_payne_straight_line_drawing(g,
  88. embedding,
  89. ordering.begin(),
  90. ordering.end(),
  91. straight_line_drawing
  92. );
  93. std::cout << "The straight line drawing is: " << std::endl;
  94. graph_traits<graph>::vertex_iterator vi, vi_end;
  95. for(boost::tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi)
  96. {
  97. coord_t coord(get(straight_line_drawing,*vi));
  98. std::cout << *vi << " -> (" << coord.x << ", " << coord.y << ")"
  99. << std::endl;
  100. }
  101. // Verify that the drawing is actually a plane drawing
  102. if (is_straight_line_drawing(g, straight_line_drawing))
  103. std::cout << "Is a plane drawing." << std::endl;
  104. else
  105. std::cout << "Is not a plane drawing." << std::endl;
  106. return 0;
  107. }