COIN-OR::LEMON - Graph Library

Changeset 209:765619b7cbb2 in lemon-1.0 for lemon/graph_to_eps.h


Ignore:
Timestamp:
07/13/08 20:51:02 (16 years ago)
Author:
Alpar Juttner <alpar@…>
Branch:
default
Phase:
public
Message:

Apply unify-sources.sh to the source tree

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/graph_to_eps.h

    r206 r209  
    1 /* -*- C++ -*-
     1/* -*- mode: C++; indent-tabs-mode: nil; -*-
    22 *
    3  * This file is a part of LEMON, a generic C++ optimization library
     3 * This file is a part of LEMON, a generic C++ optimization library.
    44 *
    55 * Copyright (C) 2003-2008
     
    6161    };
    6262  }
    63  
     63
    6464///Default traits class of \ref GraphToEps
    6565
     
    7777  typedef typename Graph::InArcIt InArcIt;
    7878  typedef typename Graph::OutArcIt OutArcIt;
    79  
     79
    8080
    8181  const Graph &g;
    8282
    8383  std::ostream& os;
    84  
     84
    8585  typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType;
    8686  CoordsMapType _coords;
     
    9494
    9595  double _arcWidthScale;
    96  
     96
    9797  double _nodeScale;
    9898  double _xBorder, _yBorder;
    9999  double _scale;
    100100  double _nodeBorderQuotient;
    101  
     101
    102102  bool _drawArrows;
    103103  double _arrowLength, _arrowWidth;
    104  
     104
    105105  bool _showNodes, _showArcs;
    106106
     
    109109
    110110  bool _showNodeText;
    111   ConstMap<typename Graph::Node,bool > _nodeTexts; 
     111  ConstMap<typename Graph::Node,bool > _nodeTexts;
    112112  double _nodeTextSize;
    113113
    114114  bool _showNodePsText;
    115   ConstMap<typename Graph::Node,bool > _nodePsTexts; 
     115  ConstMap<typename Graph::Node,bool > _nodePsTexts;
    116116  char *_nodePsTextsPreamble;
    117  
     117
    118118  bool _undirected;
    119119
     
    125125  std::string _copyright;
    126126
    127   enum NodeTextColorType 
     127  enum NodeTextColorType
    128128    { DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType;
    129129  ConstMap<typename Graph::Node,Color > _nodeTextColors;
     
    147147  ///will be explicitly deallocated by the destructor.
    148148  DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
    149                           bool _pros=false) :
     149                          bool _pros=false) :
    150150    g(_g), os(_os),
    151151    _coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0),
     
    176176///
    177177///For detailed examples see the \ref graph_to_eps_demo.cc demo file.
    178 template<class T> class GraphToEps : public T 
     178template<class T> class GraphToEps : public T
    179179{
    180180  // Can't believe it is required by the C++ standard
     
    195195  using T::_scale;
    196196  using T::_nodeBorderQuotient;
    197  
     197
    198198  using T::_drawArrows;
    199199  using T::_arrowLength;
    200200  using T::_arrowWidth;
    201  
     201
    202202  using T::_showNodes;
    203203  using T::_showArcs;
     
    207207
    208208  using T::_showNodeText;
    209   using T::_nodeTexts; 
     209  using T::_nodeTexts;
    210210  using T::_nodeTextSize;
    211211
    212212  using T::_showNodePsText;
    213   using T::_nodePsTexts; 
     213  using T::_nodePsTexts;
    214214  using T::_nodePsTextsPreamble;
    215  
     215
    216216  using T::_undirected;
    217217
     
    262262  ///Node shapes.
    263263  ///
    264   enum NodeShapes { 
     264  enum NodeShapes {
    265265    /// = 0
    266266    ///\image html nodeshape_0.png
    267267    ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm
    268     CIRCLE=0, 
     268    CIRCLE=0,
    269269    /// = 1
    270270    ///\image html nodeshape_1.png
    271271    ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm
    272272    ///
    273     SQUARE=1, 
     273    SQUARE=1,
    274274    /// = 2
    275275    ///\image html nodeshape_2.png
     
    294294  public:
    295295    arcLess(const Graph &_g) : g(_g) {}
    296     bool operator()(Arc a,Arc b) const 
     296    bool operator()(Arc a,Arc b) const
    297297    {
    298298      Node ai=std::min(g.source(a),g.target(a));
     
    301301      Node ba=std::max(g.source(b),g.target(b));
    302302      return ai<bi ||
    303         (ai==bi && (aa < ba ||
    304                     (aa==ba && ai==g.source(a) && bi==g.target(b))));
     303        (ai==bi && (aa < ba ||
     304                    (aa==ba && ai==g.source(a) && bi==g.target(b))));
    305305    }
    306306  };
     
    308308  {
    309309    return (g.source(e)==g.source(f)&&
    310             g.target(e)==g.target(f)) ||
     310            g.target(e)==g.target(f)) ||
    311311      (g.source(e)==g.target(f)&&
    312312       g.target(e)==g.source(f));
    313313  }
    314314  template<class TT>
    315   static std::string psOut(const dim2::Point<TT> &p) 
     315  static std::string psOut(const dim2::Point<TT> &p)
    316316    {
    317       std::ostringstream os;   
     317      std::ostringstream os;
    318318      os << p.x << ' ' << p.y;
    319319      return os.str();
    320320    }
    321   static std::string psOut(const Color &c) 
     321  static std::string psOut(const Color &c)
    322322    {
    323       std::ostringstream os;   
     323      std::ostringstream os;
    324324      os << c.red() << ' ' << c.green() << ' ' << c.blue();
    325325      return os.str();
    326326    }
    327  
     327
    328328public:
    329329  GraphToEps(const T &t) : T(t), dontPrint(false) {};
    330  
     330
    331331  template<class X> struct CoordsTraits : public T {
    332332  typedef X CoordsMapType;
     
    338338  ///Sets the map of the node coordinates.
    339339  ///\param x must be a node map with \ref dim2::Point "dim2::Point<double>" or
    340   ///\ref dim2::Point "dim2::Point<int>" values. 
     340  ///\ref dim2::Point "dim2::Point<int>" values.
    341341  template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
    342342    dontPrint=true;
     
    350350
    351351  ///Sets the map of the node sizes.
    352   ///\param x must be a node map with \c double (or convertible) values. 
     352  ///\param x must be a node map with \c double (or convertible) values.
    353353  template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
    354354  {
     
    365365  ///The available shape values
    366366  ///can be found in \ref NodeShapes "enum NodeShapes".
    367   ///\param x must be a node map with \c int (or convertible) values. 
     367  ///\param x must be a node map with \c int (or convertible) values.
    368368  ///\sa NodeShapes
    369369  template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
     
    380380  ///Sets the text printed on the nodes.
    381381  ///\param x must be a node map with type that can be pushed to a standard
    382   ///\c ostream. 
     382  ///\c ostream.
    383383  template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
    384384  {
     
    418418
    419419  ///Sets the map of the arc widths.
    420   ///\param x must be an arc map with \c double (or convertible) values. 
     420  ///\param x must be an arc map with \c double (or convertible) values.
    421421  template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x)
    422422  {
     
    448448
    449449  ///Sets the map of the node text colors.
    450   ///\param x must be a node map with \ref Color values. 
     450  ///\param x must be a node map with \ref Color values.
    451451  ///
    452452  ///\sa Palette
     
    466466
    467467  ///Sets the map of the arc colors.
    468   ///\param x must be an arc map with \ref Color values. 
     468  ///\param x must be an arc map with \ref Color values.
    469469  ///
    470470  ///\sa Palette
     
    478478
    479479  ///Sets a global scale factor for node sizes.
    480   /// 
     480  ///
    481481  /// If nodeSizes() is not given, this function simply sets the node
    482482  /// sizes to \c d.  If nodeSizes() is given, but
     
    569569  ///Sets the width of the arrowheads
    570570  GraphToEps<T> &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;}
    571  
     571
    572572  ///Scales the drawing to fit to A4 page
    573573  GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
    574  
     574
    575575  ///Enables parallel arcs
    576576  GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
    577  
     577
    578578  ///Sets the distance between parallel arcs
    579579  GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;}
    580  
     580
    581581  ///Hides the arcs
    582582  GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;}
    583583  ///Hides the nodes
    584584  GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
    585  
     585
    586586  ///Sets the size of the node texts
    587587  GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
     
    601601
    602602  ///Gives a preamble block for node Postscript block.
    603  
     603
    604604  ///Gives a preamble block for node Postscript block.
    605605  ///
     
    626626  ///\sa undirected()
    627627  GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;}
    628  
     628
    629629  ///Sets the title.
    630630
     
    641641
    642642protected:
    643   bool isInsideNode(dim2::Point<double> p, double r,int t) 
     643  bool isInsideNode(dim2::Point<double> p, double r,int t)
    644644  {
    645645    switch(t) {
     
    658658public:
    659659  ~GraphToEps() { }
    660  
     660
    661661  ///Draws the graph.
    662662
     
    669669    const double EPSILON=1e-9;
    670670    if(dontPrint) return;
    671    
     671
    672672    _graph_to_eps_bits::_NegY<typename T::CoordsMapType>
    673673      mycoords(_coords,_negY);
     
    678678    os << "%%Creator: LEMON, graphToEps()\n";
    679679
    680     {   
    681 #ifndef WIN32 
     680    {
     681#ifndef WIN32
    682682      timeval tv;
    683683      gettimeofday(&tv, 0);
     
    689689      SYSTEMTIME time;
    690690      char buf1[11], buf2[9], buf3[5];
    691      
     691
    692692      GetSystemTime(&time);
    693       if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time, 
    694                         "ddd MMM dd", buf1, 11) &&
    695           GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
    696                         "HH':'mm':'ss", buf2, 9) &&
    697           GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
    698                                 "yyyy", buf3, 5)) {
    699         os << "%%CreationDate: " << buf1 << ' '
    700            << buf2 << ' ' << buf3 << std::endl;
    701       }   
     693      if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
     694                        "ddd MMM dd", buf1, 11) &&
     695          GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
     696                        "HH':'mm':'ss", buf2, 9) &&
     697          GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
     698                                "yyyy", buf3, 5)) {
     699        os << "%%CreationDate: " << buf1 << ' '
     700           << buf2 << ' ' << buf3 << std::endl;
     701      }
    702702#endif
    703703    }
     
    706706      double max_w=0;
    707707      for(ArcIt e(g);e!=INVALID;++e)
    708         max_w=std::max(double(_arcWidths[e]),max_w);
     708        max_w=std::max(double(_arcWidths[e]),max_w);
    709709      //\todo better 'epsilon' would be nice here.
    710710      if(max_w>EPSILON) {
    711         _arcWidthScale/=max_w;
     711        _arcWidthScale/=max_w;
    712712      }
    713713    }
     
    716716      double max_s=0;
    717717      for(NodeIt n(g);n!=INVALID;++n)
    718         max_s=std::max(double(_nodeSizes[n]),max_s);
     718        max_s=std::max(double(_nodeSizes[n]),max_s);
    719719      //\todo better 'epsilon' would be nice here.
    720720      if(max_s>EPSILON) {
    721         _nodeScale/=max_s;
     721        _nodeScale/=max_s;
    722722      }
    723723    }
     
    728728      for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]);
    729729      if (bb.empty()) {
    730         bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0));
     730        bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0));
    731731      }
    732732      diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare());
     
    735735      if(!_absoluteArcWidths) _arcWidthScale*=diag_len;
    736736    }
    737    
     737
    738738    dim2::BoundingBox<double> bb;
    739739    for(NodeIt n(g);n!=INVALID;++n) {
     
    744744      case SQUARE:
    745745      case DIAMOND:
    746         bb.add(p+mycoords[n]);
    747         bb.add(-p+mycoords[n]);
    748         break;
     746        bb.add(p+mycoords[n]);
     747        bb.add(-p+mycoords[n]);
     748        break;
    749749      case MALE:
    750         bb.add(-p+mycoords[n]);
    751         bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]);
    752         break;
     750        bb.add(-p+mycoords[n]);
     751        bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]);
     752        break;
    753753      case FEMALE:
    754         bb.add(p+mycoords[n]);
    755         bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]);
    756         break;
     754        bb.add(p+mycoords[n]);
     755        bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]);
     756        break;
    757757      }
    758758    }
     
    760760      bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0));
    761761    }
    762    
     762
    763763    if(_scaleToA4)
    764764      os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
    765765    else {
    766766      if(_preScale) {
    767         //Rescale so that BoundingBox won't be neither to big nor too small.
    768         while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10;
    769         while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10;
     767        //Rescale so that BoundingBox won't be neither to big nor too small.
     768        while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10;
     769        while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10;
    770770      }
    771      
     771
    772772      os << "%%BoundingBox: "
    773         << int(floor(bb.left()   * _scale - _xBorder)) << ' '
    774         << int(floor(bb.bottom() * _scale - _yBorder)) << ' '
    775         << int(ceil(bb.right()  * _scale + _xBorder)) << ' '
    776         << int(ceil(bb.top()    * _scale + _yBorder)) << '\n';
    777     }
    778    
     773        << int(floor(bb.left()   * _scale - _xBorder)) << ' '
     774        << int(floor(bb.bottom() * _scale - _yBorder)) << ' '
     775        << int(ceil(bb.right()  * _scale + _xBorder)) << ' '
     776        << int(ceil(bb.top()    * _scale + _yBorder)) << '\n';
     777    }
     778
    779779    os << "%%EndComments\n";
    780    
     780
    781781    //x1 y1 x2 y2 x3 y3 cr cg cb w
    782782    os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
     
    832832       << "  setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
    833833       << "  } bind def\n";
    834    
     834
    835835
    836836    os << "/arrl " << _arrowLength << " def\n";
     
    841841    os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
    842842       << "       /w exch def /len exch def\n"
    843       //        << "       0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
     843      //        << "       0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
    844844       << "       newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
    845845       << "       len w sub arrl sub dx dy lrl\n"
     
    858858    if(_scaleToA4)
    859859      if(bb.height()>bb.width()) {
    860         double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(),
    861                   (A4WIDTH-2*A4BORDER)/bb.width());
    862         os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
    863            << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER
    864            << " translate\n"
    865            << sc << " dup scale\n"
    866            << -bb.left() << ' ' << -bb.bottom() << " translate\n";
     860        double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(),
     861                  (A4WIDTH-2*A4BORDER)/bb.width());
     862        os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
     863           << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER
     864           << " translate\n"
     865           << sc << " dup scale\n"
     866           << -bb.left() << ' ' << -bb.bottom() << " translate\n";
    867867      }
    868868      else {
    869         //\todo Verify centering
    870         double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(),
    871                   (A4WIDTH-2*A4BORDER)/bb.height());
    872         os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
    873            << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER
    874            << " translate\n"
    875            << sc << " dup scale\n90 rotate\n"
    876            << -bb.left() << ' ' << -bb.top() << " translate\n";
    877         }
     869        //\todo Verify centering
     870        double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(),
     871                  (A4WIDTH-2*A4BORDER)/bb.height());
     872        os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
     873           << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER
     874           << " translate\n"
     875           << sc << " dup scale\n90 rotate\n"
     876           << -bb.left() << ' ' << -bb.top() << " translate\n";
     877        }
    878878    else if(_scale!=1.0) os << _scale << " dup scale\n";
    879    
     879
    880880    if(_showArcs) {
    881       os << "%Arcs:\ngsave\n";     
     881      os << "%Arcs:\ngsave\n";
    882882      if(_enableParallel) {
    883         std::vector<Arc> el;
    884         for(ArcIt e(g);e!=INVALID;++e)
    885           if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
    886              &&g.source(e)!=g.target(e))
    887             el.push_back(e);
    888         std::sort(el.begin(),el.end(),arcLess(g));
    889        
    890         typename std::vector<Arc>::iterator j;
    891         for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) {
    892           for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
    893 
    894           double sw=0;
    895           for(typename std::vector<Arc>::iterator e=i;e!=j;++e)
    896             sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist;
    897           sw-=_parArcDist;
    898           sw/=-2.0;
    899           dim2::Point<double>
    900             dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]);
    901           double l=std::sqrt(dvec.normSquare());
    902           //\todo better 'epsilon' would be nice here.
    903           dim2::Point<double> d(dvec/std::max(l,EPSILON));
    904           dim2::Point<double> m;
    905 //        m=dim2::Point<double>(mycoords[g.target(*i)]+mycoords[g.source(*i)])/2.0;
    906 
    907 //        m=dim2::Point<double>(mycoords[g.source(*i)])+
    908 //          dvec*(double(_nodeSizes[g.source(*i)])/
    909 //             (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
    910 
    911           m=dim2::Point<double>(mycoords[g.source(*i)])+
    912             d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
    913 
    914           for(typename std::vector<Arc>::iterator e=i;e!=j;++e) {
    915             sw+=_arcWidths[*e]*_arcWidthScale/2.0;
    916             dim2::Point<double> mm=m+rot90(d)*sw/.75;
    917             if(_drawArrows) {
    918               int node_shape;
    919               dim2::Point<double> s=mycoords[g.source(*e)];
    920               dim2::Point<double> t=mycoords[g.target(*e)];
    921               double rn=_nodeSizes[g.target(*e)]*_nodeScale;
    922               node_shape=_nodeShapes[g.target(*e)];
    923               dim2::Bezier3 bez(s,mm,mm,t);
    924               double t1=0,t2=1;
    925               for(int ii=0;ii<INTERPOL_PREC;++ii)
    926                 if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
    927                 else t1=(t1+t2)/2;
    928               dim2::Point<double> apoint=bez((t1+t2)/2);
    929               rn = _arrowLength+_arcWidths[*e]*_arcWidthScale;
    930               rn*=rn;
    931               t2=(t1+t2)/2;t1=0;
    932               for(int ii=0;ii<INTERPOL_PREC;++ii)
    933                 if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
    934                 else t2=(t1+t2)/2;
    935               dim2::Point<double> linend=bez((t1+t2)/2);             
    936               bez=bez.before((t1+t2)/2);
    937 //            rn=_nodeSizes[g.source(*e)]*_nodeScale;
    938 //            node_shape=_nodeShapes[g.source(*e)];
    939 //            t1=0;t2=1;
    940 //            for(int i=0;i<INTERPOL_PREC;++i)
    941 //              if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t1=(t1+t2)/2;
    942 //              else t2=(t1+t2)/2;
    943 //            bez=bez.after((t1+t2)/2);
    944               os << _arcWidths[*e]*_arcWidthScale << " setlinewidth "
    945                 << _arcColors[*e].red() << ' '
    946                 << _arcColors[*e].green() << ' '
    947                 << _arcColors[*e].blue() << " setrgbcolor newpath\n"
    948                 << bez.p1.x << ' ' <<  bez.p1.y << " moveto\n"
    949                 << bez.p2.x << ' ' << bez.p2.y << ' '
    950                 << bez.p3.x << ' ' << bez.p3.y << ' '
    951                 << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
    952               dim2::Point<double> dd(rot90(linend-apoint));
    953               dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/
    954                 std::sqrt(dd.normSquare());
    955               os << "newpath " << psOut(apoint) << " moveto "
    956                 << psOut(linend+dd) << " lineto "
    957                 << psOut(linend-dd) << " lineto closepath fill\n";
    958             }
    959             else {
    960               os << mycoords[g.source(*e)].x << ' '
    961                 << mycoords[g.source(*e)].y << ' '
    962                 << mm.x << ' ' << mm.y << ' '
    963                 << mycoords[g.target(*e)].x << ' '
    964                 << mycoords[g.target(*e)].y << ' '
    965                 << _arcColors[*e].red() << ' '
    966                 << _arcColors[*e].green() << ' '
    967                 << _arcColors[*e].blue() << ' '
    968                 << _arcWidths[*e]*_arcWidthScale << " lb\n";
    969             }
    970             sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist;
    971           }
    972         }
     883        std::vector<Arc> el;
     884        for(ArcIt e(g);e!=INVALID;++e)
     885          if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
     886             &&g.source(e)!=g.target(e))
     887            el.push_back(e);
     888        std::sort(el.begin(),el.end(),arcLess(g));
     889
     890        typename std::vector<Arc>::iterator j;
     891        for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) {
     892          for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
     893
     894          double sw=0;
     895          for(typename std::vector<Arc>::iterator e=i;e!=j;++e)
     896            sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist;
     897          sw-=_parArcDist;
     898          sw/=-2.0;
     899          dim2::Point<double>
     900            dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]);
     901          double l=std::sqrt(dvec.normSquare());
     902          //\todo better 'epsilon' would be nice here.
     903          dim2::Point<double> d(dvec/std::max(l,EPSILON));
     904           dim2::Point<double> m;
     905//           m=dim2::Point<double>(mycoords[g.target(*i)]+mycoords[g.source(*i)])/2.0;
     906
     907//            m=dim2::Point<double>(mycoords[g.source(*i)])+
     908//             dvec*(double(_nodeSizes[g.source(*i)])/
     909//                (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
     910
     911           m=dim2::Point<double>(mycoords[g.source(*i)])+
     912            d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
     913
     914          for(typename std::vector<Arc>::iterator e=i;e!=j;++e) {
     915            sw+=_arcWidths[*e]*_arcWidthScale/2.0;
     916            dim2::Point<double> mm=m+rot90(d)*sw/.75;
     917            if(_drawArrows) {
     918              int node_shape;
     919              dim2::Point<double> s=mycoords[g.source(*e)];
     920              dim2::Point<double> t=mycoords[g.target(*e)];
     921              double rn=_nodeSizes[g.target(*e)]*_nodeScale;
     922              node_shape=_nodeShapes[g.target(*e)];
     923              dim2::Bezier3 bez(s,mm,mm,t);
     924              double t1=0,t2=1;
     925              for(int ii=0;ii<INTERPOL_PREC;++ii)
     926                if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
     927                else t1=(t1+t2)/2;
     928              dim2::Point<double> apoint=bez((t1+t2)/2);
     929              rn = _arrowLength+_arcWidths[*e]*_arcWidthScale;
     930              rn*=rn;
     931              t2=(t1+t2)/2;t1=0;
     932              for(int ii=0;ii<INTERPOL_PREC;++ii)
     933                if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
     934                else t2=(t1+t2)/2;
     935              dim2::Point<double> linend=bez((t1+t2)/2);
     936              bez=bez.before((t1+t2)/2);
     937//               rn=_nodeSizes[g.source(*e)]*_nodeScale;
     938//               node_shape=_nodeShapes[g.source(*e)];
     939//               t1=0;t2=1;
     940//               for(int i=0;i<INTERPOL_PREC;++i)
     941//                 if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t1=(t1+t2)/2;
     942//                 else t2=(t1+t2)/2;
     943//               bez=bez.after((t1+t2)/2);
     944              os << _arcWidths[*e]*_arcWidthScale << " setlinewidth "
     945                << _arcColors[*e].red() << ' '
     946                << _arcColors[*e].green() << ' '
     947                << _arcColors[*e].blue() << " setrgbcolor newpath\n"
     948                << bez.p1.x << ' ' <<  bez.p1.y << " moveto\n"
     949                << bez.p2.x << ' ' << bez.p2.y << ' '
     950                << bez.p3.x << ' ' << bez.p3.y << ' '
     951                << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
     952              dim2::Point<double> dd(rot90(linend-apoint));
     953              dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/
     954                std::sqrt(dd.normSquare());
     955              os << "newpath " << psOut(apoint) << " moveto "
     956                << psOut(linend+dd) << " lineto "
     957                << psOut(linend-dd) << " lineto closepath fill\n";
     958            }
     959            else {
     960              os << mycoords[g.source(*e)].x << ' '
     961                << mycoords[g.source(*e)].y << ' '
     962                << mm.x << ' ' << mm.y << ' '
     963                << mycoords[g.target(*e)].x << ' '
     964                << mycoords[g.target(*e)].y << ' '
     965                << _arcColors[*e].red() << ' '
     966                << _arcColors[*e].green() << ' '
     967                << _arcColors[*e].blue() << ' '
     968                << _arcWidths[*e]*_arcWidthScale << " lb\n";
     969            }
     970            sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist;
     971          }
     972        }
    973973      }
    974974      else for(ArcIt e(g);e!=INVALID;++e)
    975         if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
    976            &&g.source(e)!=g.target(e)) {
    977           if(_drawArrows) {
    978             dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]);
    979             double rn=_nodeSizes[g.target(e)]*_nodeScale;
    980             int node_shape=_nodeShapes[g.target(e)];
    981             double t1=0,t2=1;
    982             for(int i=0;i<INTERPOL_PREC;++i)
    983               if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
    984               else t2=(t1+t2)/2;
    985             double l=std::sqrt(d.normSquare());
    986             d/=l;
    987            
    988             os << l*(1-(t1+t2)/2) << ' '
    989                << _arcWidths[e]*_arcWidthScale << ' '
    990                << d.x << ' ' << d.y << ' '
    991                << mycoords[g.source(e)].x << ' '
    992                << mycoords[g.source(e)].y << ' '
    993                << _arcColors[e].red() << ' '
    994                << _arcColors[e].green() << ' '
    995                << _arcColors[e].blue() << " arr\n";
    996           }
    997           else os << mycoords[g.source(e)].x << ' '
    998                   << mycoords[g.source(e)].y << ' '
    999                   << mycoords[g.target(e)].x << ' '
    1000                   << mycoords[g.target(e)].y << ' '
    1001                   << _arcColors[e].red() << ' '
    1002                   << _arcColors[e].green() << ' '
    1003                   << _arcColors[e].blue() << ' '
    1004                   << _arcWidths[e]*_arcWidthScale << " l\n";
    1005         }
     975        if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
     976           &&g.source(e)!=g.target(e)) {
     977          if(_drawArrows) {
     978            dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]);
     979            double rn=_nodeSizes[g.target(e)]*_nodeScale;
     980            int node_shape=_nodeShapes[g.target(e)];
     981            double t1=0,t2=1;
     982            for(int i=0;i<INTERPOL_PREC;++i)
     983              if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
     984              else t2=(t1+t2)/2;
     985            double l=std::sqrt(d.normSquare());
     986            d/=l;
     987
     988            os << l*(1-(t1+t2)/2) << ' '
     989               << _arcWidths[e]*_arcWidthScale << ' '
     990               << d.x << ' ' << d.y << ' '
     991               << mycoords[g.source(e)].x << ' '
     992               << mycoords[g.source(e)].y << ' '
     993               << _arcColors[e].red() << ' '
     994               << _arcColors[e].green() << ' '
     995               << _arcColors[e].blue() << " arr\n";
     996          }
     997          else os << mycoords[g.source(e)].x << ' '
     998                  << mycoords[g.source(e)].y << ' '
     999                  << mycoords[g.target(e)].x << ' '
     1000                  << mycoords[g.target(e)].y << ' '
     1001                  << _arcColors[e].red() << ' '
     1002                  << _arcColors[e].green() << ' '
     1003                  << _arcColors[e].blue() << ' '
     1004                  << _arcWidths[e]*_arcWidthScale << " l\n";
     1005        }
    10061006      os << "grestore\n";
    10071007    }
     
    10091009      os << "%Nodes:\ngsave\n";
    10101010      for(NodeIt n(g);n!=INVALID;++n) {
    1011         os << mycoords[n].x << ' ' << mycoords[n].y << ' '
    1012            << _nodeSizes[n]*_nodeScale << ' '
    1013            << _nodeColors[n].red() << ' '
    1014            << _nodeColors[n].green() << ' '
    1015            << _nodeColors[n].blue() << ' ';
    1016         switch(_nodeShapes[n]) {
    1017         case CIRCLE:
    1018           os<< "nc";break;
    1019         case SQUARE:
    1020           os<< "nsq";break;
    1021         case DIAMOND:
    1022           os<< "ndi";break;
    1023         case MALE:
    1024           os<< "nmale";break;
    1025         case FEMALE:
    1026           os<< "nfemale";break;
    1027         }
    1028         os<<'\n';
     1011        os << mycoords[n].x << ' ' << mycoords[n].y << ' '
     1012           << _nodeSizes[n]*_nodeScale << ' '
     1013           << _nodeColors[n].red() << ' '
     1014           << _nodeColors[n].green() << ' '
     1015           << _nodeColors[n].blue() << ' ';
     1016        switch(_nodeShapes[n]) {
     1017        case CIRCLE:
     1018          os<< "nc";break;
     1019        case SQUARE:
     1020          os<< "nsq";break;
     1021        case DIAMOND:
     1022          os<< "ndi";break;
     1023        case MALE:
     1024          os<< "nmale";break;
     1025        case FEMALE:
     1026          os<< "nfemale";break;
     1027        }
     1028        os<<'\n';
    10291029      }
    10301030      os << "grestore\n";
     
    10351035      os << "(Helvetica) findfont fosi scalefont setfont\n";
    10361036      for(NodeIt n(g);n!=INVALID;++n) {
    1037         switch(_nodeTextColorType) {
    1038         case DIST_COL:
    1039           os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n";
    1040           break;
    1041         case DIST_BW:
    1042           os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n";
    1043           break;
    1044         case CUST_COL:
    1045           os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n";
    1046           break;
    1047         default:
    1048           os << "0 0 0 setrgbcolor\n";
    1049         }
    1050         os << mycoords[n].x << ' ' << mycoords[n].y
    1051            << " (" << _nodeTexts[n] << ") cshow\n";
     1037        switch(_nodeTextColorType) {
     1038        case DIST_COL:
     1039          os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n";
     1040          break;
     1041        case DIST_BW:
     1042          os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n";
     1043          break;
     1044        case CUST_COL:
     1045          os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n";
     1046          break;
     1047        default:
     1048          os << "0 0 0 setrgbcolor\n";
     1049        }
     1050        os << mycoords[n].x << ' ' << mycoords[n].y
     1051           << " (" << _nodeTexts[n] << ") cshow\n";
    10521052      }
    10531053      os << "grestore\n";
     
    10561056      os << "%Node PS blocks:\ngsave\n";
    10571057      for(NodeIt n(g);n!=INVALID;++n)
    1058         os << mycoords[n].x << ' ' << mycoords[n].y
    1059            << " moveto\n" << _nodePsTexts[n] << "\n";
     1058        os << mycoords[n].x << ' ' << mycoords[n].y
     1059           << " moveto\n" << _nodePsTexts[n] << "\n";
    10601060      os << "grestore\n";
    10611061    }
    1062    
     1062
    10631063    os << "grestore\nshowpage\n";
    10641064
     
    10931093    return autoArcWidthScale(b);
    10941094  }
    1095  
     1095
    10961096  ///An alias for absoluteArcWidths()
    10971097  GraphToEps<T> &absoluteEdgeWidths(bool b=true)
     
    10991099    return absoluteArcWidths(b);
    11001100  }
    1101  
     1101
    11021102  ///An alias for parArcDist()
    11031103  GraphToEps<T> &parEdgeDist(double d) {return parArcDist(d);}
    1104  
     1104
    11051105  ///An alias for hideArcs()
    11061106  GraphToEps<T> &hideEdges(bool b=true) {return hideArcs(b);}
     
    11441144///\sa graphToEps(G &g, const char *file_name)
    11451145template<class G>
    1146 GraphToEps<DefaultGraphToEpsTraits<G> > 
     1146GraphToEps<DefaultGraphToEpsTraits<G> >
    11471147graphToEps(G &g, std::ostream& os=std::cout)
    11481148{
    1149   return 
     1149  return
    11501150    GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
    11511151}
    1152  
     1152
    11531153///Generates an EPS file from a graph
    11541154
     
    11601160///\sa graphToEps(G &g, std::ostream& os)
    11611161template<class G>
    1162 GraphToEps<DefaultGraphToEpsTraits<G> > 
     1162GraphToEps<DefaultGraphToEpsTraits<G> >
    11631163graphToEps(G &g,const char *file_name)
    11641164{
     
    11761176///\sa graphToEps(G &g, std::ostream& os)
    11771177template<class G>
    1178 GraphToEps<DefaultGraphToEpsTraits<G> > 
     1178GraphToEps<DefaultGraphToEpsTraits<G> >
    11791179graphToEps(G &g,const std::string& file_name)
    11801180{
Note: See TracChangeset for help on using the changeset viewer.