3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_GRAPH_TO_EPS_H
20 #define LEMON_GRAPH_TO_EPS_H
25 #include <lemon/bits/mingw32_time.h>
36 #include<lemon/math.h>
37 #include<lemon/bits/invalid.h>
38 #include<lemon/dim2.h>
39 #include<lemon/maps.h>
40 #include<lemon/color.h>
41 #include<lemon/bits/bezier.h>
46 ///\brief Simple graph drawer
48 ///\author Alpar Juttner
52 namespace _graph_to_eps_bits {
56 typedef typename MT::Key Key;
57 typedef typename MT::Value Value;
60 _NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {}
61 Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);}
65 ///Default traits class of \ref GraphToEps
67 ///Default traits class of \ref GraphToEps
69 ///\c G is the type of the underlying graph.
71 struct DefaultGraphToEpsTraits
74 typedef typename Graph::Node Node;
75 typedef typename Graph::NodeIt NodeIt;
76 typedef typename Graph::Arc Arc;
77 typedef typename Graph::ArcIt ArcIt;
78 typedef typename Graph::InArcIt InArcIt;
79 typedef typename Graph::OutArcIt OutArcIt;
86 typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType;
87 CoordsMapType _coords;
88 ConstMap<typename Graph::Node,double > _nodeSizes;
89 ConstMap<typename Graph::Node,int > _nodeShapes;
91 ConstMap<typename Graph::Node,Color > _nodeColors;
92 ConstMap<typename Graph::Arc,Color > _arcColors;
94 ConstMap<typename Graph::Arc,double > _arcWidths;
96 double _arcWidthScale;
99 double _xBorder, _yBorder;
101 double _nodeBorderQuotient;
104 double _arrowLength, _arrowWidth;
106 bool _showNodes, _showArcs;
108 bool _enableParallel;
112 ConstMap<typename Graph::Node,bool > _nodeTexts;
113 double _nodeTextSize;
115 bool _showNodePsText;
116 ConstMap<typename Graph::Node,bool > _nodePsTexts;
117 char *_nodePsTextsPreamble;
121 bool _pleaseRemoveOsStream;
126 std::string _copyright;
128 enum NodeTextColorType
129 { DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType;
130 ConstMap<typename Graph::Node,Color > _nodeTextColors;
133 bool _autoArcWidthScale;
135 bool _absoluteNodeSizes;
136 bool _absoluteArcWidths;
144 ///\param _g is a reference to the graph to be printed
145 ///\param _os is a reference to the output stream.
146 ///\param _os is a reference to the output stream.
147 ///\param _pros If it is \c true, then the \c ostream referenced by \c _os
148 ///will be explicitly deallocated by the destructor.
149 ///By default it is <tt>std::cout</tt>
150 DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
153 _coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0),
154 _nodeColors(WHITE), _arcColors(BLACK),
155 _arcWidths(1.0), _arcWidthScale(0.003),
156 _nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0),
157 _nodeBorderQuotient(.1),
158 _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
159 _showNodes(true), _showArcs(true),
160 _enableParallel(false), _parArcDist(1),
161 _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
162 _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
163 _undirected(lemon::UndirectedTagIndicator<G>::value),
164 _pleaseRemoveOsStream(_pros), _scaleToA4(false),
165 _nodeTextColorType(SAME_COL), _nodeTextColors(BLACK),
166 _autoNodeScale(false),
167 _autoArcWidthScale(false),
168 _absoluteNodeSizes(false),
169 _absoluteArcWidths(false),
175 ///Helper class to implement the named parameters of \ref graphToEps()
177 ///Helper class to implement the named parameters of \ref graphToEps()
178 ///\todo Is 'helper class' a good name for this?
180 ///\todo Follow PostScript's DSC.
181 /// Use own dictionary.
182 ///\todo Useful new features.
183 /// - Linestyles: dotted, dashed etc.
184 /// - A second color and percent value for the lines.
185 template<class T> class GraphToEps : public T
187 // Can't believe it is required by the C++ standard
193 using T::_nodeShapes;
194 using T::_nodeColors;
198 using T::_arcWidthScale;
203 using T::_nodeBorderQuotient;
205 using T::_drawArrows;
206 using T::_arrowLength;
207 using T::_arrowWidth;
212 using T::_enableParallel;
213 using T::_parArcDist;
215 using T::_showNodeText;
217 using T::_nodeTextSize;
219 using T::_showNodePsText;
220 using T::_nodePsTexts;
221 using T::_nodePsTextsPreamble;
223 using T::_undirected;
225 using T::_pleaseRemoveOsStream;
232 using T::NodeTextColorType;
236 using T::_nodeTextColorType;
237 using T::_nodeTextColors;
239 using T::_autoNodeScale;
240 using T::_autoArcWidthScale;
242 using T::_absoluteNodeSizes;
243 using T::_absoluteArcWidths;
249 // dradnats ++C eht yb deriuqer si ti eveileb t'naC
251 typedef typename T::Graph Graph;
252 typedef typename Graph::Node Node;
253 typedef typename Graph::NodeIt NodeIt;
254 typedef typename Graph::Arc Arc;
255 typedef typename Graph::ArcIt ArcIt;
256 typedef typename Graph::InArcIt InArcIt;
257 typedef typename Graph::OutArcIt OutArcIt;
259 static const int INTERPOL_PREC;
260 static const double A4HEIGHT;
261 static const double A4WIDTH;
262 static const double A4BORDER;
273 ///\image html nodeshape_0.png
274 ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm
277 ///\image html nodeshape_1.png
278 ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm
282 ///\image html nodeshape_2.png
283 ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm
287 ///\image html nodeshape_3.png
288 ///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm
292 ///\image html nodeshape_4.png
293 ///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm
302 arcLess(const Graph &_g) : g(_g) {}
303 bool operator()(Arc a,Arc b) const
305 Node ai=std::min(g.source(a),g.target(a));
306 Node aa=std::max(g.source(a),g.target(a));
307 Node bi=std::min(g.source(b),g.target(b));
308 Node ba=std::max(g.source(b),g.target(b));
310 (ai==bi && (aa < ba ||
311 (aa==ba && ai==g.source(a) && bi==g.target(b))));
314 bool isParallel(Arc e,Arc f) const
316 return (g.source(e)==g.source(f)&&
317 g.target(e)==g.target(f)) ||
318 (g.source(e)==g.target(f)&&
319 g.target(e)==g.source(f));
322 static std::string psOut(const dim2::Point<TT> &p)
324 std::ostringstream os;
325 os << p.x << ' ' << p.y;
328 static std::string psOut(const Color &c)
330 std::ostringstream os;
331 os << c.red() << ' ' << c.green() << ' ' << c.blue();
336 GraphToEps(const T &t) : T(t), dontPrint(false) {};
338 template<class X> struct CoordsTraits : public T {
339 typedef X CoordsMapType;
341 CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
343 ///Sets the map of the node coordinates
345 ///Sets the map of the node coordinates.
346 ///\param x must be a node map with dim2::Point<double> or
347 ///\ref dim2::Point "dim2::Point<int>" values.
348 template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
350 return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
352 template<class X> struct NodeSizesTraits : public T {
354 NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
356 ///Sets the map of the node sizes
358 ///Sets the map of the node sizes
359 ///\param x must be a node map with \c double (or convertible) values.
360 template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
363 return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
365 template<class X> struct NodeShapesTraits : public T {
366 const X &_nodeShapes;
367 NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
369 ///Sets the map of the node shapes
371 ///Sets the map of the node shapes.
372 ///The availabe shape values
373 ///can be found in \ref NodeShapes "enum NodeShapes".
374 ///\param x must be a node map with \c int (or convertible) values.
376 template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
379 return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
381 template<class X> struct NodeTextsTraits : public T {
383 NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
385 ///Sets the text printed on the nodes
387 ///Sets the text printed on the nodes
388 ///\param x must be a node map with type that can be pushed to a standard
390 template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
394 return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
396 template<class X> struct NodePsTextsTraits : public T {
397 const X &_nodePsTexts;
398 NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
400 ///Inserts a PostScript block to the nodes
402 ///With this command it is possible to insert a verbatim PostScript
403 ///block to the nodes.
404 ///The PS current point will be moved to the centre of the node before
405 ///the PostScript block inserted.
407 ///Before and after the block a newline character is inserted so you
408 ///don't have to bother with the separators.
410 ///\param x must be a node map with type that can be pushed to a standard
413 ///\sa nodePsTextsPreamble()
414 ///\todo Offer the choise not to move to the centre but pass the coordinates
415 ///to the Postscript block inserted.
416 template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
419 _showNodePsText=true;
420 return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
422 template<class X> struct ArcWidthsTraits : public T {
424 ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {}
426 ///Sets the map of the arc widths
428 ///Sets the map of the arc widths
429 ///\param x must be a arc map with \c double (or convertible) values.
430 template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x)
433 return GraphToEps<ArcWidthsTraits<X> >(ArcWidthsTraits<X>(*this,x));
436 template<class X> struct NodeColorsTraits : public T {
437 const X &_nodeColors;
438 NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
440 ///Sets the map of the node colors
442 ///Sets the map of the node colors
443 ///\param x must be a node map with \ref Color values.
446 template<class X> GraphToEps<NodeColorsTraits<X> >
447 nodeColors(const X &x)
450 return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
452 template<class X> struct NodeTextColorsTraits : public T {
453 const X &_nodeTextColors;
454 NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {}
456 ///Sets the map of the node text colors
458 ///Sets the map of the node text colors
459 ///\param x must be a node map with \ref Color values.
462 template<class X> GraphToEps<NodeTextColorsTraits<X> >
463 nodeTextColors(const X &x)
466 _nodeTextColorType=CUST_COL;
467 return GraphToEps<NodeTextColorsTraits<X> >
468 (NodeTextColorsTraits<X>(*this,x));
470 template<class X> struct ArcColorsTraits : public T {
472 ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {}
474 ///Sets the map of the arc colors
476 ///Sets the map of the arc colors
477 ///\param x must be a arc map with \ref Color values.
480 template<class X> GraphToEps<ArcColorsTraits<X> >
481 arcColors(const X &x)
484 return GraphToEps<ArcColorsTraits<X> >(ArcColorsTraits<X>(*this,x));
486 ///Sets a global scale factor for node sizes
488 ///Sets a global scale factor for node sizes.
490 /// If nodeSizes() is not given, this function simply sets the node
491 /// sizes to \c d. If nodeSizes() is given, but
492 /// autoNodeScale() is not, then the node size given by
493 /// nodeSizes() will be multiplied by the value \c d.
494 /// If both nodeSizes() and autoNodeScale() are used, then the
495 /// node sizes will be scaled in such a way that the greatest size will be
498 /// \sa autoNodeScale()
499 GraphToEps<T> &nodeScale(double d=.01) {_nodeScale=d;return *this;}
500 ///Turns on/off the automatic node width scaling.
502 ///Turns on/off the automatic node width scaling.
506 GraphToEps<T> &autoNodeScale(bool b=true) {
507 _autoNodeScale=b;return *this;
510 ///Turns on/off the absolutematic node width scaling.
512 ///Turns on/off the absolutematic node width scaling.
516 GraphToEps<T> &absoluteNodeSizes(bool b=true) {
517 _absoluteNodeSizes=b;return *this;
520 ///Negates the Y coordinates.
522 ///Negates the Y coordinates.
526 GraphToEps<T> &negateY(bool b=true) {
527 _negY=b;return *this;
530 ///Turn on/off prescaling
532 ///By default graphToEps() rescales the whole image in order to avoid
533 ///very big or very small bounding boxes.
535 ///This (p)rescaling can be turned off with this function.
537 GraphToEps<T> &preScale(bool b=true) {
538 _preScale=b;return *this;
541 ///Sets a global scale factor for arc widths
543 /// Sets a global scale factor for arc widths.
545 /// If arcWidths() is not given, this function simply sets the arc
546 /// widths to \c d. If arcWidths() is given, but
547 /// autoArcWidthScale() is not, then the arc withs given by
548 /// arcWidths() will be multiplied by the value \c d.
549 /// If both arcWidths() and autoArcWidthScale() are used, then the
550 /// arc withs will be scaled in such a way that the greatest width will be
552 GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;}
553 ///Turns on/off the automatic arc width scaling.
555 ///Turns on/off the automatic arc width scaling.
557 ///\sa arcWidthScale()
559 GraphToEps<T> &autoArcWidthScale(bool b=true) {
560 _autoArcWidthScale=b;return *this;
562 ///Turns on/off the absolutematic arc width scaling.
564 ///Turns on/off the absolutematic arc width scaling.
566 ///\sa arcWidthScale()
568 GraphToEps<T> &absoluteArcWidths(bool b=true) {
569 _absoluteArcWidths=b;return *this;
571 ///Sets a global scale factor for the whole picture
573 ///Sets a global scale factor for the whole picture
576 GraphToEps<T> &scale(double d) {_scale=d;return *this;}
577 ///Sets the width of the border around the picture
579 ///Sets the width of the border around the picture
581 GraphToEps<T> &border(double b) {_xBorder=_yBorder=b;return *this;}
582 ///Sets the width of the border around the picture
584 ///Sets the width of the border around the picture
586 GraphToEps<T> &border(double x, double y) {
587 _xBorder=x;_yBorder=y;return *this;
589 ///Sets whether to draw arrows
591 ///Sets whether to draw arrows
593 GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
594 ///Sets the length of the arrowheads
596 ///Sets the length of the arrowheads
598 GraphToEps<T> &arrowLength(double d) {_arrowLength*=d;return *this;}
599 ///Sets the width of the arrowheads
601 ///Sets the width of the arrowheads
603 GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;}
605 ///Scales the drawing to fit to A4 page
607 ///Scales the drawing to fit to A4 page
609 GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
611 ///Enables parallel arcs
613 ///Enables parallel arcs
614 GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
620 GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;}
626 GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;}
631 GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
633 ///Sets the size of the node texts
635 ///Sets the size of the node texts
637 GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
639 ///Sets the color of the node texts to be different from the node color
641 ///Sets the color of the node texts to be as different from the node color
644 GraphToEps<T> &distantColorNodeTexts()
645 {_nodeTextColorType=DIST_COL;return *this;}
646 ///Sets the color of the node texts to be black or white and always visible.
648 ///Sets the color of the node texts to be black or white according to
650 ///different from the node color
652 GraphToEps<T> &distantBWNodeTexts()
653 {_nodeTextColorType=DIST_BW;return *this;}
655 ///Gives a preamble block for node Postscript block.
657 ///Gives a preamble block for node Postscript block.
660 GraphToEps<T> & nodePsTextsPreamble(const char *str) {
661 _nodePsTextsPreamble=str ;return *this;
663 ///Sets whether the the graph is undirected
665 ///Sets whether the the graph is undirected
667 GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;}
669 ///Sets whether the the graph is directed
671 ///Sets whether the the graph is directed.
672 ///Use it to show the edges as a pair of directed ones.
673 GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;}
677 ///Sets the title of the generated image,
678 ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of
680 GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
681 ///Sets the copyright statement.
683 ///Sets the copyright statement of the generated image,
684 ///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of
686 ///\todo Multiline copyright notice could be supported.
687 GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;}
690 bool isInsideNode(dim2::Point<double> p, double r,int t)
696 return p.normSquare()<=r*r;
698 return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
700 return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
710 ///Like other functions using
711 ///\ref named-templ-func-param "named template parameters",
712 ///this function calles the algorithm itself, i.e. in this case
713 ///it draws the graph.
715 ///\todo better 'epsilon' would be nice here.
716 const double EPSILON=1e-9;
717 if(dontPrint) return;
719 _graph_to_eps_bits::_NegY<typename T::CoordsMapType>
720 mycoords(_coords,_negY);
722 os << "%!PS-Adobe-2.0 EPSF-2.0\n";
723 if(_title.size()>0) os << "%%Title: " << _title << '\n';
724 if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
725 // << "%%Copyright: XXXX\n"
726 os << "%%Creator: LEMON, graphToEps()\n";
731 gettimeofday(&tv, 0);
732 ctime_r(&tv.tv_sec,cbuf);
733 os << "%%CreationDate: " << cbuf;
736 if (_autoArcWidthScale) {
738 for(ArcIt e(g);e!=INVALID;++e)
739 max_w=std::max(double(_arcWidths[e]),max_w);
740 ///\todo better 'epsilon' would be nice here.
742 _arcWidthScale/=max_w;
746 if (_autoNodeScale) {
748 for(NodeIt n(g);n!=INVALID;++n)
749 max_s=std::max(double(_nodeSizes[n]),max_s);
750 ///\todo better 'epsilon' would be nice here.
757 if(!(_absoluteNodeSizes&&_absoluteArcWidths)) {
758 dim2::BoundingBox<double> bb;
759 for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]);
761 bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0));
763 diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare());
764 if(diag_len<EPSILON) diag_len = 1;
765 if(!_absoluteNodeSizes) _nodeScale*=diag_len;
766 if(!_absoluteArcWidths) _arcWidthScale*=diag_len;
769 dim2::BoundingBox<double> bb;
770 for(NodeIt n(g);n!=INVALID;++n) {
771 double ns=_nodeSizes[n]*_nodeScale;
772 dim2::Point<double> p(ns,ns);
773 switch(_nodeShapes[n]) {
777 bb.add(p+mycoords[n]);
778 bb.add(-p+mycoords[n]);
781 bb.add(-p+mycoords[n]);
782 bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]);
785 bb.add(p+mycoords[n]);
786 bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]);
791 bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0));
795 os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
798 //Rescale so that BoundingBox won't be neither to big nor too small.
799 while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10;
800 while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10;
803 os << "%%BoundingBox: "
804 << int(floor(bb.left() * _scale - _xBorder)) << ' '
805 << int(floor(bb.bottom() * _scale - _yBorder)) << ' '
806 << int(ceil(bb.right() * _scale + _xBorder)) << ' '
807 << int(ceil(bb.top() * _scale + _yBorder)) << '\n';
810 os << "%%EndComments\n";
812 //x1 y1 x2 y2 x3 y3 cr cg cb w
813 os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
814 << " 4 2 roll 1 index 1 index curveto stroke } bind def\n";
815 os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
817 os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n";
819 os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
820 << " 2 index 1 index sub 2 index 2 index add lineto\n"
821 << " 2 index 1 index sub 2 index 2 index sub lineto\n"
822 << " 2 index 1 index add 2 index 2 index sub lineto\n"
823 << " closepath pop pop pop} bind def\n";
825 os << "/di { newpath 2 index 1 index add 2 index moveto\n"
826 << " 2 index 2 index 2 index add lineto\n"
827 << " 2 index 1 index sub 2 index lineto\n"
828 << " 2 index 2 index 2 index sub lineto\n"
829 << " closepath pop pop pop} bind def\n";
831 os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
832 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
834 os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
835 << " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
837 os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
838 << " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
840 os << "/nfemale { 0 0 0 setrgbcolor 3 index "
841 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
842 << " 1.5 mul mul setlinewidth\n"
843 << " newpath 5 index 5 index moveto "
844 << "5 index 5 index 5 index 3.01 mul sub\n"
845 << " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub moveto\n"
846 << " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto stroke\n"
847 << " 5 index 5 index 5 index c fill\n"
848 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
851 << " 0 0 0 setrgbcolor 3 index "
852 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
853 <<" 1.5 mul mul setlinewidth\n"
854 << " newpath 5 index 5 index moveto\n"
855 << " 5 index 4 index 1 mul 1.5 mul add\n"
856 << " 5 index 5 index 3 sqrt 1.5 mul mul add\n"
857 << " 1 index 1 index lineto\n"
858 << " 1 index 1 index 7 index sub moveto\n"
859 << " 1 index 1 index lineto\n"
860 << " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub lineto\n"
862 << " 5 index 5 index 5 index c fill\n"
863 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
867 os << "/arrl " << _arrowLength << " def\n";
868 os << "/arrw " << _arrowWidth << " def\n";
870 os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
871 //len w dx_norm dy_norm x1 y1 cr cg cb
872 os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
873 << " /w exch def /len exch def\n"
874 // << " 0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
875 << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
876 << " len w sub arrl sub dx dy lrl\n"
877 << " arrw dy dx neg lrl\n"
878 << " dx arrl w add mul dy w 2 div arrw add mul sub\n"
879 << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
880 << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
881 << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
882 << " arrw dy dx neg lrl\n"
883 << " len w sub arrl sub neg dx dy lrl\n"
884 << " closepath fill } bind def\n";
885 os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
886 << " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
890 if(bb.height()>bb.width()) {
891 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(),
892 (A4WIDTH-2*A4BORDER)/bb.width());
893 os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
894 << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER
896 << sc << " dup scale\n"
897 << -bb.left() << ' ' << -bb.bottom() << " translate\n";
900 //\todo Verify centering
901 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(),
902 (A4WIDTH-2*A4BORDER)/bb.height());
903 os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
904 << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER
906 << sc << " dup scale\n90 rotate\n"
907 << -bb.left() << ' ' << -bb.top() << " translate\n";
909 else if(_scale!=1.0) os << _scale << " dup scale\n";
912 os << "%Arcs:\ngsave\n";
913 if(_enableParallel) {
915 for(ArcIt e(g);e!=INVALID;++e)
916 if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
917 &&g.source(e)!=g.target(e))
919 std::sort(el.begin(),el.end(),arcLess(g));
921 typename std::vector<Arc>::iterator j;
922 for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) {
923 for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
926 for(typename std::vector<Arc>::iterator e=i;e!=j;++e)
927 sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist;
931 dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]);
932 double l=std::sqrt(dvec.normSquare());
933 ///\todo better 'epsilon' would be nice here.
934 dim2::Point<double> d(dvec/std::max(l,EPSILON));
935 dim2::Point<double> m;
936 // m=dim2::Point<double>(mycoords[g.target(*i)]+mycoords[g.source(*i)])/2.0;
938 // m=dim2::Point<double>(mycoords[g.source(*i)])+
939 // dvec*(double(_nodeSizes[g.source(*i)])/
940 // (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
942 m=dim2::Point<double>(mycoords[g.source(*i)])+
943 d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
945 for(typename std::vector<Arc>::iterator e=i;e!=j;++e) {
946 sw+=_arcWidths[*e]*_arcWidthScale/2.0;
947 dim2::Point<double> mm=m+rot90(d)*sw/.75;
950 dim2::Point<double> s=mycoords[g.source(*e)];
951 dim2::Point<double> t=mycoords[g.target(*e)];
952 double rn=_nodeSizes[g.target(*e)]*_nodeScale;
953 node_shape=_nodeShapes[g.target(*e)];
954 dim2::Bezier3 bez(s,mm,mm,t);
956 for(int ii=0;ii<INTERPOL_PREC;++ii)
957 if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
959 dim2::Point<double> apoint=bez((t1+t2)/2);
960 rn = _arrowLength+_arcWidths[*e]*_arcWidthScale;
963 for(int ii=0;ii<INTERPOL_PREC;++ii)
964 if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
966 dim2::Point<double> linend=bez((t1+t2)/2);
967 bez=bez.before((t1+t2)/2);
968 // rn=_nodeSizes[g.source(*e)]*_nodeScale;
969 // node_shape=_nodeShapes[g.source(*e)];
971 // for(int i=0;i<INTERPOL_PREC;++i)
972 // if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t1=(t1+t2)/2;
973 // else t2=(t1+t2)/2;
974 // bez=bez.after((t1+t2)/2);
975 os << _arcWidths[*e]*_arcWidthScale << " setlinewidth "
976 << _arcColors[*e].red() << ' '
977 << _arcColors[*e].green() << ' '
978 << _arcColors[*e].blue() << " setrgbcolor newpath\n"
979 << bez.p1.x << ' ' << bez.p1.y << " moveto\n"
980 << bez.p2.x << ' ' << bez.p2.y << ' '
981 << bez.p3.x << ' ' << bez.p3.y << ' '
982 << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
983 dim2::Point<double> dd(rot90(linend-apoint));
984 dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/
985 std::sqrt(dd.normSquare());
986 os << "newpath " << psOut(apoint) << " moveto "
987 << psOut(linend+dd) << " lineto "
988 << psOut(linend-dd) << " lineto closepath fill\n";
991 os << mycoords[g.source(*e)].x << ' '
992 << mycoords[g.source(*e)].y << ' '
993 << mm.x << ' ' << mm.y << ' '
994 << mycoords[g.target(*e)].x << ' '
995 << mycoords[g.target(*e)].y << ' '
996 << _arcColors[*e].red() << ' '
997 << _arcColors[*e].green() << ' '
998 << _arcColors[*e].blue() << ' '
999 << _arcWidths[*e]*_arcWidthScale << " lb\n";
1001 sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist;
1005 else for(ArcIt e(g);e!=INVALID;++e)
1006 if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
1007 &&g.source(e)!=g.target(e))
1009 dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]);
1010 double rn=_nodeSizes[g.target(e)]*_nodeScale;
1011 int node_shape=_nodeShapes[g.target(e)];
1013 for(int i=0;i<INTERPOL_PREC;++i)
1014 if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
1016 double l=std::sqrt(d.normSquare());
1019 os << l*(1-(t1+t2)/2) << ' '
1020 << _arcWidths[e]*_arcWidthScale << ' '
1021 << d.x << ' ' << d.y << ' '
1022 << mycoords[g.source(e)].x << ' '
1023 << mycoords[g.source(e)].y << ' '
1024 << _arcColors[e].red() << ' '
1025 << _arcColors[e].green() << ' '
1026 << _arcColors[e].blue() << " arr\n";
1028 else os << mycoords[g.source(e)].x << ' '
1029 << mycoords[g.source(e)].y << ' '
1030 << mycoords[g.target(e)].x << ' '
1031 << mycoords[g.target(e)].y << ' '
1032 << _arcColors[e].red() << ' '
1033 << _arcColors[e].green() << ' '
1034 << _arcColors[e].blue() << ' '
1035 << _arcWidths[e]*_arcWidthScale << " l\n";
1039 os << "%Nodes:\ngsave\n";
1040 for(NodeIt n(g);n!=INVALID;++n) {
1041 os << mycoords[n].x << ' ' << mycoords[n].y << ' '
1042 << _nodeSizes[n]*_nodeScale << ' '
1043 << _nodeColors[n].red() << ' '
1044 << _nodeColors[n].green() << ' '
1045 << _nodeColors[n].blue() << ' ';
1046 switch(_nodeShapes[n]) {
1056 os<< "nfemale";break;
1063 os << "%Node texts:\ngsave\n";
1064 os << "/fosi " << _nodeTextSize << " def\n";
1065 os << "(Helvetica) findfont fosi scalefont setfont\n";
1066 for(NodeIt n(g);n!=INVALID;++n) {
1067 switch(_nodeTextColorType) {
1069 os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n";
1072 os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n";
1075 os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n";
1078 os << "0 0 0 setrgbcolor\n";
1080 os << mycoords[n].x << ' ' << mycoords[n].y
1081 << " (" << _nodeTexts[n] << ") cshow\n";
1085 if(_showNodePsText) {
1086 os << "%Node PS blocks:\ngsave\n";
1087 for(NodeIt n(g);n!=INVALID;++n)
1088 os << mycoords[n].x << ' ' << mycoords[n].y
1089 << " moveto\n" << _nodePsTexts[n] << "\n";
1093 os << "grestore\nshowpage\n";
1096 if(_pleaseRemoveOsStream) {delete &os;}
1100 ///These are just some aliases to other parameter setting functions.
1104 ///An alias for arcWidths()
1106 ///An alias for arcWidths()
1108 template<class X> GraphToEps<ArcWidthsTraits<X> > edgeWidths(const X &x)
1110 return arcWidths(x);
1113 ///An alias for arcColors()
1115 ///An alias for arcColors()
1117 template<class X> GraphToEps<ArcColorsTraits<X> >
1118 edgeColors(const X &x)
1120 return arcColors(x);
1123 ///An alias for arcWidthScale()
1125 ///An alias for arcWidthScale()
1127 GraphToEps<T> &edgeWidthScale(double d) {return arcWidthScale(d);}
1129 ///An alias for autoArcWidthScale()
1131 ///An alias for autoArcWidthScale()
1133 GraphToEps<T> &autoEdgeWidthScale(bool b=true)
1135 return autoArcWidthScale(b);
1138 ///An alias for absoluteArcWidths()
1140 ///An alias for absoluteArcWidths()
1142 GraphToEps<T> &absoluteEdgeWidths(bool b=true)
1144 return absoluteArcWidths(b);
1147 ///An alias for parArcDist()
1149 ///An alias for parArcDist()
1151 GraphToEps<T> &parEdgeDist(double d) {return parArcDist(d);}
1153 ///An alias for hideArcs()
1155 ///An alias for hideArcs()
1157 GraphToEps<T> &hideEdges(bool b=true) {return hideArcs(b);}
1163 const int GraphToEps<T>::INTERPOL_PREC = 20;
1165 const double GraphToEps<T>::A4HEIGHT = 841.8897637795276;
1167 const double GraphToEps<T>::A4WIDTH = 595.275590551181;
1169 const double GraphToEps<T>::A4BORDER = 15;
1172 ///Generates an EPS file from a graph
1175 ///Generates an EPS file from a graph.
1176 ///\param g is a reference to the graph to be printed
1177 ///\param os is a reference to the output stream.
1178 ///By default it is <tt>std::cout</tt>
1180 ///This function also has a lot of
1181 ///\ref named-templ-func-param "named parameters",
1182 ///they are declared as the members of class \ref GraphToEps. The following
1183 ///example shows how to use these parameters.
1185 /// graphToEps(g,os).scale(10).coords(coords)
1186 /// .nodeScale(2).nodeSizes(sizes)
1187 /// .arcWidthScale(.4).run();
1189 ///\warning Don't forget to put the \ref GraphToEps::run() "run()"
1190 ///to the end of the parameter list.
1192 ///\sa graphToEps(G &g, const char *file_name)
1194 GraphToEps<DefaultGraphToEpsTraits<G> >
1195 graphToEps(G &g, std::ostream& os=std::cout)
1198 GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
1201 ///Generates an EPS file from a graph
1204 ///This function does the same as
1205 ///\ref graphToEps(G &g,std::ostream& os)
1206 ///but it writes its output into the file \c file_name
1207 ///instead of a stream.
1208 ///\sa graphToEps(G &g, std::ostream& os)
1210 GraphToEps<DefaultGraphToEpsTraits<G> >
1211 graphToEps(G &g,const char *file_name)
1213 return GraphToEps<DefaultGraphToEpsTraits<G> >
1214 (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true));
1217 ///Generates an EPS file from a graph
1220 ///This function does the same as
1221 ///\ref graphToEps(G &g,std::ostream& os)
1222 ///but it writes its output into the file \c file_name
1223 ///instead of a stream.
1224 ///\sa graphToEps(G &g, std::ostream& os)
1226 GraphToEps<DefaultGraphToEpsTraits<G> >
1227 graphToEps(G &g,const std::string& file_name)
1229 return GraphToEps<DefaultGraphToEpsTraits<G> >
1230 (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name.c_str()),true));
1233 } //END OF NAMESPACE LEMON
1235 #endif // LEMON_GRAPH_TO_EPS_H