Better handling of inexact computation.
We do not use tolerance for excess, just for edges
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
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>
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
55 typedef typename MT::Key Key;
56 typedef typename MT::Value Value;
59 _NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {}
60 Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);}
63 ///Default traits class of \ref GraphToEps
65 ///Default traits class of \ref GraphToEps
67 ///\c G 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::Edge Edge;
75 typedef typename Graph::EdgeIt EdgeIt;
76 typedef typename Graph::InEdgeIt InEdgeIt;
77 typedef typename Graph::OutEdgeIt OutEdgeIt;
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::Edge,Color > _edgeColors;
92 ConstMap<typename Graph::Edge,double > _edgeWidths;
94 double _edgeWidthScale;
97 double _xBorder, _yBorder;
99 double _nodeBorderQuotient;
102 double _arrowLength, _arrowWidth;
104 bool _showNodes, _showEdges;
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 _autoEdgeWidthScale;
133 bool _absoluteNodeSizes;
134 bool _absoluteEdgeWidths;
140 ///\param _g is a reference to the graph to be printed
141 ///\param _os is a reference to the output stream.
142 ///\param _os is a reference to the output stream.
143 ///\param _pros If it is \c true, then the \c ostream referenced by \c _os
144 ///will be explicitly deallocated by the destructor.
145 ///By default it is <tt>std::cout</tt>
146 DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
149 _coords(dim2::Point<double>(1,1)), _nodeSizes(.01), _nodeShapes(0),
150 _nodeColors(WHITE), _edgeColors(BLACK),
151 _edgeWidths(1.0), _edgeWidthScale(0.003),
152 _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0),
153 _nodeBorderQuotient(.1),
154 _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
155 _showNodes(true), _showEdges(true),
156 _enableParallel(false), _parEdgeDist(1),
157 _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
158 _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
160 _pleaseRemoveOsStream(_pros), _scaleToA4(false),
161 _nodeTextColorType(SAME_COL), _nodeTextColors(BLACK),
162 _autoNodeScale(false),
163 _autoEdgeWidthScale(false),
164 _absoluteNodeSizes(false),
165 _absoluteEdgeWidths(false),
170 ///Helper class to implement the named parameters of \ref graphToEps()
172 ///Helper class to implement the named parameters of \ref graphToEps()
173 ///\todo Is 'helper class' a good name for this?
175 ///\todo Follow PostScript's DSC.
176 /// Use own dictionary.
177 ///\todo Useful new features.
178 /// - Linestyles: dotted, dashed etc.
179 /// - A second color and percent value for the lines.
180 template<class T> class GraphToEps : public T
182 // Can't believe it is required by the C++ standard
188 using T::_nodeShapes;
189 using T::_nodeColors;
190 using T::_edgeColors;
191 using T::_edgeWidths;
193 using T::_edgeWidthScale;
198 using T::_nodeBorderQuotient;
200 using T::_drawArrows;
201 using T::_arrowLength;
202 using T::_arrowWidth;
207 using T::_enableParallel;
208 using T::_parEdgeDist;
210 using T::_showNodeText;
212 using T::_nodeTextSize;
214 using T::_showNodePsText;
215 using T::_nodePsTexts;
216 using T::_nodePsTextsPreamble;
218 using T::_undirected;
220 using T::_pleaseRemoveOsStream;
227 using T::NodeTextColorType;
231 using T::_nodeTextColorType;
232 using T::_nodeTextColors;
234 using T::_autoNodeScale;
235 using T::_autoEdgeWidthScale;
237 using T::_absoluteNodeSizes;
238 using T::_absoluteEdgeWidths;
243 // dradnats ++C eht yb deriuqer si ti eveileb t'naC
245 typedef typename T::Graph Graph;
246 typedef typename Graph::Node Node;
247 typedef typename Graph::NodeIt NodeIt;
248 typedef typename Graph::Edge Edge;
249 typedef typename Graph::EdgeIt EdgeIt;
250 typedef typename Graph::InEdgeIt InEdgeIt;
251 typedef typename Graph::OutEdgeIt OutEdgeIt;
253 static const int INTERPOL_PREC;
254 static const double A4HEIGHT;
255 static const double A4WIDTH;
256 static const double A4BORDER;
267 ///\image html nodeshape_0.png
268 ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm
271 ///\image html nodeshape_1.png
272 ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm
276 ///\image html nodeshape_2.png
277 ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm
281 ///\image html nodeshape_3.png
282 ///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm
286 ///\image html nodeshape_4.png
287 ///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm
296 edgeLess(const Graph &_g) : g(_g) {}
297 bool operator()(Edge a,Edge b) const
299 Node ai=std::min(g.source(a),g.target(a));
300 Node aa=std::max(g.source(a),g.target(a));
301 Node bi=std::min(g.source(b),g.target(b));
302 Node ba=std::max(g.source(b),g.target(b));
304 (ai==bi && (aa < ba ||
305 (aa==ba && ai==g.source(a) && bi==g.target(b))));
308 bool isParallel(Edge e,Edge f) const
310 return (g.source(e)==g.source(f)&&
311 g.target(e)==g.target(f)) ||
312 (g.source(e)==g.target(f)&&
313 g.target(e)==g.source(f));
316 static std::string psOut(const dim2::Point<TT> &p)
318 std::ostringstream os;
319 os << p.x << ' ' << p.y;
322 static std::string psOut(const Color &c)
324 std::ostringstream os;
325 os << c.red() << ' ' << c.green() << ' ' << c.blue();
330 GraphToEps(const T &t) : T(t), dontPrint(false) {};
332 template<class X> struct CoordsTraits : public T {
333 typedef X CoordsMapType;
335 CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
337 ///Sets the map of the node coordinates
339 ///Sets the map of the node coordinates.
340 ///\param x must be a node map with dim2::Point<double> or
341 ///\ref dim2::Point "dim2::Point<int>" values.
342 template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
344 return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
346 template<class X> struct NodeSizesTraits : public T {
348 NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
350 ///Sets the map of the node sizes
352 ///Sets the map of the node sizes
353 ///\param x must be a node map with \c double (or convertible) values.
354 template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
357 return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
359 template<class X> struct NodeShapesTraits : public T {
360 const X &_nodeShapes;
361 NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
363 ///Sets the map of the node shapes
365 ///Sets the map of the node shapes.
366 ///The availabe shape values
367 ///can be found in \ref NodeShapes "enum NodeShapes".
368 ///\param x must be a node map with \c int (or convertible) values.
370 template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
373 return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
375 template<class X> struct NodeTextsTraits : public T {
377 NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
379 ///Sets the text printed on the nodes
381 ///Sets the text printed on the nodes
382 ///\param x must be a node map with type that can be pushed to a standard
384 template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
388 return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
390 template<class X> struct NodePsTextsTraits : public T {
391 const X &_nodePsTexts;
392 NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
394 ///Inserts a PostScript block to the nodes
396 ///With this command it is possible to insert a verbatim PostScript
397 ///block to the nodes.
398 ///The PS current point will be moved to the centre of the node before
399 ///the PostScript block inserted.
401 ///Before and after the block a newline character is inserted so you
402 ///don't have to bother with the separators.
404 ///\param x must be a node map with type that can be pushed to a standard
407 ///\sa nodePsTextsPreamble()
408 ///\todo Offer the choise not to move to the centre but pass the coordinates
409 ///to the Postscript block inserted.
410 template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
413 _showNodePsText=true;
414 return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
416 template<class X> struct EdgeWidthsTraits : public T {
417 const X &_edgeWidths;
418 EdgeWidthsTraits(const T &t,const X &x) : T(t), _edgeWidths(x) {}
420 ///Sets the map of the edge widths
422 ///Sets the map of the edge widths
423 ///\param x must be a edge map with \c double (or convertible) values.
424 template<class X> GraphToEps<EdgeWidthsTraits<X> > edgeWidths(const X &x)
427 return GraphToEps<EdgeWidthsTraits<X> >(EdgeWidthsTraits<X>(*this,x));
430 template<class X> struct NodeColorsTraits : public T {
431 const X &_nodeColors;
432 NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
434 ///Sets the map of the node colors
436 ///Sets the map of the node colors
437 ///\param x must be a node map with \ref Color values.
440 template<class X> GraphToEps<NodeColorsTraits<X> >
441 nodeColors(const X &x)
444 return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
446 template<class X> struct NodeTextColorsTraits : public T {
447 const X &_nodeTextColors;
448 NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {}
450 ///Sets the map of the node text colors
452 ///Sets the map of the node text colors
453 ///\param x must be a node map with \ref Color values.
456 template<class X> GraphToEps<NodeTextColorsTraits<X> >
457 nodeTextColors(const X &x)
460 _nodeTextColorType=CUST_COL;
461 return GraphToEps<NodeTextColorsTraits<X> >
462 (NodeTextColorsTraits<X>(*this,x));
464 template<class X> struct EdgeColorsTraits : public T {
465 const X &_edgeColors;
466 EdgeColorsTraits(const T &t,const X &x) : T(t), _edgeColors(x) {}
468 ///Sets the map of the edge colors
470 ///Sets the map of the edge colors
471 ///\param x must be a edge map with \ref Color values.
474 template<class X> GraphToEps<EdgeColorsTraits<X> >
475 edgeColors(const X &x)
478 return GraphToEps<EdgeColorsTraits<X> >(EdgeColorsTraits<X>(*this,x));
480 ///Sets a global scale factor for node sizes
482 ///Sets a global scale factor for node sizes.
484 /// If nodeSizes() is not given, this function simply sets the node
485 /// sizes to \c d. If nodeSizes() is given, but
486 /// autoNodeScale() is not, then the node size given by
487 /// nodeSizes() will be multiplied by the value \c d.
488 /// If both nodeSizes() and autoNodeScale() are used, then the
489 /// node sizes will be scaled in such a way that the greatest size will be
492 /// \sa autoNodeScale()
493 GraphToEps<T> &nodeScale(double d) {_nodeScale=d;return *this;}
494 ///Turns on/off the automatic node width scaling.
496 ///Turns on/off the automatic node width scaling.
500 GraphToEps<T> &autoNodeScale(bool b=true) {
501 _autoNodeScale=b;return *this;
504 ///Turns on/off the absolutematic node width scaling.
506 ///Turns on/off the absolutematic node width scaling.
510 GraphToEps<T> &absoluteNodeSizes(bool b=true) {
511 _absoluteNodeSizes=b;return *this;
514 ///Negates the Y coordinates.
516 ///Negates the Y coordinates.
520 GraphToEps<T> &negateY(bool b=true) {
521 _negY=b;return *this;
524 ///Sets a global scale factor for edge widths
526 /// Sets a global scale factor for edge widths.
528 /// If edgeWidths() is not given, this function simply sets the edge
529 /// widths to \c d. If edgeWidths() is given, but
530 /// autoEdgeWidthScale() is not, then the edge withs given by
531 /// edgeWidths() will be multiplied by the value \c d.
532 /// If both edgeWidths() and autoEdgeWidthScale() are used, then the
533 /// edge withs will be scaled in such a way that the greatest width will be
535 GraphToEps<T> &edgeWidthScale(double d) {_edgeWidthScale=d;return *this;}
536 ///Turns on/off the automatic edge width scaling.
538 ///Turns on/off the automatic edge width scaling.
540 ///\sa edgeWidthScale()
542 GraphToEps<T> &autoEdgeWidthScale(bool b=true) {
543 _autoEdgeWidthScale=b;return *this;
545 ///Turns on/off the absolutematic edge width scaling.
547 ///Turns on/off the absolutematic edge width scaling.
549 ///\sa edgeWidthScale()
551 GraphToEps<T> &absoluteEdgeWidths(bool b=true) {
552 _absoluteEdgeWidths=b;return *this;
554 ///Sets a global scale factor for the whole picture
556 ///Sets a global scale factor for the whole picture
559 GraphToEps<T> &scale(double d) {_scale=d;return *this;}
560 ///Sets the width of the border around the picture
562 ///Sets the width of the border around the picture
564 GraphToEps<T> &border(double b) {_xBorder=_yBorder=b;return *this;}
565 ///Sets the width of the border around the picture
567 ///Sets the width of the border around the picture
569 GraphToEps<T> &border(double x, double y) {
570 _xBorder=x;_yBorder=y;return *this;
572 ///Sets whether to draw arrows
574 ///Sets whether to draw arrows
576 GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
577 ///Sets the length of the arrowheads
579 ///Sets the length of the arrowheads
581 GraphToEps<T> &arrowLength(double d) {_arrowLength*=d;return *this;}
582 ///Sets the width of the arrowheads
584 ///Sets the width of the arrowheads
586 GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;}
588 ///Scales the drawing to fit to A4 page
590 ///Scales the drawing to fit to A4 page
592 GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
594 ///Enables parallel edges
596 ///Enables parallel edges
597 GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
603 GraphToEps<T> &parEdgeDist(double d) {_parEdgeDist*=d;return *this;}
609 GraphToEps<T> &hideEdges(bool b=true) {_showEdges=!b;return *this;}
614 GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
616 ///Sets the size of the node texts
618 ///Sets the size of the node texts
620 GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
622 ///Sets the color of the node texts to be different from the node color
624 ///Sets the color of the node texts to be as different from the node color
627 GraphToEps<T> &distantColorNodeTexts()
628 {_nodeTextColorType=DIST_COL;return *this;}
629 ///Sets the color of the node texts to be black or white and always visible.
631 ///Sets the color of the node texts to be black or white according to
633 ///different from the node color
635 GraphToEps<T> &distantBWNodeTexts()
636 {_nodeTextColorType=DIST_BW;return *this;}
638 ///Gives a preamble block for node Postscript block.
640 ///Gives a preamble block for node Postscript block.
643 GraphToEps<T> & nodePsTextsPreamble(const char *str) {
644 _nodePsTextsPreamble=str ;return *this;
646 ///Sets whether the the graph is undirected
648 ///Sets whether the the graph is undirected
650 GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;}
652 ///Sets whether the the graph is directed
654 ///Sets whether the the graph is directed.
655 ///Use it to show the undirected edges as a pair of directed ones.
656 GraphToEps<T> &bidir(bool b=true) {_undirected=!b;return *this;}
660 ///Sets the title of the generated image,
661 ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of
663 GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
664 ///Sets the copyright statement.
666 ///Sets the copyright statement of the generated image,
667 ///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of
669 ///\todo Multiline copyright notice could be supported.
670 GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;}
673 bool isInsideNode(dim2::Point<double> p, double r,int t)
679 return p.normSquare()<=r*r;
681 return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
683 return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
693 ///Like other functions using
694 ///\ref named-templ-func-param "named template parameters",
695 ///this function calles the algorithm itself, i.e. in this case
696 ///it draws the graph.
698 ///\todo better 'epsilon' would be nice here.
699 const double EPSILON=1e-9;
700 if(dontPrint) return;
702 _NegY<typename T::CoordsMapType> mycoords(_coords,_negY);
704 os << "%!PS-Adobe-2.0 EPSF-2.0\n";
705 if(_title.size()>0) os << "%%Title: " << _title << '\n';
706 if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
707 // << "%%Copyright: XXXX\n"
708 os << "%%Creator: LEMON, graphToEps()\n";
713 gettimeofday(&tv, 0);
714 ctime_r(&tv.tv_sec,cbuf);
715 os << "%%CreationDate: " << cbuf;
718 if (_autoEdgeWidthScale) {
720 for(EdgeIt e(g);e!=INVALID;++e)
721 max_w=std::max(double(_edgeWidths[e]),max_w);
722 ///\todo better 'epsilon' would be nice here.
724 _edgeWidthScale/=max_w;
728 if (_autoNodeScale) {
730 for(NodeIt n(g);n!=INVALID;++n)
731 max_s=std::max(double(_nodeSizes[n]),max_s);
732 ///\todo better 'epsilon' would be nice here.
739 if(!(_absoluteNodeSizes&&_absoluteEdgeWidths)) {
740 dim2::BoundingBox<double> bb;
741 for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]);
743 bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0));
745 diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare());
746 if(diag_len<EPSILON) diag_len = 1;
747 if(!_absoluteNodeSizes) _nodeScale*=diag_len;
748 if(!_absoluteEdgeWidths) _edgeWidthScale*=diag_len;
751 dim2::BoundingBox<double> bb;
752 for(NodeIt n(g);n!=INVALID;++n) {
753 double ns=_nodeSizes[n]*_nodeScale;
754 dim2::Point<double> p(ns,ns);
755 switch(_nodeShapes[n]) {
759 bb.add(p+mycoords[n]);
760 bb.add(-p+mycoords[n]);
763 bb.add(-p+mycoords[n]);
764 bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]);
767 bb.add(p+mycoords[n]);
768 bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]);
773 bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0));
777 os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
779 //Rescale so that BoundingBox won't be neither to big nor too small.
780 while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10;
781 while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10;
783 os << "%%BoundingBox: "
784 << int(floor(bb.left() * _scale - _xBorder)) << ' '
785 << int(floor(bb.bottom() * _scale - _yBorder)) << ' '
786 << int(ceil(bb.right() * _scale + _xBorder)) << ' '
787 << int(ceil(bb.top() * _scale + _yBorder)) << '\n';
790 os << "%%EndComments\n";
792 //x1 y1 x2 y2 x3 y3 cr cg cb w
793 os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
794 << " 4 2 roll 1 index 1 index curveto stroke } bind def\n";
795 os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
797 os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n";
799 os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
800 << " 2 index 1 index sub 2 index 2 index add lineto\n"
801 << " 2 index 1 index sub 2 index 2 index sub lineto\n"
802 << " 2 index 1 index add 2 index 2 index sub lineto\n"
803 << " closepath pop pop pop} bind def\n";
805 os << "/di { newpath 2 index 1 index add 2 index moveto\n"
806 << " 2 index 2 index 2 index add lineto\n"
807 << " 2 index 1 index sub 2 index lineto\n"
808 << " 2 index 2 index 2 index sub lineto\n"
809 << " closepath pop pop pop} bind def\n";
811 os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
812 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
814 os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
815 << " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
817 os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
818 << " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
820 os << "/nfemale { 0 0 0 setrgbcolor 3 index "
821 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
822 << " 1.5 mul mul setlinewidth\n"
823 << " newpath 5 index 5 index moveto "
824 << "5 index 5 index 5 index 3.01 mul sub\n"
825 << " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub moveto\n"
826 << " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto stroke\n"
827 << " 5 index 5 index 5 index c fill\n"
828 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
831 << " 0 0 0 setrgbcolor 3 index "
832 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
833 <<" 1.5 mul mul setlinewidth\n"
834 << " newpath 5 index 5 index moveto\n"
835 << " 5 index 4 index 1 mul 1.5 mul add\n"
836 << " 5 index 5 index 3 sqrt 1.5 mul mul add\n"
837 << " 1 index 1 index lineto\n"
838 << " 1 index 1 index 7 index sub moveto\n"
839 << " 1 index 1 index lineto\n"
840 << " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub lineto\n"
842 << " 5 index 5 index 5 index c fill\n"
843 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
847 os << "/arrl " << _arrowLength << " def\n";
848 os << "/arrw " << _arrowWidth << " def\n";
850 os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
851 //len w dx_norm dy_norm x1 y1 cr cg cb
852 os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
853 << " /w exch def /len exch def\n"
854 // << " 0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
855 << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
856 << " len w sub arrl sub dx dy lrl\n"
857 << " arrw dy dx neg lrl\n"
858 << " dx arrl w add mul dy w 2 div arrw add mul sub\n"
859 << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
860 << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
861 << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
862 << " arrw dy dx neg lrl\n"
863 << " len w sub arrl sub neg dx dy lrl\n"
864 << " closepath fill } bind def\n";
865 os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
866 << " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
870 if(bb.height()>bb.width()) {
871 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(),
872 (A4WIDTH-2*A4BORDER)/bb.width());
873 os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
874 << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER
876 << sc << " dup scale\n"
877 << -bb.left() << ' ' << -bb.bottom() << " translate\n";
880 //\todo Verify centering
881 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(),
882 (A4WIDTH-2*A4BORDER)/bb.height());
883 os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
884 << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER
886 << sc << " dup scale\n90 rotate\n"
887 << -bb.left() << ' ' << -bb.top() << " translate\n";
889 else if(_scale!=1.0) os << _scale << " dup scale\n";
892 os << "%Edges:\ngsave\n";
893 if(_enableParallel) {
894 std::vector<Edge> el;
895 for(EdgeIt e(g);e!=INVALID;++e)
896 if((!_undirected||g.source(e)<g.target(e))&&_edgeWidths[e]>0
897 &&g.source(e)!=g.target(e))
899 std::sort(el.begin(),el.end(),edgeLess(g));
901 typename std::vector<Edge>::iterator j;
902 for(typename std::vector<Edge>::iterator i=el.begin();i!=el.end();i=j) {
903 for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
906 for(typename std::vector<Edge>::iterator e=i;e!=j;++e)
907 sw+=_edgeWidths[*e]*_edgeWidthScale+_parEdgeDist;
911 dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]);
912 double l=std::sqrt(dvec.normSquare());
913 ///\todo better 'epsilon' would be nice here.
914 dim2::Point<double> d(dvec/std::max(l,EPSILON));
915 dim2::Point<double> m;
916 // m=dim2::Point<double>(mycoords[g.target(*i)]+mycoords[g.source(*i)])/2.0;
918 // m=dim2::Point<double>(mycoords[g.source(*i)])+
919 // dvec*(double(_nodeSizes[g.source(*i)])/
920 // (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
922 m=dim2::Point<double>(mycoords[g.source(*i)])+
923 d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
925 for(typename std::vector<Edge>::iterator e=i;e!=j;++e) {
926 sw+=_edgeWidths[*e]*_edgeWidthScale/2.0;
927 dim2::Point<double> mm=m+rot90(d)*sw/.75;
930 dim2::Point<double> s=mycoords[g.source(*e)];
931 dim2::Point<double> t=mycoords[g.target(*e)];
932 double rn=_nodeSizes[g.target(*e)]*_nodeScale;
933 node_shape=_nodeShapes[g.target(*e)];
934 dim2::Bezier3 bez(s,mm,mm,t);
936 for(int i=0;i<INTERPOL_PREC;++i)
937 if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
939 dim2::Point<double> apoint=bez((t1+t2)/2);
940 rn = _arrowLength+_edgeWidths[*e]*_edgeWidthScale;
943 for(int i=0;i<INTERPOL_PREC;++i)
944 if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
946 dim2::Point<double> linend=bez((t1+t2)/2);
947 bez=bez.before((t1+t2)/2);
948 // rn=_nodeSizes[g.source(*e)]*_nodeScale;
949 // node_shape=_nodeShapes[g.source(*e)];
951 // for(int i=0;i<INTERPOL_PREC;++i)
952 // if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t1=(t1+t2)/2;
953 // else t2=(t1+t2)/2;
954 // bez=bez.after((t1+t2)/2);
955 os << _edgeWidths[*e]*_edgeWidthScale << " setlinewidth "
956 << _edgeColors[*e].red() << ' '
957 << _edgeColors[*e].green() << ' '
958 << _edgeColors[*e].blue() << " setrgbcolor newpath\n"
959 << bez.p1.x << ' ' << bez.p1.y << " moveto\n"
960 << bez.p2.x << ' ' << bez.p2.y << ' '
961 << bez.p3.x << ' ' << bez.p3.y << ' '
962 << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
963 dim2::Point<double> dd(rot90(linend-apoint));
964 dd*=(.5*_edgeWidths[*e]*_edgeWidthScale+_arrowWidth)/
965 std::sqrt(dd.normSquare());
966 os << "newpath " << psOut(apoint) << " moveto "
967 << psOut(linend+dd) << " lineto "
968 << psOut(linend-dd) << " lineto closepath fill\n";
971 os << mycoords[g.source(*e)].x << ' '
972 << mycoords[g.source(*e)].y << ' '
973 << mm.x << ' ' << mm.y << ' '
974 << mycoords[g.target(*e)].x << ' '
975 << mycoords[g.target(*e)].y << ' '
976 << _edgeColors[*e].red() << ' '
977 << _edgeColors[*e].green() << ' '
978 << _edgeColors[*e].blue() << ' '
979 << _edgeWidths[*e]*_edgeWidthScale << " lb\n";
981 sw+=_edgeWidths[*e]*_edgeWidthScale/2.0+_parEdgeDist;
985 else for(EdgeIt e(g);e!=INVALID;++e)
986 if((!_undirected||g.source(e)<g.target(e))&&_edgeWidths[e]>0
987 &&g.source(e)!=g.target(e))
989 dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]);
990 double rn=_nodeSizes[g.target(e)]*_nodeScale;
991 int node_shape=_nodeShapes[g.target(e)];
993 for(int i=0;i<INTERPOL_PREC;++i)
994 if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
996 double l=std::sqrt(d.normSquare());
999 os << l*(1-(t1+t2)/2) << ' '
1000 << _edgeWidths[e]*_edgeWidthScale << ' '
1001 << d.x << ' ' << d.y << ' '
1002 << mycoords[g.source(e)].x << ' '
1003 << mycoords[g.source(e)].y << ' '
1004 << _edgeColors[e].red() << ' '
1005 << _edgeColors[e].green() << ' '
1006 << _edgeColors[e].blue() << " arr\n";
1008 else os << mycoords[g.source(e)].x << ' '
1009 << mycoords[g.source(e)].y << ' '
1010 << mycoords[g.target(e)].x << ' '
1011 << mycoords[g.target(e)].y << ' '
1012 << _edgeColors[e].red() << ' '
1013 << _edgeColors[e].green() << ' '
1014 << _edgeColors[e].blue() << ' '
1015 << _edgeWidths[e]*_edgeWidthScale << " l\n";
1019 os << "%Nodes:\ngsave\n";
1020 for(NodeIt n(g);n!=INVALID;++n) {
1021 os << mycoords[n].x << ' ' << mycoords[n].y << ' '
1022 << _nodeSizes[n]*_nodeScale << ' '
1023 << _nodeColors[n].red() << ' '
1024 << _nodeColors[n].green() << ' '
1025 << _nodeColors[n].blue() << ' ';
1026 switch(_nodeShapes[n]) {
1036 os<< "nfemale";break;
1043 os << "%Node texts:\ngsave\n";
1044 os << "/fosi " << _nodeTextSize << " def\n";
1045 os << "(Helvetica) findfont fosi scalefont setfont\n";
1046 for(NodeIt n(g);n!=INVALID;++n) {
1047 switch(_nodeTextColorType) {
1049 os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n";
1052 os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n";
1055 os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n";
1058 os << "0 0 0 setrgbcolor\n";
1060 os << mycoords[n].x << ' ' << mycoords[n].y
1061 << " (" << _nodeTexts[n] << ") cshow\n";
1065 if(_showNodePsText) {
1066 os << "%Node PS blocks:\ngsave\n";
1067 for(NodeIt n(g);n!=INVALID;++n)
1068 os << mycoords[n].x << ' ' << mycoords[n].y
1069 << " moveto\n" << _nodePsTexts[n] << "\n";
1073 os << "grestore\nshowpage\n";
1076 if(_pleaseRemoveOsStream) {delete &os;}
1081 const int GraphToEps<T>::INTERPOL_PREC = 20;
1083 const double GraphToEps<T>::A4HEIGHT = 841.8897637795276;
1085 const double GraphToEps<T>::A4WIDTH = 595.275590551181;
1087 const double GraphToEps<T>::A4BORDER = 15;
1090 ///Generates an EPS file from a graph
1093 ///Generates an EPS file from a graph.
1094 ///\param g is a reference to the graph to be printed
1095 ///\param os is a reference to the output stream.
1096 ///By default it is <tt>std::cout</tt>
1098 ///This function also has a lot of
1099 ///\ref named-templ-func-param "named parameters",
1100 ///they are declared as the members of class \ref GraphToEps. The following
1101 ///example shows how to use these parameters.
1103 /// graphToEps(g,os).scale(10).coords(coords)
1104 /// .nodeScale(2).nodeSizes(sizes)
1105 /// .edgeWidthScale(.4).run();
1107 ///\warning Don't forget to put the \ref GraphToEps::run() "run()"
1108 ///to the end of the parameter list.
1110 ///\sa graphToEps(G &g, const char *file_name)
1112 GraphToEps<DefaultGraphToEpsTraits<G> >
1113 graphToEps(G &g, std::ostream& os=std::cout)
1116 GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
1119 ///Generates an EPS file from a graph
1122 ///This function does the same as
1123 ///\ref graphToEps(G &g,std::ostream& os)
1124 ///but it writes its output into the file \c file_name
1125 ///instead of a stream.
1126 ///\sa graphToEps(G &g, std::ostream& os)
1128 GraphToEps<DefaultGraphToEpsTraits<G> >
1129 graphToEps(G &g,const char *file_name)
1131 return GraphToEps<DefaultGraphToEpsTraits<G> >
1132 (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true));
1135 ///Generates an EPS file from a graph
1138 ///This function does the same as
1139 ///\ref graphToEps(G &g,std::ostream& os)
1140 ///but it writes its output into the file \c file_name
1141 ///instead of a stream.
1142 ///\sa graphToEps(G &g, std::ostream& os)
1144 GraphToEps<DefaultGraphToEpsTraits<G> >
1145 graphToEps(G &g,const std::string& file_name)
1147 return GraphToEps<DefaultGraphToEpsTraits<G> >
1148 (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name.c_str()),true));
1151 } //END OF NAMESPACE LEMON
1153 #endif // LEMON_GRAPH_TO_EPS_H