Update INSTALL file.
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
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/core.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.
146 ///By default it is <tt>std::cout</tt>.
147 ///\param _pros If it is \c true, then the \c ostream referenced by \c _os
148 ///will be explicitly deallocated by the destructor.
149 DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
152 _coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0),
153 _nodeColors(WHITE), _arcColors(BLACK),
154 _arcWidths(1.0), _arcWidthScale(0.003),
155 _nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0),
156 _nodeBorderQuotient(.1),
157 _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
158 _showNodes(true), _showArcs(true),
159 _enableParallel(false), _parArcDist(1),
160 _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
161 _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
162 _undirected(lemon::UndirectedTagIndicator<G>::value),
163 _pleaseRemoveOsStream(_pros), _scaleToA4(false),
164 _nodeTextColorType(SAME_COL), _nodeTextColors(BLACK),
165 _autoNodeScale(false),
166 _autoArcWidthScale(false),
167 _absoluteNodeSizes(false),
168 _absoluteArcWidths(false),
174 ///Auxiliary class to implement the named parameters of \ref graphToEps()
176 ///Auxiliary class to implement the named parameters of \ref graphToEps().
178 ///For detailed examples see the \ref graph_to_eps_demo.cc demo file.
179 template<class T> class GraphToEps : public T
181 // Can't believe it is required by the C++ standard
187 using T::_nodeShapes;
188 using T::_nodeColors;
192 using T::_arcWidthScale;
197 using T::_nodeBorderQuotient;
199 using T::_drawArrows;
200 using T::_arrowLength;
201 using T::_arrowWidth;
206 using T::_enableParallel;
207 using T::_parArcDist;
209 using T::_showNodeText;
211 using T::_nodeTextSize;
213 using T::_showNodePsText;
214 using T::_nodePsTexts;
215 using T::_nodePsTextsPreamble;
217 using T::_undirected;
219 using T::_pleaseRemoveOsStream;
226 using T::NodeTextColorType;
230 using T::_nodeTextColorType;
231 using T::_nodeTextColors;
233 using T::_autoNodeScale;
234 using T::_autoArcWidthScale;
236 using T::_absoluteNodeSizes;
237 using T::_absoluteArcWidths;
243 // dradnats ++C eht yb deriuqer si ti eveileb t'naC
245 typedef typename T::Graph Graph;
246 typedef typename Graph::Node Node;
247 typedef typename Graph::NodeIt NodeIt;
248 typedef typename Graph::Arc Arc;
249 typedef typename Graph::ArcIt ArcIt;
250 typedef typename Graph::InArcIt InArcIt;
251 typedef typename Graph::OutArcIt OutArcIt;
253 static const int INTERPOL_PREC;
254 static const double A4HEIGHT;
255 static const double A4WIDTH;
256 static const double A4BORDER;
267 ///\image html nodeshape_0.png
268 ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm
271 ///\image html nodeshape_1.png
272 ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm
276 ///\image html nodeshape_2.png
277 ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm
281 ///\image html nodeshape_3.png
282 ///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm
286 ///\image html nodeshape_4.png
287 ///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm
296 arcLess(const Graph &_g) : g(_g) {}
297 bool operator()(Arc a,Arc b) const
299 Node ai=std::min(g.source(a),g.target(a));
300 Node aa=std::max(g.source(a),g.target(a));
301 Node bi=std::min(g.source(b),g.target(b));
302 Node ba=std::max(g.source(b),g.target(b));
304 (ai==bi && (aa < ba ||
305 (aa==ba && ai==g.source(a) && bi==g.target(b))));
308 bool isParallel(Arc e,Arc f) const
310 return (g.source(e)==g.source(f)&&
311 g.target(e)==g.target(f)) ||
312 (g.source(e)==g.target(f)&&
313 g.target(e)==g.source(f));
316 static std::string psOut(const dim2::Point<TT> &p)
318 std::ostringstream os;
319 os << p.x << ' ' << p.y;
322 static std::string psOut(const Color &c)
324 std::ostringstream os;
325 os << c.red() << ' ' << c.green() << ' ' << c.blue();
330 GraphToEps(const T &t) : T(t), dontPrint(false) {};
332 template<class X> struct CoordsTraits : public T {
333 typedef X CoordsMapType;
335 CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
337 ///Sets the map of the node coordinates
339 ///Sets the map of the node coordinates.
340 ///\param x must be a node map with \ref dim2::Point "dim2::Point<double>" or
341 ///\ref dim2::Point "dim2::Point<int>" values.
342 template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
344 return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
346 template<class X> struct NodeSizesTraits : public T {
348 NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
350 ///Sets the map of the node sizes
352 ///Sets the map of the node sizes.
353 ///\param x must be a node map with \c double (or convertible) values.
354 template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
357 return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
359 template<class X> struct NodeShapesTraits : public T {
360 const X &_nodeShapes;
361 NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
363 ///Sets the map of the node shapes
365 ///Sets the map of the node shapes.
366 ///The available shape values
367 ///can be found in \ref NodeShapes "enum NodeShapes".
368 ///\param x must be a node map with \c int (or convertible) values.
370 template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
373 return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
375 template<class X> struct NodeTextsTraits : public T {
377 NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
379 ///Sets the text printed on the nodes
381 ///Sets the text printed on the nodes.
382 ///\param x must be a node map with type that can be pushed to a standard
384 template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
388 return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
390 template<class X> struct NodePsTextsTraits : public T {
391 const X &_nodePsTexts;
392 NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
394 ///Inserts a PostScript block to the nodes
396 ///With this command it is possible to insert a verbatim PostScript
397 ///block to the nodes.
398 ///The PS current point will be moved to the center of the node before
399 ///the PostScript block inserted.
401 ///Before and after the block a newline character is inserted so you
402 ///don't have to bother with the separators.
404 ///\param x must be a node map with type that can be pushed to a standard
407 ///\sa nodePsTextsPreamble()
408 template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
411 _showNodePsText=true;
412 return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
414 template<class X> struct ArcWidthsTraits : public T {
416 ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {}
418 ///Sets the map of the arc widths
420 ///Sets the map of the arc widths.
421 ///\param x must be an arc map with \c double (or convertible) values.
422 template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x)
425 return GraphToEps<ArcWidthsTraits<X> >(ArcWidthsTraits<X>(*this,x));
428 template<class X> struct NodeColorsTraits : public T {
429 const X &_nodeColors;
430 NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
432 ///Sets the map of the node colors
434 ///Sets the map of the node colors.
435 ///\param x must be a node map with \ref Color values.
438 template<class X> GraphToEps<NodeColorsTraits<X> >
439 nodeColors(const X &x)
442 return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
444 template<class X> struct NodeTextColorsTraits : public T {
445 const X &_nodeTextColors;
446 NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {}
448 ///Sets the map of the node text colors
450 ///Sets the map of the node text colors.
451 ///\param x must be a node map with \ref Color values.
454 template<class X> GraphToEps<NodeTextColorsTraits<X> >
455 nodeTextColors(const X &x)
458 _nodeTextColorType=CUST_COL;
459 return GraphToEps<NodeTextColorsTraits<X> >
460 (NodeTextColorsTraits<X>(*this,x));
462 template<class X> struct ArcColorsTraits : public T {
464 ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {}
466 ///Sets the map of the arc colors
468 ///Sets the map of the arc colors.
469 ///\param x must be an arc map with \ref Color values.
472 template<class X> GraphToEps<ArcColorsTraits<X> >
473 arcColors(const X &x)
476 return GraphToEps<ArcColorsTraits<X> >(ArcColorsTraits<X>(*this,x));
478 ///Sets a global scale factor for node sizes
480 ///Sets a global scale factor for node sizes.
482 /// If nodeSizes() is not given, this function simply sets the node
483 /// sizes to \c d. If nodeSizes() is given, but
484 /// autoNodeScale() is not, then the node size given by
485 /// nodeSizes() will be multiplied by the value \c d.
486 /// If both nodeSizes() and autoNodeScale() are used, then the
487 /// node sizes will be scaled in such a way that the greatest size will be
490 /// \sa autoNodeScale()
491 GraphToEps<T> &nodeScale(double d=.01) {_nodeScale=d;return *this;}
492 ///Turns on/off the automatic node size scaling.
494 ///Turns on/off the automatic node size scaling.
498 GraphToEps<T> &autoNodeScale(bool b=true) {
499 _autoNodeScale=b;return *this;
502 ///Turns on/off the absolutematic node size scaling.
504 ///Turns on/off the absolutematic node size scaling.
508 GraphToEps<T> &absoluteNodeSizes(bool b=true) {
509 _absoluteNodeSizes=b;return *this;
512 ///Negates the Y coordinates.
513 GraphToEps<T> &negateY(bool b=true) {
514 _negY=b;return *this;
517 ///Turn on/off pre-scaling
519 ///By default graphToEps() rescales the whole image in order to avoid
520 ///very big or very small bounding boxes.
522 ///This (p)rescaling can be turned off with this function.
524 GraphToEps<T> &preScale(bool b=true) {
525 _preScale=b;return *this;
528 ///Sets a global scale factor for arc widths
530 /// Sets a global scale factor for arc widths.
532 /// If arcWidths() is not given, this function simply sets the arc
533 /// widths to \c d. If arcWidths() is given, but
534 /// autoArcWidthScale() is not, then the arc withs given by
535 /// arcWidths() will be multiplied by the value \c d.
536 /// If both arcWidths() and autoArcWidthScale() are used, then the
537 /// arc withs will be scaled in such a way that the greatest width will be
539 GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;}
540 ///Turns on/off the automatic arc width scaling.
542 ///Turns on/off the automatic arc width scaling.
544 ///\sa arcWidthScale()
546 GraphToEps<T> &autoArcWidthScale(bool b=true) {
547 _autoArcWidthScale=b;return *this;
549 ///Turns on/off the absolutematic arc width scaling.
551 ///Turns on/off the absolutematic arc width scaling.
553 ///\sa arcWidthScale()
555 GraphToEps<T> &absoluteArcWidths(bool b=true) {
556 _absoluteArcWidths=b;return *this;
558 ///Sets a global scale factor for the whole picture
559 GraphToEps<T> &scale(double d) {_scale=d;return *this;}
560 ///Sets the width of the border around the picture
561 GraphToEps<T> &border(double b=10) {_xBorder=_yBorder=b;return *this;}
562 ///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
567 GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
568 ///Sets the length of the arrowheads
569 GraphToEps<T> &arrowLength(double d=1.0) {_arrowLength*=d;return *this;}
570 ///Sets the width of the arrowheads
571 GraphToEps<T> &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;}
573 ///Scales the drawing to fit to A4 page
574 GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
576 ///Enables parallel arcs
577 GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
579 ///Sets the distance between parallel arcs
580 GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;}
583 GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;}
585 GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
587 ///Sets the size of the node texts
588 GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
590 ///Sets the color of the node texts to be different from the node color
592 ///Sets the color of the node texts to be as different from the node color
593 ///as it is possible.
594 GraphToEps<T> &distantColorNodeTexts()
595 {_nodeTextColorType=DIST_COL;return *this;}
596 ///Sets the color of the node texts to be black or white and always visible.
598 ///Sets the color of the node texts to be black or white according to
599 ///which is more different from the node color.
600 GraphToEps<T> &distantBWNodeTexts()
601 {_nodeTextColorType=DIST_BW;return *this;}
603 ///Gives a preamble block for node Postscript block.
605 ///Gives a preamble block for node Postscript block.
608 GraphToEps<T> & nodePsTextsPreamble(const char *str) {
609 _nodePsTextsPreamble=str ;return *this;
611 ///Sets whether the graph is undirected
613 ///Sets whether the graph is undirected.
615 ///This setting is the default for undirected graphs.
618 GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;}
620 ///Sets whether the graph is directed
622 ///Sets whether the graph is directed.
623 ///Use it to show the edges as a pair of directed ones.
625 ///This setting is the default for digraphs.
628 GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;}
632 ///Sets the title of the generated image,
633 ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of
635 GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
636 ///Sets the copyright statement.
638 ///Sets the copyright statement of the generated image,
639 ///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of
641 GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;}
644 bool isInsideNode(dim2::Point<double> p, double r,int t)
650 return p.normSquare()<=r*r;
652 return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
654 return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
664 ///Like other functions using
665 ///\ref named-templ-func-param "named template parameters",
666 ///this function calls the algorithm itself, i.e. in this case
667 ///it draws the graph.
669 //\todo better 'epsilon' would be nice here.
670 const double EPSILON=1e-9;
671 if(dontPrint) return;
673 _graph_to_eps_bits::_NegY<typename T::CoordsMapType>
674 mycoords(_coords,_negY);
676 os << "%!PS-Adobe-2.0 EPSF-2.0\n";
677 if(_title.size()>0) os << "%%Title: " << _title << '\n';
678 if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
679 os << "%%Creator: LEMON, graphToEps()\n";
684 gettimeofday(&tv, 0);
687 ctime_r(&tv.tv_sec,cbuf);
688 os << "%%CreationDate: " << cbuf;
691 char buf1[11], buf2[9], buf3[5];
693 GetSystemTime(&time);
694 if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
695 "ddd MMM dd", buf1, 11) &&
696 GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
697 "HH':'mm':'ss", buf2, 9) &&
698 GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
700 os << "%%CreationDate: " << buf1 << ' '
701 << buf2 << ' ' << buf3 << std::endl;
706 if (_autoArcWidthScale) {
708 for(ArcIt e(g);e!=INVALID;++e)
709 max_w=std::max(double(_arcWidths[e]),max_w);
710 //\todo better 'epsilon' would be nice here.
712 _arcWidthScale/=max_w;
716 if (_autoNodeScale) {
718 for(NodeIt n(g);n!=INVALID;++n)
719 max_s=std::max(double(_nodeSizes[n]),max_s);
720 //\todo better 'epsilon' would be nice here.
727 if(!(_absoluteNodeSizes&&_absoluteArcWidths)) {
728 dim2::BoundingBox<double> bb;
729 for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]);
731 bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0));
733 diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare());
734 if(diag_len<EPSILON) diag_len = 1;
735 if(!_absoluteNodeSizes) _nodeScale*=diag_len;
736 if(!_absoluteArcWidths) _arcWidthScale*=diag_len;
739 dim2::BoundingBox<double> bb;
740 for(NodeIt n(g);n!=INVALID;++n) {
741 double ns=_nodeSizes[n]*_nodeScale;
742 dim2::Point<double> p(ns,ns);
743 switch(_nodeShapes[n]) {
747 bb.add(p+mycoords[n]);
748 bb.add(-p+mycoords[n]);
751 bb.add(-p+mycoords[n]);
752 bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]);
755 bb.add(p+mycoords[n]);
756 bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]);
761 bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0));
765 os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
768 //Rescale so that BoundingBox won't be neither to big nor too small.
769 while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10;
770 while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10;
773 os << "%%BoundingBox: "
774 << int(floor(bb.left() * _scale - _xBorder)) << ' '
775 << int(floor(bb.bottom() * _scale - _yBorder)) << ' '
776 << int(ceil(bb.right() * _scale + _xBorder)) << ' '
777 << int(ceil(bb.top() * _scale + _yBorder)) << '\n';
780 os << "%%EndComments\n";
782 //x1 y1 x2 y2 x3 y3 cr cg cb w
783 os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
784 << " 4 2 roll 1 index 1 index curveto stroke } bind def\n";
785 os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke }"
788 os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath }"
791 os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
792 << " 2 index 1 index sub 2 index 2 index add lineto\n"
793 << " 2 index 1 index sub 2 index 2 index sub lineto\n"
794 << " 2 index 1 index add 2 index 2 index sub lineto\n"
795 << " closepath pop pop pop} bind def\n";
797 os << "/di { newpath 2 index 1 index add 2 index moveto\n"
798 << " 2 index 2 index 2 index add lineto\n"
799 << " 2 index 1 index sub 2 index lineto\n"
800 << " 2 index 2 index 2 index sub lineto\n"
801 << " closepath pop pop pop} bind def\n";
803 os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
804 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
806 os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
807 << " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
809 os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
810 << " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
812 os << "/nfemale { 0 0 0 setrgbcolor 3 index "
813 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
814 << " 1.5 mul mul setlinewidth\n"
815 << " newpath 5 index 5 index moveto "
816 << "5 index 5 index 5 index 3.01 mul sub\n"
817 << " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub"
819 << " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto "
821 << " 5 index 5 index 5 index c fill\n"
822 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
825 << " 0 0 0 setrgbcolor 3 index "
826 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
827 <<" 1.5 mul mul setlinewidth\n"
828 << " newpath 5 index 5 index moveto\n"
829 << " 5 index 4 index 1 mul 1.5 mul add\n"
830 << " 5 index 5 index 3 sqrt 1.5 mul mul add\n"
831 << " 1 index 1 index lineto\n"
832 << " 1 index 1 index 7 index sub moveto\n"
833 << " 1 index 1 index lineto\n"
834 << " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub"
837 << " 5 index 5 index 5 index c fill\n"
838 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
842 os << "/arrl " << _arrowLength << " def\n";
843 os << "/arrw " << _arrowWidth << " def\n";
845 os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
846 //len w dx_norm dy_norm x1 y1 cr cg cb
847 os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx "
849 << " /w exch def /len exch def\n"
850 //<< "0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
851 << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
852 << " len w sub arrl sub dx dy lrl\n"
853 << " arrw dy dx neg lrl\n"
854 << " dx arrl w add mul dy w 2 div arrw add mul sub\n"
855 << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
856 << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
857 << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
858 << " arrw dy dx neg lrl\n"
859 << " len w sub arrl sub neg dx dy lrl\n"
860 << " closepath fill } bind def\n";
861 os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
862 << " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
866 if(bb.height()>bb.width()) {
867 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(),
868 (A4WIDTH-2*A4BORDER)/bb.width());
869 os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
870 << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER
872 << sc << " dup scale\n"
873 << -bb.left() << ' ' << -bb.bottom() << " translate\n";
876 //\todo Verify centering
877 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(),
878 (A4WIDTH-2*A4BORDER)/bb.height());
879 os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
880 << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER
882 << sc << " dup scale\n90 rotate\n"
883 << -bb.left() << ' ' << -bb.top() << " translate\n";
885 else if(_scale!=1.0) os << _scale << " dup scale\n";
888 os << "%Arcs:\ngsave\n";
889 if(_enableParallel) {
891 for(ArcIt e(g);e!=INVALID;++e)
892 if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
893 &&g.source(e)!=g.target(e))
895 std::sort(el.begin(),el.end(),arcLess(g));
897 typename std::vector<Arc>::iterator j;
898 for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) {
899 for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
902 for(typename std::vector<Arc>::iterator e=i;e!=j;++e)
903 sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist;
907 dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]);
908 double l=std::sqrt(dvec.normSquare());
909 //\todo better 'epsilon' would be nice here.
910 dim2::Point<double> d(dvec/std::max(l,EPSILON));
911 dim2::Point<double> m;
912 // m=dim2::Point<double>(mycoords[g.target(*i)]+
913 // mycoords[g.source(*i)])/2.0;
915 // m=dim2::Point<double>(mycoords[g.source(*i)])+
916 // dvec*(double(_nodeSizes[g.source(*i)])/
917 // (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
919 m=dim2::Point<double>(mycoords[g.source(*i)])+
920 d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
922 for(typename std::vector<Arc>::iterator e=i;e!=j;++e) {
923 sw+=_arcWidths[*e]*_arcWidthScale/2.0;
924 dim2::Point<double> mm=m+rot90(d)*sw/.75;
927 dim2::Point<double> s=mycoords[g.source(*e)];
928 dim2::Point<double> t=mycoords[g.target(*e)];
929 double rn=_nodeSizes[g.target(*e)]*_nodeScale;
930 node_shape=_nodeShapes[g.target(*e)];
931 dim2::Bezier3 bez(s,mm,mm,t);
933 for(int ii=0;ii<INTERPOL_PREC;++ii)
934 if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
936 dim2::Point<double> apoint=bez((t1+t2)/2);
937 rn = _arrowLength+_arcWidths[*e]*_arcWidthScale;
940 for(int ii=0;ii<INTERPOL_PREC;++ii)
941 if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
943 dim2::Point<double> linend=bez((t1+t2)/2);
944 bez=bez.before((t1+t2)/2);
945 // rn=_nodeSizes[g.source(*e)]*_nodeScale;
946 // node_shape=_nodeShapes[g.source(*e)];
948 // for(int i=0;i<INTERPOL_PREC;++i)
949 // if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape))
951 // else t2=(t1+t2)/2;
952 // bez=bez.after((t1+t2)/2);
953 os << _arcWidths[*e]*_arcWidthScale << " setlinewidth "
954 << _arcColors[*e].red() << ' '
955 << _arcColors[*e].green() << ' '
956 << _arcColors[*e].blue() << " setrgbcolor newpath\n"
957 << bez.p1.x << ' ' << bez.p1.y << " moveto\n"
958 << bez.p2.x << ' ' << bez.p2.y << ' '
959 << bez.p3.x << ' ' << bez.p3.y << ' '
960 << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
961 dim2::Point<double> dd(rot90(linend-apoint));
962 dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/
963 std::sqrt(dd.normSquare());
964 os << "newpath " << psOut(apoint) << " moveto "
965 << psOut(linend+dd) << " lineto "
966 << psOut(linend-dd) << " lineto closepath fill\n";
969 os << mycoords[g.source(*e)].x << ' '
970 << mycoords[g.source(*e)].y << ' '
971 << mm.x << ' ' << mm.y << ' '
972 << mycoords[g.target(*e)].x << ' '
973 << mycoords[g.target(*e)].y << ' '
974 << _arcColors[*e].red() << ' '
975 << _arcColors[*e].green() << ' '
976 << _arcColors[*e].blue() << ' '
977 << _arcWidths[*e]*_arcWidthScale << " lb\n";
979 sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist;
983 else for(ArcIt e(g);e!=INVALID;++e)
984 if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
985 &&g.source(e)!=g.target(e)) {
987 dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]);
988 double rn=_nodeSizes[g.target(e)]*_nodeScale;
989 int node_shape=_nodeShapes[g.target(e)];
991 for(int i=0;i<INTERPOL_PREC;++i)
992 if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
994 double l=std::sqrt(d.normSquare());
997 os << l*(1-(t1+t2)/2) << ' '
998 << _arcWidths[e]*_arcWidthScale << ' '
999 << d.x << ' ' << d.y << ' '
1000 << mycoords[g.source(e)].x << ' '
1001 << mycoords[g.source(e)].y << ' '
1002 << _arcColors[e].red() << ' '
1003 << _arcColors[e].green() << ' '
1004 << _arcColors[e].blue() << " arr\n";
1006 else os << mycoords[g.source(e)].x << ' '
1007 << mycoords[g.source(e)].y << ' '
1008 << mycoords[g.target(e)].x << ' '
1009 << mycoords[g.target(e)].y << ' '
1010 << _arcColors[e].red() << ' '
1011 << _arcColors[e].green() << ' '
1012 << _arcColors[e].blue() << ' '
1013 << _arcWidths[e]*_arcWidthScale << " l\n";
1018 os << "%Nodes:\ngsave\n";
1019 for(NodeIt n(g);n!=INVALID;++n) {
1020 os << mycoords[n].x << ' ' << mycoords[n].y << ' '
1021 << _nodeSizes[n]*_nodeScale << ' '
1022 << _nodeColors[n].red() << ' '
1023 << _nodeColors[n].green() << ' '
1024 << _nodeColors[n].blue() << ' ';
1025 switch(_nodeShapes[n]) {
1035 os<< "nfemale";break;
1042 os << "%Node texts:\ngsave\n";
1043 os << "/fosi " << _nodeTextSize << " def\n";
1044 os << "(Helvetica) findfont fosi scalefont setfont\n";
1045 for(NodeIt n(g);n!=INVALID;++n) {
1046 switch(_nodeTextColorType) {
1048 os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n";
1051 os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n";
1054 os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n";
1057 os << "0 0 0 setrgbcolor\n";
1059 os << mycoords[n].x << ' ' << mycoords[n].y
1060 << " (" << _nodeTexts[n] << ") cshow\n";
1064 if(_showNodePsText) {
1065 os << "%Node PS blocks:\ngsave\n";
1066 for(NodeIt n(g);n!=INVALID;++n)
1067 os << mycoords[n].x << ' ' << mycoords[n].y
1068 << " moveto\n" << _nodePsTexts[n] << "\n";
1072 os << "grestore\nshowpage\n";
1075 if(_pleaseRemoveOsStream) {delete &os;}
1079 ///These are just some aliases to other parameter setting functions.
1083 ///An alias for arcWidths()
1084 template<class X> GraphToEps<ArcWidthsTraits<X> > edgeWidths(const X &x)
1086 return arcWidths(x);
1089 ///An alias for arcColors()
1090 template<class X> GraphToEps<ArcColorsTraits<X> >
1091 edgeColors(const X &x)
1093 return arcColors(x);
1096 ///An alias for arcWidthScale()
1097 GraphToEps<T> &edgeWidthScale(double d) {return arcWidthScale(d);}
1099 ///An alias for autoArcWidthScale()
1100 GraphToEps<T> &autoEdgeWidthScale(bool b=true)
1102 return autoArcWidthScale(b);
1105 ///An alias for absoluteArcWidths()
1106 GraphToEps<T> &absoluteEdgeWidths(bool b=true)
1108 return absoluteArcWidths(b);
1111 ///An alias for parArcDist()
1112 GraphToEps<T> &parEdgeDist(double d) {return parArcDist(d);}
1114 ///An alias for hideArcs()
1115 GraphToEps<T> &hideEdges(bool b=true) {return hideArcs(b);}
1121 const int GraphToEps<T>::INTERPOL_PREC = 20;
1123 const double GraphToEps<T>::A4HEIGHT = 841.8897637795276;
1125 const double GraphToEps<T>::A4WIDTH = 595.275590551181;
1127 const double GraphToEps<T>::A4BORDER = 15;
1130 ///Generates an EPS file from a graph
1133 ///Generates an EPS file from a graph.
1134 ///\param g Reference to the graph to be printed.
1135 ///\param os Reference to the output stream.
1136 ///By default it is <tt>std::cout</tt>.
1138 ///This function also has a lot of
1139 ///\ref named-templ-func-param "named parameters",
1140 ///they are declared as the members of class \ref GraphToEps. The following
1141 ///example shows how to use these parameters.
1143 /// graphToEps(g,os).scale(10).coords(coords)
1144 /// .nodeScale(2).nodeSizes(sizes)
1145 /// .arcWidthScale(.4).run();
1148 ///For more detailed examples see the \ref graph_to_eps_demo.cc demo file.
1150 ///\warning Don't forget to put the \ref GraphToEps::run() "run()"
1151 ///to the end of the parameter list.
1153 ///\sa graphToEps(G &g, const char *file_name)
1155 GraphToEps<DefaultGraphToEpsTraits<G> >
1156 graphToEps(G &g, std::ostream& os=std::cout)
1159 GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
1162 ///Generates an EPS file from a graph
1165 ///This function does the same as
1166 ///\ref graphToEps(G &g,std::ostream& os)
1167 ///but it writes its output into the file \c file_name
1168 ///instead of a stream.
1169 ///\sa graphToEps(G &g, std::ostream& os)
1171 GraphToEps<DefaultGraphToEpsTraits<G> >
1172 graphToEps(G &g,const char *file_name)
1174 return GraphToEps<DefaultGraphToEpsTraits<G> >
1175 (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true));
1178 ///Generates an EPS file from a graph
1181 ///This function does the same as
1182 ///\ref graphToEps(G &g,std::ostream& os)
1183 ///but it writes its output into the file \c file_name
1184 ///instead of a stream.
1185 ///\sa graphToEps(G &g, std::ostream& os)
1187 GraphToEps<DefaultGraphToEpsTraits<G> >
1188 graphToEps(G &g,const std::string& file_name)
1190 return GraphToEps<DefaultGraphToEpsTraits<G> >
1191 (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name.c_str()),true));
1194 } //END OF NAMESPACE LEMON
1196 #endif // LEMON_GRAPH_TO_EPS_H