c++ - Boost图形库,修复节点大小

标签 c++ vertex boost-graph

我正在使用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设置为我的顶点。

最佳答案

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

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

  • 因此,您需要为这些 map 使用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

    Note though that the attributes are being written to temporary streams, so std::boolalpha is not honored. Also note that using a string literal would lead to the propertymap instantiating with char const(&)[5], which is why +"true" is used (decaying to 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];
    }
    

    关于c++ - Boost图形库,修复节点大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60396369/

    相关文章:

    c++ - 是否需要诊断非类型模板参数中的缩小转换?

    c++ - 洗牌字符串

    c++ - OpenGL - 动画是由着色器完成的吗?

    c++ - 有效地扩展图边集

    c++ - 如果我想使用 std::shared_ptr,要包含哪个 header ?

    javascript - WebGL:没有 VBO 绑定(bind)到启用的顶点。顶点索引需要 "vertexAttribPointer"吗?

    c++ - (C++ 和 OpenGL)我试图在批处理渲染器中旋转一组顶点(它将模拟一个正方形),但它不是 100% 工作 :(

    c++ - BOOST 中的属性映射是什么?

    boost - 输出 BGL 边权重

    c++ - C++ STL 中的 std::list<std::pair> 和 std::map 有什么区别?