c++ - Manually colouring of boost's graphs -
i'm struggling trying manually colouring graph's vertices using boost. wrote code below can't figure out why generated file not have colour.
int main(int,char*[]) { typedef property<edge_name_t, string> edgeproperties; typedef property<vertex_name_t, string, property<vertex_color_t, default_color_type>> vertexproperties; typedef adjacency_list<vecs, vecs, directeds, vertexproperties, edgeproperties> graph; typedef graph_traits<graph>::vertex_descriptor vertex; typedef graph_traits<graph>::edge_descriptor edge; graph g; property_map<graph, vertex_name_t>::type vertex_label = get(vertex_name, g); property_map<graph, vertex_color_t>::type color_map = get(vertex_color, g); property_map<graph, edge_name_t>::type edge_label = get(edge_name, g); vertex v1 = add_vertex(g); vertex_label[v1] = "v1"; put(color_map, v1, boost::red_color); std::ofstream outf("example.gv"); write_graphviz(outf, g, make_label_writer(vertex_label), make_label_writer(edge_label) ); return 0; }
vertex colourings algorithmic detail. there no "automatic" provision translate graphviz, far know.
you can add custom attributes though.
i'd change around use dynamic properties:
std::ofstream outf("example.gv"); dynamic_properties dp; dp.property("node_id", get(vertex_index, g)); dp.property("label", vertex_label); dp.property("label", edge_label); write_graphviz_dp(outf, g, dp);
now it's simple adding new vertex attribute dynamic set:
dp.property("color", make_transform_value_property_map(&color_to_string, color_map));
the complication need supply conversion default_color_type
text. symptom of fact color maps aren't used representational purposes. here's working sample:
#include <boost/graph/adjacency_list.hpp> #include <boost/graph/graphviz.hpp> #include <iostream> using namespace boost; inline char const* color_to_string(default_color_type c) { switch(c) { case default_color_type::red_color: return "red"; case default_color_type::green_color: return "green"; case default_color_type::gray_color: return "gray"; case default_color_type::white_color: return "white"; case default_color_type::black_color: return "black"; } return ""; // not known } int main() { typedef property<edge_name_t, std::string> edgeproperties; typedef property<vertex_name_t, std::string, property<vertex_color_t, default_color_type>> vertexproperties; typedef adjacency_list<vecs, vecs, directeds, vertexproperties, edgeproperties> graph; typedef graph_traits<graph>::vertex_descriptor vertex; graph g; property_map<graph, vertex_name_t>::type vertex_label = get(vertex_name, g); property_map<graph, vertex_color_t>::type color_map = get(vertex_color, g); property_map<graph, edge_name_t>::type edge_label = get(edge_name, g); vertex v1 = add_vertex(g); vertex_label[v1] = "v1"; put(color_map, v1, boost::red_color); dynamic_properties dp; dp.property("node_id", get(vertex_index, g)); dp.property("label", vertex_label); dp.property("label", edge_label); dp.property("color", make_transform_value_property_map(&color_to_string, color_map)); write_graphviz_dp(std::cout, g, dp); }
prints:
digraph g { 0 [color=red, label=v1]; }
Comments
Post a Comment