CreateDualGraph.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef CREATEDUALGRAPH_HPP
  2. #define CREATEDUALGRAPH_HPP
  3. #include <GraphLayoutLibrary_global.h>
  4. #include <vector>
  5. #include <boost/config.hpp>
  6. #include <boost/graph/adjacency_list.hpp>
  7. #include <boost/property_map/property_map.hpp>
  8. #include <GridBasedLayout/MyPlanarFaceTraversal.hpp>
  9. #include <iostream>
  10. #include <boost/graph/properties.hpp>
  11. #include <boost/graph/lookup_edge.hpp>
  12. #include <boost/ref.hpp>
  13. #include <algorithm>
  14. using namespace boost;
  15. /**
  16. * A structure to maintain dual graph of a planar graph. Inherited from my_planar_face_traversal_visitor.
  17. */
  18. template <typename InputGraph, typename OutputGraph, typename EdgeIndexMap>
  19. struct dual_graph_visitor : public my_planar_face_traversal_visitor
  20. {
  21. typedef typename graph_traits<OutputGraph>::vertex_descriptor vertex_t;
  22. typedef typename graph_traits<InputGraph>::edge_descriptor edge_t;
  23. typedef typename std::vector<vertex_t> vertex_vector_t;
  24. typedef iterator_property_map< typename vertex_vector_t::iterator, EdgeIndexMap > edge_to_face_map_t;
  25. typedef typename graph_traits<OutputGraph>::edge_descriptor edge_descriptor_t;
  26. typedef typename property_map<OutputGraph, edge_index_t>::type e_index_t;
  27. typedef typename graph_traits<OutputGraph>::edges_size_type edge_count_t;
  28. typedef typename graph_traits<OutputGraph>::edge_iterator edge_iteratot_t;
  29. dual_graph_visitor(InputGraph& arg_g, OutputGraph& arg_dual_g, EdgeIndexMap arg_em) :
  30. g(arg_g),
  31. dual_g(arg_dual_g),
  32. em(arg_em),
  33. edge_to_face_vector(num_edges(g), graph_traits<OutputGraph>::null_vertex()),
  34. edge_to_face(edge_to_face_vector.begin(), em)
  35. {
  36. }
  37. void begin_face()
  38. {
  39. //add a vertex (for current face being traversed) to dualG
  40. current_face = add_vertex(dual_g);
  41. }
  42. template <typename Edge, typename adjFaces, typename dualEdges>
  43. void next_edge(Edge& e, adjFaces &adjecentFaces, dualEdges& dual) //traverse edges bounding current face
  44. {
  45. vertex_t existing_face = edge_to_face[e];
  46. if (existing_face == graph_traits<OutputGraph>::null_vertex()) //if edge is traversed first time
  47. {
  48. edge_to_face[e] = current_face;
  49. }
  50. else //if edge is traversed second time
  51. {
  52. //add edge between its 2 adjecent faces (in dualG)
  53. edge_descriptor_t newEdge = add_edge(existing_face, current_face, dual_g).first;
  54. //Initialize the interior edge index in dualG
  55. e_index_t e_index = get(edge_index, dual_g);
  56. edge_count_t edge_count = 0;
  57. edge_iteratot_t ei, ei_end;
  58. for(boost::tie(ei, ei_end) = edges(dual_g); ei != ei_end; ++ei)
  59. put(e_index, *ei, edge_count++);
  60. //remember its dual edge from g
  61. dual[get(edge_index, dual_g, newEdge)] = e;
  62. }
  63. //remember current face as an 'adjecent face' to vertices on current edge (from g)
  64. adjecentFaces[get(vertex_index, g, source(e, g))].push_back(current_face); //adjecentFaces[u] <= current_face;
  65. adjecentFaces[get(vertex_index, g, target(e, g))].push_back(current_face); //adjecentFaces[v] <= current_face;
  66. }
  67. InputGraph& g;
  68. OutputGraph& dual_g;
  69. EdgeIndexMap em;
  70. vertex_t current_face;
  71. vertex_vector_t edge_to_face_vector;
  72. edge_to_face_map_t edge_to_face;
  73. };
  74. template <typename InputGraph, typename OutputGraph, typename PlanarEmbedding, typename EdgeIndexMap, typename adjFaces, typename dualEdges>
  75. void create_dual_graph(InputGraph& g, OutputGraph& dual_g, PlanarEmbedding embedding, EdgeIndexMap em, adjFaces &adjecentFaces, dualEdges& dualE)
  76. {
  77. dual_graph_visitor<InputGraph, OutputGraph, EdgeIndexMap> visitor(g, dual_g, em);
  78. planar_face_traversal(g, embedding, visitor, em, adjecentFaces, dualE);
  79. }
  80. template <typename InputGraph, typename OutputGraph, typename PlanarEmbedding, typename adjFaces, typename dualEdges>
  81. void create_dual_graph(InputGraph& g, OutputGraph& dual_g, PlanarEmbedding embedding, adjFaces &adjecentFaces, dualEdges& dualE)
  82. {
  83. create_dual_graph(g, dual_g, embedding, get(edge_index,g), adjecentFaces, dualE);
  84. }
  85. #endif // CREATEDUALGRAPH_HPP