if you have a nuclear power plant and wanna compute small magic squares, then let's do it
2 * src/lemon/graph_to_eps.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_GRAPH_TO_EPS_H
18 #define LEMON_GRAPH_TO_EPS_H
30 #include<lemon/maps.h>
31 #include<lemon/bezier.h>
35 ///\brief Simple graph drawer
37 ///\author Alpar Juttner
41 ///Data structure representing RGB colors.
43 ///Data structure representing RGB colors.
49 ///Default constructor
52 Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
53 ///Returns the red component
54 double getR() {return _r;}
55 ///Returns the green component
56 double getG() {return _g;}
57 ///Returns the blue component
58 double getB() {return _b;}
59 ///Set the color components
60 void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
63 ///Default traits class of \ref GraphToEps
65 ///Default traits class of \ref GraphToEps
67 ///\c G is the type of the underlying graph.
69 struct DefaultGraphToEpsTraits
72 typedef typename Graph::Node Node;
73 typedef typename Graph::NodeIt NodeIt;
74 typedef typename Graph::Edge Edge;
75 typedef typename Graph::EdgeIt EdgeIt;
76 typedef typename Graph::InEdgeIt InEdgeIt;
77 typedef typename Graph::OutEdgeIt OutEdgeIt;
84 ConstMap<typename Graph::Node,xy<double> > _coords;
85 ConstMap<typename Graph::Node,double > _nodeSizes;
86 ConstMap<typename Graph::Node,int > _nodeShapes;
88 ConstMap<typename Graph::Node,Color > _nodeColors;
89 ConstMap<typename Graph::Edge,Color > _edgeColors;
91 ConstMap<typename Graph::Edge,double > _edgeWidths;
93 static const double A4HEIGHT = 841.8897637795276;
94 static const double A4WIDTH = 595.275590551181;
95 static const double A4BORDER = 15;
98 double _edgeWidthScale;
101 double _xBorder, _yBorder;
103 double _nodeBorderQuotient;
106 double _arrowLength, _arrowWidth;
108 bool _showNodes, _showEdges;
110 bool _enableParallel;
114 ConstMap<typename Graph::Node,bool > _nodeTexts;
115 double _nodeTextSize;
117 bool _showNodePsText;
118 ConstMap<typename Graph::Node,bool > _nodePsTexts;
119 char *_nodePsTextsPreamble;
122 bool _pleaseRemoveOsStream;
127 std::string _copyright;
132 ///\param _g is a reference to the graph to be printed
133 ///\param _os is a reference to the output stream.
134 ///\param _os is a reference to the output stream.
135 ///\param _pros If it is \c true, then the \c ostream referenced by \c _os
136 ///will be explicitly deallocated by the destructor.
137 ///By default it is <tt>std::cout</tt>
138 DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
141 _coords(xy<double>(1,1)), _nodeSizes(1.0), _nodeShapes(0),
142 _nodeColors(Color(1,1,1)), _edgeColors(Color(0,0,0)),
143 _edgeWidths(1), _edgeWidthScale(0.3),
144 _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0),
145 _nodeBorderQuotient(.1),
146 _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
147 _showNodes(true), _showEdges(true),
148 _enableParallel(false), _parEdgeDist(1),
149 _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
150 _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
152 _pleaseRemoveOsStream(_pros), _scaleToA4(false) {}
155 ///Helper class to implement the named parameters of \ref graphToEps()
157 ///Helper class to implement the named parameters of \ref graphToEps()
158 ///\todo Is 'helper class' a good name for this?
160 ///\todo Follow PostScript's DSC.
161 /// Use own dictionary.
162 ///\todo Useful new features.
163 /// - Linestyles: dotted, dashed etc.
164 /// - A second color and percent value for the lines.
165 template<class T> class GraphToEps : public T
167 typedef typename T::Graph Graph;
168 typedef typename Graph::Node Node;
169 typedef typename Graph::NodeIt NodeIt;
170 typedef typename Graph::Edge Edge;
171 typedef typename Graph::EdgeIt EdgeIt;
172 typedef typename Graph::InEdgeIt InEdgeIt;
173 typedef typename Graph::OutEdgeIt OutEdgeIt;
175 static const int INTERPOL_PREC=20;
186 ///\image html nodeshape_0.png
187 ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm
190 ///\image html nodeshape_1.png
191 ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm
195 ///\image html nodeshape_2.png
196 ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm
205 edgeLess(const Graph &_g) : g(_g) {}
206 bool operator()(Edge a,Edge b) const
208 Node ai=min(g.source(a),g.target(a));
209 Node aa=max(g.source(a),g.target(a));
210 Node bi=min(g.source(b),g.target(b));
211 Node ba=max(g.source(b),g.target(b));
213 (ai==bi && (aa < ba ||
214 (aa==ba && ai==g.source(a) && bi==g.target(b))));
217 bool isParallel(Edge e,Edge f) const
219 return (g.source(e)==g.source(f)&&g.target(e)==g.target(f))||
220 (g.source(e)==g.target(f)&&g.target(e)==g.source(f));
222 static xy<double> rot(xy<double> v)
224 return xy<double>(v.y,-v.x);
227 static std::string psOut(const xy &p)
229 std::ostringstream os;
230 os << p.x << ' ' << p.y;
235 GraphToEps(const T &t) : T(t), dontPrint(false) {};
237 template<class X> struct CoordsTraits : public T {
239 CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
241 ///Sets the map of the node coordinates
243 ///Sets the map of the node coordinates.
244 ///\param x must be a node map with xy<double> or \ref xy "xy<int>" values.
245 template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
247 return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
249 template<class X> struct NodeSizesTraits : public T {
251 NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
253 ///Sets the map of the node sizes
255 ///Sets the map of the node sizes
256 ///\param x must be a node map with \c double (or convertible) values.
257 template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
260 return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
262 template<class X> struct NodeShapesTraits : public T {
263 const X &_nodeShapes;
264 NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
266 ///Sets the map of the node shapes
268 ///Sets the map of the node shapes.
269 ///The availabe shape values
270 ///can be found in \ref NodeShapes "enum NodeShapes".
271 ///\param x must be a node map with \c int (or convertible) values.
273 template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
276 return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
278 template<class X> struct NodeTextsTraits : public T {
280 NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
282 ///Sets the text printed on the nodes
284 ///Sets the text printed on the nodes
285 ///\param x must be a node map with type that can be pushed to a standard
287 template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
291 return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
293 template<class X> struct NodePsTextsTraits : public T {
294 const X &_nodePsTexts;
295 NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
297 ///Inserts a PostScript block to the nodes
299 ///With this command it is possible to insert a verbatim PostScript
300 ///block to the nodes.
301 ///The PS current point will be moved to the centre of the node before
302 ///the PostScript block inserted.
304 ///Before and after the block a newline character is inserted to you
305 ///don't have to bother with the separators.
307 ///\param x must be a node map with type that can be pushed to a standard
310 ///\sa nodePsTextsPreamble()
311 ///\todo Offer the choise not to move to the centre but pass the coordinates
312 ///to the Postscript block inserted.
313 template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
316 _showNodePsText=true;
317 return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
319 template<class X> struct EdgeWidthsTraits : public T {
320 const X &_edgeWidths;
321 EdgeWidthsTraits(const T &t,const X &x) : T(t), _edgeWidths(x) {}
323 ///Sets the map of the edge widths
325 ///Sets the map of the edge widths
326 ///\param x must be a edge map with \c double (or convertible) values.
327 template<class X> GraphToEps<EdgeWidthsTraits<X> > edgeWidths(const X &x)
330 return GraphToEps<EdgeWidthsTraits<X> >(EdgeWidthsTraits<X>(*this,x));
333 template<class X> struct NodeColorsTraits : public T {
334 const X &_nodeColors;
335 NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
337 ///Sets the map of the node colors
339 ///Sets the map of the node colors
340 ///\param x must be a node map with \ref Color values.
341 template<class X> GraphToEps<NodeColorsTraits<X> >
342 nodeColors(const X &x)
345 return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
347 template<class X> struct EdgeColorsTraits : public T {
348 const X &_edgeColors;
349 EdgeColorsTraits(const T &t,const X &x) : T(t), _edgeColors(x) {}
351 ///Sets the map of the edge colors
353 ///Sets the map of the edge colors
354 ///\param x must be a edge map with \ref Color values.
355 template<class X> GraphToEps<EdgeColorsTraits<X> >
356 edgeColors(const X &x)
359 return GraphToEps<EdgeColorsTraits<X> >(EdgeColorsTraits<X>(*this,x));
361 ///Sets a global scale factor for node sizes
363 ///Sets a global scale factor for node sizes
365 GraphToEps<T> &nodeScale(double d) {_nodeScale=d;return *this;}
366 ///Sets a global scale factor for edge widths
368 ///Sets a global scale factor for edge widths
370 GraphToEps<T> &edgeWidthScale(double d) {_edgeWidthScale=d;return *this;}
371 ///Sets a global scale factor for the whole picture
373 ///Sets a global scale factor for the whole picture
375 GraphToEps<T> &scale(double d) {_scale=d;return *this;}
376 ///Sets the width of the border around the picture
378 ///Sets the width of the border around the picture
380 GraphToEps<T> &border(double b) {_xBorder=_yBorder=b;return *this;}
381 ///Sets the width of the border around the picture
383 ///Sets the width of the border around the picture
385 GraphToEps<T> &border(double x, double y) {
386 _xBorder=x;_yBorder=y;return *this;
388 ///Sets whether to draw arrows
390 ///Sets whether to draw arrows
392 GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
393 ///Sets the length of the arrowheads
395 ///Sets the length of the arrowheads
397 GraphToEps<T> &arrowLength(double d) {_arrowLength*=d;return *this;}
398 ///Sets the width of the arrowheads
400 ///Sets the width of the arrowheads
402 GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;}
404 ///Scales the drawing to fit to A4 page
406 ///Scales the drawing to fit to A4 page
408 GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
410 ///Enables parallel edges
412 ///Enables parallel edges
413 ///\todo Partially implemented
414 GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
420 GraphToEps<T> &parEdgeDist(double d) {_parEdgeDist*=d;return *this;}
426 GraphToEps<T> &hideEdges(bool b=true) {_showEdges=!b;return *this;}
431 GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
433 ///Sets the size of the node texts
435 ///Sets the size of the node texts
437 GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
438 ///Gives a preamble block for node Postscript block.
440 ///Gives a preamble block for node Postscript block.
443 GraphToEps<T> & nodePsTextsPreamble(const char *str) {
444 _nodePsTextsPreamble=s ;return *this;
446 ///Sets whether the the graph is undirected
448 ///Sets whether the the graph is undirected
450 GraphToEps<T> &undir(bool b=true) {_undir=b;return *this;}
451 ///Sets whether the the graph is directed
453 ///Sets whether the the graph is directed.
454 ///Use it to show the undirected edges as a pair of directed ones.
455 GraphToEps<T> &bidir(bool b=true) {_undir=!b;return *this;}
459 ///Sets the title of the generated image,
460 ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of
462 GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
463 ///Sets the copyright statement.
465 ///Sets the copyright statement of the generated image,
466 ///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of
468 ///\todo Multiline copyright notice could be supported.
469 GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;}
472 bool isInsideNode(xy<double> p, double r,int t)
476 return p.normSquare()<=r*r;
478 return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
480 return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
490 ///Like other functions using
491 ///\ref named-templ-func-param "named template parameters",
492 ///this function calles the algorithm itself, i.e. in this case
493 ///it draws the graph.
495 if(dontPrint) return;
497 os << "%!PS-Adobe-2.0 EPSF-2.0\n";
498 if(_title.size()>0) os << "%%Title: " << _title << '\n';
499 if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
500 // << "%%Copyright: XXXX\n"
501 os << "%%Creator: LEMON GraphToEps function\n";
506 gettimeofday(&tv, 0);
507 ctime_r(&tv.tv_sec,cbuf);
508 os << "%%CreationDate: " << cbuf;
510 ///\todo: Chech whether the graph is empty.
511 BoundingBox<double> bb;
512 for(NodeIt n(g);n!=INVALID;++n) {
513 double ns=_nodeSizes[n]*_nodeScale;
519 os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
520 else os << "%%BoundingBox: "
521 << bb.left()* _scale-_xBorder << ' '
522 << bb.bottom()*_scale-_yBorder << ' '
523 << bb.right()* _scale+_xBorder << ' '
524 << bb.top()* _scale+_yBorder << '\n';
526 os << "%%EndComments\n";
528 //x1 y1 x2 y2 x3 y3 cr cg cb w
529 os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
530 << " 4 2 roll 1 index 1 index curveto stroke } bind def\n";
531 os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
533 os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n";
535 os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
536 << " 2 index 1 index sub 2 index 2 index add lineto\n"
537 << " 2 index 1 index sub 2 index 2 index sub lineto\n"
538 << " 2 index 1 index add 2 index 2 index sub lineto\n"
539 << " closepath pop pop pop} bind def\n";
541 os << "/di { newpath 2 index 1 index add 2 index moveto\n"
542 << " 2 index 2 index 2 index add lineto\n"
543 << " 2 index 1 index sub 2 index lineto\n"
544 << " 2 index 2 index 2 index sub lineto\n"
545 << " closepath pop pop pop} bind def\n";
547 os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
548 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
550 os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
551 << " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
553 os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
554 << " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
556 os << "/arrl " << _arrowLength << " def\n";
557 os << "/arrw " << _arrowWidth << " def\n";
559 os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
560 //len w dx_norm dy_norm x1 y1 cr cg cb
561 os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
562 << " /w exch def /len exch def\n"
563 // << " 0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
564 << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
565 << " len w sub arrl sub dx dy lrl\n"
566 << " arrw dy dx neg lrl\n"
567 << " dx arrl w add mul dy w 2 div arrw add mul sub\n"
568 << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
569 << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
570 << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
571 << " arrw dy dx neg lrl\n"
572 << " len w sub arrl sub neg dx dy lrl\n"
573 << " closepath fill } bind def\n";
574 os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
575 << " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
579 if(bb.height()>bb.width()) {
580 double sc= min((A4HEIGHT-2*A4BORDER)/bb.height(),
581 (A4WIDTH-2*A4BORDER)/bb.width());
582 os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
583 << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER << " translate\n"
584 << sc << " dup scale\n"
585 << -bb.left() << ' ' << -bb.bottom() << " translate\n";
588 //\todo Verify centering
589 double sc= min((A4HEIGHT-2*A4BORDER)/bb.width(),
590 (A4WIDTH-2*A4BORDER)/bb.height());
591 os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
592 << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER << " translate\n"
593 << sc << " dup scale\n90 rotate\n"
594 << -bb.left() << ' ' << -bb.top() << " translate\n";
596 else if(_scale!=1.0) os << _scale << " dup scale\n";
599 os << "%Edges:\ngsave\n";
600 if(_enableParallel) {
601 std::vector<Edge> el;
602 for(EdgeIt e(g);e!=INVALID;++e)
603 if(!_undir||g.source(e)<g.target(e)) el.push_back(e);
604 sort(el.begin(),el.end(),edgeLess(g));
606 typename std::vector<Edge>::iterator j;
607 for(typename std::vector<Edge>::iterator i=el.begin();i!=el.end();i=j) {
608 for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
611 for(typename std::vector<Edge>::iterator e=i;e!=j;++e)
612 sw+=_edgeWidths[*e]*_edgeWidthScale+_parEdgeDist;
615 xy<double> dvec(_coords[g.target(*i)]-_coords[g.source(*i)]);
616 double l=sqrt(dvec.normSquare());
617 xy<double> d(dvec/l);
619 // m=xy<double>(_coords[g.target(*i)]+_coords[g.source(*i)])/2.0;
621 // m=xy<double>(_coords[g.source(*i)])+
622 // dvec*(double(_nodeSizes[g.source(*i)])/
623 // (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
625 m=xy<double>(_coords[g.source(*i)])+
626 d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
628 for(typename std::vector<Edge>::iterator e=i;e!=j;++e) {
629 sw+=_edgeWidths[*e]*_edgeWidthScale/2.0;
630 xy<double> mm=m+rot(d)*sw/.75;
633 xy<double> s=_coords[g.source(*e)];
634 xy<double> t=_coords[g.target(*e)];
635 double rn=_nodeSizes[g.target(*e)]*_nodeScale;
636 node_shape=_nodeShapes[g.target(*e)];
637 Bezier3 bez(s,mm,mm,t);
639 for(int i=0;i<INTERPOL_PREC;++i)
640 if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
642 xy<double> apoint=bez((t1+t2)/2);
643 rn = _arrowLength+_edgeWidths[*e]*_edgeWidthScale;
646 for(int i=0;i<INTERPOL_PREC;++i)
647 if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
649 xy<double> linend=bez((t1+t2)/2);
650 bez=bez.before((t1+t2)/2);
651 // rn=_nodeSizes[g.source(*e)]*_nodeScale;
652 // node_shape=_nodeShapes[g.source(*e)];
654 // for(int i=0;i<INTERPOL_PREC;++i)
655 // if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t1=(t1+t2)/2;
656 // else t2=(t1+t2)/2;
657 // bez=bez.after((t1+t2)/2);
658 os << _edgeWidths[*e]*_edgeWidthScale << " setlinewidth "
659 << _edgeColors[*e].getR() << ' '
660 << _edgeColors[*e].getG() << ' '
661 << _edgeColors[*e].getB() << " setrgbcolor newpath\n"
662 << bez.p1.x << ' ' << bez.p1.y << " moveto\n"
663 << bez.p2.x << ' ' << bez.p2.y << ' '
664 << bez.p3.x << ' ' << bez.p3.y << ' '
665 << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
666 xy<double> dd(rot(linend-apoint));
667 dd*=(.5*_edgeWidths[*e]*_edgeWidthScale+_arrowWidth)/
668 sqrt(dd.normSquare());
669 os << "newpath " << psOut(apoint) << " moveto "
670 << psOut(linend+dd) << " lineto "
671 << psOut(linend-dd) << " lineto closepath fill\n";
674 os << _coords[g.source(*e)].x << ' '
675 << _coords[g.source(*e)].y << ' '
676 << mm.x << ' ' << mm.y << ' '
677 << _coords[g.target(*e)].x << ' '
678 << _coords[g.target(*e)].y << ' '
679 << _edgeColors[*e].getR() << ' '
680 << _edgeColors[*e].getG() << ' '
681 << _edgeColors[*e].getB() << ' '
682 << _edgeWidths[*e]*_edgeWidthScale << " lb\n";
684 sw+=_edgeWidths[*e]*_edgeWidthScale/2.0+_parEdgeDist;
688 else for(EdgeIt e(g);e!=INVALID;++e)
689 if(!_undir||g.source(e)<g.target(e))
691 xy<double> d(_coords[g.target(e)]-_coords[g.source(e)]);
692 double rn=_nodeSizes[g.target(e)]*_nodeScale;
693 int node_shape=_nodeShapes[g.target(e)];
695 for(int i=0;i<INTERPOL_PREC;++i)
696 if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
698 double l=sqrt(d.normSquare());
701 os << l*(1-(t1+t2)/2) << ' '
702 << _edgeWidths[e]*_edgeWidthScale << ' '
703 << d.x << ' ' << d.y << ' '
704 << _coords[g.source(e)].x << ' '
705 << _coords[g.source(e)].y << ' '
706 << _edgeColors[e].getR() << ' '
707 << _edgeColors[e].getG() << ' '
708 << _edgeColors[e].getB() << " arr\n";
710 else os << _coords[g.source(e)].x << ' '
711 << _coords[g.source(e)].y << ' '
712 << _coords[g.target(e)].x << ' '
713 << _coords[g.target(e)].y << ' '
714 << _edgeColors[e].getR() << ' '
715 << _edgeColors[e].getG() << ' '
716 << _edgeColors[e].getB() << ' '
717 << _edgeWidths[e]*_edgeWidthScale << " l\n";
721 os << "%Nodes:\ngsave\n";
722 for(NodeIt n(g);n!=INVALID;++n) {
723 os << _coords[n].x << ' ' << _coords[n].y << ' '
724 << _nodeSizes[n]*_nodeScale << ' '
725 << _nodeColors[n].getR() << ' '
726 << _nodeColors[n].getG() << ' '
727 << _nodeColors[n].getB() << ' ';
728 switch(_nodeShapes[n]) {
741 os << "%Node texts:\ngsave\n";
742 os << "/fosi " << _nodeTextSize << " def\n";
743 os << "(Helvetica) findfont fosi scalefont setfont\n";
744 os << "0 0 0 setrgbcolor\n";
745 for(NodeIt n(g);n!=INVALID;++n)
746 os << _coords[n].x << ' ' << _coords[n].y
747 << " (" << _nodeTexts[n] << ") cshow\n";
750 if(_showNodePsText) {
751 os << "%Node PS blocks:\ngsave\n";
752 for(NodeIt n(g);n!=INVALID;++n)
753 os << _coords[n].x << ' ' << _coords[n].y
754 << " moveto\n" << _nodePsTexts[n] << "\n";
758 os << "grestore\nshowpage\n";
761 if(_pleaseRemoveOsStream) {delete &os;}
766 ///Generates an EPS file from a graph
769 ///Generates an EPS file from a graph.
770 ///\param g is a reference to the graph to be printed
771 ///\param os is a reference to the output stream.
772 ///By default it is <tt>std::cout</tt>
774 ///This function also has a lot of
775 ///\ref named-templ-func-param "named parameters",
776 ///they are declared as the members of class \ref GraphToEps. The following
777 ///example shows how to use these parameters.
779 /// graphToEps(g).scale(10).coords(coords)
780 /// .nodeScale(2).nodeSizes(sizes)
781 /// .edgeWidthScale(.4).run();
783 ///\warning Don't forget to put the \ref GraphToEps::run() "run()"
784 ///to the end of the parameter list.
786 ///\sa graphToEps(G &g, char *file_name)
788 GraphToEps<DefaultGraphToEpsTraits<G> >
789 graphToEps(G &g, std::ostream& os=std::cout)
792 GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
795 ///Generates an EPS file from a graph
798 ///This function does the same as
799 ///\ref graphToEps(G &g,std::ostream& os)
800 ///but it writes its output into the file \c file_name
801 ///instead of a stream.
802 ///\sa graphToEps(G &g, std::ostream& os)
804 GraphToEps<DefaultGraphToEpsTraits<G> >
805 graphToEps(G &g,const char *file_name)
807 return GraphToEps<DefaultGraphToEpsTraits<G> >
808 (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true));
811 } //END OF NAMESPACE LEMON
813 #endif // LEMON_GRAPH_TO_EPS_H