c + + Boost图库:输出自定义顶点属性 [英] C++ Boost Graph Library: outputting custom vertex properties
问题描述
我挣扎着爬一个自定义属性作家BGL工作。
I am struggling to get a custom property writer to work with 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;
...在方法定义的自定义图
...custom graph defined in method
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;
};
这会产生错误的数据流。
This produces a stream of errors.
我真的想这样做(和不知道如果这是可能的)是能够概括这一点,并通过其自定义属性存在/我想输出。
What I would really like to do (and no idea if this is possible) is to be able to generalize this and be pass which custom properties exist / which I would like outputting.
推荐答案
我不纠正你的code,因为我不能够验证预期它会工作。
但自从我被困在了同样的问题,我会后我的code的相关部分,作为您和他人的例子。我希望这会有所帮助。
I won't correct your code, because I'm not able to verify that it will work as expected. But since I got stuck at the same problem, I will post the relevant parts of my code as an example for you and others. I hope this might be helpful.
图的定义
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;
拨打提振创建的Graphviz文件
myEdgeWriter<Graph> w(g);
ofstream outf("net.gv");
boost::write_graphviz(outf,g,boost::default_writer(),w);
有关的顶点属性的作家,我们只使用默认的作家。
For the vertex property writer we just use the default writer.
这篇关于c + + Boost图库:输出自定义顶点属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!