平面图绘制C ++

时间:2011-11-28 16:56:38

标签: c++ graph

我已经阅读了很多关于在平面上绘制平面图的文章,我尝试了很多库。

最后,我需要指定输入图形,输出以获得其顶点的新坐标,以使边缘不相交。

选择落在Boost图库中的 chrobak_payne_straight_line_drawing 函数上。

我在下图中测试了它:http://cboard.cprogramming.com/attachments/cplusplus-programming/11153d1322430048-graph-planarization-boost-library-1grb.jpg

但它没有用,并在文件的第125行chrobak_payne_drawing.hpp上调用 System.AccessViolationException

delta_x[v3] = 1;

但是在这张图上,它有效:http://cboard.cprogramming.com/attachments/cplusplus-programming/11154d1322430090-graph-planarization-boost-library-2gr.jpg

请帮助,我需要运行所有平面图,并且不会导致严重错误。下面我介绍代码,我测试了第一张图并得到了一个严重错误,顺便说一下,我正在使用visual studio 2010。

    #include <iostream>
    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/properties.hpp>
    #include <boost/graph/graph_traits.hpp>
    #include <boost/property_map/property_map.hpp>
    #include <vector>
    #include <stack>

    #include <boost/graph/planar_canonical_ordering.hpp>
    #include <boost/graph/is_straight_line_drawing.hpp>
    #include <boost/graph/chrobak_payne_drawing.hpp>
    #include <boost/graph/boyer_myrvold_planar_test.hpp>

    using namespace boost;

    //a class to hold the coordinates of the straight line embedding
    struct coord_t
    {
      std::size_t x;
      std::size_t y;
    };

    int main(int argc, char** argv)
    {
      typedef adjacency_list
        < vecS,
          vecS,
          undirectedS,
          property<vertex_index_t, int>
        > graph;  

      //Define the storage type for the planar embedding
      typedef std::vector< std::vector< graph_traits<graph>::edge_descriptor > > 
        embedding_storage_t;
      typedef boost::iterator_property_map
        < embedding_storage_t::iterator, 
          property_map<graph, vertex_index_t>::type 
        >
        embedding_t;

      // Create the graph - a maximal planar graph on 7 vertices. The functions
      // planar_canonical_ordering and chrobak_payne_straight_line_drawing both
      // require a maximal planar graph. If you start with a graph that isn't
      // maximal planar (or you're not sure), you can use the functions
      // make_connected, make_biconnected_planar, and make_maximal planar in
      // sequence to add a set of edges to any undirected planar graph to make
      // it maximal planar.

      graph g(5);
      add_edge(0,3, g);
      add_edge(0,4, g);
      add_edge(1,3, g);
      add_edge(1,4, g);
      add_edge(2,3, g);
      add_edge(2,4, g);

      // Create the planar embedding
      embedding_storage_t embedding_storage(num_vertices(g));
      embedding_t embedding(embedding_storage.begin(), get(vertex_index,g));

      boyer_myrvold_planarity_test(boyer_myrvold_params::graph = g,
                                   boyer_myrvold_params::embedding = embedding
                                   );



      // Find a canonical ordering
      std::vector<graph_traits<graph>::vertex_descriptor> ordering;
      planar_canonical_ordering(g, embedding, std::back_inserter(ordering));


      //Set up a property map to hold the mapping from vertices to coord_t's
      typedef std::vector< coord_t > straight_line_drawing_storage_t;
      typedef boost::iterator_property_map
        < straight_line_drawing_storage_t::iterator, 
          property_map<graph, vertex_index_t>::type 
        >
        straight_line_drawing_t;

      straight_line_drawing_storage_t straight_line_drawing_storage
        (num_vertices(g));
      straight_line_drawing_t straight_line_drawing
        (straight_line_drawing_storage.begin(), 
         get(vertex_index,g)
         );



      // Compute the straight line drawing
      chrobak_payne_straight_line_drawing(g, 
                                          embedding, 
                                          ordering.begin(),
                                          ordering.end(),
                                          straight_line_drawing
                                          );



      std::cout << "The straight line drawing is: " << std::endl;
      graph_traits<graph>::vertex_iterator vi, vi_end;
      for(tie(vi,vi_end) = vertices(g); vi != vi_end; ++vi)
        {
          coord_t coord(get(straight_line_drawing,*vi));
          std::cout << *vi << " -> (" << coord.x << ", " << coord.y << ")" 
                    << std::endl;
        }

      // Verify that the drawing is actually a plane drawing
      if (is_straight_line_drawing(g, straight_line_drawing))
       std::cout << "Is a plane drawing." << std::endl;
      else
        std::cout << "Is not a plane drawing." << std::endl;
      return 0;  
    }

Mayber我必须首先使图形最大平面?但是我没有成功..请帮我添加我的代码必要的功能,我不太清楚转换图表的步骤。

先谢谢,你 - 我最后的希望!

2 个答案:

答案 0 :(得分:0)

这看起来像是boost :: graph中的一个错误。我有一个不同的访问冲突,可能是因为一个不同的提升版本,但肯定有一个错误。

        left[v] = right[leftmost];
        vertex_t next_to_rightmost;
        for(vertex_t temp = leftmost; temp != rightmost;
            temp = right[temp]
            )
          {
            next_to_rightmost = temp;
          }

        right[next_to_rightmost] = graph_traits<Graph>::null_vertex();

此处leftmost==rightmost时,for循环未执行且next_to_rightmost仍未初始化。崩溃!这就是我所看到的。

答案 1 :(得分:0)

您使用的图表不是最大平面。在进行规范排序或嵌入之前,需要使用函数make_connected,make_biconnected_planar和make_maximal_planar。虽然源代码中的注释解释了这一点,但它们的使用并不完全直观,我找不到使用它们的源和函数chrobak_payne_straight_line_drawing的组合。 Here is an example I adapted from the original source and other examples