lemon/graph_to_eps.h
author ladanyi
Tue, 23 Aug 2005 07:36:09 +0000
changeset 1645 4a04bb856ac7
parent 1604 4d037c2b66aa
child 1673 8919ca9e70f3
permissions -rw-r--r--
- id maps are not editable
- handle exceptions thrown by the file reader
- texts are always above the edges
- store a default value for all maps, so that edges and nodes created after
adding a new map receive the default value too
- create node on button release, not on click (fixes a few oddities)
alpar@1073
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/graph_to_eps.h - Part of LEMON, a generic C++ optimization library
alpar@1073
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, 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@1108
    20
#include <sys/time.h>
alpar@1108
    21
alpar@1073
    22
#include<iostream>
alpar@1073
    23
#include<fstream>
alpar@1073
    24
#include<sstream>
alpar@1073
    25
#include<algorithm>
alpar@1073
    26
#include<vector>
alpar@1073
    27
deba@1417
    28
#include <ctime>
deba@1417
    29
#include <cmath>
deba@1417
    30
alpar@1234
    31
#include<lemon/invalid.h>
alpar@1073
    32
#include<lemon/xy.h>
alpar@1073
    33
#include<lemon/maps.h>
alpar@1073
    34
#include<lemon/bezier.h>
alpar@1073
    35
deba@1417
    36
alpar@1287
    37
///\ingroup io_group
alpar@1073
    38
///\file
alpar@1073
    39
///\brief Simple graph drawer
alpar@1073
    40
///
alpar@1073
    41
///\author Alpar Juttner
alpar@1073
    42
alpar@1073
    43
namespace lemon {
alpar@1073
    44
alpar@1073
    45
///Data structure representing RGB colors.
alpar@1073
    46
alpar@1073
    47
///Data structure representing RGB colors.
alpar@1073
    48
///\ingroup misc
alpar@1073
    49
class Color
alpar@1073
    50
{
alpar@1073
    51
  double _r,_g,_b;
alpar@1073
    52
public:
alpar@1073
    53
  ///Default constructor
alpar@1073
    54
  Color() {}
alpar@1073
    55
  ///Constructor
alpar@1073
    56
  Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
alpar@1073
    57
  ///Returns the red component
alpar@1575
    58
  double & red() {return _r;}
alpar@1575
    59
  ///Returns the red component
alpar@1575
    60
  const double & red() const {return _r;}
alpar@1073
    61
  ///Returns the green component
alpar@1575
    62
  double & green() {return _g;}
alpar@1575
    63
  ///Returns the green component
alpar@1575
    64
  const double & green() const {return _g;}
alpar@1073
    65
  ///Returns the blue component
alpar@1575
    66
  double & blue() {return _b;}
alpar@1575
    67
  ///Returns the blue component
alpar@1575
    68
  const double & blue() const {return _b;}
alpar@1073
    69
  ///Set the color components
alpar@1073
    70
  void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
alpar@1073
    71
};
alpar@1178
    72
alpar@1178
    73
///Maps <tt>int</tt>s to different \ref Color "Color"s
alpar@1178
    74
alpar@1573
    75
///This map assigns one of the predefined \ref Color "Color"s
alpar@1178
    76
///to each <tt>int</tt>. It is possible to change the colors as well as their
alpar@1178
    77
///number. The integer range is cyclically mapped to the provided set of colors.
alpar@1178
    78
///
alpar@1178
    79
///This is a true \ref concept::ReferenceMap "reference map", so you can also
alpar@1178
    80
///change the actual colors.
alpar@1178
    81
alpar@1178
    82
class ColorSet : public MapBase<int,Color>
alpar@1178
    83
{
alpar@1178
    84
  std::vector<Color> colors;
alpar@1178
    85
public:
alpar@1178
    86
  ///Constructor
alpar@1178
    87
alpar@1178
    88
  ///Constructor
alpar@1574
    89
  ///\param have_white indicates whether white is
alpar@1178
    90
  ///amongst the provided color (\c true) or not (\c false). If it is true,
alpar@1178
    91
  ///white will be assigned to \c 0.
alpar@1178
    92
  ///\param num the number of the allocated colors. If it is \c 0
alpar@1178
    93
  ///the default color configuration is set up (26 color plus the while).
alpar@1178
    94
  ///If \c num is less then 26/27 then the default color list is cut. Otherwise
alpar@1178
    95
  ///the color list is filled repeatedly with the default color list.
alpar@1574
    96
  ///(The colors can be changed later on.)
alpar@1178
    97
  ColorSet(bool have_white=false,int num=0)
alpar@1178
    98
  {
alpar@1178
    99
    do {
alpar@1178
   100
      if(have_white) colors.push_back(Color(1,1,1));
alpar@1178
   101
alpar@1178
   102
      colors.push_back(Color(0,0,0));
alpar@1178
   103
      colors.push_back(Color(1,0,0));
alpar@1178
   104
      colors.push_back(Color(0,1,0));
alpar@1178
   105
      colors.push_back(Color(0,0,1));
alpar@1178
   106
      colors.push_back(Color(1,1,0));
alpar@1178
   107
      colors.push_back(Color(1,0,1));
alpar@1178
   108
      colors.push_back(Color(0,1,1));
alpar@1178
   109
      
alpar@1178
   110
      colors.push_back(Color(.5,0,0));
alpar@1178
   111
      colors.push_back(Color(0,.5,0));
alpar@1178
   112
      colors.push_back(Color(0,0,.5));
alpar@1178
   113
      colors.push_back(Color(.5,.5,0));
alpar@1178
   114
      colors.push_back(Color(.5,0,.5));
alpar@1178
   115
      colors.push_back(Color(0,.5,.5));
alpar@1178
   116
      
alpar@1178
   117
      colors.push_back(Color(.5,.5,.5));
alpar@1178
   118
      colors.push_back(Color(1,.5,.5));
alpar@1178
   119
      colors.push_back(Color(.5,1,.5));
alpar@1178
   120
      colors.push_back(Color(.5,.5,1));
alpar@1178
   121
      colors.push_back(Color(1,1,.5));
alpar@1178
   122
      colors.push_back(Color(1,.5,1));
alpar@1178
   123
      colors.push_back(Color(.5,1,1));
alpar@1178
   124
      
alpar@1178
   125
      colors.push_back(Color(1,.5,0));
alpar@1178
   126
      colors.push_back(Color(.5,1,0));
alpar@1178
   127
      colors.push_back(Color(1,0,.5));
alpar@1178
   128
      colors.push_back(Color(0,1,.5));
alpar@1178
   129
      colors.push_back(Color(0,.5,1));
alpar@1178
   130
      colors.push_back(Color(.5,0,1));
alpar@1178
   131
    } while(int(colors.size())<num);
alpar@1178
   132
    //    colors.push_back(Color(1,1,1));
alpar@1178
   133
    if(num>0) colors.resize(num);
alpar@1178
   134
  }
alpar@1178
   135
  ///\e
alpar@1178
   136
  Color &operator[](int i)
alpar@1178
   137
  {
alpar@1178
   138
    return colors[i%colors.size()];
alpar@1178
   139
  }
alpar@1178
   140
  ///\e
alpar@1178
   141
  const Color &operator[](int i) const
alpar@1178
   142
  {
alpar@1178
   143
    return colors[i%colors.size()];
alpar@1178
   144
  }
alpar@1178
   145
  ///\e
alpar@1178
   146
  void set(int i,const Color &c)
alpar@1178
   147
  {
alpar@1178
   148
    colors[i%colors.size()]=c;
alpar@1178
   149
  }
alpar@1178
   150
  ///Sets the number of the exiting colors.
alpar@1178
   151
  void resize(int s) { colors.resize(s);}
alpar@1574
   152
  ///Returns the number of the existing colors.
alpar@1573
   153
  std::size_t size() const { return colors.size();}
alpar@1178
   154
};
alpar@1178
   155
alpar@1178
   156
///Returns a visible distinct \ref Color
alpar@1178
   157
alpar@1178
   158
///Returns a \ref Color which is as different from the given parameter
alpar@1178
   159
///as it is possible.
alpar@1178
   160
inline Color distantColor(const Color &c) 
alpar@1178
   161
{
alpar@1575
   162
  return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0);
alpar@1178
   163
}
alpar@1178
   164
///Returns black for light colors and white for the dark ones.
alpar@1178
   165
alpar@1178
   166
///Returns black for light colors and white for the dark ones.
alpar@1178
   167
inline Color distantBW(const Color &c){
alpar@1575
   168
  double v=(.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5?1:0;
alpar@1178
   169
  return Color(v,v,v);
alpar@1178
   170
}
alpar@1178
   171
alpar@1073
   172
///Default traits class of \ref GraphToEps
alpar@1073
   173
alpar@1073
   174
///Default traits class of \ref GraphToEps
alpar@1073
   175
///
alpar@1073
   176
///\c G is the type of the underlying graph.
alpar@1073
   177
template<class G>
alpar@1073
   178
struct DefaultGraphToEpsTraits
alpar@1073
   179
{
alpar@1073
   180
  typedef G Graph;
alpar@1073
   181
  typedef typename Graph::Node Node;
alpar@1073
   182
  typedef typename Graph::NodeIt NodeIt;
alpar@1073
   183
  typedef typename Graph::Edge Edge;
alpar@1073
   184
  typedef typename Graph::EdgeIt EdgeIt;
alpar@1073
   185
  typedef typename Graph::InEdgeIt InEdgeIt;
alpar@1073
   186
  typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@1073
   187
  
alpar@1073
   188
alpar@1073
   189
  const Graph &g;
alpar@1073
   190
alpar@1073
   191
  std::ostream& os;
alpar@1073
   192
  
alpar@1073
   193
  ConstMap<typename Graph::Node,xy<double> > _coords;
alpar@1073
   194
  ConstMap<typename Graph::Node,double > _nodeSizes;
alpar@1086
   195
  ConstMap<typename Graph::Node,int > _nodeShapes;
alpar@1073
   196
alpar@1073
   197
  ConstMap<typename Graph::Node,Color > _nodeColors;
alpar@1073
   198
  ConstMap<typename Graph::Edge,Color > _edgeColors;
alpar@1073
   199
alpar@1073
   200
  ConstMap<typename Graph::Edge,double > _edgeWidths;
alpar@1103
   201
alpar@1073
   202
  double _edgeWidthScale;
alpar@1073
   203
  
alpar@1073
   204
  double _nodeScale;
alpar@1073
   205
  double _xBorder, _yBorder;
alpar@1073
   206
  double _scale;
alpar@1073
   207
  double _nodeBorderQuotient;
alpar@1073
   208
  
alpar@1073
   209
  bool _drawArrows;
alpar@1073
   210
  double _arrowLength, _arrowWidth;
alpar@1073
   211
  
alpar@1073
   212
  bool _showNodes, _showEdges;
alpar@1073
   213
alpar@1073
   214
  bool _enableParallel;
alpar@1073
   215
  double _parEdgeDist;
alpar@1073
   216
alpar@1073
   217
  bool _showNodeText;
alpar@1073
   218
  ConstMap<typename Graph::Node,bool > _nodeTexts;  
alpar@1073
   219
  double _nodeTextSize;
alpar@1073
   220
alpar@1085
   221
  bool _showNodePsText;
alpar@1085
   222
  ConstMap<typename Graph::Node,bool > _nodePsTexts;  
alpar@1085
   223
  char *_nodePsTextsPreamble;
alpar@1085
   224
  
alpar@1073
   225
  bool _undir;
alpar@1073
   226
  bool _pleaseRemoveOsStream;
alpar@1103
   227
alpar@1103
   228
  bool _scaleToA4;
alpar@1108
   229
alpar@1108
   230
  std::string _title;
alpar@1108
   231
  std::string _copyright;
alpar@1178
   232
alpar@1178
   233
  enum NodeTextColorType 
alpar@1178
   234
    { DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType;
alpar@1178
   235
  ConstMap<typename Graph::Node,Color > _nodeTextColors;
alpar@1178
   236
alpar@1604
   237
  bool _autoNodeScale;
alpar@1604
   238
  bool _autoEdgeWidthScale;
alpar@1604
   239
alpar@1073
   240
  ///Constructor
alpar@1073
   241
alpar@1073
   242
  ///Constructor
alpar@1073
   243
  ///\param _g is a reference to the graph to be printed
alpar@1073
   244
  ///\param _os is a reference to the output stream.
alpar@1073
   245
  ///\param _os is a reference to the output stream.
alpar@1073
   246
  ///\param _pros If it is \c true, then the \c ostream referenced by \c _os
alpar@1073
   247
  ///will be explicitly deallocated by the destructor.
alpar@1073
   248
  ///By default it is <tt>std::cout</tt>
alpar@1073
   249
  DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
alpar@1073
   250
			  bool _pros=false) :
alpar@1073
   251
    g(_g), os(_os),
alpar@1086
   252
    _coords(xy<double>(1,1)), _nodeSizes(1.0), _nodeShapes(0),
alpar@1073
   253
    _nodeColors(Color(1,1,1)), _edgeColors(Color(0,0,0)),
alpar@1073
   254
    _edgeWidths(1), _edgeWidthScale(0.3),
alpar@1073
   255
    _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0),
alpar@1073
   256
    _nodeBorderQuotient(.1),
alpar@1073
   257
    _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
alpar@1073
   258
    _showNodes(true), _showEdges(true),
alpar@1073
   259
    _enableParallel(false), _parEdgeDist(1),
alpar@1073
   260
    _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
alpar@1085
   261
    _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
alpar@1073
   262
    _undir(false),
alpar@1178
   263
    _pleaseRemoveOsStream(_pros), _scaleToA4(false),
alpar@1604
   264
    _nodeTextColorType(SAME_COL), _nodeTextColors(Color(0,0,0)),
alpar@1604
   265
    _autoNodeScale(false),
alpar@1604
   266
    _autoEdgeWidthScale(false)
alpar@1178
   267
  {}
alpar@1073
   268
};
alpar@1073
   269
alpar@1073
   270
///Helper class to implement the named parameters of \ref graphToEps()
alpar@1073
   271
alpar@1073
   272
///Helper class to implement the named parameters of \ref graphToEps()
alpar@1073
   273
///\todo Is 'helper class' a good name for this?
alpar@1073
   274
///
alpar@1103
   275
///\todo Follow PostScript's DSC.
alpar@1107
   276
/// Use own dictionary.
alpar@1107
   277
///\todo Useful new features.
alpar@1107
   278
/// - Linestyles: dotted, dashed etc.
alpar@1107
   279
/// - A second color and percent value for the lines.
alpar@1073
   280
template<class T> class GraphToEps : public T 
alpar@1073
   281
{
alpar@1234
   282
  // Can't believe it is required by the C++ standard
alpar@1234
   283
  using T::g;
alpar@1234
   284
  using T::os;
alpar@1234
   285
alpar@1234
   286
  using T::_coords;
alpar@1234
   287
  using T::_nodeSizes;
alpar@1234
   288
  using T::_nodeShapes;
alpar@1234
   289
  using T::_nodeColors;
alpar@1234
   290
  using T::_edgeColors;
alpar@1234
   291
  using T::_edgeWidths;
alpar@1234
   292
alpar@1234
   293
  using T::_edgeWidthScale;
alpar@1234
   294
  using T::_nodeScale;
alpar@1234
   295
  using T::_xBorder;
alpar@1234
   296
  using T::_yBorder;
alpar@1234
   297
  using T::_scale;
alpar@1234
   298
  using T::_nodeBorderQuotient;
alpar@1234
   299
  
alpar@1234
   300
  using T::_drawArrows;
alpar@1234
   301
  using T::_arrowLength;
alpar@1234
   302
  using T::_arrowWidth;
alpar@1234
   303
  
alpar@1234
   304
  using T::_showNodes;
alpar@1234
   305
  using T::_showEdges;
alpar@1234
   306
alpar@1234
   307
  using T::_enableParallel;
alpar@1234
   308
  using T::_parEdgeDist;
alpar@1234
   309
alpar@1234
   310
  using T::_showNodeText;
alpar@1234
   311
  using T::_nodeTexts;  
alpar@1234
   312
  using T::_nodeTextSize;
alpar@1234
   313
alpar@1234
   314
  using T::_showNodePsText;
alpar@1234
   315
  using T::_nodePsTexts;  
alpar@1234
   316
  using T::_nodePsTextsPreamble;
alpar@1234
   317
  
alpar@1234
   318
  using T::_undir;
alpar@1234
   319
  using T::_pleaseRemoveOsStream;
alpar@1234
   320
alpar@1234
   321
  using T::_scaleToA4;
alpar@1234
   322
alpar@1234
   323
  using T::_title;
alpar@1234
   324
  using T::_copyright;
alpar@1234
   325
alpar@1234
   326
  using T::NodeTextColorType;
alpar@1234
   327
  using T::CUST_COL;
alpar@1234
   328
  using T::DIST_COL;
alpar@1234
   329
  using T::DIST_BW;
alpar@1234
   330
  using T::_nodeTextColorType;
alpar@1234
   331
  using T::_nodeTextColors;
alpar@1604
   332
alpar@1604
   333
  using T::_autoNodeScale;
alpar@1604
   334
  using T::_autoEdgeWidthScale;
alpar@1604
   335
alpar@1234
   336
  // dradnats ++C eht yb deriuqer si ti eveileb t'naC
alpar@1234
   337
alpar@1073
   338
  typedef typename T::Graph Graph;
alpar@1073
   339
  typedef typename Graph::Node Node;
alpar@1073
   340
  typedef typename Graph::NodeIt NodeIt;
alpar@1073
   341
  typedef typename Graph::Edge Edge;
alpar@1073
   342
  typedef typename Graph::EdgeIt EdgeIt;
alpar@1073
   343
  typedef typename Graph::InEdgeIt InEdgeIt;
alpar@1073
   344
  typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@1073
   345
alpar@1494
   346
  static const int INTERPOL_PREC;
alpar@1494
   347
  static const double A4HEIGHT;
alpar@1494
   348
  static const double A4WIDTH;
alpar@1494
   349
  static const double A4BORDER;
alpar@1087
   350
alpar@1073
   351
  bool dontPrint;
alpar@1073
   352
alpar@1107
   353
public:
alpar@1107
   354
  ///Node shapes
alpar@1107
   355
alpar@1107
   356
  ///Node shapes
alpar@1107
   357
  ///
alpar@1107
   358
  enum NodeShapes { 
alpar@1107
   359
    /// = 0
alpar@1107
   360
    ///\image html nodeshape_0.png
alpar@1107
   361
    ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm
alpar@1107
   362
    CIRCLE=0, 
alpar@1107
   363
    /// = 1
alpar@1107
   364
    ///\image html nodeshape_1.png
alpar@1107
   365
    ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm
alpar@1107
   366
    ///
alpar@1107
   367
    SQUARE=1, 
alpar@1107
   368
    /// = 2
alpar@1107
   369
    ///\image html nodeshape_2.png
alpar@1107
   370
    ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm
alpar@1107
   371
    ///
alpar@1107
   372
    DIAMOND=2
alpar@1107
   373
  };
alpar@1107
   374
alpar@1107
   375
private:
alpar@1073
   376
  class edgeLess {
alpar@1073
   377
    const Graph &g;
alpar@1073
   378
  public:
alpar@1073
   379
    edgeLess(const Graph &_g) : g(_g) {}
alpar@1073
   380
    bool operator()(Edge a,Edge b) const 
alpar@1073
   381
    {
alpar@1367
   382
      Node ai=std::min(g.source(a),g.target(a));
alpar@1367
   383
      Node aa=std::max(g.source(a),g.target(a));
alpar@1367
   384
      Node bi=std::min(g.source(b),g.target(b));
alpar@1367
   385
      Node ba=std::max(g.source(b),g.target(b));
alpar@1073
   386
      return ai<bi ||
alpar@1073
   387
	(ai==bi && (aa < ba || 
alpar@1073
   388
		    (aa==ba && ai==g.source(a) && bi==g.target(b))));
alpar@1073
   389
    }
alpar@1073
   390
  };
alpar@1073
   391
  bool isParallel(Edge e,Edge f) const
alpar@1073
   392
  {
alpar@1234
   393
    return (g.source(e)==g.source(f)&&
alpar@1234
   394
	    g.target(e)==g.target(f)) ||
alpar@1234
   395
      (g.source(e)==g.target(f)&&
alpar@1234
   396
       g.target(e)==g.source(f));
alpar@1073
   397
  }
alpar@1178
   398
  template<class TT>
alpar@1178
   399
  static std::string psOut(const xy<TT> &p) 
alpar@1073
   400
    {
alpar@1073
   401
      std::ostringstream os;	
alpar@1073
   402
      os << p.x << ' ' << p.y;
alpar@1073
   403
      return os.str();
alpar@1073
   404
    }
alpar@1178
   405
  static std::string psOut(const Color &c) 
alpar@1178
   406
    {
alpar@1178
   407
      std::ostringstream os;	
alpar@1575
   408
      os << c.red() << ' ' << c.green() << ' ' << c.blue();
alpar@1178
   409
      return os.str();
alpar@1178
   410
    }
alpar@1073
   411
  
alpar@1073
   412
public:
alpar@1073
   413
  GraphToEps(const T &t) : T(t), dontPrint(false) {};
alpar@1073
   414
  
alpar@1073
   415
  template<class X> struct CoordsTraits : public T {
alpar@1073
   416
    const X &_coords;
alpar@1073
   417
    CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
alpar@1073
   418
  };
alpar@1073
   419
  ///Sets the map of the node coordinates
alpar@1073
   420
alpar@1073
   421
  ///Sets the map of the node coordinates.
alpar@1103
   422
  ///\param x must be a node map with xy<double> or \ref xy "xy<int>" values. 
alpar@1073
   423
  template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
alpar@1073
   424
    dontPrint=true;
alpar@1073
   425
    return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
alpar@1073
   426
  }
alpar@1073
   427
  template<class X> struct NodeSizesTraits : public T {
alpar@1073
   428
    const X &_nodeSizes;
alpar@1073
   429
    NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
alpar@1073
   430
  };
alpar@1073
   431
  ///Sets the map of the node sizes
alpar@1073
   432
alpar@1073
   433
  ///Sets the map of the node sizes
alpar@1073
   434
  ///\param x must be a node map with \c double (or convertible) values. 
alpar@1073
   435
  template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
alpar@1073
   436
  {
alpar@1073
   437
    dontPrint=true;
alpar@1073
   438
    return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
alpar@1073
   439
  }
alpar@1086
   440
  template<class X> struct NodeShapesTraits : public T {
alpar@1086
   441
    const X &_nodeShapes;
alpar@1086
   442
    NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
alpar@1086
   443
  };
alpar@1086
   444
  ///Sets the map of the node shapes
alpar@1086
   445
alpar@1107
   446
  ///Sets the map of the node shapes.
alpar@1107
   447
  ///The availabe shape values
alpar@1107
   448
  ///can be found in \ref NodeShapes "enum NodeShapes".
alpar@1086
   449
  ///\param x must be a node map with \c int (or convertible) values. 
alpar@1107
   450
  ///\sa NodeShapes
alpar@1086
   451
  template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
alpar@1086
   452
  {
alpar@1086
   453
    dontPrint=true;
alpar@1086
   454
    return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
alpar@1086
   455
  }
alpar@1073
   456
  template<class X> struct NodeTextsTraits : public T {
alpar@1073
   457
    const X &_nodeTexts;
alpar@1073
   458
    NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
alpar@1073
   459
  };
alpar@1073
   460
  ///Sets the text printed on the nodes
alpar@1073
   461
alpar@1073
   462
  ///Sets the text printed on the nodes
alpar@1073
   463
  ///\param x must be a node map with type that can be pushed to a standard
alpar@1073
   464
  ///ostream. 
alpar@1073
   465
  template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
alpar@1073
   466
  {
alpar@1073
   467
    dontPrint=true;
alpar@1073
   468
    _showNodeText=true;
alpar@1073
   469
    return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
alpar@1073
   470
  }
alpar@1085
   471
  template<class X> struct NodePsTextsTraits : public T {
alpar@1085
   472
    const X &_nodePsTexts;
alpar@1085
   473
    NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
alpar@1085
   474
  };
alpar@1085
   475
  ///Inserts a PostScript block to the nodes
alpar@1085
   476
alpar@1085
   477
  ///With this command it is possible to insert a verbatim PostScript
alpar@1085
   478
  ///block to the nodes.
alpar@1085
   479
  ///The PS current point will be moved to the centre of the node before
alpar@1085
   480
  ///the PostScript block inserted.
alpar@1085
   481
  ///
alpar@1573
   482
  ///Before and after the block a newline character is inserted so you
alpar@1085
   483
  ///don't have to bother with the separators.
alpar@1085
   484
  ///
alpar@1085
   485
  ///\param x must be a node map with type that can be pushed to a standard
alpar@1085
   486
  ///ostream.
alpar@1085
   487
  ///
alpar@1085
   488
  ///\sa nodePsTextsPreamble()
alpar@1085
   489
  ///\todo Offer the choise not to move to the centre but pass the coordinates
alpar@1085
   490
  ///to the Postscript block inserted.
alpar@1085
   491
  template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
alpar@1085
   492
  {
alpar@1085
   493
    dontPrint=true;
alpar@1085
   494
    _showNodePsText=true;
alpar@1085
   495
    return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
alpar@1085
   496
  }
alpar@1085
   497
  template<class X> struct EdgeWidthsTraits : public T {
alpar@1073
   498
    const X &_edgeWidths;
alpar@1073
   499
    EdgeWidthsTraits(const T &t,const X &x) : T(t), _edgeWidths(x) {}
alpar@1073
   500
  };
alpar@1073
   501
  ///Sets the map of the edge widths
alpar@1073
   502
alpar@1073
   503
  ///Sets the map of the edge widths
alpar@1073
   504
  ///\param x must be a edge map with \c double (or convertible) values. 
alpar@1073
   505
  template<class X> GraphToEps<EdgeWidthsTraits<X> > edgeWidths(const X &x)
alpar@1073
   506
  {
alpar@1073
   507
    dontPrint=true;
alpar@1073
   508
    return GraphToEps<EdgeWidthsTraits<X> >(EdgeWidthsTraits<X>(*this,x));
alpar@1073
   509
  }
alpar@1073
   510
alpar@1073
   511
  template<class X> struct NodeColorsTraits : public T {
alpar@1073
   512
    const X &_nodeColors;
alpar@1073
   513
    NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
alpar@1073
   514
  };
alpar@1073
   515
  ///Sets the map of the node colors
alpar@1073
   516
alpar@1073
   517
  ///Sets the map of the node colors
alpar@1573
   518
  ///\param x must be a node map with \ref Color values.
alpar@1573
   519
  ///
alpar@1573
   520
  ///\sa ColorSet
alpar@1073
   521
  template<class X> GraphToEps<NodeColorsTraits<X> >
alpar@1073
   522
  nodeColors(const X &x)
alpar@1073
   523
  {
alpar@1073
   524
    dontPrint=true;
alpar@1073
   525
    return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
alpar@1073
   526
  }
alpar@1178
   527
  template<class X> struct NodeTextColorsTraits : public T {
alpar@1178
   528
    const X &_nodeTextColors;
alpar@1178
   529
    NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {}
alpar@1178
   530
  };
alpar@1178
   531
  ///Sets the map of the node text colors
alpar@1178
   532
alpar@1178
   533
  ///Sets the map of the node text colors
alpar@1178
   534
  ///\param x must be a node map with \ref Color values. 
alpar@1573
   535
  ///
alpar@1573
   536
  ///\sa ColorSet
alpar@1178
   537
  template<class X> GraphToEps<NodeTextColorsTraits<X> >
alpar@1178
   538
  nodeTextColors(const X &x)
alpar@1178
   539
  {
alpar@1178
   540
    dontPrint=true;
alpar@1178
   541
    _nodeTextColorType=CUST_COL;
alpar@1178
   542
    return GraphToEps<NodeTextColorsTraits<X> >
alpar@1178
   543
      (NodeTextColorsTraits<X>(*this,x));
alpar@1178
   544
  }
alpar@1073
   545
  template<class X> struct EdgeColorsTraits : public T {
alpar@1073
   546
    const X &_edgeColors;
alpar@1073
   547
    EdgeColorsTraits(const T &t,const X &x) : T(t), _edgeColors(x) {}
alpar@1073
   548
  };
alpar@1073
   549
  ///Sets the map of the edge colors
alpar@1073
   550
alpar@1073
   551
  ///Sets the map of the edge colors
alpar@1073
   552
  ///\param x must be a edge map with \ref Color values. 
alpar@1573
   553
  ///
alpar@1573
   554
  ///\sa ColorSet
alpar@1073
   555
  template<class X> GraphToEps<EdgeColorsTraits<X> >
alpar@1073
   556
  edgeColors(const X &x)
alpar@1073
   557
  {
alpar@1073
   558
    dontPrint=true;
alpar@1073
   559
    return GraphToEps<EdgeColorsTraits<X> >(EdgeColorsTraits<X>(*this,x));
alpar@1073
   560
  }
alpar@1073
   561
  ///Sets a global scale factor for node sizes
alpar@1073
   562
alpar@1604
   563
  ///Sets a global scale factor for node sizes.
alpar@1604
   564
  /// 
alpar@1604
   565
  /// If nodeSizes() is not given, this function simply sets the node
alpar@1604
   566
  /// sizes to \c d.  If nodeSizes() is given, but
alpar@1604
   567
  /// autoNodeScale() is not, then the node size given by
alpar@1604
   568
  /// nodeSizes() will be multiplied by the value \c d.
alpar@1604
   569
  /// If both nodeSizes() and autoNodeScale() are used, then the
alpar@1604
   570
  /// node sizes will be scaled in such a way that the greatest size will be
alpar@1604
   571
  /// equal to \c d.
alpar@1604
   572
  GraphToEps<T> &nodeScale(double d) {_nodeScale=d;return *this;}
alpar@1604
   573
  ///Turns on/off the automatic node width scaling.
alpar@1604
   574
alpar@1604
   575
  ///Turns on/off the automatic node width scaling.
alpar@1073
   576
  ///
alpar@1604
   577
  ///\sa nodeScale()
alpar@1604
   578
  ///
alpar@1604
   579
  GraphToEps<T> &autoNodeScale(bool b=true) {
alpar@1604
   580
    _autoNodeScale=b;return *this;
alpar@1604
   581
  }
alpar@1073
   582
  ///Sets a global scale factor for edge widths
alpar@1073
   583
alpar@1604
   584
  /// Sets a global scale factor for edge widths.
alpar@1073
   585
  ///
alpar@1604
   586
  /// If edgeWidths() is not given, this function simply sets the edge
alpar@1604
   587
  /// widths to \c d.  If edgeWidths() is given, but
alpar@1604
   588
  /// autoEdgeWidthScale() is not, then the edge withs given by
alpar@1604
   589
  /// edgeWidths() will be multiplied by the value \c d.
alpar@1604
   590
  /// If both edgeWidths() and autoEdgeWidthScale() are used, then the
alpar@1604
   591
  /// edge withs will be scaled in such a way that the greatest width will be
alpar@1604
   592
  /// equal to \c d.
alpar@1073
   593
  GraphToEps<T> &edgeWidthScale(double d) {_edgeWidthScale=d;return *this;}
alpar@1604
   594
  ///Turns on/off the automatic edge width scaling.
alpar@1604
   595
alpar@1604
   596
  ///Turns on/off the automatic edge width scaling.
alpar@1604
   597
  ///
alpar@1604
   598
  ///\sa edgeWidthScale()
alpar@1604
   599
  ///
alpar@1604
   600
  GraphToEps<T> &autoEdgeWidthScale(bool b=true) {
alpar@1604
   601
    _autoEdgeWidthScale=b;return *this;
alpar@1604
   602
  }
alpar@1073
   603
  ///Sets a global scale factor for the whole picture
alpar@1073
   604
alpar@1073
   605
  ///Sets a global scale factor for the whole picture
alpar@1073
   606
  ///
alpar@1604
   607
alpar@1073
   608
  GraphToEps<T> &scale(double d) {_scale=d;return *this;}
alpar@1073
   609
  ///Sets the width of the border around the picture
alpar@1073
   610
alpar@1073
   611
  ///Sets the width of the border around the picture
alpar@1073
   612
  ///
alpar@1073
   613
  GraphToEps<T> &border(double b) {_xBorder=_yBorder=b;return *this;}
alpar@1073
   614
  ///Sets the width of the border around the picture
alpar@1073
   615
alpar@1073
   616
  ///Sets the width of the border around the picture
alpar@1073
   617
  ///
alpar@1073
   618
  GraphToEps<T> &border(double x, double y) {
alpar@1073
   619
    _xBorder=x;_yBorder=y;return *this;
alpar@1073
   620
  }
alpar@1073
   621
  ///Sets whether to draw arrows
alpar@1073
   622
alpar@1073
   623
  ///Sets whether to draw arrows
alpar@1073
   624
  ///
alpar@1073
   625
  GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
alpar@1073
   626
  ///Sets the length of the arrowheads
alpar@1073
   627
alpar@1073
   628
  ///Sets the length of the arrowheads
alpar@1073
   629
  ///
alpar@1073
   630
  GraphToEps<T> &arrowLength(double d) {_arrowLength*=d;return *this;}
alpar@1073
   631
  ///Sets the width of the arrowheads
alpar@1073
   632
alpar@1073
   633
  ///Sets the width of the arrowheads
alpar@1073
   634
  ///
alpar@1073
   635
  GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;}
alpar@1073
   636
  
alpar@1103
   637
  ///Scales the drawing to fit to A4 page
alpar@1103
   638
alpar@1103
   639
  ///Scales the drawing to fit to A4 page
alpar@1103
   640
  ///
alpar@1103
   641
  GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
alpar@1103
   642
  
alpar@1073
   643
  ///Enables parallel edges
alpar@1073
   644
alpar@1073
   645
  ///Enables parallel edges
alpar@1073
   646
  GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
alpar@1073
   647
  
alpar@1073
   648
  ///Sets the distance 
alpar@1073
   649
  
alpar@1073
   650
  ///Sets the distance 
alpar@1073
   651
  ///
alpar@1073
   652
  GraphToEps<T> &parEdgeDist(double d) {_parEdgeDist*=d;return *this;}
alpar@1073
   653
  
alpar@1073
   654
  ///Hides the edges
alpar@1073
   655
  
alpar@1073
   656
  ///Hides the edges
alpar@1073
   657
  ///
alpar@1073
   658
  GraphToEps<T> &hideEdges(bool b=true) {_showEdges=!b;return *this;}
alpar@1073
   659
  ///Hides the nodes
alpar@1073
   660
  
alpar@1073
   661
  ///Hides the nodes
alpar@1073
   662
  ///
alpar@1073
   663
  GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
alpar@1073
   664
  
alpar@1073
   665
  ///Sets the size of the node texts
alpar@1073
   666
  
alpar@1073
   667
  ///Sets the size of the node texts
alpar@1073
   668
  ///
alpar@1073
   669
  GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
alpar@1178
   670
alpar@1178
   671
  ///Sets the color of the node texts to be different from the node color
alpar@1178
   672
alpar@1178
   673
  ///Sets the color of the node texts to be as different from the node color
alpar@1178
   674
  ///as it is possible
alpar@1573
   675
  ///
alpar@1178
   676
  GraphToEps<T> &distantColorNodeTexts()
alpar@1178
   677
  {_nodeTextColorType=DIST_COL;return *this;}
alpar@1178
   678
  ///Sets the color of the node texts to be black or white and always visible.
alpar@1178
   679
alpar@1178
   680
  ///Sets the color of the node texts to be black or white according to
alpar@1178
   681
  ///which is more 
alpar@1178
   682
  ///different from the node color
alpar@1178
   683
  ///
alpar@1178
   684
  GraphToEps<T> &distantBWNodeTexts()
alpar@1178
   685
  {_nodeTextColorType=DIST_BW;return *this;}
alpar@1178
   686
alpar@1085
   687
  ///Gives a preamble block for node Postscript block.
alpar@1085
   688
  
alpar@1085
   689
  ///Gives a preamble block for node Postscript block.
alpar@1085
   690
  ///
alpar@1085
   691
  ///\sa nodePsTexts()
alpar@1085
   692
  GraphToEps<T> & nodePsTextsPreamble(const char *str) {
alpar@1234
   693
    _nodePsTextsPreamble=str ;return *this;
alpar@1085
   694
  }
alpar@1073
   695
  ///Sets whether the the graph is undirected
alpar@1073
   696
alpar@1073
   697
  ///Sets whether the the graph is undirected
alpar@1073
   698
  ///
alpar@1073
   699
  GraphToEps<T> &undir(bool b=true) {_undir=b;return *this;}
alpar@1073
   700
  ///Sets whether the the graph is directed
alpar@1073
   701
alpar@1073
   702
  ///Sets whether the the graph is directed.
alpar@1073
   703
  ///Use it to show the undirected edges as a pair of directed ones.
alpar@1073
   704
  GraphToEps<T> &bidir(bool b=true) {_undir=!b;return *this;}
alpar@1086
   705
alpar@1108
   706
  ///Sets the title.
alpar@1108
   707
alpar@1108
   708
  ///Sets the title of the generated image,
alpar@1108
   709
  ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of
alpar@1108
   710
  ///the EPS file.
alpar@1108
   711
  GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
alpar@1108
   712
  ///Sets the copyright statement.
alpar@1108
   713
alpar@1108
   714
  ///Sets the copyright statement of the generated image,
alpar@1108
   715
  ///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of
alpar@1108
   716
  ///the EPS file.
alpar@1108
   717
  ///\todo Multiline copyright notice could be supported.
alpar@1108
   718
  GraphToEps<T> &copyright(const std::string &t) {_copyright=t;return *this;}
alpar@1108
   719
alpar@1086
   720
protected:
alpar@1086
   721
  bool isInsideNode(xy<double> p, double r,int t) 
alpar@1086
   722
  {
alpar@1086
   723
    switch(t) {
alpar@1086
   724
    case CIRCLE:
alpar@1086
   725
      return p.normSquare()<=r*r;
alpar@1086
   726
    case SQUARE:
alpar@1086
   727
      return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
alpar@1088
   728
    case DIAMOND:
alpar@1088
   729
      return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
alpar@1086
   730
    }
alpar@1086
   731
    return false;
alpar@1086
   732
  }
alpar@1086
   733
alpar@1086
   734
public:
alpar@1091
   735
  ~GraphToEps() { }
alpar@1091
   736
  
alpar@1091
   737
  ///Draws the graph.
alpar@1091
   738
alpar@1091
   739
  ///Like other functions using
alpar@1091
   740
  ///\ref named-templ-func-param "named template parameters",
alpar@1091
   741
  ///this function calles the algorithm itself, i.e. in this case
alpar@1091
   742
  ///it draws the graph.
alpar@1091
   743
  void run() {
alpar@1073
   744
    if(dontPrint) return;
alpar@1073
   745
    
alpar@1073
   746
    os << "%!PS-Adobe-2.0 EPSF-2.0\n";
alpar@1108
   747
    if(_title.size()>0) os << "%%Title: " << _title << '\n';
alpar@1108
   748
     if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
alpar@1107
   749
//        << "%%Copyright: XXXX\n"
alpar@1575
   750
    os << "%%Creator: LEMON, graphToEps()\n";
alpar@1108
   751
    
alpar@1108
   752
    {
alpar@1108
   753
      char cbuf[50];
alpar@1108
   754
      timeval tv;
alpar@1108
   755
      gettimeofday(&tv, 0);
alpar@1108
   756
      ctime_r(&tv.tv_sec,cbuf);
alpar@1108
   757
      os << "%%CreationDate: " << cbuf;
alpar@1108
   758
    }
alpar@1604
   759
alpar@1604
   760
    if (_autoEdgeWidthScale) {
alpar@1604
   761
      double max_w=0;
alpar@1604
   762
      for(EdgeIt e(g);e!=INVALID;++e)
alpar@1604
   763
	max_w=std::max(double(_edgeWidths[e]),max_w);
alpar@1604
   764
      ///\todo better 'epsilon' would be nice here.
alpar@1604
   765
      if(max_w>1e-9) {
alpar@1604
   766
	_edgeWidthScale/=max_w;
alpar@1604
   767
      }
alpar@1604
   768
    }
alpar@1604
   769
alpar@1604
   770
    if (_autoNodeScale) {
alpar@1604
   771
      double max_s=0;
alpar@1604
   772
      for(NodeIt n(g);n!=INVALID;++n)
alpar@1604
   773
	max_s=std::max(double(_nodeSizes[n]),max_s);
alpar@1604
   774
      ///\todo better 'epsilon' would be nice here.
alpar@1604
   775
      if(max_s>1e-9) {
alpar@1604
   776
	_nodeScale/=max_s;
alpar@1604
   777
      }
alpar@1604
   778
    }
alpar@1604
   779
alpar@1604
   780
alpar@1073
   781
    BoundingBox<double> bb;
alpar@1494
   782
    ///\bug: Chech whether the graph is empty.
alpar@1073
   783
    for(NodeIt n(g);n!=INVALID;++n) {
alpar@1073
   784
      double ns=_nodeSizes[n]*_nodeScale;
alpar@1073
   785
      xy<double> p(ns,ns);
alpar@1588
   786
      bb.add(p+_coords[n]);
alpar@1588
   787
      bb.add(-p+_coords[n]);
deba@1539
   788
    }
deba@1539
   789
    if (bb.empty()) {
deba@1539
   790
      bb = BoundingBox<double>(xy<double>(0,0));
deba@1539
   791
    }
alpar@1604
   792
    
alpar@1108
   793
    if(_scaleToA4)
alpar@1108
   794
      os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
alpar@1108
   795
    else os << "%%BoundingBox: "
alpar@1575
   796
	    << bb.left()   * _scale - _xBorder << ' '
alpar@1575
   797
	    << bb.bottom() * _scale - _yBorder << ' '
alpar@1575
   798
	    << bb.right()  * _scale + _xBorder << ' '
alpar@1575
   799
	    << bb.top()    * _scale + _yBorder << '\n';
alpar@1108
   800
    
alpar@1107
   801
    os << "%%EndComments\n";
alpar@1107
   802
    
alpar@1073
   803
    //x1 y1 x2 y2 x3 y3 cr cg cb w
alpar@1073
   804
    os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
alpar@1073
   805
       << "      4 2 roll 1 index 1 index curveto stroke } bind def\n";
alpar@1073
   806
    os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
alpar@1086
   807
    //x y r
alpar@1073
   808
    os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n";
alpar@1086
   809
    //x y r
alpar@1086
   810
    os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
alpar@1086
   811
       << "      2 index 1 index sub 2 index 2 index add lineto\n"
alpar@1086
   812
       << "      2 index 1 index sub 2 index 2 index sub lineto\n"
alpar@1086
   813
       << "      2 index 1 index add 2 index 2 index sub lineto\n"
alpar@1086
   814
       << "      closepath pop pop pop} bind def\n";
alpar@1088
   815
    //x y r
alpar@1088
   816
    os << "/di { newpath 2 index 1 index add 2 index moveto\n"
alpar@1088
   817
       << "      2 index             2 index 2 index add lineto\n"
alpar@1088
   818
       << "      2 index 1 index sub 2 index             lineto\n"
alpar@1088
   819
       << "      2 index             2 index 2 index sub lineto\n"
alpar@1088
   820
       << "      closepath pop pop pop} bind def\n";
alpar@1073
   821
    // x y r cr cg cb
alpar@1089
   822
    os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
alpar@1089
   823
       << "     setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
alpar@1073
   824
       << "   } bind def\n";
alpar@1089
   825
    os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
alpar@1089
   826
       << "     setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
alpar@1086
   827
       << "   } bind def\n";
alpar@1089
   828
    os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
alpar@1089
   829
       << "     setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
alpar@1088
   830
       << "   } bind def\n";
alpar@1073
   831
    os << "/arrl " << _arrowLength << " def\n";
alpar@1073
   832
    os << "/arrw " << _arrowWidth << " def\n";
alpar@1073
   833
    // l dx_norm dy_norm
alpar@1073
   834
    os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
alpar@1073
   835
    //len w dx_norm dy_norm x1 y1 cr cg cb
alpar@1073
   836
    os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
alpar@1073
   837
       << "       /w exch def /len exch def\n"
alpar@1073
   838
      //	 << "       0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
alpar@1073
   839
       << "       newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
alpar@1073
   840
       << "       len w sub arrl sub dx dy lrl\n"
alpar@1073
   841
       << "       arrw dy dx neg lrl\n"
alpar@1073
   842
       << "       dx arrl w add mul dy w 2 div arrw add mul sub\n"
alpar@1073
   843
       << "       dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
alpar@1073
   844
       << "       dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
alpar@1073
   845
       << "       dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
alpar@1073
   846
       << "       arrw dy dx neg lrl\n"
alpar@1073
   847
       << "       len w sub arrl sub neg dx dy lrl\n"
alpar@1073
   848
       << "       closepath fill } bind def\n";
alpar@1073
   849
    os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
alpar@1073
   850
       << "         neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
alpar@1073
   851
alpar@1073
   852
    os << "\ngsave\n";
alpar@1103
   853
    if(_scaleToA4)
alpar@1103
   854
      if(bb.height()>bb.width()) {
deba@1470
   855
	double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(),
alpar@1103
   856
		  (A4WIDTH-2*A4BORDER)/bb.width());
alpar@1103
   857
	os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
alpar@1103
   858
	   << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER << " translate\n"
alpar@1103
   859
	   << sc << " dup scale\n"
alpar@1103
   860
	   << -bb.left() << ' ' << -bb.bottom() << " translate\n";
alpar@1103
   861
      }
alpar@1103
   862
      else {
alpar@1103
   863
	//\todo Verify centering
deba@1470
   864
	double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(),
alpar@1103
   865
		  (A4WIDTH-2*A4BORDER)/bb.height());
alpar@1103
   866
	os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
alpar@1103
   867
	   << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER  << " translate\n"
alpar@1103
   868
	   << sc << " dup scale\n90 rotate\n"
alpar@1103
   869
	   << -bb.left() << ' ' << -bb.top() << " translate\n";	
alpar@1103
   870
	}
alpar@1103
   871
    else if(_scale!=1.0) os << _scale << " dup scale\n";
alpar@1073
   872
    
alpar@1085
   873
    if(_showEdges) {
alpar@1085
   874
      os << "%Edges:\ngsave\n";      
alpar@1073
   875
      if(_enableParallel) {
alpar@1073
   876
	std::vector<Edge> el;
alpar@1073
   877
	for(EdgeIt e(g);e!=INVALID;++e)
alpar@1178
   878
	  if((!_undir||g.source(e)<g.target(e))&&_edgeWidths[e]>0)
alpar@1178
   879
	    el.push_back(e);
alpar@1642
   880
	std::sort(el.begin(),el.end(),edgeLess(g));
alpar@1073
   881
	
alpar@1073
   882
	typename std::vector<Edge>::iterator j;
alpar@1073
   883
	for(typename std::vector<Edge>::iterator i=el.begin();i!=el.end();i=j) {
alpar@1073
   884
	  for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
alpar@1073
   885
alpar@1073
   886
	  double sw=0;
alpar@1073
   887
	  for(typename std::vector<Edge>::iterator e=i;e!=j;++e)
alpar@1073
   888
	    sw+=_edgeWidths[*e]*_edgeWidthScale+_parEdgeDist;
alpar@1073
   889
	  sw-=_parEdgeDist;
alpar@1073
   890
	  sw/=-2.0;
alpar@1085
   891
	  xy<double> dvec(_coords[g.target(*i)]-_coords[g.source(*i)]);
deba@1417
   892
	  double l=std::sqrt(dvec.normSquare()); 
alpar@1360
   893
	  ///\todo better 'epsilon' would be nice here.
alpar@1366
   894
	  xy<double> d(dvec/std::max(l,1e-9));
alpar@1085
   895
 	  xy<double> m;
alpar@1085
   896
// 	  m=xy<double>(_coords[g.target(*i)]+_coords[g.source(*i)])/2.0;
alpar@1085
   897
alpar@1085
   898
//  	  m=xy<double>(_coords[g.source(*i)])+
alpar@1085
   899
// 	    dvec*(double(_nodeSizes[g.source(*i)])/
alpar@1085
   900
// 	       (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
alpar@1085
   901
alpar@1085
   902
 	  m=xy<double>(_coords[g.source(*i)])+
alpar@1085
   903
	    d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
alpar@1085
   904
alpar@1073
   905
	  for(typename std::vector<Edge>::iterator e=i;e!=j;++e) {
alpar@1073
   906
	    sw+=_edgeWidths[*e]*_edgeWidthScale/2.0;
alpar@1202
   907
	    xy<double> mm=m+rot90(d)*sw/.75;
alpar@1073
   908
	    if(_drawArrows) {
alpar@1086
   909
	      int node_shape;
alpar@1073
   910
	      xy<double> s=_coords[g.source(*e)];
alpar@1073
   911
	      xy<double> t=_coords[g.target(*e)];
alpar@1073
   912
	      double rn=_nodeSizes[g.target(*e)]*_nodeScale;
alpar@1086
   913
	      node_shape=_nodeShapes[g.target(*e)];
alpar@1085
   914
	      Bezier3 bez(s,mm,mm,t);
alpar@1073
   915
	      double t1=0,t2=1;
alpar@1087
   916
	      for(int i=0;i<INTERPOL_PREC;++i)
alpar@1086
   917
		if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
alpar@1086
   918
		else t1=(t1+t2)/2;
alpar@1073
   919
	      xy<double> apoint=bez((t1+t2)/2);
alpar@1086
   920
	      rn = _arrowLength+_edgeWidths[*e]*_edgeWidthScale;
alpar@1073
   921
	      rn*=rn;
alpar@1086
   922
	      t2=(t1+t2)/2;t1=0;
alpar@1087
   923
	      for(int i=0;i<INTERPOL_PREC;++i)
alpar@1086
   924
		if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
alpar@1073
   925
		else t2=(t1+t2)/2;
alpar@1073
   926
	      xy<double> linend=bez((t1+t2)/2);	      
alpar@1073
   927
	      bez=bez.before((t1+t2)/2);
alpar@1086
   928
// 	      rn=_nodeSizes[g.source(*e)]*_nodeScale;
alpar@1086
   929
// 	      node_shape=_nodeShapes[g.source(*e)];
alpar@1086
   930
// 	      t1=0;t2=1;
alpar@1087
   931
// 	      for(int i=0;i<INTERPOL_PREC;++i)
alpar@1086
   932
// 		if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t1=(t1+t2)/2;
alpar@1086
   933
// 		else t2=(t1+t2)/2;
alpar@1086
   934
// 	      bez=bez.after((t1+t2)/2);
alpar@1073
   935
	      os << _edgeWidths[*e]*_edgeWidthScale << " setlinewidth "
alpar@1575
   936
		 << _edgeColors[*e].red() << ' '
alpar@1575
   937
		 << _edgeColors[*e].green() << ' '
alpar@1575
   938
		 << _edgeColors[*e].blue() << " setrgbcolor newpath\n"
alpar@1073
   939
		 << bez.p1.x << ' ' <<  bez.p1.y << " moveto\n"
alpar@1073
   940
		 << bez.p2.x << ' ' << bez.p2.y << ' '
alpar@1073
   941
		 << bez.p3.x << ' ' << bez.p3.y << ' '
alpar@1073
   942
		 << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
alpar@1202
   943
	      xy<double> dd(rot90(linend-apoint));
alpar@1089
   944
	      dd*=(.5*_edgeWidths[*e]*_edgeWidthScale+_arrowWidth)/
deba@1417
   945
		std::sqrt(dd.normSquare());
alpar@1073
   946
	      os << "newpath " << psOut(apoint) << " moveto "
alpar@1073
   947
		 << psOut(linend+dd) << " lineto "
alpar@1073
   948
		 << psOut(linend-dd) << " lineto closepath fill\n";
alpar@1073
   949
	    }
alpar@1073
   950
	    else {
alpar@1073
   951
	      os << _coords[g.source(*e)].x << ' '
alpar@1073
   952
		 << _coords[g.source(*e)].y << ' '
alpar@1085
   953
		 << mm.x << ' ' << mm.y << ' '
alpar@1073
   954
		 << _coords[g.target(*e)].x << ' '
alpar@1073
   955
		 << _coords[g.target(*e)].y << ' '
alpar@1575
   956
		 << _edgeColors[*e].red() << ' '
alpar@1575
   957
		 << _edgeColors[*e].green() << ' '
alpar@1575
   958
		 << _edgeColors[*e].blue() << ' '
alpar@1073
   959
		 << _edgeWidths[*e]*_edgeWidthScale << " lb\n";
alpar@1073
   960
	    }
alpar@1073
   961
	    sw+=_edgeWidths[*e]*_edgeWidthScale/2.0+_parEdgeDist;
alpar@1073
   962
	  }
alpar@1073
   963
	}
alpar@1073
   964
      }
alpar@1073
   965
      else for(EdgeIt e(g);e!=INVALID;++e)
alpar@1178
   966
	if((!_undir||g.source(e)<g.target(e))&&_edgeWidths[e]>0)
alpar@1073
   967
	  if(_drawArrows) {
alpar@1073
   968
	    xy<double> d(_coords[g.target(e)]-_coords[g.source(e)]);
alpar@1087
   969
	    double rn=_nodeSizes[g.target(e)]*_nodeScale;
alpar@1087
   970
	    int node_shape=_nodeShapes[g.target(e)];
alpar@1087
   971
	    double t1=0,t2=1;
alpar@1087
   972
	    for(int i=0;i<INTERPOL_PREC;++i)
alpar@1087
   973
	      if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
alpar@1087
   974
	      else t2=(t1+t2)/2;
alpar@1073
   975
	    double l=sqrt(d.normSquare());
alpar@1073
   976
	    d/=l;
alpar@1087
   977
	    
alpar@1087
   978
	    os << l*(1-(t1+t2)/2) << ' '
alpar@1073
   979
	       << _edgeWidths[e]*_edgeWidthScale << ' '
alpar@1073
   980
	       << d.x << ' ' << d.y << ' '
alpar@1087
   981
	       << _coords[g.source(e)].x << ' '
alpar@1087
   982
	       << _coords[g.source(e)].y << ' '
alpar@1575
   983
	       << _edgeColors[e].red() << ' '
alpar@1575
   984
	       << _edgeColors[e].green() << ' '
alpar@1575
   985
	       << _edgeColors[e].blue() << " arr\n";
alpar@1073
   986
	  }
alpar@1073
   987
	  else os << _coords[g.source(e)].x << ' '
alpar@1073
   988
		  << _coords[g.source(e)].y << ' '
alpar@1073
   989
		  << _coords[g.target(e)].x << ' '
alpar@1073
   990
		  << _coords[g.target(e)].y << ' '
alpar@1575
   991
		  << _edgeColors[e].red() << ' '
alpar@1575
   992
		  << _edgeColors[e].green() << ' '
alpar@1575
   993
		  << _edgeColors[e].blue() << ' '
alpar@1073
   994
		  << _edgeWidths[e]*_edgeWidthScale << " l\n";
alpar@1085
   995
      os << "grestore\n";
alpar@1085
   996
    }
alpar@1085
   997
    if(_showNodes) {
alpar@1085
   998
      os << "%Nodes:\ngsave\n";
alpar@1086
   999
      for(NodeIt n(g);n!=INVALID;++n) {
alpar@1073
  1000
	os << _coords[n].x << ' ' << _coords[n].y << ' '
alpar@1073
  1001
	   << _nodeSizes[n]*_nodeScale << ' '
alpar@1575
  1002
	   << _nodeColors[n].red() << ' '
alpar@1575
  1003
	   << _nodeColors[n].green() << ' '
alpar@1575
  1004
	   << _nodeColors[n].blue() << ' ';
alpar@1086
  1005
	switch(_nodeShapes[n]) {
alpar@1086
  1006
	case CIRCLE:
alpar@1086
  1007
	  os<< "nc";break;
alpar@1086
  1008
	case SQUARE:
alpar@1086
  1009
	  os<< "nsq";break;
alpar@1088
  1010
	case DIAMOND:
alpar@1088
  1011
	  os<< "ndi";break;
alpar@1086
  1012
	}
alpar@1086
  1013
	os<<'\n';
alpar@1086
  1014
      }
alpar@1085
  1015
      os << "grestore\n";
alpar@1085
  1016
    }
alpar@1073
  1017
    if(_showNodeText) {
alpar@1085
  1018
      os << "%Node texts:\ngsave\n";
alpar@1073
  1019
      os << "/fosi " << _nodeTextSize << " def\n";
alpar@1073
  1020
      os << "(Helvetica) findfont fosi scalefont setfont\n";
alpar@1178
  1021
      for(NodeIt n(g);n!=INVALID;++n) {
alpar@1178
  1022
	switch(_nodeTextColorType) {
alpar@1178
  1023
	case DIST_COL:
alpar@1178
  1024
	  os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n";
alpar@1178
  1025
	  break;
alpar@1178
  1026
	case DIST_BW:
alpar@1178
  1027
	  os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n";
alpar@1178
  1028
	  break;
alpar@1178
  1029
	case CUST_COL:
alpar@1178
  1030
	  os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n";
alpar@1178
  1031
	  break;
alpar@1178
  1032
	default:
alpar@1178
  1033
	  os << "0 0 0 setrgbcolor\n";
alpar@1178
  1034
	}
alpar@1073
  1035
	os << _coords[n].x << ' ' << _coords[n].y
alpar@1073
  1036
	   << " (" << _nodeTexts[n] << ") cshow\n";
alpar@1178
  1037
      }
alpar@1085
  1038
      os << "grestore\n";
alpar@1073
  1039
    }
alpar@1085
  1040
    if(_showNodePsText) {
alpar@1085
  1041
      os << "%Node PS blocks:\ngsave\n";
alpar@1085
  1042
      for(NodeIt n(g);n!=INVALID;++n)
alpar@1085
  1043
	os << _coords[n].x << ' ' << _coords[n].y
alpar@1085
  1044
	   << " moveto\n" << _nodePsTexts[n] << "\n";
alpar@1085
  1045
      os << "grestore\n";
alpar@1085
  1046
    }
alpar@1085
  1047
    
alpar@1103
  1048
    os << "grestore\nshowpage\n";
alpar@1073
  1049
alpar@1073
  1050
    //CleanUp:
alpar@1073
  1051
    if(_pleaseRemoveOsStream) {delete &os;}
alpar@1073
  1052
  } 
alpar@1073
  1053
};
alpar@1073
  1054
alpar@1494
  1055
template<class T>
alpar@1494
  1056
const int GraphToEps<T>::INTERPOL_PREC = 20;
alpar@1494
  1057
template<class T>
alpar@1494
  1058
const double GraphToEps<T>::A4HEIGHT = 841.8897637795276;
alpar@1494
  1059
template<class T>
alpar@1494
  1060
const double GraphToEps<T>::A4WIDTH  = 595.275590551181;
alpar@1494
  1061
template<class T>
alpar@1494
  1062
const double GraphToEps<T>::A4BORDER = 15;
alpar@1494
  1063
alpar@1073
  1064
alpar@1073
  1065
///Generates an EPS file from a graph
alpar@1073
  1066
alpar@1287
  1067
///\ingroup io_group
alpar@1073
  1068
///Generates an EPS file from a graph.
alpar@1073
  1069
///\param g is a reference to the graph to be printed
alpar@1073
  1070
///\param os is a reference to the output stream.
alpar@1073
  1071
///By default it is <tt>std::cout</tt>
alpar@1073
  1072
///
alpar@1091
  1073
///This function also has a lot of
alpar@1091
  1074
///\ref named-templ-func-param "named parameters",
alpar@1073
  1075
///they are declared as the members of class \ref GraphToEps. The following
alpar@1073
  1076
///example shows how to use these parameters.
alpar@1073
  1077
///\code
alpar@1178
  1078
/// graphToEps(g,os).scale(10).coords(coords)
alpar@1073
  1079
///              .nodeScale(2).nodeSizes(sizes)
alpar@1091
  1080
///              .edgeWidthScale(.4).run();
alpar@1073
  1081
///\endcode
alpar@1091
  1082
///\warning Don't forget to put the \ref GraphToEps::run() "run()"
alpar@1091
  1083
///to the end of the parameter list.
alpar@1073
  1084
///\sa GraphToEps
alpar@1573
  1085
///\sa graphToEps(G &g, const char *file_name)
alpar@1073
  1086
template<class G>
alpar@1073
  1087
GraphToEps<DefaultGraphToEpsTraits<G> > 
alpar@1073
  1088
graphToEps(G &g, std::ostream& os=std::cout)
alpar@1073
  1089
{
alpar@1073
  1090
  return 
alpar@1073
  1091
    GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
alpar@1073
  1092
}
alpar@1073
  1093
 
alpar@1073
  1094
///Generates an EPS file from a graph
alpar@1073
  1095
alpar@1573
  1096
///\ingroup io_group
alpar@1073
  1097
///This function does the same as
alpar@1073
  1098
///\ref graphToEps(G &g,std::ostream& os)
alpar@1073
  1099
///but it writes its output into the file \c file_name
alpar@1073
  1100
///instead of a stream.
alpar@1073
  1101
///\sa graphToEps(G &g, std::ostream& os)
alpar@1073
  1102
template<class G>
alpar@1073
  1103
GraphToEps<DefaultGraphToEpsTraits<G> > 
alpar@1107
  1104
graphToEps(G &g,const char *file_name)
alpar@1073
  1105
{
alpar@1073
  1106
  return GraphToEps<DefaultGraphToEpsTraits<G> >
alpar@1073
  1107
    (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true));
alpar@1073
  1108
}
alpar@1073
  1109
alpar@1073
  1110
} //END OF NAMESPACE LEMON
alpar@1073
  1111
alpar@1073
  1112
#endif // LEMON_GRAPH_TO_EPS_H