demo/graph_to_eps_demo.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Mon, 14 Jul 2008 19:06:29 +0200
changeset 218 0d6511647639
parent 209 765619b7cbb2
child 220 a5d8c039f218
permissions -rw-r--r--
Bug fix in assert.h (ticket #125)
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 /// \ingroup demos
    20 /// \file
    21 /// \brief Demo of the graph drawing function \ref graphToEps()
    22 ///
    23 /// This demo program shows examples how to use the function \ref
    24 /// graphToEps(). It takes no input but simply creates seven
    25 /// <tt>.eps</tt> files demonstrating the capability of \ref
    26 /// graphToEps(), and showing how to draw directed graphs,
    27 /// how to handle parallel egdes, how to change the properties (like
    28 /// color, shape, size, title etc.) of nodes and arcs individually
    29 /// using appropriate \ref maps-page "graph maps".
    30 ///
    31 /// \include graph_to_eps_demo.cc
    32 
    33 #include<lemon/list_graph.h>
    34 #include<lemon/graph_utils.h>
    35 #include<lemon/graph_to_eps.h>
    36 #include<lemon/math.h>
    37 
    38 using namespace std;
    39 using namespace lemon;
    40 
    41 int main()
    42 {
    43   Palette palette;
    44   Palette paletteW(true);
    45 
    46   // Create a small digraph
    47   ListDigraph g;
    48   typedef ListDigraph::Node Node;
    49   typedef ListDigraph::NodeIt NodeIt;
    50   typedef ListDigraph::Arc Arc;
    51   typedef dim2::Point<int> Point;
    52 
    53   Node n1=g.addNode();
    54   Node n2=g.addNode();
    55   Node n3=g.addNode();
    56   Node n4=g.addNode();
    57   Node n5=g.addNode();
    58 
    59   ListDigraph::NodeMap<Point> coords(g);
    60   ListDigraph::NodeMap<double> sizes(g);
    61   ListDigraph::NodeMap<int> colors(g);
    62   ListDigraph::NodeMap<int> shapes(g);
    63   ListDigraph::ArcMap<int> acolors(g);
    64   ListDigraph::ArcMap<int> widths(g);
    65 
    66   coords[n1]=Point(50,50);  sizes[n1]=1; colors[n1]=1; shapes[n1]=0;
    67   coords[n2]=Point(50,70);  sizes[n2]=2; colors[n2]=2; shapes[n2]=2;
    68   coords[n3]=Point(70,70);  sizes[n3]=1; colors[n3]=3; shapes[n3]=0;
    69   coords[n4]=Point(70,50);  sizes[n4]=2; colors[n4]=4; shapes[n4]=1;
    70   coords[n5]=Point(85,60);  sizes[n5]=3; colors[n5]=5; shapes[n5]=2;
    71 
    72   Arc a;
    73 
    74   a=g.addArc(n1,n2); acolors[a]=0; widths[a]=1;
    75   a=g.addArc(n2,n3); acolors[a]=0; widths[a]=1;
    76   a=g.addArc(n3,n5); acolors[a]=0; widths[a]=3;
    77   a=g.addArc(n5,n4); acolors[a]=0; widths[a]=1;
    78   a=g.addArc(n4,n1); acolors[a]=0; widths[a]=1;
    79   a=g.addArc(n2,n4); acolors[a]=1; widths[a]=2;
    80   a=g.addArc(n3,n4); acolors[a]=2; widths[a]=1;
    81 
    82   IdMap<ListDigraph,Node> id(g);
    83 
    84   // Create .eps files showing the digraph with different options
    85   cout << "Create 'graph_to_eps_demo_out_1_pure.eps'" << endl;
    86   graphToEps(g,"graph_to_eps_demo_out_1_pure.eps").
    87     coords(coords).
    88     title("Sample .eps figure").
    89     copyright("(C) 2003-2008 LEMON Project").
    90     run();
    91 
    92   cout << "Create 'graph_to_eps_demo_out_2.eps'" << endl;
    93   graphToEps(g,"graph_to_eps_demo_out_2.eps").
    94     coords(coords).
    95     title("Sample .eps figure").
    96     copyright("(C) 2003-2008 LEMON Project").
    97     absoluteNodeSizes().absoluteArcWidths().
    98     nodeScale(2).nodeSizes(sizes).
    99     nodeShapes(shapes).
   100     nodeColors(composeMap(palette,colors)).
   101     arcColors(composeMap(palette,acolors)).
   102     arcWidthScale(.4).arcWidths(widths).
   103     nodeTexts(id).nodeTextSize(3).
   104     run();
   105 
   106   cout << "Create 'graph_to_eps_demo_out_3_arr.eps'" << endl;
   107   graphToEps(g,"graph_to_eps_demo_out_3_arr.eps").
   108     title("Sample .eps figure (with arrowheads)").
   109     copyright("(C) 2003-2008 LEMON Project").
   110     absoluteNodeSizes().absoluteArcWidths().
   111     nodeColors(composeMap(palette,colors)).
   112     coords(coords).
   113     nodeScale(2).nodeSizes(sizes).
   114     nodeShapes(shapes).
   115     arcColors(composeMap(palette,acolors)).
   116     arcWidthScale(.4).arcWidths(widths).
   117     nodeTexts(id).nodeTextSize(3).
   118     drawArrows().arrowWidth(2).arrowLength(2).
   119     run();
   120 
   121   // Add more arcs to the digraph
   122   a=g.addArc(n1,n4); acolors[a]=2; widths[a]=1;
   123   a=g.addArc(n4,n1); acolors[a]=1; widths[a]=2;
   124 
   125   a=g.addArc(n1,n2); acolors[a]=1; widths[a]=1;
   126   a=g.addArc(n1,n2); acolors[a]=2; widths[a]=1;
   127   a=g.addArc(n1,n2); acolors[a]=3; widths[a]=1;
   128   a=g.addArc(n1,n2); acolors[a]=4; widths[a]=1;
   129   a=g.addArc(n1,n2); acolors[a]=5; widths[a]=1;
   130   a=g.addArc(n1,n2); acolors[a]=6; widths[a]=1;
   131   a=g.addArc(n1,n2); acolors[a]=7; widths[a]=1;
   132 
   133   cout << "Create 'graph_to_eps_demo_out_4_par.eps'" << endl;
   134   graphToEps(g,"graph_to_eps_demo_out_4_par.eps").
   135     title("Sample .eps figure (parallel arcs)").
   136     copyright("(C) 2003-2008 LEMON Project").
   137     absoluteNodeSizes().absoluteArcWidths().
   138     nodeShapes(shapes).
   139     coords(coords).
   140     nodeScale(2).nodeSizes(sizes).
   141     nodeColors(composeMap(palette,colors)).
   142     arcColors(composeMap(palette,acolors)).
   143     arcWidthScale(.4).arcWidths(widths).
   144     nodeTexts(id).nodeTextSize(3).
   145     enableParallel().parArcDist(1.5).
   146     run();
   147 
   148   cout << "Create 'graph_to_eps_demo_out_5_par_arr.eps'" << endl;
   149   graphToEps(g,"graph_to_eps_demo_out_5_par_arr.eps").
   150     title("Sample .eps figure (parallel arcs and arrowheads)").
   151     copyright("(C) 2003-2008 LEMON Project").
   152     absoluteNodeSizes().absoluteArcWidths().
   153     nodeScale(2).nodeSizes(sizes).
   154     coords(coords).
   155     nodeShapes(shapes).
   156     nodeColors(composeMap(palette,colors)).
   157     arcColors(composeMap(palette,acolors)).
   158     arcWidthScale(.3).arcWidths(widths).
   159     nodeTexts(id).nodeTextSize(3).
   160     enableParallel().parArcDist(1).
   161     drawArrows().arrowWidth(1).arrowLength(1).
   162     run();
   163 
   164   cout << "Create 'graph_to_eps_demo_out_6_par_arr_a4.eps'" << endl;
   165   graphToEps(g,"graph_to_eps_demo_out_6_par_arr_a4.eps").
   166     title("Sample .eps figure (fits to A4)").
   167     copyright("(C) 2003-2008 LEMON Project").
   168     scaleToA4().
   169     absoluteNodeSizes().absoluteArcWidths().
   170     nodeScale(2).nodeSizes(sizes).
   171     coords(coords).
   172     nodeShapes(shapes).
   173     nodeColors(composeMap(palette,colors)).
   174     arcColors(composeMap(palette,acolors)).
   175     arcWidthScale(.3).arcWidths(widths).
   176     nodeTexts(id).nodeTextSize(3).
   177     enableParallel().parArcDist(1).
   178     drawArrows().arrowWidth(1).arrowLength(1).
   179     run();
   180 
   181   // Create an .eps file showing the colors of a default Palette
   182   ListDigraph h;
   183   ListDigraph::NodeMap<int> hcolors(h);
   184   ListDigraph::NodeMap<Point> hcoords(h);
   185 
   186   int cols=int(sqrt(double(palette.size())));
   187   for(int i=0;i<int(paletteW.size());i++) {
   188     Node n=h.addNode();
   189     hcoords[n]=Point(1+i%cols,1+i/cols);
   190     hcolors[n]=i;
   191   }
   192 
   193   cout << "Create 'graph_to_eps_demo_out_7_colors.eps'" << endl;
   194   graphToEps(h,"graph_to_eps_demo_out_7_colors.eps").
   195     scale(60).
   196     title("Sample .eps figure (Palette demo)").
   197     copyright("(C) 2003-2008 LEMON Project").
   198     coords(hcoords).
   199     absoluteNodeSizes().absoluteArcWidths().
   200     nodeScale(.45).
   201     distantColorNodeTexts().
   202     nodeTexts(hcolors).nodeTextSize(.6).
   203     nodeColors(composeMap(paletteW,hcolors)).
   204     run();
   205 
   206   return 0;
   207 }