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