make_biconnected_planar.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifndef __MAKE_BICONNECTED_PLANAR_HPP__
  9. #define __MAKE_BICONNECTED_PLANAR_HPP__
  10. #include <boost/config.hpp>
  11. #include <boost/tuple/tuple.hpp> //for tie
  12. #include <boost/graph/biconnected_components.hpp>
  13. #include <boost/property_map/property_map.hpp>
  14. #include <vector>
  15. #include <iterator>
  16. #include <algorithm>
  17. #include <boost/graph/planar_detail/add_edge_visitors.hpp>
  18. namespace boost
  19. {
  20. template < typename Graph, typename PlanarEmbedding, typename EdgeIndexMap,
  21. typename AddEdgeVisitor >
  22. void make_biconnected_planar(
  23. Graph& g, PlanarEmbedding embedding, EdgeIndexMap em, AddEdgeVisitor& vis)
  24. {
  25. typedef typename graph_traits< Graph >::vertex_descriptor vertex_t;
  26. typedef typename graph_traits< Graph >::edge_descriptor edge_t;
  27. typedef typename graph_traits< Graph >::edges_size_type edge_size_t;
  28. typedef typename property_traits< PlanarEmbedding >::value_type
  29. embedding_value_t;
  30. typedef typename embedding_value_t::const_iterator embedding_iterator_t;
  31. typedef iterator_property_map< std::vector< std::size_t >::iterator,
  32. EdgeIndexMap >
  33. component_map_t;
  34. edge_size_t n_edges(num_edges(g));
  35. std::vector< vertex_t > articulation_points;
  36. std::vector< edge_size_t > component_vector(n_edges);
  37. component_map_t component_map(component_vector.begin(), em);
  38. biconnected_components(
  39. g, component_map, std::back_inserter(articulation_points));
  40. typename std::vector< vertex_t >::iterator ap, ap_end;
  41. ap_end = articulation_points.end();
  42. for (ap = articulation_points.begin(); ap != ap_end; ++ap)
  43. {
  44. vertex_t v(*ap);
  45. embedding_iterator_t pi = embedding[v].begin();
  46. embedding_iterator_t pi_end = embedding[v].end();
  47. edge_size_t previous_component(n_edges + 1);
  48. vertex_t previous_vertex = graph_traits< Graph >::null_vertex();
  49. for (; pi != pi_end; ++pi)
  50. {
  51. edge_t e(*pi);
  52. vertex_t e_source(source(e, g));
  53. vertex_t e_target(target(e, g));
  54. // Skip self-loops and parallel edges
  55. if (e_source == e_target || previous_vertex == e_target)
  56. continue;
  57. vertex_t current_vertex = e_source == v ? e_target : e_source;
  58. edge_size_t current_component = component_map[e];
  59. if (previous_vertex != graph_traits< Graph >::null_vertex()
  60. && current_component != previous_component)
  61. {
  62. vis.visit_vertex_pair(current_vertex, previous_vertex, g);
  63. }
  64. previous_vertex = current_vertex;
  65. previous_component = current_component;
  66. }
  67. }
  68. }
  69. template < typename Graph, typename PlanarEmbedding, typename EdgeIndexMap >
  70. inline void make_biconnected_planar(
  71. Graph& g, PlanarEmbedding embedding, EdgeIndexMap em)
  72. {
  73. default_add_edge_visitor vis;
  74. make_biconnected_planar(g, embedding, em, vis);
  75. }
  76. template < typename Graph, typename PlanarEmbedding >
  77. inline void make_biconnected_planar(Graph& g, PlanarEmbedding embedding)
  78. {
  79. make_biconnected_planar(g, embedding, get(edge_index, g));
  80. }
  81. } // namespace boost
  82. #endif //__MAKE_BICONNECTED_PLANAR_HPP__