获取Boost::图的顶点属性
c++ Getting vertex properties of a Boost::Graph
给定顶点:
class VertexProps {
public:
int id;
float frame;
string name;
};
我已经使用捆绑属性初始化了我的boost图。我知道我可以使用:
来获取框架std::cout << "Vertex frame: " << boost::get(&VertexProps::frame, graph, vertex) << std::endl;
//Need to make this work: float frame = boost::get(&VertexProps::frame, graph, vertex);
//graph is a boost::adjacency_list and vertex is a boost::vertex_descriptor
然而,我想写一个更通用的函数或包装器,这样:
std::vector<float> frames;
std::string prop_name = "frame";
float frame = graph.get_vertex_prop(vertex, prop_name);
frames.push_back(frame);
我希望是这样的:
typedef boost::variant< int, unsigned int, float, std::string > PropValType;
typedef boost::vertex_bundle_type<Graph>::type PropIdType;
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
PropValType get_vertex_prop(Vertex& v, PropIdType pname)
{
boost::get(pname, graph, v);
//If pname = frame then return value as float (or cast boost::variant to float)
//If pname = name then return value as a string
}
我想避免这样的事情:
PropValType get_vertex_prop(Vertex& v, std::string pname) {
if (pname == "frame") {
boost::get(&VertexProps::frame, graph, v)
//return value as float
}
if (...)
}
在编译时没有任何宏魔术是无法做到这一点的。c++不允许字符串字面值作为非类型模板参数,并且具有非常弱的反射功能。
您提出的(并想要避免的)解决方案需要在运行时进行一些工作,通常应该避免。
宏观解决方案如下:
#define MY_GET(v, pname) boost::get(&VertexProps##pname, graph, v)
PropValType v = MY_GET(v, frame);
相关文章:
- 我对 MyGraph 属性顶点名称和边权重有问题
- BGL:无法访问捆绑的顶点属性
- 输出(用于 GraphViz)提升图顶点及其属性,使用具有私有变量的类作为捆绑属性
- 我可以将单个顶点索引与具有所有属性(位置、法线、texCoord)的顶点缓冲区一起使用吗?
- 可以在同一 VBO 中存储不同的顶点属性和索引
- Boost:列出GraphMl中指定的顶点和边缘属性
- 如何使用boost::graph dijkstra的算法,如果顶点属性是指针?
- BGL Adjacency_list:如何使用顶点属性(而不是描述符)对OUT_EDGE进行排序
- 在 OpenGL 中,为什么除非我再次重新指定顶点属性,否则我的缓冲区对象不会绘制?
- 打开"GL着色器存储缓冲区对象"以替换"顶点属性"
- 如何在 GLSL 中使用 4x4 矩阵作为顶点属性
- 顶点属性 - 对顶点位置使用短点而不是浮点数
- OpenGL顶点属性指针或着色器不工作
- 顶点属性大小不匹配时会发生什么情况
- C++ 提升图形库:输出自定义顶点属性
- 获取Boost::图的顶点属性
- 发送规范化顶点属性会产生垃圾
- Opengl ES 2.0的顶点属性为ivec4
- 当不使用某些顶点属性时,glDrawElements崩溃
- 自定义顶点属性