3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
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
32 #define WIN32_LEAN_AND_MEAN
37 #include<lemon/math.h>
38 #include<lemon/bits/invalid.h>
39 #include<lemon/dim2.h>
40 #include<lemon/maps.h>
41 #include<lemon/color.h>
42 #include<lemon/bits/bezier.h>
47 ///\brief A well configurable tool for visualizing graphs
51 namespace _graph_to_eps_bits {
55 typedef typename MT::Key Key;
56 typedef typename MT::Value Value;
59 _NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {}
60 Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);}
64 ///Default traits class of \ref GraphToEps
66 ///Default traits class of \ref GraphToEps.
68 ///\c G is the type of the underlying graph.
70 struct DefaultGraphToEpsTraits
73 typedef typename Graph::Node Node;
74 typedef typename Graph::NodeIt NodeIt;
75 typedef typename Graph::Arc Arc;
76 typedef typename Graph::ArcIt ArcIt;
77 typedef typename Graph::InArcIt InArcIt;
78 typedef typename Graph::OutArcIt OutArcIt;
85 typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType;
86 CoordsMapType _coords;
87 ConstMap<typename Graph::Node,double > _nodeSizes;
88 ConstMap<typename Graph::Node,int > _nodeShapes;
90 ConstMap<typename Graph::Node,Color > _nodeColors;
91 ConstMap<typename Graph::Arc,Color > _arcColors;
93 ConstMap<typename Graph::Arc,double > _arcWidths;
95 double _arcWidthScale;
98 double _xBorder, _yBorder;
100 double _nodeBorderQuotient;
103 double _arrowLength, _arrowWidth;
105 bool _showNodes, _showArcs;
107 bool _enableParallel;
111 ConstMap<typename Graph::Node,bool > _nodeTexts;
112 double _nodeTextSize;
114 bool _showNodePsText;
115 ConstMap<typename Graph::Node,bool > _nodePsTexts;
116 char *_nodePsTextsPreamble;
120 bool _pleaseRemoveOsStream;
125 std::string _copyright;
127 enum NodeTextColorType
128 { DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType;
129 ConstMap<typename Graph::Node,Color > _nodeTextColors;
132 bool _autoArcWidthScale;
134 bool _absoluteNodeSizes;
135 bool _absoluteArcWidths;
143 ///\param _g Reference to the graph to be printed.
144 ///\param _os Reference to the output stream.
145 ///\param _os Reference to the output stream. By default it is <tt>std::cout</tt>.
146 ///\param _pros If it is \c true, then the \c ostream referenced by \c _os
147 ///will be explicitly deallocated by the destructor.
148 DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
151 _coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0),
152 _nodeColors(WHITE), _arcColors(BLACK),
153 _arcWidths(1.0), _arcWidthScale(0.003),
154 _nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0),
155 _nodeBorderQuotient(.1),
156 _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
157 _showNodes(true), _showArcs(true),
158 _enableParallel(false), _parArcDist(1),
159 _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
160 _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
161 _undirected(lemon::UndirectedTagIndicator<G>::value),
162 _pleaseRemoveOsStream(_pros), _scaleToA4(false),
163 _nodeTextColorType(SAME_COL), _nodeTextColors(BLACK),
164 _autoNodeScale(false),
165 _autoArcWidthScale(false),
166 _absoluteNodeSizes(false),
167 _absoluteArcWidths(false),
173 ///Auxiliary class to implement the named parameters of \ref graphToEps()
175 ///Auxiliary class to implement the named parameters of \ref graphToEps().
177 ///For detailed examples see the \ref graph_to_eps_demo.cc demo file.
178 template<class T> class GraphToEps : public T
180 // Can't believe it is required by the C++ standard
186 using T::_nodeShapes;
187 using T::_nodeColors;
191 using T::_arcWidthScale;
196 using T::_nodeBorderQuotient;
198 using T::_drawArrows;
199 using T::_arrowLength;
200 using T::_arrowWidth;
205 using T::_enableParallel;
206 using T::_parArcDist;
208 using T::_showNodeText;
210 using T::_nodeTextSize;
212 using T::_showNodePsText;
213 using T::_nodePsTexts;
214 using T::_nodePsTextsPreamble;
216 using T::_undirected;
218 using T::_pleaseRemoveOsStream;
225 using T::NodeTextColorType;
229 using T::_nodeTextColorType;
230 using T::_nodeTextColors;
232 using T::_autoNodeScale;
233 using T::_autoArcWidthScale;
235 using T::_absoluteNodeSizes;
236 using T::_absoluteArcWidths;
242 // dradnats ++C eht yb deriuqer si ti eveileb t'naC
244 typedef typename T::Graph Graph;
245 typedef typename Graph::Node Node;
246 typedef typename Graph::NodeIt NodeIt;
247 typedef typename Graph::Arc Arc;
248 typedef typename Graph::ArcIt ArcIt;
249 typedef typename Graph::InArcIt InArcIt;
250 typedef typename Graph::OutArcIt OutArcIt;
252 static const int INTERPOL_PREC;
253 static const double A4HEIGHT;
254 static const double A4WIDTH;
255 static const double A4BORDER;
266 ///\image html nodeshape_0.png
267 ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm
270 ///\image html nodeshape_1.png
271 ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm
275 ///\image html nodeshape_2.png
276 ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm
280 ///\image html nodeshape_3.png
281 ///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm
285 ///\image html nodeshape_4.png
286 ///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm
295 arcLess(const Graph &_g) : g(_g) {}
296 bool operator()(Arc a,Arc b) const
298 Node ai=std::min(g.source(a),g.target(a));
299 Node aa=std::max(g.source(a),g.target(a));
300 Node bi=std::min(g.source(b),g.target(b));
301 Node ba=std::max(g.source(b),g.target(b));
303 (ai==bi && (aa < ba ||
304 (aa==ba && ai==g.source(a) && bi==g.target(b))));
307 bool isParallel(Arc e,Arc f) const
309 return (g.source(e)==g.source(f)&&
310 g.target(e)==g.target(f)) ||
311 (g.source(e)==g.target(f)&&
312 g.target(e)==g.source(f));
315 static std::string psOut(const dim2::Point<TT> &p)
317 std::ostringstream os;
318 os << p.x << ' ' << p.y;
321 static std::string psOut(const Color &c)
323 std::ostringstream os;
324 os << c.red() << ' ' << c.green() << ' ' << c.blue();
329 GraphToEps(const T &t) : T(t), dontPrint(false) {};
331 template<class X> struct CoordsTraits : public T {
332 typedef X CoordsMapType;
334 CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
336 ///Sets the map of the node coordinates
338 ///Sets the map of the node coordinates.
339 ///\param x must be a node map with \ref dim2::Point "dim2::Point<double>" or
340 ///\ref dim2::Point "dim2::Point<int>" values.
341 template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
343 return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
345 template<class X> struct NodeSizesTraits : public T {
347 NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
349 ///Sets the map of the node sizes
351 ///Sets the map of the node sizes.
352 ///\param x must be a node map with \c double (or convertible) values.
353 template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
356 return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
358 template<class X> struct NodeShapesTraits : public T {
359 const X &_nodeShapes;
360 NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
362 ///Sets the map of the node shapes
364 ///Sets the map of the node shapes.
365 ///The available shape values
366 ///can be found in \ref NodeShapes "enum NodeShapes".
367 ///\param x must be a node map with \c int (or convertible) values.
369 template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
372 return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
374 template<class X> struct NodeTextsTraits : public T {
376 NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
378 ///Sets the text printed on the nodes
380 ///Sets the text printed on the nodes.
381 ///\param x must be a node map with type that can be pushed to a standard
383 template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
387 return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
389 template<class X> struct NodePsTextsTraits : public T {
390 const X &_nodePsTexts;
391 NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
393 ///Inserts a PostScript block to the nodes
395 ///With this command it is possible to insert a verbatim PostScript
396 ///block to the nodes.
397 ///The PS current point will be moved to the center of the node before
398 ///the PostScript block inserted.
400 ///Before and after the block a newline character is inserted so you
401 ///don't have to bother with the separators.
403 ///\param x must be a node map with type that can be pushed to a standard
406 ///\sa nodePsTextsPreamble()
407 template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
410 _showNodePsText=true;
411 return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
413 template<class X> struct ArcWidthsTraits : public T {
415 ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {}
417 ///Sets the map of the arc widths
419 ///Sets the map of the arc widths.
420 ///\param x must be an arc map with \c double (or convertible) values.
421 template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x)
424 return GraphToEps<ArcWidthsTraits<X> >(ArcWidthsTraits<X>(*this,x));
427 template<class X> struct NodeColorsTraits : public T {
428 const X &_nodeColors;
429 NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
431 ///Sets the map of the node colors
433 ///Sets the map of the node colors.
434 ///\param x must be a node map with \ref Color values.
437 template<class X> GraphToEps<NodeColorsTraits<X> >
438 nodeColors(const X &x)
441 return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
443 template<class X> struct NodeTextColorsTraits : public T {
444 const X &_nodeTextColors;
445 NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {}
447 ///Sets the map of the node text colors
449 ///Sets the map of the node text colors.
450 ///\param x must be a node map with \ref Color values.
453 template<class X> GraphToEps<NodeTextColorsTraits<X> >
454 nodeTextColors(const X &x)
457 _nodeTextColorType=CUST_COL;
458 return GraphToEps<NodeTextColorsTraits<X> >
459 (NodeTextColorsTraits<X>(*this,x));
461 template<class X> struct ArcColorsTraits : public T {
463 ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {}
465 ///Sets the map of the arc colors
467 ///Sets the map of the arc colors.
468 ///\param x must be an arc map with \ref Color values.
471 template<class X> GraphToEps<ArcColorsTraits<X> >
472 arcColors(const X &x)
475 return GraphToEps<ArcColorsTraits<X> >(ArcColorsTraits<X>(*this,x));
477 ///Sets a global scale factor for node sizes
479 ///Sets a global scale factor for node sizes.
481 /// If nodeSizes() is not given, this function simply sets the node
482 /// sizes to \c d. If nodeSizes() is given, but
483 /// autoNodeScale() is not, then the node size given by
484 /// nodeSizes() will be multiplied by the value \c d.
485 /// If both nodeSizes() and autoNodeScale() are used, then the
486 /// node sizes will be scaled in such a way that the greatest size will be
489 /// \sa autoNodeScale()
490 GraphToEps<T> &nodeScale(double d=.01) {_nodeScale=d;return *this;}
491 ///Turns on/off the automatic node size scaling.
493 ///Turns on/off the automatic node size scaling.
497 GraphToEps<T> &autoNodeScale(bool b=true) {
498 _autoNodeScale=b;return *this;
501 ///Turns on/off the absolutematic node size scaling.
503 ///Turns on/off the absolutematic node size scaling.
507 GraphToEps<T> &absoluteNodeSizes(bool b=true) {
508 _absoluteNodeSizes=b;return *this;
511 ///Negates the Y coordinates.
512 GraphToEps<T> &negateY(bool b=true) {
513 _negY=b;return *this;
516 ///Turn on/off pre-scaling
518 ///By default graphToEps() rescales the whole image in order to avoid
519 ///very big or very small bounding boxes.
521 ///This (p)rescaling can be turned off with this function.
523 GraphToEps<T> &preScale(bool b=true) {
524 _preScale=b;return *this;
527 ///Sets a global scale factor for arc widths
529 /// Sets a global scale factor for arc widths.
531 /// If arcWidths() is not given, this function simply sets the arc
532 /// widths to \c d. If arcWidths() is given, but
533 /// autoArcWidthScale() is not, then the arc withs given by
534 /// arcWidths() will be multiplied by the value \c d.
535 /// If both arcWidths() and autoArcWidthScale() are used, then the
536 /// arc withs will be scaled in such a way that the greatest width will be
538 GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;}
539 ///Turns on/off the automatic arc width scaling.
541 ///Turns on/off the automatic arc width scaling.
543 ///\sa arcWidthScale()
545 GraphToEps<T> &autoArcWidthScale(bool b=true) {
546 _autoArcWidthScale=b;return *this;
548 ///Turns on/off the absolutematic arc width scaling.
550 ///Turns on/off the absolutematic arc width scaling.
552 ///\sa arcWidthScale()
554 GraphToEps<T> &absoluteArcWidths(bool b=true) {
555 _absoluteArcWidths=b;return *this;
557 ///Sets a global scale factor for the whole picture
558 GraphToEps<T> &scale(double d) {_scale=d;return *this;}
559 ///Sets the width of the border around the picture
560 GraphToEps<T> &border(double b=10) {_xBorder=_yBorder=b;return *this;}
561 ///Sets the width of the border around the picture
562 GraphToEps<T> &border(double x, double y) {
563 _xBorder=x;_yBorder=y;return *this;
565 ///Sets whether to draw arrows
566 GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
567 ///Sets the length of the arrowheads
568 GraphToEps<T> &arrowLength(double d=1.0) {_arrowLength*=d;return *this;}
569 ///Sets the width of the arrowheads
570 GraphToEps<T> &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;}
572 ///Scales the drawing to fit to A4 page
573 GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
575 ///Enables parallel arcs
576 GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
578 ///Sets the distance between parallel arcs
579 GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;}
582 GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;}
584 GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
586 ///Sets the size of the node texts
587 GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
589 ///Sets the color of the node texts to be different from the node color
591 ///Sets the color of the node texts to be as different from the node color
592 ///as it is possible.
593 GraphToEps<T> &distantColorNodeTexts()
594 {_nodeTextColorType=DIST_COL;return *this;}
595 ///Sets the color of the node texts to be black or white and always visible.
597 ///Sets the color of the node texts to be black or white according to
598 ///which is more different from the node color.
599 GraphToEps<T> &distantBWNodeTexts()
600 {_nodeTextColorType=DIST_BW;return *this;}
602 ///Gives a preamble block for node Postscript block.
604 ///Gives a preamble block for node Postscript block.
607 GraphToEps<T> & nodePsTextsPreamble(const char *str) {
608 _nodePsTextsPreamble=str ;return *this;
610 ///Sets whether the graph is undirected
612 ///Sets whether the graph is undirected.
614 ///This setting is the default for undirected graphs.
617 GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;}
619 ///Sets whether the graph is directed
621 ///Sets whether the graph is directed.
622 ///Use it to show the edges as a pair of directed ones.
624 ///This setting is the default for digraphs.
627 GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;}
631 ///Sets the title of the generated image,
632 ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of
634 GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
635 ///Sets the copyright statement.
637 ///Sets the copyright statement of the generated image,
638 ///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of
640 GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;}
643 bool isInsideNode(dim2::Point<double> p, double r,int t)
649 return p.normSquare()<=r*r;
651 return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
653 return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
663 ///Like other functions using
664 ///\ref named-templ-func-param "named template parameters",
665 ///this function calls the algorithm itself, i.e. in this case
666 ///it draws the graph.
668 //\todo better 'epsilon' would be nice here.
669 const double EPSILON=1e-9;
670 if(dontPrint) return;
672 _graph_to_eps_bits::_NegY<typename T::CoordsMapType>
673 mycoords(_coords,_negY);
675 os << "%!PS-Adobe-2.0 EPSF-2.0\n";
676 if(_title.size()>0) os << "%%Title: " << _title << '\n';
677 if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
678 os << "%%Creator: LEMON, graphToEps()\n";
683 gettimeofday(&tv, 0);
686 ctime_r(&tv.tv_sec,cbuf);
687 os << "%%CreationDate: " << cbuf;
690 char buf1[11], buf2[9], buf3[5];
692 GetSystemTime(&time);
693 if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
694 "ddd MMM dd", buf1, 11) &&
695 GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
696 "HH':'mm':'ss", buf2, 9) &&
697 GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
699 os << "%%CreationDate: " << buf1 << ' '
700 << buf2 << ' ' << buf3 << std::endl;
705 if (_autoArcWidthScale) {
707 for(ArcIt e(g);e!=INVALID;++e)
708 max_w=std::max(double(_arcWidths[e]),max_w);
709 //\todo better 'epsilon' would be nice here.
711 _arcWidthScale/=max_w;
715 if (_autoNodeScale) {
717 for(NodeIt n(g);n!=INVALID;++n)
718 max_s=std::max(double(_nodeSizes[n]),max_s);
719 //\todo better 'epsilon' would be nice here.
726 if(!(_absoluteNodeSizes&&_absoluteArcWidths)) {
727 dim2::BoundingBox<double> bb;
728 for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]);
730 bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0));
732 diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare());
733 if(diag_len<EPSILON) diag_len = 1;
734 if(!_absoluteNodeSizes) _nodeScale*=diag_len;
735 if(!_absoluteArcWidths) _arcWidthScale*=diag_len;
738 dim2::BoundingBox<double> bb;
739 for(NodeIt n(g);n!=INVALID;++n) {
740 double ns=_nodeSizes[n]*_nodeScale;
741 dim2::Point<double> p(ns,ns);
742 switch(_nodeShapes[n]) {
746 bb.add(p+mycoords[n]);
747 bb.add(-p+mycoords[n]);
750 bb.add(-p+mycoords[n]);
751 bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]);
754 bb.add(p+mycoords[n]);
755 bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]);
760 bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0));
764 os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
767 //Rescale so that BoundingBox won't be neither to big nor too small.
768 while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10;
769 while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10;
772 os << "%%BoundingBox: "
773 << int(floor(bb.left() * _scale - _xBorder)) << ' '
774 << int(floor(bb.bottom() * _scale - _yBorder)) << ' '
775 << int(ceil(bb.right() * _scale + _xBorder)) << ' '
776 << int(ceil(bb.top() * _scale + _yBorder)) << '\n';
779 os << "%%EndComments\n";
781 //x1 y1 x2 y2 x3 y3 cr cg cb w
782 os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
783 << " 4 2 roll 1 index 1 index curveto stroke } bind def\n";
784 os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
786 os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n";
788 os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
789 << " 2 index 1 index sub 2 index 2 index add lineto\n"
790 << " 2 index 1 index sub 2 index 2 index sub lineto\n"
791 << " 2 index 1 index add 2 index 2 index sub lineto\n"
792 << " closepath pop pop pop} bind def\n";
794 os << "/di { newpath 2 index 1 index add 2 index moveto\n"
795 << " 2 index 2 index 2 index add lineto\n"
796 << " 2 index 1 index sub 2 index lineto\n"
797 << " 2 index 2 index 2 index sub lineto\n"
798 << " closepath pop pop pop} bind def\n";
800 os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
801 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
803 os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
804 << " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
806 os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
807 << " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
809 os << "/nfemale { 0 0 0 setrgbcolor 3 index "
810 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
811 << " 1.5 mul mul setlinewidth\n"
812 << " newpath 5 index 5 index moveto "
813 << "5 index 5 index 5 index 3.01 mul sub\n"
814 << " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub moveto\n"
815 << " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto stroke\n"
816 << " 5 index 5 index 5 index c fill\n"
817 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
820 << " 0 0 0 setrgbcolor 3 index "
821 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
822 <<" 1.5 mul mul setlinewidth\n"
823 << " newpath 5 index 5 index moveto\n"
824 << " 5 index 4 index 1 mul 1.5 mul add\n"
825 << " 5 index 5 index 3 sqrt 1.5 mul mul add\n"
826 << " 1 index 1 index lineto\n"
827 << " 1 index 1 index 7 index sub moveto\n"
828 << " 1 index 1 index lineto\n"
829 << " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub lineto\n"
831 << " 5 index 5 index 5 index c fill\n"
832 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
836 os << "/arrl " << _arrowLength << " def\n";
837 os << "/arrw " << _arrowWidth << " def\n";
839 os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
840 //len w dx_norm dy_norm x1 y1 cr cg cb
841 os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
842 << " /w exch def /len exch def\n"
843 // << " 0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
844 << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
845 << " len w sub arrl sub dx dy lrl\n"
846 << " arrw dy dx neg lrl\n"
847 << " dx arrl w add mul dy w 2 div arrw add mul sub\n"
848 << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
849 << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
850 << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
851 << " arrw dy dx neg lrl\n"
852 << " len w sub arrl sub neg dx dy lrl\n"
853 << " closepath fill } bind def\n";
854 os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
855 << " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
859 if(bb.height()>bb.width()) {
860 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(),
861 (A4WIDTH-2*A4BORDER)/bb.width());
862 os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
863 << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER
865 << sc << " dup scale\n"
866 << -bb.left() << ' ' << -bb.bottom() << " translate\n";
869 //\todo Verify centering
870 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(),
871 (A4WIDTH-2*A4BORDER)/bb.height());
872 os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
873 << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER
875 << sc << " dup scale\n90 rotate\n"
876 << -bb.left() << ' ' << -bb.top() << " translate\n";
878 else if(_scale!=1.0) os << _scale << " dup scale\n";
881 os << "%Arcs:\ngsave\n";
882 if(_enableParallel) {
884 for(ArcIt e(g);e!=INVALID;++e)
885 if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
886 &&g.source(e)!=g.target(e))
888 std::sort(el.begin(),el.end(),arcLess(g));
890 typename std::vector<Arc>::iterator j;
891 for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) {
892 for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
895 for(typename std::vector<Arc>::iterator e=i;e!=j;++e)
896 sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist;
900 dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]);
901 double l=std::sqrt(dvec.normSquare());
902 //\todo better 'epsilon' would be nice here.
903 dim2::Point<double> d(dvec/std::max(l,EPSILON));
904 dim2::Point<double> m;
905 // m=dim2::Point<double>(mycoords[g.target(*i)]+mycoords[g.source(*i)])/2.0;
907 // m=dim2::Point<double>(mycoords[g.source(*i)])+
908 // dvec*(double(_nodeSizes[g.source(*i)])/
909 // (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
911 m=dim2::Point<double>(mycoords[g.source(*i)])+
912 d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
914 for(typename std::vector<Arc>::iterator e=i;e!=j;++e) {
915 sw+=_arcWidths[*e]*_arcWidthScale/2.0;
916 dim2::Point<double> mm=m+rot90(d)*sw/.75;
919 dim2::Point<double> s=mycoords[g.source(*e)];
920 dim2::Point<double> t=mycoords[g.target(*e)];
921 double rn=_nodeSizes[g.target(*e)]*_nodeScale;
922 node_shape=_nodeShapes[g.target(*e)];
923 dim2::Bezier3 bez(s,mm,mm,t);
925 for(int ii=0;ii<INTERPOL_PREC;++ii)
926 if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
928 dim2::Point<double> apoint=bez((t1+t2)/2);
929 rn = _arrowLength+_arcWidths[*e]*_arcWidthScale;
932 for(int ii=0;ii<INTERPOL_PREC;++ii)
933 if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
935 dim2::Point<double> linend=bez((t1+t2)/2);
936 bez=bez.before((t1+t2)/2);
937 // rn=_nodeSizes[g.source(*e)]*_nodeScale;
938 // node_shape=_nodeShapes[g.source(*e)];
940 // for(int i=0;i<INTERPOL_PREC;++i)
941 // if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t1=(t1+t2)/2;
942 // else t2=(t1+t2)/2;
943 // bez=bez.after((t1+t2)/2);
944 os << _arcWidths[*e]*_arcWidthScale << " setlinewidth "
945 << _arcColors[*e].red() << ' '
946 << _arcColors[*e].green() << ' '
947 << _arcColors[*e].blue() << " setrgbcolor newpath\n"
948 << bez.p1.x << ' ' << bez.p1.y << " moveto\n"
949 << bez.p2.x << ' ' << bez.p2.y << ' '
950 << bez.p3.x << ' ' << bez.p3.y << ' '
951 << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
952 dim2::Point<double> dd(rot90(linend-apoint));
953 dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/
954 std::sqrt(dd.normSquare());
955 os << "newpath " << psOut(apoint) << " moveto "
956 << psOut(linend+dd) << " lineto "
957 << psOut(linend-dd) << " lineto closepath fill\n";
960 os << mycoords[g.source(*e)].x << ' '
961 << mycoords[g.source(*e)].y << ' '
962 << mm.x << ' ' << mm.y << ' '
963 << mycoords[g.target(*e)].x << ' '
964 << mycoords[g.target(*e)].y << ' '
965 << _arcColors[*e].red() << ' '
966 << _arcColors[*e].green() << ' '
967 << _arcColors[*e].blue() << ' '
968 << _arcWidths[*e]*_arcWidthScale << " lb\n";
970 sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist;
974 else for(ArcIt e(g);e!=INVALID;++e)
975 if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
976 &&g.source(e)!=g.target(e)) {
978 dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]);
979 double rn=_nodeSizes[g.target(e)]*_nodeScale;
980 int node_shape=_nodeShapes[g.target(e)];
982 for(int i=0;i<INTERPOL_PREC;++i)
983 if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
985 double l=std::sqrt(d.normSquare());
988 os << l*(1-(t1+t2)/2) << ' '
989 << _arcWidths[e]*_arcWidthScale << ' '
990 << d.x << ' ' << d.y << ' '
991 << mycoords[g.source(e)].x << ' '
992 << mycoords[g.source(e)].y << ' '
993 << _arcColors[e].red() << ' '
994 << _arcColors[e].green() << ' '
995 << _arcColors[e].blue() << " arr\n";
997 else os << mycoords[g.source(e)].x << ' '
998 << mycoords[g.source(e)].y << ' '
999 << mycoords[g.target(e)].x << ' '
1000 << mycoords[g.target(e)].y << ' '
1001 << _arcColors[e].red() << ' '
1002 << _arcColors[e].green() << ' '
1003 << _arcColors[e].blue() << ' '
1004 << _arcWidths[e]*_arcWidthScale << " l\n";
1009 os << "%Nodes:\ngsave\n";
1010 for(NodeIt n(g);n!=INVALID;++n) {
1011 os << mycoords[n].x << ' ' << mycoords[n].y << ' '
1012 << _nodeSizes[n]*_nodeScale << ' '
1013 << _nodeColors[n].red() << ' '
1014 << _nodeColors[n].green() << ' '
1015 << _nodeColors[n].blue() << ' ';
1016 switch(_nodeShapes[n]) {
1026 os<< "nfemale";break;
1033 os << "%Node texts:\ngsave\n";
1034 os << "/fosi " << _nodeTextSize << " def\n";
1035 os << "(Helvetica) findfont fosi scalefont setfont\n";
1036 for(NodeIt n(g);n!=INVALID;++n) {
1037 switch(_nodeTextColorType) {
1039 os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n";
1042 os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n";
1045 os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n";
1048 os << "0 0 0 setrgbcolor\n";
1050 os << mycoords[n].x << ' ' << mycoords[n].y
1051 << " (" << _nodeTexts[n] << ") cshow\n";
1055 if(_showNodePsText) {
1056 os << "%Node PS blocks:\ngsave\n";
1057 for(NodeIt n(g);n!=INVALID;++n)
1058 os << mycoords[n].x << ' ' << mycoords[n].y
1059 << " moveto\n" << _nodePsTexts[n] << "\n";
1063 os << "grestore\nshowpage\n";
1066 if(_pleaseRemoveOsStream) {delete &os;}
1070 ///These are just some aliases to other parameter setting functions.
1074 ///An alias for arcWidths()
1075 template<class X> GraphToEps<ArcWidthsTraits<X> > edgeWidths(const X &x)
1077 return arcWidths(x);
1080 ///An alias for arcColors()
1081 template<class X> GraphToEps<ArcColorsTraits<X> >
1082 edgeColors(const X &x)
1084 return arcColors(x);
1087 ///An alias for arcWidthScale()
1088 GraphToEps<T> &edgeWidthScale(double d) {return arcWidthScale(d);}
1090 ///An alias for autoArcWidthScale()
1091 GraphToEps<T> &autoEdgeWidthScale(bool b=true)
1093 return autoArcWidthScale(b);
1096 ///An alias for absoluteArcWidths()
1097 GraphToEps<T> &absoluteEdgeWidths(bool b=true)
1099 return absoluteArcWidths(b);
1102 ///An alias for parArcDist()
1103 GraphToEps<T> &parEdgeDist(double d) {return parArcDist(d);}
1105 ///An alias for hideArcs()
1106 GraphToEps<T> &hideEdges(bool b=true) {return hideArcs(b);}
1112 const int GraphToEps<T>::INTERPOL_PREC = 20;
1114 const double GraphToEps<T>::A4HEIGHT = 841.8897637795276;
1116 const double GraphToEps<T>::A4WIDTH = 595.275590551181;
1118 const double GraphToEps<T>::A4BORDER = 15;
1121 ///Generates an EPS file from a graph
1124 ///Generates an EPS file from a graph.
1125 ///\param g Reference to the graph to be printed.
1126 ///\param os Reference to the output stream.
1127 ///By default it is <tt>std::cout</tt>.
1129 ///This function also has a lot of
1130 ///\ref named-templ-func-param "named parameters",
1131 ///they are declared as the members of class \ref GraphToEps. The following
1132 ///example shows how to use these parameters.
1134 /// graphToEps(g,os).scale(10).coords(coords)
1135 /// .nodeScale(2).nodeSizes(sizes)
1136 /// .arcWidthScale(.4).run();
1139 ///For more detailed examples see the \ref graph_to_eps_demo.cc demo file.
1141 ///\warning Don't forget to put the \ref GraphToEps::run() "run()"
1142 ///to the end of the parameter list.
1144 ///\sa graphToEps(G &g, const char *file_name)
1146 GraphToEps<DefaultGraphToEpsTraits<G> >
1147 graphToEps(G &g, std::ostream& os=std::cout)
1150 GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
1153 ///Generates an EPS file from a graph
1156 ///This function does the same as
1157 ///\ref graphToEps(G &g,std::ostream& os)
1158 ///but it writes its output into the file \c file_name
1159 ///instead of a stream.
1160 ///\sa graphToEps(G &g, std::ostream& os)
1162 GraphToEps<DefaultGraphToEpsTraits<G> >
1163 graphToEps(G &g,const char *file_name)
1165 return GraphToEps<DefaultGraphToEpsTraits<G> >
1166 (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true));
1169 ///Generates an EPS file from a graph
1172 ///This function does the same as
1173 ///\ref graphToEps(G &g,std::ostream& os)
1174 ///but it writes its output into the file \c file_name
1175 ///instead of a stream.
1176 ///\sa graphToEps(G &g, std::ostream& os)
1178 GraphToEps<DefaultGraphToEpsTraits<G> >
1179 graphToEps(G &g,const std::string& file_name)
1181 return GraphToEps<DefaultGraphToEpsTraits<G> >
1182 (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name.c_str()),true));
1185 } //END OF NAMESPACE LEMON
1187 #endif // LEMON_GRAPH_TO_EPS_H