1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
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 #include<lemon/bits/windows.h>
35 #include<lemon/math.h>
36 #include<lemon/core.h>
37 #include<lemon/dim2.h>
38 #include<lemon/maps.h>
39 #include<lemon/color.h>
40 #include<lemon/bits/bezier.h>
41 #include<lemon/error.h>
46 ///\brief A well configurable tool for visualizing graphs
50 namespace _graph_to_eps_bits {
54 typedef typename MT::Key Key;
55 typedef typename MT::Value Value;
58 _NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {}
59 Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);}
63 ///Default traits class of GraphToEps
65 ///Default traits class of \ref GraphToEps.
67 ///\param GR is the type of the underlying graph.
69 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 gr Reference to the graph to be printed.
144 ///\param ost Reference to the output stream.
145 ///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 GR &gr, std::ostream& ost = 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<GR>::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;
228 using T::_nodeTextColorType;
229 using T::_nodeTextColors;
231 using T::_autoNodeScale;
232 using T::_autoArcWidthScale;
234 using T::_absoluteNodeSizes;
235 using T::_absoluteArcWidths;
241 // dradnats ++C eht yb deriuqer si ti eveileb t'naC
243 typedef typename T::Graph Graph;
244 typedef typename T::Digraph Digraph;
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
274 ///\image html nodeshape_2.png
275 ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm
278 ///\image html nodeshape_3.png
279 ///\image latex nodeshape_3.eps "MALE shape (3)" width=2cm
282 ///\image html nodeshape_4.png
283 ///\image latex nodeshape_4.eps "FEMALE shape (4)" width=2cm
291 arcLess(const Graph &_g) : g(_g) {}
292 bool operator()(Arc a,Arc b) const
294 Node ai=std::min(g.source(a),g.target(a));
295 Node aa=std::max(g.source(a),g.target(a));
296 Node bi=std::min(g.source(b),g.target(b));
297 Node ba=std::max(g.source(b),g.target(b));
299 (ai==bi && (aa < ba ||
300 (aa==ba && ai==g.source(a) && bi==g.target(b))));
303 bool isParallel(Arc e,Arc f) const
305 return (g.source(e)==g.source(f)&&
306 g.target(e)==g.target(f)) ||
307 (g.source(e)==g.target(f)&&
308 g.target(e)==g.source(f));
311 static std::string psOut(const dim2::Point<TT> &p)
313 std::ostringstream os;
314 os << p.x << ' ' << p.y;
317 static std::string psOut(const Color &c)
319 std::ostringstream os;
320 os << c.red() << ' ' << c.green() << ' ' << c.blue();
325 GraphToEps(const T &t) : T(t), dontPrint(false) {};
327 template<class X> struct CoordsTraits : public T {
328 typedef X CoordsMapType;
330 CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
332 ///Sets the map of the node coordinates
334 ///Sets the map of the node coordinates.
335 ///\param x must be a node map with \ref dim2::Point "dim2::Point<double>" or
336 ///\ref dim2::Point "dim2::Point<int>" values.
337 template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
339 return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
341 template<class X> struct NodeSizesTraits : public T {
343 NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
345 ///Sets the map of the node sizes
347 ///Sets the map of the node sizes.
348 ///\param x must be a node map with \c double (or convertible) values.
349 template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
352 return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
354 template<class X> struct NodeShapesTraits : public T {
355 const X &_nodeShapes;
356 NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
358 ///Sets the map of the node shapes
360 ///Sets the map of the node shapes.
361 ///The available shape values
362 ///can be found in \ref NodeShapes "enum NodeShapes".
363 ///\param x must be a node map with \c int (or convertible) values.
365 template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
368 return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
370 template<class X> struct NodeTextsTraits : public T {
372 NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
374 ///Sets the text printed on the nodes
376 ///Sets the text printed on the nodes.
377 ///\param x must be a node map with type that can be pushed to a standard
379 template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
383 return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
385 template<class X> struct NodePsTextsTraits : public T {
386 const X &_nodePsTexts;
387 NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
389 ///Inserts a PostScript block to the nodes
391 ///With this command it is possible to insert a verbatim PostScript
392 ///block to the nodes.
393 ///The PS current point will be moved to the center of the node before
394 ///the PostScript block inserted.
396 ///Before and after the block a newline character is inserted so you
397 ///don't have to bother with the separators.
399 ///\param x must be a node map with type that can be pushed to a standard
402 ///\sa nodePsTextsPreamble()
403 template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
406 _showNodePsText=true;
407 return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
409 template<class X> struct ArcWidthsTraits : public T {
411 ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {}
413 ///Sets the map of the arc widths
415 ///Sets the map of the arc widths.
416 ///\param x must be an arc map with \c double (or convertible) values.
417 template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x)
420 return GraphToEps<ArcWidthsTraits<X> >(ArcWidthsTraits<X>(*this,x));
423 template<class X> struct NodeColorsTraits : public T {
424 const X &_nodeColors;
425 NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
427 ///Sets the map of the node colors
429 ///Sets the map of the node colors.
430 ///\param x must be a node map with \ref Color values.
433 template<class X> GraphToEps<NodeColorsTraits<X> >
434 nodeColors(const X &x)
437 return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
439 template<class X> struct NodeTextColorsTraits : public T {
440 const X &_nodeTextColors;
441 NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {}
443 ///Sets the map of the node text colors
445 ///Sets the map of the node text colors.
446 ///\param x must be a node map with \ref Color values.
449 template<class X> GraphToEps<NodeTextColorsTraits<X> >
450 nodeTextColors(const X &x)
453 _nodeTextColorType=CUST_COL;
454 return GraphToEps<NodeTextColorsTraits<X> >
455 (NodeTextColorsTraits<X>(*this,x));
457 template<class X> struct ArcColorsTraits : public T {
459 ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {}
461 ///Sets the map of the arc colors
463 ///Sets the map of the arc colors.
464 ///\param x must be an arc map with \ref Color values.
467 template<class X> GraphToEps<ArcColorsTraits<X> >
468 arcColors(const X &x)
471 return GraphToEps<ArcColorsTraits<X> >(ArcColorsTraits<X>(*this,x));
473 ///Sets a global scale factor for node sizes
475 ///Sets a global scale factor for node sizes.
477 /// If nodeSizes() is not given, this function simply sets the node
478 /// sizes to \c d. If nodeSizes() is given, but
479 /// autoNodeScale() is not, then the node size given by
480 /// nodeSizes() will be multiplied by the value \c d.
481 /// If both nodeSizes() and autoNodeScale() are used, then the
482 /// node sizes will be scaled in such a way that the greatest size will be
485 /// \sa autoNodeScale()
486 GraphToEps<T> &nodeScale(double d=.01) {_nodeScale=d;return *this;}
487 ///Turns on/off the automatic node size scaling.
489 ///Turns on/off the automatic node size scaling.
493 GraphToEps<T> &autoNodeScale(bool b=true) {
494 _autoNodeScale=b;return *this;
497 ///Turns on/off the absolutematic node size scaling.
499 ///Turns on/off the absolutematic node size scaling.
503 GraphToEps<T> &absoluteNodeSizes(bool b=true) {
504 _absoluteNodeSizes=b;return *this;
507 ///Negates the Y coordinates.
508 GraphToEps<T> &negateY(bool b=true) {
509 _negY=b;return *this;
512 ///Turn on/off pre-scaling
514 ///By default, graphToEps() rescales the whole image in order to avoid
515 ///very big or very small bounding boxes.
517 ///This (p)rescaling can be turned off with this function.
519 GraphToEps<T> &preScale(bool b=true) {
520 _preScale=b;return *this;
523 ///Sets a global scale factor for arc widths
525 /// Sets a global scale factor for arc widths.
527 /// If arcWidths() is not given, this function simply sets the arc
528 /// widths to \c d. If arcWidths() is given, but
529 /// autoArcWidthScale() is not, then the arc withs given by
530 /// arcWidths() will be multiplied by the value \c d.
531 /// If both arcWidths() and autoArcWidthScale() are used, then the
532 /// arc withs will be scaled in such a way that the greatest width will be
534 GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;}
535 ///Turns on/off the automatic arc width scaling.
537 ///Turns on/off the automatic arc width scaling.
539 ///\sa arcWidthScale()
541 GraphToEps<T> &autoArcWidthScale(bool b=true) {
542 _autoArcWidthScale=b;return *this;
544 ///Turns on/off the absolutematic arc width scaling.
546 ///Turns on/off the absolutematic arc width scaling.
548 ///\sa arcWidthScale()
550 GraphToEps<T> &absoluteArcWidths(bool b=true) {
551 _absoluteArcWidths=b;return *this;
553 ///Sets a global scale factor for the whole picture
554 GraphToEps<T> &scale(double d) {_scale=d;return *this;}
555 ///Sets the width of the border around the picture
556 GraphToEps<T> &border(double b=10) {_xBorder=_yBorder=b;return *this;}
557 ///Sets the width of the border around the picture
558 GraphToEps<T> &border(double x, double y) {
559 _xBorder=x;_yBorder=y;return *this;
561 ///Sets whether to draw arrows
562 GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
563 ///Sets the length of the arrowheads
564 GraphToEps<T> &arrowLength(double d=1.0) {_arrowLength*=d;return *this;}
565 ///Sets the width of the arrowheads
566 GraphToEps<T> &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;}
568 ///Scales the drawing to fit to A4 page
569 GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
571 ///Enables parallel arcs
572 GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
574 ///Sets the distance between parallel arcs
575 GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;}
578 GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;}
580 GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
582 ///Sets the size of the node texts
583 GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
585 ///Sets the color of the node texts to be different from the node color
587 ///Sets the color of the node texts to be as different from the node color
588 ///as it is possible.
589 GraphToEps<T> &distantColorNodeTexts()
590 {_nodeTextColorType=DIST_COL;return *this;}
591 ///Sets the color of the node texts to be black or white and always visible.
593 ///Sets the color of the node texts to be black or white according to
594 ///which is more different from the node color.
595 GraphToEps<T> &distantBWNodeTexts()
596 {_nodeTextColorType=DIST_BW;return *this;}
598 ///Gives a preamble block for node Postscript block.
600 ///Gives a preamble block for node Postscript block.
603 GraphToEps<T> & nodePsTextsPreamble(const char *str) {
604 _nodePsTextsPreamble=str ;return *this;
606 ///Sets whether the graph is undirected
608 ///Sets whether the graph is undirected.
610 ///This setting is the default for undirected graphs.
613 GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;}
615 ///Sets whether the graph is directed
617 ///Sets whether the graph is directed.
618 ///Use it to show the edges as a pair of directed ones.
620 ///This setting is the default for digraphs.
623 GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;}
627 ///Sets the title of the generated image,
628 ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of
630 GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
631 ///Sets the copyright statement.
633 ///Sets the copyright statement of the generated image,
634 ///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of
636 GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;}
639 bool isInsideNode(dim2::Point<double> p, double r,int t)
645 return p.normSquare()<=r*r;
647 return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
649 return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
659 ///Like other functions using
660 ///\ref named-templ-func-param "named template parameters",
661 ///this function calls the algorithm itself, i.e. in this case
662 ///it draws the graph.
664 const double EPSILON=1e-9;
665 if(dontPrint) return;
667 _graph_to_eps_bits::_NegY<typename T::CoordsMapType>
668 mycoords(_coords,_negY);
670 os << "%!PS-Adobe-2.0 EPSF-2.0\n";
671 if(_title.size()>0) os << "%%Title: " << _title << '\n';
672 if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
673 os << "%%Creator: LEMON, graphToEps()\n";
676 os << "%%CreationDate: ";
679 gettimeofday(&tv, 0);
682 ctime_r(&tv.tv_sec,cbuf);
685 os << bits::getWinFormattedDate();
690 if (_autoArcWidthScale) {
692 for(ArcIt e(g);e!=INVALID;++e)
693 max_w=std::max(double(_arcWidths[e]),max_w);
695 _arcWidthScale/=max_w;
699 if (_autoNodeScale) {
701 for(NodeIt n(g);n!=INVALID;++n)
702 max_s=std::max(double(_nodeSizes[n]),max_s);
709 if(!(_absoluteNodeSizes&&_absoluteArcWidths)) {
710 dim2::Box<double> bb;
711 for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]);
713 bb = dim2::Box<double>(dim2::Point<double>(0,0));
715 diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare());
716 if(diag_len<EPSILON) diag_len = 1;
717 if(!_absoluteNodeSizes) _nodeScale*=diag_len;
718 if(!_absoluteArcWidths) _arcWidthScale*=diag_len;
721 dim2::Box<double> bb;
722 for(NodeIt n(g);n!=INVALID;++n) {
723 double ns=_nodeSizes[n]*_nodeScale;
724 dim2::Point<double> p(ns,ns);
725 switch(_nodeShapes[n]) {
729 bb.add(p+mycoords[n]);
730 bb.add(-p+mycoords[n]);
733 bb.add(-p+mycoords[n]);
734 bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]);
737 bb.add(p+mycoords[n]);
738 bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]);
743 bb = dim2::Box<double>(dim2::Point<double>(0,0));
747 os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
750 //Rescale so that BoundingBox won't be neither to big nor too small.
751 while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10;
752 while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10;
755 os << "%%BoundingBox: "
756 << int(floor(bb.left() * _scale - _xBorder)) << ' '
757 << int(floor(bb.bottom() * _scale - _yBorder)) << ' '
758 << int(ceil(bb.right() * _scale + _xBorder)) << ' '
759 << int(ceil(bb.top() * _scale + _yBorder)) << '\n';
762 os << "%%EndComments\n";
764 //x1 y1 x2 y2 x3 y3 cr cg cb w
765 os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
766 << " 4 2 roll 1 index 1 index curveto stroke } bind def\n";
767 os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke }"
770 os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath }"
773 os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
774 << " 2 index 1 index sub 2 index 2 index add lineto\n"
775 << " 2 index 1 index sub 2 index 2 index sub lineto\n"
776 << " 2 index 1 index add 2 index 2 index sub lineto\n"
777 << " closepath pop pop pop} bind def\n";
779 os << "/di { newpath 2 index 1 index add 2 index moveto\n"
780 << " 2 index 2 index 2 index add lineto\n"
781 << " 2 index 1 index sub 2 index lineto\n"
782 << " 2 index 2 index 2 index sub lineto\n"
783 << " closepath pop pop pop} bind def\n";
785 os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
786 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
788 os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
789 << " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
791 os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
792 << " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
794 os << "/nfemale { 0 0 0 setrgbcolor 3 index "
795 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
796 << " 1.5 mul mul setlinewidth\n"
797 << " newpath 5 index 5 index moveto "
798 << "5 index 5 index 5 index 3.01 mul sub\n"
799 << " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub"
801 << " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto "
803 << " 5 index 5 index 5 index c fill\n"
804 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
807 << " 0 0 0 setrgbcolor 3 index "
808 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
809 <<" 1.5 mul mul setlinewidth\n"
810 << " newpath 5 index 5 index moveto\n"
811 << " 5 index 4 index 1 mul 1.5 mul add\n"
812 << " 5 index 5 index 3 sqrt 1.5 mul mul add\n"
813 << " 1 index 1 index lineto\n"
814 << " 1 index 1 index 7 index sub moveto\n"
815 << " 1 index 1 index lineto\n"
816 << " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub"
819 << " 5 index 5 index 5 index c fill\n"
820 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
824 os << "/arrl " << _arrowLength << " def\n";
825 os << "/arrw " << _arrowWidth << " def\n";
827 os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
828 //len w dx_norm dy_norm x1 y1 cr cg cb
829 os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx "
831 << " /w exch def /len exch def\n"
832 //<< "0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
833 << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
834 << " len w sub arrl sub dx dy lrl\n"
835 << " arrw dy dx neg lrl\n"
836 << " dx arrl w add mul dy w 2 div arrw add mul sub\n"
837 << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
838 << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
839 << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
840 << " arrw dy dx neg lrl\n"
841 << " len w sub arrl sub neg dx dy lrl\n"
842 << " closepath fill } bind def\n";
843 os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
844 << " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
848 if(bb.height()>bb.width()) {
849 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(),
850 (A4WIDTH-2*A4BORDER)/bb.width());
851 os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
852 << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER
854 << sc << " dup scale\n"
855 << -bb.left() << ' ' << -bb.bottom() << " translate\n";
858 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(),
859 (A4WIDTH-2*A4BORDER)/bb.height());
860 os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
861 << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER
863 << sc << " dup scale\n90 rotate\n"
864 << -bb.left() << ' ' << -bb.top() << " translate\n";
866 else if(_scale!=1.0) os << _scale << " dup scale\n";
869 os << "%Arcs:\ngsave\n";
870 if(_enableParallel) {
872 for(ArcIt e(g);e!=INVALID;++e)
873 if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
874 &&g.source(e)!=g.target(e))
876 std::sort(el.begin(),el.end(),arcLess(g));
878 typename std::vector<Arc>::iterator j;
879 for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) {
880 for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
883 for(typename std::vector<Arc>::iterator e=i;e!=j;++e)
884 sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist;
888 dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]);
889 double l=std::sqrt(dvec.normSquare());
890 dim2::Point<double> d(dvec/std::max(l,EPSILON));
891 dim2::Point<double> m;
892 // m=dim2::Point<double>(mycoords[g.target(*i)]+
893 // mycoords[g.source(*i)])/2.0;
895 // m=dim2::Point<double>(mycoords[g.source(*i)])+
896 // dvec*(double(_nodeSizes[g.source(*i)])/
897 // (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
899 m=dim2::Point<double>(mycoords[g.source(*i)])+
900 d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
902 for(typename std::vector<Arc>::iterator e=i;e!=j;++e) {
903 sw+=_arcWidths[*e]*_arcWidthScale/2.0;
904 dim2::Point<double> mm=m+rot90(d)*sw/.75;
907 dim2::Point<double> s=mycoords[g.source(*e)];
908 dim2::Point<double> t=mycoords[g.target(*e)];
909 double rn=_nodeSizes[g.target(*e)]*_nodeScale;
910 node_shape=_nodeShapes[g.target(*e)];
911 dim2::Bezier3 bez(s,mm,mm,t);
913 for(int ii=0;ii<INTERPOL_PREC;++ii)
914 if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
916 dim2::Point<double> apoint=bez((t1+t2)/2);
917 rn = _arrowLength+_arcWidths[*e]*_arcWidthScale;
920 for(int ii=0;ii<INTERPOL_PREC;++ii)
921 if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
923 dim2::Point<double> linend=bez((t1+t2)/2);
924 bez=bez.before((t1+t2)/2);
925 // rn=_nodeSizes[g.source(*e)]*_nodeScale;
926 // node_shape=_nodeShapes[g.source(*e)];
928 // for(int i=0;i<INTERPOL_PREC;++i)
929 // if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape))
931 // else t2=(t1+t2)/2;
932 // bez=bez.after((t1+t2)/2);
933 os << _arcWidths[*e]*_arcWidthScale << " setlinewidth "
934 << _arcColors[*e].red() << ' '
935 << _arcColors[*e].green() << ' '
936 << _arcColors[*e].blue() << " setrgbcolor newpath\n"
937 << bez.p1.x << ' ' << bez.p1.y << " moveto\n"
938 << bez.p2.x << ' ' << bez.p2.y << ' '
939 << bez.p3.x << ' ' << bez.p3.y << ' '
940 << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
941 dim2::Point<double> dd(rot90(linend-apoint));
942 dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/
943 std::sqrt(dd.normSquare());
944 os << "newpath " << psOut(apoint) << " moveto "
945 << psOut(linend+dd) << " lineto "
946 << psOut(linend-dd) << " lineto closepath fill\n";
949 os << mycoords[g.source(*e)].x << ' '
950 << mycoords[g.source(*e)].y << ' '
951 << mm.x << ' ' << mm.y << ' '
952 << mycoords[g.target(*e)].x << ' '
953 << mycoords[g.target(*e)].y << ' '
954 << _arcColors[*e].red() << ' '
955 << _arcColors[*e].green() << ' '
956 << _arcColors[*e].blue() << ' '
957 << _arcWidths[*e]*_arcWidthScale << " lb\n";
959 sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist;
963 else for(ArcIt e(g);e!=INVALID;++e)
964 if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
965 &&g.source(e)!=g.target(e)) {
967 dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]);
968 double rn=_nodeSizes[g.target(e)]*_nodeScale;
969 int node_shape=_nodeShapes[g.target(e)];
971 for(int i=0;i<INTERPOL_PREC;++i)
972 if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
974 double l=std::sqrt(d.normSquare());
977 os << l*(1-(t1+t2)/2) << ' '
978 << _arcWidths[e]*_arcWidthScale << ' '
979 << d.x << ' ' << d.y << ' '
980 << mycoords[g.source(e)].x << ' '
981 << mycoords[g.source(e)].y << ' '
982 << _arcColors[e].red() << ' '
983 << _arcColors[e].green() << ' '
984 << _arcColors[e].blue() << " arr\n";
986 else os << mycoords[g.source(e)].x << ' '
987 << mycoords[g.source(e)].y << ' '
988 << mycoords[g.target(e)].x << ' '
989 << mycoords[g.target(e)].y << ' '
990 << _arcColors[e].red() << ' '
991 << _arcColors[e].green() << ' '
992 << _arcColors[e].blue() << ' '
993 << _arcWidths[e]*_arcWidthScale << " l\n";
998 os << "%Nodes:\ngsave\n";
999 for(NodeIt n(g);n!=INVALID;++n) {
1000 os << mycoords[n].x << ' ' << mycoords[n].y << ' '
1001 << _nodeSizes[n]*_nodeScale << ' '
1002 << _nodeColors[n].red() << ' '
1003 << _nodeColors[n].green() << ' '
1004 << _nodeColors[n].blue() << ' ';
1005 switch(_nodeShapes[n]) {
1015 os<< "nfemale";break;
1022 os << "%Node texts:\ngsave\n";
1023 os << "/fosi " << _nodeTextSize << " def\n";
1024 os << "(Helvetica) findfont fosi scalefont setfont\n";
1025 for(NodeIt n(g);n!=INVALID;++n) {
1026 switch(_nodeTextColorType) {
1028 os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n";
1031 os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n";
1034 os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n";
1037 os << "0 0 0 setrgbcolor\n";
1039 os << mycoords[n].x << ' ' << mycoords[n].y
1040 << " (" << _nodeTexts[n] << ") cshow\n";
1044 if(_showNodePsText) {
1045 os << "%Node PS blocks:\ngsave\n";
1046 for(NodeIt n(g);n!=INVALID;++n)
1047 os << mycoords[n].x << ' ' << mycoords[n].y
1048 << " moveto\n" << _nodePsTexts[n] << "\n";
1052 os << "grestore\nshowpage\n";
1055 if(_pleaseRemoveOsStream) {delete &os;}
1059 ///These are just some aliases to other parameter setting functions.
1063 ///An alias for arcWidths()
1064 template<class X> GraphToEps<ArcWidthsTraits<X> > edgeWidths(const X &x)
1066 return arcWidths(x);
1069 ///An alias for arcColors()
1070 template<class X> GraphToEps<ArcColorsTraits<X> >
1071 edgeColors(const X &x)
1073 return arcColors(x);
1076 ///An alias for arcWidthScale()
1077 GraphToEps<T> &edgeWidthScale(double d) {return arcWidthScale(d);}
1079 ///An alias for autoArcWidthScale()
1080 GraphToEps<T> &autoEdgeWidthScale(bool b=true)
1082 return autoArcWidthScale(b);
1085 ///An alias for absoluteArcWidths()
1086 GraphToEps<T> &absoluteEdgeWidths(bool b=true)
1088 return absoluteArcWidths(b);
1091 ///An alias for parArcDist()
1092 GraphToEps<T> &parEdgeDist(double d) {return parArcDist(d);}
1094 ///An alias for hideArcs()
1095 GraphToEps<T> &hideEdges(bool b=true) {return hideArcs(b);}
1101 const int GraphToEps<T>::INTERPOL_PREC = 20;
1103 const double GraphToEps<T>::A4HEIGHT = 841.8897637795276;
1105 const double GraphToEps<T>::A4WIDTH = 595.275590551181;
1107 const double GraphToEps<T>::A4BORDER = 15;
1110 ///Generates an EPS file from a graph
1113 ///Generates an EPS file from a graph.
1114 ///\param g Reference to the graph to be printed.
1115 ///\param os Reference to the output stream.
1116 ///By default, it is <tt>std::cout</tt>.
1118 ///This function also has a lot of
1119 ///\ref named-templ-func-param "named parameters",
1120 ///they are declared as the members of class \ref GraphToEps. The following
1121 ///example shows how to use these parameters.
1123 /// graphToEps(g,os).scale(10).coords(coords)
1124 /// .nodeScale(2).nodeSizes(sizes)
1125 /// .arcWidthScale(.4).run();
1128 ///For more detailed examples, see the \ref graph_to_eps_demo.cc demo file.
1130 ///\warning Don't forget to put the \ref GraphToEps::run() "run()"
1131 ///to the end of the parameter list.
1133 ///\sa graphToEps(GR &g, const char *file_name)
1135 GraphToEps<DefaultGraphToEpsTraits<GR> >
1136 graphToEps(GR &g, std::ostream& os=std::cout)
1139 GraphToEps<DefaultGraphToEpsTraits<GR> >(DefaultGraphToEpsTraits<GR>(g,os));
1142 ///Generates an EPS file from a graph
1145 ///This function does the same as
1146 ///\ref graphToEps(GR &g,std::ostream& os)
1147 ///but it writes its output into the file \c file_name
1148 ///instead of a stream.
1149 ///\sa graphToEps(GR &g, std::ostream& os)
1151 GraphToEps<DefaultGraphToEpsTraits<GR> >
1152 graphToEps(GR &g,const char *file_name)
1154 std::ostream* os = new std::ofstream(file_name);
1157 throw IoError("Cannot write file", file_name);
1159 return GraphToEps<DefaultGraphToEpsTraits<GR> >
1160 (DefaultGraphToEpsTraits<GR>(g,*os,true));
1163 ///Generates an EPS file from a graph
1166 ///This function does the same as
1167 ///\ref graphToEps(GR &g,std::ostream& os)
1168 ///but it writes its output into the file \c file_name
1169 ///instead of a stream.
1170 ///\sa graphToEps(GR &g, std::ostream& os)
1172 GraphToEps<DefaultGraphToEpsTraits<GR> >
1173 graphToEps(GR &g,const std::string& file_name)
1175 std::ostream* os = new std::ofstream(file_name.c_str());
1178 throw IoError("Cannot write file", file_name);
1180 return GraphToEps<DefaultGraphToEpsTraits<GR> >
1181 (DefaultGraphToEpsTraits<GR>(g,*os,true));
1184 } //END OF NAMESPACE LEMON
1186 #endif // LEMON_GRAPH_TO_EPS_H