使用 Boost Graph Library 将 boost 动态属性写入文件
Writing boost dynamic properties to a file using Boost Graph Library
我已经问过 关于使用 Boost 图形库和将图形写入文件的问题。由于我的要求发生变化,我需要将动态图形属性写入 DOT 文件。经过一番查找,我设法想出了一些代码,但它不起作用。以下是我到目前为止所做的:
Map class 使用 Cell class 作为顶点,Cell class 使用单独的 CellProperty class 来设置和获取所有 Cell 属性。
最后是 Map class,我在其中构建图表并尝试将图表写入 DOT 文件。
Map.h
class Map {
public:
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, Cell> Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
explicit Map(std::string pGraphFilePath);
virtual ~Map();
void LoadGraph();
private:
Graph mGraph;
std::vector<std::vector<Vertex>> mGrid;
};
Map.cpp
const unsigned int RowNum = 3;
const unsigned int ColumnNum = 4;
Map::Map(std::string pGraphFilePath) : mGraph(), mGrid() {}
Map::~Map() {}
void Map::LoadGraph() {
int dummyID = 1;
for (unsigned int row = 0; row < RowNum; row++) {
mGrid.resize(RowNum);
for (unsigned int col = 0; col < ColumnNum; col++) {
mGrid[row].resize(ColumnNum);
Vertex vID = boost::add_vertex(mGraph);
mGraph[vID].SetProperty<unsigned int>("ID", dummyID);
mGraph[vID].SetProperty<bool>("Navigable", true);
mGrid[row][col] = vID;
dummyID++;
// add the edges for the contained cells in the grid
if (col > 0) { boost::add_edge(mGrid[row][col - 1], mGrid[row][col], mGraph); }
if (row > 0) { boost::add_edge(mGrid[row - 1][col], mGrid[row][col], mGraph); }
}
}
// write cell properties
boost::dynamic_properties propertiesOutPut;
propertiesOutPut.property("ID", boost::get(boost::vertex_index, mGraph));
// As Navigable is an external property, it need to be mapped with the internal graph property
// the lines below are the update after I got the answers and link for my query
// cell.GetProperty() is a templated method the takes a default parameter, thus passing "false" bool parameter which returns the "Navigable" cell property
auto valueNavigable = boost::make_transform_value_property_map([](Cell &cell) { return cell.GetProperty<bool>("Navigable", false); }, boost::get(boost::vertex_bundle, mGraph));
propertiesOutPut.property("Navigable", valueNavigable);
std::ofstream fout("MyGraph.dot");
boost::write_graphviz_dp(fout, mGraph, propertiesOutPut, std::string("ID"));
}
我遇到的问题是 boost::get() 的 propertiesOutPut.property() 方法。我无法找出 boost::get() 的正确参数。请帮帮我。谢谢!!
您可以在包含顶点属性结构的 属性 地图之上使用 transform_value_property_map
。 (你没有显示)。
我有很多答案展示了如何做到这一点,虽然这些都是使用内部属性,但没有太大区别,因为 anu 属性 地图可以用相同的方式转换,无论是否属性 映射是内部的或外部的(这就是 属性 映射的全部目的:解耦访问属性的方式)。
最相关:
其他:
我已经问过
Map class 使用 Cell class 作为顶点,Cell class 使用单独的 CellProperty class 来设置和获取所有 Cell 属性。
最后是 Map class,我在其中构建图表并尝试将图表写入 DOT 文件。
Map.h
class Map {
public:
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, Cell> Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
explicit Map(std::string pGraphFilePath);
virtual ~Map();
void LoadGraph();
private:
Graph mGraph;
std::vector<std::vector<Vertex>> mGrid;
};
Map.cpp
const unsigned int RowNum = 3;
const unsigned int ColumnNum = 4;
Map::Map(std::string pGraphFilePath) : mGraph(), mGrid() {}
Map::~Map() {}
void Map::LoadGraph() {
int dummyID = 1;
for (unsigned int row = 0; row < RowNum; row++) {
mGrid.resize(RowNum);
for (unsigned int col = 0; col < ColumnNum; col++) {
mGrid[row].resize(ColumnNum);
Vertex vID = boost::add_vertex(mGraph);
mGraph[vID].SetProperty<unsigned int>("ID", dummyID);
mGraph[vID].SetProperty<bool>("Navigable", true);
mGrid[row][col] = vID;
dummyID++;
// add the edges for the contained cells in the grid
if (col > 0) { boost::add_edge(mGrid[row][col - 1], mGrid[row][col], mGraph); }
if (row > 0) { boost::add_edge(mGrid[row - 1][col], mGrid[row][col], mGraph); }
}
}
// write cell properties
boost::dynamic_properties propertiesOutPut;
propertiesOutPut.property("ID", boost::get(boost::vertex_index, mGraph));
// As Navigable is an external property, it need to be mapped with the internal graph property
// the lines below are the update after I got the answers and link for my query
// cell.GetProperty() is a templated method the takes a default parameter, thus passing "false" bool parameter which returns the "Navigable" cell property
auto valueNavigable = boost::make_transform_value_property_map([](Cell &cell) { return cell.GetProperty<bool>("Navigable", false); }, boost::get(boost::vertex_bundle, mGraph));
propertiesOutPut.property("Navigable", valueNavigable);
std::ofstream fout("MyGraph.dot");
boost::write_graphviz_dp(fout, mGraph, propertiesOutPut, std::string("ID"));
}
我遇到的问题是 boost::get() 的 propertiesOutPut.property() 方法。我无法找出 boost::get() 的正确参数。请帮帮我。谢谢!!
您可以在包含顶点属性结构的 属性 地图之上使用 transform_value_property_map
。 (你没有显示)。
我有很多答案展示了如何做到这一点,虽然这些都是使用内部属性,但没有太大区别,因为 anu 属性 地图可以用相同的方式转换,无论是否属性 映射是内部的或外部的(这就是 属性 映射的全部目的:解耦访问属性的方式)。
最相关:
其他: