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 #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
72 typedef typename Graph::Node Node;
73 typedef typename Graph::NodeIt NodeIt;
74 typedef typename Graph::Arc Arc;
75 typedef typename Graph::ArcIt ArcIt;
76 typedef typename Graph::InArcIt InArcIt;
77 typedef typename Graph::OutArcIt OutArcIt;
84 typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType;
85 CoordsMapType _coords;
86 ConstMap<typename Graph::Node,double > _nodeSizes;
87 ConstMap<typename Graph::Node,int > _nodeShapes;
89 ConstMap<typename Graph::Node,Color > _nodeColors;
90 ConstMap<typename Graph::Arc,Color > _arcColors;
92 ConstMap<typename Graph::Arc,double > _arcWidths;
94 double _arcWidthScale;
97 double _xBorder, _yBorder;
99 double _nodeBorderQuotient;
102 double _arrowLength, _arrowWidth;
104 bool _showNodes, _showArcs;
106 bool _enableParallel;
110 ConstMap<typename Graph::Node,bool > _nodeTexts;
111 double _nodeTextSize;
113 bool _showNodePsText;
114 ConstMap<typename Graph::Node,bool > _nodePsTexts;
115 char *_nodePsTextsPreamble;
119 bool _pleaseRemoveOsStream;
124 std::string _copyright;
126 enum NodeTextColorType
127 { DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType;
128 ConstMap<typename Graph::Node,Color > _nodeTextColors;
131 bool _autoArcWidthScale;
133 bool _absoluteNodeSizes;
134 bool _absoluteArcWidths;
142 ///\param gr Reference to the graph to be printed.
143 ///\param ost Reference to the output stream.
144 ///By default it is <tt>std::cout</tt>.
145 ///\param pros If it is \c true, then the \c ostream referenced by \c os
146 ///will be explicitly deallocated by the destructor.
147 DefaultGraphToEpsTraits(const GR &gr, std::ostream& ost = std::cout,
150 _coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0),
151 _nodeColors(WHITE), _arcColors(BLACK),
152 _arcWidths(1.0), _arcWidthScale(0.003),
153 _nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0),
154 _nodeBorderQuotient(.1),
155 _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
156 _showNodes(true), _showArcs(true),
157 _enableParallel(false), _parArcDist(1),
158 _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
159 _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
160 _undirected(lemon::UndirectedTagIndicator<GR>::value),
161 _pleaseRemoveOsStream(pros), _scaleToA4(false),
162 _nodeTextColorType(SAME_COL), _nodeTextColors(BLACK),
163 _autoNodeScale(false),
164 _autoArcWidthScale(false),
165 _absoluteNodeSizes(false),
166 _absoluteArcWidths(false),
172 ///Auxiliary class to implement the named parameters of \ref graphToEps()
174 ///Auxiliary class to implement the named parameters of \ref graphToEps().
176 ///For detailed examples see the \ref graph_to_eps_demo.cc demo file.
177 template<class T> class GraphToEps : public T
179 // Can't believe it is required by the C++ standard
185 using T::_nodeShapes;
186 using T::_nodeColors;
190 using T::_arcWidthScale;
195 using T::_nodeBorderQuotient;
197 using T::_drawArrows;
198 using T::_arrowLength;
199 using T::_arrowWidth;
204 using T::_enableParallel;
205 using T::_parArcDist;
207 using T::_showNodeText;
209 using T::_nodeTextSize;
211 using T::_showNodePsText;
212 using T::_nodePsTexts;
213 using T::_nodePsTextsPreamble;
215 using T::_undirected;
217 using T::_pleaseRemoveOsStream;
224 using T::NodeTextColorType;
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 Graph::Node Node;
245 typedef typename Graph::NodeIt NodeIt;
246 typedef typename Graph::Arc Arc;
247 typedef typename Graph::ArcIt ArcIt;
248 typedef typename Graph::InArcIt InArcIt;
249 typedef typename Graph::OutArcIt OutArcIt;
251 static const int INTERPOL_PREC;
252 static const double A4HEIGHT;
253 static const double A4WIDTH;
254 static const double A4BORDER;
265 ///\image html nodeshape_0.png
266 ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm
269 ///\image html nodeshape_1.png
270 ///\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
279 ///\image html nodeshape_3.png
280 ///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm
284 ///\image html nodeshape_4.png
285 ///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm
294 arcLess(const Graph &_g) : g(_g) {}
295 bool operator()(Arc a,Arc b) const
297 Node ai=std::min(g.source(a),g.target(a));
298 Node aa=std::max(g.source(a),g.target(a));
299 Node bi=std::min(g.source(b),g.target(b));
300 Node ba=std::max(g.source(b),g.target(b));
302 (ai==bi && (aa < ba ||
303 (aa==ba && ai==g.source(a) && bi==g.target(b))));
306 bool isParallel(Arc e,Arc f) const
308 return (g.source(e)==g.source(f)&&
309 g.target(e)==g.target(f)) ||
310 (g.source(e)==g.target(f)&&
311 g.target(e)==g.source(f));
314 static std::string psOut(const dim2::Point<TT> &p)
316 std::ostringstream os;
317 os << p.x << ' ' << p.y;
320 static std::string psOut(const Color &c)
322 std::ostringstream os;
323 os << c.red() << ' ' << c.green() << ' ' << c.blue();
328 GraphToEps(const T &t) : T(t), dontPrint(false) {};
330 template<class X> struct CoordsTraits : public T {
331 typedef X CoordsMapType;
333 CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
335 ///Sets the map of the node coordinates
337 ///Sets the map of the node coordinates.
338 ///\param x must be a node map with \ref dim2::Point "dim2::Point<double>" or
339 ///\ref dim2::Point "dim2::Point<int>" values.
340 template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
342 return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
344 template<class X> struct NodeSizesTraits : public T {
346 NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
348 ///Sets the map of the node sizes
350 ///Sets the map of the node sizes.
351 ///\param x must be a node map with \c double (or convertible) values.
352 template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
355 return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
357 template<class X> struct NodeShapesTraits : public T {
358 const X &_nodeShapes;
359 NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
361 ///Sets the map of the node shapes
363 ///Sets the map of the node shapes.
364 ///The available shape values
365 ///can be found in \ref NodeShapes "enum NodeShapes".
366 ///\param x must be a node map with \c int (or convertible) values.
368 template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
371 return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
373 template<class X> struct NodeTextsTraits : public T {
375 NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
377 ///Sets the text printed on the nodes
379 ///Sets the text printed on the nodes.
380 ///\param x must be a node map with type that can be pushed to a standard
382 template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
386 return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
388 template<class X> struct NodePsTextsTraits : public T {
389 const X &_nodePsTexts;
390 NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
392 ///Inserts a PostScript block to the nodes
394 ///With this command it is possible to insert a verbatim PostScript
395 ///block to the nodes.
396 ///The PS current point will be moved to the center of the node before
397 ///the PostScript block inserted.
399 ///Before and after the block a newline character is inserted so you
400 ///don't have to bother with the separators.
402 ///\param x must be a node map with type that can be pushed to a standard
405 ///\sa nodePsTextsPreamble()
406 template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
409 _showNodePsText=true;
410 return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
412 template<class X> struct ArcWidthsTraits : public T {
414 ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {}
416 ///Sets the map of the arc widths
418 ///Sets the map of the arc widths.
419 ///\param x must be an arc map with \c double (or convertible) values.
420 template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x)
423 return GraphToEps<ArcWidthsTraits<X> >(ArcWidthsTraits<X>(*this,x));
426 template<class X> struct NodeColorsTraits : public T {
427 const X &_nodeColors;
428 NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
430 ///Sets the map of the node colors
432 ///Sets the map of the node colors.
433 ///\param x must be a node map with \ref Color values.
436 template<class X> GraphToEps<NodeColorsTraits<X> >
437 nodeColors(const X &x)
440 return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
442 template<class X> struct NodeTextColorsTraits : public T {
443 const X &_nodeTextColors;
444 NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {}
446 ///Sets the map of the node text colors
448 ///Sets the map of the node text colors.
449 ///\param x must be a node map with \ref Color values.
452 template<class X> GraphToEps<NodeTextColorsTraits<X> >
453 nodeTextColors(const X &x)
456 _nodeTextColorType=CUST_COL;
457 return GraphToEps<NodeTextColorsTraits<X> >
458 (NodeTextColorsTraits<X>(*this,x));
460 template<class X> struct ArcColorsTraits : public T {
462 ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {}
464 ///Sets the map of the arc colors
466 ///Sets the map of the arc colors.
467 ///\param x must be an arc map with \ref Color values.
470 template<class X> GraphToEps<ArcColorsTraits<X> >
471 arcColors(const X &x)
474 return GraphToEps<ArcColorsTraits<X> >(ArcColorsTraits<X>(*this,x));
476 ///Sets a global scale factor for node sizes
478 ///Sets a global scale factor for node sizes.
480 /// If nodeSizes() is not given, this function simply sets the node
481 /// sizes to \c d. If nodeSizes() is given, but
482 /// autoNodeScale() is not, then the node size given by
483 /// nodeSizes() will be multiplied by the value \c d.
484 /// If both nodeSizes() and autoNodeScale() are used, then the
485 /// node sizes will be scaled in such a way that the greatest size will be
488 /// \sa autoNodeScale()
489 GraphToEps<T> &nodeScale(double d=.01) {_nodeScale=d;return *this;}
490 ///Turns on/off the automatic node size scaling.
492 ///Turns on/off the automatic node size scaling.
496 GraphToEps<T> &autoNodeScale(bool b=true) {
497 _autoNodeScale=b;return *this;
500 ///Turns on/off the absolutematic node size scaling.
502 ///Turns on/off the absolutematic node size scaling.
506 GraphToEps<T> &absoluteNodeSizes(bool b=true) {
507 _absoluteNodeSizes=b;return *this;
510 ///Negates the Y coordinates.
511 GraphToEps<T> &negateY(bool b=true) {
512 _negY=b;return *this;
515 ///Turn on/off pre-scaling
517 ///By default graphToEps() rescales the whole image in order to avoid
518 ///very big or very small bounding boxes.
520 ///This (p)rescaling can be turned off with this function.
522 GraphToEps<T> &preScale(bool b=true) {
523 _preScale=b;return *this;
526 ///Sets a global scale factor for arc widths
528 /// Sets a global scale factor for arc widths.
530 /// If arcWidths() is not given, this function simply sets the arc
531 /// widths to \c d. If arcWidths() is given, but
532 /// autoArcWidthScale() is not, then the arc withs given by
533 /// arcWidths() will be multiplied by the value \c d.
534 /// If both arcWidths() and autoArcWidthScale() are used, then the
535 /// arc withs will be scaled in such a way that the greatest width will be
537 GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;}
538 ///Turns on/off the automatic arc width scaling.
540 ///Turns on/off the automatic arc width scaling.
542 ///\sa arcWidthScale()
544 GraphToEps<T> &autoArcWidthScale(bool b=true) {
545 _autoArcWidthScale=b;return *this;
547 ///Turns on/off the absolutematic arc width scaling.
549 ///Turns on/off the absolutematic arc width scaling.
551 ///\sa arcWidthScale()
553 GraphToEps<T> &absoluteArcWidths(bool b=true) {
554 _absoluteArcWidths=b;return *this;
556 ///Sets a global scale factor for the whole picture
557 GraphToEps<T> &scale(double d) {_scale=d;return *this;}
558 ///Sets the width of the border around the picture
559 GraphToEps<T> &border(double b=10) {_xBorder=_yBorder=b;return *this;}
560 ///Sets the width of the border around the picture
561 GraphToEps<T> &border(double x, double y) {
562 _xBorder=x;_yBorder=y;return *this;
564 ///Sets whether to draw arrows
565 GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
566 ///Sets the length of the arrowheads
567 GraphToEps<T> &arrowLength(double d=1.0) {_arrowLength*=d;return *this;}
568 ///Sets the width of the arrowheads
569 GraphToEps<T> &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;}
571 ///Scales the drawing to fit to A4 page
572 GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
574 ///Enables parallel arcs
575 GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
577 ///Sets the distance between parallel arcs
578 GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;}
581 GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;}
583 GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
585 ///Sets the size of the node texts
586 GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
588 ///Sets the color of the node texts to be different from the node color
590 ///Sets the color of the node texts to be as different from the node color
591 ///as it is possible.
592 GraphToEps<T> &distantColorNodeTexts()
593 {_nodeTextColorType=DIST_COL;return *this;}
594 ///Sets the color of the node texts to be black or white and always visible.
596 ///Sets the color of the node texts to be black or white according to
597 ///which is more different from the node color.
598 GraphToEps<T> &distantBWNodeTexts()
599 {_nodeTextColorType=DIST_BW;return *this;}
601 ///Gives a preamble block for node Postscript block.
603 ///Gives a preamble block for node Postscript block.
606 GraphToEps<T> & nodePsTextsPreamble(const char *str) {
607 _nodePsTextsPreamble=str ;return *this;
609 ///Sets whether the graph is undirected
611 ///Sets whether the graph is undirected.
613 ///This setting is the default for undirected graphs.
616 GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;}
618 ///Sets whether the graph is directed
620 ///Sets whether the graph is directed.
621 ///Use it to show the edges as a pair of directed ones.
623 ///This setting is the default for digraphs.
626 GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;}
630 ///Sets the title of the generated image,
631 ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of
633 GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
634 ///Sets the copyright statement.
636 ///Sets the copyright statement of the generated image,
637 ///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of
639 GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;}
642 bool isInsideNode(dim2::Point<double> p, double r,int t)
648 return p.normSquare()<=r*r;
650 return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
652 return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
662 ///Like other functions using
663 ///\ref named-templ-func-param "named template parameters",
664 ///this function calls the algorithm itself, i.e. in this case
665 ///it draws the graph.
667 const double EPSILON=1e-9;
668 if(dontPrint) return;
670 _graph_to_eps_bits::_NegY<typename T::CoordsMapType>
671 mycoords(_coords,_negY);
673 os << "%!PS-Adobe-2.0 EPSF-2.0\n";
674 if(_title.size()>0) os << "%%Title: " << _title << '\n';
675 if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
676 os << "%%Creator: LEMON, graphToEps()\n";
679 os << "%%CreationDate: ";
682 gettimeofday(&tv, 0);
685 ctime_r(&tv.tv_sec,cbuf);
688 os << bits::getWinFormattedDate();
693 if (_autoArcWidthScale) {
695 for(ArcIt e(g);e!=INVALID;++e)
696 max_w=std::max(double(_arcWidths[e]),max_w);
698 _arcWidthScale/=max_w;
702 if (_autoNodeScale) {
704 for(NodeIt n(g);n!=INVALID;++n)
705 max_s=std::max(double(_nodeSizes[n]),max_s);
712 if(!(_absoluteNodeSizes&&_absoluteArcWidths)) {
713 dim2::Box<double> bb;
714 for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]);
716 bb = dim2::Box<double>(dim2::Point<double>(0,0));
718 diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare());
719 if(diag_len<EPSILON) diag_len = 1;
720 if(!_absoluteNodeSizes) _nodeScale*=diag_len;
721 if(!_absoluteArcWidths) _arcWidthScale*=diag_len;
724 dim2::Box<double> bb;
725 for(NodeIt n(g);n!=INVALID;++n) {
726 double ns=_nodeSizes[n]*_nodeScale;
727 dim2::Point<double> p(ns,ns);
728 switch(_nodeShapes[n]) {
732 bb.add(p+mycoords[n]);
733 bb.add(-p+mycoords[n]);
736 bb.add(-p+mycoords[n]);
737 bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]);
740 bb.add(p+mycoords[n]);
741 bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]);
746 bb = dim2::Box<double>(dim2::Point<double>(0,0));
750 os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
753 //Rescale so that BoundingBox won't be neither to big nor too small.
754 while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10;
755 while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10;
758 os << "%%BoundingBox: "
759 << int(floor(bb.left() * _scale - _xBorder)) << ' '
760 << int(floor(bb.bottom() * _scale - _yBorder)) << ' '
761 << int(ceil(bb.right() * _scale + _xBorder)) << ' '
762 << int(ceil(bb.top() * _scale + _yBorder)) << '\n';
765 os << "%%EndComments\n";
767 //x1 y1 x2 y2 x3 y3 cr cg cb w
768 os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
769 << " 4 2 roll 1 index 1 index curveto stroke } bind def\n";
770 os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke }"
773 os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath }"
776 os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
777 << " 2 index 1 index sub 2 index 2 index add lineto\n"
778 << " 2 index 1 index sub 2 index 2 index sub lineto\n"
779 << " 2 index 1 index add 2 index 2 index sub lineto\n"
780 << " closepath pop pop pop} bind def\n";
782 os << "/di { newpath 2 index 1 index add 2 index moveto\n"
783 << " 2 index 2 index 2 index add lineto\n"
784 << " 2 index 1 index sub 2 index lineto\n"
785 << " 2 index 2 index 2 index sub lineto\n"
786 << " closepath pop pop pop} bind def\n";
788 os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
789 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
791 os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
792 << " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
794 os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
795 << " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
797 os << "/nfemale { 0 0 0 setrgbcolor 3 index "
798 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
799 << " 1.5 mul mul setlinewidth\n"
800 << " newpath 5 index 5 index moveto "
801 << "5 index 5 index 5 index 3.01 mul sub\n"
802 << " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub"
804 << " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto "
806 << " 5 index 5 index 5 index c fill\n"
807 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
810 << " 0 0 0 setrgbcolor 3 index "
811 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
812 <<" 1.5 mul mul setlinewidth\n"
813 << " newpath 5 index 5 index moveto\n"
814 << " 5 index 4 index 1 mul 1.5 mul add\n"
815 << " 5 index 5 index 3 sqrt 1.5 mul mul add\n"
816 << " 1 index 1 index lineto\n"
817 << " 1 index 1 index 7 index sub moveto\n"
818 << " 1 index 1 index lineto\n"
819 << " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub"
822 << " 5 index 5 index 5 index c fill\n"
823 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
827 os << "/arrl " << _arrowLength << " def\n";
828 os << "/arrw " << _arrowWidth << " def\n";
830 os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
831 //len w dx_norm dy_norm x1 y1 cr cg cb
832 os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx "
834 << " /w exch def /len exch def\n"
835 //<< "0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
836 << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
837 << " len w sub arrl sub dx dy lrl\n"
838 << " arrw dy dx neg lrl\n"
839 << " dx arrl w add mul dy w 2 div arrw add mul sub\n"
840 << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
841 << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
842 << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
843 << " arrw dy dx neg lrl\n"
844 << " len w sub arrl sub neg dx dy lrl\n"
845 << " closepath fill } bind def\n";
846 os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
847 << " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
851 if(bb.height()>bb.width()) {
852 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(),
853 (A4WIDTH-2*A4BORDER)/bb.width());
854 os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
855 << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER
857 << sc << " dup scale\n"
858 << -bb.left() << ' ' << -bb.bottom() << " translate\n";
861 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(),
862 (A4WIDTH-2*A4BORDER)/bb.height());
863 os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
864 << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER
866 << sc << " dup scale\n90 rotate\n"
867 << -bb.left() << ' ' << -bb.top() << " translate\n";
869 else if(_scale!=1.0) os << _scale << " dup scale\n";
872 os << "%Arcs:\ngsave\n";
873 if(_enableParallel) {
875 for(ArcIt e(g);e!=INVALID;++e)
876 if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
877 &&g.source(e)!=g.target(e))
879 std::sort(el.begin(),el.end(),arcLess(g));
881 typename std::vector<Arc>::iterator j;
882 for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) {
883 for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
886 for(typename std::vector<Arc>::iterator e=i;e!=j;++e)
887 sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist;
891 dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]);
892 double l=std::sqrt(dvec.normSquare());
893 dim2::Point<double> d(dvec/std::max(l,EPSILON));
894 dim2::Point<double> m;
895 // m=dim2::Point<double>(mycoords[g.target(*i)]+
896 // mycoords[g.source(*i)])/2.0;
898 // m=dim2::Point<double>(mycoords[g.source(*i)])+
899 // dvec*(double(_nodeSizes[g.source(*i)])/
900 // (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
902 m=dim2::Point<double>(mycoords[g.source(*i)])+
903 d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
905 for(typename std::vector<Arc>::iterator e=i;e!=j;++e) {
906 sw+=_arcWidths[*e]*_arcWidthScale/2.0;
907 dim2::Point<double> mm=m+rot90(d)*sw/.75;
910 dim2::Point<double> s=mycoords[g.source(*e)];
911 dim2::Point<double> t=mycoords[g.target(*e)];
912 double rn=_nodeSizes[g.target(*e)]*_nodeScale;
913 node_shape=_nodeShapes[g.target(*e)];
914 dim2::Bezier3 bez(s,mm,mm,t);
916 for(int ii=0;ii<INTERPOL_PREC;++ii)
917 if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
919 dim2::Point<double> apoint=bez((t1+t2)/2);
920 rn = _arrowLength+_arcWidths[*e]*_arcWidthScale;
923 for(int ii=0;ii<INTERPOL_PREC;++ii)
924 if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
926 dim2::Point<double> linend=bez((t1+t2)/2);
927 bez=bez.before((t1+t2)/2);
928 // rn=_nodeSizes[g.source(*e)]*_nodeScale;
929 // node_shape=_nodeShapes[g.source(*e)];
931 // for(int i=0;i<INTERPOL_PREC;++i)
932 // if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape))
934 // else t2=(t1+t2)/2;
935 // bez=bez.after((t1+t2)/2);
936 os << _arcWidths[*e]*_arcWidthScale << " setlinewidth "
937 << _arcColors[*e].red() << ' '
938 << _arcColors[*e].green() << ' '
939 << _arcColors[*e].blue() << " setrgbcolor newpath\n"
940 << bez.p1.x << ' ' << bez.p1.y << " moveto\n"
941 << bez.p2.x << ' ' << bez.p2.y << ' '
942 << bez.p3.x << ' ' << bez.p3.y << ' '
943 << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
944 dim2::Point<double> dd(rot90(linend-apoint));
945 dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/
946 std::sqrt(dd.normSquare());
947 os << "newpath " << psOut(apoint) << " moveto "
948 << psOut(linend+dd) << " lineto "
949 << psOut(linend-dd) << " lineto closepath fill\n";
952 os << mycoords[g.source(*e)].x << ' '
953 << mycoords[g.source(*e)].y << ' '
954 << mm.x << ' ' << mm.y << ' '
955 << mycoords[g.target(*e)].x << ' '
956 << mycoords[g.target(*e)].y << ' '
957 << _arcColors[*e].red() << ' '
958 << _arcColors[*e].green() << ' '
959 << _arcColors[*e].blue() << ' '
960 << _arcWidths[*e]*_arcWidthScale << " lb\n";
962 sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist;
966 else for(ArcIt e(g);e!=INVALID;++e)
967 if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
968 &&g.source(e)!=g.target(e)) {
970 dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]);
971 double rn=_nodeSizes[g.target(e)]*_nodeScale;
972 int node_shape=_nodeShapes[g.target(e)];
974 for(int i=0;i<INTERPOL_PREC;++i)
975 if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
977 double l=std::sqrt(d.normSquare());
980 os << l*(1-(t1+t2)/2) << ' '
981 << _arcWidths[e]*_arcWidthScale << ' '
982 << d.x << ' ' << d.y << ' '
983 << mycoords[g.source(e)].x << ' '
984 << mycoords[g.source(e)].y << ' '
985 << _arcColors[e].red() << ' '
986 << _arcColors[e].green() << ' '
987 << _arcColors[e].blue() << " arr\n";
989 else os << mycoords[g.source(e)].x << ' '
990 << mycoords[g.source(e)].y << ' '
991 << mycoords[g.target(e)].x << ' '
992 << mycoords[g.target(e)].y << ' '
993 << _arcColors[e].red() << ' '
994 << _arcColors[e].green() << ' '
995 << _arcColors[e].blue() << ' '
996 << _arcWidths[e]*_arcWidthScale << " l\n";
1001 os << "%Nodes:\ngsave\n";
1002 for(NodeIt n(g);n!=INVALID;++n) {
1003 os << mycoords[n].x << ' ' << mycoords[n].y << ' '
1004 << _nodeSizes[n]*_nodeScale << ' '
1005 << _nodeColors[n].red() << ' '
1006 << _nodeColors[n].green() << ' '
1007 << _nodeColors[n].blue() << ' ';
1008 switch(_nodeShapes[n]) {
1018 os<< "nfemale";break;
1025 os << "%Node texts:\ngsave\n";
1026 os << "/fosi " << _nodeTextSize << " def\n";
1027 os << "(Helvetica) findfont fosi scalefont setfont\n";
1028 for(NodeIt n(g);n!=INVALID;++n) {
1029 switch(_nodeTextColorType) {
1031 os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n";
1034 os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n";
1037 os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n";
1040 os << "0 0 0 setrgbcolor\n";
1042 os << mycoords[n].x << ' ' << mycoords[n].y
1043 << " (" << _nodeTexts[n] << ") cshow\n";
1047 if(_showNodePsText) {
1048 os << "%Node PS blocks:\ngsave\n";
1049 for(NodeIt n(g);n!=INVALID;++n)
1050 os << mycoords[n].x << ' ' << mycoords[n].y
1051 << " moveto\n" << _nodePsTexts[n] << "\n";
1055 os << "grestore\nshowpage\n";
1058 if(_pleaseRemoveOsStream) {delete &os;}
1062 ///These are just some aliases to other parameter setting functions.
1066 ///An alias for arcWidths()
1067 template<class X> GraphToEps<ArcWidthsTraits<X> > edgeWidths(const X &x)
1069 return arcWidths(x);
1072 ///An alias for arcColors()
1073 template<class X> GraphToEps<ArcColorsTraits<X> >
1074 edgeColors(const X &x)
1076 return arcColors(x);
1079 ///An alias for arcWidthScale()
1080 GraphToEps<T> &edgeWidthScale(double d) {return arcWidthScale(d);}
1082 ///An alias for autoArcWidthScale()
1083 GraphToEps<T> &autoEdgeWidthScale(bool b=true)
1085 return autoArcWidthScale(b);
1088 ///An alias for absoluteArcWidths()
1089 GraphToEps<T> &absoluteEdgeWidths(bool b=true)
1091 return absoluteArcWidths(b);
1094 ///An alias for parArcDist()
1095 GraphToEps<T> &parEdgeDist(double d) {return parArcDist(d);}
1097 ///An alias for hideArcs()
1098 GraphToEps<T> &hideEdges(bool b=true) {return hideArcs(b);}
1104 const int GraphToEps<T>::INTERPOL_PREC = 20;
1106 const double GraphToEps<T>::A4HEIGHT = 841.8897637795276;
1108 const double GraphToEps<T>::A4WIDTH = 595.275590551181;
1110 const double GraphToEps<T>::A4BORDER = 15;
1113 ///Generates an EPS file from a graph
1116 ///Generates an EPS file from a graph.
1117 ///\param g Reference to the graph to be printed.
1118 ///\param os Reference to the output stream.
1119 ///By default it is <tt>std::cout</tt>.
1121 ///This function also has a lot of
1122 ///\ref named-templ-func-param "named parameters",
1123 ///they are declared as the members of class \ref GraphToEps. The following
1124 ///example shows how to use these parameters.
1126 /// graphToEps(g,os).scale(10).coords(coords)
1127 /// .nodeScale(2).nodeSizes(sizes)
1128 /// .arcWidthScale(.4).run();
1131 ///For more detailed examples see the \ref graph_to_eps_demo.cc demo file.
1133 ///\warning Don't forget to put the \ref GraphToEps::run() "run()"
1134 ///to the end of the parameter list.
1136 ///\sa graphToEps(GR &g, const char *file_name)
1138 GraphToEps<DefaultGraphToEpsTraits<GR> >
1139 graphToEps(GR &g, std::ostream& os=std::cout)
1142 GraphToEps<DefaultGraphToEpsTraits<GR> >(DefaultGraphToEpsTraits<GR>(g,os));
1145 ///Generates an EPS file from a graph
1148 ///This function does the same as
1149 ///\ref graphToEps(GR &g,std::ostream& os)
1150 ///but it writes its output into the file \c file_name
1151 ///instead of a stream.
1152 ///\sa graphToEps(GR &g, std::ostream& os)
1154 GraphToEps<DefaultGraphToEpsTraits<GR> >
1155 graphToEps(GR &g,const char *file_name)
1157 std::ostream* os = new std::ofstream(file_name);
1160 throw IoError("Cannot write file", file_name);
1162 return GraphToEps<DefaultGraphToEpsTraits<GR> >
1163 (DefaultGraphToEpsTraits<GR>(g,*os,true));
1166 ///Generates an EPS file from a graph
1169 ///This function does the same as
1170 ///\ref graphToEps(GR &g,std::ostream& os)
1171 ///but it writes its output into the file \c file_name
1172 ///instead of a stream.
1173 ///\sa graphToEps(GR &g, std::ostream& os)
1175 GraphToEps<DefaultGraphToEpsTraits<GR> >
1176 graphToEps(GR &g,const std::string& file_name)
1178 std::ostream* os = new std::ofstream(file_name.c_str());
1181 throw IoError("Cannot write file", file_name);
1183 return GraphToEps<DefaultGraphToEpsTraits<GR> >
1184 (DefaultGraphToEpsTraits<GR>(g,*os,true));
1187 } //END OF NAMESPACE LEMON
1189 #endif // LEMON_GRAPH_TO_EPS_H