planar_face_traversal.hpp 5.9 KB

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