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