COIN-OR::LEMON - Graph Library

source: lemon/demo/graph_to_eps_demo.cc @ 669:28f58740b6f8

Last change on this file since 669:28f58740b6f8 was 659:0c8e5c688440, checked in by Alpar Juttner <alpar@…>, 16 years ago

Fix usage of sqrt() (#268)

File size: 6.9 KB
Line 
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-2009
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 graph maps.
30///
31/// \include graph_to_eps_demo.cc
32
33#include<lemon/list_graph.h>
34#include<lemon/graph_to_eps.h>
35#include<lemon/math.h>
36
37using namespace std;
38using namespace lemon;
39
40int main()
41{
42  Palette palette;
43  Palette paletteW(true);
44
45  // Create a small digraph
46  ListDigraph g;
47  typedef ListDigraph::Node Node;
48  typedef ListDigraph::NodeIt NodeIt;
49  typedef ListDigraph::Arc Arc;
50  typedef dim2::Point<int> Point;
51
52  Node n1=g.addNode();
53  Node n2=g.addNode();
54  Node n3=g.addNode();
55  Node n4=g.addNode();
56  Node n5=g.addNode();
57
58  ListDigraph::NodeMap<Point> coords(g);
59  ListDigraph::NodeMap<double> sizes(g);
60  ListDigraph::NodeMap<int> colors(g);
61  ListDigraph::NodeMap<int> shapes(g);
62  ListDigraph::ArcMap<int> acolors(g);
63  ListDigraph::ArcMap<int> widths(g);
64
65  coords[n1]=Point(50,50);  sizes[n1]=1; colors[n1]=1; shapes[n1]=0;
66  coords[n2]=Point(50,70);  sizes[n2]=2; colors[n2]=2; shapes[n2]=2;
67  coords[n3]=Point(70,70);  sizes[n3]=1; colors[n3]=3; shapes[n3]=0;
68  coords[n4]=Point(70,50);  sizes[n4]=2; colors[n4]=4; shapes[n4]=1;
69  coords[n5]=Point(85,60);  sizes[n5]=3; colors[n5]=5; shapes[n5]=2;
70
71  Arc a;
72
73  a=g.addArc(n1,n2); acolors[a]=0; widths[a]=1;
74  a=g.addArc(n2,n3); acolors[a]=0; widths[a]=1;
75  a=g.addArc(n3,n5); acolors[a]=0; widths[a]=3;
76  a=g.addArc(n5,n4); acolors[a]=0; widths[a]=1;
77  a=g.addArc(n4,n1); acolors[a]=0; widths[a]=1;
78  a=g.addArc(n2,n4); acolors[a]=1; widths[a]=2;
79  a=g.addArc(n3,n4); acolors[a]=2; widths[a]=1;
80
81  IdMap<ListDigraph,Node> id(g);
82
83  // Create .eps files showing the digraph with different options
84  cout << "Create 'graph_to_eps_demo_out_1_pure.eps'" << endl;
85  graphToEps(g,"graph_to_eps_demo_out_1_pure.eps").
86    coords(coords).
87    title("Sample .eps figure").
88    copyright("(C) 2003-2009 LEMON Project").
89    run();
90
91  cout << "Create 'graph_to_eps_demo_out_2.eps'" << endl;
92  graphToEps(g,"graph_to_eps_demo_out_2.eps").
93    coords(coords).
94    title("Sample .eps figure").
95    copyright("(C) 2003-2009 LEMON Project").
96    absoluteNodeSizes().absoluteArcWidths().
97    nodeScale(2).nodeSizes(sizes).
98    nodeShapes(shapes).
99    nodeColors(composeMap(palette,colors)).
100    arcColors(composeMap(palette,acolors)).
101    arcWidthScale(.4).arcWidths(widths).
102    nodeTexts(id).nodeTextSize(3).
103    run();
104
105  cout << "Create 'graph_to_eps_demo_out_3_arr.eps'" << endl;
106  graphToEps(g,"graph_to_eps_demo_out_3_arr.eps").
107    title("Sample .eps figure (with arrowheads)").
108    copyright("(C) 2003-2009 LEMON Project").
109    absoluteNodeSizes().absoluteArcWidths().
110    nodeColors(composeMap(palette,colors)).
111    coords(coords).
112    nodeScale(2).nodeSizes(sizes).
113    nodeShapes(shapes).
114    arcColors(composeMap(palette,acolors)).
115    arcWidthScale(.4).arcWidths(widths).
116    nodeTexts(id).nodeTextSize(3).
117    drawArrows().arrowWidth(2).arrowLength(2).
118    run();
119
120  // Add more arcs to the digraph
121  a=g.addArc(n1,n4); acolors[a]=2; widths[a]=1;
122  a=g.addArc(n4,n1); acolors[a]=1; widths[a]=2;
123
124  a=g.addArc(n1,n2); acolors[a]=1; widths[a]=1;
125  a=g.addArc(n1,n2); acolors[a]=2; widths[a]=1;
126  a=g.addArc(n1,n2); acolors[a]=3; widths[a]=1;
127  a=g.addArc(n1,n2); acolors[a]=4; widths[a]=1;
128  a=g.addArc(n1,n2); acolors[a]=5; widths[a]=1;
129  a=g.addArc(n1,n2); acolors[a]=6; widths[a]=1;
130  a=g.addArc(n1,n2); acolors[a]=7; widths[a]=1;
131
132  cout << "Create 'graph_to_eps_demo_out_4_par.eps'" << endl;
133  graphToEps(g,"graph_to_eps_demo_out_4_par.eps").
134    title("Sample .eps figure (parallel arcs)").
135    copyright("(C) 2003-2009 LEMON Project").
136    absoluteNodeSizes().absoluteArcWidths().
137    nodeShapes(shapes).
138    coords(coords).
139    nodeScale(2).nodeSizes(sizes).
140    nodeColors(composeMap(palette,colors)).
141    arcColors(composeMap(palette,acolors)).
142    arcWidthScale(.4).arcWidths(widths).
143    nodeTexts(id).nodeTextSize(3).
144    enableParallel().parArcDist(1.5).
145    run();
146
147  cout << "Create 'graph_to_eps_demo_out_5_par_arr.eps'" << endl;
148  graphToEps(g,"graph_to_eps_demo_out_5_par_arr.eps").
149    title("Sample .eps figure (parallel arcs and arrowheads)").
150    copyright("(C) 2003-2009 LEMON Project").
151    absoluteNodeSizes().absoluteArcWidths().
152    nodeScale(2).nodeSizes(sizes).
153    coords(coords).
154    nodeShapes(shapes).
155    nodeColors(composeMap(palette,colors)).
156    arcColors(composeMap(palette,acolors)).
157    arcWidthScale(.3).arcWidths(widths).
158    nodeTexts(id).nodeTextSize(3).
159    enableParallel().parArcDist(1).
160    drawArrows().arrowWidth(1).arrowLength(1).
161    run();
162
163  cout << "Create 'graph_to_eps_demo_out_6_par_arr_a4.eps'" << endl;
164  graphToEps(g,"graph_to_eps_demo_out_6_par_arr_a4.eps").
165    title("Sample .eps figure (fits to A4)").
166    copyright("(C) 2003-2009 LEMON Project").
167    scaleToA4().
168    absoluteNodeSizes().absoluteArcWidths().
169    nodeScale(2).nodeSizes(sizes).
170    coords(coords).
171    nodeShapes(shapes).
172    nodeColors(composeMap(palette,colors)).
173    arcColors(composeMap(palette,acolors)).
174    arcWidthScale(.3).arcWidths(widths).
175    nodeTexts(id).nodeTextSize(3).
176    enableParallel().parArcDist(1).
177    drawArrows().arrowWidth(1).arrowLength(1).
178    run();
179
180  // Create an .eps file showing the colors of a default Palette
181  ListDigraph h;
182  ListDigraph::NodeMap<int> hcolors(h);
183  ListDigraph::NodeMap<Point> hcoords(h);
184
185  int cols=int(std::sqrt(double(palette.size())));
186  for(int i=0;i<int(paletteW.size());i++) {
187    Node n=h.addNode();
188    hcoords[n]=Point(1+i%cols,1+i/cols);
189    hcolors[n]=i;
190  }
191
192  cout << "Create 'graph_to_eps_demo_out_7_colors.eps'" << endl;
193  graphToEps(h,"graph_to_eps_demo_out_7_colors.eps").
194    scale(60).
195    title("Sample .eps figure (Palette demo)").
196    copyright("(C) 2003-2009 LEMON Project").
197    coords(hcoords).
198    absoluteNodeSizes().absoluteArcWidths().
199    nodeScale(.45).
200    distantColorNodeTexts().
201    nodeTexts(hcolors).nodeTextSize(.6).
202    nodeColors(composeMap(paletteW,hcolors)).
203    run();
204
205  return 0;
206}
Note: See TracBrowser for help on using the repository browser.