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