COIN-OR::LEMON - Graph Library

Changeset 1055:f901ff02b2d7 in lemon-0.x for src/work/alpar


Ignore:
Timestamp:
01/06/05 09:39:50 (19 years ago)
Author:
Alpar Juttner
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1449
Message:

graphToEps also accepts an output file name parameter.

File:
1 edited

Legend:

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

    r1051 r1055  
    11#include <iostream>
     2#include <fstream>
     3#include <algorithm>
    24#include<math.h>
    35
     
    7375  double _arrowLength, _arrowWidth;
    7476 
     77  bool _enableParallel;
     78
     79  bool _pleaseRemoveOsStream;
    7580  ///Constructor
    7681
     
    7883  ///\param _g is a reference to the graph to be printed
    7984  ///\param _os is a reference to the output stream.
     85  ///\param _os is a reference to the output stream.
     86  ///\param _pros If it is \c true, then the \c ostream referenced by \c _os
     87  ///will be explicitly deallocated by the destructor.
    8088  ///By default it is <tt>std::cout</tt>
    81   DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout) :
     89  DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
     90                          bool _pros=false) :
    8291    g(_g), os(_os),
    8392    _coords(xy<double>(1,1)), _nodeSizes(1.0),
     
    8695    _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0),
    8796    _nodeBorderQuotient(.1),
    88     _drawArrows(false), _arrowLength(1), _arrowWidth(0.3) {}
     97    _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
     98    _enableParallel(false), _pleaseRemoveOsStream(_pros) {}
    8999};
    90100
     
    106116  bool dontPrint;
    107117
     118  class edgeLess {
     119    const Graph &g;
     120  public:
     121    edgeLess(const Graph &_g) : g(_g) {}
     122    bool operator()(Edge a,Edge b) const
     123    {
     124      Node ai=min(g.source(a),g.target(a));
     125      Node aa=max(g.source(a),g.target(a));
     126      Node bi=min(g.source(b),g.target(b));
     127      Node ba=max(g.source(b),g.target(b));
     128      return ai<bi ||
     129        (ai==bi && (aa < ba ||
     130                    (aa==ba && ai==g.source(a) && bi==g.target(b))));
     131    }
     132  };
     133   
    108134public:
    109135  GraphToEps(const T &t) : T(t), dontPrint(false) {};
     
    218244  ///
    219245  GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;}
     246 
     247  ///Enables parallel edges
     248
     249  ///Enables parallel edges
     250  ///\todo Unimplemented
     251  GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
    220252 
    221253  ~GraphToEps()
     
    266298    os << "\ngsave\n";
    267299    if(_scale!=1.0) os << _scale << " dup scale\n";
     300   
    268301    os << "%Edges:\ngsave\n";
     302   
     303    vector<Edge> el;
     304    if(_enableParallel) {
     305      for(EdgeIt e(g);e!=INVALID;++e) el.push_back(e);
     306      sort(el.begin(),el.end(),edgeLess(g));
     307    }
     308   
    269309    for(NodeIt n(g);n!=INVALID;++n)
    270310      for(OutEdgeIt e(g,n);e!=INVALID;++e)
     
    300340           << _nodeColors[n].getB() << " n\n";
    301341    os << "grestore\ngrestore\n";
     342
     343
     344    //CleanUp:
     345    if(_pleaseRemoveOsStream) {delete &os;}
    302346  }
    303347};
     
    322366///\sa GraphToEps
    323367template<class G>
    324 GraphToEps<DefaultGraphToEpsTraits<G> > graphToEps(G &g,std::ostream& os=std::cout)
    325 {
    326   return GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g));
     368GraphToEps<DefaultGraphToEpsTraits<G> >
     369graphToEps(G &g,std::ostream& os=std::cout)
     370{
     371  return
     372    GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
    327373}
    328374 
     375///Generates an EPS file from a graph
     376
     377///\ingroup misc
     378///Generates an EPS file from a graph.
     379///\param g is a reference to the graph to be printed
     380///\param file_name is the output file_name.
     381///
     382///This function also has a lot of \ref named-templ-param "named parameters",
     383///they are declared as the members of class \ref GraphToEps. The following
     384///example shows how to use these parameters.
     385///\code
     386/// graphToEps(g).scale(10).coords(coords)
     387///              .nodeScale(2).nodeSizes(sizes)
     388///              .edgeWidthScale(.4);
     389///\endcode
     390///\sa GraphToEps
     391///\todo Avoid duplicated documentation
     392///\bug Exception handling is missing? (Or we can just ignore it?)
     393template<class G>
     394GraphToEps<DefaultGraphToEpsTraits<G> >
     395graphToEps(G &g,char *file_name)
     396{
     397  return GraphToEps<DefaultGraphToEpsTraits<G> >
     398    (DefaultGraphToEpsTraits<G>(g,*new ofstream(file_name),true));
     399}
     400
     401
    329402}
    330403
     
    386459  e=g.addEdge(n3,n4); ecolors[e]=2; widths[e]=1;
    387460 
    388   graphToEps(g).scale(10).coords(coords).
     461  graphToEps(g,"proba.eps").scale(10).coords(coords).
     462    nodeScale(2).nodeSizes(sizes).
     463    nodeColors(composeMap(colorSet,colors)).
     464    edgeColors(composeMap(colorSet,ecolors)).
     465    edgeWidthScale(.4).edgeWidths(widths);
     466  graphToEps(g,"proba_arr.eps").scale(10).coords(coords).
    389467    nodeScale(2).nodeSizes(sizes).
    390468    nodeColors(composeMap(colorSet,colors)).
Note: See TracChangeset for help on using the changeset viewer.