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