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