alpar@1051: #include alpar@1050: #include alpar@1051: alpar@1046: #include alpar@1046: #include alpar@1046: #include alpar@1046: alpar@1050: alpar@1050: ///\file \ingroup misc alpar@1046: ///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@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@1051: ///By default it is std::cout alpar@1051: DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout) : 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@1050: _drawArrows(false), _arrowLength(1), _arrowWidth(0.3) {} 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@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@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@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@1050: //x1 y1 x2 y2 cr cg cb w alpar@1051: os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n"; alpar@1051: os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc } 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@1050: << " 0 0 0 setrgbcolor dup " alpar@1050: << _nodeBorderQuotient << " mul setlinewidth " alpar@1050: << 1+_nodeBorderQuotient/2 << " div c stroke\n" alpar@1046: << " } 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@1050: << " /w exch def /len exch def\n" alpar@1050: // << " 0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke" alpar@1050: << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n" alpar@1050: << " len w sub arrl sub dx dy lrl\n" alpar@1050: << " arrw dy dx neg lrl\n" alpar@1050: << " dx arrl w add mul dy w 2 div arrw add mul sub\n" alpar@1050: << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n" alpar@1050: << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n" alpar@1050: << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n" alpar@1050: << " arrw dy dx neg lrl\n" alpar@1050: << " len w sub arrl sub neg dx dy lrl\n" alpar@1050: << " closepath fill } bind def\n"; alpar@1051: os << "\ngsave\n"; alpar@1051: if(_scale!=1.0) os << _scale << " dup scale\n"; alpar@1051: os << "%Edges:\ngsave\n"; alpar@1046: for(NodeIt n(g);n!=INVALID;++n) alpar@1046: for(OutEdgeIt e(g,n);e!=INVALID;++e) alpar@1050: if(_drawArrows) { alpar@1050: xy d(_coords[g.target(e)]-_coords[g.source(e)]); alpar@1050: double l=sqrt(d.normSquare()); alpar@1050: d/=l; alpar@1050: xy x1(d*_nodeScale*_nodeSizes[g.source(e)]+ alpar@1050: _coords[g.source(e)]); alpar@1051: 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@1050: } alpar@1051: 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@1046: for(NodeIt n(g);n!=INVALID;++n) alpar@1051: 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@1051: os << "grestore\ngrestore\n"; 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@1046: template alpar@1051: GraphToEps > graphToEps(G &g,std::ostream& os=std::cout) alpar@1046: { alpar@1046: return GraphToEps >(DefaultGraphToEpsTraits(g)); alpar@1046: } alpar@1046: 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@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@1050: graphToEps(g).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@1050: drawArrows().arrowWidth(1).arrowLength(1) alpar@1050: ; alpar@1046: }