平面图形绘制c++

Planar graph drawing C++

本文关键字:c++ 绘制 图形 平面图 平面      更新时间:2023-10-16

我读了很多关于在平面上画平面图形的文章,我尝试了很多库。

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

选择Boost Graph Library中的函数chrobak_payne_straight_line_drawing

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

但它不起作用,并调用System。AccessViolationException在文件chrobak_payne_drawing.hpp:

125行
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;  
    }

也许我必须首先使图形最大平面?但是我没有成功……请帮助我在我的代码中添加必要的函数,我不太确定转换图形的步骤。

提前感谢你——我最后的希望!

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

        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保持未初始化。崩溃!这就是我看到的

您使用的图形不是最大平面。在进行规范排序或嵌入之前,需要使用make_connected、make_biconnected_planar和make_maximal_planar函数。虽然源代码中的注释解释了这一点,但它们的使用并不完全直观,我找不到使用它们和函数chrobak_payne_straight_line_drawing相结合的源代码。以下是我从原始资料和其他示例中改编的示例。