src/lemon/graph_to_eps.h
author alpar
Fri, 28 Jan 2005 08:53:48 +0000
changeset 1101 9286569c3749
parent 1089 c0f4ebdc0034
child 1103 f196dc4f1b31
permissions -rw-r--r--
Wrap a long line
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@1087
   156
  static const int INTERPOL_PREC=20;
alpar@1087
   157
alpar@1073
   158
  bool dontPrint;
alpar@1073
   159
alpar@1088
   160
  enum NodeShapes { CIRCLE=0, SQUARE=1, DIAMOND=2 };
alpar@1086
   161
		   
alpar@1073
   162
  class edgeLess {
alpar@1073
   163
    const Graph &g;
alpar@1073
   164
  public:
alpar@1073
   165
    edgeLess(const Graph &_g) : g(_g) {}
alpar@1073
   166
    bool operator()(Edge a,Edge b) const 
alpar@1073
   167
    {
alpar@1073
   168
      Node ai=min(g.source(a),g.target(a));
alpar@1073
   169
      Node aa=max(g.source(a),g.target(a));
alpar@1073
   170
      Node bi=min(g.source(b),g.target(b));
alpar@1073
   171
      Node ba=max(g.source(b),g.target(b));
alpar@1073
   172
      return ai<bi ||
alpar@1073
   173
	(ai==bi && (aa < ba || 
alpar@1073
   174
		    (aa==ba && ai==g.source(a) && bi==g.target(b))));
alpar@1073
   175
    }
alpar@1073
   176
  };
alpar@1073
   177
  bool isParallel(Edge e,Edge f) const
alpar@1073
   178
  {
alpar@1073
   179
    return (g.source(e)==g.source(f)&&g.target(e)==g.target(f))||
alpar@1073
   180
      (g.source(e)==g.target(f)&&g.target(e)==g.source(f));
alpar@1073
   181
  }
alpar@1073
   182
  static xy<double> rot(xy<double> v) 
alpar@1073
   183
  {
alpar@1073
   184
    return xy<double>(v.y,-v.x);
alpar@1073
   185
  }
alpar@1073
   186
  template<class xy>
alpar@1073
   187
  static std::string psOut(const xy &p) 
alpar@1073
   188
    {
alpar@1073
   189
      std::ostringstream os;	
alpar@1073
   190
      os << p.x << ' ' << p.y;
alpar@1073
   191
      return os.str();
alpar@1073
   192
    }
alpar@1073
   193
  
alpar@1073
   194
public:
alpar@1073
   195
  GraphToEps(const T &t) : T(t), dontPrint(false) {};
alpar@1073
   196
  
alpar@1073
   197
  template<class X> struct CoordsTraits : public T {
alpar@1073
   198
    const X &_coords;
alpar@1073
   199
    CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
alpar@1073
   200
  };
alpar@1073
   201
  ///Sets the map of the node coordinates
alpar@1073
   202
alpar@1073
   203
  ///Sets the map of the node coordinates.
alpar@1073
   204
  ///\param x must be a node map with xy<double> or xy<int> values. 
alpar@1073
   205
  template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
alpar@1073
   206
    dontPrint=true;
alpar@1073
   207
    return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
alpar@1073
   208
  }
alpar@1073
   209
  template<class X> struct NodeSizesTraits : public T {
alpar@1073
   210
    const X &_nodeSizes;
alpar@1073
   211
    NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
alpar@1073
   212
  };
alpar@1073
   213
  ///Sets the map of the node sizes
alpar@1073
   214
alpar@1073
   215
  ///Sets the map of the node sizes
alpar@1073
   216
  ///\param x must be a node map with \c double (or convertible) values. 
alpar@1073
   217
  template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
alpar@1073
   218
  {
alpar@1073
   219
    dontPrint=true;
alpar@1073
   220
    return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
alpar@1073
   221
  }
alpar@1086
   222
  template<class X> struct NodeShapesTraits : public T {
alpar@1086
   223
    const X &_nodeShapes;
alpar@1086
   224
    NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
alpar@1086
   225
  };
alpar@1086
   226
  ///Sets the map of the node shapes
alpar@1086
   227
alpar@1086
   228
  ///Sets the map of the node shapes
alpar@1086
   229
  ///\param x must be a node map with \c int (or convertible) values. 
alpar@1086
   230
  ///\todo Incomplete doc.
alpar@1086
   231
  template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
alpar@1086
   232
  {
alpar@1086
   233
    dontPrint=true;
alpar@1086
   234
    return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
alpar@1086
   235
  }
alpar@1073
   236
  template<class X> struct NodeTextsTraits : public T {
alpar@1073
   237
    const X &_nodeTexts;
alpar@1073
   238
    NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
alpar@1073
   239
  };
alpar@1073
   240
  ///Sets the text printed on the nodes
alpar@1073
   241
alpar@1073
   242
  ///Sets the text printed on the nodes
alpar@1073
   243
  ///\param x must be a node map with type that can be pushed to a standard
alpar@1073
   244
  ///ostream. 
alpar@1073
   245
  template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
alpar@1073
   246
  {
alpar@1073
   247
    dontPrint=true;
alpar@1073
   248
    _showNodeText=true;
alpar@1073
   249
    return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
alpar@1073
   250
  }
alpar@1085
   251
  template<class X> struct NodePsTextsTraits : public T {
alpar@1085
   252
    const X &_nodePsTexts;
alpar@1085
   253
    NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
alpar@1085
   254
  };
alpar@1085
   255
  ///Inserts a PostScript block to the nodes
alpar@1085
   256
alpar@1085
   257
  ///With this command it is possible to insert a verbatim PostScript
alpar@1085
   258
  ///block to the nodes.
alpar@1085
   259
  ///The PS current point will be moved to the centre of the node before
alpar@1085
   260
  ///the PostScript block inserted.
alpar@1085
   261
  ///
alpar@1085
   262
  ///Before and after the block a newline character is inserted to you
alpar@1085
   263
  ///don't have to bother with the separators.
alpar@1085
   264
  ///
alpar@1085
   265
  ///\param x must be a node map with type that can be pushed to a standard
alpar@1085
   266
  ///ostream.
alpar@1085
   267
  ///
alpar@1085
   268
  ///\sa nodePsTextsPreamble()
alpar@1085
   269
  ///\todo Offer the choise not to move to the centre but pass the coordinates
alpar@1085
   270
  ///to the Postscript block inserted.
alpar@1085
   271
  template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
alpar@1085
   272
  {
alpar@1085
   273
    dontPrint=true;
alpar@1085
   274
    _showNodePsText=true;
alpar@1085
   275
    return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
alpar@1085
   276
  }
alpar@1085
   277
  template<class X> struct EdgeWidthsTraits : public T {
alpar@1073
   278
    const X &_edgeWidths;
alpar@1073
   279
    EdgeWidthsTraits(const T &t,const X &x) : T(t), _edgeWidths(x) {}
alpar@1073
   280
  };
alpar@1073
   281
  ///Sets the map of the edge widths
alpar@1073
   282
alpar@1073
   283
  ///Sets the map of the edge widths
alpar@1073
   284
  ///\param x must be a edge map with \c double (or convertible) values. 
alpar@1073
   285
  template<class X> GraphToEps<EdgeWidthsTraits<X> > edgeWidths(const X &x)
alpar@1073
   286
  {
alpar@1073
   287
    dontPrint=true;
alpar@1073
   288
    return GraphToEps<EdgeWidthsTraits<X> >(EdgeWidthsTraits<X>(*this,x));
alpar@1073
   289
  }
alpar@1073
   290
alpar@1073
   291
  template<class X> struct NodeColorsTraits : public T {
alpar@1073
   292
    const X &_nodeColors;
alpar@1073
   293
    NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
alpar@1073
   294
  };
alpar@1073
   295
  ///Sets the map of the node colors
alpar@1073
   296
alpar@1073
   297
  ///Sets the map of the node colors
alpar@1073
   298
  ///\param x must be a node map with \ref Color values. 
alpar@1073
   299
  template<class X> GraphToEps<NodeColorsTraits<X> >
alpar@1073
   300
  nodeColors(const X &x)
alpar@1073
   301
  {
alpar@1073
   302
    dontPrint=true;
alpar@1073
   303
    return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
alpar@1073
   304
  }
alpar@1073
   305
  template<class X> struct EdgeColorsTraits : public T {
alpar@1073
   306
    const X &_edgeColors;
alpar@1073
   307
    EdgeColorsTraits(const T &t,const X &x) : T(t), _edgeColors(x) {}
alpar@1073
   308
  };
alpar@1073
   309
  ///Sets the map of the edge colors
alpar@1073
   310
alpar@1073
   311
  ///Sets the map of the edge colors
alpar@1073
   312
  ///\param x must be a edge map with \ref Color values. 
alpar@1073
   313
  template<class X> GraphToEps<EdgeColorsTraits<X> >
alpar@1073
   314
  edgeColors(const X &x)
alpar@1073
   315
  {
alpar@1073
   316
    dontPrint=true;
alpar@1073
   317
    return GraphToEps<EdgeColorsTraits<X> >(EdgeColorsTraits<X>(*this,x));
alpar@1073
   318
  }
alpar@1073
   319
  ///Sets a global scale factor for node sizes
alpar@1073
   320
alpar@1073
   321
  ///Sets a global scale factor for node sizes
alpar@1073
   322
  ///
alpar@1073
   323
  GraphToEps<T> &nodeScale(double d) {_nodeScale=d;return *this;}
alpar@1073
   324
  ///Sets a global scale factor for edge widths
alpar@1073
   325
alpar@1073
   326
  ///Sets a global scale factor for edge widths
alpar@1073
   327
  ///
alpar@1073
   328
  GraphToEps<T> &edgeWidthScale(double d) {_edgeWidthScale=d;return *this;}
alpar@1073
   329
  ///Sets a global scale factor for the whole picture
alpar@1073
   330
alpar@1073
   331
  ///Sets a global scale factor for the whole picture
alpar@1073
   332
  ///
alpar@1073
   333
  GraphToEps<T> &scale(double d) {_scale=d;return *this;}
alpar@1073
   334
  ///Sets the width of the border around the picture
alpar@1073
   335
alpar@1073
   336
  ///Sets the width of the border around the picture
alpar@1073
   337
  ///
alpar@1073
   338
  GraphToEps<T> &border(double b) {_xBorder=_yBorder=b;return *this;}
alpar@1073
   339
  ///Sets the width of the border around the picture
alpar@1073
   340
alpar@1073
   341
  ///Sets the width of the border around the picture
alpar@1073
   342
  ///
alpar@1073
   343
  GraphToEps<T> &border(double x, double y) {
alpar@1073
   344
    _xBorder=x;_yBorder=y;return *this;
alpar@1073
   345
  }
alpar@1073
   346
  ///Sets whether to draw arrows
alpar@1073
   347
alpar@1073
   348
  ///Sets whether to draw arrows
alpar@1073
   349
  ///
alpar@1073
   350
  GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
alpar@1073
   351
  ///Sets the length of the arrowheads
alpar@1073
   352
alpar@1073
   353
  ///Sets the length of the arrowheads
alpar@1073
   354
  ///
alpar@1073
   355
  GraphToEps<T> &arrowLength(double d) {_arrowLength*=d;return *this;}
alpar@1073
   356
  ///Sets the width of the arrowheads
alpar@1073
   357
alpar@1073
   358
  ///Sets the width of the arrowheads
alpar@1073
   359
  ///
alpar@1073
   360
  GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;}
alpar@1073
   361
  
alpar@1073
   362
  ///Enables parallel edges
alpar@1073
   363
alpar@1073
   364
  ///Enables parallel edges
alpar@1073
   365
  ///\todo Partially implemented
alpar@1073
   366
  GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
alpar@1073
   367
  
alpar@1073
   368
  ///Sets the distance 
alpar@1073
   369
  
alpar@1073
   370
  ///Sets the distance 
alpar@1073
   371
  ///
alpar@1073
   372
  GraphToEps<T> &parEdgeDist(double d) {_parEdgeDist*=d;return *this;}
alpar@1073
   373
  
alpar@1073
   374
  ///Hides the edges
alpar@1073
   375
  
alpar@1073
   376
  ///Hides the edges
alpar@1073
   377
  ///
alpar@1073
   378
  GraphToEps<T> &hideEdges(bool b=true) {_showEdges=!b;return *this;}
alpar@1073
   379
  ///Hides the nodes
alpar@1073
   380
  
alpar@1073
   381
  ///Hides the nodes
alpar@1073
   382
  ///
alpar@1073
   383
  GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
alpar@1073
   384
  
alpar@1073
   385
  ///Sets the size of the node texts
alpar@1073
   386
  
alpar@1073
   387
  ///Sets the size of the node texts
alpar@1073
   388
  ///
alpar@1073
   389
  GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
alpar@1085
   390
  ///Gives a preamble block for node Postscript block.
alpar@1085
   391
  
alpar@1085
   392
  ///Gives a preamble block for node Postscript block.
alpar@1085
   393
  ///
alpar@1085
   394
  ///\sa nodePsTexts()
alpar@1085
   395
  GraphToEps<T> & nodePsTextsPreamble(const char *str) {
alpar@1085
   396
    _nodePsTextsPreamble=s ;return *this;
alpar@1085
   397
  }
alpar@1073
   398
  ///Sets whether the the graph is undirected
alpar@1073
   399
alpar@1073
   400
  ///Sets whether the the graph is undirected
alpar@1073
   401
  ///
alpar@1073
   402
  GraphToEps<T> &undir(bool b=true) {_undir=b;return *this;}
alpar@1073
   403
  ///Sets whether the the graph is directed
alpar@1073
   404
alpar@1073
   405
  ///Sets whether the the graph is directed.
alpar@1073
   406
  ///Use it to show the undirected edges as a pair of directed ones.
alpar@1073
   407
  GraphToEps<T> &bidir(bool b=true) {_undir=!b;return *this;}
alpar@1086
   408
alpar@1086
   409
protected:
alpar@1086
   410
  bool isInsideNode(xy<double> p, double r,int t) 
alpar@1086
   411
  {
alpar@1086
   412
    switch(t) {
alpar@1086
   413
    case CIRCLE:
alpar@1086
   414
      return p.normSquare()<=r*r;
alpar@1086
   415
    case SQUARE:
alpar@1086
   416
      return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
alpar@1088
   417
    case DIAMOND:
alpar@1088
   418
      return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
alpar@1086
   419
    }
alpar@1086
   420
    return false;
alpar@1086
   421
  }
alpar@1086
   422
alpar@1086
   423
public:
alpar@1091
   424
  ~GraphToEps() { }
alpar@1091
   425
  
alpar@1091
   426
  ///Draws the graph.
alpar@1091
   427
alpar@1091
   428
  ///Like other functions using
alpar@1091
   429
  ///\ref named-templ-func-param "named template parameters",
alpar@1091
   430
  ///this function calles the algorithm itself, i.e. in this case
alpar@1091
   431
  ///it draws the graph.
alpar@1091
   432
  void run() {
alpar@1073
   433
    if(dontPrint) return;
alpar@1073
   434
    
alpar@1073
   435
    os << "%!PS-Adobe-2.0 EPSF-2.0\n";
alpar@1073
   436
    //\todo: Chech whether the graph is empty.
alpar@1073
   437
    BoundingBox<double> bb;
alpar@1073
   438
    for(NodeIt n(g);n!=INVALID;++n) {
alpar@1073
   439
      double ns=_nodeSizes[n]*_nodeScale;
alpar@1073
   440
      xy<double> p(ns,ns);
alpar@1073
   441
      bb+=p+_coords[n];
alpar@1073
   442
      bb+=-p+_coords[n];
alpar@1073
   443
      }
alpar@1073
   444
    os << "%%BoundingBox: "
alpar@1073
   445
	 << bb.left()*  _scale-_xBorder << ' '
alpar@1073
   446
	 << bb.bottom()*_scale-_yBorder << ' '
alpar@1073
   447
	 << bb.right()* _scale+_xBorder << ' '
alpar@1073
   448
	 << bb.top()*   _scale+_yBorder << '\n';
alpar@1073
   449
    //x1 y1 x2 y2 x3 y3 cr cg cb w
alpar@1073
   450
    os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
alpar@1073
   451
       << "      4 2 roll 1 index 1 index curveto stroke } bind def\n";
alpar@1073
   452
    os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
alpar@1086
   453
    //x y r
alpar@1073
   454
    os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n";
alpar@1086
   455
    //x y r
alpar@1086
   456
    os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
alpar@1086
   457
       << "      2 index 1 index sub 2 index 2 index add lineto\n"
alpar@1086
   458
       << "      2 index 1 index sub 2 index 2 index sub lineto\n"
alpar@1086
   459
       << "      2 index 1 index add 2 index 2 index sub lineto\n"
alpar@1086
   460
       << "      closepath pop pop pop} bind def\n";
alpar@1088
   461
    //x y r
alpar@1088
   462
    os << "/di { newpath 2 index 1 index add 2 index moveto\n"
alpar@1088
   463
       << "      2 index             2 index 2 index add lineto\n"
alpar@1088
   464
       << "      2 index 1 index sub 2 index             lineto\n"
alpar@1088
   465
       << "      2 index             2 index 2 index sub lineto\n"
alpar@1088
   466
       << "      closepath pop pop pop} bind def\n";
alpar@1073
   467
    // x y r cr cg cb
alpar@1089
   468
    os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
alpar@1089
   469
       << "     setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
alpar@1073
   470
       << "   } bind def\n";
alpar@1089
   471
    os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
alpar@1089
   472
       << "     setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
alpar@1086
   473
       << "   } bind def\n";
alpar@1089
   474
    os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
alpar@1089
   475
       << "     setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
alpar@1088
   476
       << "   } bind def\n";
alpar@1073
   477
    os << "/arrl " << _arrowLength << " def\n";
alpar@1073
   478
    os << "/arrw " << _arrowWidth << " def\n";
alpar@1073
   479
    // l dx_norm dy_norm
alpar@1073
   480
    os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
alpar@1073
   481
    //len w dx_norm dy_norm x1 y1 cr cg cb
alpar@1073
   482
    os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
alpar@1073
   483
       << "       /w exch def /len exch def\n"
alpar@1073
   484
      //	 << "       0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
alpar@1073
   485
       << "       newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
alpar@1073
   486
       << "       len w sub arrl sub dx dy lrl\n"
alpar@1073
   487
       << "       arrw dy dx neg lrl\n"
alpar@1073
   488
       << "       dx arrl w add mul dy w 2 div arrw add mul sub\n"
alpar@1073
   489
       << "       dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
alpar@1073
   490
       << "       dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
alpar@1073
   491
       << "       dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
alpar@1073
   492
       << "       arrw dy dx neg lrl\n"
alpar@1073
   493
       << "       len w sub arrl sub neg dx dy lrl\n"
alpar@1073
   494
       << "       closepath fill } bind def\n";
alpar@1073
   495
    os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
alpar@1073
   496
       << "         neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
alpar@1073
   497
alpar@1073
   498
    os << "\ngsave\n";
alpar@1073
   499
    if(_scale!=1.0) os << _scale << " dup scale\n";
alpar@1073
   500
    
alpar@1085
   501
    if(_showEdges) {
alpar@1085
   502
      os << "%Edges:\ngsave\n";      
alpar@1073
   503
      if(_enableParallel) {
alpar@1073
   504
	std::vector<Edge> el;
alpar@1073
   505
	for(EdgeIt e(g);e!=INVALID;++e)
alpar@1073
   506
	  if(!_undir||g.source(e)<g.target(e)) el.push_back(e);
alpar@1073
   507
	sort(el.begin(),el.end(),edgeLess(g));
alpar@1073
   508
	
alpar@1073
   509
	typename std::vector<Edge>::iterator j;
alpar@1073
   510
	for(typename std::vector<Edge>::iterator i=el.begin();i!=el.end();i=j) {
alpar@1073
   511
	  for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
alpar@1073
   512
alpar@1073
   513
	  double sw=0;
alpar@1073
   514
	  for(typename std::vector<Edge>::iterator e=i;e!=j;++e)
alpar@1073
   515
	    sw+=_edgeWidths[*e]*_edgeWidthScale+_parEdgeDist;
alpar@1073
   516
	  sw-=_parEdgeDist;
alpar@1073
   517
	  sw/=-2.0;
alpar@1085
   518
	  xy<double> dvec(_coords[g.target(*i)]-_coords[g.source(*i)]);
alpar@1085
   519
	  double l=sqrt(dvec.normSquare());
alpar@1085
   520
	  xy<double> d(dvec/l);
alpar@1085
   521
 	  xy<double> m;
alpar@1085
   522
// 	  m=xy<double>(_coords[g.target(*i)]+_coords[g.source(*i)])/2.0;
alpar@1085
   523
alpar@1085
   524
//  	  m=xy<double>(_coords[g.source(*i)])+
alpar@1085
   525
// 	    dvec*(double(_nodeSizes[g.source(*i)])/
alpar@1085
   526
// 	       (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
alpar@1085
   527
alpar@1085
   528
 	  m=xy<double>(_coords[g.source(*i)])+
alpar@1085
   529
	    d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
alpar@1085
   530
alpar@1073
   531
	  for(typename std::vector<Edge>::iterator e=i;e!=j;++e) {
alpar@1073
   532
	    sw+=_edgeWidths[*e]*_edgeWidthScale/2.0;
alpar@1085
   533
	    xy<double> mm=m+rot(d)*sw/.75;
alpar@1073
   534
	    if(_drawArrows) {
alpar@1086
   535
	      int node_shape;
alpar@1073
   536
	      xy<double> s=_coords[g.source(*e)];
alpar@1073
   537
	      xy<double> t=_coords[g.target(*e)];
alpar@1073
   538
	      double rn=_nodeSizes[g.target(*e)]*_nodeScale;
alpar@1086
   539
	      node_shape=_nodeShapes[g.target(*e)];
alpar@1085
   540
	      Bezier3 bez(s,mm,mm,t);
alpar@1073
   541
	      double t1=0,t2=1;
alpar@1087
   542
	      for(int i=0;i<INTERPOL_PREC;++i)
alpar@1086
   543
		if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
alpar@1086
   544
		else t1=(t1+t2)/2;
alpar@1073
   545
	      xy<double> apoint=bez((t1+t2)/2);
alpar@1086
   546
	      rn = _arrowLength+_edgeWidths[*e]*_edgeWidthScale;
alpar@1073
   547
	      rn*=rn;
alpar@1086
   548
	      t2=(t1+t2)/2;t1=0;
alpar@1087
   549
	      for(int i=0;i<INTERPOL_PREC;++i)
alpar@1086
   550
		if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
alpar@1073
   551
		else t2=(t1+t2)/2;
alpar@1073
   552
	      xy<double> linend=bez((t1+t2)/2);	      
alpar@1073
   553
	      bez=bez.before((t1+t2)/2);
alpar@1086
   554
// 	      rn=_nodeSizes[g.source(*e)]*_nodeScale;
alpar@1086
   555
// 	      node_shape=_nodeShapes[g.source(*e)];
alpar@1086
   556
// 	      t1=0;t2=1;
alpar@1087
   557
// 	      for(int i=0;i<INTERPOL_PREC;++i)
alpar@1086
   558
// 		if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t1=(t1+t2)/2;
alpar@1086
   559
// 		else t2=(t1+t2)/2;
alpar@1086
   560
// 	      bez=bez.after((t1+t2)/2);
alpar@1073
   561
	      os << _edgeWidths[*e]*_edgeWidthScale << " setlinewidth "
alpar@1073
   562
		 << _edgeColors[*e].getR() << ' '
alpar@1073
   563
		 << _edgeColors[*e].getG() << ' '
alpar@1073
   564
		 << _edgeColors[*e].getB() << " setrgbcolor newpath\n"
alpar@1073
   565
		 << bez.p1.x << ' ' <<  bez.p1.y << " moveto\n"
alpar@1073
   566
		 << bez.p2.x << ' ' << bez.p2.y << ' '
alpar@1073
   567
		 << bez.p3.x << ' ' << bez.p3.y << ' '
alpar@1073
   568
		 << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
alpar@1073
   569
	      xy<double> dd(rot(linend-apoint));
alpar@1089
   570
	      dd*=(.5*_edgeWidths[*e]*_edgeWidthScale+_arrowWidth)/
alpar@1073
   571
		sqrt(dd.normSquare());
alpar@1073
   572
	      os << "newpath " << psOut(apoint) << " moveto "
alpar@1073
   573
		 << psOut(linend+dd) << " lineto "
alpar@1073
   574
		 << psOut(linend-dd) << " lineto closepath fill\n";
alpar@1073
   575
	    }
alpar@1073
   576
	    else {
alpar@1073
   577
	      os << _coords[g.source(*e)].x << ' '
alpar@1073
   578
		 << _coords[g.source(*e)].y << ' '
alpar@1085
   579
		 << mm.x << ' ' << mm.y << ' '
alpar@1073
   580
		 << _coords[g.target(*e)].x << ' '
alpar@1073
   581
		 << _coords[g.target(*e)].y << ' '
alpar@1073
   582
		 << _edgeColors[*e].getR() << ' '
alpar@1073
   583
		 << _edgeColors[*e].getG() << ' '
alpar@1073
   584
		 << _edgeColors[*e].getB() << ' '
alpar@1073
   585
		 << _edgeWidths[*e]*_edgeWidthScale << " lb\n";
alpar@1073
   586
	    }
alpar@1073
   587
	    sw+=_edgeWidths[*e]*_edgeWidthScale/2.0+_parEdgeDist;
alpar@1073
   588
	  }
alpar@1073
   589
	}
alpar@1073
   590
      }
alpar@1073
   591
      else for(EdgeIt e(g);e!=INVALID;++e)
alpar@1073
   592
	if(!_undir||g.source(e)<g.target(e))
alpar@1073
   593
	  if(_drawArrows) {
alpar@1073
   594
	    xy<double> d(_coords[g.target(e)]-_coords[g.source(e)]);
alpar@1087
   595
	    double rn=_nodeSizes[g.target(e)]*_nodeScale;
alpar@1087
   596
	    int node_shape=_nodeShapes[g.target(e)];
alpar@1087
   597
	    double t1=0,t2=1;
alpar@1087
   598
	    for(int i=0;i<INTERPOL_PREC;++i)
alpar@1087
   599
	      if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
alpar@1087
   600
	      else t2=(t1+t2)/2;
alpar@1073
   601
	    double l=sqrt(d.normSquare());
alpar@1073
   602
	    d/=l;
alpar@1087
   603
	    
alpar@1087
   604
	    os << l*(1-(t1+t2)/2) << ' '
alpar@1073
   605
	       << _edgeWidths[e]*_edgeWidthScale << ' '
alpar@1073
   606
	       << d.x << ' ' << d.y << ' '
alpar@1087
   607
	       << _coords[g.source(e)].x << ' '
alpar@1087
   608
	       << _coords[g.source(e)].y << ' '
alpar@1073
   609
	       << _edgeColors[e].getR() << ' '
alpar@1073
   610
	       << _edgeColors[e].getG() << ' '
alpar@1073
   611
	       << _edgeColors[e].getB() << " arr\n";
alpar@1073
   612
	  }
alpar@1073
   613
	  else os << _coords[g.source(e)].x << ' '
alpar@1073
   614
		  << _coords[g.source(e)].y << ' '
alpar@1073
   615
		  << _coords[g.target(e)].x << ' '
alpar@1073
   616
		  << _coords[g.target(e)].y << ' '
alpar@1073
   617
		  << _edgeColors[e].getR() << ' '
alpar@1073
   618
		  << _edgeColors[e].getG() << ' '
alpar@1073
   619
		  << _edgeColors[e].getB() << ' '
alpar@1073
   620
		  << _edgeWidths[e]*_edgeWidthScale << " l\n";
alpar@1085
   621
      os << "grestore\n";
alpar@1085
   622
    }
alpar@1085
   623
    if(_showNodes) {
alpar@1085
   624
      os << "%Nodes:\ngsave\n";
alpar@1086
   625
      for(NodeIt n(g);n!=INVALID;++n) {
alpar@1073
   626
	os << _coords[n].x << ' ' << _coords[n].y << ' '
alpar@1073
   627
	   << _nodeSizes[n]*_nodeScale << ' '
alpar@1073
   628
	   << _nodeColors[n].getR() << ' '
alpar@1073
   629
	   << _nodeColors[n].getG() << ' '
alpar@1086
   630
	   << _nodeColors[n].getB() << ' ';
alpar@1086
   631
	switch(_nodeShapes[n]) {
alpar@1086
   632
	case CIRCLE:
alpar@1086
   633
	  os<< "nc";break;
alpar@1086
   634
	case SQUARE:
alpar@1086
   635
	  os<< "nsq";break;
alpar@1088
   636
	case DIAMOND:
alpar@1088
   637
	  os<< "ndi";break;
alpar@1086
   638
	}
alpar@1086
   639
	os<<'\n';
alpar@1086
   640
      }
alpar@1085
   641
      os << "grestore\n";
alpar@1085
   642
    }
alpar@1073
   643
    if(_showNodeText) {
alpar@1085
   644
      os << "%Node texts:\ngsave\n";
alpar@1073
   645
      os << "/fosi " << _nodeTextSize << " def\n";
alpar@1073
   646
      os << "(Helvetica) findfont fosi scalefont setfont\n";
alpar@1073
   647
      os << "0 0 0 setrgbcolor\n";
alpar@1073
   648
      for(NodeIt n(g);n!=INVALID;++n)
alpar@1073
   649
	os << _coords[n].x << ' ' << _coords[n].y
alpar@1073
   650
	   << " (" << _nodeTexts[n] << ") cshow\n";
alpar@1085
   651
      os << "grestore\n";
alpar@1073
   652
    }
alpar@1085
   653
    if(_showNodePsText) {
alpar@1085
   654
      os << "%Node PS blocks:\ngsave\n";
alpar@1085
   655
      for(NodeIt n(g);n!=INVALID;++n)
alpar@1085
   656
	os << _coords[n].x << ' ' << _coords[n].y
alpar@1085
   657
	   << " moveto\n" << _nodePsTexts[n] << "\n";
alpar@1085
   658
      os << "grestore\n";
alpar@1085
   659
    }
alpar@1085
   660
    
alpar@1085
   661
    os << "grestore\n";
alpar@1073
   662
alpar@1073
   663
    //CleanUp:
alpar@1073
   664
    if(_pleaseRemoveOsStream) {delete &os;}
alpar@1073
   665
  } 
alpar@1073
   666
};
alpar@1073
   667
alpar@1073
   668
alpar@1073
   669
///Generates an EPS file from a graph
alpar@1073
   670
alpar@1073
   671
///\ingroup misc
alpar@1073
   672
///Generates an EPS file from a graph.
alpar@1073
   673
///\param g is a reference to the graph to be printed
alpar@1073
   674
///\param os is a reference to the output stream.
alpar@1073
   675
///By default it is <tt>std::cout</tt>
alpar@1073
   676
///
alpar@1091
   677
///This function also has a lot of
alpar@1091
   678
///\ref named-templ-func-param "named parameters",
alpar@1073
   679
///they are declared as the members of class \ref GraphToEps. The following
alpar@1073
   680
///example shows how to use these parameters.
alpar@1073
   681
///\code
alpar@1073
   682
/// graphToEps(g).scale(10).coords(coords)
alpar@1073
   683
///              .nodeScale(2).nodeSizes(sizes)
alpar@1091
   684
///              .edgeWidthScale(.4).run();
alpar@1073
   685
///\endcode
alpar@1091
   686
///\warning Don't forget to put the \ref GraphToEps::run() "run()"
alpar@1091
   687
///to the end of the parameter list.
alpar@1073
   688
///\sa GraphToEps
alpar@1073
   689
///\sa graphToEps(G &g, char *file_name)
alpar@1073
   690
template<class G>
alpar@1073
   691
GraphToEps<DefaultGraphToEpsTraits<G> > 
alpar@1073
   692
graphToEps(G &g, std::ostream& os=std::cout)
alpar@1073
   693
{
alpar@1073
   694
  return 
alpar@1073
   695
    GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
alpar@1073
   696
}
alpar@1073
   697
 
alpar@1073
   698
///Generates an EPS file from a graph
alpar@1073
   699
alpar@1073
   700
//\ingroup misc
alpar@1073
   701
///This function does the same as
alpar@1073
   702
///\ref graphToEps(G &g,std::ostream& os)
alpar@1073
   703
///but it writes its output into the file \c file_name
alpar@1073
   704
///instead of a stream.
alpar@1073
   705
///\sa graphToEps(G &g, std::ostream& os)
alpar@1073
   706
template<class G>
alpar@1073
   707
GraphToEps<DefaultGraphToEpsTraits<G> > 
alpar@1073
   708
graphToEps(G &g,char *file_name)
alpar@1073
   709
{
alpar@1073
   710
  return GraphToEps<DefaultGraphToEpsTraits<G> >
alpar@1073
   711
    (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true));
alpar@1073
   712
}
alpar@1073
   713
alpar@1073
   714
} //END OF NAMESPACE LEMON
alpar@1073
   715
alpar@1073
   716
#endif // LEMON_GRAPH_TO_EPS_H