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