在给定点文本的情况下生成GraphViz图的图像c ++ [英] Generate image of GraphViz graph given dot text c++

查看:103
本文介绍了在给定点文本的情况下生成GraphViz图的图像c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++ Qt应用程序中(在Ubuntu上)有一个字符串,其中包含有效的GraphViz/点图语法.我想从此文本生成一个图像文件/对象,类似于各种在线工具(例如这样的图像): http ://www.webgraphviz.com/)吐出.也许我使用了错误的搜索词,但似乎找不到与此相关的帮助.

I have a string in a C++ Qt application (on Ubuntu) which contains valid GraphViz/dot graph syntax. I want to generate an image file/object from this text, similar to the images that various online tools (like this one: http://www.webgraphviz.com/) spit out. Maybe I'm using wrong search terms, but I can't seem to find relevant help with this.

我基本上想要的是这样的东西:

What I basically want is something like this:

generate_dot_graph_image(std::string dot_text, std::string image_file_path)

其他详细信息:我有一个Dijkstra求解器,我想在其应用程序中可视化其解决方案(基本上是除去未使用的边后的原始图形).求解器已经包含一个选项,可以将解决方案转换为可以使用实用工具(例如,我上面链接的工具)解析为点阵图的字符串.但是我想要的是能够从C ++内部执行此操作.

Additional details: I have a Dijkstra solver whose solution (basically the original graph after removing non-used edges) I want to visualize inside my application. The solver already includes an option to convert the solution to a string that can be parsed as a dot graph using a utility such as the one I linked above. But what I want is to be able to do this from inside C++.

推荐答案

因此,使用GraphViz库,我可以完全按照自己的意愿进行操作.您可以使用sudo apt-get install graphviz-libsudo apt-get install libgraphviz-dev在Ubuntu上安装它们.完成后:

So I was able to do exactly what I wanted using the GraphViz libraries. You can install them on Ubuntu using sudo apt-get install graphviz-lib and sudo apt-get install libgraphviz-dev. Once that's done:

#include <graphviz/gvc.h>

bool DotGraphGenerator::saveImage()
{
  std::string o_arg = std::string("-o") + "/path/to/image_file.png";
  char* args[] = {const_cast<char*>("dot"), const_cast<char*>("Tpng"), const_cast<char*>("-Gsize=8,4!"), const_cast<char*>("-Gdpi=100"),
  const_cast<char*>(DOT_TEXT_FILE.c_str()),  //DOT_TEXT_FILE is the file path in which the graph is saved as valid DOT syntax
  const_cast<char*>(o_arg.c_str()) };

  const int argc = sizeof(args)/sizeof(args[0]);
  Agraph_t *g, *prev = NULL;
  GVC_t *gvc;

  gvc = gvContext();
  gvParseArgs(gvc, argc, args);

  while ((g = gvNextInputGraph(gvc)))
  {
    if (prev)
    {
      gvFreeLayout(gvc, prev);
      agclose(prev);
    }
    gvLayoutJobs(gvc, g);
    gvRenderJobs(gvc, g);
    prev = g;
  }

  return !gvFreeContext(gvc);
}

gvc是一个C库,函数将非const C字符串作为参数,因此从一开始就是const_casts.您还可以通过更改-Gsize=8,4-Gdpi=100参数来编辑图像尺寸.使用当前配置,您将获得一个8 * 100 x 4 * 100 = 800x400的图像文件.无论如何,这些参数与从bash运行dot命令时所应用的参数相同.

gvc is a C library, and the functions take non-const C strings as arguments, hence the const_casts in the beginning. You can also edit the image size by altering the -Gsize=8,4 and -Gdpi=100 args. With the current configuration you'll get an 8*100 x 4*100 = 800x400 image file. Anyway, these arguments are the same as you would apply when running the dot command from bash.

除此之外,该代码基本上是从graphViz中的示例之一作为库手册复制而来的:

Other than that, this code is basically copied from one of the examples in the graphViz as a library manual: http://www.graphviz.org/pdf/libguide.pdf

这篇关于在给定点文本的情况下生成GraphViz图的图像c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆