2 * src/lemon/graph_to_eps.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 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
29 #include<lemon/invalid.h>
31 #include<lemon/maps.h>
32 #include<lemon/bezier.h>
36 ///\brief Simple graph drawer
38 ///\author Alpar Juttner
42 ///Data structure representing RGB colors.
44 ///Data structure representing RGB colors.
50 ///Default constructor
53 Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
54 ///Returns the red component
56 ///\todo \c red() could be a better name...
57 double getR() const {return _r;}
58 ///Returns the green component
59 double getG() const {return _g;}
60 ///Returns the blue component
61 double getB() const {return _b;}
62 ///Set the color components
63 void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
66 ///Maps <tt>int</tt>s to different \ref Color "Color"s
68 ///This map assing one of the predefined \ref Color "Color"s
69 ///to each <tt>int</tt>. It is possible to change the colors as well as their
70 ///number. The integer range is cyclically mapped to the provided set of colors.
72 ///This is a true \ref concept::ReferenceMap "reference map", so you can also
73 ///change the actual colors.
75 class ColorSet : public MapBase<int,Color>
77 std::vector<Color> colors;
82 ///\param have_white indicates wheter white is
83 ///amongst the provided color (\c true) or not (\c false). If it is true,
84 ///white will be assigned to \c 0.
85 ///\param num the number of the allocated colors. If it is \c 0
86 ///the default color configuration is set up (26 color plus the while).
87 ///If \c num is less then 26/27 then the default color list is cut. Otherwise
88 ///the color list is filled repeatedly with the default color list.
89 ColorSet(bool have_white=false,int num=0)
92 if(have_white) colors.push_back(Color(1,1,1));
94 colors.push_back(Color(0,0,0));
95 colors.push_back(Color(1,0,0));
96 colors.push_back(Color(0,1,0));
97 colors.push_back(Color(0,0,1));
98 colors.push_back(Color(1,1,0));
99 colors.push_back(Color(1,0,1));
100 colors.push_back(Color(0,1,1));
102 colors.push_back(Color(.5,0,0));
103 colors.push_back(Color(0,.5,0));
104 colors.push_back(Color(0,0,.5));
105 colors.push_back(Color(.5,.5,0));
106 colors.push_back(Color(.5,0,.5));
107 colors.push_back(Color(0,.5,.5));
109 colors.push_back(Color(.5,.5,.5));
110 colors.push_back(Color(1,.5,.5));
111 colors.push_back(Color(.5,1,.5));
112 colors.push_back(Color(.5,.5,1));
113 colors.push_back(Color(1,1,.5));
114 colors.push_back(Color(1,.5,1));
115 colors.push_back(Color(.5,1,1));
117 colors.push_back(Color(1,.5,0));
118 colors.push_back(Color(.5,1,0));
119 colors.push_back(Color(1,0,.5));
120 colors.push_back(Color(0,1,.5));
121 colors.push_back(Color(0,.5,1));
122 colors.push_back(Color(.5,0,1));
123 } while(int(colors.size())<num);
124 // colors.push_back(Color(1,1,1));
125 if(num>0) colors.resize(num);
128 Color &operator[](int i)
130 return colors[i%colors.size()];
133 const Color &operator[](int i) const
135 return colors[i%colors.size()];
138 void set(int i,const Color &c)
140 colors[i%colors.size()]=c;
142 ///Sets the number of the exiting colors.
143 void resize(int s) { colors.resize(s);}
144 ///Returns the munber of the existing colors.
145 std::size_t size() { return colors.size();}
148 ///Returns a visible distinct \ref Color
150 ///Returns a \ref Color which is as different from the given parameter
151 ///as it is possible.
152 inline Color distantColor(const Color &c)
154 return Color(c.getR()<.5?1:0,c.getG()<.5?1:0,c.getB()<.5?1:0);
156 ///Returns black for light colors and white for the dark ones.
158 ///Returns black for light colors and white for the dark ones.
159 ///\todo weighted average would be better
160 inline Color distantBW(const Color &c){
161 double v=(.2125*c.getR()+.7154*c.getG()+.0721*c.getB())<.5?1:0;
165 ///Default traits class of \ref GraphToEps
167 ///Default traits class of \ref GraphToEps
169 ///\c G is the type of the underlying graph.
171 struct DefaultGraphToEpsTraits
174 typedef typename Graph::Node Node;
175 typedef typename Graph::NodeIt NodeIt;
176 typedef typename Graph::Edge Edge;
177 typedef typename Graph::EdgeIt EdgeIt;
178 typedef typename Graph::InEdgeIt InEdgeIt;
179 typedef typename Graph::OutEdgeIt OutEdgeIt;
186 ConstMap<typename Graph::Node,xy<double> > _coords;
187 ConstMap<typename Graph::Node,double > _nodeSizes;
188 ConstMap<typename Graph::Node,int > _nodeShapes;
190 ConstMap<typename Graph::Node,Color > _nodeColors;
191 ConstMap<typename Graph::Edge,Color > _edgeColors;
193 ConstMap<typename Graph::Edge,double > _edgeWidths;
195 double _edgeWidthScale;
198 double _xBorder, _yBorder;
200 double _nodeBorderQuotient;
203 double _arrowLength, _arrowWidth;
205 bool _showNodes, _showEdges;
207 bool _enableParallel;
211 ConstMap<typename Graph::Node,bool > _nodeTexts;
212 double _nodeTextSize;
214 bool _showNodePsText;
215 ConstMap<typename Graph::Node,bool > _nodePsTexts;
216 char *_nodePsTextsPreamble;
219 bool _pleaseRemoveOsStream;
224 std::string _copyright;
226 enum NodeTextColorType
227 { DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType;
228 ConstMap<typename Graph::Node,Color > _nodeTextColors;
233 ///\param _g is a reference to the graph to be printed
234 ///\param _os is a reference to the output stream.
235 ///\param _os is a reference to the output stream.
236 ///\param _pros If it is \c true, then the \c ostream referenced by \c _os
237 ///will be explicitly deallocated by the destructor.
238 ///By default it is <tt>std::cout</tt>
239 DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
242 _coords(xy<double>(1,1)), _nodeSizes(1.0), _nodeShapes(0),
243 _nodeColors(Color(1,1,1)), _edgeColors(Color(0,0,0)),
244 _edgeWidths(1), _edgeWidthScale(0.3),
245 _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0),
246 _nodeBorderQuotient(.1),
247 _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
248 _showNodes(true), _showEdges(true),
249 _enableParallel(false), _parEdgeDist(1),
250 _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
251 _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
253 _pleaseRemoveOsStream(_pros), _scaleToA4(false),
254 _nodeTextColorType(SAME_COL), _nodeTextColors(Color(0,0,0))
258 ///Helper class to implement the named parameters of \ref graphToEps()
260 ///Helper class to implement the named parameters of \ref graphToEps()
261 ///\todo Is 'helper class' a good name for this?
263 ///\todo Follow PostScript's DSC.
264 /// Use own dictionary.
265 ///\todo Useful new features.
266 /// - Linestyles: dotted, dashed etc.
267 /// - A second color and percent value for the lines.
268 template<class T> class GraphToEps : public T
270 // Can't believe it is required by the C++ standard
276 using T::_nodeShapes;
277 using T::_nodeColors;
278 using T::_edgeColors;
279 using T::_edgeWidths;
281 using T::_edgeWidthScale;
286 using T::_nodeBorderQuotient;
288 using T::_drawArrows;
289 using T::_arrowLength;
290 using T::_arrowWidth;
295 using T::_enableParallel;
296 using T::_parEdgeDist;
298 using T::_showNodeText;
300 using T::_nodeTextSize;
302 using T::_showNodePsText;
303 using T::_nodePsTexts;
304 using T::_nodePsTextsPreamble;
307 using T::_pleaseRemoveOsStream;
314 using T::NodeTextColorType;
318 using T::_nodeTextColorType;
319 using T::_nodeTextColors;
320 // dradnats ++C eht yb deriuqer si ti eveileb t'naC
322 typedef typename T::Graph Graph;
323 typedef typename Graph::Node Node;
324 typedef typename Graph::NodeIt NodeIt;
325 typedef typename Graph::Edge Edge;
326 typedef typename Graph::EdgeIt EdgeIt;
327 typedef typename Graph::InEdgeIt InEdgeIt;
328 typedef typename Graph::OutEdgeIt OutEdgeIt;
330 static const int INTERPOL_PREC=20;
331 static const double A4HEIGHT = 841.8897637795276;
332 static const double A4WIDTH = 595.275590551181;
333 static const double A4BORDER = 15;
344 ///\image html nodeshape_0.png
345 ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm
348 ///\image html nodeshape_1.png
349 ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm
353 ///\image html nodeshape_2.png
354 ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm
363 edgeLess(const Graph &_g) : g(_g) {}
364 bool operator()(Edge a,Edge b) const
366 Node ai=min(g.source(a),g.target(a));
367 Node aa=max(g.source(a),g.target(a));
368 Node bi=min(g.source(b),g.target(b));
369 Node ba=max(g.source(b),g.target(b));
371 (ai==bi && (aa < ba ||
372 (aa==ba && ai==g.source(a) && bi==g.target(b))));
375 bool isParallel(Edge e,Edge f) const
377 return (g.source(e)==g.source(f)&&
378 g.target(e)==g.target(f)) ||
379 (g.source(e)==g.target(f)&&
380 g.target(e)==g.source(f));
383 static std::string psOut(const xy<TT> &p)
385 std::ostringstream os;
386 os << p.x << ' ' << p.y;
389 static std::string psOut(const Color &c)
391 std::ostringstream os;
392 os << c.getR() << ' ' << c.getG() << ' ' << c.getB();
397 GraphToEps(const T &t) : T(t), dontPrint(false) {};
399 template<class X> struct CoordsTraits : public T {
401 CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
403 ///Sets the map of the node coordinates
405 ///Sets the map of the node coordinates.
406 ///\param x must be a node map with xy<double> or \ref xy "xy<int>" values.
407 template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
409 return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
411 template<class X> struct NodeSizesTraits : public T {
413 NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
415 ///Sets the map of the node sizes
417 ///Sets the map of the node sizes
418 ///\param x must be a node map with \c double (or convertible) values.
419 template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
422 return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
424 template<class X> struct NodeShapesTraits : public T {
425 const X &_nodeShapes;
426 NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
428 ///Sets the map of the node shapes
430 ///Sets the map of the node shapes.
431 ///The availabe shape values
432 ///can be found in \ref NodeShapes "enum NodeShapes".
433 ///\param x must be a node map with \c int (or convertible) values.
435 template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
438 return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
440 template<class X> struct NodeTextsTraits : public T {
442 NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
444 ///Sets the text printed on the nodes
446 ///Sets the text printed on the nodes
447 ///\param x must be a node map with type that can be pushed to a standard
449 template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
453 return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
455 template<class X> struct NodePsTextsTraits : public T {
456 const X &_nodePsTexts;
457 NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
459 ///Inserts a PostScript block to the nodes
461 ///With this command it is possible to insert a verbatim PostScript
462 ///block to the nodes.
463 ///The PS current point will be moved to the centre of the node before
464 ///the PostScript block inserted.
466 ///Before and after the block a newline character is inserted to you
467 ///don't have to bother with the separators.
469 ///\param x must be a node map with type that can be pushed to a standard
472 ///\sa nodePsTextsPreamble()
473 ///\todo Offer the choise not to move to the centre but pass the coordinates
474 ///to the Postscript block inserted.
475 template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
478 _showNodePsText=true;
479 return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
481 template<class X> struct EdgeWidthsTraits : public T {
482 const X &_edgeWidths;
483 EdgeWidthsTraits(const T &t,const X &x) : T(t), _edgeWidths(x) {}
485 ///Sets the map of the edge widths
487 ///Sets the map of the edge widths
488 ///\param x must be a edge map with \c double (or convertible) values.
489 template<class X> GraphToEps<EdgeWidthsTraits<X> > edgeWidths(const X &x)
492 return GraphToEps<EdgeWidthsTraits<X> >(EdgeWidthsTraits<X>(*this,x));
495 template<class X> struct NodeColorsTraits : public T {
496 const X &_nodeColors;
497 NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
499 ///Sets the map of the node colors
501 ///Sets the map of the node colors
502 ///\param x must be a node map with \ref Color values.
503 template<class X> GraphToEps<NodeColorsTraits<X> >
504 nodeColors(const X &x)
507 return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
509 template<class X> struct NodeTextColorsTraits : public T {
510 const X &_nodeTextColors;
511 NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {}
513 ///Sets the map of the node text colors
515 ///Sets the map of the node text colors
516 ///\param x must be a node map with \ref Color values.
517 template<class X> GraphToEps<NodeTextColorsTraits<X> >
518 nodeTextColors(const X &x)
521 _nodeTextColorType=CUST_COL;
522 return GraphToEps<NodeTextColorsTraits<X> >
523 (NodeTextColorsTraits<X>(*this,x));
525 template<class X> struct EdgeColorsTraits : public T {
526 const X &_edgeColors;
527 EdgeColorsTraits(const T &t,const X &x) : T(t), _edgeColors(x) {}
529 ///Sets the map of the edge colors
531 ///Sets the map of the edge colors
532 ///\param x must be a edge map with \ref Color values.
533 template<class X> GraphToEps<EdgeColorsTraits<X> >
534 edgeColors(const X &x)
537 return GraphToEps<EdgeColorsTraits<X> >(EdgeColorsTraits<X>(*this,x));
539 ///Sets a global scale factor for node sizes
541 ///Sets a global scale factor for node sizes
543 GraphToEps<T> &nodeScale(double d) {_nodeScale=d;return *this;}
544 ///Sets a global scale factor for edge widths
546 ///Sets a global scale factor for edge widths
548 GraphToEps<T> &edgeWidthScale(double d) {_edgeWidthScale=d;return *this;}
549 ///Sets a global scale factor for the whole picture
551 ///Sets a global scale factor for the whole picture
553 GraphToEps<T> &scale(double d) {_scale=d;return *this;}
554 ///Sets the width of the border around the picture
556 ///Sets the width of the border around the picture
558 GraphToEps<T> &border(double b) {_xBorder=_yBorder=b;return *this;}
559 ///Sets the width of the border around the picture
561 ///Sets the width of the border around the picture
563 GraphToEps<T> &border(double x, double y) {
564 _xBorder=x;_yBorder=y;return *this;
566 ///Sets whether to draw arrows
568 ///Sets whether to draw arrows
570 GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
571 ///Sets the length of the arrowheads
573 ///Sets the length of the arrowheads
575 GraphToEps<T> &arrowLength(double d) {_arrowLength*=d;return *this;}
576 ///Sets the width of the arrowheads
578 ///Sets the width of the arrowheads
580 GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;}
582 ///Scales the drawing to fit to A4 page
584 ///Scales the drawing to fit to A4 page
586 GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
588 ///Enables parallel edges
590 ///Enables parallel edges
591 ///\todo Partially implemented
592 GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
598 GraphToEps<T> &parEdgeDist(double d) {_parEdgeDist*=d;return *this;}
604 GraphToEps<T> &hideEdges(bool b=true) {_showEdges=!b;return *this;}
609 GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
611 ///Sets the size of the node texts
613 ///Sets the size of the node texts
615 GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
617 ///Sets the color of the node texts to be different from the node color
619 ///Sets the color of the node texts to be as different from the node color
622 GraphToEps<T> &distantColorNodeTexts()
623 {_nodeTextColorType=DIST_COL;return *this;}
624 ///Sets the color of the node texts to be black or white and always visible.
626 ///Sets the color of the node texts to be black or white according to
628 ///different from the node color
630 GraphToEps<T> &distantBWNodeTexts()
631 {_nodeTextColorType=DIST_BW;return *this;}
633 ///Gives a preamble block for node Postscript block.
635 ///Gives a preamble block for node Postscript block.
638 GraphToEps<T> & nodePsTextsPreamble(const char *str) {
639 _nodePsTextsPreamble=str ;return *this;
641 ///Sets whether the the graph is undirected
643 ///Sets whether the the graph is undirected
645 GraphToEps<T> &undir(bool b=true) {_undir=b;return *this;}
646 ///Sets whether the the graph is directed
648 ///Sets whether the the graph is directed.
649 ///Use it to show the undirected edges as a pair of directed ones.
650 GraphToEps<T> &bidir(bool b=true) {_undir=!b;return *this;}
654 ///Sets the title of the generated image,
655 ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of
657 GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
658 ///Sets the copyright statement.
660 ///Sets the copyright statement of the generated image,
661 ///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of
663 ///\todo Multiline copyright notice could be supported.
664 GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;}
667 bool isInsideNode(xy<double> p, double r,int t)
671 return p.normSquare()<=r*r;
673 return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
675 return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
685 ///Like other functions using
686 ///\ref named-templ-func-param "named template parameters",
687 ///this function calles the algorithm itself, i.e. in this case
688 ///it draws the graph.
690 if(dontPrint) return;
692 os << "%!PS-Adobe-2.0 EPSF-2.0\n";
693 if(_title.size()>0) os << "%%Title: " << _title << '\n';
694 if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
695 // << "%%Copyright: XXXX\n"
696 os << "%%Creator: LEMON GraphToEps function\n";
701 gettimeofday(&tv, 0);
702 ctime_r(&tv.tv_sec,cbuf);
703 os << "%%CreationDate: " << cbuf;
705 ///\todo: Chech whether the graph is empty.
706 BoundingBox<double> bb;
707 for(NodeIt n(g);n!=INVALID;++n) {
708 double ns=_nodeSizes[n]*_nodeScale;
714 os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
715 else os << "%%BoundingBox: "
716 << bb.left()* _scale-_xBorder << ' '
717 << bb.bottom()*_scale-_yBorder << ' '
718 << bb.right()* _scale+_xBorder << ' '
719 << bb.top()* _scale+_yBorder << '\n';
721 os << "%%EndComments\n";
723 //x1 y1 x2 y2 x3 y3 cr cg cb w
724 os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
725 << " 4 2 roll 1 index 1 index curveto stroke } bind def\n";
726 os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
728 os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n";
730 os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
731 << " 2 index 1 index sub 2 index 2 index add lineto\n"
732 << " 2 index 1 index sub 2 index 2 index sub lineto\n"
733 << " 2 index 1 index add 2 index 2 index sub lineto\n"
734 << " closepath pop pop pop} bind def\n";
736 os << "/di { newpath 2 index 1 index add 2 index moveto\n"
737 << " 2 index 2 index 2 index add lineto\n"
738 << " 2 index 1 index sub 2 index lineto\n"
739 << " 2 index 2 index 2 index sub lineto\n"
740 << " closepath pop pop pop} bind def\n";
742 os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
743 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
745 os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
746 << " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
748 os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
749 << " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
751 os << "/arrl " << _arrowLength << " def\n";
752 os << "/arrw " << _arrowWidth << " def\n";
754 os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
755 //len w dx_norm dy_norm x1 y1 cr cg cb
756 os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
757 << " /w exch def /len exch def\n"
758 // << " 0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
759 << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
760 << " len w sub arrl sub dx dy lrl\n"
761 << " arrw dy dx neg lrl\n"
762 << " dx arrl w add mul dy w 2 div arrw add mul sub\n"
763 << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
764 << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
765 << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
766 << " arrw dy dx neg lrl\n"
767 << " len w sub arrl sub neg dx dy lrl\n"
768 << " closepath fill } bind def\n";
769 os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
770 << " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
774 if(bb.height()>bb.width()) {
775 double sc= min((A4HEIGHT-2*A4BORDER)/bb.height(),
776 (A4WIDTH-2*A4BORDER)/bb.width());
777 os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
778 << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER << " translate\n"
779 << sc << " dup scale\n"
780 << -bb.left() << ' ' << -bb.bottom() << " translate\n";
783 //\todo Verify centering
784 double sc= min((A4HEIGHT-2*A4BORDER)/bb.width(),
785 (A4WIDTH-2*A4BORDER)/bb.height());
786 os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
787 << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER << " translate\n"
788 << sc << " dup scale\n90 rotate\n"
789 << -bb.left() << ' ' << -bb.top() << " translate\n";
791 else if(_scale!=1.0) os << _scale << " dup scale\n";
794 os << "%Edges:\ngsave\n";
795 if(_enableParallel) {
796 std::vector<Edge> el;
797 for(EdgeIt e(g);e!=INVALID;++e)
798 if((!_undir||g.source(e)<g.target(e))&&_edgeWidths[e]>0)
800 sort(el.begin(),el.end(),edgeLess(g));
802 typename std::vector<Edge>::iterator j;
803 for(typename std::vector<Edge>::iterator i=el.begin();i!=el.end();i=j) {
804 for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
807 for(typename std::vector<Edge>::iterator e=i;e!=j;++e)
808 sw+=_edgeWidths[*e]*_edgeWidthScale+_parEdgeDist;
811 xy<double> dvec(_coords[g.target(*i)]-_coords[g.source(*i)]);
812 double l=sqrt(dvec.normSquare());
813 xy<double> d(dvec/l);
815 // m=xy<double>(_coords[g.target(*i)]+_coords[g.source(*i)])/2.0;
817 // m=xy<double>(_coords[g.source(*i)])+
818 // dvec*(double(_nodeSizes[g.source(*i)])/
819 // (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
821 m=xy<double>(_coords[g.source(*i)])+
822 d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
824 for(typename std::vector<Edge>::iterator e=i;e!=j;++e) {
825 sw+=_edgeWidths[*e]*_edgeWidthScale/2.0;
826 xy<double> mm=m+rot90(d)*sw/.75;
829 xy<double> s=_coords[g.source(*e)];
830 xy<double> t=_coords[g.target(*e)];
831 double rn=_nodeSizes[g.target(*e)]*_nodeScale;
832 node_shape=_nodeShapes[g.target(*e)];
833 Bezier3 bez(s,mm,mm,t);
835 for(int i=0;i<INTERPOL_PREC;++i)
836 if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
838 xy<double> apoint=bez((t1+t2)/2);
839 rn = _arrowLength+_edgeWidths[*e]*_edgeWidthScale;
842 for(int i=0;i<INTERPOL_PREC;++i)
843 if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
845 xy<double> linend=bez((t1+t2)/2);
846 bez=bez.before((t1+t2)/2);
847 // rn=_nodeSizes[g.source(*e)]*_nodeScale;
848 // node_shape=_nodeShapes[g.source(*e)];
850 // for(int i=0;i<INTERPOL_PREC;++i)
851 // if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t1=(t1+t2)/2;
852 // else t2=(t1+t2)/2;
853 // bez=bez.after((t1+t2)/2);
854 os << _edgeWidths[*e]*_edgeWidthScale << " setlinewidth "
855 << _edgeColors[*e].getR() << ' '
856 << _edgeColors[*e].getG() << ' '
857 << _edgeColors[*e].getB() << " setrgbcolor newpath\n"
858 << bez.p1.x << ' ' << bez.p1.y << " moveto\n"
859 << bez.p2.x << ' ' << bez.p2.y << ' '
860 << bez.p3.x << ' ' << bez.p3.y << ' '
861 << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
862 xy<double> dd(rot90(linend-apoint));
863 dd*=(.5*_edgeWidths[*e]*_edgeWidthScale+_arrowWidth)/
864 sqrt(dd.normSquare());
865 os << "newpath " << psOut(apoint) << " moveto "
866 << psOut(linend+dd) << " lineto "
867 << psOut(linend-dd) << " lineto closepath fill\n";
870 os << _coords[g.source(*e)].x << ' '
871 << _coords[g.source(*e)].y << ' '
872 << mm.x << ' ' << mm.y << ' '
873 << _coords[g.target(*e)].x << ' '
874 << _coords[g.target(*e)].y << ' '
875 << _edgeColors[*e].getR() << ' '
876 << _edgeColors[*e].getG() << ' '
877 << _edgeColors[*e].getB() << ' '
878 << _edgeWidths[*e]*_edgeWidthScale << " lb\n";
880 sw+=_edgeWidths[*e]*_edgeWidthScale/2.0+_parEdgeDist;
884 else for(EdgeIt e(g);e!=INVALID;++e)
885 if((!_undir||g.source(e)<g.target(e))&&_edgeWidths[e]>0)
887 xy<double> d(_coords[g.target(e)]-_coords[g.source(e)]);
888 double rn=_nodeSizes[g.target(e)]*_nodeScale;
889 int node_shape=_nodeShapes[g.target(e)];
891 for(int i=0;i<INTERPOL_PREC;++i)
892 if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
894 double l=sqrt(d.normSquare());
897 os << l*(1-(t1+t2)/2) << ' '
898 << _edgeWidths[e]*_edgeWidthScale << ' '
899 << d.x << ' ' << d.y << ' '
900 << _coords[g.source(e)].x << ' '
901 << _coords[g.source(e)].y << ' '
902 << _edgeColors[e].getR() << ' '
903 << _edgeColors[e].getG() << ' '
904 << _edgeColors[e].getB() << " arr\n";
906 else os << _coords[g.source(e)].x << ' '
907 << _coords[g.source(e)].y << ' '
908 << _coords[g.target(e)].x << ' '
909 << _coords[g.target(e)].y << ' '
910 << _edgeColors[e].getR() << ' '
911 << _edgeColors[e].getG() << ' '
912 << _edgeColors[e].getB() << ' '
913 << _edgeWidths[e]*_edgeWidthScale << " l\n";
917 os << "%Nodes:\ngsave\n";
918 for(NodeIt n(g);n!=INVALID;++n) {
919 os << _coords[n].x << ' ' << _coords[n].y << ' '
920 << _nodeSizes[n]*_nodeScale << ' '
921 << _nodeColors[n].getR() << ' '
922 << _nodeColors[n].getG() << ' '
923 << _nodeColors[n].getB() << ' ';
924 switch(_nodeShapes[n]) {
937 os << "%Node texts:\ngsave\n";
938 os << "/fosi " << _nodeTextSize << " def\n";
939 os << "(Helvetica) findfont fosi scalefont setfont\n";
940 for(NodeIt n(g);n!=INVALID;++n) {
941 switch(_nodeTextColorType) {
943 os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n";
946 os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n";
949 os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n";
952 os << "0 0 0 setrgbcolor\n";
954 os << _coords[n].x << ' ' << _coords[n].y
955 << " (" << _nodeTexts[n] << ") cshow\n";
959 if(_showNodePsText) {
960 os << "%Node PS blocks:\ngsave\n";
961 for(NodeIt n(g);n!=INVALID;++n)
962 os << _coords[n].x << ' ' << _coords[n].y
963 << " moveto\n" << _nodePsTexts[n] << "\n";
967 os << "grestore\nshowpage\n";
970 if(_pleaseRemoveOsStream) {delete &os;}
975 ///Generates an EPS file from a graph
978 ///Generates an EPS file from a graph.
979 ///\param g is a reference to the graph to be printed
980 ///\param os is a reference to the output stream.
981 ///By default it is <tt>std::cout</tt>
983 ///This function also has a lot of
984 ///\ref named-templ-func-param "named parameters",
985 ///they are declared as the members of class \ref GraphToEps. The following
986 ///example shows how to use these parameters.
988 /// graphToEps(g,os).scale(10).coords(coords)
989 /// .nodeScale(2).nodeSizes(sizes)
990 /// .edgeWidthScale(.4).run();
992 ///\warning Don't forget to put the \ref GraphToEps::run() "run()"
993 ///to the end of the parameter list.
995 ///\sa graphToEps(G &g, char *file_name)
997 GraphToEps<DefaultGraphToEpsTraits<G> >
998 graphToEps(G &g, std::ostream& os=std::cout)
1001 GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
1004 ///Generates an EPS file from a graph
1007 ///This function does the same as
1008 ///\ref graphToEps(G &g,std::ostream& os)
1009 ///but it writes its output into the file \c file_name
1010 ///instead of a stream.
1011 ///\sa graphToEps(G &g, std::ostream& os)
1013 GraphToEps<DefaultGraphToEpsTraits<G> >
1014 graphToEps(G &g,const char *file_name)
1016 return GraphToEps<DefaultGraphToEpsTraits<G> >
1017 (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true));
1020 } //END OF NAMESPACE LEMON
1022 #endif // LEMON_GRAPH_TO_EPS_H