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