3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_GRAPH_TO_EPS_H
20 #define LEMON_GRAPH_TO_EPS_H
33 #include<lemon/invalid.h>
35 #include<lemon/maps.h>
36 #include<lemon/bezier.h>
41 ///\brief Simple graph drawer
43 ///\author Alpar Juttner
47 ///Data structure representing RGB colors.
49 ///Data structure representing RGB colors.
55 ///Default constructor
58 Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
59 ///Returns the red component
60 double & red() {return _r;}
61 ///Returns the red component
62 const double & red() const {return _r;}
63 ///Returns the green component
64 double & green() {return _g;}
65 ///Returns the green component
66 const double & green() const {return _g;}
67 ///Returns the blue component
68 double & blue() {return _b;}
69 ///Returns the blue component
70 const double & blue() const {return _b;}
71 ///Set the color components
72 void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
75 ///Maps <tt>int</tt>s to different \ref Color "Color"s
77 ///This map assigns one of the predefined \ref Color "Color"s
78 ///to each <tt>int</tt>. It is possible to change the colors as well as their
79 ///number. The integer range is cyclically mapped to the provided set of colors.
81 ///This is a true \ref concept::ReferenceMap "reference map", so you can also
82 ///change the actual colors.
84 class ColorSet : public MapBase<int,Color>
86 std::vector<Color> colors;
91 ///\param have_white indicates whether white is
92 ///amongst the provided color (\c true) or not (\c false). If it is true,
93 ///white will be assigned to \c 0.
94 ///\param num the number of the allocated colors. If it is \c 0
95 ///the default color configuration is set up (26 color plus the while).
96 ///If \c num is less then 26/27 then the default color list is cut. Otherwise
97 ///the color list is filled repeatedly with the default color list.
98 ///(The colors can be changed later on.)
99 ColorSet(bool have_white=false,int num=0)
102 if(have_white) colors.push_back(Color(1,1,1));
104 colors.push_back(Color(0,0,0));
105 colors.push_back(Color(1,0,0));
106 colors.push_back(Color(0,1,0));
107 colors.push_back(Color(0,0,1));
108 colors.push_back(Color(1,1,0));
109 colors.push_back(Color(1,0,1));
110 colors.push_back(Color(0,1,1));
112 colors.push_back(Color(.5,0,0));
113 colors.push_back(Color(0,.5,0));
114 colors.push_back(Color(0,0,.5));
115 colors.push_back(Color(.5,.5,0));
116 colors.push_back(Color(.5,0,.5));
117 colors.push_back(Color(0,.5,.5));
119 colors.push_back(Color(.5,.5,.5));
120 colors.push_back(Color(1,.5,.5));
121 colors.push_back(Color(.5,1,.5));
122 colors.push_back(Color(.5,.5,1));
123 colors.push_back(Color(1,1,.5));
124 colors.push_back(Color(1,.5,1));
125 colors.push_back(Color(.5,1,1));
127 colors.push_back(Color(1,.5,0));
128 colors.push_back(Color(.5,1,0));
129 colors.push_back(Color(1,0,.5));
130 colors.push_back(Color(0,1,.5));
131 colors.push_back(Color(0,.5,1));
132 colors.push_back(Color(.5,0,1));
133 } while(int(colors.size())<num);
134 // colors.push_back(Color(1,1,1));
135 if(num>0) colors.resize(num);
138 Color &operator[](int i)
140 return colors[i%colors.size()];
143 const Color &operator[](int i) const
145 return colors[i%colors.size()];
148 void set(int i,const Color &c)
150 colors[i%colors.size()]=c;
152 ///Sets the number of the exiting colors.
153 void resize(int s) { colors.resize(s);}
154 ///Returns the number of the existing colors.
155 std::size_t size() const { return colors.size();}
158 ///Returns a visible distinct \ref Color
160 ///Returns a \ref Color which is as different from the given parameter
161 ///as it is possible.
162 inline Color distantColor(const Color &c)
164 return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0);
166 ///Returns black for light colors and white for the dark ones.
168 ///Returns black for light colors and white for the dark ones.
169 inline Color distantBW(const Color &c){
170 double v=(.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5?1:0;
177 typedef typename MT::Key Key;
178 typedef typename MT::Value Value;
181 _NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {}
182 Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);}
185 ///Default traits class of \ref GraphToEps
187 ///Default traits class of \ref GraphToEps
189 ///\c G is the type of the underlying graph.
191 struct DefaultGraphToEpsTraits
194 typedef typename Graph::Node Node;
195 typedef typename Graph::NodeIt NodeIt;
196 typedef typename Graph::Edge Edge;
197 typedef typename Graph::EdgeIt EdgeIt;
198 typedef typename Graph::InEdgeIt InEdgeIt;
199 typedef typename Graph::OutEdgeIt OutEdgeIt;
206 typedef ConstMap<typename Graph::Node,xy<double> > CoordsMapType;
207 CoordsMapType _coords;
208 ConstMap<typename Graph::Node,double > _nodeSizes;
209 ConstMap<typename Graph::Node,int > _nodeShapes;
211 ConstMap<typename Graph::Node,Color > _nodeColors;
212 ConstMap<typename Graph::Edge,Color > _edgeColors;
214 ConstMap<typename Graph::Edge,double > _edgeWidths;
216 double _edgeWidthScale;
219 double _xBorder, _yBorder;
221 double _nodeBorderQuotient;
224 double _arrowLength, _arrowWidth;
226 bool _showNodes, _showEdges;
228 bool _enableParallel;
232 ConstMap<typename Graph::Node,bool > _nodeTexts;
233 double _nodeTextSize;
235 bool _showNodePsText;
236 ConstMap<typename Graph::Node,bool > _nodePsTexts;
237 char *_nodePsTextsPreamble;
241 bool _pleaseRemoveOsStream;
246 std::string _copyright;
248 enum NodeTextColorType
249 { DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType;
250 ConstMap<typename Graph::Node,Color > _nodeTextColors;
253 bool _autoEdgeWidthScale;
259 ///\param _g is a reference to the graph to be printed
260 ///\param _os is a reference to the output stream.
261 ///\param _os is a reference to the output stream.
262 ///\param _pros If it is \c true, then the \c ostream referenced by \c _os
263 ///will be explicitly deallocated by the destructor.
264 ///By default it is <tt>std::cout</tt>
265 DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
268 _coords(xy<double>(1,1)), _nodeSizes(1.0), _nodeShapes(0),
269 _nodeColors(Color(1,1,1)), _edgeColors(Color(0,0,0)),
270 _edgeWidths(1), _edgeWidthScale(0.3),
271 _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0),
272 _nodeBorderQuotient(.1),
273 _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
274 _showNodes(true), _showEdges(true),
275 _enableParallel(false), _parEdgeDist(1),
276 _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
277 _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
279 _pleaseRemoveOsStream(_pros), _scaleToA4(false),
280 _nodeTextColorType(SAME_COL), _nodeTextColors(Color(0,0,0)),
281 _autoNodeScale(false),
282 _autoEdgeWidthScale(false),
287 ///Helper class to implement the named parameters of \ref graphToEps()
289 ///Helper class to implement the named parameters of \ref graphToEps()
290 ///\todo Is 'helper class' a good name for this?
292 ///\todo Follow PostScript's DSC.
293 /// Use own dictionary.
294 ///\todo Useful new features.
295 /// - Linestyles: dotted, dashed etc.
296 /// - A second color and percent value for the lines.
297 template<class T> class GraphToEps : public T
299 // Can't believe it is required by the C++ standard
305 using T::_nodeShapes;
306 using T::_nodeColors;
307 using T::_edgeColors;
308 using T::_edgeWidths;
310 using T::_edgeWidthScale;
315 using T::_nodeBorderQuotient;
317 using T::_drawArrows;
318 using T::_arrowLength;
319 using T::_arrowWidth;
324 using T::_enableParallel;
325 using T::_parEdgeDist;
327 using T::_showNodeText;
329 using T::_nodeTextSize;
331 using T::_showNodePsText;
332 using T::_nodePsTexts;
333 using T::_nodePsTextsPreamble;
335 using T::_undirected;
337 using T::_pleaseRemoveOsStream;
344 using T::NodeTextColorType;
348 using T::_nodeTextColorType;
349 using T::_nodeTextColors;
351 using T::_autoNodeScale;
352 using T::_autoEdgeWidthScale;
356 // dradnats ++C eht yb deriuqer si ti eveileb t'naC
358 typedef typename T::Graph Graph;
359 typedef typename Graph::Node Node;
360 typedef typename Graph::NodeIt NodeIt;
361 typedef typename Graph::Edge Edge;
362 typedef typename Graph::EdgeIt EdgeIt;
363 typedef typename Graph::InEdgeIt InEdgeIt;
364 typedef typename Graph::OutEdgeIt OutEdgeIt;
366 static const int INTERPOL_PREC;
367 static const double A4HEIGHT;
368 static const double A4WIDTH;
369 static const double A4BORDER;
380 ///\image html nodeshape_0.png
381 ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm
384 ///\image html nodeshape_1.png
385 ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm
389 ///\image html nodeshape_2.png
390 ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm
394 ///\image html nodeshape_3.png
395 ///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm
399 ///\image html nodeshape_4.png
400 ///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm
409 edgeLess(const Graph &_g) : g(_g) {}
410 bool operator()(Edge a,Edge b) const
412 Node ai=std::min(g.source(a),g.target(a));
413 Node aa=std::max(g.source(a),g.target(a));
414 Node bi=std::min(g.source(b),g.target(b));
415 Node ba=std::max(g.source(b),g.target(b));
417 (ai==bi && (aa < ba ||
418 (aa==ba && ai==g.source(a) && bi==g.target(b))));
421 bool isParallel(Edge e,Edge f) const
423 return (g.source(e)==g.source(f)&&
424 g.target(e)==g.target(f)) ||
425 (g.source(e)==g.target(f)&&
426 g.target(e)==g.source(f));
429 static std::string psOut(const xy<TT> &p)
431 std::ostringstream os;
432 os << p.x << ' ' << p.y;
435 static std::string psOut(const Color &c)
437 std::ostringstream os;
438 os << c.red() << ' ' << c.green() << ' ' << c.blue();
443 GraphToEps(const T &t) : T(t), dontPrint(false) {};
445 template<class X> struct CoordsTraits : public T {
446 typedef X CoordsMapType;
448 CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
450 ///Sets the map of the node coordinates
452 ///Sets the map of the node coordinates.
453 ///\param x must be a node map with xy<double> or \ref xy "xy<int>" values.
454 template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
456 return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
458 template<class X> struct NodeSizesTraits : public T {
460 NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
462 ///Sets the map of the node sizes
464 ///Sets the map of the node sizes
465 ///\param x must be a node map with \c double (or convertible) values.
466 template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
469 return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
471 template<class X> struct NodeShapesTraits : public T {
472 const X &_nodeShapes;
473 NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
475 ///Sets the map of the node shapes
477 ///Sets the map of the node shapes.
478 ///The availabe shape values
479 ///can be found in \ref NodeShapes "enum NodeShapes".
480 ///\param x must be a node map with \c int (or convertible) values.
482 template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
485 return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
487 template<class X> struct NodeTextsTraits : public T {
489 NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
491 ///Sets the text printed on the nodes
493 ///Sets the text printed on the nodes
494 ///\param x must be a node map with type that can be pushed to a standard
496 template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
500 return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
502 template<class X> struct NodePsTextsTraits : public T {
503 const X &_nodePsTexts;
504 NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
506 ///Inserts a PostScript block to the nodes
508 ///With this command it is possible to insert a verbatim PostScript
509 ///block to the nodes.
510 ///The PS current point will be moved to the centre of the node before
511 ///the PostScript block inserted.
513 ///Before and after the block a newline character is inserted so you
514 ///don't have to bother with the separators.
516 ///\param x must be a node map with type that can be pushed to a standard
519 ///\sa nodePsTextsPreamble()
520 ///\todo Offer the choise not to move to the centre but pass the coordinates
521 ///to the Postscript block inserted.
522 template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
525 _showNodePsText=true;
526 return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
528 template<class X> struct EdgeWidthsTraits : public T {
529 const X &_edgeWidths;
530 EdgeWidthsTraits(const T &t,const X &x) : T(t), _edgeWidths(x) {}
532 ///Sets the map of the edge widths
534 ///Sets the map of the edge widths
535 ///\param x must be a edge map with \c double (or convertible) values.
536 template<class X> GraphToEps<EdgeWidthsTraits<X> > edgeWidths(const X &x)
539 return GraphToEps<EdgeWidthsTraits<X> >(EdgeWidthsTraits<X>(*this,x));
542 template<class X> struct NodeColorsTraits : public T {
543 const X &_nodeColors;
544 NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
546 ///Sets the map of the node colors
548 ///Sets the map of the node colors
549 ///\param x must be a node map with \ref Color values.
552 template<class X> GraphToEps<NodeColorsTraits<X> >
553 nodeColors(const X &x)
556 return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
558 template<class X> struct NodeTextColorsTraits : public T {
559 const X &_nodeTextColors;
560 NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {}
562 ///Sets the map of the node text colors
564 ///Sets the map of the node text colors
565 ///\param x must be a node map with \ref Color values.
568 template<class X> GraphToEps<NodeTextColorsTraits<X> >
569 nodeTextColors(const X &x)
572 _nodeTextColorType=CUST_COL;
573 return GraphToEps<NodeTextColorsTraits<X> >
574 (NodeTextColorsTraits<X>(*this,x));
576 template<class X> struct EdgeColorsTraits : public T {
577 const X &_edgeColors;
578 EdgeColorsTraits(const T &t,const X &x) : T(t), _edgeColors(x) {}
580 ///Sets the map of the edge colors
582 ///Sets the map of the edge colors
583 ///\param x must be a edge map with \ref Color values.
586 template<class X> GraphToEps<EdgeColorsTraits<X> >
587 edgeColors(const X &x)
590 return GraphToEps<EdgeColorsTraits<X> >(EdgeColorsTraits<X>(*this,x));
592 ///Sets a global scale factor for node sizes
594 ///Sets a global scale factor for node sizes.
596 /// If nodeSizes() is not given, this function simply sets the node
597 /// sizes to \c d. If nodeSizes() is given, but
598 /// autoNodeScale() is not, then the node size given by
599 /// nodeSizes() will be multiplied by the value \c d.
600 /// If both nodeSizes() and autoNodeScale() are used, then the
601 /// node sizes will be scaled in such a way that the greatest size will be
603 GraphToEps<T> &nodeScale(double d) {_nodeScale=d;return *this;}
604 ///Turns on/off the automatic node width scaling.
606 ///Turns on/off the automatic node width scaling.
610 GraphToEps<T> &autoNodeScale(bool b=true) {
611 _autoNodeScale=b;return *this;
614 ///Negates the Y coordinates.
616 ///Negates the Y coordinates.
620 GraphToEps<T> &negateY(bool b=true) {
621 _negY=b;return *this;
624 ///Sets a global scale factor for edge widths
626 /// Sets a global scale factor for edge widths.
628 /// If edgeWidths() is not given, this function simply sets the edge
629 /// widths to \c d. If edgeWidths() is given, but
630 /// autoEdgeWidthScale() is not, then the edge withs given by
631 /// edgeWidths() will be multiplied by the value \c d.
632 /// If both edgeWidths() and autoEdgeWidthScale() are used, then the
633 /// edge withs will be scaled in such a way that the greatest width will be
635 GraphToEps<T> &edgeWidthScale(double d) {_edgeWidthScale=d;return *this;}
636 ///Turns on/off the automatic edge width scaling.
638 ///Turns on/off the automatic edge width scaling.
640 ///\sa edgeWidthScale()
642 GraphToEps<T> &autoEdgeWidthScale(bool b=true) {
643 _autoEdgeWidthScale=b;return *this;
645 ///Sets a global scale factor for the whole picture
647 ///Sets a global scale factor for the whole picture
650 GraphToEps<T> &scale(double d) {_scale=d;return *this;}
651 ///Sets the width of the border around the picture
653 ///Sets the width of the border around the picture
655 GraphToEps<T> &border(double b) {_xBorder=_yBorder=b;return *this;}
656 ///Sets the width of the border around the picture
658 ///Sets the width of the border around the picture
660 GraphToEps<T> &border(double x, double y) {
661 _xBorder=x;_yBorder=y;return *this;
663 ///Sets whether to draw arrows
665 ///Sets whether to draw arrows
667 GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
668 ///Sets the length of the arrowheads
670 ///Sets the length of the arrowheads
672 GraphToEps<T> &arrowLength(double d) {_arrowLength*=d;return *this;}
673 ///Sets the width of the arrowheads
675 ///Sets the width of the arrowheads
677 GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;}
679 ///Scales the drawing to fit to A4 page
681 ///Scales the drawing to fit to A4 page
683 GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
685 ///Enables parallel edges
687 ///Enables parallel edges
688 GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
694 GraphToEps<T> &parEdgeDist(double d) {_parEdgeDist*=d;return *this;}
700 GraphToEps<T> &hideEdges(bool b=true) {_showEdges=!b;return *this;}
705 GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
707 ///Sets the size of the node texts
709 ///Sets the size of the node texts
711 GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
713 ///Sets the color of the node texts to be different from the node color
715 ///Sets the color of the node texts to be as different from the node color
718 GraphToEps<T> &distantColorNodeTexts()
719 {_nodeTextColorType=DIST_COL;return *this;}
720 ///Sets the color of the node texts to be black or white and always visible.
722 ///Sets the color of the node texts to be black or white according to
724 ///different from the node color
726 GraphToEps<T> &distantBWNodeTexts()
727 {_nodeTextColorType=DIST_BW;return *this;}
729 ///Gives a preamble block for node Postscript block.
731 ///Gives a preamble block for node Postscript block.
734 GraphToEps<T> & nodePsTextsPreamble(const char *str) {
735 _nodePsTextsPreamble=str ;return *this;
737 ///Sets whether the the graph is undirected
739 ///Sets whether the the graph is undirected
741 GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;}
743 ///Sets whether the the graph is directed
745 ///Sets whether the the graph is directed.
746 ///Use it to show the undirected edges as a pair of directed ones.
747 GraphToEps<T> &bidir(bool b=true) {_undirected=!b;return *this;}
751 ///Sets the title of the generated image,
752 ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of
754 GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
755 ///Sets the copyright statement.
757 ///Sets the copyright statement of the generated image,
758 ///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of
760 ///\todo Multiline copyright notice could be supported.
761 GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;}
764 bool isInsideNode(xy<double> p, double r,int t)
770 return p.normSquare()<=r*r;
772 return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
774 return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
784 ///Like other functions using
785 ///\ref named-templ-func-param "named template parameters",
786 ///this function calles the algorithm itself, i.e. in this case
787 ///it draws the graph.
789 if(dontPrint) return;
791 _NegY<typename T::CoordsMapType> mycoords(_coords,_negY);
793 os << "%!PS-Adobe-2.0 EPSF-2.0\n";
794 if(_title.size()>0) os << "%%Title: " << _title << '\n';
795 if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
796 // << "%%Copyright: XXXX\n"
797 os << "%%Creator: LEMON, graphToEps()\n";
802 gettimeofday(&tv, 0);
803 ctime_r(&tv.tv_sec,cbuf);
804 os << "%%CreationDate: " << cbuf;
807 if (_autoEdgeWidthScale) {
809 for(EdgeIt e(g);e!=INVALID;++e)
810 max_w=std::max(double(_edgeWidths[e]),max_w);
811 ///\todo better 'epsilon' would be nice here.
813 _edgeWidthScale/=max_w;
817 if (_autoNodeScale) {
819 for(NodeIt n(g);n!=INVALID;++n)
820 max_s=std::max(double(_nodeSizes[n]),max_s);
821 ///\todo better 'epsilon' would be nice here.
828 BoundingBox<double> bb;
829 ///\bug: Chech whether the graph is empty.
830 for(NodeIt n(g);n!=INVALID;++n) {
831 double ns=_nodeSizes[n]*_nodeScale;
833 switch(_nodeShapes[n]) {
837 bb.add(p+mycoords[n]);
838 bb.add(-p+mycoords[n]);
841 bb.add(-p+mycoords[n]);
842 bb.add(xy<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]);
845 bb.add(p+mycoords[n]);
846 bb.add(xy<double>(-ns,-3.01*ns)+mycoords[n]);
851 bb = BoundingBox<double>(xy<double>(0,0));
855 os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
857 //Rescale so that BoundingBox won't be neither to big nor too small.
858 while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10;
859 while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10;
861 os << "%%BoundingBox: "
862 << int(floor(bb.left() * _scale - _xBorder)) << ' '
863 << int(floor(bb.bottom() * _scale - _yBorder)) << ' '
864 << int(ceil(bb.right() * _scale + _xBorder)) << ' '
865 << int(ceil(bb.top() * _scale + _yBorder)) << '\n';
868 os << "%%EndComments\n";
870 //x1 y1 x2 y2 x3 y3 cr cg cb w
871 os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
872 << " 4 2 roll 1 index 1 index curveto stroke } bind def\n";
873 os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
875 os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n";
877 os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
878 << " 2 index 1 index sub 2 index 2 index add lineto\n"
879 << " 2 index 1 index sub 2 index 2 index sub lineto\n"
880 << " 2 index 1 index add 2 index 2 index sub lineto\n"
881 << " closepath pop pop pop} bind def\n";
883 os << "/di { newpath 2 index 1 index add 2 index moveto\n"
884 << " 2 index 2 index 2 index add lineto\n"
885 << " 2 index 1 index sub 2 index lineto\n"
886 << " 2 index 2 index 2 index sub lineto\n"
887 << " closepath pop pop pop} bind def\n";
889 os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
890 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
892 os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
893 << " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
895 os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
896 << " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
898 os << "/nfemale { 0 0 0 setrgbcolor 3 index "
899 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
900 << " 1.5 mul mul setlinewidth\n"
901 << " newpath 5 index 5 index moveto "
902 << "5 index 5 index 5 index 3.01 mul sub\n"
903 << " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub moveto\n"
904 << " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto stroke\n"
905 << " 5 index 5 index 5 index c fill\n"
906 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
909 << " 0 0 0 setrgbcolor 3 index "
910 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
911 <<" 1.5 mul mul setlinewidth\n"
912 << " newpath 5 index 5 index moveto\n"
913 << " 5 index 4 index 1 mul 1.5 mul add\n"
914 << " 5 index 5 index 3 sqrt 1.5 mul mul add\n"
915 << " 1 index 1 index lineto\n"
916 << " 1 index 1 index 7 index sub moveto\n"
917 << " 1 index 1 index lineto\n"
918 << " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub lineto\n"
920 << " 5 index 5 index 5 index c fill\n"
921 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
925 os << "/arrl " << _arrowLength << " def\n";
926 os << "/arrw " << _arrowWidth << " def\n";
928 os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
929 //len w dx_norm dy_norm x1 y1 cr cg cb
930 os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
931 << " /w exch def /len exch def\n"
932 // << " 0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
933 << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
934 << " len w sub arrl sub dx dy lrl\n"
935 << " arrw dy dx neg lrl\n"
936 << " dx arrl w add mul dy w 2 div arrw add mul sub\n"
937 << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
938 << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
939 << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
940 << " arrw dy dx neg lrl\n"
941 << " len w sub arrl sub neg dx dy lrl\n"
942 << " closepath fill } bind def\n";
943 os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
944 << " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
948 if(bb.height()>bb.width()) {
949 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(),
950 (A4WIDTH-2*A4BORDER)/bb.width());
951 os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
952 << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER << " translate\n"
953 << sc << " dup scale\n"
954 << -bb.left() << ' ' << -bb.bottom() << " translate\n";
957 //\todo Verify centering
958 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(),
959 (A4WIDTH-2*A4BORDER)/bb.height());
960 os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
961 << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER << " translate\n"
962 << sc << " dup scale\n90 rotate\n"
963 << -bb.left() << ' ' << -bb.top() << " translate\n";
965 else if(_scale!=1.0) os << _scale << " dup scale\n";
968 os << "%Edges:\ngsave\n";
969 if(_enableParallel) {
970 std::vector<Edge> el;
971 for(EdgeIt e(g);e!=INVALID;++e)
972 if((!_undirected||g.source(e)<g.target(e))&&_edgeWidths[e]>0)
974 std::sort(el.begin(),el.end(),edgeLess(g));
976 typename std::vector<Edge>::iterator j;
977 for(typename std::vector<Edge>::iterator i=el.begin();i!=el.end();i=j) {
978 for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
981 for(typename std::vector<Edge>::iterator e=i;e!=j;++e)
982 sw+=_edgeWidths[*e]*_edgeWidthScale+_parEdgeDist;
985 xy<double> dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]);
986 double l=std::sqrt(dvec.normSquare());
987 ///\todo better 'epsilon' would be nice here.
988 xy<double> d(dvec/std::max(l,1e-9));
990 // m=xy<double>(mycoords[g.target(*i)]+mycoords[g.source(*i)])/2.0;
992 // m=xy<double>(mycoords[g.source(*i)])+
993 // dvec*(double(_nodeSizes[g.source(*i)])/
994 // (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
996 m=xy<double>(mycoords[g.source(*i)])+
997 d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
999 for(typename std::vector<Edge>::iterator e=i;e!=j;++e) {
1000 sw+=_edgeWidths[*e]*_edgeWidthScale/2.0;
1001 xy<double> mm=m+rot90(d)*sw/.75;
1004 xy<double> s=mycoords[g.source(*e)];
1005 xy<double> t=mycoords[g.target(*e)];
1006 double rn=_nodeSizes[g.target(*e)]*_nodeScale;
1007 node_shape=_nodeShapes[g.target(*e)];
1008 Bezier3 bez(s,mm,mm,t);
1010 for(int i=0;i<INTERPOL_PREC;++i)
1011 if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
1013 xy<double> apoint=bez((t1+t2)/2);
1014 rn = _arrowLength+_edgeWidths[*e]*_edgeWidthScale;
1017 for(int i=0;i<INTERPOL_PREC;++i)
1018 if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
1020 xy<double> linend=bez((t1+t2)/2);
1021 bez=bez.before((t1+t2)/2);
1022 // rn=_nodeSizes[g.source(*e)]*_nodeScale;
1023 // node_shape=_nodeShapes[g.source(*e)];
1025 // for(int i=0;i<INTERPOL_PREC;++i)
1026 // if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t1=(t1+t2)/2;
1027 // else t2=(t1+t2)/2;
1028 // bez=bez.after((t1+t2)/2);
1029 os << _edgeWidths[*e]*_edgeWidthScale << " setlinewidth "
1030 << _edgeColors[*e].red() << ' '
1031 << _edgeColors[*e].green() << ' '
1032 << _edgeColors[*e].blue() << " setrgbcolor newpath\n"
1033 << bez.p1.x << ' ' << bez.p1.y << " moveto\n"
1034 << bez.p2.x << ' ' << bez.p2.y << ' '
1035 << bez.p3.x << ' ' << bez.p3.y << ' '
1036 << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
1037 xy<double> dd(rot90(linend-apoint));
1038 dd*=(.5*_edgeWidths[*e]*_edgeWidthScale+_arrowWidth)/
1039 std::sqrt(dd.normSquare());
1040 os << "newpath " << psOut(apoint) << " moveto "
1041 << psOut(linend+dd) << " lineto "
1042 << psOut(linend-dd) << " lineto closepath fill\n";
1045 os << mycoords[g.source(*e)].x << ' '
1046 << mycoords[g.source(*e)].y << ' '
1047 << mm.x << ' ' << mm.y << ' '
1048 << mycoords[g.target(*e)].x << ' '
1049 << mycoords[g.target(*e)].y << ' '
1050 << _edgeColors[*e].red() << ' '
1051 << _edgeColors[*e].green() << ' '
1052 << _edgeColors[*e].blue() << ' '
1053 << _edgeWidths[*e]*_edgeWidthScale << " lb\n";
1055 sw+=_edgeWidths[*e]*_edgeWidthScale/2.0+_parEdgeDist;
1059 else for(EdgeIt e(g);e!=INVALID;++e)
1060 if((!_undirected||g.source(e)<g.target(e))&&_edgeWidths[e]>0)
1062 xy<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]);
1063 double rn=_nodeSizes[g.target(e)]*_nodeScale;
1064 int node_shape=_nodeShapes[g.target(e)];
1066 for(int i=0;i<INTERPOL_PREC;++i)
1067 if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
1069 double l=std::sqrt(d.normSquare());
1072 os << l*(1-(t1+t2)/2) << ' '
1073 << _edgeWidths[e]*_edgeWidthScale << ' '
1074 << d.x << ' ' << d.y << ' '
1075 << mycoords[g.source(e)].x << ' '
1076 << mycoords[g.source(e)].y << ' '
1077 << _edgeColors[e].red() << ' '
1078 << _edgeColors[e].green() << ' '
1079 << _edgeColors[e].blue() << " arr\n";
1081 else os << mycoords[g.source(e)].x << ' '
1082 << mycoords[g.source(e)].y << ' '
1083 << mycoords[g.target(e)].x << ' '
1084 << mycoords[g.target(e)].y << ' '
1085 << _edgeColors[e].red() << ' '
1086 << _edgeColors[e].green() << ' '
1087 << _edgeColors[e].blue() << ' '
1088 << _edgeWidths[e]*_edgeWidthScale << " l\n";
1092 os << "%Nodes:\ngsave\n";
1093 for(NodeIt n(g);n!=INVALID;++n) {
1094 os << mycoords[n].x << ' ' << mycoords[n].y << ' '
1095 << _nodeSizes[n]*_nodeScale << ' '
1096 << _nodeColors[n].red() << ' '
1097 << _nodeColors[n].green() << ' '
1098 << _nodeColors[n].blue() << ' ';
1099 switch(_nodeShapes[n]) {
1109 os<< "nfemale";break;
1116 os << "%Node texts:\ngsave\n";
1117 os << "/fosi " << _nodeTextSize << " def\n";
1118 os << "(Helvetica) findfont fosi scalefont setfont\n";
1119 for(NodeIt n(g);n!=INVALID;++n) {
1120 switch(_nodeTextColorType) {
1122 os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n";
1125 os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n";
1128 os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n";
1131 os << "0 0 0 setrgbcolor\n";
1133 os << mycoords[n].x << ' ' << mycoords[n].y
1134 << " (" << _nodeTexts[n] << ") cshow\n";
1138 if(_showNodePsText) {
1139 os << "%Node PS blocks:\ngsave\n";
1140 for(NodeIt n(g);n!=INVALID;++n)
1141 os << mycoords[n].x << ' ' << mycoords[n].y
1142 << " moveto\n" << _nodePsTexts[n] << "\n";
1146 os << "grestore\nshowpage\n";
1149 if(_pleaseRemoveOsStream) {delete &os;}
1154 const int GraphToEps<T>::INTERPOL_PREC = 20;
1156 const double GraphToEps<T>::A4HEIGHT = 841.8897637795276;
1158 const double GraphToEps<T>::A4WIDTH = 595.275590551181;
1160 const double GraphToEps<T>::A4BORDER = 15;
1163 ///Generates an EPS file from a graph
1165 ///\ingroup io_group
1166 ///Generates an EPS file from a graph.
1167 ///\param g is a reference to the graph to be printed
1168 ///\param os is a reference to the output stream.
1169 ///By default it is <tt>std::cout</tt>
1171 ///This function also has a lot of
1172 ///\ref named-templ-func-param "named parameters",
1173 ///they are declared as the members of class \ref GraphToEps. The following
1174 ///example shows how to use these parameters.
1176 /// graphToEps(g,os).scale(10).coords(coords)
1177 /// .nodeScale(2).nodeSizes(sizes)
1178 /// .edgeWidthScale(.4).run();
1180 ///\warning Don't forget to put the \ref GraphToEps::run() "run()"
1181 ///to the end of the parameter list.
1183 ///\sa graphToEps(G &g, const char *file_name)
1185 GraphToEps<DefaultGraphToEpsTraits<G> >
1186 graphToEps(G &g, std::ostream& os=std::cout)
1189 GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
1192 ///Generates an EPS file from a graph
1194 ///\ingroup io_group
1195 ///This function does the same as
1196 ///\ref graphToEps(G &g,std::ostream& os)
1197 ///but it writes its output into the file \c file_name
1198 ///instead of a stream.
1199 ///\sa graphToEps(G &g, std::ostream& os)
1201 GraphToEps<DefaultGraphToEpsTraits<G> >
1202 graphToEps(G &g,const char *file_name)
1204 return GraphToEps<DefaultGraphToEpsTraits<G> >
1205 (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true));
1208 ///Generates an EPS file from a graph
1210 ///\ingroup io_group
1211 ///This function does the same as
1212 ///\ref graphToEps(G &g,std::ostream& os)
1213 ///but it writes its output into the file \c file_name
1214 ///instead of a stream.
1215 ///\sa graphToEps(G &g, std::ostream& os)
1217 GraphToEps<DefaultGraphToEpsTraits<G> >
1218 graphToEps(G &g,const std::string& file_name)
1220 return GraphToEps<DefaultGraphToEpsTraits<G> >
1221 (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name.c_str()),true));
1224 } //END OF NAMESPACE LEMON
1226 #endif // LEMON_GRAPH_TO_EPS_H