src/work/alpar/graph_to_eps.cc
author jacint
Fri, 07 Jan 2005 08:50:38 +0000
changeset 1059 bd97feae7d90
parent 1051 4ebe32765b48
child 1062 8226427845bc
permissions -rw-r--r--
(none)
alpar@1051
     1
#include <iostream>
alpar@1055
     2
#include <fstream>
alpar@1055
     3
#include <algorithm>
alpar@1050
     4
#include<math.h>
alpar@1051
     5
alpar@1046
     6
#include<lemon/xy.h>
alpar@1046
     7
#include<lemon/maps.h>
alpar@1046
     8
#include<lemon/list_graph.h>
alpar@1046
     9
alpar@1050
    10
alpar@1050
    11
///\file \ingroup misc
alpar@1046
    12
///Simple graph drawer
alpar@1046
    13
alpar@1046
    14
namespace lemon {
alpar@1046
    15
alpar@1050
    16
///Data structure representing RGB colors.
alpar@1050
    17
alpar@1050
    18
///Data structure representing RGB colors.
alpar@1050
    19
///\ingroup misc
alpar@1046
    20
class Color
alpar@1046
    21
{
alpar@1046
    22
  double _r,_g,_b;
alpar@1046
    23
public:
alpar@1050
    24
  ///Default constructor
alpar@1046
    25
  Color() {}
alpar@1050
    26
  ///Constructor
alpar@1046
    27
  Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
alpar@1050
    28
  ///Returns the red component
alpar@1046
    29
  double getR() {return _r;}
alpar@1050
    30
  ///Returns the green component
alpar@1046
    31
  double getG() {return _g;}
alpar@1050
    32
  ///Returns the blue component
alpar@1046
    33
  double getB() {return _b;}
alpar@1050
    34
  ///Set the color components
alpar@1046
    35
  void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
alpar@1046
    36
};
alpar@1046
    37
  
alpar@1050
    38
///Default traits class of \ref GraphToEps
alpar@1050
    39
alpar@1050
    40
///Default traits class of \ref GraphToEps
alpar@1050
    41
///
alpar@1050
    42
///\c G is the type of the underlying graph.
alpar@1046
    43
template<class G>
alpar@1046
    44
struct DefaultGraphToEpsTraits
alpar@1046
    45
{
alpar@1046
    46
  typedef G Graph;
alpar@1046
    47
  typedef typename Graph::Node Node;
alpar@1046
    48
  typedef typename Graph::NodeIt NodeIt;
alpar@1046
    49
  typedef typename Graph::Edge Edge;
alpar@1046
    50
  typedef typename Graph::EdgeIt EdgeIt;
alpar@1046
    51
  typedef typename Graph::InEdgeIt InEdgeIt;
alpar@1046
    52
  typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@1046
    53
  
alpar@1046
    54
alpar@1046
    55
  const Graph &g;
alpar@1051
    56
alpar@1051
    57
  std::ostream& os;
alpar@1051
    58
  
alpar@1050
    59
  ConstMap<typename Graph::Node,xy<double> > _coords;
alpar@1050
    60
  ConstMap<typename Graph::Node,double > _nodeSizes;
alpar@1046
    61
alpar@1050
    62
  ConstMap<typename Graph::Node,Color > _nodeColors;
alpar@1050
    63
  ConstMap<typename Graph::Edge,Color > _edgeColors;
alpar@1050
    64
alpar@1050
    65
  ConstMap<typename Graph::Edge,double > _edgeWidths;
alpar@1050
    66
  
alpar@1050
    67
  double _edgeWidthScale;
alpar@1050
    68
  
alpar@1050
    69
  double _nodeScale;
alpar@1050
    70
  double _xBorder, _yBorder;
alpar@1050
    71
  double _scale;
alpar@1050
    72
  double _nodeBorderQuotient;
alpar@1050
    73
  
alpar@1050
    74
  bool _drawArrows;
alpar@1050
    75
  double _arrowLength, _arrowWidth;
alpar@1050
    76
  
alpar@1055
    77
  bool _enableParallel;
alpar@1055
    78
alpar@1055
    79
  bool _pleaseRemoveOsStream;
alpar@1050
    80
  ///Constructor
alpar@1050
    81
alpar@1050
    82
  ///Constructor
alpar@1051
    83
  ///\param _g is a reference to the graph to be printed
alpar@1051
    84
  ///\param _os is a reference to the output stream.
alpar@1055
    85
  ///\param _os is a reference to the output stream.
alpar@1055
    86
  ///\param _pros If it is \c true, then the \c ostream referenced by \c _os
alpar@1055
    87
  ///will be explicitly deallocated by the destructor.
alpar@1051
    88
  ///By default it is <tt>std::cout</tt>
alpar@1055
    89
  DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
alpar@1055
    90
			  bool _pros=false) :
alpar@1051
    91
    g(_g), os(_os),
alpar@1051
    92
    _coords(xy<double>(1,1)), _nodeSizes(1.0),
alpar@1050
    93
    _nodeColors(Color(1,1,1)), _edgeColors(Color(0,0,0)),
alpar@1050
    94
    _edgeWidths(1), _edgeWidthScale(0.3),
alpar@1050
    95
    _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0),
alpar@1050
    96
    _nodeBorderQuotient(.1),
alpar@1055
    97
    _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
alpar@1055
    98
    _enableParallel(false), _pleaseRemoveOsStream(_pros) {}
alpar@1046
    99
};
alpar@1046
   100
alpar@1050
   101
///Helper class to implement the named parameters of \ref graphToEps()
alpar@1050
   102
alpar@1050
   103
///Helper class to implement the named parameters of \ref graphToEps()
alpar@1050
   104
///\todo Is 'helper class' a good name for this?
alpar@1050
   105
///
alpar@1046
   106
template<class T> class GraphToEps : public T 
alpar@1046
   107
{
alpar@1046
   108
  typedef typename T::Graph Graph;
alpar@1046
   109
  typedef typename Graph::Node Node;
alpar@1046
   110
  typedef typename Graph::NodeIt NodeIt;
alpar@1046
   111
  typedef typename Graph::Edge Edge;
alpar@1046
   112
  typedef typename Graph::EdgeIt EdgeIt;
alpar@1046
   113
  typedef typename Graph::InEdgeIt InEdgeIt;
alpar@1046
   114
  typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@1046
   115
alpar@1046
   116
  bool dontPrint;
alpar@1046
   117
alpar@1055
   118
  class edgeLess {
alpar@1055
   119
    const Graph &g;
alpar@1055
   120
  public:
alpar@1055
   121
    edgeLess(const Graph &_g) : g(_g) {}
alpar@1055
   122
    bool operator()(Edge a,Edge b) const 
alpar@1055
   123
    {
alpar@1055
   124
      Node ai=min(g.source(a),g.target(a));
alpar@1055
   125
      Node aa=max(g.source(a),g.target(a));
alpar@1055
   126
      Node bi=min(g.source(b),g.target(b));
alpar@1055
   127
      Node ba=max(g.source(b),g.target(b));
alpar@1055
   128
      return ai<bi ||
alpar@1055
   129
	(ai==bi && (aa < ba || 
alpar@1055
   130
		    (aa==ba && ai==g.source(a) && bi==g.target(b))));
alpar@1055
   131
    }
alpar@1055
   132
  };
alpar@1055
   133
    
alpar@1046
   134
public:
alpar@1046
   135
  GraphToEps(const T &t) : T(t), dontPrint(false) {};
alpar@1046
   136
  
alpar@1050
   137
  template<class X> struct CoordsTraits : public T {
alpar@1050
   138
    const X &_coords;
alpar@1050
   139
    CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
alpar@1046
   140
  };
alpar@1050
   141
  ///Sets the map of the node coordinates
alpar@1050
   142
alpar@1050
   143
  ///Sets the map of the node coordinates.
alpar@1050
   144
  ///\param x must be a node map with xy<double> or xy<int> values. 
alpar@1050
   145
  template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
alpar@1046
   146
    dontPrint=true;
alpar@1050
   147
    return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
alpar@1046
   148
  }
alpar@1050
   149
  template<class X> struct NodeSizesTraits : public T {
alpar@1050
   150
    const X &_nodeSizes;
alpar@1050
   151
    NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
alpar@1046
   152
  };
alpar@1050
   153
  ///Sets the map of the node sizes
alpar@1050
   154
alpar@1050
   155
  ///Sets the map of the node sizes
alpar@1050
   156
  ///\param x must be a node map with \c double (or convertible) values. 
alpar@1050
   157
  template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
alpar@1046
   158
  {
alpar@1046
   159
    dontPrint=true;
alpar@1050
   160
    return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
alpar@1046
   161
  }
alpar@1050
   162
   template<class X> struct EdgeWidthsTraits : public T {
alpar@1050
   163
    const X &_edgeWidths;
alpar@1050
   164
    EdgeWidthsTraits(const T &t,const X &x) : T(t), _edgeWidths(x) {}
alpar@1046
   165
  };
alpar@1050
   166
  ///Sets the map of the edge widths
alpar@1050
   167
alpar@1050
   168
  ///Sets the map of the edge widths
alpar@1050
   169
  ///\param x must be a edge map with \c double (or convertible) values. 
alpar@1050
   170
  template<class X> GraphToEps<EdgeWidthsTraits<X> > edgeWidths(const X &x)
alpar@1046
   171
  {
alpar@1046
   172
    dontPrint=true;
alpar@1050
   173
    return GraphToEps<EdgeWidthsTraits<X> >(EdgeWidthsTraits<X>(*this,x));
alpar@1046
   174
  }
alpar@1050
   175
alpar@1050
   176
  template<class X> struct NodeColorsTraits : public T {
alpar@1050
   177
    const X &_nodeColors;
alpar@1050
   178
    NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
alpar@1046
   179
  };
alpar@1050
   180
  ///Sets the map of the node colors
alpar@1050
   181
alpar@1050
   182
  ///Sets the map of the node colors
alpar@1050
   183
  ///\param x must be a node map with \ref Color values. 
alpar@1050
   184
  template<class X> GraphToEps<NodeColorsTraits<X> >
alpar@1050
   185
  nodeColors(const X &x)
alpar@1046
   186
  {
alpar@1046
   187
    dontPrint=true;
alpar@1050
   188
    return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
alpar@1046
   189
  }
alpar@1050
   190
  template<class X> struct EdgeColorsTraits : public T {
alpar@1050
   191
    const X &_edgeColors;
alpar@1050
   192
    EdgeColorsTraits(const T &t,const X &x) : T(t), _edgeColors(x) {}
alpar@1050
   193
  };
alpar@1050
   194
  ///Sets the map of the edge colors
alpar@1050
   195
alpar@1050
   196
  ///Sets the map of the edge colors
alpar@1050
   197
  ///\param x must be a edge map with \ref Color values. 
alpar@1050
   198
  template<class X> GraphToEps<EdgeColorsTraits<X> >
alpar@1050
   199
  edgeColors(const X &x)
alpar@1050
   200
  {
alpar@1050
   201
    dontPrint=true;
alpar@1050
   202
    return GraphToEps<EdgeColorsTraits<X> >(EdgeColorsTraits<X>(*this,x));
alpar@1050
   203
  }
alpar@1050
   204
  ///Sets a global scale factor for node sizes
alpar@1050
   205
alpar@1050
   206
  ///Sets a global scale factor for node sizes
alpar@1050
   207
  ///
alpar@1050
   208
  GraphToEps<T> &nodeScale(double d) {_nodeScale=d;return *this;}
alpar@1050
   209
  ///Sets a global scale factor for edge widths
alpar@1050
   210
alpar@1050
   211
  ///Sets a global scale factor for edge widths
alpar@1050
   212
  ///
alpar@1050
   213
  GraphToEps<T> &edgeWidthScale(double d) {_edgeWidthScale=d;return *this;}
alpar@1050
   214
  ///Sets a global scale factor for the whole picture
alpar@1050
   215
alpar@1050
   216
  ///Sets a global scale factor for the whole picture
alpar@1050
   217
  ///
alpar@1050
   218
  GraphToEps<T> &scale(double d) {_scale=d;return *this;}
alpar@1050
   219
  ///Sets the width of the border around the picture
alpar@1050
   220
alpar@1050
   221
  ///Sets the width of the border around the picture
alpar@1050
   222
  ///
alpar@1050
   223
  GraphToEps<T> &border(double b) {_xBorder=_yBorder=b;return *this;}
alpar@1050
   224
  ///Sets the width of the border around the picture
alpar@1050
   225
alpar@1050
   226
  ///Sets the width of the border around the picture
alpar@1050
   227
  ///
alpar@1050
   228
  GraphToEps<T> &border(double x, double y) {
alpar@1050
   229
    _xBorder=x;_yBorder=y;return *this;
alpar@1050
   230
  }
alpar@1050
   231
  ///Sets whether to draw arrows
alpar@1050
   232
alpar@1050
   233
  ///Sets whether to draw arrows
alpar@1050
   234
  ///
alpar@1050
   235
  GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
alpar@1050
   236
  ///Sets the length of the arrowheads
alpar@1050
   237
alpar@1050
   238
  ///Sets the length of the arrowheads
alpar@1050
   239
  ///
alpar@1050
   240
  GraphToEps<T> &arrowLength(double d) {_arrowLength*=d;return *this;}
alpar@1050
   241
  ///Sets the width of the arrowheads
alpar@1050
   242
alpar@1050
   243
  ///Sets the width of the arrowheads
alpar@1050
   244
  ///
alpar@1050
   245
  GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;}
alpar@1046
   246
  
alpar@1055
   247
  ///Enables parallel edges
alpar@1055
   248
alpar@1055
   249
  ///Enables parallel edges
alpar@1055
   250
  ///\todo Unimplemented
alpar@1055
   251
  GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
alpar@1055
   252
  
alpar@1046
   253
  ~GraphToEps() 
alpar@1046
   254
  {
alpar@1046
   255
    if(dontPrint) return;
alpar@1046
   256
    
alpar@1051
   257
    os << "%!PS-Adobe-2.0 EPSF-2.0\n";
alpar@1046
   258
    //\todo: Chech whether the graph is empty.
alpar@1046
   259
    BoundingBox<double> bb;
alpar@1050
   260
    for(NodeIt n(g);n!=INVALID;++n) {
alpar@1050
   261
      double ns=_nodeSizes[n]*_nodeScale;
alpar@1050
   262
      xy<double> p(ns,ns);
alpar@1050
   263
      bb+=p+_coords[n];
alpar@1050
   264
      bb+=-p+_coords[n];
alpar@1046
   265
      }
alpar@1051
   266
    os << "%%BoundingBox: "
alpar@1050
   267
	 << bb.left()*  _scale-_xBorder << ' '
alpar@1050
   268
	 << bb.bottom()*_scale-_yBorder << ' '
alpar@1050
   269
	 << bb.right()* _scale+_xBorder << ' '
alpar@1050
   270
	 << bb.top()*   _scale+_yBorder << '\n';
alpar@1050
   271
    //x1 y1 x2 y2 cr cg cb w
alpar@1051
   272
    os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
alpar@1051
   273
    os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc } bind def\n";
alpar@1046
   274
    // x y r cr cg cb
alpar@1051
   275
    os << "/n { setrgbcolor 2 index 2 index 2 index c fill\n"
alpar@1050
   276
	 << "     0 0 0 setrgbcolor dup "
alpar@1050
   277
	 << _nodeBorderQuotient << " mul setlinewidth "
alpar@1050
   278
	 << 1+_nodeBorderQuotient/2 << " div c stroke\n"
alpar@1046
   279
	 << "   } bind def\n";
alpar@1051
   280
    os << "/arrl " << _arrowLength << " def\n";
alpar@1051
   281
    os << "/arrw " << _arrowWidth << " def\n";
alpar@1050
   282
    // l dx_norm dy_norm
alpar@1051
   283
    os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
alpar@1050
   284
    //len w dx_norm dy_norm x1 y1 cr cg cb
alpar@1051
   285
    os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
alpar@1050
   286
	 << "       /w exch def /len exch def\n"
alpar@1050
   287
      //	 << "       0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
alpar@1050
   288
	 << "       newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
alpar@1050
   289
	 << "       len w sub arrl sub dx dy lrl\n"
alpar@1050
   290
	 << "       arrw dy dx neg lrl\n"
alpar@1050
   291
	 << "       dx arrl w add mul dy w 2 div arrw add mul sub\n"
alpar@1050
   292
	 << "       dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
alpar@1050
   293
	 << "       dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
alpar@1050
   294
	 << "       dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
alpar@1050
   295
	 << "       arrw dy dx neg lrl\n"
alpar@1050
   296
	 << "       len w sub arrl sub neg dx dy lrl\n"
alpar@1050
   297
	 << "       closepath fill } bind def\n";
alpar@1051
   298
    os << "\ngsave\n";
alpar@1051
   299
    if(_scale!=1.0) os << _scale << " dup scale\n";
alpar@1055
   300
    
alpar@1051
   301
    os << "%Edges:\ngsave\n";
alpar@1055
   302
    
alpar@1055
   303
    vector<Edge> el;
alpar@1055
   304
    if(_enableParallel) {
alpar@1055
   305
      for(EdgeIt e(g);e!=INVALID;++e) el.push_back(e);
alpar@1055
   306
      sort(el.begin(),el.end(),edgeLess(g));
alpar@1055
   307
    }
alpar@1055
   308
    
alpar@1046
   309
    for(NodeIt n(g);n!=INVALID;++n)
alpar@1046
   310
      for(OutEdgeIt e(g,n);e!=INVALID;++e)
alpar@1050
   311
	if(_drawArrows) {
alpar@1050
   312
	  xy<double> d(_coords[g.target(e)]-_coords[g.source(e)]);
alpar@1050
   313
	  double l=sqrt(d.normSquare());
alpar@1050
   314
	  d/=l;
alpar@1050
   315
	  xy<double> x1(d*_nodeScale*_nodeSizes[g.source(e)]+
alpar@1050
   316
			_coords[g.source(e)]);
alpar@1051
   317
	  os << l-(_nodeSizes[g.source(e)]+
alpar@1050
   318
		     _nodeSizes[g.target(e)])*_nodeScale << ' '
alpar@1050
   319
	       << _edgeWidths[e]*_edgeWidthScale << ' '
alpar@1050
   320
	       << d.x << ' ' << d.y << ' '
alpar@1050
   321
	       << x1.x << ' ' << x1.y << ' '
alpar@1050
   322
	       << _edgeColors[e].getR() << ' '
alpar@1050
   323
	       << _edgeColors[e].getG() << ' '
alpar@1050
   324
	       << _edgeColors[e].getB() << " arr\n";
alpar@1050
   325
	}
alpar@1051
   326
    	else os << _coords[g.source(e)].x << ' '
alpar@1050
   327
		  << _coords[g.source(e)].y << ' '
alpar@1050
   328
		  << _coords[g.target(e)].x << ' '
alpar@1050
   329
		  << _coords[g.target(e)].y << ' '
alpar@1050
   330
		  << _edgeColors[e].getR() << ' '
alpar@1050
   331
		  << _edgeColors[e].getG() << ' '
alpar@1050
   332
		  << _edgeColors[e].getB() << ' '
alpar@1050
   333
		  << _edgeWidths[e]*_edgeWidthScale << " l\n";
alpar@1051
   334
    os << "grestore\n%Nodes:\ngsave\n";
alpar@1046
   335
    for(NodeIt n(g);n!=INVALID;++n)
alpar@1051
   336
      os << _coords[n].x << ' ' << _coords[n].y << ' '
alpar@1050
   337
	   << _nodeSizes[n]*_nodeScale << ' '
alpar@1050
   338
	   << _nodeColors[n].getR() << ' '
alpar@1050
   339
	   << _nodeColors[n].getG() << ' '
alpar@1050
   340
	   << _nodeColors[n].getB() << " n\n"; 
alpar@1051
   341
    os << "grestore\ngrestore\n";
alpar@1055
   342
alpar@1055
   343
alpar@1055
   344
    //CleanUp:
alpar@1055
   345
    if(_pleaseRemoveOsStream) {delete &os;}
alpar@1046
   346
  } 
alpar@1046
   347
};
alpar@1046
   348
alpar@1046
   349
alpar@1050
   350
///Generates an EPS file from a graph
alpar@1050
   351
alpar@1050
   352
///\ingroup misc
alpar@1050
   353
///Generates an EPS file from a graph.
alpar@1051
   354
///\param g is a reference to the graph to be printed
alpar@1051
   355
///\param os is a reference to the output stream.
alpar@1051
   356
///By default it is <tt>std::cout</tt>
alpar@1050
   357
///
alpar@1051
   358
///This function also has a lot of \ref named-templ-param "named parameters",
alpar@1050
   359
///they are declared as the members of class \ref GraphToEps. The following
alpar@1050
   360
///example shows how to use these parameters.
alpar@1050
   361
///\code
alpar@1050
   362
/// graphToEps(g).scale(10).coords(coords)
alpar@1050
   363
///              .nodeScale(2).nodeSizes(sizes)
alpar@1050
   364
///              .edgeWidthScale(.4);
alpar@1050
   365
///\endcode
alpar@1050
   366
///\sa GraphToEps
alpar@1046
   367
template<class G>
alpar@1055
   368
GraphToEps<DefaultGraphToEpsTraits<G> > 
alpar@1055
   369
graphToEps(G &g,std::ostream& os=std::cout)
alpar@1046
   370
{
alpar@1055
   371
  return 
alpar@1055
   372
    GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
alpar@1046
   373
}
alpar@1046
   374
 
alpar@1055
   375
///Generates an EPS file from a graph
alpar@1055
   376
alpar@1055
   377
///\ingroup misc
alpar@1055
   378
///Generates an EPS file from a graph.
alpar@1055
   379
///\param g is a reference to the graph to be printed
alpar@1055
   380
///\param file_name is the output file_name.
alpar@1055
   381
///
alpar@1055
   382
///This function also has a lot of \ref named-templ-param "named parameters",
alpar@1055
   383
///they are declared as the members of class \ref GraphToEps. The following
alpar@1055
   384
///example shows how to use these parameters.
alpar@1055
   385
///\code
alpar@1055
   386
/// graphToEps(g).scale(10).coords(coords)
alpar@1055
   387
///              .nodeScale(2).nodeSizes(sizes)
alpar@1055
   388
///              .edgeWidthScale(.4);
alpar@1055
   389
///\endcode
alpar@1055
   390
///\sa GraphToEps
alpar@1055
   391
///\todo Avoid duplicated documentation
alpar@1055
   392
///\bug Exception handling is missing? (Or we can just ignore it?)
alpar@1055
   393
template<class G>
alpar@1055
   394
GraphToEps<DefaultGraphToEpsTraits<G> > 
alpar@1055
   395
graphToEps(G &g,char *file_name)
alpar@1055
   396
{
alpar@1055
   397
  return GraphToEps<DefaultGraphToEpsTraits<G> >
alpar@1055
   398
    (DefaultGraphToEpsTraits<G>(g,*new ofstream(file_name),true));
alpar@1055
   399
}
alpar@1055
   400
alpar@1055
   401
alpar@1046
   402
}
alpar@1046
   403
alpar@1046
   404
using namespace lemon;
alpar@1046
   405
alpar@1046
   406
class ColorSet : public MapBase<int,Color>
alpar@1046
   407
{
alpar@1046
   408
public:
alpar@1046
   409
  Color operator[](int i) const
alpar@1046
   410
  {
alpar@1046
   411
    switch(i%8){
alpar@1046
   412
    case 0: return Color(0,0,0);
alpar@1046
   413
    case 1: return Color(1,0,0);
alpar@1046
   414
    case 2: return Color(0,1,0);
alpar@1046
   415
    case 3: return Color(0,0,1);
alpar@1046
   416
    case 4: return Color(1,1,0);
alpar@1046
   417
    case 5: return Color(1,0,1);
alpar@1046
   418
    case 6: return Color(0,1,1);
alpar@1046
   419
    case 7: return Color(1,1,1);
alpar@1046
   420
    }
alpar@1046
   421
    return Color(0,0,0);
alpar@1046
   422
  }
alpar@1046
   423
} colorSet;
alpar@1046
   424
alpar@1046
   425
int main()
alpar@1046
   426
{
alpar@1046
   427
  ListGraph g;
alpar@1046
   428
  typedef ListGraph::Node Node;
alpar@1046
   429
  typedef ListGraph::NodeIt NodeIt;
alpar@1046
   430
  typedef ListGraph::Edge Edge;
alpar@1050
   431
  typedef xy<int> Xy;
alpar@1046
   432
  
alpar@1046
   433
  Node n1=g.addNode();
alpar@1046
   434
  Node n2=g.addNode();
alpar@1046
   435
  Node n3=g.addNode();
alpar@1046
   436
  Node n4=g.addNode();
alpar@1046
   437
  Node n5=g.addNode();
alpar@1046
   438
alpar@1046
   439
  ListGraph::NodeMap<Xy> coords(g);
alpar@1046
   440
  ListGraph::NodeMap<double> sizes(g);
alpar@1046
   441
  ListGraph::NodeMap<int> colors(g);
alpar@1047
   442
  ListGraph::EdgeMap<int> ecolors(g);
alpar@1050
   443
  ListGraph::EdgeMap<int> widths(g);
alpar@1046
   444
  
alpar@1046
   445
  coords[n1]=Xy(50,50);  sizes[n1]=1; colors[n1]=1;
alpar@1046
   446
  coords[n2]=Xy(50,70);  sizes[n2]=2; colors[n2]=2;
alpar@1046
   447
  coords[n3]=Xy(70,70);  sizes[n3]=1; colors[n3]=3;
alpar@1046
   448
  coords[n4]=Xy(70,50);  sizes[n4]=2; colors[n4]=4;
alpar@1046
   449
  coords[n5]=Xy(85,60);  sizes[n5]=3; colors[n5]=5;
alpar@1046
   450
  
alpar@1046
   451
  Edge e;
alpar@1046
   452
alpar@1050
   453
  e=g.addEdge(n1,n2); ecolors[e]=0; widths[e]=1;
alpar@1050
   454
  e=g.addEdge(n2,n3); ecolors[e]=0; widths[e]=1;
alpar@1050
   455
  e=g.addEdge(n3,n5); ecolors[e]=0; widths[e]=3;
alpar@1050
   456
  e=g.addEdge(n5,n4); ecolors[e]=0; widths[e]=1;
alpar@1050
   457
  e=g.addEdge(n4,n1); ecolors[e]=0; widths[e]=1;
alpar@1050
   458
  e=g.addEdge(n2,n4); ecolors[e]=1; widths[e]=2;
alpar@1050
   459
  e=g.addEdge(n3,n4); ecolors[e]=2; widths[e]=1;
alpar@1046
   460
  
alpar@1055
   461
  graphToEps(g,"proba.eps").scale(10).coords(coords).
alpar@1055
   462
    nodeScale(2).nodeSizes(sizes).
alpar@1055
   463
    nodeColors(composeMap(colorSet,colors)).
alpar@1055
   464
    edgeColors(composeMap(colorSet,ecolors)).
alpar@1055
   465
    edgeWidthScale(.4).edgeWidths(widths);
alpar@1055
   466
  graphToEps(g,"proba_arr.eps").scale(10).coords(coords).
alpar@1050
   467
    nodeScale(2).nodeSizes(sizes).
alpar@1050
   468
    nodeColors(composeMap(colorSet,colors)).
alpar@1050
   469
    edgeColors(composeMap(colorSet,ecolors)).
alpar@1050
   470
    edgeWidthScale(.4).edgeWidths(widths).
alpar@1050
   471
    drawArrows().arrowWidth(1).arrowLength(1)
alpar@1050
   472
    ;
alpar@1046
   473
}