alpar@1062: /* -*- C++ -*- alpar@1062: * src/lemon/graph_to_eps.h - Part of LEMON, a generic C++ optimization library alpar@1062: * alpar@1062: * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@1062: * (Egervary Combinatorial Optimization Research Group, EGRES). alpar@1062: * alpar@1062: * Permission to use, modify and distribute this software is granted alpar@1062: * provided that this copyright notice appears in all copies. For alpar@1062: * precise terms see the accompanying LICENSE file. alpar@1062: * alpar@1062: * This software is provided "AS IS" with no warranty of any kind, alpar@1062: * express or implied, and with no claim as to its suitability for any alpar@1062: * purpose. alpar@1062: * alpar@1062: */ alpar@1062: alpar@1051: #include alpar@1055: #include alpar@1055: #include alpar@1050: #include alpar@1051: alpar@1046: #include alpar@1046: #include alpar@1046: #include alpar@1046: alpar@1050: alpar@1062: ///\ingroup misc alpar@1062: ///\file alpar@1062: ///\brief Simple graph drawer alpar@1046: alpar@1046: namespace lemon { alpar@1046: alpar@1050: ///Data structure representing RGB colors. alpar@1050: alpar@1050: ///Data structure representing RGB colors. alpar@1050: ///\ingroup misc alpar@1046: class Color alpar@1046: { alpar@1046: double _r,_g,_b; alpar@1046: public: alpar@1050: ///Default constructor alpar@1046: Color() {} alpar@1050: ///Constructor alpar@1046: Color(double r,double g,double b) :_r(r),_g(g),_b(b) {}; alpar@1050: ///Returns the red component alpar@1046: double getR() {return _r;} alpar@1050: ///Returns the green component alpar@1046: double getG() {return _g;} alpar@1050: ///Returns the blue component alpar@1046: double getB() {return _b;} alpar@1050: ///Set the color components alpar@1046: void set(double r,double g,double b) { _r=r;_g=g;_b=b; }; alpar@1046: }; alpar@1046: alpar@1050: ///Default traits class of \ref GraphToEps alpar@1050: alpar@1050: ///Default traits class of \ref GraphToEps alpar@1050: /// alpar@1050: ///\c G is the type of the underlying graph. alpar@1046: template alpar@1046: struct DefaultGraphToEpsTraits alpar@1046: { alpar@1046: typedef G Graph; alpar@1046: typedef typename Graph::Node Node; alpar@1046: typedef typename Graph::NodeIt NodeIt; alpar@1046: typedef typename Graph::Edge Edge; alpar@1046: typedef typename Graph::EdgeIt EdgeIt; alpar@1046: typedef typename Graph::InEdgeIt InEdgeIt; alpar@1046: typedef typename Graph::OutEdgeIt OutEdgeIt; alpar@1046: alpar@1046: alpar@1046: const Graph &g; alpar@1051: alpar@1051: std::ostream& os; alpar@1051: alpar@1050: ConstMap > _coords; alpar@1050: ConstMap _nodeSizes; alpar@1046: alpar@1050: ConstMap _nodeColors; alpar@1050: ConstMap _edgeColors; alpar@1050: alpar@1050: ConstMap _edgeWidths; alpar@1050: alpar@1050: double _edgeWidthScale; alpar@1050: alpar@1050: double _nodeScale; alpar@1050: double _xBorder, _yBorder; alpar@1050: double _scale; alpar@1050: double _nodeBorderQuotient; alpar@1050: alpar@1050: bool _drawArrows; alpar@1050: double _arrowLength, _arrowWidth; alpar@1050: alpar@1062: bool _showNodes, _showEdges; alpar@1062: alpar@1055: bool _enableParallel; alpar@1062: double _parEdgeDist; alpar@1062: alpar@1062: bool _showNodeText; alpar@1062: ConstMap _nodeTexts; alpar@1062: double _nodeTextSize; alpar@1055: alpar@1055: bool _pleaseRemoveOsStream; alpar@1050: ///Constructor alpar@1050: alpar@1050: ///Constructor alpar@1051: ///\param _g is a reference to the graph to be printed alpar@1051: ///\param _os is a reference to the output stream. alpar@1055: ///\param _os is a reference to the output stream. alpar@1055: ///\param _pros If it is \c true, then the \c ostream referenced by \c _os alpar@1055: ///will be explicitly deallocated by the destructor. alpar@1051: ///By default it is std::cout alpar@1055: DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout, alpar@1055: bool _pros=false) : alpar@1051: g(_g), os(_os), alpar@1051: _coords(xy(1,1)), _nodeSizes(1.0), alpar@1050: _nodeColors(Color(1,1,1)), _edgeColors(Color(0,0,0)), alpar@1050: _edgeWidths(1), _edgeWidthScale(0.3), alpar@1050: _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0), alpar@1050: _nodeBorderQuotient(.1), alpar@1055: _drawArrows(false), _arrowLength(1), _arrowWidth(0.3), alpar@1062: _showNodes(true), _showEdges(true), alpar@1062: _enableParallel(false), _parEdgeDist(1), alpar@1062: _showNodeText(false), _nodeTexts(false), _nodeTextSize(1), alpar@1062: _pleaseRemoveOsStream(_pros) {} alpar@1046: }; alpar@1046: alpar@1050: ///Helper class to implement the named parameters of \ref graphToEps() alpar@1050: alpar@1050: ///Helper class to implement the named parameters of \ref graphToEps() alpar@1050: ///\todo Is 'helper class' a good name for this? alpar@1050: /// alpar@1046: template class GraphToEps : public T alpar@1046: { alpar@1046: typedef typename T::Graph Graph; alpar@1046: typedef typename Graph::Node Node; alpar@1046: typedef typename Graph::NodeIt NodeIt; alpar@1046: typedef typename Graph::Edge Edge; alpar@1046: typedef typename Graph::EdgeIt EdgeIt; alpar@1046: typedef typename Graph::InEdgeIt InEdgeIt; alpar@1046: typedef typename Graph::OutEdgeIt OutEdgeIt; alpar@1046: alpar@1046: bool dontPrint; alpar@1046: alpar@1055: class edgeLess { alpar@1055: const Graph &g; alpar@1055: public: alpar@1055: edgeLess(const Graph &_g) : g(_g) {} alpar@1055: bool operator()(Edge a,Edge b) const alpar@1055: { alpar@1055: Node ai=min(g.source(a),g.target(a)); alpar@1055: Node aa=max(g.source(a),g.target(a)); alpar@1055: Node bi=min(g.source(b),g.target(b)); alpar@1055: Node ba=max(g.source(b),g.target(b)); alpar@1055: return ai rot(xy v) alpar@1062: { alpar@1062: return xy(v.y,-v.x); alpar@1062: } alpar@1062: alpar@1046: public: alpar@1046: GraphToEps(const T &t) : T(t), dontPrint(false) {}; alpar@1046: alpar@1050: template struct CoordsTraits : public T { alpar@1050: const X &_coords; alpar@1050: CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {} alpar@1046: }; alpar@1050: ///Sets the map of the node coordinates alpar@1050: alpar@1050: ///Sets the map of the node coordinates. alpar@1050: ///\param x must be a node map with xy or xy values. alpar@1050: template GraphToEps > coords(const X &x) { alpar@1046: dontPrint=true; alpar@1050: return GraphToEps >(CoordsTraits(*this,x)); alpar@1046: } alpar@1050: template struct NodeSizesTraits : public T { alpar@1050: const X &_nodeSizes; alpar@1050: NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {} alpar@1046: }; alpar@1050: ///Sets the map of the node sizes alpar@1050: alpar@1050: ///Sets the map of the node sizes alpar@1050: ///\param x must be a node map with \c double (or convertible) values. alpar@1050: template GraphToEps > nodeSizes(const X &x) alpar@1046: { alpar@1046: dontPrint=true; alpar@1050: return GraphToEps >(NodeSizesTraits(*this,x)); alpar@1046: } alpar@1062: template struct NodeTextsTraits : public T { alpar@1062: const X &_nodeTexts; alpar@1062: NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {} alpar@1062: }; alpar@1062: ///Sets the text printed on the nodes alpar@1062: alpar@1062: ///Sets the text printed on the nodes alpar@1062: ///\param x must be a node map with type that can be pushed to a standard alpar@1062: ///ostream. alpar@1062: template GraphToEps > nodeTexts(const X &x) alpar@1062: { alpar@1062: dontPrint=true; alpar@1062: _showNodeText=true; alpar@1062: return GraphToEps >(NodeTextsTraits(*this,x)); alpar@1062: } alpar@1050: template struct EdgeWidthsTraits : public T { alpar@1050: const X &_edgeWidths; alpar@1050: EdgeWidthsTraits(const T &t,const X &x) : T(t), _edgeWidths(x) {} alpar@1046: }; alpar@1050: ///Sets the map of the edge widths alpar@1050: alpar@1050: ///Sets the map of the edge widths alpar@1050: ///\param x must be a edge map with \c double (or convertible) values. alpar@1050: template GraphToEps > edgeWidths(const X &x) alpar@1046: { alpar@1046: dontPrint=true; alpar@1050: return GraphToEps >(EdgeWidthsTraits(*this,x)); alpar@1046: } alpar@1050: alpar@1050: template struct NodeColorsTraits : public T { alpar@1050: const X &_nodeColors; alpar@1050: NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {} alpar@1046: }; alpar@1050: ///Sets the map of the node colors alpar@1050: alpar@1050: ///Sets the map of the node colors alpar@1050: ///\param x must be a node map with \ref Color values. alpar@1050: template GraphToEps > alpar@1050: nodeColors(const X &x) alpar@1046: { alpar@1046: dontPrint=true; alpar@1050: return GraphToEps >(NodeColorsTraits(*this,x)); alpar@1046: } alpar@1050: template struct EdgeColorsTraits : public T { alpar@1050: const X &_edgeColors; alpar@1050: EdgeColorsTraits(const T &t,const X &x) : T(t), _edgeColors(x) {} alpar@1050: }; alpar@1050: ///Sets the map of the edge colors alpar@1050: alpar@1050: ///Sets the map of the edge colors alpar@1050: ///\param x must be a edge map with \ref Color values. alpar@1050: template GraphToEps > alpar@1050: edgeColors(const X &x) alpar@1050: { alpar@1050: dontPrint=true; alpar@1050: return GraphToEps >(EdgeColorsTraits(*this,x)); alpar@1050: } alpar@1050: ///Sets a global scale factor for node sizes alpar@1050: alpar@1050: ///Sets a global scale factor for node sizes alpar@1050: /// alpar@1050: GraphToEps &nodeScale(double d) {_nodeScale=d;return *this;} alpar@1050: ///Sets a global scale factor for edge widths alpar@1050: alpar@1050: ///Sets a global scale factor for edge widths alpar@1050: /// alpar@1050: GraphToEps &edgeWidthScale(double d) {_edgeWidthScale=d;return *this;} alpar@1050: ///Sets a global scale factor for the whole picture alpar@1050: alpar@1050: ///Sets a global scale factor for the whole picture alpar@1050: /// alpar@1050: GraphToEps &scale(double d) {_scale=d;return *this;} alpar@1050: ///Sets the width of the border around the picture alpar@1050: alpar@1050: ///Sets the width of the border around the picture alpar@1050: /// alpar@1050: GraphToEps &border(double b) {_xBorder=_yBorder=b;return *this;} alpar@1050: ///Sets the width of the border around the picture alpar@1050: alpar@1050: ///Sets the width of the border around the picture alpar@1050: /// alpar@1050: GraphToEps &border(double x, double y) { alpar@1050: _xBorder=x;_yBorder=y;return *this; alpar@1050: } alpar@1050: ///Sets whether to draw arrows alpar@1050: alpar@1050: ///Sets whether to draw arrows alpar@1050: /// alpar@1050: GraphToEps &drawArrows(bool b=true) {_drawArrows=b;return *this;} alpar@1050: ///Sets the length of the arrowheads alpar@1050: alpar@1050: ///Sets the length of the arrowheads alpar@1050: /// alpar@1050: GraphToEps &arrowLength(double d) {_arrowLength*=d;return *this;} alpar@1050: ///Sets the width of the arrowheads alpar@1050: alpar@1050: ///Sets the width of the arrowheads alpar@1050: /// alpar@1050: GraphToEps &arrowWidth(double d) {_arrowWidth*=d;return *this;} alpar@1046: alpar@1055: ///Enables parallel edges alpar@1055: alpar@1055: ///Enables parallel edges alpar@1062: ///\todo Partially implemented alpar@1055: GraphToEps &enableParallel(bool b=true) {_enableParallel=b;return *this;} alpar@1055: alpar@1062: ///Sets the distance alpar@1062: alpar@1062: ///Sets the distance alpar@1062: /// alpar@1062: GraphToEps &parEdgeDist(double d) {_parEdgeDist*=d;return *this;} alpar@1062: alpar@1062: ///Hides the edges alpar@1062: alpar@1062: ///Hides the edges alpar@1062: /// alpar@1062: GraphToEps &hideEdges(bool b=true) {_showEdges=!b;return *this;} alpar@1062: ///Hides the nodes alpar@1062: alpar@1062: ///Hides the nodes alpar@1062: /// alpar@1062: GraphToEps &hideNodes(bool b=true) {_showNodes=!b;return *this;} alpar@1062: alpar@1062: ///Sets the size of the node texts alpar@1062: alpar@1062: ///Sets the size of the node texts alpar@1062: /// alpar@1062: GraphToEps &nodeTextSize(double d) {_nodeTextSize=d;return *this;} alpar@1062: alpar@1062: alpar@1046: ~GraphToEps() alpar@1046: { alpar@1046: if(dontPrint) return; alpar@1046: alpar@1051: os << "%!PS-Adobe-2.0 EPSF-2.0\n"; alpar@1046: //\todo: Chech whether the graph is empty. alpar@1046: BoundingBox bb; alpar@1050: for(NodeIt n(g);n!=INVALID;++n) { alpar@1050: double ns=_nodeSizes[n]*_nodeScale; alpar@1050: xy p(ns,ns); alpar@1050: bb+=p+_coords[n]; alpar@1050: bb+=-p+_coords[n]; alpar@1046: } alpar@1051: os << "%%BoundingBox: " alpar@1050: << bb.left()* _scale-_xBorder << ' ' alpar@1050: << bb.bottom()*_scale-_yBorder << ' ' alpar@1050: << bb.right()* _scale+_xBorder << ' ' alpar@1050: << bb.top()* _scale+_yBorder << '\n'; alpar@1062: //x1 y1 x2 y2 x3 y3 cr cg cb w alpar@1062: os << "/lb { setlinewidth setrgbcolor newpath moveto\n" alpar@1062: << " 4 2 roll 1 index 1 index curveto stroke } bind def\n"; alpar@1051: os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n"; alpar@1062: os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n"; alpar@1046: // x y r cr cg cb alpar@1051: os << "/n { setrgbcolor 2 index 2 index 2 index c fill\n" alpar@1062: << " 0 0 0 setrgbcolor dup " alpar@1062: << _nodeBorderQuotient << " mul setlinewidth " alpar@1062: << 1+_nodeBorderQuotient/2 << " div c stroke\n" alpar@1062: << " } bind def\n"; alpar@1051: os << "/arrl " << _arrowLength << " def\n"; alpar@1051: os << "/arrw " << _arrowWidth << " def\n"; alpar@1050: // l dx_norm dy_norm alpar@1051: os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n"; alpar@1050: //len w dx_norm dy_norm x1 y1 cr cg cb alpar@1051: os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n" alpar@1062: << " /w exch def /len exch def\n" alpar@1050: // << " 0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke" alpar@1062: << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n" alpar@1062: << " len w sub arrl sub dx dy lrl\n" alpar@1062: << " arrw dy dx neg lrl\n" alpar@1062: << " dx arrl w add mul dy w 2 div arrw add mul sub\n" alpar@1062: << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n" alpar@1062: << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n" alpar@1062: << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n" alpar@1062: << " arrw dy dx neg lrl\n" alpar@1062: << " len w sub arrl sub neg dx dy lrl\n" alpar@1062: << " closepath fill } bind def\n"; alpar@1062: os << "/cshow { 2 index 2 index moveto\n" alpar@1062: << " dup stringwidth pop neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n"; alpar@1062: alpar@1051: os << "\ngsave\n"; alpar@1051: if(_scale!=1.0) os << _scale << " dup scale\n"; alpar@1055: alpar@1051: os << "%Edges:\ngsave\n"; alpar@1055: alpar@1062: if(_showEdges) alpar@1062: if(_enableParallel) { alpar@1062: vector el; alpar@1062: for(EdgeIt e(g);e!=INVALID;++e) el.push_back(e); alpar@1062: sort(el.begin(),el.end(),edgeLess(g)); alpar@1062: alpar@1062: typename vector::iterator j; alpar@1062: for(typename vector::iterator i=el.begin();i!=el.end();i=j) { alpar@1062: for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ; alpar@1062: alpar@1062: if(_drawArrows) { alpar@1062: // xy d(_coords[g.target(e)]-_coords[g.source(e)]); alpar@1062: // double l=sqrt(d.normSquare()); alpar@1062: // d/=l; alpar@1062: // xy x1(d*_nodeScale*_nodeSizes[g.source(e)]+ alpar@1062: // _coords[g.source(e)]); alpar@1062: // os << l-(_nodeSizes[g.source(e)]+ alpar@1062: // _nodeSizes[g.target(e)])*_nodeScale << ' ' alpar@1062: // << _edgeWidths[e]*_edgeWidthScale << ' ' alpar@1062: // << d.x << ' ' << d.y << ' ' alpar@1062: // << x1.x << ' ' << x1.y << ' ' alpar@1062: // << _edgeColors[e].getR() << ' ' alpar@1062: // << _edgeColors[e].getG() << ' ' alpar@1062: // << _edgeColors[e].getB() << " arr\n"; alpar@1062: } alpar@1062: else { alpar@1062: double sw=0; alpar@1062: for(typename vector::iterator e=i;e!=j;++e) alpar@1062: sw+=_edgeWidths[*e]*_edgeWidthScale+_parEdgeDist; alpar@1062: sw-=_parEdgeDist; alpar@1062: sw/=-2.0; alpar@1062: xy d(_coords[g.target(*i)]-_coords[g.source(*i)]); alpar@1062: double l=sqrt(d.normSquare()); alpar@1062: d/=l; alpar@1062: for(typename vector::iterator e=i;e!=j;++e) { alpar@1062: sw+=_edgeWidths[*e]*_edgeWidthScale/2.0; alpar@1062: xy m(_coords[g.target(*e)]+_coords[g.source(*e)]); alpar@1062: m=m/2.0+rot(d)*sw/.75; alpar@1062: os << _coords[g.source(*e)].x << ' ' alpar@1062: << _coords[g.source(*e)].y << ' ' alpar@1062: << m.x << ' ' << m.y << ' ' alpar@1062: << _coords[g.target(*e)].x << ' ' alpar@1062: << _coords[g.target(*e)].y << ' ' alpar@1062: << _edgeColors[*e].getR() << ' ' alpar@1062: << _edgeColors[*e].getG() << ' ' alpar@1062: << _edgeColors[*e].getB() << ' ' alpar@1062: << _edgeWidths[*e]*_edgeWidthScale << " lb\n"; alpar@1062: sw+=_edgeWidths[*e]*_edgeWidthScale/2.0+_parEdgeDist; alpar@1062: } alpar@1062: } alpar@1062: } alpar@1062: } alpar@1062: else for(NodeIt n(g);n!=INVALID;++n) alpar@1062: for(OutEdgeIt e(g,n);e!=INVALID;++e) alpar@1062: if(_drawArrows) { alpar@1062: xy d(_coords[g.target(e)]-_coords[g.source(e)]); alpar@1062: double l=sqrt(d.normSquare()); alpar@1062: d/=l; alpar@1062: xy x1(d*_nodeScale*_nodeSizes[g.source(e)]+ alpar@1062: _coords[g.source(e)]); alpar@1062: os << l-(_nodeSizes[g.source(e)]+ alpar@1050: _nodeSizes[g.target(e)])*_nodeScale << ' ' alpar@1050: << _edgeWidths[e]*_edgeWidthScale << ' ' alpar@1050: << d.x << ' ' << d.y << ' ' alpar@1050: << x1.x << ' ' << x1.y << ' ' alpar@1050: << _edgeColors[e].getR() << ' ' alpar@1050: << _edgeColors[e].getG() << ' ' alpar@1050: << _edgeColors[e].getB() << " arr\n"; alpar@1062: } alpar@1062: else os << _coords[g.source(e)].x << ' ' alpar@1050: << _coords[g.source(e)].y << ' ' alpar@1050: << _coords[g.target(e)].x << ' ' alpar@1050: << _coords[g.target(e)].y << ' ' alpar@1050: << _edgeColors[e].getR() << ' ' alpar@1050: << _edgeColors[e].getG() << ' ' alpar@1050: << _edgeColors[e].getB() << ' ' alpar@1050: << _edgeWidths[e]*_edgeWidthScale << " l\n"; alpar@1051: os << "grestore\n%Nodes:\ngsave\n"; alpar@1062: if(_showNodes) alpar@1062: for(NodeIt n(g);n!=INVALID;++n) alpar@1062: os << _coords[n].x << ' ' << _coords[n].y << ' ' alpar@1050: << _nodeSizes[n]*_nodeScale << ' ' alpar@1050: << _nodeColors[n].getR() << ' ' alpar@1050: << _nodeColors[n].getG() << ' ' alpar@1050: << _nodeColors[n].getB() << " n\n"; alpar@1062: if(_showNodeText) { alpar@1062: os << "grestore\n%Node texts:\ngsave\n"; alpar@1062: os << "/fosi " << _nodeTextSize << " def\n"; alpar@1062: os << "(Helvetica) findfont fosi scalefont setfont\n"; alpar@1062: os << "0 0 0 setrgbcolor\n"; alpar@1062: for(NodeIt n(g);n!=INVALID;++n) alpar@1062: os << _coords[n].x << ' ' << _coords[n].y alpar@1062: << " (" << _nodeTexts[n] << ") cshow\n"; alpar@1062: } alpar@1051: os << "grestore\ngrestore\n"; alpar@1055: alpar@1055: //CleanUp: alpar@1055: if(_pleaseRemoveOsStream) {delete &os;} alpar@1046: } alpar@1046: }; alpar@1046: alpar@1046: alpar@1050: ///Generates an EPS file from a graph alpar@1050: alpar@1050: ///\ingroup misc alpar@1050: ///Generates an EPS file from a graph. alpar@1051: ///\param g is a reference to the graph to be printed alpar@1051: ///\param os is a reference to the output stream. alpar@1051: ///By default it is std::cout alpar@1050: /// alpar@1051: ///This function also has a lot of \ref named-templ-param "named parameters", alpar@1050: ///they are declared as the members of class \ref GraphToEps. The following alpar@1050: ///example shows how to use these parameters. alpar@1050: ///\code alpar@1050: /// graphToEps(g).scale(10).coords(coords) alpar@1050: /// .nodeScale(2).nodeSizes(sizes) alpar@1050: /// .edgeWidthScale(.4); alpar@1050: ///\endcode alpar@1050: ///\sa GraphToEps alpar@1062: ///\sa graphToEps(G &g, char *file_name) alpar@1046: template alpar@1055: GraphToEps > alpar@1062: graphToEps(G &g, std::ostream& os=std::cout) alpar@1046: { alpar@1055: return alpar@1055: GraphToEps >(DefaultGraphToEpsTraits(g,os)); alpar@1046: } alpar@1046: alpar@1055: ///Generates an EPS file from a graph alpar@1055: alpar@1062: //\ingroup misc alpar@1062: ///This function does the same as alpar@1062: ///\ref graphToEps(G &g,std::ostream& os) alpar@1062: ///but it writes its output into the file \c file_name alpar@1062: ///instead of a stream. alpar@1062: ///\sa graphToEps(G &g, std::ostream& os) alpar@1055: template alpar@1055: GraphToEps > alpar@1055: graphToEps(G &g,char *file_name) alpar@1055: { alpar@1055: return GraphToEps > alpar@1055: (DefaultGraphToEpsTraits(g,*new ofstream(file_name),true)); alpar@1055: } alpar@1055: alpar@1062: //Generates an EPS file from a graph. alpar@1062: //\param g is a reference to the graph to be printed alpar@1062: //\param file_name is the output file_name. alpar@1062: // alpar@1062: //This function also has a lot of \ref named-templ-param "named parameters", alpar@1062: //they are declared as the members of class \ref GraphToEps. The following alpar@1062: //example shows how to use these parameters. alpar@1062: //\code alpar@1062: // graphToEps(g).scale(10).coords(coords) alpar@1062: // .nodeScale(2).nodeSizes(sizes) alpar@1062: // .edgeWidthScale(.4); alpar@1062: //\endcode alpar@1062: //\sa GraphToEps alpar@1062: //\todo Avoid duplicated documentation alpar@1062: //\bug Exception handling is missing? (Or we can just ignore it?) alpar@1055: alpar@1046: } alpar@1046: alpar@1046: using namespace lemon; alpar@1046: alpar@1046: class ColorSet : public MapBase alpar@1046: { alpar@1046: public: alpar@1046: Color operator[](int i) const alpar@1046: { alpar@1046: switch(i%8){ alpar@1046: case 0: return Color(0,0,0); alpar@1046: case 1: return Color(1,0,0); alpar@1046: case 2: return Color(0,1,0); alpar@1046: case 3: return Color(0,0,1); alpar@1046: case 4: return Color(1,1,0); alpar@1046: case 5: return Color(1,0,1); alpar@1046: case 6: return Color(0,1,1); alpar@1046: case 7: return Color(1,1,1); alpar@1046: } alpar@1046: return Color(0,0,0); alpar@1046: } alpar@1046: } colorSet; alpar@1046: alpar@1062: class IdMap :public MapBase alpar@1062: { alpar@1062: const ListGraph &g; alpar@1062: public: alpar@1062: IdMap(const ListGraph &_g) :g(_g) {} alpar@1062: Value operator[](Key n) const { return g.id(n); } alpar@1062: }; alpar@1062: alpar@1062: alpar@1062: alpar@1046: int main() alpar@1046: { alpar@1046: ListGraph g; alpar@1046: typedef ListGraph::Node Node; alpar@1046: typedef ListGraph::NodeIt NodeIt; alpar@1046: typedef ListGraph::Edge Edge; alpar@1050: typedef xy Xy; alpar@1046: alpar@1046: Node n1=g.addNode(); alpar@1046: Node n2=g.addNode(); alpar@1046: Node n3=g.addNode(); alpar@1046: Node n4=g.addNode(); alpar@1046: Node n5=g.addNode(); alpar@1046: alpar@1046: ListGraph::NodeMap coords(g); alpar@1046: ListGraph::NodeMap sizes(g); alpar@1046: ListGraph::NodeMap colors(g); alpar@1047: ListGraph::EdgeMap ecolors(g); alpar@1050: ListGraph::EdgeMap widths(g); alpar@1046: alpar@1046: coords[n1]=Xy(50,50); sizes[n1]=1; colors[n1]=1; alpar@1046: coords[n2]=Xy(50,70); sizes[n2]=2; colors[n2]=2; alpar@1046: coords[n3]=Xy(70,70); sizes[n3]=1; colors[n3]=3; alpar@1046: coords[n4]=Xy(70,50); sizes[n4]=2; colors[n4]=4; alpar@1046: coords[n5]=Xy(85,60); sizes[n5]=3; colors[n5]=5; alpar@1046: alpar@1046: Edge e; alpar@1046: alpar@1050: e=g.addEdge(n1,n2); ecolors[e]=0; widths[e]=1; alpar@1050: e=g.addEdge(n2,n3); ecolors[e]=0; widths[e]=1; alpar@1050: e=g.addEdge(n3,n5); ecolors[e]=0; widths[e]=3; alpar@1050: e=g.addEdge(n5,n4); ecolors[e]=0; widths[e]=1; alpar@1050: e=g.addEdge(n4,n1); ecolors[e]=0; widths[e]=1; alpar@1050: e=g.addEdge(n2,n4); ecolors[e]=1; widths[e]=2; alpar@1050: e=g.addEdge(n3,n4); ecolors[e]=2; widths[e]=1; alpar@1046: alpar@1062: IdMap id(g); alpar@1062: alpar@1055: graphToEps(g,"proba.eps").scale(10).coords(coords). alpar@1055: nodeScale(2).nodeSizes(sizes). alpar@1055: nodeColors(composeMap(colorSet,colors)). alpar@1055: edgeColors(composeMap(colorSet,ecolors)). alpar@1062: edgeWidthScale(.4).edgeWidths(widths). alpar@1062: nodeTexts(id).nodeTextSize(3); alpar@1062: alpar@1055: graphToEps(g,"proba_arr.eps").scale(10).coords(coords). alpar@1050: nodeScale(2).nodeSizes(sizes). alpar@1050: nodeColors(composeMap(colorSet,colors)). alpar@1050: edgeColors(composeMap(colorSet,ecolors)). alpar@1050: edgeWidthScale(.4).edgeWidths(widths). alpar@1062: nodeTexts(id).nodeTextSize(3). alpar@1062: drawArrows().arrowWidth(1).arrowLength(1); alpar@1062: alpar@1062: e=g.addEdge(n1,n4); ecolors[e]=2; widths[e]=1; alpar@1062: e=g.addEdge(n4,n1); ecolors[e]=1; widths[e]=2; alpar@1062: alpar@1062: e=g.addEdge(n1,n2); ecolors[e]=1; widths[e]=1; alpar@1062: e=g.addEdge(n1,n2); ecolors[e]=2; widths[e]=1; alpar@1062: e=g.addEdge(n1,n2); ecolors[e]=3; widths[e]=1; alpar@1062: e=g.addEdge(n1,n2); ecolors[e]=4; widths[e]=1; alpar@1062: e=g.addEdge(n1,n2); ecolors[e]=5; widths[e]=1; alpar@1062: e=g.addEdge(n1,n2); ecolors[e]=6; widths[e]=1; alpar@1062: e=g.addEdge(n1,n2); ecolors[e]=7; widths[e]=1; alpar@1062: alpar@1062: graphToEps(g,"proba_par.eps").scale(10).coords(coords). alpar@1062: nodeScale(2).nodeSizes(sizes). alpar@1062: nodeColors(composeMap(colorSet,colors)). alpar@1062: edgeColors(composeMap(colorSet,ecolors)). alpar@1062: edgeWidthScale(.4).edgeWidths(widths). alpar@1062: nodeTexts(id).nodeTextSize(3). alpar@1062: enableParallel().parEdgeDist(1.5); alpar@1046: }