Boost图形库,修复节点大小

问题描述 投票:1回答:1

我正在使用Boost Graph Library和Graphviz构建图形。在我的代码中,我有一个将Graph导出为.dot格式的类。我使用以下几行来设置图形的属性:

dynamic_properties dp;

dp.property("rankdir", boost::make_constant_property<Graph*>(std::string("TB")));
dp.property("node_id", get(&VertexP::tag, m_graph));
dp.property("label", get(&VertexP::tag, m_graph));
dp.property("shape", get(&VertexP::shape, m_graph));
dp.property("style", get(&VertexP::style, m_graph));
dp.property("pos", get(&VertexP::pos, m_graph));
dp.property("label", get(&ArrowP::symbol, m_graph));
dp.property("color", get(&ArrowP::color, m_graph));
dp.property("style", get(&ArrowP::style, m_graph));
write_graphviz_dp(ss, m_graph, dp);

[我正在尝试使用graphviz的fixedsize关键字以及widthheight关键字来固定我的节点的大小(根据标签中文本的数量,它们的大小不同。

我想通过dp.property方法进行设置。目的是在我的.dot文档中添加以下行:

node [ fixedsize = true, width = 1.1, height = 1.1]

我尝试了以下操作,但不起作用:

dp.property("fixedsize", boost::make_constant_property<VertexP*>(std::string("true")));
dp.property("width", boost::make_constant_property<VertexP*>(std::string("1")));
dp.property("height", boost::make_constant_property<VertexP*>(std::string("1")));

您知道如何在dp.property中的点文件中将属性fixedsize设置为我的顶点。

c++ vertex boost-graph
1个回答
0
投票

property maps中,key_type(boost::property_traits<PMap>::key_type)通知库属性所属的对象类型:

  • [graph_traits<G>::vertex_descriptor将属性附加到节点类型的对象(顶点)
  • [graph_traits<G>::edge_descriptor将属性附加到边缘
  • [G*在图级别附加属性

因此,对于这些地图,您需要使用vertex_descriptor而不是VertexP*

dp.property("fixedsize", boost::make_constant_property<Graph::vertex_descriptor>(+"true"));
dp.property("width",     boost::make_constant_property<Graph::vertex_descriptor>(1));
dp.property("height",    boost::make_constant_property<Graph::vertex_descriptor>(1));

请注意,由于使用了默认的流操作,因此无需专门转换为std::string

尽管请注意,属性已写入临时流,所以不遵守std::boolalpha。还要注意,使用字符串文字会导致属性映射以char const(&)[5]实例化,这就是为什么使用+"true"(衰减为char const*)的原因。

完整演示

[Live On Coliru] >>

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

struct VertexP {
    std::string tag, shape, style, pos;
};

struct ArrowP {
    std::string symbol, color, style;
};

using Graph = boost::adjacency_list<
    boost::vecS, boost::vecS, boost::directedS,
    VertexP, ArrowP>;

int main() {
    Graph g;

    boost::dynamic_properties dp;
    dp.property("rankdir", boost::make_constant_property<Graph*>(+"TB"));
    dp.property("node_id", get(&VertexP::tag,   g));
    dp.property("label",   get(&VertexP::tag,   g));
    dp.property("shape",   get(&VertexP::shape, g));
    dp.property("style",   get(&VertexP::style, g));
    dp.property("pos",     get(&VertexP::pos,   g));
    dp.property("label",   get(&ArrowP::symbol, g));
    dp.property("color",   get(&ArrowP::color,  g));
    dp.property("style",   get(&ArrowP::style,  g));

    dp.property("fixedsize", boost::make_constant_property<Graph::vertex_descriptor>(+"true"));
    dp.property("width",     boost::make_constant_property<Graph::vertex_descriptor>(1));
    dp.property("height",    boost::make_constant_property<Graph::vertex_descriptor>(1));

    add_edge(
            add_vertex({"foo", "diamond", "dotted", "0,1"}, g),
            add_vertex({"bar", "circle", "dashed", "1,1"}, g),
            {"foo-bar", "blue", "dashed"},
            g);

    write_graphviz_dp(std::cout, g, dp);
}

打印

digraph G {
rankdir=TB;
bar [fixedsize=true, height=1, label=bar, pos="1,1", shape=circle, style=dashed, width=1];
foo [fixedsize=true, height=1, label=foo, pos="0,1", shape=diamond, style=dotted, width=1];
foo->bar  [color=blue, label="foo-bar", style=dashed];
}
© www.soinside.com 2019 - 2024. All rights reserved.