3

我刚刚开始使用 OGDF,并尝试通过运行 OGDF 网页上 How-Tos 下的一些示例来掌握它。我的代码可以编译,但是当我尝试在节点上调用 GraphAttributes 函数时会出现段错误。

这是我的代码:

   ogdf::Graph G;
   ogdf::GraphAttributes GA(G);

   if (!ogdf::GraphIO::readGML(G, "sierpinski_04.gml") ) {
      std::cerr << "Could not load sierpinski_04.gml" << std::endl;
      return 1;
   }


   ogdf::node v;

   GA.setAllHeight(10.0);
   GA.setAllWidth(10.0);

   ogdf::FMMMLayout fmmm;

   fmmm.useHighLevelOptions(true);
   fmmm.unitEdgeLength(15.0);
   fmmm.newInitialPlacement(true);
   //fmmm.qualityVersusSpeed(ogdf::FMMMLayout::qvsGorgeousAndEfficient);

   fmmm.call(GA);
   ogdf::GraphIO::writeGML(GA, "sierpinski_04-layout.gml");

   for(v=G.firstNode(); v; v=v->succ()) {
      std::cout << v << std::endl;
      //the following line causes the segfault
      double xCoord = GA.x(v);
   }

如果我注释掉我在评论中提到的导致段错误的行,则程序运行良好而没有段错误。如果我然后查看写出的 .gml 文件,则节点具有 x 和 y 坐标。我收到以下消息:

MT: /home/work/lib/OGDF-snapshot/include/ogdf/basic/NodeArray.h:174: T& ogdf::NodeArray<T>::operator[](ogdf::node) [with T = double; ogdf::node = ogdf::NodeElement*]: Assertion `v->graphOf() == m_pGraph' failed.

当我在 GraphAttributes 上调用不同的函数时也会发生这种情况,例如 .idNode(v)。

有人可以指出我正确的方向,为什么会发生这种情况?我现在完全不明白这是从哪里来的,而且 OGDF 太大了,无法浏览代码并理解它。(至少对我来说)

非常感谢您!

4

2 回答 2

1

不幸的是,您的问题不容易重现。

我的直觉是在从文件加载 Graph 后初始化 GraphAttributes。

ogdf::Graph G;
if (!ogdf::GraphIO::readGML(G, "sierpinski_04.gml") ) {
    std::cerr << "Could not load sierpinski_04.gml" << std::endl;
    return 1;
}
ogdf::GraphAttributes GA(G, ogdf::GraphAttributes::nodeGraphics |
                         ogdf::GraphAttributes::nodeStyle |
                         ogdf::GraphAttributes::edgeGraphics );

或者在加载图形后调用 initAttributes。

ogdf::Graph G;
ogdf::GraphAttributes GA(G);

if (!ogdf::GraphIO::readGML(G, "sierpinski_04.gml") ) {
   std::cerr << "Could not load sierpinski_04.gml" << std::endl;
   return 1;
}

GA.initAttributes(ogdf::GraphAttributes::nodeGraphics |
                  ogdf::GraphAttributes::nodeStyle |
                  ogdf::GraphAttributes::edgeGraphics);

希望这会有所帮助。

于 2017-05-18T09:43:49.073 回答
0

对我来说,没有 -DOGDF_DEBUG的构建工作。

断言(意外失败)仅在调试模式下检查。

Graph_d.h:168

#ifdef OGDF_DEBUG
    // we store the graph containing this node for debugging purposes
    const Graph *m_pGraph; //!< The graph containg this node (**debug only**).
#endif
于 2019-06-14T15:42:55.350 回答