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