1 //=======================================================================
2 // Copyright (c) Aaron Windsor 2007
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
9 #ifndef __PLANAR_FACE_TRAVERSAL_HPP__
10 #define __PLANAR_FACE_TRAVERSAL_HPP__
11
12 #include <vector>
13 #include <set>
14 #include <map>
15 #include <boost/next_prior.hpp>
16 #include <boost/graph/graph_traits.hpp>
17 #include <boost/graph/properties.hpp>
18
19 namespace boost
20 {
21
22 struct planar_face_traversal_visitor
23 {
begin_traversalboost::planar_face_traversal_visitor24 void begin_traversal() {}
25
begin_faceboost::planar_face_traversal_visitor26 void begin_face() {}
27
next_edgeboost::planar_face_traversal_visitor28 template < typename Edge > void next_edge(Edge) {}
29
next_vertexboost::planar_face_traversal_visitor30 template < typename Vertex > void next_vertex(Vertex) {}
31
end_faceboost::planar_face_traversal_visitor32 void end_face() {}
33
end_traversalboost::planar_face_traversal_visitor34 void end_traversal() {}
35 };
36
37 template < typename Graph, typename PlanarEmbedding, typename Visitor,
38 typename EdgeIndexMap >
planar_face_traversal(const Graph & g,PlanarEmbedding embedding,Visitor & visitor,EdgeIndexMap em)39 void planar_face_traversal(const Graph& g, PlanarEmbedding embedding,
40 Visitor& visitor, EdgeIndexMap em)
41 {
42 typedef typename graph_traits< Graph >::vertex_descriptor vertex_t;
43 typedef typename graph_traits< Graph >::edge_descriptor edge_t;
44 typedef typename graph_traits< Graph >::vertex_iterator vertex_iterator_t;
45 typedef typename graph_traits< Graph >::edge_iterator edge_iterator_t;
46 typedef typename property_traits< PlanarEmbedding >::value_type
47 embedding_value_t;
48 typedef typename embedding_value_t::const_iterator embedding_iterator_t;
49
50 typedef typename std::vector< std::set< vertex_t > >
51 distinguished_edge_storage_t;
52 typedef typename std::vector< std::map< vertex_t, edge_t > >
53 distinguished_edge_to_edge_storage_t;
54
55 typedef typename boost::iterator_property_map<
56 typename distinguished_edge_storage_t::iterator, EdgeIndexMap >
57 distinguished_edge_map_t;
58
59 typedef typename boost::iterator_property_map<
60 typename distinguished_edge_to_edge_storage_t::iterator, EdgeIndexMap >
61 distinguished_edge_to_edge_map_t;
62
63 distinguished_edge_storage_t visited_vector(num_edges(g));
64 distinguished_edge_to_edge_storage_t next_edge_vector(num_edges(g));
65
66 distinguished_edge_map_t visited(visited_vector.begin(), em);
67 distinguished_edge_to_edge_map_t next_edge(next_edge_vector.begin(), em);
68
69 vertex_iterator_t vi, vi_end;
70 typename std::vector< edge_t >::iterator ei, ei_end;
71 edge_iterator_t fi, fi_end;
72 embedding_iterator_t pi, pi_begin, pi_end;
73
74 visitor.begin_traversal();
75
76 // Initialize the next_edge property map. This map is initialized from the
77 // PlanarEmbedding so that get(next_edge, e)[v] is the edge that comes
78 // after e in the clockwise embedding around vertex v.
79
80 for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
81 {
82 vertex_t v(*vi);
83 pi_begin = embedding[v].begin();
84 pi_end = embedding[v].end();
85 for (pi = pi_begin; pi != pi_end; ++pi)
86 {
87 edge_t e(*pi);
88 std::map< vertex_t, edge_t > m = get(next_edge, e);
89 m[v] = boost::next(pi) == pi_end ? *pi_begin : *boost::next(pi);
90 put(next_edge, e, m);
91 }
92 }
93
94 // Take a copy of the edges in the graph here, since we want to accomodate
95 // face traversals that add edges to the graph (for triangulation, in
96 // particular) and don't want to use invalidated edge iterators.
97 // Also, while iterating over all edges in the graph, we single out
98 // any self-loops, which need some special treatment in the face traversal.
99
100 std::vector< edge_t > self_loops;
101 std::vector< edge_t > edges_cache;
102 std::vector< vertex_t > vertices_in_edge;
103
104 for (boost::tie(fi, fi_end) = edges(g); fi != fi_end; ++fi)
105 {
106 edge_t e(*fi);
107 edges_cache.push_back(e);
108 if (source(e, g) == target(e, g))
109 self_loops.push_back(e);
110 }
111
112 // Iterate over all edges in the graph
113 ei_end = edges_cache.end();
114 for (ei = edges_cache.begin(); ei != ei_end; ++ei)
115 {
116
117 edge_t e(*ei);
118 vertices_in_edge.clear();
119 vertices_in_edge.push_back(source(e, g));
120 vertices_in_edge.push_back(target(e, g));
121
122 typename std::vector< vertex_t >::iterator vi, vi_end;
123 vi_end = vertices_in_edge.end();
124
125 // Iterate over both vertices in the current edge
126 for (vi = vertices_in_edge.begin(); vi != vi_end; ++vi)
127 {
128
129 vertex_t v(*vi);
130 std::set< vertex_t > e_visited = get(visited, e);
131 typename std::set< vertex_t >::iterator e_visited_found
132 = e_visited.find(v);
133
134 if (e_visited_found == e_visited.end())
135 visitor.begin_face();
136
137 while (e_visited.find(v) == e_visited.end())
138 {
139 visitor.next_vertex(v);
140 visitor.next_edge(e);
141 e_visited.insert(v);
142 put(visited, e, e_visited);
143 v = source(e, g) == v ? target(e, g) : source(e, g);
144 e = get(next_edge, e)[v];
145 e_visited = get(visited, e);
146 }
147
148 if (e_visited_found == e_visited.end())
149 visitor.end_face();
150 }
151 }
152
153 // Iterate over all self-loops, visiting them once separately
154 // (they've already been visited once, this visitation is for
155 // the "inside" of the self-loop)
156
157 ei_end = self_loops.end();
158 for (ei = self_loops.begin(); ei != ei_end; ++ei)
159 {
160 visitor.begin_face();
161 visitor.next_edge(*ei);
162 visitor.next_vertex(source(*ei, g));
163 visitor.end_face();
164 }
165
166 visitor.end_traversal();
167 }
168
169 template < typename Graph, typename PlanarEmbedding, typename Visitor >
planar_face_traversal(const Graph & g,PlanarEmbedding embedding,Visitor & visitor)170 inline void planar_face_traversal(
171 const Graph& g, PlanarEmbedding embedding, Visitor& visitor)
172 {
173 planar_face_traversal(g, embedding, visitor, get(edge_index, g));
174 }
175
176 } // namespace boost
177
178 #endif //__PLANAR_FACE_TRAVERSAL_HPP__
179