src/work/alpar/graph_to_eps.cc
changeset 1051 4ebe32765b48
parent 1050 bcc0766a7b86
child 1055 f901ff02b2d7
     1.1 --- a/src/work/alpar/graph_to_eps.cc	Tue Jan 04 22:16:46 2005 +0000
     1.2 +++ b/src/work/alpar/graph_to_eps.cc	Wed Jan 05 10:14:37 2005 +0000
     1.3 @@ -1,4 +1,6 @@
     1.4 +#include <iostream>
     1.5  #include<math.h>
     1.6 +
     1.7  #include<lemon/xy.h>
     1.8  #include<lemon/maps.h>
     1.9  #include<lemon/list_graph.h>
    1.10 @@ -49,6 +51,9 @@
    1.11    
    1.12  
    1.13    const Graph &g;
    1.14 +
    1.15 +  std::ostream& os;
    1.16 +  
    1.17    ConstMap<typename Graph::Node,xy<double> > _coords;
    1.18    ConstMap<typename Graph::Node,double > _nodeSizes;
    1.19  
    1.20 @@ -70,9 +75,12 @@
    1.21    ///Constructor
    1.22  
    1.23    ///Constructor
    1.24 -  ///\param _g is a reference to the underlying graph
    1.25 -  DefaultGraphToEpsTraits(const G &_g) :
    1.26 -    g(_g), _coords(xy<double>(1,1)), _nodeSizes(1.0),
    1.27 +  ///\param _g is a reference to the graph to be printed
    1.28 +  ///\param _os is a reference to the output stream.
    1.29 +  ///By default it is <tt>std::cout</tt>
    1.30 +  DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout) :
    1.31 +    g(_g), os(_os),
    1.32 +    _coords(xy<double>(1,1)), _nodeSizes(1.0),
    1.33      _nodeColors(Color(1,1,1)), _edgeColors(Color(0,0,0)),
    1.34      _edgeWidths(1), _edgeWidthScale(0.3),
    1.35      _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0),
    1.36 @@ -214,7 +222,7 @@
    1.37    {
    1.38      if(dontPrint) return;
    1.39      
    1.40 -    cout << "%!PS-Adobe-2.0 EPSF-2.0\n";
    1.41 +    os << "%!PS-Adobe-2.0 EPSF-2.0\n";
    1.42      //\todo: Chech whether the graph is empty.
    1.43      BoundingBox<double> bb;
    1.44      for(NodeIt n(g);n!=INVALID;++n) {
    1.45 @@ -223,26 +231,26 @@
    1.46        bb+=p+_coords[n];
    1.47        bb+=-p+_coords[n];
    1.48        }
    1.49 -    cout << "%%BoundingBox: "
    1.50 +    os << "%%BoundingBox: "
    1.51  	 << bb.left()*  _scale-_xBorder << ' '
    1.52  	 << bb.bottom()*_scale-_yBorder << ' '
    1.53  	 << bb.right()* _scale+_xBorder << ' '
    1.54  	 << bb.top()*   _scale+_yBorder << '\n';
    1.55      //x1 y1 x2 y2 cr cg cb w
    1.56 -    cout << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
    1.57 -    cout << "/c { newpath dup 3 index add 2 index moveto 0 360 arc } bind def\n";
    1.58 +    os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
    1.59 +    os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc } bind def\n";
    1.60      // x y r cr cg cb
    1.61 -    cout << "/n { setrgbcolor 2 index 2 index 2 index c fill\n"
    1.62 +    os << "/n { setrgbcolor 2 index 2 index 2 index c fill\n"
    1.63  	 << "     0 0 0 setrgbcolor dup "
    1.64  	 << _nodeBorderQuotient << " mul setlinewidth "
    1.65  	 << 1+_nodeBorderQuotient/2 << " div c stroke\n"
    1.66  	 << "   } bind def\n";
    1.67 -    cout << "/arrl " << _arrowLength << " def\n";
    1.68 -    cout << "/arrw " << _arrowWidth << " def\n";
    1.69 +    os << "/arrl " << _arrowLength << " def\n";
    1.70 +    os << "/arrw " << _arrowWidth << " def\n";
    1.71      // l dx_norm dy_norm
    1.72 -    cout << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
    1.73 +    os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
    1.74      //len w dx_norm dy_norm x1 y1 cr cg cb
    1.75 -    cout << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
    1.76 +    os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
    1.77  	 << "       /w exch def /len exch def\n"
    1.78        //	 << "       0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
    1.79  	 << "       newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
    1.80 @@ -255,9 +263,9 @@
    1.81  	 << "       arrw dy dx neg lrl\n"
    1.82  	 << "       len w sub arrl sub neg dx dy lrl\n"
    1.83  	 << "       closepath fill } bind def\n";
    1.84 -    cout << "\ngsave\n";
    1.85 -    if(_scale!=1.0) cout << _scale << " dup scale\n";
    1.86 -    cout << "%Edges:\ngsave\n";
    1.87 +    os << "\ngsave\n";
    1.88 +    if(_scale!=1.0) os << _scale << " dup scale\n";
    1.89 +    os << "%Edges:\ngsave\n";
    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 @@ -266,7 +274,7 @@
    1.94  	  d/=l;
    1.95  	  xy<double> x1(d*_nodeScale*_nodeSizes[g.source(e)]+
    1.96  			_coords[g.source(e)]);
    1.97 -	  cout << l-(_nodeSizes[g.source(e)]+
    1.98 +	  os << l-(_nodeSizes[g.source(e)]+
    1.99  		     _nodeSizes[g.target(e)])*_nodeScale << ' '
   1.100  	       << _edgeWidths[e]*_edgeWidthScale << ' '
   1.101  	       << d.x << ' ' << d.y << ' '
   1.102 @@ -275,7 +283,7 @@
   1.103  	       << _edgeColors[e].getG() << ' '
   1.104  	       << _edgeColors[e].getB() << " arr\n";
   1.105  	}
   1.106 -    	else cout << _coords[g.source(e)].x << ' '
   1.107 +    	else os << _coords[g.source(e)].x << ' '
   1.108  		  << _coords[g.source(e)].y << ' '
   1.109  		  << _coords[g.target(e)].x << ' '
   1.110  		  << _coords[g.target(e)].y << ' '
   1.111 @@ -283,14 +291,14 @@
   1.112  		  << _edgeColors[e].getG() << ' '
   1.113  		  << _edgeColors[e].getB() << ' '
   1.114  		  << _edgeWidths[e]*_edgeWidthScale << " l\n";
   1.115 -    cout << "grestore\n%Nodes:\ngsave\n";
   1.116 +    os << "grestore\n%Nodes:\ngsave\n";
   1.117      for(NodeIt n(g);n!=INVALID;++n)
   1.118 -      cout << _coords[n].x << ' ' << _coords[n].y << ' '
   1.119 +      os << _coords[n].x << ' ' << _coords[n].y << ' '
   1.120  	   << _nodeSizes[n]*_nodeScale << ' '
   1.121  	   << _nodeColors[n].getR() << ' '
   1.122  	   << _nodeColors[n].getG() << ' '
   1.123  	   << _nodeColors[n].getB() << " n\n"; 
   1.124 -    cout << "grestore\ngrestore\n";
   1.125 +    os << "grestore\ngrestore\n";
   1.126    } 
   1.127  };
   1.128  
   1.129 @@ -299,8 +307,11 @@
   1.130  
   1.131  ///\ingroup misc
   1.132  ///Generates an EPS file from a graph.
   1.133 +///\param g is a reference to the graph to be printed
   1.134 +///\param os is a reference to the output stream.
   1.135 +///By default it is <tt>std::cout</tt>
   1.136  ///
   1.137 -///This function has a lot of \ref named-templ-param "named parameters",
   1.138 +///This function also has a lot of \ref named-templ-param "named parameters",
   1.139  ///they are declared as the members of class \ref GraphToEps. The following
   1.140  ///example shows how to use these parameters.
   1.141  ///\code
   1.142 @@ -310,7 +321,7 @@
   1.143  ///\endcode
   1.144  ///\sa GraphToEps
   1.145  template<class G>
   1.146 -GraphToEps<DefaultGraphToEpsTraits<G> > graphToEps(G &g)
   1.147 +GraphToEps<DefaultGraphToEpsTraits<G> > graphToEps(G &g,std::ostream& os=std::cout)
   1.148  {
   1.149    return GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g));
   1.150  }