Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

graph_to_eps.h

Go to the documentation of this file.
00001 /* -*- C++ -*-
00002  * lemon/graph_to_eps.h - Part of LEMON, a generic C++ optimization library
00003  *
00004  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
00005  * (Egervary Research Group on Combinatorial Optimization, EGRES).
00006  *
00007  * Permission to use, modify and distribute this software is granted
00008  * provided that this copyright notice appears in all copies. For
00009  * precise terms see the accompanying LICENSE file.
00010  *
00011  * This software is provided "AS IS" with no warranty of any kind,
00012  * express or implied, and with no claim as to its suitability for any
00013  * purpose.
00014  *
00015  */
00016 
00017 #ifndef LEMON_GRAPH_TO_EPS_H
00018 #define LEMON_GRAPH_TO_EPS_H
00019 
00020 #include <sys/time.h>
00021 
00022 #include<iostream>
00023 #include<fstream>
00024 #include<sstream>
00025 #include<algorithm>
00026 #include<vector>
00027 
00028 #include <ctime>
00029 #include <cmath>
00030 
00031 #include<lemon/invalid.h>
00032 #include<lemon/xy.h>
00033 #include<lemon/maps.h>
00034 #include<lemon/bezier.h>
00035 
00036 
00042 
00043 namespace lemon {
00044 
00046 
00049 class Color
00050 {
00051   double _r,_g,_b;
00052 public:
00054   Color() {}
00056   Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
00058   double & red() {return _r;}
00060   const double & red() const {return _r;}
00062   double & green() {return _g;}
00064   const double & green() const {return _g;}
00066   double & blue() {return _b;}
00068   const double & blue() const {return _b;}
00070   void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
00071 };
00072 
00074 
00081 
00082 class ColorSet : public MapBase<int,Color>
00083 {
00084   std::vector<Color> colors;
00085 public:
00087 
00097   ColorSet(bool have_white=false,int num=0)
00098   {
00099     do {
00100       if(have_white) colors.push_back(Color(1,1,1));
00101 
00102       colors.push_back(Color(0,0,0));
00103       colors.push_back(Color(1,0,0));
00104       colors.push_back(Color(0,1,0));
00105       colors.push_back(Color(0,0,1));
00106       colors.push_back(Color(1,1,0));
00107       colors.push_back(Color(1,0,1));
00108       colors.push_back(Color(0,1,1));
00109       
00110       colors.push_back(Color(.5,0,0));
00111       colors.push_back(Color(0,.5,0));
00112       colors.push_back(Color(0,0,.5));
00113       colors.push_back(Color(.5,.5,0));
00114       colors.push_back(Color(.5,0,.5));
00115       colors.push_back(Color(0,.5,.5));
00116       
00117       colors.push_back(Color(.5,.5,.5));
00118       colors.push_back(Color(1,.5,.5));
00119       colors.push_back(Color(.5,1,.5));
00120       colors.push_back(Color(.5,.5,1));
00121       colors.push_back(Color(1,1,.5));
00122       colors.push_back(Color(1,.5,1));
00123       colors.push_back(Color(.5,1,1));
00124       
00125       colors.push_back(Color(1,.5,0));
00126       colors.push_back(Color(.5,1,0));
00127       colors.push_back(Color(1,0,.5));
00128       colors.push_back(Color(0,1,.5));
00129       colors.push_back(Color(0,.5,1));
00130       colors.push_back(Color(.5,0,1));
00131     } while(int(colors.size())<num);
00132     //    colors.push_back(Color(1,1,1));
00133     if(num>0) colors.resize(num);
00134   }
00136   Color &operator[](int i)
00137   {
00138     return colors[i%colors.size()];
00139   }
00141   const Color &operator[](int i) const
00142   {
00143     return colors[i%colors.size()];
00144   }
00146   void set(int i,const Color &c)
00147   {
00148     colors[i%colors.size()]=c;
00149   }
00151   void resize(int s) { colors.resize(s);}
00153   std::size_t size() const { return colors.size();}
00154 };
00155 
00157 
00160 inline Color distantColor(const Color &c) 
00161 {
00162   return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0);
00163 }
00165 
00167 inline Color distantBW(const Color &c){
00168   double v=(.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5?1:0;
00169   return Color(v,v,v);
00170 }
00171 
00173 
00177 template<class G>
00178 struct DefaultGraphToEpsTraits
00179 {
00180   typedef G Graph;
00181   typedef typename Graph::Node Node;
00182   typedef typename Graph::NodeIt NodeIt;
00183   typedef typename Graph::Edge Edge;
00184   typedef typename Graph::EdgeIt EdgeIt;
00185   typedef typename Graph::InEdgeIt InEdgeIt;
00186   typedef typename Graph::OutEdgeIt OutEdgeIt;
00187   
00188 
00189   const Graph &g;
00190 
00191   std::ostream& os;
00192   
00193   ConstMap<typename Graph::Node,xy<double> > _coords;
00194   ConstMap<typename Graph::Node,double > _nodeSizes;
00195   ConstMap<typename Graph::Node,int > _nodeShapes;
00196 
00197   ConstMap<typename Graph::Node,Color > _nodeColors;
00198   ConstMap<typename Graph::Edge,Color > _edgeColors;
00199 
00200   ConstMap<typename Graph::Edge,double > _edgeWidths;
00201 
00202   double _edgeWidthScale;
00203   
00204   double _nodeScale;
00205   double _xBorder, _yBorder;
00206   double _scale;
00207   double _nodeBorderQuotient;
00208   
00209   bool _drawArrows;
00210   double _arrowLength, _arrowWidth;
00211   
00212   bool _showNodes, _showEdges;
00213 
00214   bool _enableParallel;
00215   double _parEdgeDist;
00216 
00217   bool _showNodeText;
00218   ConstMap<typename Graph::Node,bool > _nodeTexts;  
00219   double _nodeTextSize;
00220 
00221   bool _showNodePsText;
00222   ConstMap<typename Graph::Node,bool > _nodePsTexts;  
00223   char *_nodePsTextsPreamble;
00224   
00225   bool _undir;
00226   bool _pleaseRemoveOsStream;
00227 
00228   bool _scaleToA4;
00229 
00230   std::string _title;
00231   std::string _copyright;
00232 
00233   enum NodeTextColorType 
00234     { DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType;
00235   ConstMap<typename Graph::Node,Color > _nodeTextColors;
00236 
00237   bool _autoNodeScale;
00238   bool _autoEdgeWidthScale;
00239 
00241 
00249   DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
00250                           bool _pros=false) :
00251     g(_g), os(_os),
00252     _coords(xy<double>(1,1)), _nodeSizes(1.0), _nodeShapes(0),
00253     _nodeColors(Color(1,1,1)), _edgeColors(Color(0,0,0)),
00254     _edgeWidths(1), _edgeWidthScale(0.3),
00255     _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0),
00256     _nodeBorderQuotient(.1),
00257     _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
00258     _showNodes(true), _showEdges(true),
00259     _enableParallel(false), _parEdgeDist(1),
00260     _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
00261     _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
00262     _undir(false),
00263     _pleaseRemoveOsStream(_pros), _scaleToA4(false),
00264     _nodeTextColorType(SAME_COL), _nodeTextColors(Color(0,0,0)),
00265     _autoNodeScale(false),
00266     _autoEdgeWidthScale(false)
00267   {}
00268 };
00269 
00271 
00280 template<class T> class GraphToEps : public T 
00281 {
00282   // Can't believe it is required by the C++ standard
00283   using T::g;
00284   using T::os;
00285 
00286   using T::_coords;
00287   using T::_nodeSizes;
00288   using T::_nodeShapes;
00289   using T::_nodeColors;
00290   using T::_edgeColors;
00291   using T::_edgeWidths;
00292 
00293   using T::_edgeWidthScale;
00294   using T::_nodeScale;
00295   using T::_xBorder;
00296   using T::_yBorder;
00297   using T::_scale;
00298   using T::_nodeBorderQuotient;
00299   
00300   using T::_drawArrows;
00301   using T::_arrowLength;
00302   using T::_arrowWidth;
00303   
00304   using T::_showNodes;
00305   using T::_showEdges;
00306 
00307   using T::_enableParallel;
00308   using T::_parEdgeDist;
00309 
00310   using T::_showNodeText;
00311   using T::_nodeTexts;  
00312   using T::_nodeTextSize;
00313 
00314   using T::_showNodePsText;
00315   using T::_nodePsTexts;  
00316   using T::_nodePsTextsPreamble;
00317   
00318   using T::_undir;
00319   using T::_pleaseRemoveOsStream;
00320 
00321   using T::_scaleToA4;
00322 
00323   using T::_title;
00324   using T::_copyright;
00325 
00326   using T::NodeTextColorType;
00327   using T::CUST_COL;
00328   using T::DIST_COL;
00329   using T::DIST_BW;
00330   using T::_nodeTextColorType;
00331   using T::_nodeTextColors;
00332 
00333   using T::_autoNodeScale;
00334   using T::_autoEdgeWidthScale;
00335 
00336   // dradnats ++C eht yb deriuqer si ti eveileb t'naC
00337 
00338   typedef typename T::Graph Graph;
00339   typedef typename Graph::Node Node;
00340   typedef typename Graph::NodeIt NodeIt;
00341   typedef typename Graph::Edge Edge;
00342   typedef typename Graph::EdgeIt EdgeIt;
00343   typedef typename Graph::InEdgeIt InEdgeIt;
00344   typedef typename Graph::OutEdgeIt OutEdgeIt;
00345 
00346   static const int INTERPOL_PREC;
00347   static const double A4HEIGHT;
00348   static const double A4WIDTH;
00349   static const double A4BORDER;
00350 
00351   bool dontPrint;
00352 
00353 public:
00355 
00358   enum NodeShapes { 
00362     CIRCLE=0, 
00367     SQUARE=1, 
00372     DIAMOND=2
00373   };
00374 
00375 private:
00376   class edgeLess {
00377     const Graph &g;
00378   public:
00379     edgeLess(const Graph &_g) : g(_g) {}
00380     bool operator()(Edge a,Edge b) const 
00381     {
00382       Node ai=std::min(g.source(a),g.target(a));
00383       Node aa=std::max(g.source(a),g.target(a));
00384       Node bi=std::min(g.source(b),g.target(b));
00385       Node ba=std::max(g.source(b),g.target(b));
00386       return ai<bi ||
00387         (ai==bi && (aa < ba || 
00388                     (aa==ba && ai==g.source(a) && bi==g.target(b))));
00389     }
00390   };
00391   bool isParallel(Edge e,Edge f) const
00392   {
00393     return (g.source(e)==g.source(f)&&
00394             g.target(e)==g.target(f)) ||
00395       (g.source(e)==g.target(f)&&
00396        g.target(e)==g.source(f));
00397   }
00398   template<class TT>
00399   static std::string psOut(const xy<TT> &p) 
00400     {
00401       std::ostringstream os;    
00402       os << p.x << ' ' << p.y;
00403       return os.str();
00404     }
00405   static std::string psOut(const Color &c) 
00406     {
00407       std::ostringstream os;    
00408       os << c.red() << ' ' << c.green() << ' ' << c.blue();
00409       return os.str();
00410     }
00411   
00412 public:
00413   GraphToEps(const T &t) : T(t), dontPrint(false) {};
00414   
00415   template<class X> struct CoordsTraits : public T {
00416     const X &_coords;
00417     CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
00418   };
00420 
00423   template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
00424     dontPrint=true;
00425     return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
00426   }
00427   template<class X> struct NodeSizesTraits : public T {
00428     const X &_nodeSizes;
00429     NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
00430   };
00432 
00435   template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
00436   {
00437     dontPrint=true;
00438     return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
00439   }
00440   template<class X> struct NodeShapesTraits : public T {
00441     const X &_nodeShapes;
00442     NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
00443   };
00445 
00451   template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
00452   {
00453     dontPrint=true;
00454     return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
00455   }
00456   template<class X> struct NodeTextsTraits : public T {
00457     const X &_nodeTexts;
00458     NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
00459   };
00461 
00465   template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
00466   {
00467     dontPrint=true;
00468     _showNodeText=true;
00469     return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
00470   }
00471   template<class X> struct NodePsTextsTraits : public T {
00472     const X &_nodePsTexts;
00473     NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
00474   };
00476 
00491   template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
00492   {
00493     dontPrint=true;
00494     _showNodePsText=true;
00495     return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
00496   }
00497   template<class X> struct EdgeWidthsTraits : public T {
00498     const X &_edgeWidths;
00499     EdgeWidthsTraits(const T &t,const X &x) : T(t), _edgeWidths(x) {}
00500   };
00502 
00505   template<class X> GraphToEps<EdgeWidthsTraits<X> > edgeWidths(const X &x)
00506   {
00507     dontPrint=true;
00508     return GraphToEps<EdgeWidthsTraits<X> >(EdgeWidthsTraits<X>(*this,x));
00509   }
00510 
00511   template<class X> struct NodeColorsTraits : public T {
00512     const X &_nodeColors;
00513     NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
00514   };
00516 
00521   template<class X> GraphToEps<NodeColorsTraits<X> >
00522   nodeColors(const X &x)
00523   {
00524     dontPrint=true;
00525     return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
00526   }
00527   template<class X> struct NodeTextColorsTraits : public T {
00528     const X &_nodeTextColors;
00529     NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {}
00530   };
00532 
00537   template<class X> GraphToEps<NodeTextColorsTraits<X> >
00538   nodeTextColors(const X &x)
00539   {
00540     dontPrint=true;
00541     _nodeTextColorType=CUST_COL;
00542     return GraphToEps<NodeTextColorsTraits<X> >
00543       (NodeTextColorsTraits<X>(*this,x));
00544   }
00545   template<class X> struct EdgeColorsTraits : public T {
00546     const X &_edgeColors;
00547     EdgeColorsTraits(const T &t,const X &x) : T(t), _edgeColors(x) {}
00548   };
00550 
00555   template<class X> GraphToEps<EdgeColorsTraits<X> >
00556   edgeColors(const X &x)
00557   {
00558     dontPrint=true;
00559     return GraphToEps<EdgeColorsTraits<X> >(EdgeColorsTraits<X>(*this,x));
00560   }
00562 
00572   GraphToEps<T> &nodeScale(double d) {_nodeScale=d;return *this;}
00574 
00579   GraphToEps<T> &autoNodeScale(bool b=true) {
00580     _autoNodeScale=b;return *this;
00581   }
00583 
00593   GraphToEps<T> &edgeWidthScale(double d) {_edgeWidthScale=d;return *this;}
00595 
00600   GraphToEps<T> &autoEdgeWidthScale(bool b=true) {
00601     _autoEdgeWidthScale=b;return *this;
00602   }
00604 
00607 
00608   GraphToEps<T> &scale(double d) {_scale=d;return *this;}
00610 
00613   GraphToEps<T> &border(double b) {_xBorder=_yBorder=b;return *this;}
00615 
00618   GraphToEps<T> &border(double x, double y) {
00619     _xBorder=x;_yBorder=y;return *this;
00620   }
00622 
00625   GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
00627 
00630   GraphToEps<T> &arrowLength(double d) {_arrowLength*=d;return *this;}
00632 
00635   GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;}
00636   
00638 
00641   GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
00642   
00644 
00646   GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
00647   
00649   
00652   GraphToEps<T> &parEdgeDist(double d) {_parEdgeDist*=d;return *this;}
00653   
00655   
00658   GraphToEps<T> &hideEdges(bool b=true) {_showEdges=!b;return *this;}
00660   
00663   GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
00664   
00666   
00669   GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
00670 
00672 
00676   GraphToEps<T> &distantColorNodeTexts()
00677   {_nodeTextColorType=DIST_COL;return *this;}
00679 
00684   GraphToEps<T> &distantBWNodeTexts()
00685   {_nodeTextColorType=DIST_BW;return *this;}
00686 
00688   
00692   GraphToEps<T> & nodePsTextsPreamble(const char *str) {
00693     _nodePsTextsPreamble=str ;return *this;
00694   }
00696 
00699   GraphToEps<T> &undir(bool b=true) {_undir=b;return *this;}
00701 
00704   GraphToEps<T> &bidir(bool b=true) {_undir=!b;return *this;}
00705 
00707 
00711   GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
00713 
00718   GraphToEps<T> &copyright(const std::string &t) {_copyright=t;return *this;}
00719 
00720 protected:
00721   bool isInsideNode(xy<double> p, double r,int t) 
00722   {
00723     switch(t) {
00724     case CIRCLE:
00725       return p.normSquare()<=r*r;
00726     case SQUARE:
00727       return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
00728     case DIAMOND:
00729       return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
00730     }
00731     return false;
00732   }
00733 
00734 public:
00735   ~GraphToEps() { }
00736   
00738 
00743   void run() {
00744     if(dontPrint) return;
00745     
00746     os << "%!PS-Adobe-2.0 EPSF-2.0\n";
00747     if(_title.size()>0) os << "%%Title: " << _title << '\n';
00748      if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
00749 //        << "%%Copyright: XXXX\n"
00750     os << "%%Creator: LEMON, graphToEps()\n";
00751     
00752     {
00753       char cbuf[50];
00754       timeval tv;
00755       gettimeofday(&tv, 0);
00756       ctime_r(&tv.tv_sec,cbuf);
00757       os << "%%CreationDate: " << cbuf;
00758     }
00759 
00760     if (_autoEdgeWidthScale) {
00761       double max_w=0;
00762       for(EdgeIt e(g);e!=INVALID;++e)
00763         max_w=std::max(double(_edgeWidths[e]),max_w);
00765       if(max_w>1e-9) {
00766         _edgeWidthScale/=max_w;
00767       }
00768     }
00769 
00770     if (_autoNodeScale) {
00771       double max_s=0;
00772       for(NodeIt n(g);n!=INVALID;++n)
00773         max_s=std::max(double(_nodeSizes[n]),max_s);
00775       if(max_s>1e-9) {
00776         _nodeScale/=max_s;
00777       }
00778     }
00779 
00780 
00781     BoundingBox<double> bb;
00783     for(NodeIt n(g);n!=INVALID;++n) {
00784       double ns=_nodeSizes[n]*_nodeScale;
00785       xy<double> p(ns,ns);
00786       bb.add(p+_coords[n]);
00787       bb.add(-p+_coords[n]);
00788     }
00789     if (bb.empty()) {
00790       bb = BoundingBox<double>(xy<double>(0,0));
00791     }
00792     
00793     if(_scaleToA4)
00794       os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
00795     else os << "%%BoundingBox: "
00796             << bb.left()   * _scale - _xBorder << ' '
00797             << bb.bottom() * _scale - _yBorder << ' '
00798             << bb.right()  * _scale + _xBorder << ' '
00799             << bb.top()    * _scale + _yBorder << '\n';
00800     
00801     os << "%%EndComments\n";
00802     
00803     //x1 y1 x2 y2 x3 y3 cr cg cb w
00804     os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
00805        << "      4 2 roll 1 index 1 index curveto stroke } bind def\n";
00806     os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
00807     //x y r
00808     os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n";
00809     //x y r
00810     os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
00811        << "      2 index 1 index sub 2 index 2 index add lineto\n"
00812        << "      2 index 1 index sub 2 index 2 index sub lineto\n"
00813        << "      2 index 1 index add 2 index 2 index sub lineto\n"
00814        << "      closepath pop pop pop} bind def\n";
00815     //x y r
00816     os << "/di { newpath 2 index 1 index add 2 index moveto\n"
00817        << "      2 index             2 index 2 index add lineto\n"
00818        << "      2 index 1 index sub 2 index             lineto\n"
00819        << "      2 index             2 index 2 index sub lineto\n"
00820        << "      closepath pop pop pop} bind def\n";
00821     // x y r cr cg cb
00822     os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
00823        << "     setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
00824        << "   } bind def\n";
00825     os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
00826        << "     setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
00827        << "   } bind def\n";
00828     os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
00829        << "     setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
00830        << "   } bind def\n";
00831     os << "/arrl " << _arrowLength << " def\n";
00832     os << "/arrw " << _arrowWidth << " def\n";
00833     // l dx_norm dy_norm
00834     os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
00835     //len w dx_norm dy_norm x1 y1 cr cg cb
00836     os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
00837        << "       /w exch def /len exch def\n"
00838       //         << "       0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
00839        << "       newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
00840        << "       len w sub arrl sub dx dy lrl\n"
00841        << "       arrw dy dx neg lrl\n"
00842        << "       dx arrl w add mul dy w 2 div arrw add mul sub\n"
00843        << "       dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
00844        << "       dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
00845        << "       dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
00846        << "       arrw dy dx neg lrl\n"
00847        << "       len w sub arrl sub neg dx dy lrl\n"
00848        << "       closepath fill } bind def\n";
00849     os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
00850        << "         neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
00851 
00852     os << "\ngsave\n";
00853     if(_scaleToA4)
00854       if(bb.height()>bb.width()) {
00855         double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(),
00856                   (A4WIDTH-2*A4BORDER)/bb.width());
00857         os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
00858            << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER << " translate\n"
00859            << sc << " dup scale\n"
00860            << -bb.left() << ' ' << -bb.bottom() << " translate\n";
00861       }
00862       else {
00863         //\todo Verify centering
00864         double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(),
00865                   (A4WIDTH-2*A4BORDER)/bb.height());
00866         os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
00867            << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER  << " translate\n"
00868            << sc << " dup scale\n90 rotate\n"
00869            << -bb.left() << ' ' << -bb.top() << " translate\n"; 
00870         }
00871     else if(_scale!=1.0) os << _scale << " dup scale\n";
00872     
00873     if(_showEdges) {
00874       os << "%Edges:\ngsave\n";      
00875       if(_enableParallel) {
00876         std::vector<Edge> el;
00877         for(EdgeIt e(g);e!=INVALID;++e)
00878           if((!_undir||g.source(e)<g.target(e))&&_edgeWidths[e]>0)
00879             el.push_back(e);
00880         std::sort(el.begin(),el.end(),edgeLess(g));
00881         
00882         typename std::vector<Edge>::iterator j;
00883         for(typename std::vector<Edge>::iterator i=el.begin();i!=el.end();i=j) {
00884           for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
00885 
00886           double sw=0;
00887           for(typename std::vector<Edge>::iterator e=i;e!=j;++e)
00888             sw+=_edgeWidths[*e]*_edgeWidthScale+_parEdgeDist;
00889           sw-=_parEdgeDist;
00890           sw/=-2.0;
00891           xy<double> dvec(_coords[g.target(*i)]-_coords[g.source(*i)]);
00892           double l=std::sqrt(dvec.normSquare()); 
00894           xy<double> d(dvec/std::max(l,1e-9));
00895           xy<double> m;
00896 //        m=xy<double>(_coords[g.target(*i)]+_coords[g.source(*i)])/2.0;
00897 
00898 //        m=xy<double>(_coords[g.source(*i)])+
00899 //          dvec*(double(_nodeSizes[g.source(*i)])/
00900 //             (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
00901 
00902           m=xy<double>(_coords[g.source(*i)])+
00903             d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
00904 
00905           for(typename std::vector<Edge>::iterator e=i;e!=j;++e) {
00906             sw+=_edgeWidths[*e]*_edgeWidthScale/2.0;
00907             xy<double> mm=m+rot90(d)*sw/.75;
00908             if(_drawArrows) {
00909               int node_shape;
00910               xy<double> s=_coords[g.source(*e)];
00911               xy<double> t=_coords[g.target(*e)];
00912               double rn=_nodeSizes[g.target(*e)]*_nodeScale;
00913               node_shape=_nodeShapes[g.target(*e)];
00914               Bezier3 bez(s,mm,mm,t);
00915               double t1=0,t2=1;
00916               for(int i=0;i<INTERPOL_PREC;++i)
00917                 if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
00918                 else t1=(t1+t2)/2;
00919               xy<double> apoint=bez((t1+t2)/2);
00920               rn = _arrowLength+_edgeWidths[*e]*_edgeWidthScale;
00921               rn*=rn;
00922               t2=(t1+t2)/2;t1=0;
00923               for(int i=0;i<INTERPOL_PREC;++i)
00924                 if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
00925                 else t2=(t1+t2)/2;
00926               xy<double> linend=bez((t1+t2)/2);       
00927               bez=bez.before((t1+t2)/2);
00928 //            rn=_nodeSizes[g.source(*e)]*_nodeScale;
00929 //            node_shape=_nodeShapes[g.source(*e)];
00930 //            t1=0;t2=1;
00931 //            for(int i=0;i<INTERPOL_PREC;++i)
00932 //              if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t1=(t1+t2)/2;
00933 //              else t2=(t1+t2)/2;
00934 //            bez=bez.after((t1+t2)/2);
00935               os << _edgeWidths[*e]*_edgeWidthScale << " setlinewidth "
00936                  << _edgeColors[*e].red() << ' '
00937                  << _edgeColors[*e].green() << ' '
00938                  << _edgeColors[*e].blue() << " setrgbcolor newpath\n"
00939                  << bez.p1.x << ' ' <<  bez.p1.y << " moveto\n"
00940                  << bez.p2.x << ' ' << bez.p2.y << ' '
00941                  << bez.p3.x << ' ' << bez.p3.y << ' '
00942                  << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
00943               xy<double> dd(rot90(linend-apoint));
00944               dd*=(.5*_edgeWidths[*e]*_edgeWidthScale+_arrowWidth)/
00945                 std::sqrt(dd.normSquare());
00946               os << "newpath " << psOut(apoint) << " moveto "
00947                  << psOut(linend+dd) << " lineto "
00948                  << psOut(linend-dd) << " lineto closepath fill\n";
00949             }
00950             else {
00951               os << _coords[g.source(*e)].x << ' '
00952                  << _coords[g.source(*e)].y << ' '
00953                  << mm.x << ' ' << mm.y << ' '
00954                  << _coords[g.target(*e)].x << ' '
00955                  << _coords[g.target(*e)].y << ' '
00956                  << _edgeColors[*e].red() << ' '
00957                  << _edgeColors[*e].green() << ' '
00958                  << _edgeColors[*e].blue() << ' '
00959                  << _edgeWidths[*e]*_edgeWidthScale << " lb\n";
00960             }
00961             sw+=_edgeWidths[*e]*_edgeWidthScale/2.0+_parEdgeDist;
00962           }
00963         }
00964       }
00965       else for(EdgeIt e(g);e!=INVALID;++e)
00966         if((!_undir||g.source(e)<g.target(e))&&_edgeWidths[e]>0)
00967           if(_drawArrows) {
00968             xy<double> d(_coords[g.target(e)]-_coords[g.source(e)]);
00969             double rn=_nodeSizes[g.target(e)]*_nodeScale;
00970             int node_shape=_nodeShapes[g.target(e)];
00971             double t1=0,t2=1;
00972             for(int i=0;i<INTERPOL_PREC;++i)
00973               if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
00974               else t2=(t1+t2)/2;
00975             double l=sqrt(d.normSquare());
00976             d/=l;
00977             
00978             os << l*(1-(t1+t2)/2) << ' '
00979                << _edgeWidths[e]*_edgeWidthScale << ' '
00980                << d.x << ' ' << d.y << ' '
00981                << _coords[g.source(e)].x << ' '
00982                << _coords[g.source(e)].y << ' '
00983                << _edgeColors[e].red() << ' '
00984                << _edgeColors[e].green() << ' '
00985                << _edgeColors[e].blue() << " arr\n";
00986           }
00987           else os << _coords[g.source(e)].x << ' '
00988                   << _coords[g.source(e)].y << ' '
00989                   << _coords[g.target(e)].x << ' '
00990                   << _coords[g.target(e)].y << ' '
00991                   << _edgeColors[e].red() << ' '
00992                   << _edgeColors[e].green() << ' '
00993                   << _edgeColors[e].blue() << ' '
00994                   << _edgeWidths[e]*_edgeWidthScale << " l\n";
00995       os << "grestore\n";
00996     }
00997     if(_showNodes) {
00998       os << "%Nodes:\ngsave\n";
00999       for(NodeIt n(g);n!=INVALID;++n) {
01000         os << _coords[n].x << ' ' << _coords[n].y << ' '
01001            << _nodeSizes[n]*_nodeScale << ' '
01002            << _nodeColors[n].red() << ' '
01003            << _nodeColors[n].green() << ' '
01004            << _nodeColors[n].blue() << ' ';
01005         switch(_nodeShapes[n]) {
01006         case CIRCLE:
01007           os<< "nc";break;
01008         case SQUARE:
01009           os<< "nsq";break;
01010         case DIAMOND:
01011           os<< "ndi";break;
01012         }
01013         os<<'\n';
01014       }
01015       os << "grestore\n";
01016     }
01017     if(_showNodeText) {
01018       os << "%Node texts:\ngsave\n";
01019       os << "/fosi " << _nodeTextSize << " def\n";
01020       os << "(Helvetica) findfont fosi scalefont setfont\n";
01021       for(NodeIt n(g);n!=INVALID;++n) {
01022         switch(_nodeTextColorType) {
01023         case DIST_COL:
01024           os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n";
01025           break;
01026         case DIST_BW:
01027           os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n";
01028           break;
01029         case CUST_COL:
01030           os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n";
01031           break;
01032         default:
01033           os << "0 0 0 setrgbcolor\n";
01034         }
01035         os << _coords[n].x << ' ' << _coords[n].y
01036            << " (" << _nodeTexts[n] << ") cshow\n";
01037       }
01038       os << "grestore\n";
01039     }
01040     if(_showNodePsText) {
01041       os << "%Node PS blocks:\ngsave\n";
01042       for(NodeIt n(g);n!=INVALID;++n)
01043         os << _coords[n].x << ' ' << _coords[n].y
01044            << " moveto\n" << _nodePsTexts[n] << "\n";
01045       os << "grestore\n";
01046     }
01047     
01048     os << "grestore\nshowpage\n";
01049 
01050     //CleanUp:
01051     if(_pleaseRemoveOsStream) {delete &os;}
01052   } 
01053 };
01054 
01055 template<class T>
01056 const int GraphToEps<T>::INTERPOL_PREC = 20;
01057 template<class T>
01058 const double GraphToEps<T>::A4HEIGHT = 841.8897637795276;
01059 template<class T>
01060 const double GraphToEps<T>::A4WIDTH  = 595.275590551181;
01061 template<class T>
01062 const double GraphToEps<T>::A4BORDER = 15;
01063 
01064 
01066 
01086 template<class G>
01087 GraphToEps<DefaultGraphToEpsTraits<G> > 
01088 graphToEps(G &g, std::ostream& os=std::cout)
01089 {
01090   return 
01091     GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
01092 }
01093  
01095 
01102 template<class G>
01103 GraphToEps<DefaultGraphToEpsTraits<G> > 
01104 graphToEps(G &g,const char *file_name)
01105 {
01106   return GraphToEps<DefaultGraphToEpsTraits<G> >
01107     (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true));
01108 }
01109 
01110 } //END OF NAMESPACE LEMON
01111 
01112 #endif // LEMON_GRAPH_TO_EPS_H

Generated on Sat Aug 27 14:14:52 2005 for LEMON by  doxygen 1.4.4