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