【问题标题】:Writing boost dynamic properties to a file using Boost Graph Library使用 Boost Graph Library 将 boost 动态属性写入文件
【发布时间】:2015-12-15 15:04:01
【问题描述】:

我已经问过一个问题here,关于使用 Boost Graph Library 并将图形写入文件。由于我的要求发生了变化,我需要将动态图形属性写入 DOT 文件。经过一番查找,我设法想出了一些代码,但它不起作用。以下是我到目前为止所做的:

Map 类使用 Cell 类作为顶点,而 Cell 类使用单独的 CellProperty 类来设置和获取所有 Cell 属性。

最后是我构建图形并尝试将图形写入 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;
};

地图.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() 的正确参数。请帮帮我。谢谢!!

【问题讨论】:

  • 如果这已经是一个最小的例子,那么祝你好运,没有明确的问题陈述。

标签: c++ boost graph boost-graph boost-property-map


【解决方案1】:

您可以在包含顶点属性结构的属性映射顶部使用transform_value_property_map。 (你没有显示)。

我有很多答案显示如何做到这一点,虽然这些都是使用内部属性,但没有太大区别,因为无论属性映射是内部还是外部,anu 属性映射都可以以相同的方式进行转换(这就是属性映射的全部目的:解耦访问属性的方式)。

最相关:

其他:

【讨论】:

  • 谢谢谢赫。因此,如果我理解正确,那么外部属性必须以某种方式映射到内部属性上。查看您发布的链接之一,我更改了我的图表并添加了一个内部属性。请看一下上面编辑过的代码。由于这个原因,我仍然会遇到很多错误: boost::property_map::type vertex_label = boost::get(boost::vertex_name, mGraph);
  • 你好,谢赫。你救了我 !!您的一个链接是解决我问题的关键。我使用了一个使用 make_transform_value_property_map 的 lambda 并且确实有效。我现在将更新上面的代码,所以有需要的人可以使用它。非常感谢您一直以来的支持。
  • 干杯!我认为用解决方案编辑问题有点令人困惑(除非明确标记为更新)。另外,请注意在 lambda (cell.GetProperty&lt;bool&gt;("Navigable", false);) 中包含这些内容是多么重要,没有人可以为您弥补这一点,因为信息不存在。
  • 嗨@sehe。我又来这里是因为读取 DOT 文件的一些问题。我已经发布了一个单独的问题。 bit.ly/1P7fT0u 请帮帮我。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2011-12-08
  • 1970-01-01
  • 2012-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-09
  • 1970-01-01
相关资源
最近更新 更多