graphToEps also accepts an output file name parameter.
authoralpar
Thu, 06 Jan 2005 08:39:50 +0000
changeset 1055f901ff02b2d7
parent 1054 6a62b1b4cf23
child 1056 cbc27743e17a
graphToEps also accepts an output file name parameter.
src/work/alpar/graph_to_eps.cc
     1.1 --- a/src/work/alpar/graph_to_eps.cc	Wed Jan 05 16:59:50 2005 +0000
     1.2 +++ b/src/work/alpar/graph_to_eps.cc	Thu Jan 06 08:39:50 2005 +0000
     1.3 @@ -1,4 +1,6 @@
     1.4  #include <iostream>
     1.5 +#include <fstream>
     1.6 +#include <algorithm>
     1.7  #include<math.h>
     1.8  
     1.9  #include<lemon/xy.h>
    1.10 @@ -72,20 +74,28 @@
    1.11    bool _drawArrows;
    1.12    double _arrowLength, _arrowWidth;
    1.13    
    1.14 +  bool _enableParallel;
    1.15 +
    1.16 +  bool _pleaseRemoveOsStream;
    1.17    ///Constructor
    1.18  
    1.19    ///Constructor
    1.20    ///\param _g is a reference to the graph to be printed
    1.21    ///\param _os is a reference to the output stream.
    1.22 +  ///\param _os is a reference to the output stream.
    1.23 +  ///\param _pros If it is \c true, then the \c ostream referenced by \c _os
    1.24 +  ///will be explicitly deallocated by the destructor.
    1.25    ///By default it is <tt>std::cout</tt>
    1.26 -  DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout) :
    1.27 +  DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
    1.28 +			  bool _pros=false) :
    1.29      g(_g), os(_os),
    1.30      _coords(xy<double>(1,1)), _nodeSizes(1.0),
    1.31      _nodeColors(Color(1,1,1)), _edgeColors(Color(0,0,0)),
    1.32      _edgeWidths(1), _edgeWidthScale(0.3),
    1.33      _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0),
    1.34      _nodeBorderQuotient(.1),
    1.35 -    _drawArrows(false), _arrowLength(1), _arrowWidth(0.3) {}
    1.36 +    _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
    1.37 +    _enableParallel(false), _pleaseRemoveOsStream(_pros) {}
    1.38  };
    1.39  
    1.40  ///Helper class to implement the named parameters of \ref graphToEps()
    1.41 @@ -105,6 +115,22 @@
    1.42  
    1.43    bool dontPrint;
    1.44  
    1.45 +  class edgeLess {
    1.46 +    const Graph &g;
    1.47 +  public:
    1.48 +    edgeLess(const Graph &_g) : g(_g) {}
    1.49 +    bool operator()(Edge a,Edge b) const 
    1.50 +    {
    1.51 +      Node ai=min(g.source(a),g.target(a));
    1.52 +      Node aa=max(g.source(a),g.target(a));
    1.53 +      Node bi=min(g.source(b),g.target(b));
    1.54 +      Node ba=max(g.source(b),g.target(b));
    1.55 +      return ai<bi ||
    1.56 +	(ai==bi && (aa < ba || 
    1.57 +		    (aa==ba && ai==g.source(a) && bi==g.target(b))));
    1.58 +    }
    1.59 +  };
    1.60 +    
    1.61  public:
    1.62    GraphToEps(const T &t) : T(t), dontPrint(false) {};
    1.63    
    1.64 @@ -218,6 +244,12 @@
    1.65    ///
    1.66    GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;}
    1.67    
    1.68 +  ///Enables parallel edges
    1.69 +
    1.70 +  ///Enables parallel edges
    1.71 +  ///\todo Unimplemented
    1.72 +  GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
    1.73 +  
    1.74    ~GraphToEps() 
    1.75    {
    1.76      if(dontPrint) return;
    1.77 @@ -265,7 +297,15 @@
    1.78  	 << "       closepath fill } bind def\n";
    1.79      os << "\ngsave\n";
    1.80      if(_scale!=1.0) os << _scale << " dup scale\n";
    1.81 +    
    1.82      os << "%Edges:\ngsave\n";
    1.83 +    
    1.84 +    vector<Edge> el;
    1.85 +    if(_enableParallel) {
    1.86 +      for(EdgeIt e(g);e!=INVALID;++e) el.push_back(e);
    1.87 +      sort(el.begin(),el.end(),edgeLess(g));
    1.88 +    }
    1.89 +    
    1.90      for(NodeIt n(g);n!=INVALID;++n)
    1.91        for(OutEdgeIt e(g,n);e!=INVALID;++e)
    1.92  	if(_drawArrows) {
    1.93 @@ -299,6 +339,10 @@
    1.94  	   << _nodeColors[n].getG() << ' '
    1.95  	   << _nodeColors[n].getB() << " n\n"; 
    1.96      os << "grestore\ngrestore\n";
    1.97 +
    1.98 +
    1.99 +    //CleanUp:
   1.100 +    if(_pleaseRemoveOsStream) {delete &os;}
   1.101    } 
   1.102  };
   1.103  
   1.104 @@ -321,11 +365,40 @@
   1.105  ///\endcode
   1.106  ///\sa GraphToEps
   1.107  template<class G>
   1.108 -GraphToEps<DefaultGraphToEpsTraits<G> > graphToEps(G &g,std::ostream& os=std::cout)
   1.109 +GraphToEps<DefaultGraphToEpsTraits<G> > 
   1.110 +graphToEps(G &g,std::ostream& os=std::cout)
   1.111  {
   1.112 -  return GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g));
   1.113 +  return 
   1.114 +    GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
   1.115  }
   1.116   
   1.117 +///Generates an EPS file from a graph
   1.118 +
   1.119 +///\ingroup misc
   1.120 +///Generates an EPS file from a graph.
   1.121 +///\param g is a reference to the graph to be printed
   1.122 +///\param file_name is the output file_name.
   1.123 +///
   1.124 +///This function also has a lot of \ref named-templ-param "named parameters",
   1.125 +///they are declared as the members of class \ref GraphToEps. The following
   1.126 +///example shows how to use these parameters.
   1.127 +///\code
   1.128 +/// graphToEps(g).scale(10).coords(coords)
   1.129 +///              .nodeScale(2).nodeSizes(sizes)
   1.130 +///              .edgeWidthScale(.4);
   1.131 +///\endcode
   1.132 +///\sa GraphToEps
   1.133 +///\todo Avoid duplicated documentation
   1.134 +///\bug Exception handling is missing? (Or we can just ignore it?)
   1.135 +template<class G>
   1.136 +GraphToEps<DefaultGraphToEpsTraits<G> > 
   1.137 +graphToEps(G &g,char *file_name)
   1.138 +{
   1.139 +  return GraphToEps<DefaultGraphToEpsTraits<G> >
   1.140 +    (DefaultGraphToEpsTraits<G>(g,*new ofstream(file_name),true));
   1.141 +}
   1.142 +
   1.143 +
   1.144  }
   1.145  
   1.146  using namespace lemon;
   1.147 @@ -385,7 +458,12 @@
   1.148    e=g.addEdge(n2,n4); ecolors[e]=1; widths[e]=2;
   1.149    e=g.addEdge(n3,n4); ecolors[e]=2; widths[e]=1;
   1.150    
   1.151 -  graphToEps(g).scale(10).coords(coords).
   1.152 +  graphToEps(g,"proba.eps").scale(10).coords(coords).
   1.153 +    nodeScale(2).nodeSizes(sizes).
   1.154 +    nodeColors(composeMap(colorSet,colors)).
   1.155 +    edgeColors(composeMap(colorSet,ecolors)).
   1.156 +    edgeWidthScale(.4).edgeWidths(widths);
   1.157 +  graphToEps(g,"proba_arr.eps").scale(10).coords(coords).
   1.158      nodeScale(2).nodeSizes(sizes).
   1.159      nodeColors(composeMap(colorSet,colors)).
   1.160      edgeColors(composeMap(colorSet,ecolors)).