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
273 ///\image html nodeshape_2.png
274 ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm
277 ///\image html nodeshape_3.png
278 ///\image latex nodeshape_3.eps "MALE shape (3)" width=2cm
281 ///\image html nodeshape_4.png
282 ///\image latex nodeshape_4.eps "FEMALE shape (4)" width=2cm
290 arcLess(const Graph &_g) : g(_g) {}
291 bool operator()(Arc a,Arc b) const
293 Node ai=std::min(g.source(a),g.target(a));
294 Node aa=std::max(g.source(a),g.target(a));
295 Node bi=std::min(g.source(b),g.target(b));
296 Node ba=std::max(g.source(b),g.target(b));
298 (ai==bi && (aa < ba ||
299 (aa==ba && ai==g.source(a) && bi==g.target(b))));
302 bool isParallel(Arc e,Arc f) const
304 return (g.source(e)==g.source(f)&&
305 g.target(e)==g.target(f)) ||
306 (g.source(e)==g.target(f)&&
307 g.target(e)==g.source(f));
310 static std::string psOut(const dim2::Point<TT> &p)
312 std::ostringstream os;
313 os << p.x << ' ' << p.y;
316 static std::string psOut(const Color &c)
318 std::ostringstream os;
319 os << c.red() << ' ' << c.green() << ' ' << c.blue();
324 GraphToEps(const T &t) : T(t), dontPrint(false) {};
326 template<class X> struct CoordsTraits : public T {
327 typedef X CoordsMapType;
329 CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
331 ///Sets the map of the node coordinates
333 ///Sets the map of the node coordinates.
334 ///\param x must be a node map with \ref dim2::Point "dim2::Point<double>" or
335 ///\ref dim2::Point "dim2::Point<int>" values.
336 template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
338 return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
340 template<class X> struct NodeSizesTraits : public T {
342 NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
344 ///Sets the map of the node sizes
346 ///Sets the map of the node sizes.
347 ///\param x must be a node map with \c double (or convertible) values.
348 template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
351 return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
353 template<class X> struct NodeShapesTraits : public T {
354 const X &_nodeShapes;
355 NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
357 ///Sets the map of the node shapes
359 ///Sets the map of the node shapes.
360 ///The available shape values
361 ///can be found in \ref NodeShapes "enum NodeShapes".
362 ///\param x must be a node map with \c int (or convertible) values.
364 template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
367 return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
369 template<class X> struct NodeTextsTraits : public T {
371 NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
373 ///Sets the text printed on the nodes
375 ///Sets the text printed on the nodes.
376 ///\param x must be a node map with type that can be pushed to a standard
378 template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
382 return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
384 template<class X> struct NodePsTextsTraits : public T {
385 const X &_nodePsTexts;
386 NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
388 ///Inserts a PostScript block to the nodes
390 ///With this command it is possible to insert a verbatim PostScript
391 ///block to the nodes.
392 ///The PS current point will be moved to the center of the node before
393 ///the PostScript block inserted.
395 ///Before and after the block a newline character is inserted so you
396 ///don't have to bother with the separators.
398 ///\param x must be a node map with type that can be pushed to a standard
401 ///\sa nodePsTextsPreamble()
402 template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
405 _showNodePsText=true;
406 return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
408 template<class X> struct ArcWidthsTraits : public T {
410 ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {}
412 ///Sets the map of the arc widths
414 ///Sets the map of the arc widths.
415 ///\param x must be an arc map with \c double (or convertible) values.
416 template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x)
419 return GraphToEps<ArcWidthsTraits<X> >(ArcWidthsTraits<X>(*this,x));
422 template<class X> struct NodeColorsTraits : public T {
423 const X &_nodeColors;
424 NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
426 ///Sets the map of the node colors
428 ///Sets the map of the node colors.
429 ///\param x must be a node map with \ref Color values.
432 template<class X> GraphToEps<NodeColorsTraits<X> >
433 nodeColors(const X &x)
436 return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
438 template<class X> struct NodeTextColorsTraits : public T {
439 const X &_nodeTextColors;
440 NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {}
442 ///Sets the map of the node text colors
444 ///Sets the map of the node text colors.
445 ///\param x must be a node map with \ref Color values.
448 template<class X> GraphToEps<NodeTextColorsTraits<X> >
449 nodeTextColors(const X &x)
452 _nodeTextColorType=CUST_COL;
453 return GraphToEps<NodeTextColorsTraits<X> >
454 (NodeTextColorsTraits<X>(*this,x));
456 template<class X> struct ArcColorsTraits : public T {
458 ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {}
460 ///Sets the map of the arc colors
462 ///Sets the map of the arc colors.
463 ///\param x must be an arc map with \ref Color values.
466 template<class X> GraphToEps<ArcColorsTraits<X> >
467 arcColors(const X &x)
470 return GraphToEps<ArcColorsTraits<X> >(ArcColorsTraits<X>(*this,x));
472 ///Sets a global scale factor for node sizes
474 ///Sets a global scale factor for node sizes.
476 /// If nodeSizes() is not given, this function simply sets the node
477 /// sizes to \c d. If nodeSizes() is given, but
478 /// autoNodeScale() is not, then the node size given by
479 /// nodeSizes() will be multiplied by the value \c d.
480 /// If both nodeSizes() and autoNodeScale() are used, then the
481 /// node sizes will be scaled in such a way that the greatest size will be
484 /// \sa autoNodeScale()
485 GraphToEps<T> &nodeScale(double d=.01) {_nodeScale=d;return *this;}
486 ///Turns on/off the automatic node size scaling.
488 ///Turns on/off the automatic node size scaling.
492 GraphToEps<T> &autoNodeScale(bool b=true) {
493 _autoNodeScale=b;return *this;
496 ///Turns on/off the absolutematic node size scaling.
498 ///Turns on/off the absolutematic node size scaling.
502 GraphToEps<T> &absoluteNodeSizes(bool b=true) {
503 _absoluteNodeSizes=b;return *this;
506 ///Negates the Y coordinates.
507 GraphToEps<T> &negateY(bool b=true) {
508 _negY=b;return *this;
511 ///Turn on/off pre-scaling
513 ///By default graphToEps() rescales the whole image in order to avoid
514 ///very big or very small bounding boxes.
516 ///This (p)rescaling can be turned off with this function.
518 GraphToEps<T> &preScale(bool b=true) {
519 _preScale=b;return *this;
522 ///Sets a global scale factor for arc widths
524 /// Sets a global scale factor for arc widths.
526 /// If arcWidths() is not given, this function simply sets the arc
527 /// widths to \c d. If arcWidths() is given, but
528 /// autoArcWidthScale() is not, then the arc withs given by
529 /// arcWidths() will be multiplied by the value \c d.
530 /// If both arcWidths() and autoArcWidthScale() are used, then the
531 /// arc withs will be scaled in such a way that the greatest width will be
533 GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;}
534 ///Turns on/off the automatic arc width scaling.
536 ///Turns on/off the automatic arc width scaling.
538 ///\sa arcWidthScale()
540 GraphToEps<T> &autoArcWidthScale(bool b=true) {
541 _autoArcWidthScale=b;return *this;
543 ///Turns on/off the absolutematic arc width scaling.
545 ///Turns on/off the absolutematic arc width scaling.
547 ///\sa arcWidthScale()
549 GraphToEps<T> &absoluteArcWidths(bool b=true) {
550 _absoluteArcWidths=b;return *this;
552 ///Sets a global scale factor for the whole picture
553 GraphToEps<T> &scale(double d) {_scale=d;return *this;}
554 ///Sets the width of the border around the picture
555 GraphToEps<T> &border(double b=10) {_xBorder=_yBorder=b;return *this;}
556 ///Sets the width of the border around the picture
557 GraphToEps<T> &border(double x, double y) {
558 _xBorder=x;_yBorder=y;return *this;
560 ///Sets whether to draw arrows
561 GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
562 ///Sets the length of the arrowheads
563 GraphToEps<T> &arrowLength(double d=1.0) {_arrowLength*=d;return *this;}
564 ///Sets the width of the arrowheads
565 GraphToEps<T> &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;}
567 ///Scales the drawing to fit to A4 page
568 GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
570 ///Enables parallel arcs
571 GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
573 ///Sets the distance between parallel arcs
574 GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;}
577 GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;}
579 GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
581 ///Sets the size of the node texts
582 GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
584 ///Sets the color of the node texts to be different from the node color
586 ///Sets the color of the node texts to be as different from the node color
587 ///as it is possible.
588 GraphToEps<T> &distantColorNodeTexts()
589 {_nodeTextColorType=DIST_COL;return *this;}
590 ///Sets the color of the node texts to be black or white and always visible.
592 ///Sets the color of the node texts to be black or white according to
593 ///which is more different from the node color.
594 GraphToEps<T> &distantBWNodeTexts()
595 {_nodeTextColorType=DIST_BW;return *this;}
597 ///Gives a preamble block for node Postscript block.
599 ///Gives a preamble block for node Postscript block.
602 GraphToEps<T> & nodePsTextsPreamble(const char *str) {
603 _nodePsTextsPreamble=str ;return *this;
605 ///Sets whether the graph is undirected
607 ///Sets whether the graph is undirected.
609 ///This setting is the default for undirected graphs.
612 GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;}
614 ///Sets whether the graph is directed
616 ///Sets whether the graph is directed.
617 ///Use it to show the edges as a pair of directed ones.
619 ///This setting is the default for digraphs.
622 GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;}
626 ///Sets the title of the generated image,
627 ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of
629 GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
630 ///Sets the copyright statement.
632 ///Sets the copyright statement of the generated image,
633 ///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of
635 GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;}
638 bool isInsideNode(dim2::Point<double> p, double r,int t)
644 return p.normSquare()<=r*r;
646 return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
648 return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
658 ///Like other functions using
659 ///\ref named-templ-func-param "named template parameters",
660 ///this function calls the algorithm itself, i.e. in this case
661 ///it draws the graph.
663 const double EPSILON=1e-9;
664 if(dontPrint) return;
666 _graph_to_eps_bits::_NegY<typename T::CoordsMapType>
667 mycoords(_coords,_negY);
669 os << "%!PS-Adobe-2.0 EPSF-2.0\n";
670 if(_title.size()>0) os << "%%Title: " << _title << '\n';
671 if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
672 os << "%%Creator: LEMON, graphToEps()\n";
675 os << "%%CreationDate: ";
678 gettimeofday(&tv, 0);
681 ctime_r(&tv.tv_sec,cbuf);
684 os << bits::getWinFormattedDate();
689 if (_autoArcWidthScale) {
691 for(ArcIt e(g);e!=INVALID;++e)
692 max_w=std::max(double(_arcWidths[e]),max_w);
694 _arcWidthScale/=max_w;
698 if (_autoNodeScale) {
700 for(NodeIt n(g);n!=INVALID;++n)
701 max_s=std::max(double(_nodeSizes[n]),max_s);
708 if(!(_absoluteNodeSizes&&_absoluteArcWidths)) {
709 dim2::Box<double> bb;
710 for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]);
712 bb = dim2::Box<double>(dim2::Point<double>(0,0));
714 diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare());
715 if(diag_len<EPSILON) diag_len = 1;
716 if(!_absoluteNodeSizes) _nodeScale*=diag_len;
717 if(!_absoluteArcWidths) _arcWidthScale*=diag_len;
720 dim2::Box<double> bb;
721 for(NodeIt n(g);n!=INVALID;++n) {
722 double ns=_nodeSizes[n]*_nodeScale;
723 dim2::Point<double> p(ns,ns);
724 switch(_nodeShapes[n]) {
728 bb.add(p+mycoords[n]);
729 bb.add(-p+mycoords[n]);
732 bb.add(-p+mycoords[n]);
733 bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]);
736 bb.add(p+mycoords[n]);
737 bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]);
742 bb = dim2::Box<double>(dim2::Point<double>(0,0));
746 os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
749 //Rescale so that BoundingBox won't be neither to big nor too small.
750 while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10;
751 while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10;
754 os << "%%BoundingBox: "
755 << int(floor(bb.left() * _scale - _xBorder)) << ' '
756 << int(floor(bb.bottom() * _scale - _yBorder)) << ' '
757 << int(ceil(bb.right() * _scale + _xBorder)) << ' '
758 << int(ceil(bb.top() * _scale + _yBorder)) << '\n';
761 os << "%%EndComments\n";
763 //x1 y1 x2 y2 x3 y3 cr cg cb w
764 os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
765 << " 4 2 roll 1 index 1 index curveto stroke } bind def\n";
766 os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke }"
769 os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath }"
772 os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
773 << " 2 index 1 index sub 2 index 2 index add lineto\n"
774 << " 2 index 1 index sub 2 index 2 index sub lineto\n"
775 << " 2 index 1 index add 2 index 2 index sub lineto\n"
776 << " closepath pop pop pop} bind def\n";
778 os << "/di { newpath 2 index 1 index add 2 index moveto\n"
779 << " 2 index 2 index 2 index add lineto\n"
780 << " 2 index 1 index sub 2 index lineto\n"
781 << " 2 index 2 index 2 index sub lineto\n"
782 << " closepath pop pop pop} bind def\n";
784 os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
785 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
787 os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
788 << " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
790 os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
791 << " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
793 os << "/nfemale { 0 0 0 setrgbcolor 3 index "
794 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
795 << " 1.5 mul mul setlinewidth\n"
796 << " newpath 5 index 5 index moveto "
797 << "5 index 5 index 5 index 3.01 mul sub\n"
798 << " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub"
800 << " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto "
802 << " 5 index 5 index 5 index c fill\n"
803 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
806 << " 0 0 0 setrgbcolor 3 index "
807 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
808 <<" 1.5 mul mul setlinewidth\n"
809 << " newpath 5 index 5 index moveto\n"
810 << " 5 index 4 index 1 mul 1.5 mul add\n"
811 << " 5 index 5 index 3 sqrt 1.5 mul mul add\n"
812 << " 1 index 1 index lineto\n"
813 << " 1 index 1 index 7 index sub moveto\n"
814 << " 1 index 1 index lineto\n"
815 << " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub"
818 << " 5 index 5 index 5 index c fill\n"
819 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
823 os << "/arrl " << _arrowLength << " def\n";
824 os << "/arrw " << _arrowWidth << " def\n";
826 os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
827 //len w dx_norm dy_norm x1 y1 cr cg cb
828 os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx "
830 << " /w exch def /len exch def\n"
831 //<< "0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
832 << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
833 << " len w sub arrl sub dx dy lrl\n"
834 << " arrw dy dx neg lrl\n"
835 << " dx arrl w add mul dy w 2 div arrw add mul sub\n"
836 << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
837 << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
838 << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
839 << " arrw dy dx neg lrl\n"
840 << " len w sub arrl sub neg dx dy lrl\n"
841 << " closepath fill } bind def\n";
842 os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
843 << " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
847 if(bb.height()>bb.width()) {
848 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(),
849 (A4WIDTH-2*A4BORDER)/bb.width());
850 os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
851 << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER
853 << sc << " dup scale\n"
854 << -bb.left() << ' ' << -bb.bottom() << " translate\n";
857 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(),
858 (A4WIDTH-2*A4BORDER)/bb.height());
859 os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
860 << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER
862 << sc << " dup scale\n90 rotate\n"
863 << -bb.left() << ' ' << -bb.top() << " translate\n";
865 else if(_scale!=1.0) os << _scale << " dup scale\n";
868 os << "%Arcs:\ngsave\n";
869 if(_enableParallel) {
871 for(ArcIt e(g);e!=INVALID;++e)
872 if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
873 &&g.source(e)!=g.target(e))
875 std::sort(el.begin(),el.end(),arcLess(g));
877 typename std::vector<Arc>::iterator j;
878 for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) {
879 for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
882 for(typename std::vector<Arc>::iterator e=i;e!=j;++e)
883 sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist;
887 dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]);
888 double l=std::sqrt(dvec.normSquare());
889 dim2::Point<double> d(dvec/std::max(l,EPSILON));
890 dim2::Point<double> m;
891 // m=dim2::Point<double>(mycoords[g.target(*i)]+
892 // mycoords[g.source(*i)])/2.0;
894 // m=dim2::Point<double>(mycoords[g.source(*i)])+
895 // dvec*(double(_nodeSizes[g.source(*i)])/
896 // (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
898 m=dim2::Point<double>(mycoords[g.source(*i)])+
899 d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
901 for(typename std::vector<Arc>::iterator e=i;e!=j;++e) {
902 sw+=_arcWidths[*e]*_arcWidthScale/2.0;
903 dim2::Point<double> mm=m+rot90(d)*sw/.75;
906 dim2::Point<double> s=mycoords[g.source(*e)];
907 dim2::Point<double> t=mycoords[g.target(*e)];
908 double rn=_nodeSizes[g.target(*e)]*_nodeScale;
909 node_shape=_nodeShapes[g.target(*e)];
910 dim2::Bezier3 bez(s,mm,mm,t);
912 for(int ii=0;ii<INTERPOL_PREC;++ii)
913 if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
915 dim2::Point<double> apoint=bez((t1+t2)/2);
916 rn = _arrowLength+_arcWidths[*e]*_arcWidthScale;
919 for(int ii=0;ii<INTERPOL_PREC;++ii)
920 if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
922 dim2::Point<double> linend=bez((t1+t2)/2);
923 bez=bez.before((t1+t2)/2);
924 // rn=_nodeSizes[g.source(*e)]*_nodeScale;
925 // node_shape=_nodeShapes[g.source(*e)];
927 // for(int i=0;i<INTERPOL_PREC;++i)
928 // if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape))
930 // else t2=(t1+t2)/2;
931 // bez=bez.after((t1+t2)/2);
932 os << _arcWidths[*e]*_arcWidthScale << " setlinewidth "
933 << _arcColors[*e].red() << ' '
934 << _arcColors[*e].green() << ' '
935 << _arcColors[*e].blue() << " setrgbcolor newpath\n"
936 << bez.p1.x << ' ' << bez.p1.y << " moveto\n"
937 << bez.p2.x << ' ' << bez.p2.y << ' '
938 << bez.p3.x << ' ' << bez.p3.y << ' '
939 << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
940 dim2::Point<double> dd(rot90(linend-apoint));
941 dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/
942 std::sqrt(dd.normSquare());
943 os << "newpath " << psOut(apoint) << " moveto "
944 << psOut(linend+dd) << " lineto "
945 << psOut(linend-dd) << " lineto closepath fill\n";
948 os << mycoords[g.source(*e)].x << ' '
949 << mycoords[g.source(*e)].y << ' '
950 << mm.x << ' ' << mm.y << ' '
951 << mycoords[g.target(*e)].x << ' '
952 << mycoords[g.target(*e)].y << ' '
953 << _arcColors[*e].red() << ' '
954 << _arcColors[*e].green() << ' '
955 << _arcColors[*e].blue() << ' '
956 << _arcWidths[*e]*_arcWidthScale << " lb\n";
958 sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist;
962 else for(ArcIt e(g);e!=INVALID;++e)
963 if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
964 &&g.source(e)!=g.target(e)) {
966 dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]);
967 double rn=_nodeSizes[g.target(e)]*_nodeScale;
968 int node_shape=_nodeShapes[g.target(e)];
970 for(int i=0;i<INTERPOL_PREC;++i)
971 if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
973 double l=std::sqrt(d.normSquare());
976 os << l*(1-(t1+t2)/2) << ' '
977 << _arcWidths[e]*_arcWidthScale << ' '
978 << d.x << ' ' << d.y << ' '
979 << mycoords[g.source(e)].x << ' '
980 << mycoords[g.source(e)].y << ' '
981 << _arcColors[e].red() << ' '
982 << _arcColors[e].green() << ' '
983 << _arcColors[e].blue() << " arr\n";
985 else os << mycoords[g.source(e)].x << ' '
986 << mycoords[g.source(e)].y << ' '
987 << mycoords[g.target(e)].x << ' '
988 << mycoords[g.target(e)].y << ' '
989 << _arcColors[e].red() << ' '
990 << _arcColors[e].green() << ' '
991 << _arcColors[e].blue() << ' '
992 << _arcWidths[e]*_arcWidthScale << " l\n";
997 os << "%Nodes:\ngsave\n";
998 for(NodeIt n(g);n!=INVALID;++n) {
999 os << mycoords[n].x << ' ' << mycoords[n].y << ' '
1000 << _nodeSizes[n]*_nodeScale << ' '
1001 << _nodeColors[n].red() << ' '
1002 << _nodeColors[n].green() << ' '
1003 << _nodeColors[n].blue() << ' ';
1004 switch(_nodeShapes[n]) {
1014 os<< "nfemale";break;
1021 os << "%Node texts:\ngsave\n";
1022 os << "/fosi " << _nodeTextSize << " def\n";
1023 os << "(Helvetica) findfont fosi scalefont setfont\n";
1024 for(NodeIt n(g);n!=INVALID;++n) {
1025 switch(_nodeTextColorType) {
1027 os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n";
1030 os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n";
1033 os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n";
1036 os << "0 0 0 setrgbcolor\n";
1038 os << mycoords[n].x << ' ' << mycoords[n].y
1039 << " (" << _nodeTexts[n] << ") cshow\n";
1043 if(_showNodePsText) {
1044 os << "%Node PS blocks:\ngsave\n";
1045 for(NodeIt n(g);n!=INVALID;++n)
1046 os << mycoords[n].x << ' ' << mycoords[n].y
1047 << " moveto\n" << _nodePsTexts[n] << "\n";
1051 os << "grestore\nshowpage\n";
1054 if(_pleaseRemoveOsStream) {delete &os;}
1058 ///These are just some aliases to other parameter setting functions.
1062 ///An alias for arcWidths()
1063 template<class X> GraphToEps<ArcWidthsTraits<X> > edgeWidths(const X &x)
1065 return arcWidths(x);
1068 ///An alias for arcColors()
1069 template<class X> GraphToEps<ArcColorsTraits<X> >
1070 edgeColors(const X &x)
1072 return arcColors(x);
1075 ///An alias for arcWidthScale()
1076 GraphToEps<T> &edgeWidthScale(double d) {return arcWidthScale(d);}
1078 ///An alias for autoArcWidthScale()
1079 GraphToEps<T> &autoEdgeWidthScale(bool b=true)
1081 return autoArcWidthScale(b);
1084 ///An alias for absoluteArcWidths()
1085 GraphToEps<T> &absoluteEdgeWidths(bool b=true)
1087 return absoluteArcWidths(b);
1090 ///An alias for parArcDist()
1091 GraphToEps<T> &parEdgeDist(double d) {return parArcDist(d);}
1093 ///An alias for hideArcs()
1094 GraphToEps<T> &hideEdges(bool b=true) {return hideArcs(b);}
1100 const int GraphToEps<T>::INTERPOL_PREC = 20;
1102 const double GraphToEps<T>::A4HEIGHT = 841.8897637795276;
1104 const double GraphToEps<T>::A4WIDTH = 595.275590551181;
1106 const double GraphToEps<T>::A4BORDER = 15;
1109 ///Generates an EPS file from a graph
1112 ///Generates an EPS file from a graph.
1113 ///\param g Reference to the graph to be printed.
1114 ///\param os Reference to the output stream.
1115 ///By default it is <tt>std::cout</tt>.
1117 ///This function also has a lot of
1118 ///\ref named-templ-func-param "named parameters",
1119 ///they are declared as the members of class \ref GraphToEps. The following
1120 ///example shows how to use these parameters.
1122 /// graphToEps(g,os).scale(10).coords(coords)
1123 /// .nodeScale(2).nodeSizes(sizes)
1124 /// .arcWidthScale(.4).run();
1127 ///For more detailed examples see the \ref graph_to_eps_demo.cc demo file.
1129 ///\warning Don't forget to put the \ref GraphToEps::run() "run()"
1130 ///to the end of the parameter list.
1132 ///\sa graphToEps(GR &g, const char *file_name)
1134 GraphToEps<DefaultGraphToEpsTraits<GR> >
1135 graphToEps(GR &g, std::ostream& os=std::cout)
1138 GraphToEps<DefaultGraphToEpsTraits<GR> >(DefaultGraphToEpsTraits<GR>(g,os));
1141 ///Generates an EPS file from a graph
1144 ///This function does the same as
1145 ///\ref graphToEps(GR &g,std::ostream& os)
1146 ///but it writes its output into the file \c file_name
1147 ///instead of a stream.
1148 ///\sa graphToEps(GR &g, std::ostream& os)
1150 GraphToEps<DefaultGraphToEpsTraits<GR> >
1151 graphToEps(GR &g,const char *file_name)
1153 std::ostream* os = new std::ofstream(file_name);
1156 throw IoError("Cannot write file", file_name);
1158 return GraphToEps<DefaultGraphToEpsTraits<GR> >
1159 (DefaultGraphToEpsTraits<GR>(g,*os,true));
1162 ///Generates an EPS file from a graph
1165 ///This function does the same as
1166 ///\ref graphToEps(GR &g,std::ostream& os)
1167 ///but it writes its output into the file \c file_name
1168 ///instead of a stream.
1169 ///\sa graphToEps(GR &g, std::ostream& os)
1171 GraphToEps<DefaultGraphToEpsTraits<GR> >
1172 graphToEps(GR &g,const std::string& file_name)
1174 std::ostream* os = new std::ofstream(file_name.c_str());
1177 throw IoError("Cannot write file", file_name);
1179 return GraphToEps<DefaultGraphToEpsTraits<GR> >
1180 (DefaultGraphToEpsTraits<GR>(g,*os,true));
1183 } //END OF NAMESPACE LEMON
1185 #endif // LEMON_GRAPH_TO_EPS_H