Set props.
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>
39 #include<lemon/maps.h>
40 #include<lemon/color.h>
41 #include<lemon/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,xy<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;
137 ///\param _g is a reference to the graph to be printed
138 ///\param _os is a reference to the output stream.
139 ///\param _os is a reference to the output stream.
140 ///\param _pros If it is \c true, then the \c ostream referenced by \c _os
141 ///will be explicitly deallocated by the destructor.
142 ///By default it is <tt>std::cout</tt>
143 DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
146 _coords(xy<double>(1,1)), _nodeSizes(1.0), _nodeShapes(0),
147 _nodeColors(Color(1,1,1)), _edgeColors(Color(0,0,0)),
148 _edgeWidths(1), _edgeWidthScale(0.3),
149 _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0),
150 _nodeBorderQuotient(.1),
151 _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
152 _showNodes(true), _showEdges(true),
153 _enableParallel(false), _parEdgeDist(1),
154 _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
155 _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
157 _pleaseRemoveOsStream(_pros), _scaleToA4(false),
158 _nodeTextColorType(SAME_COL), _nodeTextColors(Color(0,0,0)),
159 _autoNodeScale(false),
160 _autoEdgeWidthScale(false),
165 ///Helper class to implement the named parameters of \ref graphToEps()
167 ///Helper class to implement the named parameters of \ref graphToEps()
168 ///\todo Is 'helper class' a good name for this?
170 ///\todo Follow PostScript's DSC.
171 /// Use own dictionary.
172 ///\todo Useful new features.
173 /// - Linestyles: dotted, dashed etc.
174 /// - A second color and percent value for the lines.
175 template<class T> class GraphToEps : public T
177 // Can't believe it is required by the C++ standard
183 using T::_nodeShapes;
184 using T::_nodeColors;
185 using T::_edgeColors;
186 using T::_edgeWidths;
188 using T::_edgeWidthScale;
193 using T::_nodeBorderQuotient;
195 using T::_drawArrows;
196 using T::_arrowLength;
197 using T::_arrowWidth;
202 using T::_enableParallel;
203 using T::_parEdgeDist;
205 using T::_showNodeText;
207 using T::_nodeTextSize;
209 using T::_showNodePsText;
210 using T::_nodePsTexts;
211 using T::_nodePsTextsPreamble;
213 using T::_undirected;
215 using T::_pleaseRemoveOsStream;
222 using T::NodeTextColorType;
226 using T::_nodeTextColorType;
227 using T::_nodeTextColors;
229 using T::_autoNodeScale;
230 using T::_autoEdgeWidthScale;
234 // dradnats ++C eht yb deriuqer si ti eveileb t'naC
236 typedef typename T::Graph Graph;
237 typedef typename Graph::Node Node;
238 typedef typename Graph::NodeIt NodeIt;
239 typedef typename Graph::Edge Edge;
240 typedef typename Graph::EdgeIt EdgeIt;
241 typedef typename Graph::InEdgeIt InEdgeIt;
242 typedef typename Graph::OutEdgeIt OutEdgeIt;
244 static const int INTERPOL_PREC;
245 static const double A4HEIGHT;
246 static const double A4WIDTH;
247 static const double A4BORDER;
258 ///\image html nodeshape_0.png
259 ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm
262 ///\image html nodeshape_1.png
263 ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm
267 ///\image html nodeshape_2.png
268 ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm
272 ///\image html nodeshape_3.png
273 ///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm
277 ///\image html nodeshape_4.png
278 ///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm
287 edgeLess(const Graph &_g) : g(_g) {}
288 bool operator()(Edge a,Edge b) const
290 Node ai=std::min(g.source(a),g.target(a));
291 Node aa=std::max(g.source(a),g.target(a));
292 Node bi=std::min(g.source(b),g.target(b));
293 Node ba=std::max(g.source(b),g.target(b));
295 (ai==bi && (aa < ba ||
296 (aa==ba && ai==g.source(a) && bi==g.target(b))));
299 bool isParallel(Edge e,Edge f) const
301 return (g.source(e)==g.source(f)&&
302 g.target(e)==g.target(f)) ||
303 (g.source(e)==g.target(f)&&
304 g.target(e)==g.source(f));
307 static std::string psOut(const xy<TT> &p)
309 std::ostringstream os;
310 os << p.x << ' ' << p.y;
313 static std::string psOut(const Color &c)
315 std::ostringstream os;
316 os << c.red() << ' ' << c.green() << ' ' << c.blue();
321 GraphToEps(const T &t) : T(t), dontPrint(false) {};
323 template<class X> struct CoordsTraits : public T {
324 typedef X CoordsMapType;
326 CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
328 ///Sets the map of the node coordinates
330 ///Sets the map of the node coordinates.
331 ///\param x must be a node map with xy<double> or \ref xy "xy<int>" values.
332 template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
334 return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
336 template<class X> struct NodeSizesTraits : public T {
338 NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
340 ///Sets the map of the node sizes
342 ///Sets the map of the node sizes
343 ///\param x must be a node map with \c double (or convertible) values.
344 template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
347 return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
349 template<class X> struct NodeShapesTraits : public T {
350 const X &_nodeShapes;
351 NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
353 ///Sets the map of the node shapes
355 ///Sets the map of the node shapes.
356 ///The availabe shape values
357 ///can be found in \ref NodeShapes "enum NodeShapes".
358 ///\param x must be a node map with \c int (or convertible) values.
360 template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
363 return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
365 template<class X> struct NodeTextsTraits : public T {
367 NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
369 ///Sets the text printed on the nodes
371 ///Sets the text printed on the nodes
372 ///\param x must be a node map with type that can be pushed to a standard
374 template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
378 return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
380 template<class X> struct NodePsTextsTraits : public T {
381 const X &_nodePsTexts;
382 NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
384 ///Inserts a PostScript block to the nodes
386 ///With this command it is possible to insert a verbatim PostScript
387 ///block to the nodes.
388 ///The PS current point will be moved to the centre of the node before
389 ///the PostScript block inserted.
391 ///Before and after the block a newline character is inserted so you
392 ///don't have to bother with the separators.
394 ///\param x must be a node map with type that can be pushed to a standard
397 ///\sa nodePsTextsPreamble()
398 ///\todo Offer the choise not to move to the centre but pass the coordinates
399 ///to the Postscript block inserted.
400 template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
403 _showNodePsText=true;
404 return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
406 template<class X> struct EdgeWidthsTraits : public T {
407 const X &_edgeWidths;
408 EdgeWidthsTraits(const T &t,const X &x) : T(t), _edgeWidths(x) {}
410 ///Sets the map of the edge widths
412 ///Sets the map of the edge widths
413 ///\param x must be a edge map with \c double (or convertible) values.
414 template<class X> GraphToEps<EdgeWidthsTraits<X> > edgeWidths(const X &x)
417 return GraphToEps<EdgeWidthsTraits<X> >(EdgeWidthsTraits<X>(*this,x));
420 template<class X> struct NodeColorsTraits : public T {
421 const X &_nodeColors;
422 NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
424 ///Sets the map of the node colors
426 ///Sets the map of the node colors
427 ///\param x must be a node map with \ref Color values.
430 template<class X> GraphToEps<NodeColorsTraits<X> >
431 nodeColors(const X &x)
434 return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
436 template<class X> struct NodeTextColorsTraits : public T {
437 const X &_nodeTextColors;
438 NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {}
440 ///Sets the map of the node text colors
442 ///Sets the map of the node text colors
443 ///\param x must be a node map with \ref Color values.
446 template<class X> GraphToEps<NodeTextColorsTraits<X> >
447 nodeTextColors(const X &x)
450 _nodeTextColorType=CUST_COL;
451 return GraphToEps<NodeTextColorsTraits<X> >
452 (NodeTextColorsTraits<X>(*this,x));
454 template<class X> struct EdgeColorsTraits : public T {
455 const X &_edgeColors;
456 EdgeColorsTraits(const T &t,const X &x) : T(t), _edgeColors(x) {}
458 ///Sets the map of the edge colors
460 ///Sets the map of the edge colors
461 ///\param x must be a edge map with \ref Color values.
464 template<class X> GraphToEps<EdgeColorsTraits<X> >
465 edgeColors(const X &x)
468 return GraphToEps<EdgeColorsTraits<X> >(EdgeColorsTraits<X>(*this,x));
470 ///Sets a global scale factor for node sizes
472 ///Sets a global scale factor for node sizes.
474 /// If nodeSizes() is not given, this function simply sets the node
475 /// sizes to \c d. If nodeSizes() is given, but
476 /// autoNodeScale() is not, then the node size given by
477 /// nodeSizes() will be multiplied by the value \c d.
478 /// If both nodeSizes() and autoNodeScale() are used, then the
479 /// node sizes will be scaled in such a way that the greatest size will be
481 GraphToEps<T> &nodeScale(double d) {_nodeScale=d;return *this;}
482 ///Turns on/off the automatic node width scaling.
484 ///Turns on/off the automatic node width scaling.
488 GraphToEps<T> &autoNodeScale(bool b=true) {
489 _autoNodeScale=b;return *this;
492 ///Negates the Y coordinates.
494 ///Negates the Y coordinates.
498 GraphToEps<T> &negateY(bool b=true) {
499 _negY=b;return *this;
502 ///Sets a global scale factor for edge widths
504 /// Sets a global scale factor for edge widths.
506 /// If edgeWidths() is not given, this function simply sets the edge
507 /// widths to \c d. If edgeWidths() is given, but
508 /// autoEdgeWidthScale() is not, then the edge withs given by
509 /// edgeWidths() will be multiplied by the value \c d.
510 /// If both edgeWidths() and autoEdgeWidthScale() are used, then the
511 /// edge withs will be scaled in such a way that the greatest width will be
513 GraphToEps<T> &edgeWidthScale(double d) {_edgeWidthScale=d;return *this;}
514 ///Turns on/off the automatic edge width scaling.
516 ///Turns on/off the automatic edge width scaling.
518 ///\sa edgeWidthScale()
520 GraphToEps<T> &autoEdgeWidthScale(bool b=true) {
521 _autoEdgeWidthScale=b;return *this;
523 ///Sets a global scale factor for the whole picture
525 ///Sets a global scale factor for the whole picture
528 GraphToEps<T> &scale(double d) {_scale=d;return *this;}
529 ///Sets the width of the border around the picture
531 ///Sets the width of the border around the picture
533 GraphToEps<T> &border(double b) {_xBorder=_yBorder=b;return *this;}
534 ///Sets the width of the border around the picture
536 ///Sets the width of the border around the picture
538 GraphToEps<T> &border(double x, double y) {
539 _xBorder=x;_yBorder=y;return *this;
541 ///Sets whether to draw arrows
543 ///Sets whether to draw arrows
545 GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
546 ///Sets the length of the arrowheads
548 ///Sets the length of the arrowheads
550 GraphToEps<T> &arrowLength(double d) {_arrowLength*=d;return *this;}
551 ///Sets the width of the arrowheads
553 ///Sets the width of the arrowheads
555 GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;}
557 ///Scales the drawing to fit to A4 page
559 ///Scales the drawing to fit to A4 page
561 GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
563 ///Enables parallel edges
565 ///Enables parallel edges
566 GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
572 GraphToEps<T> &parEdgeDist(double d) {_parEdgeDist*=d;return *this;}
578 GraphToEps<T> &hideEdges(bool b=true) {_showEdges=!b;return *this;}
583 GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
585 ///Sets the size of the node texts
587 ///Sets the size of the node texts
589 GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
591 ///Sets the color of the node texts to be different from the node color
593 ///Sets the color of the node texts to be as different from the node color
596 GraphToEps<T> &distantColorNodeTexts()
597 {_nodeTextColorType=DIST_COL;return *this;}
598 ///Sets the color of the node texts to be black or white and always visible.
600 ///Sets the color of the node texts to be black or white according to
602 ///different from the node color
604 GraphToEps<T> &distantBWNodeTexts()
605 {_nodeTextColorType=DIST_BW;return *this;}
607 ///Gives a preamble block for node Postscript block.
609 ///Gives a preamble block for node Postscript block.
612 GraphToEps<T> & nodePsTextsPreamble(const char *str) {
613 _nodePsTextsPreamble=str ;return *this;
615 ///Sets whether the the graph is undirected
617 ///Sets whether the the graph is undirected
619 GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;}
621 ///Sets whether the the graph is directed
623 ///Sets whether the the graph is directed.
624 ///Use it to show the undirected edges as a pair of directed ones.
625 GraphToEps<T> &bidir(bool b=true) {_undirected=!b;return *this;}
629 ///Sets the title of the generated image,
630 ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of
632 GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
633 ///Sets the copyright statement.
635 ///Sets the copyright statement of the generated image,
636 ///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of
638 ///\todo Multiline copyright notice could be supported.
639 GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;}
642 bool isInsideNode(xy<double> p, double r,int t)
648 return p.normSquare()<=r*r;
650 return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
652 return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
662 ///Like other functions using
663 ///\ref named-templ-func-param "named template parameters",
664 ///this function calles the algorithm itself, i.e. in this case
665 ///it draws the graph.
667 if(dontPrint) return;
669 _NegY<typename T::CoordsMapType> mycoords(_coords,_negY);
671 os << "%!PS-Adobe-2.0 EPSF-2.0\n";
672 if(_title.size()>0) os << "%%Title: " << _title << '\n';
673 if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
674 // << "%%Copyright: XXXX\n"
675 os << "%%Creator: LEMON, graphToEps()\n";
680 gettimeofday(&tv, 0);
681 ctime_r(&tv.tv_sec,cbuf);
682 os << "%%CreationDate: " << cbuf;
685 if (_autoEdgeWidthScale) {
687 for(EdgeIt e(g);e!=INVALID;++e)
688 max_w=std::max(double(_edgeWidths[e]),max_w);
689 ///\todo better 'epsilon' would be nice here.
691 _edgeWidthScale/=max_w;
695 if (_autoNodeScale) {
697 for(NodeIt n(g);n!=INVALID;++n)
698 max_s=std::max(double(_nodeSizes[n]),max_s);
699 ///\todo better 'epsilon' would be nice here.
706 BoundingBox<double> bb;
707 ///\bug: Chech whether the graph is empty.
708 for(NodeIt n(g);n!=INVALID;++n) {
709 double ns=_nodeSizes[n]*_nodeScale;
711 switch(_nodeShapes[n]) {
715 bb.add(p+mycoords[n]);
716 bb.add(-p+mycoords[n]);
719 bb.add(-p+mycoords[n]);
720 bb.add(xy<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]);
723 bb.add(p+mycoords[n]);
724 bb.add(xy<double>(-ns,-3.01*ns)+mycoords[n]);
729 bb = BoundingBox<double>(xy<double>(0,0));
733 os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
735 //Rescale so that BoundingBox won't be neither to big nor too small.
736 while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10;
737 while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10;
739 os << "%%BoundingBox: "
740 << int(floor(bb.left() * _scale - _xBorder)) << ' '
741 << int(floor(bb.bottom() * _scale - _yBorder)) << ' '
742 << int(ceil(bb.right() * _scale + _xBorder)) << ' '
743 << int(ceil(bb.top() * _scale + _yBorder)) << '\n';
746 os << "%%EndComments\n";
748 //x1 y1 x2 y2 x3 y3 cr cg cb w
749 os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
750 << " 4 2 roll 1 index 1 index curveto stroke } bind def\n";
751 os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n";
753 os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n";
755 os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
756 << " 2 index 1 index sub 2 index 2 index add lineto\n"
757 << " 2 index 1 index sub 2 index 2 index sub lineto\n"
758 << " 2 index 1 index add 2 index 2 index sub lineto\n"
759 << " closepath pop pop pop} bind def\n";
761 os << "/di { newpath 2 index 1 index add 2 index moveto\n"
762 << " 2 index 2 index 2 index add lineto\n"
763 << " 2 index 1 index sub 2 index lineto\n"
764 << " 2 index 2 index 2 index sub lineto\n"
765 << " closepath pop pop pop} bind def\n";
767 os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
768 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
770 os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
771 << " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
773 os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
774 << " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
776 os << "/nfemale { 0 0 0 setrgbcolor 3 index "
777 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
778 << " 1.5 mul mul setlinewidth\n"
779 << " newpath 5 index 5 index moveto "
780 << "5 index 5 index 5 index 3.01 mul sub\n"
781 << " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub moveto\n"
782 << " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto stroke\n"
783 << " 5 index 5 index 5 index c fill\n"
784 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
787 << " 0 0 0 setrgbcolor 3 index "
788 << _nodeBorderQuotient/(1+_nodeBorderQuotient)
789 <<" 1.5 mul mul setlinewidth\n"
790 << " newpath 5 index 5 index moveto\n"
791 << " 5 index 4 index 1 mul 1.5 mul add\n"
792 << " 5 index 5 index 3 sqrt 1.5 mul mul add\n"
793 << " 1 index 1 index lineto\n"
794 << " 1 index 1 index 7 index sub moveto\n"
795 << " 1 index 1 index lineto\n"
796 << " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub lineto\n"
798 << " 5 index 5 index 5 index c fill\n"
799 << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
803 os << "/arrl " << _arrowLength << " def\n";
804 os << "/arrw " << _arrowWidth << " def\n";
806 os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
807 //len w dx_norm dy_norm x1 y1 cr cg cb
808 os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n"
809 << " /w exch def /len exch def\n"
810 // << " 0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
811 << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
812 << " len w sub arrl sub dx dy lrl\n"
813 << " arrw dy dx neg lrl\n"
814 << " dx arrl w add mul dy w 2 div arrw add mul sub\n"
815 << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
816 << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
817 << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
818 << " arrw dy dx neg lrl\n"
819 << " len w sub arrl sub neg dx dy lrl\n"
820 << " closepath fill } bind def\n";
821 os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
822 << " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
826 if(bb.height()>bb.width()) {
827 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(),
828 (A4WIDTH-2*A4BORDER)/bb.width());
829 os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
830 << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER << " translate\n"
831 << sc << " dup scale\n"
832 << -bb.left() << ' ' << -bb.bottom() << " translate\n";
835 //\todo Verify centering
836 double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(),
837 (A4WIDTH-2*A4BORDER)/bb.height());
838 os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
839 << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER << " translate\n"
840 << sc << " dup scale\n90 rotate\n"
841 << -bb.left() << ' ' << -bb.top() << " translate\n";
843 else if(_scale!=1.0) os << _scale << " dup scale\n";
846 os << "%Edges:\ngsave\n";
847 if(_enableParallel) {
848 std::vector<Edge> el;
849 for(EdgeIt e(g);e!=INVALID;++e)
850 if((!_undirected||g.source(e)<g.target(e))&&_edgeWidths[e]>0
851 &&g.source(e)!=g.target(e))
853 std::sort(el.begin(),el.end(),edgeLess(g));
855 typename std::vector<Edge>::iterator j;
856 for(typename std::vector<Edge>::iterator i=el.begin();i!=el.end();i=j) {
857 for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
860 for(typename std::vector<Edge>::iterator e=i;e!=j;++e)
861 sw+=_edgeWidths[*e]*_edgeWidthScale+_parEdgeDist;
864 xy<double> dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]);
865 double l=std::sqrt(dvec.normSquare());
866 ///\todo better 'epsilon' would be nice here.
867 xy<double> d(dvec/std::max(l,1e-9));
869 // m=xy<double>(mycoords[g.target(*i)]+mycoords[g.source(*i)])/2.0;
871 // m=xy<double>(mycoords[g.source(*i)])+
872 // dvec*(double(_nodeSizes[g.source(*i)])/
873 // (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
875 m=xy<double>(mycoords[g.source(*i)])+
876 d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
878 for(typename std::vector<Edge>::iterator e=i;e!=j;++e) {
879 sw+=_edgeWidths[*e]*_edgeWidthScale/2.0;
880 xy<double> mm=m+rot90(d)*sw/.75;
883 xy<double> s=mycoords[g.source(*e)];
884 xy<double> t=mycoords[g.target(*e)];
885 double rn=_nodeSizes[g.target(*e)]*_nodeScale;
886 node_shape=_nodeShapes[g.target(*e)];
887 Bezier3 bez(s,mm,mm,t);
889 for(int i=0;i<INTERPOL_PREC;++i)
890 if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
892 xy<double> apoint=bez((t1+t2)/2);
893 rn = _arrowLength+_edgeWidths[*e]*_edgeWidthScale;
896 for(int i=0;i<INTERPOL_PREC;++i)
897 if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
899 xy<double> linend=bez((t1+t2)/2);
900 bez=bez.before((t1+t2)/2);
901 // rn=_nodeSizes[g.source(*e)]*_nodeScale;
902 // node_shape=_nodeShapes[g.source(*e)];
904 // for(int i=0;i<INTERPOL_PREC;++i)
905 // if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t1=(t1+t2)/2;
906 // else t2=(t1+t2)/2;
907 // bez=bez.after((t1+t2)/2);
908 os << _edgeWidths[*e]*_edgeWidthScale << " setlinewidth "
909 << _edgeColors[*e].red() << ' '
910 << _edgeColors[*e].green() << ' '
911 << _edgeColors[*e].blue() << " setrgbcolor newpath\n"
912 << bez.p1.x << ' ' << bez.p1.y << " moveto\n"
913 << bez.p2.x << ' ' << bez.p2.y << ' '
914 << bez.p3.x << ' ' << bez.p3.y << ' '
915 << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
916 xy<double> dd(rot90(linend-apoint));
917 dd*=(.5*_edgeWidths[*e]*_edgeWidthScale+_arrowWidth)/
918 std::sqrt(dd.normSquare());
919 os << "newpath " << psOut(apoint) << " moveto "
920 << psOut(linend+dd) << " lineto "
921 << psOut(linend-dd) << " lineto closepath fill\n";
924 os << mycoords[g.source(*e)].x << ' '
925 << mycoords[g.source(*e)].y << ' '
926 << mm.x << ' ' << mm.y << ' '
927 << mycoords[g.target(*e)].x << ' '
928 << mycoords[g.target(*e)].y << ' '
929 << _edgeColors[*e].red() << ' '
930 << _edgeColors[*e].green() << ' '
931 << _edgeColors[*e].blue() << ' '
932 << _edgeWidths[*e]*_edgeWidthScale << " lb\n";
934 sw+=_edgeWidths[*e]*_edgeWidthScale/2.0+_parEdgeDist;
938 else for(EdgeIt e(g);e!=INVALID;++e)
939 if((!_undirected||g.source(e)<g.target(e))&&_edgeWidths[e]>0
940 &&g.source(e)!=g.target(e))
942 xy<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]);
943 double rn=_nodeSizes[g.target(e)]*_nodeScale;
944 int node_shape=_nodeShapes[g.target(e)];
946 for(int i=0;i<INTERPOL_PREC;++i)
947 if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
949 double l=std::sqrt(d.normSquare());
952 os << l*(1-(t1+t2)/2) << ' '
953 << _edgeWidths[e]*_edgeWidthScale << ' '
954 << d.x << ' ' << d.y << ' '
955 << mycoords[g.source(e)].x << ' '
956 << mycoords[g.source(e)].y << ' '
957 << _edgeColors[e].red() << ' '
958 << _edgeColors[e].green() << ' '
959 << _edgeColors[e].blue() << " arr\n";
961 else os << mycoords[g.source(e)].x << ' '
962 << mycoords[g.source(e)].y << ' '
963 << mycoords[g.target(e)].x << ' '
964 << mycoords[g.target(e)].y << ' '
965 << _edgeColors[e].red() << ' '
966 << _edgeColors[e].green() << ' '
967 << _edgeColors[e].blue() << ' '
968 << _edgeWidths[e]*_edgeWidthScale << " l\n";
972 os << "%Nodes:\ngsave\n";
973 for(NodeIt n(g);n!=INVALID;++n) {
974 os << mycoords[n].x << ' ' << mycoords[n].y << ' '
975 << _nodeSizes[n]*_nodeScale << ' '
976 << _nodeColors[n].red() << ' '
977 << _nodeColors[n].green() << ' '
978 << _nodeColors[n].blue() << ' ';
979 switch(_nodeShapes[n]) {
989 os<< "nfemale";break;
996 os << "%Node texts:\ngsave\n";
997 os << "/fosi " << _nodeTextSize << " def\n";
998 os << "(Helvetica) findfont fosi scalefont setfont\n";
999 for(NodeIt n(g);n!=INVALID;++n) {
1000 switch(_nodeTextColorType) {
1002 os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n";
1005 os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n";
1008 os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n";
1011 os << "0 0 0 setrgbcolor\n";
1013 os << mycoords[n].x << ' ' << mycoords[n].y
1014 << " (" << _nodeTexts[n] << ") cshow\n";
1018 if(_showNodePsText) {
1019 os << "%Node PS blocks:\ngsave\n";
1020 for(NodeIt n(g);n!=INVALID;++n)
1021 os << mycoords[n].x << ' ' << mycoords[n].y
1022 << " moveto\n" << _nodePsTexts[n] << "\n";
1026 os << "grestore\nshowpage\n";
1029 if(_pleaseRemoveOsStream) {delete &os;}
1034 const int GraphToEps<T>::INTERPOL_PREC = 20;
1036 const double GraphToEps<T>::A4HEIGHT = 841.8897637795276;
1038 const double GraphToEps<T>::A4WIDTH = 595.275590551181;
1040 const double GraphToEps<T>::A4BORDER = 15;
1043 ///Generates an EPS file from a graph
1046 ///Generates an EPS file from a graph.
1047 ///\param g is a reference to the graph to be printed
1048 ///\param os is a reference to the output stream.
1049 ///By default it is <tt>std::cout</tt>
1051 ///This function also has a lot of
1052 ///\ref named-templ-func-param "named parameters",
1053 ///they are declared as the members of class \ref GraphToEps. The following
1054 ///example shows how to use these parameters.
1056 /// graphToEps(g,os).scale(10).coords(coords)
1057 /// .nodeScale(2).nodeSizes(sizes)
1058 /// .edgeWidthScale(.4).run();
1060 ///\warning Don't forget to put the \ref GraphToEps::run() "run()"
1061 ///to the end of the parameter list.
1063 ///\sa graphToEps(G &g, const char *file_name)
1065 GraphToEps<DefaultGraphToEpsTraits<G> >
1066 graphToEps(G &g, std::ostream& os=std::cout)
1069 GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
1072 ///Generates an EPS file from a graph
1075 ///This function does the same as
1076 ///\ref graphToEps(G &g,std::ostream& os)
1077 ///but it writes its output into the file \c file_name
1078 ///instead of a stream.
1079 ///\sa graphToEps(G &g, std::ostream& os)
1081 GraphToEps<DefaultGraphToEpsTraits<G> >
1082 graphToEps(G &g,const char *file_name)
1084 return GraphToEps<DefaultGraphToEpsTraits<G> >
1085 (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true));
1088 ///Generates an EPS file from a graph
1091 ///This function does the same as
1092 ///\ref graphToEps(G &g,std::ostream& os)
1093 ///but it writes its output into the file \c file_name
1094 ///instead of a stream.
1095 ///\sa graphToEps(G &g, std::ostream& os)
1097 GraphToEps<DefaultGraphToEpsTraits<G> >
1098 graphToEps(G &g,const std::string& file_name)
1100 return GraphToEps<DefaultGraphToEpsTraits<G> >
1101 (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name.c_str()),true));
1104 } //END OF NAMESPACE LEMON
1106 #endif // LEMON_GRAPH_TO_EPS_H