C++ Boost 图库 : outputting custom vertex properties

标签 c++ boost graphviz boost-graph

我正在努力让自定义属性编写器与 BGL 一起工作。

struct IkGraph_VertexProperty {
    int id_ ;
    int type_ ;
    std::pair<int,int> gaussians_ ; // Type of Joint, Ids of Gaussians
};


struct IkGraph_VertexPropertyTag
{
typedef edge_property_tag kind;
static std::size_t const num; 
};

std::size_t const IkGraph_VertexPropertyTag::num = (std::size_t)&IkGraph_VertexPropertyTag::num;

typedef property<IkGraph_VertexPropertyTag, IkGraph_VertexProperty> vertex_info_type;

...方法中定义的自定义图形

typedef adjacency_list<setS, vecS, bidirectionalS, vertex_info_type, IkGraph_EdgeProperty> TGraph ;
TGraph testGraph ;
std::ofstream outStr(filename) ;
write_graphviz(outStr, testGraph, OurVertexPropertyWriter<TGraph,IkGraph_VertexPropertyTag, IkGraph_VertexProperty>(testGraph));

...

template <class Graph, class VertexPropertyTag, class VertexProperty>
struct OurVertexPropertyWriter {

  OurVertexPropertyWriter(Graph &g_) : g(g_) {}

 template <class Vertex>
 void operator() (std::ostream &out, Vertex v) {

    VertexProperty p = get (VertexPropertyTag(), g, v);
      out << "[label=" << p.gaussians_.first << "]";

  }

 Graph &g;
};

这会产生一系列错误。

我真正想做的(不知道这是否可能)是能够概括这一点并传递哪些自定义属性存在/我想输出哪些。

最佳答案

我不会更正您的代码,因为我无法验证它是否会按预期工作。 但是由于我遇到了同样的问题,我将发布我的代码的相关部分作为您和其他人的示例。我希望这可能有所帮助。

图的定义

typedef boost::adjacency_list<boost::vecS, 
                              boost::vecS, 
                              boost::bidirectionalS, 
                              boost::no_property, 
                              EdgeProp, //this is the type of the edge properties
                              boost::no_property, 
                              boost::listS> Graph;

边缘属性

struct EdgeProp
{
        char name;
        //...

};

边的属性编写器

template <class Name>
class myEdgeWriter {
public:
     myEdgeWriter(Name _name) : name(_name) {}
     template <class VertexOrEdge>
     void operator()(std::ostream& out, const VertexOrEdge& v) const {
            out << "[label=\"" << name[v].name << "\"]";
     }
private:
     Name name;
};

属性必须预先附加到边上。 例如

EdgeProp p;
p.name = 'a';
g[edge_descriptor] = p;

调用 boost 以创建 graphviz 文件

myEdgeWriter<Graph> w(g);
ofstream outf("net.gv");
boost::write_graphviz(outf,g,boost::default_writer(),w);

对于顶点属性编写器,我们只使用默认编写器。

关于C++ Boost 图库 : outputting custom vertex properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10235927/

相关文章:

c++ - 为什么非限定名称查找会找到所有重载声明

c++ - 排序 vector : how to have the boolian function accept a third variable

c++ - 如何更改 boost 线程限制?

Rgraphviz : edge labels outside plotting region

graphviz - 改进节点标签上下标和上标的定位

android - JNI 中 const char* 的生命周期是多少?

c++ - 这两个 std::vector 的赋值方法有什么区别?

c++ - std::bind 占位符的重载运算符

宏中的 C++ 编译时间计数器

scikit-learn - Graphviz.Source 未在 Jupyter Notebook 中呈现