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+ {
32+ face_counter () : count(0 ) {}
33+ void begin_face () { ++count; }
34+ int count;
35+ };
36+
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+
45+ graph g (5 );
46+ add_edge (0 , 1 , g);
47+ add_edge (0 , 2 , g);
48+ add_edge (0 , 3 , g);
49+ add_edge (0 , 4 , g);
50+ // Star: 0 is center, 1-4 are leaves
51+
52+ std::cout << " Since the input graph is planar with " << num_vertices (g)
53+ << " vertices," << std::endl
54+ << " The output graph should be planar with "
55+ << 3 * num_vertices (g) - 6 << " edges and "
56+ << 2 * num_vertices (g) - 4 << " faces." << std::endl;
57+
58+ // Initialize the interior edge index
59+ auto e_index = get (edge_index, g);
60+ graph_traits< graph >::edges_size_type edge_count = 0 ;
61+ graph_traits< graph >::edge_iterator ei, ei_end;
62+ for (boost::tie (ei, ei_end) = edges (g); ei != ei_end; ++ei)
63+ put (e_index, *ei, edge_count++);
64+
65+ // Test for planarity; compute the planar embedding as a side-effect
66+ typedef std::vector< graph_traits< graph >::edge_descriptor > vec_t ;
67+ std::vector< vec_t > embedding (num_vertices (g));
68+ if (boyer_myrvold_planarity_test (boyer_myrvold_params::graph = g,
69+ boyer_myrvold_params::embedding = &embedding[0 ]))
70+ std::cout << " Input graph is planar" << std::endl;
71+ else
72+ std::cout << " Input graph is not planar" << std::endl;
73+
74+ make_biconnected_planar (g, &embedding[0 ]);
75+
76+ // Re-initialize the edge index, since we just added a few edges
77+ edge_count = 0 ;
78+ for (boost::tie (ei, ei_end) = edges (g); ei != ei_end; ++ei)
79+ put (e_index, *ei, edge_count++);
80+
81+ // Test for planarity again; compute the planar embedding as a side-effect
82+ if (boyer_myrvold_planarity_test (boyer_myrvold_params::graph = g,
83+ boyer_myrvold_params::embedding = &embedding[0 ]))
84+ std::cout << " After calling make_biconnected, the graph is still planar"
85+ << std::endl;
86+ else
87+ std::cout << " After calling make_biconnected, the graph is not planar"
88+ << std::endl;
89+
90+ make_maximal_planar (g, &embedding[0 ]);
91+
92+ // Re-initialize the edge index, since we just added a few edges
93+ edge_count = 0 ;
94+ for (boost::tie (ei, ei_end) = edges (g); ei != ei_end; ++ei)
95+ put (e_index, *ei, edge_count++);
96+
97+ // Test for planarity one final time; compute the planar embedding as a
98+ // side-effect
99+ std::cout << " After calling make_maximal_planar, the final graph " ;
100+ if (boyer_myrvold_planarity_test (boyer_myrvold_params::graph = g,
101+ boyer_myrvold_params::embedding = &embedding[0 ]))
102+ std::cout << " is planar." << std::endl;
103+ else
104+ std::cout << " is not planar." << std::endl;
105+
106+ std::cout << " The final graph has " << num_edges (g) << " edges."
107+ << std::endl;
108+
109+ face_counter count_visitor;
110+ planar_face_traversal (g, &embedding[0 ], count_visitor);
111+ std::cout << " The final graph has " << count_visitor.count << " faces."
112+ << std::endl;
113+
114+ return 0 ;
115+ }
0 commit comments