Orthogonal layout

Back to List of Examples

Topic

Load a graph from a GML file, apply customized Orthogonal Layout, and write the graph with layout information back to a file.

Source-Code

Requires: ogdf v.2007.11

#include <ogdf/planarity/PlanarizationLayout.h>
#include <ogdf/planarity/VariableEmbeddingInserter.h>
#include <ogdf/planarity/FastPlanarSubgraph.h>
#include <ogdf/orthogonal/OrthoLayout.h>
#include <ogdf/planarity/EmbedderMinDepthMaxFaceLayers.h>
 
 
using namespace ogdf;
 
int main()
{
    Graph G;
    GraphAttributes GA(G,
	GraphAttributes::nodeGraphics | GraphAttributes::edgeGraphics |
	GraphAttributes::nodeLabel | GraphAttributes::nodeColor | 
	GraphAttributes::edgeColor | GraphAttributes::edgeStyle | 
	GraphAttributes::nodeStyle | GraphAttributes::nodeTemplate);
    GA.readGML(G, "er-diagram.gml");
 
    PlanarizationLayout pl;
 
    FastPlanarSubgraph *ps = new FastPlanarSubgraph;
    ps->runs(100);
    VariableEmbeddingInserter *ves = new VariableEmbeddingInserter;
    ves->removeReinsert(EdgeInsertionModule::rrAll);
    pl.setSubgraph(ps);
    pl.setInserter(ves);
 
    EmbedderMinDepthMaxFaceLayers *emb = new EmbedderMinDepthMaxFaceLayers;
    pl.setEmbedder(emb);
 
    OrthoLayout *ol = new OrthoLayout;
    ol->separation(20.0);
    ol->cOverhang(0.4);
    ol->setOptions(2+4);
    pl.setPlanarLayouter(ol);
 
    pl.call(GA);
 
    GA.writeGML("er-diagram-layout.gml");
 
    return 0;
}

Example graph: er-diagram.gml

Explanation

In this example, we want to produce an orthogonal layout of an ER-diagram. We will use the class PlanarizationLayout which implements the planarization approach for drawing graphs. This approach divides the layout process into two steps: The crossing minimization step seeks for a planarized representation of the graph with as few crossings as possible (a planarized representation is a planar graph, where a dummy vertex is “put” on each edge crossing) and the planar layout step applies a planar drawing algorithm (usually an orthogonal layout algorithm) to the planarized representation. The final layout is then simply obtained by replacing the dummy vertices again with edge crossings.

In our example, we proceed as follows. First, we declare a Graph G with GraphAttributes GA storing all the layout information we need (these include in our case also colors and line styles, since these shall be preserved in the output GML file). Then, we declare an instance pl of PlanarizationLayout and set various module options:

Finally, we call the planarization layout algorithm in the usual way by passing the graph attributes object, which will then be assigned the produced layout. This layout is saved in the file er-diagram-layout.gml.

Output

Graph layout:
er-diagram-layout.gml

Drawing:
er-diagram-layout.svg