1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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 #ifndef WIN32_LEAN_AND_MEAN
33 #define WIN32_LEAN_AND_MEAN
41 #include<lemon/math.h>
42 #include<lemon/core.h>
43 #include<lemon/dim2.h>
44 #include<lemon/maps.h>
45 #include<lemon/color.h>
46 #include<lemon/bits/bezier.h>
47 #include<lemon/error.h>
52 ///\brief A well configurable tool for visualizing graphs
56 namespace _graph_to_eps_bits {
60 typedef typename MT::Key Key;
61 typedef typename MT::Value Value;
64 _NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {}
65 Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);}
69 ///Default traits class of GraphToEps
71 ///Default traits class of \ref GraphToEps.
73 ///\c G is the type of the underlying graph.
75 struct DefaultGraphToEpsTraits
78 typedef typename Graph::Node Node;
79 typedef typename Graph::NodeIt NodeIt;
80 typedef typename Graph::Arc Arc;
81 typedef typename Graph::ArcIt ArcIt;
82 typedef typename Graph::InArcIt InArcIt;
83 typedef typename Graph::OutArcIt OutArcIt;
90 typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType;
91 CoordsMapType _coords;
92 ConstMap<typename Graph::Node,double > _nodeSizes;
93 ConstMap<typename Graph::Node,int > _nodeShapes;
95 ConstMap<typename Graph::Node,Color > _nodeColors;
96 ConstMap<typename Graph::Arc,Color > _arcColors;
98 ConstMap<typename Graph::Arc,double > _arcWidths;
100 double _arcWidthScale;
103 double _xBorder, _yBorder;
105 double _nodeBorderQuotient;
108 double _arrowLength, _arrowWidth;
110 bool _showNodes, _showArcs;
112 bool _enableParallel;
116 ConstMap<typename Graph::Node,bool > _nodeTexts;
117 double _nodeTextSize;
119 bool _showNodePsText;
120 ConstMap<typename Graph::Node,bool > _nodePsTexts;
121 char *_nodePsTextsPreamble;
125 bool _pleaseRemoveOsStream;
130 std::string _copyright;
132 enum NodeTextColorType
133 { DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType;
134 ConstMap<typename Graph::Node,Color > _nodeTextColors;
137 bool _autoArcWidthScale;
139 bool _absoluteNodeSizes;
140 bool _absoluteArcWidths;
148 ///\param _g Reference to the graph to be printed.
149 ///\param _os Reference to the output stream.
150 ///\param _os Reference to the output stream.
151 ///By default it is <tt>std::cout</tt>.
152 ///\param _pros If it is \c true, then the \c ostream referenced by \c _os
153 ///will be explicitly deallocated by the destructor.
154 DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
157 _coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0),
158 _nodeColors(WHITE), _arcColors(BLACK),
159 _arcWidths(1.0), _arcWidthScale(0.003),
160 _nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0),
161 _nodeBorderQuotient(.1),
162 _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
163 _showNodes(true), _showArcs(true),
164 _enableParallel(false), _parArcDist(1),
165 _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
166 _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
167 _undirected(lemon::UndirectedTagIndicator<G>::value),
168 _pleaseRemoveOsStream(_pros), _scaleToA4(false),
169 _nodeTextColorType(SAME_COL), _nodeTextColors(BLACK),
170 _autoNodeScale(false),
171 _autoArcWidthScale(false),
172 _absoluteNodeSizes(false),
173 _absoluteArcWidths(false),
179 ///Auxiliary class to implement the named parameters of \ref graphToEps()
181 ///Auxiliary class to implement the named parameters of \ref graphToEps().
183 ///For detailed examples see the \ref graph_to_eps_demo.cc demo file.
184 template<class T> class GraphToEps : public T
186 // Can't believe it is required by the C++ standard
192 using T::_nodeShapes;
193 using T::_nodeColors;
197 using T::_arcWidthScale;
202 using T::_nodeBorderQuotient;
204 using T::_drawArrows;
205 using T::_arrowLength;
206 using T::_arrowWidth;
211 using T::_enableParallel;
212 using T::_parArcDist;
214 using T::_showNodeText;
216 using T::_nodeTextSize;
218 using T::_showNodePsText;
219 using T::_nodePsTexts;
220 using T::_nodePsTextsPreamble;
222 using T::_undirected;
224 using T::_pleaseRemoveOsStream;
231 using T::NodeTextColorType;
235 using T::_nodeTextColorType;
236 using T::_nodeTextColors;
238 using T::_autoNodeScale;
239 using T::_autoArcWidthScale;
241 using T::_absoluteNodeSizes;
242 using T::_absoluteArcWidths;
248 // dradnats ++C eht yb deriuqer si ti eveileb t'naC
250 typedef typename T::Graph Graph;
251 typedef typename Graph::Node Node;
252 typedef typename Graph::NodeIt NodeIt;
253 typedef typename Graph::Arc Arc;
254 typedef typename Graph::ArcIt ArcIt;
255 typedef typename Graph::InArcIt InArcIt;
256 typedef typename Graph::OutArcIt OutArcIt;
258 static const int INTERPOL_PREC;
259 static const double A4HEIGHT;
260 static const double A4WIDTH;
261 static const double A4BORDER;
272 ///\image html nodeshape_0.png
273 ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm
276 ///\image html nodeshape_1.png
277 ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm
281 ///\image html nodeshape_2.png
282 ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm
286 ///\image html nodeshape_3.png
287 ///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm
291 ///\image html nodeshape_4.png
292 ///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm
301 arcLess(const Graph &_g) : g(_g) {}
302 bool operator()(Arc a,Arc b) const
304 Node ai=std::min(g.source(a),g.target(a));
305 Node aa=std::max(g.source(a),g.target(a));
306 Node bi=std::min(g.source(b),g.target(b));
307 Node ba=std::max(g.source(b),g.target(b));
309 (ai==bi && (aa < ba ||
310 (aa==ba && ai==g.source(a) && bi==g.target(b))));
313 bool isParallel(Arc e,Arc f) const
315 return (g.source(e)==g.source(f)&&
316 g.target(e)==g.target(f)) ||
317 (g.source(e)==g.target(f)&&
318 g.target(e)==g.source(f));
321 static std::string psOut(const dim2::Point<TT> &p)
323 std::ostringstream os;
324 os << p.x << ' ' << p.y;
327 static std::string psOut(const Color &c)
329 std::ostringstream os;
330 os << c.red() << ' ' << c.green() << ' ' << c.blue();
335 GraphToEps(const T &t) : T(t), dontPrint(false) {};
337 template<class X> struct CoordsTraits : public T {
338 typedef X CoordsMapType;
340 CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
342 ///Sets the map of the node coordinates
344 ///Sets the map of the node coordinates.
345 ///\param x must be a node map with \ref dim2::Point "dim2::Point<double>" or
346 ///\ref dim2::Point "dim2::Point<int>" values.
347 template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
349 return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
351 template<class X> struct NodeSizesTraits : public T {
353 NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
355 ///Sets the map of the node sizes
357 ///Sets the map of the node sizes.
358 ///\param x must be a node map with \c double (or convertible) values.
359 template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
362 return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
364 template<class X> struct NodeShapesTraits : public T {
365 const X &_nodeShapes;
366 NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
368 ///Sets the map of the node shapes
370 ///Sets the map of the node shapes.
371 ///The available shape values
372 ///can be found in \ref NodeShapes "enum NodeShapes".
373 ///\param x must be a node map with \c int (or convertible) values.
375 template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
378 return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
380 template<class X> struct NodeTextsTraits : public T {
382 NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
384 ///Sets the text printed on the nodes
386 ///Sets the text printed on the nodes.
387 ///\param x must be a node map with type that can be pushed to a standard
389 template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
393 return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
395 template<class X> struct NodePsTextsTraits : public T {
396 const X &_nodePsTexts;
397 NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
399 ///Inserts a PostScript block to the nodes
401 ///With this command it is possible to insert a verbatim PostScript
402 ///block to the nodes.
403 ///The PS current point will be moved to the center of the node before
404 ///the PostScript block inserted.
406 ///Before and after the block a newline character is inserted so you
407 ///don't have to bother with the separators.
409 ///\param x must be a node map with type that can be pushed to a standard
412 ///\sa nodePsTextsPreamble()
413 template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
416 _showNodePsText=true;
417 return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
419 template<class X> struct ArcWidthsTraits : public T {
421 ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {}
423 ///Sets the map of the arc widths
425 ///Sets the map of the arc widths.
426 ///\param x must be an arc map with \c double (or convertible) values.
427 template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x)
430 return GraphToEps<ArcWidthsTraits<X> >(ArcWidthsTraits<X>(*this,x));
433 template<class X> struct NodeColorsTraits : public T {
434 const X &_nodeColors;
435 NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
437 ///Sets the map of the node colors
439 ///Sets the map of the node colors.
440 ///\param x must be a node map with \ref Color values.
443 template<class X> GraphToEps<NodeColorsTraits<X> >
444 nodeColors(const X &x)
447 return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
449 template<class X> struct NodeTextColorsTraits : public T {
450 const X &_nodeTextColors;
451 NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {}
453 ///Sets the map of the node text colors
455 ///Sets the map of the node text colors.
456 ///\param x must be a node map with \ref Color values.
459 template<class X> GraphToEps<NodeTextColorsTraits<X> >
460 nodeTextColors(const X &x)
463 _nodeTextColorType=CUST_COL;
464 return GraphToEps<NodeTextColorsTraits<X> >
465 (NodeTextColorsTraits<X>(*this,x));
467 template<class X> struct ArcColorsTraits : public T {
469 ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {}
471 ///Sets the map of the arc colors
473 ///Sets the map of the arc colors.
474 ///\param x must be an arc map with \ref Color values.
477 template<class X> GraphToEps<ArcColorsTraits<X> >
478 arcColors(const X &x)
481 return GraphToEps<ArcColorsTraits<X> >(ArcColorsTraits<X>(*this,x));
483 ///Sets a global scale factor for node sizes
485 ///Sets a global scale factor for node sizes.
487 /// If nodeSizes() is not given, this function simply sets the node
488 /// sizes to \c d. If nodeSizes() is given, but
489 /// autoNodeScale() is not, then the node size given by
490 /// nodeSizes() will be multiplied by the value \c d.
491 /// If both nodeSizes() and autoNodeScale() are used, then the
492 /// node sizes will be scaled in such a way that the greatest size will be
495 /// \sa autoNodeScale()
496 GraphToEps<T> &nodeScale(double d=.01) {_nodeScale=d;return *this;}
497 ///Turns on/off the automatic node size scaling.
499 ///Turns on/off the automatic node size scaling.
503 GraphToEps<T> &autoNodeScale(bool b=true) {
504 _autoNodeScale=b;return *this;
507 ///Turns on/off the absolutematic node size scaling.
509 ///Turns on/off the absolutematic node size scaling.
513 GraphToEps<T> &absoluteNodeSizes(bool b=true) {
514 _absoluteNodeSizes=b;return *this;
517 ///Negates the Y coordinates.
518 GraphToEps<T> &negateY(bool b=true) {
519 _negY=b;return *this;
522 ///Turn on/off pre-scaling
524 ///By default graphToEps() rescales the whole image in order to avoid
525 ///very big or very small bounding boxes.
527 ///This (p)rescaling can be turned off with this function.
529 GraphToEps<T> &preScale(bool b=true) {
530 _preScale=b;return *this;
533 ///Sets a global scale factor for arc widths
535 /// Sets a global scale factor for arc widths.
537 /// If arcWidths() is not given, this function simply sets the arc
538 /// widths to \c d. If arcWidths() is given, but
539 /// autoArcWidthScale() is not, then the arc withs given by
540 /// arcWidths() will be multiplied by the value \c d.
541 /// If both arcWidths() and autoArcWidthScale() are used, then the
542 /// arc withs will be scaled in such a way that the greatest width will be
544 GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;}
545 ///Turns on/off the automatic arc width scaling.
547 ///Turns on/off the automatic arc width scaling.
549 ///\sa arcWidthScale()
551 GraphToEps<T> &autoArcWidthScale(bool b=true) {
552 _autoArcWidthScale=b;return *this;
554 ///Turns on/off the absolutematic arc width scaling.
556 ///Turns on/off the absolutematic arc width scaling.
558 ///\sa arcWidthScale()
560 GraphToEps<T> &absoluteArcWidths(bool b=true) {
561 _absoluteArcWidths=b;return *this;
563 ///Sets a global scale factor for the whole picture
564 GraphToEps<T> &scale(double d) {_scale=d;return *this;}
565 ///Sets the width of the border around the picture
566 GraphToEps<T> &border(double b=10) {_xBorder=_yBorder=b;return *this;}
567 ///Sets the width of the border around the picture
568 GraphToEps<T> &border(double x, double y) {
569 _xBorder=x;_yBorder=y;return *this;
571 ///Sets whether to draw arrows
572 GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
573 ///Sets the length of the arrowheads
574 GraphToEps<T> &arrowLength(double d=1.0) {_arrowLength*=d;return *this;}
575 ///Sets the width of the arrowheads
576 GraphToEps<T> &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;}
578 ///Scales the drawing to fit to A4 page
579 GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
581 ///Enables parallel arcs
582 GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
584 ///Sets the distance between parallel arcs
585 GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;}
588 GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;}
590 GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
592 ///Sets the size of the node texts
593 GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
595 ///Sets the color of the node texts to be different from the node color
597 ///Sets the color of the node texts to be as different from the node color
598 ///as it is possible.
599 GraphToEps<T> &distantColorNodeTexts()
600 {_nodeTextColorType=DIST_COL;return *this;}
601 ///Sets the color of the node texts to be black or white and always visible.
603 ///Sets the color of the node texts to be black or white according to
604 ///which is more different from the node color.
605 GraphToEps<T> &distantBWNodeTexts()
606 {_nodeTextColorType=DIST_BW;return *this;}
608 ///Gives a preamble block for node Postscript block.
610 ///Gives a preamble block for node Postscript block.
613 GraphToEps<T> & nodePsTextsPreamble(const char *str) {
614 _nodePsTextsPreamble=str ;return *this;
616 ///Sets whether the graph is undirected
618 ///Sets whether the graph is undirected.
620 ///This setting is the default for undirected graphs.
623 GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;}
625 ///Sets whether the graph is directed
627 ///Sets whether the graph is directed.
628 ///Use it to show the edges as a pair of directed ones.
630 ///This setting is the default for digraphs.
633 GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;}
637 ///Sets the title of the generated image,
638 ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of
640 GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
641 ///Sets the copyright statement.
643 ///Sets the copyright statement of the generated image,
644 ///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of
646 GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;}
649 bool isInsideNode(dim2::Point<double> p, double r,int t)
655 return p.normSquare()<=r*r;
657 return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
659 return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
669 ///Like other functions using
670 ///\ref named-templ-func-param "named template parameters",
671 ///this function calls the algorithm itself, i.e. in this case
672 ///it draws the graph.
674 const double EPSILON=1e-9;
675 if(dontPrint) return;
677 _graph_to_eps_bits::_NegY<typename T::CoordsMapType>
678 mycoords(_coords,_negY);
680 os << "%!PS-Adobe-2.0 EPSF-2.0\n";
681 if(_title.size()>0) os << "%%Title: " << _title << '\n';
682 if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
683 os << "%%Creator: LEMON, graphToEps()\n";
688 gettimeofday(&tv, 0);
691 ctime_r(&tv.tv_sec,cbuf);
692 os << "%%CreationDate: " << cbuf;
695 GetSystemTime(&time);
696 #if defined(_MSC_VER) && (_MSC_VER < 1500)
697 LPWSTR buf1, buf2, buf3;
698 if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
699 L"ddd MMM dd", buf1, 11) &&
700 GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
701 L"HH':'mm':'ss", buf2, 9) &&
702 GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
704 os << "%%CreationDate: " << buf1 << ' '
705 << buf2 << ' ' << buf3 << std::endl;
708 char buf1[11], buf2[9], buf3[5];
709 if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
710 "ddd MMM dd", buf1, 11) &&
711 GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
712 "HH':'mm':'ss", buf2, 9) &&
713 GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
715 os << "%%CreationDate: " << buf1 << ' '
716 << buf2 << ' ' << buf3 << std::endl;
722 if (_autoArcWidthScale) {
724 for(ArcIt e(g);e!=INVALID;++e)
725 max_w=std::max(double(_arcWidths[e]),max_w);
727 _arcWidthScale/=max_w;
731 if (_autoNodeScale) {
733 for(NodeIt n(g);n!=INVALID;++n)
734 max_s=std::max(double(_nodeSizes[n]),max_s);
741 if(!(_absoluteNodeSizes&&_absoluteArcWidths)) {
742 dim2::Box<double> bb;
743 for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]);
745 bb = dim2::Box<double>(dim2::Point<double>(0,0));
747 diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare());
748 if(diag_len<EPSILON) diag_len = 1;
749 if(!_absoluteNodeSizes) _nodeScale*=diag_len;
750 if(!_absoluteArcWidths) _arcWidthScale*=diag_len;
753 dim2::Box<double> bb;
754 for(NodeIt n(g);n!=INVALID;++n) {
755 double ns=_nodeSizes[n]*_nodeScale;
756 dim2::Point<double> p(ns,ns);
757 switch(_nodeShapes[n]) {
761 bb.add(p+mycoords[n]);
762 bb.add(-p+mycoords[n]);
765 bb.add(-p+mycoords[n]);
766 bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]);
769 bb.add(p+mycoords[n]);
770 bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]);
775 bb = dim2::Box<double>(dim2::Point<double>(0,0));
779 os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
782 //Rescale so that BoundingBox won't be neither to big nor too small.
783 while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10;
784 while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10;
787 os << "%%BoundingBox: "
788 << int(floor(bb.left() * _scale - _xBorder)) << ' '
789 << int(floor(bb.bottom() * _scale - _yBorder)) << ' '
790 << int(ceil(bb.right() * _scale + _xBorder)) << ' '
791 << int(ceil(bb.top() * _scale + _yBorder)) << '\n';
794 os << "%%EndComments\n";
796 //x1 y1 x2 y2 x3 y3 cr cg cb w
797 os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
798 << " 4 2 roll 1 index 1 index curveto stroke } bind def\n";
799 os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke }"
802 os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath }"
805 os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
806 << " 2 index 1 index sub 2 index 2 index add lineto\n"
807 << " 2 index 1 index sub 2 index 2 index sub lineto\n"
808 << " 2 index 1 index add 2 index 2 index sub lineto\n"
809 << " closepath pop pop pop} bind def\n";
811 os << "/di { newpath 2 index 1 index add 2 index moveto\n"
812 << " 2 index 2 index 2 index add lineto\n"
813 << " 2 index 1 index sub 2 index lineto\n"
814 << " 2 index 2 index 2 index sub lineto\n"
815 << " closepath pop pop pop} bind def\n";
817 os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
818 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
820 os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
821 << " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
823 os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
824 << " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
826 os << "/nfemale { 0 0 0 setrgbcolor 3 index "
827 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
828 << " 1.5 mul mul setlinewidth\n"
829 << " newpath 5 index 5 index moveto "
830 << "5 index 5 index 5 index 3.01 mul sub\n"
831 << " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub"
833 << " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto "
835 << " 5 index 5 index 5 index c fill\n"
836 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
839 << " 0 0 0 setrgbcolor 3 index "
840 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
841 <<" 1.5 mul mul setlinewidth\n"
842 << " newpath 5 index 5 index moveto\n"
843 << " 5 index 4 index 1 mul 1.5 mul add\n"
844 << " 5 index 5 index 3 sqrt 1.5 mul mul add\n"
845 << " 1 index 1 index lineto\n"
846 << " 1 index 1 index 7 index sub moveto\n"
847 << " 1 index 1 index lineto\n"
848 << " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub"
851 << " 5 index 5 index 5 index c fill\n"
852 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
856 os << "/arrl " << _arrowLength << " def\n";
857 os << "/arrw " << _arrowWidth << " def\n";
859 os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
860 //len w dx_norm dy_norm x1 y1 cr cg cb
861 os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx "
863 << " /w exch def /len exch def\n"
864 //<< "0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
865 << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
866 << " len w sub arrl sub dx dy lrl\n"
867 << " arrw dy dx neg lrl\n"
868 << " dx arrl w add mul dy w 2 div arrw add mul sub\n"
869 << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
870 << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
871 << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
872 << " arrw dy dx neg lrl\n"
873 << " len w sub arrl sub neg dx dy lrl\n"
874 << " closepath fill } bind def\n";
875 os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
876 << " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
880 if(bb.height()>bb.width()) {
881 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(),
882 (A4WIDTH-2*A4BORDER)/bb.width());
883 os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
884 << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER
886 << sc << " dup scale\n"
887 << -bb.left() << ' ' << -bb.bottom() << " translate\n";
890 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(),
891 (A4WIDTH-2*A4BORDER)/bb.height());
892 os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
893 << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER
895 << sc << " dup scale\n90 rotate\n"
896 << -bb.left() << ' ' << -bb.top() << " translate\n";
898 else if(_scale!=1.0) os << _scale << " dup scale\n";
901 os << "%Arcs:\ngsave\n";
902 if(_enableParallel) {
904 for(ArcIt e(g);e!=INVALID;++e)
905 if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
906 &&g.source(e)!=g.target(e))
908 std::sort(el.begin(),el.end(),arcLess(g));
910 typename std::vector<Arc>::iterator j;
911 for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) {
912 for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
915 for(typename std::vector<Arc>::iterator e=i;e!=j;++e)
916 sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist;
920 dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]);
921 double l=std::sqrt(dvec.normSquare());
922 dim2::Point<double> d(dvec/std::max(l,EPSILON));
923 dim2::Point<double> m;
924 // m=dim2::Point<double>(mycoords[g.target(*i)]+
925 // mycoords[g.source(*i)])/2.0;
927 // m=dim2::Point<double>(mycoords[g.source(*i)])+
928 // dvec*(double(_nodeSizes[g.source(*i)])/
929 // (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
931 m=dim2::Point<double>(mycoords[g.source(*i)])+
932 d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
934 for(typename std::vector<Arc>::iterator e=i;e!=j;++e) {
935 sw+=_arcWidths[*e]*_arcWidthScale/2.0;
936 dim2::Point<double> mm=m+rot90(d)*sw/.75;
939 dim2::Point<double> s=mycoords[g.source(*e)];
940 dim2::Point<double> t=mycoords[g.target(*e)];
941 double rn=_nodeSizes[g.target(*e)]*_nodeScale;
942 node_shape=_nodeShapes[g.target(*e)];
943 dim2::Bezier3 bez(s,mm,mm,t);
945 for(int ii=0;ii<INTERPOL_PREC;++ii)
946 if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
948 dim2::Point<double> apoint=bez((t1+t2)/2);
949 rn = _arrowLength+_arcWidths[*e]*_arcWidthScale;
952 for(int ii=0;ii<INTERPOL_PREC;++ii)
953 if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
955 dim2::Point<double> linend=bez((t1+t2)/2);
956 bez=bez.before((t1+t2)/2);
957 // rn=_nodeSizes[g.source(*e)]*_nodeScale;
958 // node_shape=_nodeShapes[g.source(*e)];
960 // for(int i=0;i<INTERPOL_PREC;++i)
961 // if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape))
963 // else t2=(t1+t2)/2;
964 // bez=bez.after((t1+t2)/2);
965 os << _arcWidths[*e]*_arcWidthScale << " setlinewidth "
966 << _arcColors[*e].red() << ' '
967 << _arcColors[*e].green() << ' '
968 << _arcColors[*e].blue() << " setrgbcolor newpath\n"
969 << bez.p1.x << ' ' << bez.p1.y << " moveto\n"
970 << bez.p2.x << ' ' << bez.p2.y << ' '
971 << bez.p3.x << ' ' << bez.p3.y << ' '
972 << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
973 dim2::Point<double> dd(rot90(linend-apoint));
974 dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/
975 std::sqrt(dd.normSquare());
976 os << "newpath " << psOut(apoint) << " moveto "
977 << psOut(linend+dd) << " lineto "
978 << psOut(linend-dd) << " lineto closepath fill\n";
981 os << mycoords[g.source(*e)].x << ' '
982 << mycoords[g.source(*e)].y << ' '
983 << mm.x << ' ' << mm.y << ' '
984 << mycoords[g.target(*e)].x << ' '
985 << mycoords[g.target(*e)].y << ' '
986 << _arcColors[*e].red() << ' '
987 << _arcColors[*e].green() << ' '
988 << _arcColors[*e].blue() << ' '
989 << _arcWidths[*e]*_arcWidthScale << " lb\n";
991 sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist;
995 else for(ArcIt e(g);e!=INVALID;++e)
996 if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
997 &&g.source(e)!=g.target(e)) {
999 dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]);
1000 double rn=_nodeSizes[g.target(e)]*_nodeScale;
1001 int node_shape=_nodeShapes[g.target(e)];
1003 for(int i=0;i<INTERPOL_PREC;++i)
1004 if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
1006 double l=std::sqrt(d.normSquare());
1009 os << l*(1-(t1+t2)/2) << ' '
1010 << _arcWidths[e]*_arcWidthScale << ' '
1011 << d.x << ' ' << d.y << ' '
1012 << mycoords[g.source(e)].x << ' '
1013 << mycoords[g.source(e)].y << ' '
1014 << _arcColors[e].red() << ' '
1015 << _arcColors[e].green() << ' '
1016 << _arcColors[e].blue() << " arr\n";
1018 else os << mycoords[g.source(e)].x << ' '
1019 << mycoords[g.source(e)].y << ' '
1020 << mycoords[g.target(e)].x << ' '
1021 << mycoords[g.target(e)].y << ' '
1022 << _arcColors[e].red() << ' '
1023 << _arcColors[e].green() << ' '
1024 << _arcColors[e].blue() << ' '
1025 << _arcWidths[e]*_arcWidthScale << " l\n";
1030 os << "%Nodes:\ngsave\n";
1031 for(NodeIt n(g);n!=INVALID;++n) {
1032 os << mycoords[n].x << ' ' << mycoords[n].y << ' '
1033 << _nodeSizes[n]*_nodeScale << ' '
1034 << _nodeColors[n].red() << ' '
1035 << _nodeColors[n].green() << ' '
1036 << _nodeColors[n].blue() << ' ';
1037 switch(_nodeShapes[n]) {
1047 os<< "nfemale";break;
1054 os << "%Node texts:\ngsave\n";
1055 os << "/fosi " << _nodeTextSize << " def\n";
1056 os << "(Helvetica) findfont fosi scalefont setfont\n";
1057 for(NodeIt n(g);n!=INVALID;++n) {
1058 switch(_nodeTextColorType) {
1060 os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n";
1063 os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n";
1066 os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n";
1069 os << "0 0 0 setrgbcolor\n";
1071 os << mycoords[n].x << ' ' << mycoords[n].y
1072 << " (" << _nodeTexts[n] << ") cshow\n";
1076 if(_showNodePsText) {
1077 os << "%Node PS blocks:\ngsave\n";
1078 for(NodeIt n(g);n!=INVALID;++n)
1079 os << mycoords[n].x << ' ' << mycoords[n].y
1080 << " moveto\n" << _nodePsTexts[n] << "\n";
1084 os << "grestore\nshowpage\n";
1087 if(_pleaseRemoveOsStream) {delete &os;}
1091 ///These are just some aliases to other parameter setting functions.
1095 ///An alias for arcWidths()
1096 template<class X> GraphToEps<ArcWidthsTraits<X> > edgeWidths(const X &x)
1098 return arcWidths(x);
1101 ///An alias for arcColors()
1102 template<class X> GraphToEps<ArcColorsTraits<X> >
1103 edgeColors(const X &x)
1105 return arcColors(x);
1108 ///An alias for arcWidthScale()
1109 GraphToEps<T> &edgeWidthScale(double d) {return arcWidthScale(d);}
1111 ///An alias for autoArcWidthScale()
1112 GraphToEps<T> &autoEdgeWidthScale(bool b=true)
1114 return autoArcWidthScale(b);
1117 ///An alias for absoluteArcWidths()
1118 GraphToEps<T> &absoluteEdgeWidths(bool b=true)
1120 return absoluteArcWidths(b);
1123 ///An alias for parArcDist()
1124 GraphToEps<T> &parEdgeDist(double d) {return parArcDist(d);}
1126 ///An alias for hideArcs()
1127 GraphToEps<T> &hideEdges(bool b=true) {return hideArcs(b);}
1133 const int GraphToEps<T>::INTERPOL_PREC = 20;
1135 const double GraphToEps<T>::A4HEIGHT = 841.8897637795276;
1137 const double GraphToEps<T>::A4WIDTH = 595.275590551181;
1139 const double GraphToEps<T>::A4BORDER = 15;
1142 ///Generates an EPS file from a graph
1145 ///Generates an EPS file from a graph.
1146 ///\param g Reference to the graph to be printed.
1147 ///\param os Reference to the output stream.
1148 ///By default it is <tt>std::cout</tt>.
1150 ///This function also has a lot of
1151 ///\ref named-templ-func-param "named parameters",
1152 ///they are declared as the members of class \ref GraphToEps. The following
1153 ///example shows how to use these parameters.
1155 /// graphToEps(g,os).scale(10).coords(coords)
1156 /// .nodeScale(2).nodeSizes(sizes)
1157 /// .arcWidthScale(.4).run();
1160 ///For more detailed examples see the \ref graph_to_eps_demo.cc demo file.
1162 ///\warning Don't forget to put the \ref GraphToEps::run() "run()"
1163 ///to the end of the parameter list.
1165 ///\sa graphToEps(G &g, const char *file_name)
1167 GraphToEps<DefaultGraphToEpsTraits<G> >
1168 graphToEps(G &g, std::ostream& os=std::cout)
1171 GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
1174 ///Generates an EPS file from a graph
1177 ///This function does the same as
1178 ///\ref graphToEps(G &g,std::ostream& os)
1179 ///but it writes its output into the file \c file_name
1180 ///instead of a stream.
1181 ///\sa graphToEps(G &g, std::ostream& os)
1183 GraphToEps<DefaultGraphToEpsTraits<G> >
1184 graphToEps(G &g,const char *file_name)
1186 std::ostream* os = new std::ofstream(file_name);
1189 throw IoError("Cannot write file", file_name);
1191 return GraphToEps<DefaultGraphToEpsTraits<G> >
1192 (DefaultGraphToEpsTraits<G>(g,*os,true));
1195 ///Generates an EPS file from a graph
1198 ///This function does the same as
1199 ///\ref graphToEps(G &g,std::ostream& os)
1200 ///but it writes its output into the file \c file_name
1201 ///instead of a stream.
1202 ///\sa graphToEps(G &g, std::ostream& os)
1204 GraphToEps<DefaultGraphToEpsTraits<G> >
1205 graphToEps(G &g,const std::string& file_name)
1207 std::ostream* os = new std::ofstream(file_name.c_str());
1210 throw IoError("Cannot write file", file_name);
1212 return GraphToEps<DefaultGraphToEpsTraits<G> >
1213 (DefaultGraphToEpsTraits<G>(g,*os,true));
1216 } //END OF NAMESPACE LEMON
1218 #endif // LEMON_GRAPH_TO_EPS_H