graph_to_eps.h

Go to the documentation of this file.
00001 /* -*- C++ -*-
00002  *
00003  * This file is a part of LEMON, a generic C++ optimization library
00004  *
00005  * Copyright (C) 2003-2006
00006  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
00007  * (Egervary Research Group on Combinatorial Optimization, EGRES).
00008  *
00009  * Permission to use, modify and distribute this software is granted
00010  * provided that this copyright notice appears in all copies. For
00011  * precise terms see the accompanying LICENSE file.
00012  *
00013  * This software is provided "AS IS" with no warranty of any kind,
00014  * express or implied, and with no claim as to its suitability for any
00015  * purpose.
00016  *
00017  */
00018 
00019 #ifndef LEMON_GRAPH_TO_EPS_H
00020 #define LEMON_GRAPH_TO_EPS_H
00021 
00022 #include <sys/time.h>
00023 
00024 #include<iostream>
00025 #include<fstream>
00026 #include<sstream>
00027 #include<algorithm>
00028 #include<vector>
00029 
00030 #include <ctime>
00031 #include <cmath>
00032 
00033 #include<lemon/invalid.h>
00034 #include<lemon/xy.h>
00035 #include<lemon/maps.h>
00036 #include<lemon/bezier.h>
00037 
00038 
00044 
00045 namespace lemon {
00046 
00048 
00051 class Color
00052 {
00053   double _r,_g,_b;
00054 public:
00056   Color() {}
00058   Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
00060   double & red() {return _r;}
00062   const double & red() const {return _r;}
00064   double & green() {return _g;}
00066   const double & green() const {return _g;}
00068   double & blue() {return _b;}
00070   const double & blue() const {return _b;}
00072   void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
00073 };
00074 
00076 
00083 
00084 class ColorSet : public MapBase<int,Color>
00085 {
00086   std::vector<Color> colors;
00087 public:
00089 
00099   ColorSet(bool have_white=false,int num=0)
00100   {
00101     do {
00102       if(have_white) colors.push_back(Color(1,1,1));
00103 
00104       colors.push_back(Color(0,0,0));
00105       colors.push_back(Color(1,0,0));
00106       colors.push_back(Color(0,1,0));
00107       colors.push_back(Color(0,0,1));
00108       colors.push_back(Color(1,1,0));
00109       colors.push_back(Color(1,0,1));
00110       colors.push_back(Color(0,1,1));
00111       
00112       colors.push_back(Color(.5,0,0));
00113       colors.push_back(Color(0,.5,0));
00114       colors.push_back(Color(0,0,.5));
00115       colors.push_back(Color(.5,.5,0));
00116       colors.push_back(Color(.5,0,.5));
00117       colors.push_back(Color(0,.5,.5));
00118       
00119       colors.push_back(Color(.5,.5,.5));
00120       colors.push_back(Color(1,.5,.5));
00121       colors.push_back(Color(.5,1,.5));
00122       colors.push_back(Color(.5,.5,1));
00123       colors.push_back(Color(1,1,.5));
00124       colors.push_back(Color(1,.5,1));
00125       colors.push_back(Color(.5,1,1));
00126       
00127       colors.push_back(Color(1,.5,0));
00128       colors.push_back(Color(.5,1,0));
00129       colors.push_back(Color(1,0,.5));
00130       colors.push_back(Color(0,1,.5));
00131       colors.push_back(Color(0,.5,1));
00132       colors.push_back(Color(.5,0,1));
00133     } while(int(colors.size())<num);
00134     //    colors.push_back(Color(1,1,1));
00135     if(num>0) colors.resize(num);
00136   }
00138   Color &operator[](int i)
00139   {
00140     return colors[i%colors.size()];
00141   }
00143   const Color &operator[](int i) const
00144   {
00145     return colors[i%colors.size()];
00146   }
00148   void set(int i,const Color &c)
00149   {
00150     colors[i%colors.size()]=c;
00151   }
00153   void resize(int s) { colors.resize(s);}
00155   std::size_t size() const { return colors.size();}
00156 };
00157 
00159 
00162 inline Color distantColor(const Color &c) 
00163 {
00164   return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0);
00165 }
00167 
00169 inline Color distantBW(const Color &c){
00170   double v=(.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5?1:0;
00171   return Color(v,v,v);
00172 }
00173 
00174 template<class MT>
00175 class _NegY {
00176 public:
00177   typedef typename MT::Key Key;
00178   typedef typename MT::Value Value;
00179   const MT &map;
00180   int yscale;
00181   _NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {}
00182   Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);}
00183 };
00184 
00186 
00190 template<class G>
00191 struct DefaultGraphToEpsTraits
00192 {
00193   typedef G Graph;
00194   typedef typename Graph::Node Node;
00195   typedef typename Graph::NodeIt NodeIt;
00196   typedef typename Graph::Edge Edge;
00197   typedef typename Graph::EdgeIt EdgeIt;
00198   typedef typename Graph::InEdgeIt InEdgeIt;
00199   typedef typename Graph::OutEdgeIt OutEdgeIt;
00200   
00201 
00202   const Graph &g;
00203 
00204   std::ostream& os;
00205   
00206   typedef ConstMap<typename Graph::Node,xy<double> > CoordsMapType;
00207   CoordsMapType _coords;
00208   ConstMap<typename Graph::Node,double > _nodeSizes;
00209   ConstMap<typename Graph::Node,int > _nodeShapes;
00210 
00211   ConstMap<typename Graph::Node,Color > _nodeColors;
00212   ConstMap<typename Graph::Edge,Color > _edgeColors;
00213 
00214   ConstMap<typename Graph::Edge,double > _edgeWidths;
00215 
00216   double _edgeWidthScale;
00217   
00218   double _nodeScale;
00219   double _xBorder, _yBorder;
00220   double _scale;
00221   double _nodeBorderQuotient;
00222   
00223   bool _drawArrows;
00224   double _arrowLength, _arrowWidth;
00225   
00226   bool _showNodes, _showEdges;
00227 
00228   bool _enableParallel;
00229   double _parEdgeDist;
00230 
00231   bool _showNodeText;
00232   ConstMap<typename Graph::Node,bool > _nodeTexts;  
00233   double _nodeTextSize;
00234 
00235   bool _showNodePsText;
00236   ConstMap<typename Graph::Node,bool > _nodePsTexts;  
00237   char *_nodePsTextsPreamble;
00238   
00239   bool _undirected;
00240 
00241   bool _pleaseRemoveOsStream;
00242 
00243   bool _scaleToA4;
00244 
00245   std::string _title;
00246   std::string _copyright;
00247 
00248   enum NodeTextColorType 
00249     { DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType;
00250   ConstMap<typename Graph::Node,Color > _nodeTextColors;
00251 
00252   bool _autoNodeScale;
00253   bool _autoEdgeWidthScale;
00254 
00255   bool _negY;
00257 
00265   DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
00266                           bool _pros=false) :
00267     g(_g), os(_os),
00268     _coords(xy<double>(1,1)), _nodeSizes(1.0), _nodeShapes(0),
00269     _nodeColors(Color(1,1,1)), _edgeColors(Color(0,0,0)),
00270     _edgeWidths(1), _edgeWidthScale(0.3),
00271     _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0),
00272     _nodeBorderQuotient(.1),
00273     _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
00274     _showNodes(true), _showEdges(true),
00275     _enableParallel(false), _parEdgeDist(1),
00276     _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
00277     _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
00278     _undirected(false),
00279     _pleaseRemoveOsStream(_pros), _scaleToA4(false),
00280     _nodeTextColorType(SAME_COL), _nodeTextColors(Color(0,0,0)),
00281     _autoNodeScale(false),
00282     _autoEdgeWidthScale(false),
00283     _negY(false)
00284   {}
00285 };
00286 
00288 
00297 template<class T> class GraphToEps : public T 
00298 {
00299   // Can't believe it is required by the C++ standard
00300   using T::g;
00301   using T::os;
00302 
00303   using T::_coords;
00304   using T::_nodeSizes;
00305   using T::_nodeShapes;
00306   using T::_nodeColors;
00307   using T::_edgeColors;
00308   using T::_edgeWidths;
00309 
00310   using T::_edgeWidthScale;
00311   using T::_nodeScale;
00312   using T::_xBorder;
00313   using T::_yBorder;
00314   using T::_scale;
00315   using T::_nodeBorderQuotient;
00316   
00317   using T::_drawArrows;
00318   using T::_arrowLength;
00319   using T::_arrowWidth;
00320   
00321   using T::_showNodes;
00322   using T::_showEdges;
00323 
00324   using T::_enableParallel;
00325   using T::_parEdgeDist;
00326 
00327   using T::_showNodeText;
00328   using T::_nodeTexts;  
00329   using T::_nodeTextSize;
00330 
00331   using T::_showNodePsText;
00332   using T::_nodePsTexts;  
00333   using T::_nodePsTextsPreamble;
00334   
00335   using T::_undirected;
00336 
00337   using T::_pleaseRemoveOsStream;
00338 
00339   using T::_scaleToA4;
00340 
00341   using T::_title;
00342   using T::_copyright;
00343 
00344   using T::NodeTextColorType;
00345   using T::CUST_COL;
00346   using T::DIST_COL;
00347   using T::DIST_BW;
00348   using T::_nodeTextColorType;
00349   using T::_nodeTextColors;
00350 
00351   using T::_autoNodeScale;
00352   using T::_autoEdgeWidthScale;
00353 
00354   using T::_negY;
00355 
00356   // dradnats ++C eht yb deriuqer si ti eveileb t'naC
00357 
00358   typedef typename T::Graph Graph;
00359   typedef typename Graph::Node Node;
00360   typedef typename Graph::NodeIt NodeIt;
00361   typedef typename Graph::Edge Edge;
00362   typedef typename Graph::EdgeIt EdgeIt;
00363   typedef typename Graph::InEdgeIt InEdgeIt;
00364   typedef typename Graph::OutEdgeIt OutEdgeIt;
00365 
00366   static const int INTERPOL_PREC;
00367   static const double A4HEIGHT;
00368   static const double A4WIDTH;
00369   static const double A4BORDER;
00370 
00371   bool dontPrint;
00372 
00373 public:
00375 
00378   enum NodeShapes { 
00382     CIRCLE=0, 
00387     SQUARE=1, 
00392     DIAMOND=2,
00397     MALE=3,
00402     FEMALE=4
00403   };
00404 
00405 private:
00406   class edgeLess {
00407     const Graph &g;
00408   public:
00409     edgeLess(const Graph &_g) : g(_g) {}
00410     bool operator()(Edge a,Edge b) const 
00411     {
00412       Node ai=std::min(g.source(a),g.target(a));
00413       Node aa=std::max(g.source(a),g.target(a));
00414       Node bi=std::min(g.source(b),g.target(b));
00415       Node ba=std::max(g.source(b),g.target(b));
00416       return ai<bi ||
00417         (ai==bi && (aa < ba || 
00418                     (aa==ba && ai==g.source(a) && bi==g.target(b))));
00419     }
00420   };
00421   bool isParallel(Edge e,Edge f) const
00422   {
00423     return (g.source(e)==g.source(f)&&
00424             g.target(e)==g.target(f)) ||
00425       (g.source(e)==g.target(f)&&
00426        g.target(e)==g.source(f));
00427   }
00428   template<class TT>
00429   static std::string psOut(const xy<TT> &p) 
00430     {
00431       std::ostringstream os;    
00432       os << p.x << ' ' << p.y;
00433       return os.str();
00434     }
00435   static std::string psOut(const Color &c) 
00436     {
00437       std::ostringstream os;    
00438       os << c.red() << ' ' << c.green() << ' ' << c.blue();
00439       return os.str();
00440     }
00441   
00442 public:
00443   GraphToEps(const T &t) : T(t), dontPrint(false) {};
00444   
00445   template<class X> struct CoordsTraits : public T {
00446   typedef X CoordsMapType;
00447     const X &_coords;
00448     CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
00449   };
00451 
00454   template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
00455     dontPrint=true;
00456     return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
00457   }
00458   template<class X> struct NodeSizesTraits : public T {
00459     const X &_nodeSizes;
00460     NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
00461   };
00463 
00466   template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
00467   {
00468     dontPrint=true;
00469     return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
00470   }
00471   template<class X> struct NodeShapesTraits : public T {
00472     const X &_nodeShapes;
00473     NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
00474   };
00476 
00482   template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
00483   {
00484     dontPrint=true;
00485     return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
00486   }
00487   template<class X> struct NodeTextsTraits : public T {
00488     const X &_nodeTexts;
00489     NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
00490   };
00492 
00496   template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
00497   {
00498     dontPrint=true;
00499     _showNodeText=true;
00500     return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
00501   }
00502   template<class X> struct NodePsTextsTraits : public T {
00503     const X &_nodePsTexts;
00504     NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
00505   };
00507 
00522   template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
00523   {
00524     dontPrint=true;
00525     _showNodePsText=true;
00526     return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
00527   }
00528   template<class X> struct EdgeWidthsTraits : public T {
00529     const X &_edgeWidths;
00530     EdgeWidthsTraits(const T &t,const X &x) : T(t), _edgeWidths(x) {}
00531   };
00533 
00536   template<class X> GraphToEps<EdgeWidthsTraits<X> > edgeWidths(const X &x)
00537   {
00538     dontPrint=true;
00539     return GraphToEps<EdgeWidthsTraits<X> >(EdgeWidthsTraits<X>(*this,x));
00540   }
00541 
00542   template<class X> struct NodeColorsTraits : public T {
00543     const X &_nodeColors;
00544     NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
00545   };
00547 
00552   template<class X> GraphToEps<NodeColorsTraits<X> >
00553   nodeColors(const X &x)
00554   {
00555     dontPrint=true;
00556     return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
00557   }
00558   template<class X> struct NodeTextColorsTraits : public T {
00559     const X &_nodeTextColors;
00560     NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {}
00561   };
00563 
00568   template<class X> GraphToEps<NodeTextColorsTraits<X> >
00569   nodeTextColors(const X &x)
00570   {
00571     dontPrint=true;
00572     _nodeTextColorType=CUST_COL;
00573     return GraphToEps<NodeTextColorsTraits<X> >
00574       (NodeTextColorsTraits<X>(*this,x));
00575   }
00576   template<class X> struct EdgeColorsTraits : public T {
00577     const X &_edgeColors;
00578     EdgeColorsTraits(const T &t,const X &x) : T(t), _edgeColors(x) {}
00579   };
00581 
00586   template<class X> GraphToEps<EdgeColorsTraits<X> >
00587   edgeColors(const X &x)
00588   {
00589     dontPrint=true;
00590     return GraphToEps<EdgeColorsTraits<X> >(EdgeColorsTraits<X>(*this,x));
00591   }
00593 
00603   GraphToEps<T> &nodeScale(double d) {_nodeScale=d;return *this;}
00605 
00610   GraphToEps<T> &autoNodeScale(bool b=true) {
00611     _autoNodeScale=b;return *this;
00612   }
00613 
00615 
00620   GraphToEps<T> &negateY(bool b=true) {
00621     _negY=b;return *this;
00622   }
00623 
00625 
00635   GraphToEps<T> &edgeWidthScale(double d) {_edgeWidthScale=d;return *this;}
00637 
00642   GraphToEps<T> &autoEdgeWidthScale(bool b=true) {
00643     _autoEdgeWidthScale=b;return *this;
00644   }
00646 
00649 
00650   GraphToEps<T> &scale(double d) {_scale=d;return *this;}
00652 
00655   GraphToEps<T> &border(double b) {_xBorder=_yBorder=b;return *this;}
00657 
00660   GraphToEps<T> &border(double x, double y) {
00661     _xBorder=x;_yBorder=y;return *this;
00662   }
00664 
00667   GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
00669 
00672   GraphToEps<T> &arrowLength(double d) {_arrowLength*=d;return *this;}
00674 
00677   GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;}
00678   
00680 
00683   GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
00684   
00686 
00688   GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
00689   
00691   
00694   GraphToEps<T> &parEdgeDist(double d) {_parEdgeDist*=d;return *this;}
00695   
00697   
00700   GraphToEps<T> &hideEdges(bool b=true) {_showEdges=!b;return *this;}
00702   
00705   GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
00706   
00708   
00711   GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
00712 
00714 
00718   GraphToEps<T> &distantColorNodeTexts()
00719   {_nodeTextColorType=DIST_COL;return *this;}
00721 
00726   GraphToEps<T> &distantBWNodeTexts()
00727   {_nodeTextColorType=DIST_BW;return *this;}
00728 
00730   
00734   GraphToEps<T> & nodePsTextsPreamble(const char *str) {
00735     _nodePsTextsPreamble=str ;return *this;
00736   }
00738 
00741   GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;}
00742 
00744 
00747   GraphToEps<T> &bidir(bool b=true) {_undirected=!b;return *this;}
00748 
00750 
00754   GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
00756 
00761   GraphToEps<T> &copyright(const std::string &t) {_copyright=t;return *this;}
00762 
00763 protected:
00764   bool isInsideNode(xy<double> p, double r,int t) 
00765   {
00766     switch(t) {
00767     case CIRCLE:
00768     case MALE:
00769     case FEMALE:
00770       return p.normSquare()<=r*r;
00771     case SQUARE:
00772       return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
00773     case DIAMOND:
00774       return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
00775     }
00776     return false;
00777   }
00778 
00779 public:
00780   ~GraphToEps() { }
00781   
00783 
00788   void run() {
00789     if(dontPrint) return;
00790     
00791     _NegY<typename T::CoordsMapType> mycoords(_coords,_negY);
00792 
00793     os << "%!PS-Adobe-2.0 EPSF-2.0\n";
00794     if(_title.size()>0) os << "%%Title: " << _title << '\n';
00795      if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
00796 //        << "%%Copyright: XXXX\n"
00797     os << "%%Creator: LEMON, graphToEps()\n";
00798     
00799     {
00800       char cbuf[50];
00801       timeval tv;
00802       gettimeofday(&tv, 0);
00803       ctime_r(&tv.tv_sec,cbuf);
00804       os << "%%CreationDate: " << cbuf;
00805     }
00806 
00807     if (_autoEdgeWidthScale) {
00808       double max_w=0;
00809       for(EdgeIt e(g);e!=INVALID;++e)
00810         max_w=std::max(double(_edgeWidths[e]),max_w);
00812       if(max_w>1e-9) {
00813         _edgeWidthScale/=max_w;
00814       }
00815     }
00816 
00817     if (_autoNodeScale) {
00818       double max_s=0;
00819       for(NodeIt n(g);n!=INVALID;++n)
00820         max_s=std::max(double(_nodeSizes[n]),max_s);
00822       if(max_s>1e-9) {
00823         _nodeScale/=max_s;
00824       }
00825     }
00826 
00827 
00828     BoundingBox<double> bb;
00830     for(NodeIt n(g);n!=INVALID;++n) {
00831       double ns=_nodeSizes[n]*_nodeScale;
00832       xy<double> p(ns,ns);
00833       switch(_nodeShapes[n]) {
00834       case CIRCLE:
00835       case SQUARE:
00836       case DIAMOND:
00837         bb.add(p+mycoords[n]);
00838         bb.add(-p+mycoords[n]);
00839         break;
00840       case MALE:
00841         bb.add(-p+mycoords[n]);
00842         bb.add(xy<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]);
00843         break;
00844       case FEMALE:
00845         bb.add(p+mycoords[n]);
00846         bb.add(xy<double>(-ns,-3.01*ns)+mycoords[n]);
00847         break;
00848       }
00849     }
00850     if (bb.empty()) {
00851       bb = BoundingBox<double>(xy<double>(0,0));
00852     }
00853     
00854     if(_scaleToA4)
00855       os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
00856     else {
00857       //Rescale so that BoundingBox won't be neither to big nor too small.
00858       while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10;
00859       while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10;
00860     
00861       os << "%%BoundingBox: "
00862          << int(floor(bb.left()   * _scale - _xBorder)) << ' '
00863          << int(floor(bb.bottom() * _scale - _yBorder)) << ' '
00864          << int(ceil(bb.right()  * _scale + _xBorder)) << ' '
00865          << int(ceil(bb.top()    * _scale + _yBorder)) << '\n';
00866     }
00867     
00868     os << "%%EndComments\n";
00869     
00870     //x1 y1 x2 y2 x3 y3 cr cg cb w
00871     os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
00872        << "      4 2 roll 1 index 1 index curveto stroke } bind def\n";
00873     os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
00874     //x y r
00875     os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n";
00876     //x y r
00877     os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
00878        << "      2 index 1 index sub 2 index 2 index add lineto\n"
00879        << "      2 index 1 index sub 2 index 2 index sub lineto\n"
00880        << "      2 index 1 index add 2 index 2 index sub lineto\n"
00881        << "      closepath pop pop pop} bind def\n";
00882     //x y r
00883     os << "/di { newpath 2 index 1 index add 2 index moveto\n"
00884        << "      2 index             2 index 2 index add lineto\n"
00885        << "      2 index 1 index sub 2 index             lineto\n"
00886        << "      2 index             2 index 2 index sub lineto\n"
00887        << "      closepath pop pop pop} bind def\n";
00888     // x y r cr cg cb
00889     os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
00890        << "     setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
00891        << "   } bind def\n";
00892     os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
00893        << "     setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
00894        << "   } bind def\n";
00895     os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
00896        << "     setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
00897        << "   } bind def\n";
00898     os << "/nfemale { 0 0 0 setrgbcolor 3 index "
00899        << _nodeBorderQuotient/(1+_nodeBorderQuotient)
00900        << " 1.5 mul mul setlinewidth\n"
00901        << "  newpath 5 index 5 index moveto "
00902        << "5 index 5 index 5 index 3.01 mul sub\n"
00903        << "  lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub moveto\n"
00904        << "  5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto stroke\n"
00905        << "  5 index 5 index 5 index c fill\n"
00906        << "  setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
00907        << "  } bind def\n";
00908     os << "/nmale {\n"
00909        << "  0 0 0 setrgbcolor 3 index "
00910        << _nodeBorderQuotient/(1+_nodeBorderQuotient)
00911        <<" 1.5 mul mul setlinewidth\n"
00912        << "  newpath 5 index 5 index moveto\n"
00913        << "  5 index 4 index 1 mul 1.5 mul add\n"
00914        << "  5 index 5 index 3 sqrt 1.5 mul mul add\n"
00915        << "  1 index 1 index lineto\n"
00916        << "  1 index 1 index 7 index sub moveto\n"
00917        << "  1 index 1 index lineto\n"
00918        << "  exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub lineto\n"
00919        << "  stroke\n"
00920        << "  5 index 5 index 5 index c fill\n"
00921        << "  setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
00922        << "  } bind def\n";
00923     
00924 
00925     os << "/arrl " << _arrowLength << " def\n";
00926     os << "/arrw " << _arrowWidth << " def\n";
00927     // l dx_norm dy_norm
00928     os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
00929     //len w dx_norm dy_norm x1 y1 cr cg cb
00930     os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
00931        << "       /w exch def /len exch def\n"
00932       //         << "       0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
00933        << "       newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
00934        << "       len w sub arrl sub dx dy lrl\n"
00935        << "       arrw dy dx neg lrl\n"
00936        << "       dx arrl w add mul dy w 2 div arrw add mul sub\n"
00937        << "       dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
00938        << "       dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
00939        << "       dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
00940        << "       arrw dy dx neg lrl\n"
00941        << "       len w sub arrl sub neg dx dy lrl\n"
00942        << "       closepath fill } bind def\n";
00943     os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
00944        << "         neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
00945 
00946     os << "\ngsave\n";
00947     if(_scaleToA4)
00948       if(bb.height()>bb.width()) {
00949         double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(),
00950                   (A4WIDTH-2*A4BORDER)/bb.width());
00951         os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
00952            << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER << " translate\n"
00953            << sc << " dup scale\n"
00954            << -bb.left() << ' ' << -bb.bottom() << " translate\n";
00955       }
00956       else {
00957         //\todo Verify centering
00958         double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(),
00959                   (A4WIDTH-2*A4BORDER)/bb.height());
00960         os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
00961            << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER  << " translate\n"
00962            << sc << " dup scale\n90 rotate\n"
00963            << -bb.left() << ' ' << -bb.top() << " translate\n"; 
00964         }
00965     else if(_scale!=1.0) os << _scale << " dup scale\n";
00966     
00967     if(_showEdges) {
00968       os << "%Edges:\ngsave\n";      
00969       if(_enableParallel) {
00970         std::vector<Edge> el;
00971         for(EdgeIt e(g);e!=INVALID;++e)
00972           if((!_undirected||g.source(e)<g.target(e))&&_edgeWidths[e]>0)
00973             el.push_back(e);
00974         std::sort(el.begin(),el.end(),edgeLess(g));
00975         
00976         typename std::vector<Edge>::iterator j;
00977         for(typename std::vector<Edge>::iterator i=el.begin();i!=el.end();i=j) {
00978           for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
00979 
00980           double sw=0;
00981           for(typename std::vector<Edge>::iterator e=i;e!=j;++e)
00982             sw+=_edgeWidths[*e]*_edgeWidthScale+_parEdgeDist;
00983           sw-=_parEdgeDist;
00984           sw/=-2.0;
00985           xy<double> dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]);
00986           double l=std::sqrt(dvec.normSquare()); 
00988           xy<double> d(dvec/std::max(l,1e-9));
00989           xy<double> m;
00990 //        m=xy<double>(mycoords[g.target(*i)]+mycoords[g.source(*i)])/2.0;
00991 
00992 //        m=xy<double>(mycoords[g.source(*i)])+
00993 //          dvec*(double(_nodeSizes[g.source(*i)])/
00994 //             (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
00995 
00996           m=xy<double>(mycoords[g.source(*i)])+
00997             d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
00998 
00999           for(typename std::vector<Edge>::iterator e=i;e!=j;++e) {
01000             sw+=_edgeWidths[*e]*_edgeWidthScale/2.0;
01001             xy<double> mm=m+rot90(d)*sw/.75;
01002             if(_drawArrows) {
01003               int node_shape;
01004               xy<double> s=mycoords[g.source(*e)];
01005               xy<double> t=mycoords[g.target(*e)];
01006               double rn=_nodeSizes[g.target(*e)]*_nodeScale;
01007               node_shape=_nodeShapes[g.target(*e)];
01008               Bezier3 bez(s,mm,mm,t);
01009               double t1=0,t2=1;
01010               for(int i=0;i<INTERPOL_PREC;++i)
01011                 if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
01012                 else t1=(t1+t2)/2;
01013               xy<double> apoint=bez((t1+t2)/2);
01014               rn = _arrowLength+_edgeWidths[*e]*_edgeWidthScale;
01015               rn*=rn;
01016               t2=(t1+t2)/2;t1=0;
01017               for(int i=0;i<INTERPOL_PREC;++i)
01018                 if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
01019                 else t2=(t1+t2)/2;
01020               xy<double> linend=bez((t1+t2)/2);       
01021               bez=bez.before((t1+t2)/2);
01022 //            rn=_nodeSizes[g.source(*e)]*_nodeScale;
01023 //            node_shape=_nodeShapes[g.source(*e)];
01024 //            t1=0;t2=1;
01025 //            for(int i=0;i<INTERPOL_PREC;++i)
01026 //              if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t1=(t1+t2)/2;
01027 //              else t2=(t1+t2)/2;
01028 //            bez=bez.after((t1+t2)/2);
01029               os << _edgeWidths[*e]*_edgeWidthScale << " setlinewidth "
01030                  << _edgeColors[*e].red() << ' '
01031                  << _edgeColors[*e].green() << ' '
01032                  << _edgeColors[*e].blue() << " setrgbcolor newpath\n"
01033                  << bez.p1.x << ' ' <<  bez.p1.y << " moveto\n"
01034                  << bez.p2.x << ' ' << bez.p2.y << ' '
01035                  << bez.p3.x << ' ' << bez.p3.y << ' '
01036                  << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
01037               xy<double> dd(rot90(linend-apoint));
01038               dd*=(.5*_edgeWidths[*e]*_edgeWidthScale+_arrowWidth)/
01039                 std::sqrt(dd.normSquare());
01040               os << "newpath " << psOut(apoint) << " moveto "
01041                  << psOut(linend+dd) << " lineto "
01042                  << psOut(linend-dd) << " lineto closepath fill\n";
01043             }
01044             else {
01045               os << mycoords[g.source(*e)].x << ' '
01046                  << mycoords[g.source(*e)].y << ' '
01047                  << mm.x << ' ' << mm.y << ' '
01048                  << mycoords[g.target(*e)].x << ' '
01049                  << mycoords[g.target(*e)].y << ' '
01050                  << _edgeColors[*e].red() << ' '
01051                  << _edgeColors[*e].green() << ' '
01052                  << _edgeColors[*e].blue() << ' '
01053                  << _edgeWidths[*e]*_edgeWidthScale << " lb\n";
01054             }
01055             sw+=_edgeWidths[*e]*_edgeWidthScale/2.0+_parEdgeDist;
01056           }
01057         }
01058       }
01059       else for(EdgeIt e(g);e!=INVALID;++e)
01060         if((!_undirected||g.source(e)<g.target(e))&&_edgeWidths[e]>0)
01061           if(_drawArrows) {
01062             xy<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]);
01063             double rn=_nodeSizes[g.target(e)]*_nodeScale;
01064             int node_shape=_nodeShapes[g.target(e)];
01065             double t1=0,t2=1;
01066             for(int i=0;i<INTERPOL_PREC;++i)
01067               if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
01068               else t2=(t1+t2)/2;
01069             double l=std::sqrt(d.normSquare());
01070             d/=l;
01071             
01072             os << l*(1-(t1+t2)/2) << ' '
01073                << _edgeWidths[e]*_edgeWidthScale << ' '
01074                << d.x << ' ' << d.y << ' '
01075                << mycoords[g.source(e)].x << ' '
01076                << mycoords[g.source(e)].y << ' '
01077                << _edgeColors[e].red() << ' '
01078                << _edgeColors[e].green() << ' '
01079                << _edgeColors[e].blue() << " arr\n";
01080           }
01081           else os << mycoords[g.source(e)].x << ' '
01082                   << mycoords[g.source(e)].y << ' '
01083                   << mycoords[g.target(e)].x << ' '
01084                   << mycoords[g.target(e)].y << ' '
01085                   << _edgeColors[e].red() << ' '
01086                   << _edgeColors[e].green() << ' '
01087                   << _edgeColors[e].blue() << ' '
01088                   << _edgeWidths[e]*_edgeWidthScale << " l\n";
01089       os << "grestore\n";
01090     }
01091     if(_showNodes) {
01092       os << "%Nodes:\ngsave\n";
01093       for(NodeIt n(g);n!=INVALID;++n) {
01094         os << mycoords[n].x << ' ' << mycoords[n].y << ' '
01095            << _nodeSizes[n]*_nodeScale << ' '
01096            << _nodeColors[n].red() << ' '
01097            << _nodeColors[n].green() << ' '
01098            << _nodeColors[n].blue() << ' ';
01099         switch(_nodeShapes[n]) {
01100         case CIRCLE:
01101           os<< "nc";break;
01102         case SQUARE:
01103           os<< "nsq";break;
01104         case DIAMOND:
01105           os<< "ndi";break;
01106         case MALE:
01107           os<< "nmale";break;
01108         case FEMALE:
01109           os<< "nfemale";break;
01110         }
01111         os<<'\n';
01112       }
01113       os << "grestore\n";
01114     }
01115     if(_showNodeText) {
01116       os << "%Node texts:\ngsave\n";
01117       os << "/fosi " << _nodeTextSize << " def\n";
01118       os << "(Helvetica) findfont fosi scalefont setfont\n";
01119       for(NodeIt n(g);n!=INVALID;++n) {
01120         switch(_nodeTextColorType) {
01121         case DIST_COL:
01122           os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n";
01123           break;
01124         case DIST_BW:
01125           os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n";
01126           break;
01127         case CUST_COL:
01128           os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n";
01129           break;
01130         default:
01131           os << "0 0 0 setrgbcolor\n";
01132         }
01133         os << mycoords[n].x << ' ' << mycoords[n].y
01134            << " (" << _nodeTexts[n] << ") cshow\n";
01135       }
01136       os << "grestore\n";
01137     }
01138     if(_showNodePsText) {
01139       os << "%Node PS blocks:\ngsave\n";
01140       for(NodeIt n(g);n!=INVALID;++n)
01141         os << mycoords[n].x << ' ' << mycoords[n].y
01142            << " moveto\n" << _nodePsTexts[n] << "\n";
01143       os << "grestore\n";
01144     }
01145     
01146     os << "grestore\nshowpage\n";
01147 
01148     //CleanUp:
01149     if(_pleaseRemoveOsStream) {delete &os;}
01150   } 
01151 };
01152 
01153 template<class T>
01154 const int GraphToEps<T>::INTERPOL_PREC = 20;
01155 template<class T>
01156 const double GraphToEps<T>::A4HEIGHT = 841.8897637795276;
01157 template<class T>
01158 const double GraphToEps<T>::A4WIDTH  = 595.275590551181;
01159 template<class T>
01160 const double GraphToEps<T>::A4BORDER = 15;
01161 
01162 
01164 
01184 template<class G>
01185 GraphToEps<DefaultGraphToEpsTraits<G> > 
01186 graphToEps(G &g, std::ostream& os=std::cout)
01187 {
01188   return 
01189     GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
01190 }
01191  
01193 
01200 template<class G>
01201 GraphToEps<DefaultGraphToEpsTraits<G> > 
01202 graphToEps(G &g,const char *file_name)
01203 {
01204   return GraphToEps<DefaultGraphToEpsTraits<G> >
01205     (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true));
01206 }
01207 
01209 
01216 template<class G>
01217 GraphToEps<DefaultGraphToEpsTraits<G> > 
01218 graphToEps(G &g,const std::string& file_name)
01219 {
01220   return GraphToEps<DefaultGraphToEpsTraits<G> >
01221     (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name.c_str()),true));
01222 }
01223 
01224 } //END OF NAMESPACE LEMON
01225 
01226 #endif // LEMON_GRAPH_TO_EPS_H

Generated on Fri Feb 3 18:37:17 2006 for LEMON by  doxygen 1.4.6