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