COIN-OR::LEMON - Graph Library

Ignore:
Timestamp:
01/05/05 11:14:37 (20 years ago)
Author:
Alpar Juttner
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1445
Message:

graphToEps is now able to write to any ostream.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/work/alpar/graph_to_eps.cc

    r1050 r1051  
     1#include <iostream>
    12#include<math.h>
     3
    24#include<lemon/xy.h>
    35#include<lemon/maps.h>
     
    5052
    5153  const Graph &g;
     54
     55  std::ostream& os;
     56 
    5257  ConstMap<typename Graph::Node,xy<double> > _coords;
    5358  ConstMap<typename Graph::Node,double > _nodeSizes;
     
    7176
    7277  ///Constructor
    73   ///\param _g is a reference to the underlying graph
    74   DefaultGraphToEpsTraits(const G &_g) :
    75     g(_g), _coords(xy<double>(1,1)), _nodeSizes(1.0),
     78  ///\param _g is a reference to the graph to be printed
     79  ///\param _os is a reference to the output stream.
     80  ///By default it is <tt>std::cout</tt>
     81  DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout) :
     82    g(_g), os(_os),
     83    _coords(xy<double>(1,1)), _nodeSizes(1.0),
    7684    _nodeColors(Color(1,1,1)), _edgeColors(Color(0,0,0)),
    7785    _edgeWidths(1), _edgeWidthScale(0.3),
     
    215223    if(dontPrint) return;
    216224   
    217     cout << "%!PS-Adobe-2.0 EPSF-2.0\n";
     225    os << "%!PS-Adobe-2.0 EPSF-2.0\n";
    218226    //\todo: Chech whether the graph is empty.
    219227    BoundingBox<double> bb;
     
    224232      bb+=-p+_coords[n];
    225233      }
    226     cout << "%%BoundingBox: "
     234    os << "%%BoundingBox: "
    227235         << bb.left()*  _scale-_xBorder << ' '
    228236         << bb.bottom()*_scale-_yBorder << ' '
     
    230238         << bb.top()*   _scale+_yBorder << '\n';
    231239    //x1 y1 x2 y2 cr cg cb w
    232     cout << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
    233     cout << "/c { newpath dup 3 index add 2 index moveto 0 360 arc } bind def\n";
     240    os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
     241    os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc } bind def\n";
    234242    // x y r cr cg cb
    235     cout << "/n { setrgbcolor 2 index 2 index 2 index c fill\n"
     243    os << "/n { setrgbcolor 2 index 2 index 2 index c fill\n"
    236244         << "     0 0 0 setrgbcolor dup "
    237245         << _nodeBorderQuotient << " mul setlinewidth "
    238246         << 1+_nodeBorderQuotient/2 << " div c stroke\n"
    239247         << "   } bind def\n";
    240     cout << "/arrl " << _arrowLength << " def\n";
    241     cout << "/arrw " << _arrowWidth << " def\n";
     248    os << "/arrl " << _arrowLength << " def\n";
     249    os << "/arrw " << _arrowWidth << " def\n";
    242250    // l dx_norm dy_norm
    243     cout << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
     251    os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
    244252    //len w dx_norm dy_norm x1 y1 cr cg cb
    245     cout << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
     253    os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
    246254         << "       /w exch def /len exch def\n"
    247255      //         << "       0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
     
    256264         << "       len w sub arrl sub neg dx dy lrl\n"
    257265         << "       closepath fill } bind def\n";
    258     cout << "\ngsave\n";
    259     if(_scale!=1.0) cout << _scale << " dup scale\n";
    260     cout << "%Edges:\ngsave\n";
     266    os << "\ngsave\n";
     267    if(_scale!=1.0) os << _scale << " dup scale\n";
     268    os << "%Edges:\ngsave\n";
    261269    for(NodeIt n(g);n!=INVALID;++n)
    262270      for(OutEdgeIt e(g,n);e!=INVALID;++e)
     
    267275          xy<double> x1(d*_nodeScale*_nodeSizes[g.source(e)]+
    268276                        _coords[g.source(e)]);
    269           cout << l-(_nodeSizes[g.source(e)]+
     277          os << l-(_nodeSizes[g.source(e)]+
    270278                     _nodeSizes[g.target(e)])*_nodeScale << ' '
    271279               << _edgeWidths[e]*_edgeWidthScale << ' '
     
    276284               << _edgeColors[e].getB() << " arr\n";
    277285        }
    278         else cout << _coords[g.source(e)].x << ' '
     286        else os << _coords[g.source(e)].x << ' '
    279287                  << _coords[g.source(e)].y << ' '
    280288                  << _coords[g.target(e)].x << ' '
     
    284292                  << _edgeColors[e].getB() << ' '
    285293                  << _edgeWidths[e]*_edgeWidthScale << " l\n";
    286     cout << "grestore\n%Nodes:\ngsave\n";
     294    os << "grestore\n%Nodes:\ngsave\n";
    287295    for(NodeIt n(g);n!=INVALID;++n)
    288       cout << _coords[n].x << ' ' << _coords[n].y << ' '
     296      os << _coords[n].x << ' ' << _coords[n].y << ' '
    289297           << _nodeSizes[n]*_nodeScale << ' '
    290298           << _nodeColors[n].getR() << ' '
    291299           << _nodeColors[n].getG() << ' '
    292300           << _nodeColors[n].getB() << " n\n";
    293     cout << "grestore\ngrestore\n";
     301    os << "grestore\ngrestore\n";
    294302  }
    295303};
     
    300308///\ingroup misc
    301309///Generates an EPS file from a graph.
     310///\param g is a reference to the graph to be printed
     311///\param os is a reference to the output stream.
     312///By default it is <tt>std::cout</tt>
    302313///
    303 ///This function has a lot of \ref named-templ-param "named parameters",
     314///This function also has a lot of \ref named-templ-param "named parameters",
    304315///they are declared as the members of class \ref GraphToEps. The following
    305316///example shows how to use these parameters.
     
    311322///\sa GraphToEps
    312323template<class G>
    313 GraphToEps<DefaultGraphToEpsTraits<G> > graphToEps(G &g)
     324GraphToEps<DefaultGraphToEpsTraits<G> > graphToEps(G &g,std::ostream& os=std::cout)
    314325{
    315326  return GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g));
Note: See TracChangeset for help on using the changeset viewer.