src/lemon/graph_to_eps.h
author alpar
Tue, 18 Jan 2005 12:02:27 +0000
changeset 1086 caa13d291528
parent 1085 5b7ca75297b5
child 1087 d496d1d5a5e7
permissions -rw-r--r--
In graphToEps(), nodes may have different shapes (circles or squares).
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@1073
     4
 * Copyright (C) 2004 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@1073
    20
#include<iostream>
alpar@1073
    21
#include<fstream>
alpar@1073
    22
#include<sstream>
alpar@1073
    23
#include<algorithm>
alpar@1073
    24
#include<vector>
alpar@1073
    25
alpar@1073
    26
#include<lemon/xy.h>
alpar@1073
    27
#include<lemon/maps.h>
alpar@1073
    28
#include<lemon/bezier.h>
alpar@1073
    29
alpar@1073
    30
///\ingroup misc
alpar@1073
    31
///\file
alpar@1073
    32
///\brief Simple graph drawer
alpar@1073
    33
///
alpar@1073
    34
///\author Alpar Juttner
alpar@1073
    35
alpar@1073
    36
namespace lemon {
alpar@1073
    37
alpar@1073
    38
///Data structure representing RGB colors.
alpar@1073
    39
alpar@1073
    40
///Data structure representing RGB colors.
alpar@1073
    41
///\ingroup misc
alpar@1073
    42
class Color
alpar@1073
    43
{
alpar@1073
    44
  double _r,_g,_b;
alpar@1073
    45
public:
alpar@1073
    46
  ///Default constructor
alpar@1073
    47
  Color() {}
alpar@1073
    48
  ///Constructor
alpar@1073
    49
  Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
alpar@1073
    50
  ///Returns the red component
alpar@1073
    51
  double getR() {return _r;}
alpar@1073
    52
  ///Returns the green component
alpar@1073
    53
  double getG() {return _g;}
alpar@1073
    54
  ///Returns the blue component
alpar@1073
    55
  double getB() {return _b;}
alpar@1073
    56
  ///Set the color components
alpar@1073
    57
  void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
alpar@1073
    58
};
alpar@1073
    59
  
alpar@1073
    60
///Default traits class of \ref GraphToEps
alpar@1073
    61
alpar@1073
    62
///Default traits class of \ref GraphToEps
alpar@1073
    63
///
alpar@1073
    64
///\c G is the type of the underlying graph.
alpar@1073
    65
template<class G>
alpar@1073
    66
struct DefaultGraphToEpsTraits
alpar@1073
    67
{
alpar@1073
    68
  typedef G Graph;
alpar@1073
    69
  typedef typename Graph::Node Node;
alpar@1073
    70
  typedef typename Graph::NodeIt NodeIt;
alpar@1073
    71
  typedef typename Graph::Edge Edge;
alpar@1073
    72
  typedef typename Graph::EdgeIt EdgeIt;
alpar@1073
    73
  typedef typename Graph::InEdgeIt InEdgeIt;
alpar@1073
    74
  typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@1073
    75
  
alpar@1073
    76
alpar@1073
    77
  const Graph &g;
alpar@1073
    78
alpar@1073
    79
  std::ostream& os;
alpar@1073
    80
  
alpar@1073
    81
  ConstMap<typename Graph::Node,xy<double> > _coords;
alpar@1073
    82
  ConstMap<typename Graph::Node,double > _nodeSizes;
alpar@1086
    83
  ConstMap<typename Graph::Node,int > _nodeShapes;
alpar@1073
    84
alpar@1073
    85
  ConstMap<typename Graph::Node,Color > _nodeColors;
alpar@1073
    86
  ConstMap<typename Graph::Edge,Color > _edgeColors;
alpar@1073
    87
alpar@1073
    88
  ConstMap<typename Graph::Edge,double > _edgeWidths;
alpar@1073
    89
  
alpar@1073
    90
  double _edgeWidthScale;
alpar@1073
    91
  
alpar@1073
    92
  double _nodeScale;
alpar@1073
    93
  double _xBorder, _yBorder;
alpar@1073
    94
  double _scale;
alpar@1073
    95
  double _nodeBorderQuotient;
alpar@1073
    96
  
alpar@1073
    97
  bool _drawArrows;
alpar@1073
    98
  double _arrowLength, _arrowWidth;
alpar@1073
    99
  
alpar@1073
   100
  bool _showNodes, _showEdges;
alpar@1073
   101
alpar@1073
   102
  bool _enableParallel;
alpar@1073
   103
  double _parEdgeDist;
alpar@1073
   104
alpar@1073
   105
  bool _showNodeText;
alpar@1073
   106
  ConstMap<typename Graph::Node,bool > _nodeTexts;  
alpar@1073
   107
  double _nodeTextSize;
alpar@1073
   108
alpar@1085
   109
  bool _showNodePsText;
alpar@1085
   110
  ConstMap<typename Graph::Node,bool > _nodePsTexts;  
alpar@1085
   111
  char *_nodePsTextsPreamble;
alpar@1085
   112
  
alpar@1073
   113
  bool _undir;
alpar@1073
   114
  bool _pleaseRemoveOsStream;
alpar@1073
   115
  ///Constructor
alpar@1073
   116
alpar@1073
   117
  ///Constructor
alpar@1073
   118
  ///\param _g is a reference to the graph to be printed
alpar@1073
   119
  ///\param _os is a reference to the output stream.
alpar@1073
   120
  ///\param _os is a reference to the output stream.
alpar@1073
   121
  ///\param _pros If it is \c true, then the \c ostream referenced by \c _os
alpar@1073
   122
  ///will be explicitly deallocated by the destructor.
alpar@1073
   123
  ///By default it is <tt>std::cout</tt>
alpar@1073
   124
  DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
alpar@1073
   125
			  bool _pros=false) :
alpar@1073
   126
    g(_g), os(_os),
alpar@1086
   127
    _coords(xy<double>(1,1)), _nodeSizes(1.0), _nodeShapes(0),
alpar@1073
   128
    _nodeColors(Color(1,1,1)), _edgeColors(Color(0,0,0)),
alpar@1073
   129
    _edgeWidths(1), _edgeWidthScale(0.3),
alpar@1073
   130
    _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0),
alpar@1073
   131
    _nodeBorderQuotient(.1),
alpar@1073
   132
    _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
alpar@1073
   133
    _showNodes(true), _showEdges(true),
alpar@1073
   134
    _enableParallel(false), _parEdgeDist(1),
alpar@1073
   135
    _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
alpar@1085
   136
    _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
alpar@1073
   137
    _undir(false),
alpar@1073
   138
    _pleaseRemoveOsStream(_pros) {}
alpar@1073
   139
};
alpar@1073
   140
alpar@1073
   141
///Helper class to implement the named parameters of \ref graphToEps()
alpar@1073
   142
alpar@1073
   143
///Helper class to implement the named parameters of \ref graphToEps()
alpar@1073
   144
///\todo Is 'helper class' a good name for this?
alpar@1073
   145
///
alpar@1073
   146
template<class T> class GraphToEps : public T 
alpar@1073
   147
{
alpar@1073
   148
  typedef typename T::Graph Graph;
alpar@1073
   149
  typedef typename Graph::Node Node;
alpar@1073
   150
  typedef typename Graph::NodeIt NodeIt;
alpar@1073
   151
  typedef typename Graph::Edge Edge;
alpar@1073
   152
  typedef typename Graph::EdgeIt EdgeIt;
alpar@1073
   153
  typedef typename Graph::InEdgeIt InEdgeIt;
alpar@1073
   154
  typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@1073
   155
alpar@1073
   156
  bool dontPrint;
alpar@1073
   157
alpar@1086
   158
  enum NodeShapes { CIRCLE=0, SQUARE=1 };
alpar@1086
   159
		   
alpar@1073
   160
  class edgeLess {
alpar@1073
   161
    const Graph &g;
alpar@1073
   162
  public:
alpar@1073
   163
    edgeLess(const Graph &_g) : g(_g) {}
alpar@1073
   164
    bool operator()(Edge a,Edge b) const 
alpar@1073
   165
    {
alpar@1073
   166
      Node ai=min(g.source(a),g.target(a));
alpar@1073
   167
      Node aa=max(g.source(a),g.target(a));
alpar@1073
   168
      Node bi=min(g.source(b),g.target(b));
alpar@1073
   169
      Node ba=max(g.source(b),g.target(b));
alpar@1073
   170
      return ai<bi ||
alpar@1073
   171
	(ai==bi && (aa < ba || 
alpar@1073
   172
		    (aa==ba && ai==g.source(a) && bi==g.target(b))));
alpar@1073
   173
    }
alpar@1073
   174
  };
alpar@1073
   175
  bool isParallel(Edge e,Edge f) const
alpar@1073
   176
  {
alpar@1073
   177
    return (g.source(e)==g.source(f)&&g.target(e)==g.target(f))||
alpar@1073
   178
      (g.source(e)==g.target(f)&&g.target(e)==g.source(f));
alpar@1073
   179
  }
alpar@1073
   180
  static xy<double> rot(xy<double> v) 
alpar@1073
   181
  {
alpar@1073
   182
    return xy<double>(v.y,-v.x);
alpar@1073
   183
  }
alpar@1073
   184
  template<class xy>
alpar@1073
   185
  static std::string psOut(const xy &p) 
alpar@1073
   186
    {
alpar@1073
   187
      std::ostringstream os;	
alpar@1073
   188
      os << p.x << ' ' << p.y;
alpar@1073
   189
      return os.str();
alpar@1073
   190
    }
alpar@1073
   191
  
alpar@1073
   192
public:
alpar@1073
   193
  GraphToEps(const T &t) : T(t), dontPrint(false) {};
alpar@1073
   194
  
alpar@1073
   195
  template<class X> struct CoordsTraits : public T {
alpar@1073
   196
    const X &_coords;
alpar@1073
   197
    CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
alpar@1073
   198
  };
alpar@1073
   199
  ///Sets the map of the node coordinates
alpar@1073
   200
alpar@1073
   201
  ///Sets the map of the node coordinates.
alpar@1073
   202
  ///\param x must be a node map with xy<double> or xy<int> values. 
alpar@1073
   203
  template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
alpar@1073
   204
    dontPrint=true;
alpar@1073
   205
    return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
alpar@1073
   206
  }
alpar@1073
   207
  template<class X> struct NodeSizesTraits : public T {
alpar@1073
   208
    const X &_nodeSizes;
alpar@1073
   209
    NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
alpar@1073
   210
  };
alpar@1073
   211
  ///Sets the map of the node sizes
alpar@1073
   212
alpar@1073
   213
  ///Sets the map of the node sizes
alpar@1073
   214
  ///\param x must be a node map with \c double (or convertible) values. 
alpar@1073
   215
  template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
alpar@1073
   216
  {
alpar@1073
   217
    dontPrint=true;
alpar@1073
   218
    return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
alpar@1073
   219
  }
alpar@1086
   220
  template<class X> struct NodeShapesTraits : public T {
alpar@1086
   221
    const X &_nodeShapes;
alpar@1086
   222
    NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
alpar@1086
   223
  };
alpar@1086
   224
  ///Sets the map of the node shapes
alpar@1086
   225
alpar@1086
   226
  ///Sets the map of the node shapes
alpar@1086
   227
  ///\param x must be a node map with \c int (or convertible) values. 
alpar@1086
   228
  ///\todo Incomplete doc.
alpar@1086
   229
  template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
alpar@1086
   230
  {
alpar@1086
   231
    dontPrint=true;
alpar@1086
   232
    return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
alpar@1086
   233
  }
alpar@1073
   234
  template<class X> struct NodeTextsTraits : public T {
alpar@1073
   235
    const X &_nodeTexts;
alpar@1073
   236
    NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
alpar@1073
   237
  };
alpar@1073
   238
  ///Sets the text printed on the nodes
alpar@1073
   239
alpar@1073
   240
  ///Sets the text printed on the nodes
alpar@1073
   241
  ///\param x must be a node map with type that can be pushed to a standard
alpar@1073
   242
  ///ostream. 
alpar@1073
   243
  template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
alpar@1073
   244
  {
alpar@1073
   245
    dontPrint=true;
alpar@1073
   246
    _showNodeText=true;
alpar@1073
   247
    return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
alpar@1073
   248
  }
alpar@1085
   249
  template<class X> struct NodePsTextsTraits : public T {
alpar@1085
   250
    const X &_nodePsTexts;
alpar@1085
   251
    NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
alpar@1085
   252
  };
alpar@1085
   253
  ///Inserts a PostScript block to the nodes
alpar@1085
   254
alpar@1085
   255
  ///With this command it is possible to insert a verbatim PostScript
alpar@1085
   256
  ///block to the nodes.
alpar@1085
   257
  ///The PS current point will be moved to the centre of the node before
alpar@1085
   258
  ///the PostScript block inserted.
alpar@1085
   259
  ///
alpar@1085
   260
  ///Before and after the block a newline character is inserted to you
alpar@1085
   261
  ///don't have to bother with the separators.
alpar@1085
   262
  ///
alpar@1085
   263
  ///\param x must be a node map with type that can be pushed to a standard
alpar@1085
   264
  ///ostream.
alpar@1085
   265
  ///
alpar@1085
   266
  ///\sa nodePsTextsPreamble()
alpar@1085
   267
  ///\todo Offer the choise not to move to the centre but pass the coordinates
alpar@1085
   268
  ///to the Postscript block inserted.
alpar@1085
   269
  template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
alpar@1085
   270
  {
alpar@1085
   271
    dontPrint=true;
alpar@1085
   272
    _showNodePsText=true;
alpar@1085
   273
    return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
alpar@1085
   274
  }
alpar@1085
   275
  template<class X> struct EdgeWidthsTraits : public T {
alpar@1073
   276
    const X &_edgeWidths;
alpar@1073
   277
    EdgeWidthsTraits(const T &t,const X &x) : T(t), _edgeWidths(x) {}
alpar@1073
   278
  };
alpar@1073
   279
  ///Sets the map of the edge widths
alpar@1073
   280
alpar@1073
   281
  ///Sets the map of the edge widths
alpar@1073
   282
  ///\param x must be a edge map with \c double (or convertible) values. 
alpar@1073
   283
  template<class X> GraphToEps<EdgeWidthsTraits<X> > edgeWidths(const X &x)
alpar@1073
   284
  {
alpar@1073
   285
    dontPrint=true;
alpar@1073
   286
    return GraphToEps<EdgeWidthsTraits<X> >(EdgeWidthsTraits<X>(*this,x));
alpar@1073
   287
  }
alpar@1073
   288
alpar@1073
   289
  template<class X> struct NodeColorsTraits : public T {
alpar@1073
   290
    const X &_nodeColors;
alpar@1073
   291
    NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
alpar@1073
   292
  };
alpar@1073
   293
  ///Sets the map of the node colors
alpar@1073
   294
alpar@1073
   295
  ///Sets the map of the node colors
alpar@1073
   296
  ///\param x must be a node map with \ref Color values. 
alpar@1073
   297
  template<class X> GraphToEps<NodeColorsTraits<X> >
alpar@1073
   298
  nodeColors(const X &x)
alpar@1073
   299
  {
alpar@1073
   300
    dontPrint=true;
alpar@1073
   301
    return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
alpar@1073
   302
  }
alpar@1073
   303
  template<class X> struct EdgeColorsTraits : public T {
alpar@1073
   304
    const X &_edgeColors;
alpar@1073
   305
    EdgeColorsTraits(const T &t,const X &x) : T(t), _edgeColors(x) {}
alpar@1073
   306
  };
alpar@1073
   307
  ///Sets the map of the edge colors
alpar@1073
   308
alpar@1073
   309
  ///Sets the map of the edge colors
alpar@1073
   310
  ///\param x must be a edge map with \ref Color values. 
alpar@1073
   311
  template<class X> GraphToEps<EdgeColorsTraits<X> >
alpar@1073
   312
  edgeColors(const X &x)
alpar@1073
   313
  {
alpar@1073
   314
    dontPrint=true;
alpar@1073
   315
    return GraphToEps<EdgeColorsTraits<X> >(EdgeColorsTraits<X>(*this,x));
alpar@1073
   316
  }
alpar@1073
   317
  ///Sets a global scale factor for node sizes
alpar@1073
   318
alpar@1073
   319
  ///Sets a global scale factor for node sizes
alpar@1073
   320
  ///
alpar@1073
   321
  GraphToEps<T> &nodeScale(double d) {_nodeScale=d;return *this;}
alpar@1073
   322
  ///Sets a global scale factor for edge widths
alpar@1073
   323
alpar@1073
   324
  ///Sets a global scale factor for edge widths
alpar@1073
   325
  ///
alpar@1073
   326
  GraphToEps<T> &edgeWidthScale(double d) {_edgeWidthScale=d;return *this;}
alpar@1073
   327
  ///Sets a global scale factor for the whole picture
alpar@1073
   328
alpar@1073
   329
  ///Sets a global scale factor for the whole picture
alpar@1073
   330
  ///
alpar@1073
   331
  GraphToEps<T> &scale(double d) {_scale=d;return *this;}
alpar@1073
   332
  ///Sets the width of the border around the picture
alpar@1073
   333
alpar@1073
   334
  ///Sets the width of the border around the picture
alpar@1073
   335
  ///
alpar@1073
   336
  GraphToEps<T> &border(double b) {_xBorder=_yBorder=b;return *this;}
alpar@1073
   337
  ///Sets the width of the border around the picture
alpar@1073
   338
alpar@1073
   339
  ///Sets the width of the border around the picture
alpar@1073
   340
  ///
alpar@1073
   341
  GraphToEps<T> &border(double x, double y) {
alpar@1073
   342
    _xBorder=x;_yBorder=y;return *this;
alpar@1073
   343
  }
alpar@1073
   344
  ///Sets whether to draw arrows
alpar@1073
   345
alpar@1073
   346
  ///Sets whether to draw arrows
alpar@1073
   347
  ///
alpar@1073
   348
  GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
alpar@1073
   349
  ///Sets the length of the arrowheads
alpar@1073
   350
alpar@1073
   351
  ///Sets the length of the arrowheads
alpar@1073
   352
  ///
alpar@1073
   353
  GraphToEps<T> &arrowLength(double d) {_arrowLength*=d;return *this;}
alpar@1073
   354
  ///Sets the width of the arrowheads
alpar@1073
   355
alpar@1073
   356
  ///Sets the width of the arrowheads
alpar@1073
   357
  ///
alpar@1073
   358
  GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;}
alpar@1073
   359
  
alpar@1073
   360
  ///Enables parallel edges
alpar@1073
   361
alpar@1073
   362
  ///Enables parallel edges
alpar@1073
   363
  ///\todo Partially implemented
alpar@1073
   364
  GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
alpar@1073
   365
  
alpar@1073
   366
  ///Sets the distance 
alpar@1073
   367
  
alpar@1073
   368
  ///Sets the distance 
alpar@1073
   369
  ///
alpar@1073
   370
  GraphToEps<T> &parEdgeDist(double d) {_parEdgeDist*=d;return *this;}
alpar@1073
   371
  
alpar@1073
   372
  ///Hides the edges
alpar@1073
   373
  
alpar@1073
   374
  ///Hides the edges
alpar@1073
   375
  ///
alpar@1073
   376
  GraphToEps<T> &hideEdges(bool b=true) {_showEdges=!b;return *this;}
alpar@1073
   377
  ///Hides the nodes
alpar@1073
   378
  
alpar@1073
   379
  ///Hides the nodes
alpar@1073
   380
  ///
alpar@1073
   381
  GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
alpar@1073
   382
  
alpar@1073
   383
  ///Sets the size of the node texts
alpar@1073
   384
  
alpar@1073
   385
  ///Sets the size of the node texts
alpar@1073
   386
  ///
alpar@1073
   387
  GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
alpar@1085
   388
  ///Gives a preamble block for node Postscript block.
alpar@1085
   389
  
alpar@1085
   390
  ///Gives a preamble block for node Postscript block.
alpar@1085
   391
  ///
alpar@1085
   392
  ///\sa nodePsTexts()
alpar@1085
   393
  GraphToEps<T> & nodePsTextsPreamble(const char *str) {
alpar@1085
   394
    _nodePsTextsPreamble=s ;return *this;
alpar@1085
   395
  }
alpar@1073
   396
  ///Sets whether the the graph is undirected
alpar@1073
   397
alpar@1073
   398
  ///Sets whether the the graph is undirected
alpar@1073
   399
  ///
alpar@1073
   400
  GraphToEps<T> &undir(bool b=true) {_undir=b;return *this;}
alpar@1073
   401
  ///Sets whether the the graph is directed
alpar@1073
   402
alpar@1073
   403
  ///Sets whether the the graph is directed.
alpar@1073
   404
  ///Use it to show the undirected edges as a pair of directed ones.
alpar@1073
   405
  GraphToEps<T> &bidir(bool b=true) {_undir=!b;return *this;}
alpar@1086
   406
alpar@1086
   407
protected:
alpar@1086
   408
  bool isInsideNode(xy<double> p, double r,int t) 
alpar@1086
   409
  {
alpar@1086
   410
    switch(t) {
alpar@1086
   411
    case CIRCLE:
alpar@1086
   412
      return p.normSquare()<=r*r;
alpar@1086
   413
    case SQUARE:
alpar@1086
   414
      return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
alpar@1086
   415
    }
alpar@1086
   416
    return false;
alpar@1086
   417
  }
alpar@1086
   418
alpar@1086
   419
public:
alpar@1073
   420
  ~GraphToEps() 
alpar@1073
   421
  {
alpar@1073
   422
    if(dontPrint) return;
alpar@1073
   423
    
alpar@1073
   424
    os << "%!PS-Adobe-2.0 EPSF-2.0\n";
alpar@1073
   425
    //\todo: Chech whether the graph is empty.
alpar@1073
   426
    BoundingBox<double> bb;
alpar@1073
   427
    for(NodeIt n(g);n!=INVALID;++n) {
alpar@1073
   428
      double ns=_nodeSizes[n]*_nodeScale;
alpar@1073
   429
      xy<double> p(ns,ns);
alpar@1073
   430
      bb+=p+_coords[n];
alpar@1073
   431
      bb+=-p+_coords[n];
alpar@1073
   432
      }
alpar@1073
   433
    os << "%%BoundingBox: "
alpar@1073
   434
	 << bb.left()*  _scale-_xBorder << ' '
alpar@1073
   435
	 << bb.bottom()*_scale-_yBorder << ' '
alpar@1073
   436
	 << bb.right()* _scale+_xBorder << ' '
alpar@1073
   437
	 << bb.top()*   _scale+_yBorder << '\n';
alpar@1073
   438
    //x1 y1 x2 y2 x3 y3 cr cg cb w
alpar@1073
   439
    os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
alpar@1073
   440
       << "      4 2 roll 1 index 1 index curveto stroke } bind def\n";
alpar@1073
   441
    os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
alpar@1086
   442
    //x y r
alpar@1073
   443
    os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n";
alpar@1086
   444
    //x y r
alpar@1086
   445
    os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
alpar@1086
   446
       << "      2 index 1 index sub 2 index 2 index add lineto\n"
alpar@1086
   447
       << "      2 index 1 index sub 2 index 2 index sub lineto\n"
alpar@1086
   448
       << "      2 index 1 index add 2 index 2 index sub lineto\n"
alpar@1086
   449
       << "      closepath pop pop pop} bind def\n";
alpar@1073
   450
    // x y r cr cg cb
alpar@1086
   451
    os << "/nc { setrgbcolor 2 index 2 index 2 index c fill\n"
alpar@1073
   452
       << "     0 0 0 setrgbcolor dup "
alpar@1073
   453
       << _nodeBorderQuotient << " mul setlinewidth "
alpar@1073
   454
       << 1+_nodeBorderQuotient/2 << " div c stroke\n"
alpar@1073
   455
       << "   } bind def\n";
alpar@1086
   456
    os << "/nsq { setrgbcolor 2 index 2 index 2 index sq fill\n"
alpar@1086
   457
       << "     0 0 0 setrgbcolor dup "
alpar@1086
   458
       << _nodeBorderQuotient << " mul setlinewidth "
alpar@1086
   459
       << 1+_nodeBorderQuotient/2 << " div sq stroke\n"
alpar@1086
   460
       << "   } bind def\n";
alpar@1073
   461
    os << "/arrl " << _arrowLength << " def\n";
alpar@1073
   462
    os << "/arrw " << _arrowWidth << " def\n";
alpar@1073
   463
    // l dx_norm dy_norm
alpar@1073
   464
    os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
alpar@1073
   465
    //len w dx_norm dy_norm x1 y1 cr cg cb
alpar@1073
   466
    os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
alpar@1073
   467
       << "       /w exch def /len exch def\n"
alpar@1073
   468
      //	 << "       0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
alpar@1073
   469
       << "       newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
alpar@1073
   470
       << "       len w sub arrl sub dx dy lrl\n"
alpar@1073
   471
       << "       arrw dy dx neg lrl\n"
alpar@1073
   472
       << "       dx arrl w add mul dy w 2 div arrw add mul sub\n"
alpar@1073
   473
       << "       dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
alpar@1073
   474
       << "       dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
alpar@1073
   475
       << "       dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
alpar@1073
   476
       << "       arrw dy dx neg lrl\n"
alpar@1073
   477
       << "       len w sub arrl sub neg dx dy lrl\n"
alpar@1073
   478
       << "       closepath fill } bind def\n";
alpar@1073
   479
    os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
alpar@1073
   480
       << "         neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
alpar@1073
   481
alpar@1073
   482
    os << "\ngsave\n";
alpar@1073
   483
    if(_scale!=1.0) os << _scale << " dup scale\n";
alpar@1073
   484
    
alpar@1085
   485
    if(_showEdges) {
alpar@1085
   486
      os << "%Edges:\ngsave\n";      
alpar@1073
   487
      if(_enableParallel) {
alpar@1073
   488
	std::vector<Edge> el;
alpar@1073
   489
	for(EdgeIt e(g);e!=INVALID;++e)
alpar@1073
   490
	  if(!_undir||g.source(e)<g.target(e)) el.push_back(e);
alpar@1073
   491
	sort(el.begin(),el.end(),edgeLess(g));
alpar@1073
   492
	
alpar@1073
   493
	typename std::vector<Edge>::iterator j;
alpar@1073
   494
	for(typename std::vector<Edge>::iterator i=el.begin();i!=el.end();i=j) {
alpar@1073
   495
	  for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
alpar@1073
   496
alpar@1073
   497
	  double sw=0;
alpar@1073
   498
	  for(typename std::vector<Edge>::iterator e=i;e!=j;++e)
alpar@1073
   499
	    sw+=_edgeWidths[*e]*_edgeWidthScale+_parEdgeDist;
alpar@1073
   500
	  sw-=_parEdgeDist;
alpar@1073
   501
	  sw/=-2.0;
alpar@1085
   502
	  xy<double> dvec(_coords[g.target(*i)]-_coords[g.source(*i)]);
alpar@1085
   503
	  double l=sqrt(dvec.normSquare());
alpar@1085
   504
	  xy<double> d(dvec/l);
alpar@1085
   505
 	  xy<double> m;
alpar@1085
   506
// 	  m=xy<double>(_coords[g.target(*i)]+_coords[g.source(*i)])/2.0;
alpar@1085
   507
alpar@1085
   508
//  	  m=xy<double>(_coords[g.source(*i)])+
alpar@1085
   509
// 	    dvec*(double(_nodeSizes[g.source(*i)])/
alpar@1085
   510
// 	       (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
alpar@1085
   511
alpar@1085
   512
 	  m=xy<double>(_coords[g.source(*i)])+
alpar@1085
   513
	    d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
alpar@1085
   514
alpar@1073
   515
	  for(typename std::vector<Edge>::iterator e=i;e!=j;++e) {
alpar@1073
   516
	    sw+=_edgeWidths[*e]*_edgeWidthScale/2.0;
alpar@1085
   517
	    xy<double> mm=m+rot(d)*sw/.75;
alpar@1073
   518
	    if(_drawArrows) {
alpar@1086
   519
	      int node_shape;
alpar@1073
   520
	      const int INERPOL_PREC=20;
alpar@1073
   521
	      xy<double> s=_coords[g.source(*e)];
alpar@1073
   522
	      xy<double> t=_coords[g.target(*e)];
alpar@1073
   523
	      double rn=_nodeSizes[g.target(*e)]*_nodeScale;
alpar@1086
   524
	      node_shape=_nodeShapes[g.target(*e)];
alpar@1085
   525
	      Bezier3 bez(s,mm,mm,t);
alpar@1073
   526
	      double t1=0,t2=1;
alpar@1073
   527
	      for(int i=0;i<INERPOL_PREC;++i)
alpar@1086
   528
		if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
alpar@1086
   529
		else t1=(t1+t2)/2;
alpar@1073
   530
	      xy<double> apoint=bez((t1+t2)/2);
alpar@1086
   531
	      rn = _arrowLength+_edgeWidths[*e]*_edgeWidthScale;
alpar@1073
   532
	      rn*=rn;
alpar@1086
   533
	      t2=(t1+t2)/2;t1=0;
alpar@1073
   534
	      for(int i=0;i<INERPOL_PREC;++i)
alpar@1086
   535
		if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
alpar@1073
   536
		else t2=(t1+t2)/2;
alpar@1073
   537
	      xy<double> linend=bez((t1+t2)/2);	      
alpar@1073
   538
	      bez=bez.before((t1+t2)/2);
alpar@1086
   539
// 	      rn=_nodeSizes[g.source(*e)]*_nodeScale;
alpar@1086
   540
// 	      node_shape=_nodeShapes[g.source(*e)];
alpar@1086
   541
// 	      t1=0;t2=1;
alpar@1086
   542
// 	      for(int i=0;i<INERPOL_PREC;++i)
alpar@1086
   543
// 		if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t1=(t1+t2)/2;
alpar@1086
   544
// 		else t2=(t1+t2)/2;
alpar@1086
   545
// 	      bez=bez.after((t1+t2)/2);
alpar@1073
   546
	      os << _edgeWidths[*e]*_edgeWidthScale << " setlinewidth "
alpar@1073
   547
		 << _edgeColors[*e].getR() << ' '
alpar@1073
   548
		 << _edgeColors[*e].getG() << ' '
alpar@1073
   549
		 << _edgeColors[*e].getB() << " setrgbcolor newpath\n"
alpar@1073
   550
		 << bez.p1.x << ' ' <<  bez.p1.y << " moveto\n"
alpar@1073
   551
		 << bez.p2.x << ' ' << bez.p2.y << ' '
alpar@1073
   552
		 << bez.p3.x << ' ' << bez.p3.y << ' '
alpar@1073
   553
		 << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
alpar@1073
   554
	      xy<double> dd(rot(linend-apoint));
alpar@1073
   555
	      dd*=(_edgeWidths[*e]*_edgeWidthScale+_arrowWidth)/
alpar@1073
   556
		sqrt(dd.normSquare());
alpar@1073
   557
	      os << "newpath " << psOut(apoint) << " moveto "
alpar@1073
   558
		 << psOut(linend+dd) << " lineto "
alpar@1073
   559
		 << psOut(linend-dd) << " lineto closepath fill\n";
alpar@1073
   560
	    }
alpar@1073
   561
	    else {
alpar@1073
   562
	      os << _coords[g.source(*e)].x << ' '
alpar@1073
   563
		 << _coords[g.source(*e)].y << ' '
alpar@1085
   564
		 << mm.x << ' ' << mm.y << ' '
alpar@1073
   565
		 << _coords[g.target(*e)].x << ' '
alpar@1073
   566
		 << _coords[g.target(*e)].y << ' '
alpar@1073
   567
		 << _edgeColors[*e].getR() << ' '
alpar@1073
   568
		 << _edgeColors[*e].getG() << ' '
alpar@1073
   569
		 << _edgeColors[*e].getB() << ' '
alpar@1073
   570
		 << _edgeWidths[*e]*_edgeWidthScale << " lb\n";
alpar@1073
   571
	    }
alpar@1073
   572
	    sw+=_edgeWidths[*e]*_edgeWidthScale/2.0+_parEdgeDist;
alpar@1073
   573
	  }
alpar@1073
   574
	}
alpar@1073
   575
      }
alpar@1073
   576
      else for(EdgeIt e(g);e!=INVALID;++e)
alpar@1073
   577
	if(!_undir||g.source(e)<g.target(e))
alpar@1073
   578
	  if(_drawArrows) {
alpar@1073
   579
	    xy<double> d(_coords[g.target(e)]-_coords[g.source(e)]);
alpar@1073
   580
	    double l=sqrt(d.normSquare());
alpar@1073
   581
	    d/=l;
alpar@1073
   582
	    xy<double> x1(d*_nodeScale*_nodeSizes[g.source(e)]+
alpar@1073
   583
			  _coords[g.source(e)]);
alpar@1073
   584
	    os << l-(_nodeSizes[g.source(e)]+
alpar@1073
   585
		     _nodeSizes[g.target(e)])*_nodeScale << ' '
alpar@1073
   586
	       << _edgeWidths[e]*_edgeWidthScale << ' '
alpar@1073
   587
	       << d.x << ' ' << d.y << ' '
alpar@1073
   588
	       << x1.x << ' ' << x1.y << ' '
alpar@1073
   589
	       << _edgeColors[e].getR() << ' '
alpar@1073
   590
	       << _edgeColors[e].getG() << ' '
alpar@1073
   591
	       << _edgeColors[e].getB() << " arr\n";
alpar@1073
   592
	  }
alpar@1073
   593
	  else os << _coords[g.source(e)].x << ' '
alpar@1073
   594
		  << _coords[g.source(e)].y << ' '
alpar@1073
   595
		  << _coords[g.target(e)].x << ' '
alpar@1073
   596
		  << _coords[g.target(e)].y << ' '
alpar@1073
   597
		  << _edgeColors[e].getR() << ' '
alpar@1073
   598
		  << _edgeColors[e].getG() << ' '
alpar@1073
   599
		  << _edgeColors[e].getB() << ' '
alpar@1073
   600
		  << _edgeWidths[e]*_edgeWidthScale << " l\n";
alpar@1085
   601
      os << "grestore\n";
alpar@1085
   602
    }
alpar@1085
   603
    if(_showNodes) {
alpar@1085
   604
      os << "%Nodes:\ngsave\n";
alpar@1086
   605
      for(NodeIt n(g);n!=INVALID;++n) {
alpar@1073
   606
	os << _coords[n].x << ' ' << _coords[n].y << ' '
alpar@1073
   607
	   << _nodeSizes[n]*_nodeScale << ' '
alpar@1073
   608
	   << _nodeColors[n].getR() << ' '
alpar@1073
   609
	   << _nodeColors[n].getG() << ' '
alpar@1086
   610
	   << _nodeColors[n].getB() << ' ';
alpar@1086
   611
	switch(_nodeShapes[n]) {
alpar@1086
   612
	case CIRCLE:
alpar@1086
   613
	  os<< "nc";break;
alpar@1086
   614
	case SQUARE:
alpar@1086
   615
	  os<< "nsq";break;
alpar@1086
   616
	}
alpar@1086
   617
	os<<'\n';
alpar@1086
   618
      }
alpar@1085
   619
      os << "grestore\n";
alpar@1085
   620
    }
alpar@1073
   621
    if(_showNodeText) {
alpar@1085
   622
      os << "%Node texts:\ngsave\n";
alpar@1073
   623
      os << "/fosi " << _nodeTextSize << " def\n";
alpar@1073
   624
      os << "(Helvetica) findfont fosi scalefont setfont\n";
alpar@1073
   625
      os << "0 0 0 setrgbcolor\n";
alpar@1073
   626
      for(NodeIt n(g);n!=INVALID;++n)
alpar@1073
   627
	os << _coords[n].x << ' ' << _coords[n].y
alpar@1073
   628
	   << " (" << _nodeTexts[n] << ") cshow\n";
alpar@1085
   629
      os << "grestore\n";
alpar@1073
   630
    }
alpar@1085
   631
    if(_showNodePsText) {
alpar@1085
   632
      os << "%Node PS blocks:\ngsave\n";
alpar@1085
   633
      for(NodeIt n(g);n!=INVALID;++n)
alpar@1085
   634
	os << _coords[n].x << ' ' << _coords[n].y
alpar@1085
   635
	   << " moveto\n" << _nodePsTexts[n] << "\n";
alpar@1085
   636
      os << "grestore\n";
alpar@1085
   637
    }
alpar@1085
   638
    
alpar@1085
   639
    os << "grestore\n";
alpar@1073
   640
alpar@1073
   641
    //CleanUp:
alpar@1073
   642
    if(_pleaseRemoveOsStream) {delete &os;}
alpar@1073
   643
  } 
alpar@1073
   644
};
alpar@1073
   645
alpar@1073
   646
alpar@1073
   647
///Generates an EPS file from a graph
alpar@1073
   648
alpar@1073
   649
///\ingroup misc
alpar@1073
   650
///Generates an EPS file from a graph.
alpar@1073
   651
///\param g is a reference to the graph to be printed
alpar@1073
   652
///\param os is a reference to the output stream.
alpar@1073
   653
///By default it is <tt>std::cout</tt>
alpar@1073
   654
///
alpar@1073
   655
///This function also has a lot of \ref named-templ-param "named parameters",
alpar@1073
   656
///they are declared as the members of class \ref GraphToEps. The following
alpar@1073
   657
///example shows how to use these parameters.
alpar@1073
   658
///\code
alpar@1073
   659
/// graphToEps(g).scale(10).coords(coords)
alpar@1073
   660
///              .nodeScale(2).nodeSizes(sizes)
alpar@1073
   661
///              .edgeWidthScale(.4);
alpar@1073
   662
///\endcode
alpar@1073
   663
///\sa GraphToEps
alpar@1073
   664
///\sa graphToEps(G &g, char *file_name)
alpar@1073
   665
template<class G>
alpar@1073
   666
GraphToEps<DefaultGraphToEpsTraits<G> > 
alpar@1073
   667
graphToEps(G &g, std::ostream& os=std::cout)
alpar@1073
   668
{
alpar@1073
   669
  return 
alpar@1073
   670
    GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
alpar@1073
   671
}
alpar@1073
   672
 
alpar@1073
   673
///Generates an EPS file from a graph
alpar@1073
   674
alpar@1073
   675
//\ingroup misc
alpar@1073
   676
///This function does the same as
alpar@1073
   677
///\ref graphToEps(G &g,std::ostream& os)
alpar@1073
   678
///but it writes its output into the file \c file_name
alpar@1073
   679
///instead of a stream.
alpar@1073
   680
///\sa graphToEps(G &g, std::ostream& os)
alpar@1073
   681
template<class G>
alpar@1073
   682
GraphToEps<DefaultGraphToEpsTraits<G> > 
alpar@1073
   683
graphToEps(G &g,char *file_name)
alpar@1073
   684
{
alpar@1073
   685
  return GraphToEps<DefaultGraphToEpsTraits<G> >
alpar@1073
   686
    (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true));
alpar@1073
   687
}
alpar@1073
   688
alpar@1073
   689
} //END OF NAMESPACE LEMON
alpar@1073
   690
alpar@1073
   691
#endif // LEMON_GRAPH_TO_EPS_H