Boost graphviz自定义顶点标签 [英] Boost graphviz custom vertex labels

查看:104
本文介绍了Boost graphviz自定义顶点标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个项目的代码,代表一些概率树,并使用自定义结构的顶点和边缘类型:

Currently I have the following code for a project that represents some probability trees and uses custom structs for the vertex and edge types:

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>

struct Vertex{
    std::string name;
    size_t times_visited;
    float value;
    bool terminal_node;
    bool root_node;
};

struct Edge{
    float probability;
};

//Some typedefs for simplicity
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::directedS, Vertex, Edge> directed_graph_t;

typedef boost::graph_traits<directed_graph_t>::vertex_descriptor vertex_t;
typedef boost::graph_traits<directed_graph_t>::edge_descriptor edge_t;

我目前有一个简单的视觉表示的一些简单的树使用boost graphviz,但没有标签。我想让顶点之间的连接用在Edge结构中找到的概率标记,顶点用在Vertex结构中找到的关联名称标记。我第一次尝试这样做是使用这个代码:

I currently have a simple visual representation of some simple trees using boost graphviz but with no labels. I would like to have the connections between vertices being labeled with the probability found in the Edge struct and the vertices labeled with the associate name found in the Vertex struct. My first attempt at doing this was to use this code:

std::ofstream outf("test.dot");
boost::dynamic_properties dp;
dp.property("name", get(&Vertex::name, test));
dp.property("node_id", get(boost::vertex_index, test));
write_graphviz_dp(outf, test, dp);

但是这似乎不能做我想要的,因为它不输出顶点的名称。我应该在这里改变什么?

But this seems to not do what I want as it doesn't output the name of the vertex. What should I be changing here?

推荐答案

看起来像这只是因为我不理解graphviz正确。以下代码按预期工作:

Looks like this was just because I didn't understand graphviz properly. The following code worked as expected:

std::ofstream outf("test.dot");
boost::dynamic_properties dp;
dp.property("label", boost::get(&Vertex::name, test));
dp.property("node_id", boost::get(boost::vertex_index, test));
dp.property("label", boost::get(&Edge::probability, test));
write_graphviz_dp(outf, test, dp);

这篇关于Boost graphviz自定义顶点标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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