src/lemon/graph_to_eps.h
author alpar
Fri, 28 Jan 2005 09:09:59 +0000
changeset 1103 f196dc4f1b31
parent 1091 c756973cd53c
child 1107 d972653c89d5
permissions -rw-r--r--
Add a 'scaleToA4()' function.
alpar@1073
     1
/* -*- C++ -*-
alpar@1073
     2
 * src/lemon/graph_to_eps.h - Part of LEMON, a generic C++ optimization library
alpar@1073
     3
 *
alpar@1073
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1073
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@1073
     6
 *
alpar@1073
     7
 * Permission to use, modify and distribute this software is granted
alpar@1073
     8
 * provided that this copyright notice appears in all copies. For
alpar@1073
     9
 * precise terms see the accompanying LICENSE file.
alpar@1073
    10
 *
alpar@1073
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@1073
    12
 * express or implied, and with no claim as to its suitability for any
alpar@1073
    13
 * purpose.
alpar@1073
    14
 *
alpar@1073
    15
 */
alpar@1073
    16
alpar@1073
    17
#ifndef LEMON_GRAPH_TO_EPS_H
alpar@1073
    18
#define LEMON_GRAPH_TO_EPS_H
alpar@1073
    19
alpar@1073
    20
#include<iostream>
alpar@1073
    21
#include<fstream>
alpar@1073
    22
#include<sstream>
alpar@1073
    23
#include<algorithm>
alpar@1073
    24
#include<vector>
alpar@1073
    25
alpar@1073
    26
#include<lemon/xy.h>
alpar@1073
    27
#include<lemon/maps.h>
alpar@1073
    28
#include<lemon/bezier.h>
alpar@1073
    29
alpar@1073
    30
///\ingroup misc
alpar@1073
    31
///\file
alpar@1073
    32
///\brief Simple graph drawer
alpar@1073
    33
///
alpar@1073
    34
///\author Alpar Juttner
alpar@1073
    35
alpar@1073
    36
namespace lemon {
alpar@1073
    37
alpar@1073
    38
///Data structure representing RGB colors.
alpar@1073
    39
alpar@1073
    40
///Data structure representing RGB colors.
alpar@1073
    41
///\ingroup misc
alpar@1073
    42
class Color
alpar@1073
    43
{
alpar@1073
    44
  double _r,_g,_b;
alpar@1073
    45
public:
alpar@1073
    46
  ///Default constructor
alpar@1073
    47
  Color() {}
alpar@1073
    48
  ///Constructor
alpar@1073
    49
  Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
alpar@1073
    50
  ///Returns the red component
alpar@1073
    51
  double getR() {return _r;}
alpar@1073
    52
  ///Returns the green component
alpar@1073
    53
  double getG() {return _g;}
alpar@1073
    54
  ///Returns the blue component
alpar@1073
    55
  double getB() {return _b;}
alpar@1073
    56
  ///Set the color components
alpar@1073
    57
  void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
alpar@1073
    58
};
alpar@1073
    59
  
alpar@1073
    60
///Default traits class of \ref GraphToEps
alpar@1073
    61
alpar@1073
    62
///Default traits class of \ref GraphToEps
alpar@1073
    63
///
alpar@1073
    64
///\c G is the type of the underlying graph.
alpar@1073
    65
template<class G>
alpar@1073
    66
struct DefaultGraphToEpsTraits
alpar@1073
    67
{
alpar@1073
    68
  typedef G Graph;
alpar@1073
    69
  typedef typename Graph::Node Node;
alpar@1073
    70
  typedef typename Graph::NodeIt NodeIt;
alpar@1073
    71
  typedef typename Graph::Edge Edge;
alpar@1073
    72
  typedef typename Graph::EdgeIt EdgeIt;
alpar@1073
    73
  typedef typename Graph::InEdgeIt InEdgeIt;
alpar@1073
    74
  typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@1073
    75
  
alpar@1073
    76
alpar@1073
    77
  const Graph &g;
alpar@1073
    78
alpar@1073
    79
  std::ostream& os;
alpar@1073
    80
  
alpar@1073
    81
  ConstMap<typename Graph::Node,xy<double> > _coords;
alpar@1073
    82
  ConstMap<typename Graph::Node,double > _nodeSizes;
alpar@1086
    83
  ConstMap<typename Graph::Node,int > _nodeShapes;
alpar@1073
    84
alpar@1073
    85
  ConstMap<typename Graph::Node,Color > _nodeColors;
alpar@1073
    86
  ConstMap<typename Graph::Edge,Color > _edgeColors;
alpar@1073
    87
alpar@1073
    88
  ConstMap<typename Graph::Edge,double > _edgeWidths;
alpar@1103
    89
alpar@1103
    90
  static const double A4HEIGHT = 841.8897637795276;
alpar@1103
    91
  static const double A4WIDTH  = 595.275590551181;
alpar@1103
    92
  static const double A4BORDER = 15;
alpar@1103
    93
alpar@1073
    94
  
alpar@1073
    95
  double _edgeWidthScale;
alpar@1073
    96
  
alpar@1073
    97
  double _nodeScale;
alpar@1073
    98
  double _xBorder, _yBorder;
alpar@1073
    99
  double _scale;
alpar@1073
   100
  double _nodeBorderQuotient;
alpar@1073
   101
  
alpar@1073
   102
  bool _drawArrows;
alpar@1073
   103
  double _arrowLength, _arrowWidth;
alpar@1073
   104
  
alpar@1073
   105
  bool _showNodes, _showEdges;
alpar@1073
   106
alpar@1073
   107
  bool _enableParallel;
alpar@1073
   108
  double _parEdgeDist;
alpar@1073
   109
alpar@1073
   110
  bool _showNodeText;
alpar@1073
   111
  ConstMap<typename Graph::Node,bool > _nodeTexts;  
alpar@1073
   112
  double _nodeTextSize;
alpar@1073
   113
alpar@1085
   114
  bool _showNodePsText;
alpar@1085
   115
  ConstMap<typename Graph::Node,bool > _nodePsTexts;  
alpar@1085
   116
  char *_nodePsTextsPreamble;
alpar@1085
   117
  
alpar@1073
   118
  bool _undir;
alpar@1073
   119
  bool _pleaseRemoveOsStream;
alpar@1103
   120
alpar@1103
   121
  bool _scaleToA4;
alpar@1103
   122
  
alpar@1073
   123
  ///Constructor
alpar@1073
   124
alpar@1073
   125
  ///Constructor
alpar@1073
   126
  ///\param _g is a reference to the graph to be printed
alpar@1073
   127
  ///\param _os is a reference to the output stream.
alpar@1073
   128
  ///\param _os is a reference to the output stream.
alpar@1073
   129
  ///\param _pros If it is \c true, then the \c ostream referenced by \c _os
alpar@1073
   130
  ///will be explicitly deallocated by the destructor.
alpar@1073
   131
  ///By default it is <tt>std::cout</tt>
alpar@1073
   132
  DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
alpar@1073
   133
			  bool _pros=false) :
alpar@1073
   134
    g(_g), os(_os),
alpar@1086
   135
    _coords(xy<double>(1,1)), _nodeSizes(1.0), _nodeShapes(0),
alpar@1073
   136
    _nodeColors(Color(1,1,1)), _edgeColors(Color(0,0,0)),
alpar@1073
   137
    _edgeWidths(1), _edgeWidthScale(0.3),
alpar@1073
   138
    _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0),
alpar@1073
   139
    _nodeBorderQuotient(.1),
alpar@1073
   140
    _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
alpar@1073
   141
    _showNodes(true), _showEdges(true),
alpar@1073
   142
    _enableParallel(false), _parEdgeDist(1),
alpar@1073
   143
    _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
alpar@1085
   144
    _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
alpar@1073
   145
    _undir(false),
alpar@1103
   146
    _pleaseRemoveOsStream(_pros), _scaleToA4(false) {}
alpar@1073
   147
};
alpar@1073
   148
alpar@1073
   149
///Helper class to implement the named parameters of \ref graphToEps()
alpar@1073
   150
alpar@1073
   151
///Helper class to implement the named parameters of \ref graphToEps()
alpar@1073
   152
///\todo Is 'helper class' a good name for this?
alpar@1073
   153
///
alpar@1103
   154
///\todo Follow PostScript's DSC.
alpar@1103
   155
///\todo Use own dictionary.
alpar@1073
   156
template<class T> class GraphToEps : public T 
alpar@1073
   157
{
alpar@1073
   158
  typedef typename T::Graph Graph;
alpar@1073
   159
  typedef typename Graph::Node Node;
alpar@1073
   160
  typedef typename Graph::NodeIt NodeIt;
alpar@1073
   161
  typedef typename Graph::Edge Edge;
alpar@1073
   162
  typedef typename Graph::EdgeIt EdgeIt;
alpar@1073
   163
  typedef typename Graph::InEdgeIt InEdgeIt;
alpar@1073
   164
  typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@1073
   165
alpar@1087
   166
  static const int INTERPOL_PREC=20;
alpar@1087
   167
alpar@1073
   168
  bool dontPrint;
alpar@1073
   169
alpar@1088
   170
  enum NodeShapes { CIRCLE=0, SQUARE=1, DIAMOND=2 };
alpar@1086
   171
		   
alpar@1073
   172
  class edgeLess {
alpar@1073
   173
    const Graph &g;
alpar@1073
   174
  public:
alpar@1073
   175
    edgeLess(const Graph &_g) : g(_g) {}
alpar@1073
   176
    bool operator()(Edge a,Edge b) const 
alpar@1073
   177
    {
alpar@1073
   178
      Node ai=min(g.source(a),g.target(a));
alpar@1073
   179
      Node aa=max(g.source(a),g.target(a));
alpar@1073
   180
      Node bi=min(g.source(b),g.target(b));
alpar@1073
   181
      Node ba=max(g.source(b),g.target(b));
alpar@1073
   182
      return ai<bi ||
alpar@1073
   183
	(ai==bi && (aa < ba || 
alpar@1073
   184
		    (aa==ba && ai==g.source(a) && bi==g.target(b))));
alpar@1073
   185
    }
alpar@1073
   186
  };
alpar@1073
   187
  bool isParallel(Edge e,Edge f) const
alpar@1073
   188
  {
alpar@1073
   189
    return (g.source(e)==g.source(f)&&g.target(e)==g.target(f))||
alpar@1073
   190
      (g.source(e)==g.target(f)&&g.target(e)==g.source(f));
alpar@1073
   191
  }
alpar@1073
   192
  static xy<double> rot(xy<double> v) 
alpar@1073
   193
  {
alpar@1073
   194
    return xy<double>(v.y,-v.x);
alpar@1073
   195
  }
alpar@1073
   196
  template<class xy>
alpar@1073
   197
  static std::string psOut(const xy &p) 
alpar@1073
   198
    {
alpar@1073
   199
      std::ostringstream os;	
alpar@1073
   200
      os << p.x << ' ' << p.y;
alpar@1073
   201
      return os.str();
alpar@1073
   202
    }
alpar@1073
   203
  
alpar@1073
   204
public:
alpar@1073
   205
  GraphToEps(const T &t) : T(t), dontPrint(false) {};
alpar@1073
   206
  
alpar@1073
   207
  template<class X> struct CoordsTraits : public T {
alpar@1073
   208
    const X &_coords;
alpar@1073
   209
    CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
alpar@1073
   210
  };
alpar@1073
   211
  ///Sets the map of the node coordinates
alpar@1073
   212
alpar@1073
   213
  ///Sets the map of the node coordinates.
alpar@1103
   214
  ///\param x must be a node map with xy<double> or \ref xy "xy<int>" values. 
alpar@1073
   215
  template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
alpar@1073
   216
    dontPrint=true;
alpar@1073
   217
    return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
alpar@1073
   218
  }
alpar@1073
   219
  template<class X> struct NodeSizesTraits : public T {
alpar@1073
   220
    const X &_nodeSizes;
alpar@1073
   221
    NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
alpar@1073
   222
  };
alpar@1073
   223
  ///Sets the map of the node sizes
alpar@1073
   224
alpar@1073
   225
  ///Sets the map of the node sizes
alpar@1073
   226
  ///\param x must be a node map with \c double (or convertible) values. 
alpar@1073
   227
  template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
alpar@1073
   228
  {
alpar@1073
   229
    dontPrint=true;
alpar@1073
   230
    return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
alpar@1073
   231
  }
alpar@1086
   232
  template<class X> struct NodeShapesTraits : public T {
alpar@1086
   233
    const X &_nodeShapes;
alpar@1086
   234
    NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
alpar@1086
   235
  };
alpar@1086
   236
  ///Sets the map of the node shapes
alpar@1086
   237
alpar@1086
   238
  ///Sets the map of the node shapes
alpar@1086
   239
  ///\param x must be a node map with \c int (or convertible) values. 
alpar@1086
   240
  ///\todo Incomplete doc.
alpar@1086
   241
  template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
alpar@1086
   242
  {
alpar@1086
   243
    dontPrint=true;
alpar@1086
   244
    return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
alpar@1086
   245
  }
alpar@1073
   246
  template<class X> struct NodeTextsTraits : public T {
alpar@1073
   247
    const X &_nodeTexts;
alpar@1073
   248
    NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
alpar@1073
   249
  };
alpar@1073
   250
  ///Sets the text printed on the nodes
alpar@1073
   251
alpar@1073
   252
  ///Sets the text printed on the nodes
alpar@1073
   253
  ///\param x must be a node map with type that can be pushed to a standard
alpar@1073
   254
  ///ostream. 
alpar@1073
   255
  template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
alpar@1073
   256
  {
alpar@1073
   257
    dontPrint=true;
alpar@1073
   258
    _showNodeText=true;
alpar@1073
   259
    return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
alpar@1073
   260
  }
alpar@1085
   261
  template<class X> struct NodePsTextsTraits : public T {
alpar@1085
   262
    const X &_nodePsTexts;
alpar@1085
   263
    NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
alpar@1085
   264
  };
alpar@1085
   265
  ///Inserts a PostScript block to the nodes
alpar@1085
   266
alpar@1085
   267
  ///With this command it is possible to insert a verbatim PostScript
alpar@1085
   268
  ///block to the nodes.
alpar@1085
   269
  ///The PS current point will be moved to the centre of the node before
alpar@1085
   270
  ///the PostScript block inserted.
alpar@1085
   271
  ///
alpar@1085
   272
  ///Before and after the block a newline character is inserted to you
alpar@1085
   273
  ///don't have to bother with the separators.
alpar@1085
   274
  ///
alpar@1085
   275
  ///\param x must be a node map with type that can be pushed to a standard
alpar@1085
   276
  ///ostream.
alpar@1085
   277
  ///
alpar@1085
   278
  ///\sa nodePsTextsPreamble()
alpar@1085
   279
  ///\todo Offer the choise not to move to the centre but pass the coordinates
alpar@1085
   280
  ///to the Postscript block inserted.
alpar@1085
   281
  template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
alpar@1085
   282
  {
alpar@1085
   283
    dontPrint=true;
alpar@1085
   284
    _showNodePsText=true;
alpar@1085
   285
    return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
alpar@1085
   286
  }
alpar@1085
   287
  template<class X> struct EdgeWidthsTraits : public T {
alpar@1073
   288
    const X &_edgeWidths;
alpar@1073
   289
    EdgeWidthsTraits(const T &t,const X &x) : T(t), _edgeWidths(x) {}
alpar@1073
   290
  };
alpar@1073
   291
  ///Sets the map of the edge widths
alpar@1073
   292
alpar@1073
   293
  ///Sets the map of the edge widths
alpar@1073
   294
  ///\param x must be a edge map with \c double (or convertible) values. 
alpar@1073
   295
  template<class X> GraphToEps<EdgeWidthsTraits<X> > edgeWidths(const X &x)
alpar@1073
   296
  {
alpar@1073
   297
    dontPrint=true;
alpar@1073
   298
    return GraphToEps<EdgeWidthsTraits<X> >(EdgeWidthsTraits<X>(*this,x));
alpar@1073
   299
  }
alpar@1073
   300
alpar@1073
   301
  template<class X> struct NodeColorsTraits : public T {
alpar@1073
   302
    const X &_nodeColors;
alpar@1073
   303
    NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
alpar@1073
   304
  };
alpar@1073
   305
  ///Sets the map of the node colors
alpar@1073
   306
alpar@1073
   307
  ///Sets the map of the node colors
alpar@1073
   308
  ///\param x must be a node map with \ref Color values. 
alpar@1073
   309
  template<class X> GraphToEps<NodeColorsTraits<X> >
alpar@1073
   310
  nodeColors(const X &x)
alpar@1073
   311
  {
alpar@1073
   312
    dontPrint=true;
alpar@1073
   313
    return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
alpar@1073
   314
  }
alpar@1073
   315
  template<class X> struct EdgeColorsTraits : public T {
alpar@1073
   316
    const X &_edgeColors;
alpar@1073
   317
    EdgeColorsTraits(const T &t,const X &x) : T(t), _edgeColors(x) {}
alpar@1073
   318
  };
alpar@1073
   319
  ///Sets the map of the edge colors
alpar@1073
   320
alpar@1073
   321
  ///Sets the map of the edge colors
alpar@1073
   322
  ///\param x must be a edge map with \ref Color values. 
alpar@1073
   323
  template<class X> GraphToEps<EdgeColorsTraits<X> >
alpar@1073
   324
  edgeColors(const X &x)
alpar@1073
   325
  {
alpar@1073
   326
    dontPrint=true;
alpar@1073
   327
    return GraphToEps<EdgeColorsTraits<X> >(EdgeColorsTraits<X>(*this,x));
alpar@1073
   328
  }
alpar@1073
   329
  ///Sets a global scale factor for node sizes
alpar@1073
   330
alpar@1073
   331
  ///Sets a global scale factor for node sizes
alpar@1073
   332
  ///
alpar@1073
   333
  GraphToEps<T> &nodeScale(double d) {_nodeScale=d;return *this;}
alpar@1073
   334
  ///Sets a global scale factor for edge widths
alpar@1073
   335
alpar@1073
   336
  ///Sets a global scale factor for edge widths
alpar@1073
   337
  ///
alpar@1073
   338
  GraphToEps<T> &edgeWidthScale(double d) {_edgeWidthScale=d;return *this;}
alpar@1073
   339
  ///Sets a global scale factor for the whole picture
alpar@1073
   340
alpar@1073
   341
  ///Sets a global scale factor for the whole picture
alpar@1073
   342
  ///
alpar@1073
   343
  GraphToEps<T> &scale(double d) {_scale=d;return *this;}
alpar@1073
   344
  ///Sets the width of the border around the picture
alpar@1073
   345
alpar@1073
   346
  ///Sets the width of the border around the picture
alpar@1073
   347
  ///
alpar@1073
   348
  GraphToEps<T> &border(double b) {_xBorder=_yBorder=b;return *this;}
alpar@1073
   349
  ///Sets the width of the border around the picture
alpar@1073
   350
alpar@1073
   351
  ///Sets the width of the border around the picture
alpar@1073
   352
  ///
alpar@1073
   353
  GraphToEps<T> &border(double x, double y) {
alpar@1073
   354
    _xBorder=x;_yBorder=y;return *this;
alpar@1073
   355
  }
alpar@1073
   356
  ///Sets whether to draw arrows
alpar@1073
   357
alpar@1073
   358
  ///Sets whether to draw arrows
alpar@1073
   359
  ///
alpar@1073
   360
  GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
alpar@1073
   361
  ///Sets the length of the arrowheads
alpar@1073
   362
alpar@1073
   363
  ///Sets the length of the arrowheads
alpar@1073
   364
  ///
alpar@1073
   365
  GraphToEps<T> &arrowLength(double d) {_arrowLength*=d;return *this;}
alpar@1073
   366
  ///Sets the width of the arrowheads
alpar@1073
   367
alpar@1073
   368
  ///Sets the width of the arrowheads
alpar@1073
   369
  ///
alpar@1073
   370
  GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;}
alpar@1073
   371
  
alpar@1103
   372
  ///Scales the drawing to fit to A4 page
alpar@1103
   373
alpar@1103
   374
  ///Scales the drawing to fit to A4 page
alpar@1103
   375
  ///
alpar@1103
   376
  GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
alpar@1103
   377
  
alpar@1073
   378
  ///Enables parallel edges
alpar@1073
   379
alpar@1073
   380
  ///Enables parallel edges
alpar@1073
   381
  ///\todo Partially implemented
alpar@1073
   382
  GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
alpar@1073
   383
  
alpar@1073
   384
  ///Sets the distance 
alpar@1073
   385
  
alpar@1073
   386
  ///Sets the distance 
alpar@1073
   387
  ///
alpar@1073
   388
  GraphToEps<T> &parEdgeDist(double d) {_parEdgeDist*=d;return *this;}
alpar@1073
   389
  
alpar@1073
   390
  ///Hides the edges
alpar@1073
   391
  
alpar@1073
   392
  ///Hides the edges
alpar@1073
   393
  ///
alpar@1073
   394
  GraphToEps<T> &hideEdges(bool b=true) {_showEdges=!b;return *this;}
alpar@1073
   395
  ///Hides the nodes
alpar@1073
   396
  
alpar@1073
   397
  ///Hides the nodes
alpar@1073
   398
  ///
alpar@1073
   399
  GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
alpar@1073
   400
  
alpar@1073
   401
  ///Sets the size of the node texts
alpar@1073
   402
  
alpar@1073
   403
  ///Sets the size of the node texts
alpar@1073
   404
  ///
alpar@1073
   405
  GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
alpar@1085
   406
  ///Gives a preamble block for node Postscript block.
alpar@1085
   407
  
alpar@1085
   408
  ///Gives a preamble block for node Postscript block.
alpar@1085
   409
  ///
alpar@1085
   410
  ///\sa nodePsTexts()
alpar@1085
   411
  GraphToEps<T> & nodePsTextsPreamble(const char *str) {
alpar@1085
   412
    _nodePsTextsPreamble=s ;return *this;
alpar@1085
   413
  }
alpar@1073
   414
  ///Sets whether the the graph is undirected
alpar@1073
   415
alpar@1073
   416
  ///Sets whether the the graph is undirected
alpar@1073
   417
  ///
alpar@1073
   418
  GraphToEps<T> &undir(bool b=true) {_undir=b;return *this;}
alpar@1073
   419
  ///Sets whether the the graph is directed
alpar@1073
   420
alpar@1073
   421
  ///Sets whether the the graph is directed.
alpar@1073
   422
  ///Use it to show the undirected edges as a pair of directed ones.
alpar@1073
   423
  GraphToEps<T> &bidir(bool b=true) {_undir=!b;return *this;}
alpar@1086
   424
alpar@1086
   425
protected:
alpar@1086
   426
  bool isInsideNode(xy<double> p, double r,int t) 
alpar@1086
   427
  {
alpar@1086
   428
    switch(t) {
alpar@1086
   429
    case CIRCLE:
alpar@1086
   430
      return p.normSquare()<=r*r;
alpar@1086
   431
    case SQUARE:
alpar@1086
   432
      return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
alpar@1088
   433
    case DIAMOND:
alpar@1088
   434
      return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
alpar@1086
   435
    }
alpar@1086
   436
    return false;
alpar@1086
   437
  }
alpar@1086
   438
alpar@1086
   439
public:
alpar@1091
   440
  ~GraphToEps() { }
alpar@1091
   441
  
alpar@1091
   442
  ///Draws the graph.
alpar@1091
   443
alpar@1091
   444
  ///Like other functions using
alpar@1091
   445
  ///\ref named-templ-func-param "named template parameters",
alpar@1091
   446
  ///this function calles the algorithm itself, i.e. in this case
alpar@1091
   447
  ///it draws the graph.
alpar@1091
   448
  void run() {
alpar@1073
   449
    if(dontPrint) return;
alpar@1073
   450
    
alpar@1073
   451
    os << "%!PS-Adobe-2.0 EPSF-2.0\n";
alpar@1073
   452
    //\todo: Chech whether the graph is empty.
alpar@1073
   453
    BoundingBox<double> bb;
alpar@1073
   454
    for(NodeIt n(g);n!=INVALID;++n) {
alpar@1073
   455
      double ns=_nodeSizes[n]*_nodeScale;
alpar@1073
   456
      xy<double> p(ns,ns);
alpar@1073
   457
      bb+=p+_coords[n];
alpar@1073
   458
      bb+=-p+_coords[n];
alpar@1073
   459
      }
alpar@1103
   460
    if(!_scaleToA4) os << "%%BoundingBox: "
alpar@1103
   461
		      << bb.left()*  _scale-_xBorder << ' '
alpar@1103
   462
		      << bb.bottom()*_scale-_yBorder << ' '
alpar@1103
   463
		      << bb.right()* _scale+_xBorder << ' '
alpar@1103
   464
		      << bb.top()*   _scale+_yBorder << '\n';
alpar@1073
   465
    //x1 y1 x2 y2 x3 y3 cr cg cb w
alpar@1073
   466
    os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
alpar@1073
   467
       << "      4 2 roll 1 index 1 index curveto stroke } bind def\n";
alpar@1073
   468
    os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
alpar@1086
   469
    //x y r
alpar@1073
   470
    os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n";
alpar@1086
   471
    //x y r
alpar@1086
   472
    os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
alpar@1086
   473
       << "      2 index 1 index sub 2 index 2 index add lineto\n"
alpar@1086
   474
       << "      2 index 1 index sub 2 index 2 index sub lineto\n"
alpar@1086
   475
       << "      2 index 1 index add 2 index 2 index sub lineto\n"
alpar@1086
   476
       << "      closepath pop pop pop} bind def\n";
alpar@1088
   477
    //x y r
alpar@1088
   478
    os << "/di { newpath 2 index 1 index add 2 index moveto\n"
alpar@1088
   479
       << "      2 index             2 index 2 index add lineto\n"
alpar@1088
   480
       << "      2 index 1 index sub 2 index             lineto\n"
alpar@1088
   481
       << "      2 index             2 index 2 index sub lineto\n"
alpar@1088
   482
       << "      closepath pop pop pop} bind def\n";
alpar@1073
   483
    // x y r cr cg cb
alpar@1089
   484
    os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
alpar@1089
   485
       << "     setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
alpar@1073
   486
       << "   } bind def\n";
alpar@1089
   487
    os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
alpar@1089
   488
       << "     setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
alpar@1086
   489
       << "   } bind def\n";
alpar@1089
   490
    os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
alpar@1089
   491
       << "     setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
alpar@1088
   492
       << "   } bind def\n";
alpar@1073
   493
    os << "/arrl " << _arrowLength << " def\n";
alpar@1073
   494
    os << "/arrw " << _arrowWidth << " def\n";
alpar@1073
   495
    // l dx_norm dy_norm
alpar@1073
   496
    os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
alpar@1073
   497
    //len w dx_norm dy_norm x1 y1 cr cg cb
alpar@1073
   498
    os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
alpar@1073
   499
       << "       /w exch def /len exch def\n"
alpar@1073
   500
      //	 << "       0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
alpar@1073
   501
       << "       newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
alpar@1073
   502
       << "       len w sub arrl sub dx dy lrl\n"
alpar@1073
   503
       << "       arrw dy dx neg lrl\n"
alpar@1073
   504
       << "       dx arrl w add mul dy w 2 div arrw add mul sub\n"
alpar@1073
   505
       << "       dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
alpar@1073
   506
       << "       dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
alpar@1073
   507
       << "       dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
alpar@1073
   508
       << "       arrw dy dx neg lrl\n"
alpar@1073
   509
       << "       len w sub arrl sub neg dx dy lrl\n"
alpar@1073
   510
       << "       closepath fill } bind def\n";
alpar@1073
   511
    os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
alpar@1073
   512
       << "         neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
alpar@1073
   513
alpar@1073
   514
    os << "\ngsave\n";
alpar@1103
   515
    if(_scaleToA4)
alpar@1103
   516
      if(bb.height()>bb.width()) {
alpar@1103
   517
	double sc= min((A4HEIGHT-2*A4BORDER)/bb.height(),
alpar@1103
   518
		  (A4WIDTH-2*A4BORDER)/bb.width());
alpar@1103
   519
	os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
alpar@1103
   520
	   << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER << " translate\n"
alpar@1103
   521
	   << sc << " dup scale\n"
alpar@1103
   522
	   << -bb.left() << ' ' << -bb.bottom() << " translate\n";
alpar@1103
   523
      }
alpar@1103
   524
      else {
alpar@1103
   525
	//\todo Verify centering
alpar@1103
   526
	double sc= min((A4HEIGHT-2*A4BORDER)/bb.width(),
alpar@1103
   527
		  (A4WIDTH-2*A4BORDER)/bb.height());
alpar@1103
   528
	os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
alpar@1103
   529
	   << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER  << " translate\n"
alpar@1103
   530
	   << sc << " dup scale\n90 rotate\n"
alpar@1103
   531
	   << -bb.left() << ' ' << -bb.top() << " translate\n";	
alpar@1103
   532
	}
alpar@1103
   533
    else if(_scale!=1.0) os << _scale << " dup scale\n";
alpar@1073
   534
    
alpar@1085
   535
    if(_showEdges) {
alpar@1085
   536
      os << "%Edges:\ngsave\n";      
alpar@1073
   537
      if(_enableParallel) {
alpar@1073
   538
	std::vector<Edge> el;
alpar@1073
   539
	for(EdgeIt e(g);e!=INVALID;++e)
alpar@1073
   540
	  if(!_undir||g.source(e)<g.target(e)) el.push_back(e);
alpar@1073
   541
	sort(el.begin(),el.end(),edgeLess(g));
alpar@1073
   542
	
alpar@1073
   543
	typename std::vector<Edge>::iterator j;
alpar@1073
   544
	for(typename std::vector<Edge>::iterator i=el.begin();i!=el.end();i=j) {
alpar@1073
   545
	  for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
alpar@1073
   546
alpar@1073
   547
	  double sw=0;
alpar@1073
   548
	  for(typename std::vector<Edge>::iterator e=i;e!=j;++e)
alpar@1073
   549
	    sw+=_edgeWidths[*e]*_edgeWidthScale+_parEdgeDist;
alpar@1073
   550
	  sw-=_parEdgeDist;
alpar@1073
   551
	  sw/=-2.0;
alpar@1085
   552
	  xy<double> dvec(_coords[g.target(*i)]-_coords[g.source(*i)]);
alpar@1085
   553
	  double l=sqrt(dvec.normSquare());
alpar@1085
   554
	  xy<double> d(dvec/l);
alpar@1085
   555
 	  xy<double> m;
alpar@1085
   556
// 	  m=xy<double>(_coords[g.target(*i)]+_coords[g.source(*i)])/2.0;
alpar@1085
   557
alpar@1085
   558
//  	  m=xy<double>(_coords[g.source(*i)])+
alpar@1085
   559
// 	    dvec*(double(_nodeSizes[g.source(*i)])/
alpar@1085
   560
// 	       (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
alpar@1085
   561
alpar@1085
   562
 	  m=xy<double>(_coords[g.source(*i)])+
alpar@1085
   563
	    d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
alpar@1085
   564
alpar@1073
   565
	  for(typename std::vector<Edge>::iterator e=i;e!=j;++e) {
alpar@1073
   566
	    sw+=_edgeWidths[*e]*_edgeWidthScale/2.0;
alpar@1085
   567
	    xy<double> mm=m+rot(d)*sw/.75;
alpar@1073
   568
	    if(_drawArrows) {
alpar@1086
   569
	      int node_shape;
alpar@1073
   570
	      xy<double> s=_coords[g.source(*e)];
alpar@1073
   571
	      xy<double> t=_coords[g.target(*e)];
alpar@1073
   572
	      double rn=_nodeSizes[g.target(*e)]*_nodeScale;
alpar@1086
   573
	      node_shape=_nodeShapes[g.target(*e)];
alpar@1085
   574
	      Bezier3 bez(s,mm,mm,t);
alpar@1073
   575
	      double t1=0,t2=1;
alpar@1087
   576
	      for(int i=0;i<INTERPOL_PREC;++i)
alpar@1086
   577
		if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
alpar@1086
   578
		else t1=(t1+t2)/2;
alpar@1073
   579
	      xy<double> apoint=bez((t1+t2)/2);
alpar@1086
   580
	      rn = _arrowLength+_edgeWidths[*e]*_edgeWidthScale;
alpar@1073
   581
	      rn*=rn;
alpar@1086
   582
	      t2=(t1+t2)/2;t1=0;
alpar@1087
   583
	      for(int i=0;i<INTERPOL_PREC;++i)
alpar@1086
   584
		if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
alpar@1073
   585
		else t2=(t1+t2)/2;
alpar@1073
   586
	      xy<double> linend=bez((t1+t2)/2);	      
alpar@1073
   587
	      bez=bez.before((t1+t2)/2);
alpar@1086
   588
// 	      rn=_nodeSizes[g.source(*e)]*_nodeScale;
alpar@1086
   589
// 	      node_shape=_nodeShapes[g.source(*e)];
alpar@1086
   590
// 	      t1=0;t2=1;
alpar@1087
   591
// 	      for(int i=0;i<INTERPOL_PREC;++i)
alpar@1086
   592
// 		if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t1=(t1+t2)/2;
alpar@1086
   593
// 		else t2=(t1+t2)/2;
alpar@1086
   594
// 	      bez=bez.after((t1+t2)/2);
alpar@1073
   595
	      os << _edgeWidths[*e]*_edgeWidthScale << " setlinewidth "
alpar@1073
   596
		 << _edgeColors[*e].getR() << ' '
alpar@1073
   597
		 << _edgeColors[*e].getG() << ' '
alpar@1073
   598
		 << _edgeColors[*e].getB() << " setrgbcolor newpath\n"
alpar@1073
   599
		 << bez.p1.x << ' ' <<  bez.p1.y << " moveto\n"
alpar@1073
   600
		 << bez.p2.x << ' ' << bez.p2.y << ' '
alpar@1073
   601
		 << bez.p3.x << ' ' << bez.p3.y << ' '
alpar@1073
   602
		 << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
alpar@1073
   603
	      xy<double> dd(rot(linend-apoint));
alpar@1089
   604
	      dd*=(.5*_edgeWidths[*e]*_edgeWidthScale+_arrowWidth)/
alpar@1073
   605
		sqrt(dd.normSquare());
alpar@1073
   606
	      os << "newpath " << psOut(apoint) << " moveto "
alpar@1073
   607
		 << psOut(linend+dd) << " lineto "
alpar@1073
   608
		 << psOut(linend-dd) << " lineto closepath fill\n";
alpar@1073
   609
	    }
alpar@1073
   610
	    else {
alpar@1073
   611
	      os << _coords[g.source(*e)].x << ' '
alpar@1073
   612
		 << _coords[g.source(*e)].y << ' '
alpar@1085
   613
		 << mm.x << ' ' << mm.y << ' '
alpar@1073
   614
		 << _coords[g.target(*e)].x << ' '
alpar@1073
   615
		 << _coords[g.target(*e)].y << ' '
alpar@1073
   616
		 << _edgeColors[*e].getR() << ' '
alpar@1073
   617
		 << _edgeColors[*e].getG() << ' '
alpar@1073
   618
		 << _edgeColors[*e].getB() << ' '
alpar@1073
   619
		 << _edgeWidths[*e]*_edgeWidthScale << " lb\n";
alpar@1073
   620
	    }
alpar@1073
   621
	    sw+=_edgeWidths[*e]*_edgeWidthScale/2.0+_parEdgeDist;
alpar@1073
   622
	  }
alpar@1073
   623
	}
alpar@1073
   624
      }
alpar@1073
   625
      else for(EdgeIt e(g);e!=INVALID;++e)
alpar@1073
   626
	if(!_undir||g.source(e)<g.target(e))
alpar@1073
   627
	  if(_drawArrows) {
alpar@1073
   628
	    xy<double> d(_coords[g.target(e)]-_coords[g.source(e)]);
alpar@1087
   629
	    double rn=_nodeSizes[g.target(e)]*_nodeScale;
alpar@1087
   630
	    int node_shape=_nodeShapes[g.target(e)];
alpar@1087
   631
	    double t1=0,t2=1;
alpar@1087
   632
	    for(int i=0;i<INTERPOL_PREC;++i)
alpar@1087
   633
	      if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
alpar@1087
   634
	      else t2=(t1+t2)/2;
alpar@1073
   635
	    double l=sqrt(d.normSquare());
alpar@1073
   636
	    d/=l;
alpar@1087
   637
	    
alpar@1087
   638
	    os << l*(1-(t1+t2)/2) << ' '
alpar@1073
   639
	       << _edgeWidths[e]*_edgeWidthScale << ' '
alpar@1073
   640
	       << d.x << ' ' << d.y << ' '
alpar@1087
   641
	       << _coords[g.source(e)].x << ' '
alpar@1087
   642
	       << _coords[g.source(e)].y << ' '
alpar@1073
   643
	       << _edgeColors[e].getR() << ' '
alpar@1073
   644
	       << _edgeColors[e].getG() << ' '
alpar@1073
   645
	       << _edgeColors[e].getB() << " arr\n";
alpar@1073
   646
	  }
alpar@1073
   647
	  else os << _coords[g.source(e)].x << ' '
alpar@1073
   648
		  << _coords[g.source(e)].y << ' '
alpar@1073
   649
		  << _coords[g.target(e)].x << ' '
alpar@1073
   650
		  << _coords[g.target(e)].y << ' '
alpar@1073
   651
		  << _edgeColors[e].getR() << ' '
alpar@1073
   652
		  << _edgeColors[e].getG() << ' '
alpar@1073
   653
		  << _edgeColors[e].getB() << ' '
alpar@1073
   654
		  << _edgeWidths[e]*_edgeWidthScale << " l\n";
alpar@1085
   655
      os << "grestore\n";
alpar@1085
   656
    }
alpar@1085
   657
    if(_showNodes) {
alpar@1085
   658
      os << "%Nodes:\ngsave\n";
alpar@1086
   659
      for(NodeIt n(g);n!=INVALID;++n) {
alpar@1073
   660
	os << _coords[n].x << ' ' << _coords[n].y << ' '
alpar@1073
   661
	   << _nodeSizes[n]*_nodeScale << ' '
alpar@1073
   662
	   << _nodeColors[n].getR() << ' '
alpar@1073
   663
	   << _nodeColors[n].getG() << ' '
alpar@1086
   664
	   << _nodeColors[n].getB() << ' ';
alpar@1086
   665
	switch(_nodeShapes[n]) {
alpar@1086
   666
	case CIRCLE:
alpar@1086
   667
	  os<< "nc";break;
alpar@1086
   668
	case SQUARE:
alpar@1086
   669
	  os<< "nsq";break;
alpar@1088
   670
	case DIAMOND:
alpar@1088
   671
	  os<< "ndi";break;
alpar@1086
   672
	}
alpar@1086
   673
	os<<'\n';
alpar@1086
   674
      }
alpar@1085
   675
      os << "grestore\n";
alpar@1085
   676
    }
alpar@1073
   677
    if(_showNodeText) {
alpar@1085
   678
      os << "%Node texts:\ngsave\n";
alpar@1073
   679
      os << "/fosi " << _nodeTextSize << " def\n";
alpar@1073
   680
      os << "(Helvetica) findfont fosi scalefont setfont\n";
alpar@1073
   681
      os << "0 0 0 setrgbcolor\n";
alpar@1073
   682
      for(NodeIt n(g);n!=INVALID;++n)
alpar@1073
   683
	os << _coords[n].x << ' ' << _coords[n].y
alpar@1073
   684
	   << " (" << _nodeTexts[n] << ") cshow\n";
alpar@1085
   685
      os << "grestore\n";
alpar@1073
   686
    }
alpar@1085
   687
    if(_showNodePsText) {
alpar@1085
   688
      os << "%Node PS blocks:\ngsave\n";
alpar@1085
   689
      for(NodeIt n(g);n!=INVALID;++n)
alpar@1085
   690
	os << _coords[n].x << ' ' << _coords[n].y
alpar@1085
   691
	   << " moveto\n" << _nodePsTexts[n] << "\n";
alpar@1085
   692
      os << "grestore\n";
alpar@1085
   693
    }
alpar@1085
   694
    
alpar@1103
   695
    os << "grestore\nshowpage\n";
alpar@1073
   696
alpar@1073
   697
    //CleanUp:
alpar@1073
   698
    if(_pleaseRemoveOsStream) {delete &os;}
alpar@1073
   699
  } 
alpar@1073
   700
};
alpar@1073
   701
alpar@1073
   702
alpar@1073
   703
///Generates an EPS file from a graph
alpar@1073
   704
alpar@1073
   705
///\ingroup misc
alpar@1073
   706
///Generates an EPS file from a graph.
alpar@1073
   707
///\param g is a reference to the graph to be printed
alpar@1073
   708
///\param os is a reference to the output stream.
alpar@1073
   709
///By default it is <tt>std::cout</tt>
alpar@1073
   710
///
alpar@1091
   711
///This function also has a lot of
alpar@1091
   712
///\ref named-templ-func-param "named parameters",
alpar@1073
   713
///they are declared as the members of class \ref GraphToEps. The following
alpar@1073
   714
///example shows how to use these parameters.
alpar@1073
   715
///\code
alpar@1073
   716
/// graphToEps(g).scale(10).coords(coords)
alpar@1073
   717
///              .nodeScale(2).nodeSizes(sizes)
alpar@1091
   718
///              .edgeWidthScale(.4).run();
alpar@1073
   719
///\endcode
alpar@1091
   720
///\warning Don't forget to put the \ref GraphToEps::run() "run()"
alpar@1091
   721
///to the end of the parameter list.
alpar@1073
   722
///\sa GraphToEps
alpar@1073
   723
///\sa graphToEps(G &g, char *file_name)
alpar@1073
   724
template<class G>
alpar@1073
   725
GraphToEps<DefaultGraphToEpsTraits<G> > 
alpar@1073
   726
graphToEps(G &g, std::ostream& os=std::cout)
alpar@1073
   727
{
alpar@1073
   728
  return 
alpar@1073
   729
    GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
alpar@1073
   730
}
alpar@1073
   731
 
alpar@1073
   732
///Generates an EPS file from a graph
alpar@1073
   733
alpar@1103
   734
///\ingroup misc
alpar@1073
   735
///This function does the same as
alpar@1073
   736
///\ref graphToEps(G &g,std::ostream& os)
alpar@1073
   737
///but it writes its output into the file \c file_name
alpar@1073
   738
///instead of a stream.
alpar@1073
   739
///\sa graphToEps(G &g, std::ostream& os)
alpar@1073
   740
template<class G>
alpar@1073
   741
GraphToEps<DefaultGraphToEpsTraits<G> > 
alpar@1073
   742
graphToEps(G &g,char *file_name)
alpar@1073
   743
{
alpar@1073
   744
  return GraphToEps<DefaultGraphToEpsTraits<G> >
alpar@1073
   745
    (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true));
alpar@1073
   746
}
alpar@1073
   747
alpar@1073
   748
} //END OF NAMESPACE LEMON
alpar@1073
   749
alpar@1073
   750
#endif // LEMON_GRAPH_TO_EPS_H