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