1 | /* -*- C++ -*- |
---|
2 | * src/lemon/graph_to_eps.h - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
6 | * |
---|
7 | * Permission to use, modify and distribute this software is granted |
---|
8 | * provided that this copyright notice appears in all copies. For |
---|
9 | * precise terms see the accompanying LICENSE file. |
---|
10 | * |
---|
11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
12 | * express or implied, and with no claim as to its suitability for any |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef LEMON_GRAPH_TO_EPS_H |
---|
18 | #define LEMON_GRAPH_TO_EPS_H |
---|
19 | |
---|
20 | #include<iostream> |
---|
21 | #include<fstream> |
---|
22 | #include<sstream> |
---|
23 | #include<algorithm> |
---|
24 | #include<vector> |
---|
25 | |
---|
26 | #include<lemon/xy.h> |
---|
27 | #include<lemon/maps.h> |
---|
28 | #include<lemon/bezier.h> |
---|
29 | |
---|
30 | ///\ingroup misc |
---|
31 | ///\file |
---|
32 | ///\brief Simple graph drawer |
---|
33 | /// |
---|
34 | ///\author Alpar Juttner |
---|
35 | |
---|
36 | namespace lemon { |
---|
37 | |
---|
38 | ///Data structure representing RGB colors. |
---|
39 | |
---|
40 | ///Data structure representing RGB colors. |
---|
41 | ///\ingroup misc |
---|
42 | class Color |
---|
43 | { |
---|
44 | double _r,_g,_b; |
---|
45 | public: |
---|
46 | ///Default constructor |
---|
47 | Color() {} |
---|
48 | ///Constructor |
---|
49 | Color(double r,double g,double b) :_r(r),_g(g),_b(b) {}; |
---|
50 | ///Returns the red component |
---|
51 | double getR() {return _r;} |
---|
52 | ///Returns the green component |
---|
53 | double getG() {return _g;} |
---|
54 | ///Returns the blue component |
---|
55 | double getB() {return _b;} |
---|
56 | ///Set the color components |
---|
57 | void set(double r,double g,double b) { _r=r;_g=g;_b=b; }; |
---|
58 | }; |
---|
59 | |
---|
60 | ///Default traits class of \ref GraphToEps |
---|
61 | |
---|
62 | ///Default traits class of \ref GraphToEps |
---|
63 | /// |
---|
64 | ///\c G is the type of the underlying graph. |
---|
65 | template<class G> |
---|
66 | struct DefaultGraphToEpsTraits |
---|
67 | { |
---|
68 | typedef G Graph; |
---|
69 | typedef typename Graph::Node Node; |
---|
70 | typedef typename Graph::NodeIt NodeIt; |
---|
71 | typedef typename Graph::Edge Edge; |
---|
72 | typedef typename Graph::EdgeIt EdgeIt; |
---|
73 | typedef typename Graph::InEdgeIt InEdgeIt; |
---|
74 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
75 | |
---|
76 | |
---|
77 | const Graph &g; |
---|
78 | |
---|
79 | std::ostream& os; |
---|
80 | |
---|
81 | ConstMap<typename Graph::Node,xy<double> > _coords; |
---|
82 | ConstMap<typename Graph::Node,double > _nodeSizes; |
---|
83 | ConstMap<typename Graph::Node,int > _nodeShapes; |
---|
84 | |
---|
85 | ConstMap<typename Graph::Node,Color > _nodeColors; |
---|
86 | ConstMap<typename Graph::Edge,Color > _edgeColors; |
---|
87 | |
---|
88 | ConstMap<typename Graph::Edge,double > _edgeWidths; |
---|
89 | |
---|
90 | double _edgeWidthScale; |
---|
91 | |
---|
92 | double _nodeScale; |
---|
93 | double _xBorder, _yBorder; |
---|
94 | double _scale; |
---|
95 | double _nodeBorderQuotient; |
---|
96 | |
---|
97 | bool _drawArrows; |
---|
98 | double _arrowLength, _arrowWidth; |
---|
99 | |
---|
100 | bool _showNodes, _showEdges; |
---|
101 | |
---|
102 | bool _enableParallel; |
---|
103 | double _parEdgeDist; |
---|
104 | |
---|
105 | bool _showNodeText; |
---|
106 | ConstMap<typename Graph::Node,bool > _nodeTexts; |
---|
107 | double _nodeTextSize; |
---|
108 | |
---|
109 | bool _showNodePsText; |
---|
110 | ConstMap<typename Graph::Node,bool > _nodePsTexts; |
---|
111 | char *_nodePsTextsPreamble; |
---|
112 | |
---|
113 | bool _undir; |
---|
114 | bool _pleaseRemoveOsStream; |
---|
115 | ///Constructor |
---|
116 | |
---|
117 | ///Constructor |
---|
118 | ///\param _g is a reference to the graph to be printed |
---|
119 | ///\param _os is a reference to the output stream. |
---|
120 | ///\param _os is a reference to the output stream. |
---|
121 | ///\param _pros If it is \c true, then the \c ostream referenced by \c _os |
---|
122 | ///will be explicitly deallocated by the destructor. |
---|
123 | ///By default it is <tt>std::cout</tt> |
---|
124 | DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout, |
---|
125 | bool _pros=false) : |
---|
126 | g(_g), os(_os), |
---|
127 | _coords(xy<double>(1,1)), _nodeSizes(1.0), _nodeShapes(0), |
---|
128 | _nodeColors(Color(1,1,1)), _edgeColors(Color(0,0,0)), |
---|
129 | _edgeWidths(1), _edgeWidthScale(0.3), |
---|
130 | _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0), |
---|
131 | _nodeBorderQuotient(.1), |
---|
132 | _drawArrows(false), _arrowLength(1), _arrowWidth(0.3), |
---|
133 | _showNodes(true), _showEdges(true), |
---|
134 | _enableParallel(false), _parEdgeDist(1), |
---|
135 | _showNodeText(false), _nodeTexts(false), _nodeTextSize(1), |
---|
136 | _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0), |
---|
137 | _undir(false), |
---|
138 | _pleaseRemoveOsStream(_pros) {} |
---|
139 | }; |
---|
140 | |
---|
141 | ///Helper class to implement the named parameters of \ref graphToEps() |
---|
142 | |
---|
143 | ///Helper class to implement the named parameters of \ref graphToEps() |
---|
144 | ///\todo Is 'helper class' a good name for this? |
---|
145 | /// |
---|
146 | template<class T> class GraphToEps : public T |
---|
147 | { |
---|
148 | typedef typename T::Graph Graph; |
---|
149 | typedef typename Graph::Node Node; |
---|
150 | typedef typename Graph::NodeIt NodeIt; |
---|
151 | typedef typename Graph::Edge Edge; |
---|
152 | typedef typename Graph::EdgeIt EdgeIt; |
---|
153 | typedef typename Graph::InEdgeIt InEdgeIt; |
---|
154 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
155 | |
---|
156 | bool dontPrint; |
---|
157 | |
---|
158 | enum NodeShapes { CIRCLE=0, SQUARE=1 }; |
---|
159 | |
---|
160 | class edgeLess { |
---|
161 | const Graph &g; |
---|
162 | public: |
---|
163 | edgeLess(const Graph &_g) : g(_g) {} |
---|
164 | bool operator()(Edge a,Edge b) const |
---|
165 | { |
---|
166 | Node ai=min(g.source(a),g.target(a)); |
---|
167 | Node aa=max(g.source(a),g.target(a)); |
---|
168 | Node bi=min(g.source(b),g.target(b)); |
---|
169 | Node ba=max(g.source(b),g.target(b)); |
---|
170 | return ai<bi || |
---|
171 | (ai==bi && (aa < ba || |
---|
172 | (aa==ba && ai==g.source(a) && bi==g.target(b)))); |
---|
173 | } |
---|
174 | }; |
---|
175 | bool isParallel(Edge e,Edge f) const |
---|
176 | { |
---|
177 | return (g.source(e)==g.source(f)&&g.target(e)==g.target(f))|| |
---|
178 | (g.source(e)==g.target(f)&&g.target(e)==g.source(f)); |
---|
179 | } |
---|
180 | static xy<double> rot(xy<double> v) |
---|
181 | { |
---|
182 | return xy<double>(v.y,-v.x); |
---|
183 | } |
---|
184 | template<class xy> |
---|
185 | static std::string psOut(const xy &p) |
---|
186 | { |
---|
187 | std::ostringstream os; |
---|
188 | os << p.x << ' ' << p.y; |
---|
189 | return os.str(); |
---|
190 | } |
---|
191 | |
---|
192 | public: |
---|
193 | GraphToEps(const T &t) : T(t), dontPrint(false) {}; |
---|
194 | |
---|
195 | template<class X> struct CoordsTraits : public T { |
---|
196 | const X &_coords; |
---|
197 | CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {} |
---|
198 | }; |
---|
199 | ///Sets the map of the node coordinates |
---|
200 | |
---|
201 | ///Sets the map of the node coordinates. |
---|
202 | ///\param x must be a node map with xy<double> or xy<int> values. |
---|
203 | template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) { |
---|
204 | dontPrint=true; |
---|
205 | return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x)); |
---|
206 | } |
---|
207 | template<class X> struct NodeSizesTraits : public T { |
---|
208 | const X &_nodeSizes; |
---|
209 | NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {} |
---|
210 | }; |
---|
211 | ///Sets the map of the node sizes |
---|
212 | |
---|
213 | ///Sets the map of the node sizes |
---|
214 | ///\param x must be a node map with \c double (or convertible) values. |
---|
215 | template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x) |
---|
216 | { |
---|
217 | dontPrint=true; |
---|
218 | return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x)); |
---|
219 | } |
---|
220 | template<class X> struct NodeShapesTraits : public T { |
---|
221 | const X &_nodeShapes; |
---|
222 | NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {} |
---|
223 | }; |
---|
224 | ///Sets the map of the node shapes |
---|
225 | |
---|
226 | ///Sets the map of the node shapes |
---|
227 | ///\param x must be a node map with \c int (or convertible) values. |
---|
228 | ///\todo Incomplete doc. |
---|
229 | template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x) |
---|
230 | { |
---|
231 | dontPrint=true; |
---|
232 | return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x)); |
---|
233 | } |
---|
234 | template<class X> struct NodeTextsTraits : public T { |
---|
235 | const X &_nodeTexts; |
---|
236 | NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {} |
---|
237 | }; |
---|
238 | ///Sets the text printed on the nodes |
---|
239 | |
---|
240 | ///Sets the text printed on the nodes |
---|
241 | ///\param x must be a node map with type that can be pushed to a standard |
---|
242 | ///ostream. |
---|
243 | template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x) |
---|
244 | { |
---|
245 | dontPrint=true; |
---|
246 | _showNodeText=true; |
---|
247 | return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x)); |
---|
248 | } |
---|
249 | template<class X> struct NodePsTextsTraits : public T { |
---|
250 | const X &_nodePsTexts; |
---|
251 | NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {} |
---|
252 | }; |
---|
253 | ///Inserts a PostScript block to the nodes |
---|
254 | |
---|
255 | ///With this command it is possible to insert a verbatim PostScript |
---|
256 | ///block to the nodes. |
---|
257 | ///The PS current point will be moved to the centre of the node before |
---|
258 | ///the PostScript block inserted. |
---|
259 | /// |
---|
260 | ///Before and after the block a newline character is inserted to you |
---|
261 | ///don't have to bother with the separators. |
---|
262 | /// |
---|
263 | ///\param x must be a node map with type that can be pushed to a standard |
---|
264 | ///ostream. |
---|
265 | /// |
---|
266 | ///\sa nodePsTextsPreamble() |
---|
267 | ///\todo Offer the choise not to move to the centre but pass the coordinates |
---|
268 | ///to the Postscript block inserted. |
---|
269 | template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x) |
---|
270 | { |
---|
271 | dontPrint=true; |
---|
272 | _showNodePsText=true; |
---|
273 | return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x)); |
---|
274 | } |
---|
275 | template<class X> struct EdgeWidthsTraits : public T { |
---|
276 | const X &_edgeWidths; |
---|
277 | EdgeWidthsTraits(const T &t,const X &x) : T(t), _edgeWidths(x) {} |
---|
278 | }; |
---|
279 | ///Sets the map of the edge widths |
---|
280 | |
---|
281 | ///Sets the map of the edge widths |
---|
282 | ///\param x must be a edge map with \c double (or convertible) values. |
---|
283 | template<class X> GraphToEps<EdgeWidthsTraits<X> > edgeWidths(const X &x) |
---|
284 | { |
---|
285 | dontPrint=true; |
---|
286 | return GraphToEps<EdgeWidthsTraits<X> >(EdgeWidthsTraits<X>(*this,x)); |
---|
287 | } |
---|
288 | |
---|
289 | template<class X> struct NodeColorsTraits : public T { |
---|
290 | const X &_nodeColors; |
---|
291 | NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {} |
---|
292 | }; |
---|
293 | ///Sets the map of the node colors |
---|
294 | |
---|
295 | ///Sets the map of the node colors |
---|
296 | ///\param x must be a node map with \ref Color values. |
---|
297 | template<class X> GraphToEps<NodeColorsTraits<X> > |
---|
298 | nodeColors(const X &x) |
---|
299 | { |
---|
300 | dontPrint=true; |
---|
301 | return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x)); |
---|
302 | } |
---|
303 | template<class X> struct EdgeColorsTraits : public T { |
---|
304 | const X &_edgeColors; |
---|
305 | EdgeColorsTraits(const T &t,const X &x) : T(t), _edgeColors(x) {} |
---|
306 | }; |
---|
307 | ///Sets the map of the edge colors |
---|
308 | |
---|
309 | ///Sets the map of the edge colors |
---|
310 | ///\param x must be a edge map with \ref Color values. |
---|
311 | template<class X> GraphToEps<EdgeColorsTraits<X> > |
---|
312 | edgeColors(const X &x) |
---|
313 | { |
---|
314 | dontPrint=true; |
---|
315 | return GraphToEps<EdgeColorsTraits<X> >(EdgeColorsTraits<X>(*this,x)); |
---|
316 | } |
---|
317 | ///Sets a global scale factor for node sizes |
---|
318 | |
---|
319 | ///Sets a global scale factor for node sizes |
---|
320 | /// |
---|
321 | GraphToEps<T> &nodeScale(double d) {_nodeScale=d;return *this;} |
---|
322 | ///Sets a global scale factor for edge widths |
---|
323 | |
---|
324 | ///Sets a global scale factor for edge widths |
---|
325 | /// |
---|
326 | GraphToEps<T> &edgeWidthScale(double d) {_edgeWidthScale=d;return *this;} |
---|
327 | ///Sets a global scale factor for the whole picture |
---|
328 | |
---|
329 | ///Sets a global scale factor for the whole picture |
---|
330 | /// |
---|
331 | GraphToEps<T> &scale(double d) {_scale=d;return *this;} |
---|
332 | ///Sets the width of the border around the picture |
---|
333 | |
---|
334 | ///Sets the width of the border around the picture |
---|
335 | /// |
---|
336 | GraphToEps<T> &border(double b) {_xBorder=_yBorder=b;return *this;} |
---|
337 | ///Sets the width of the border around the picture |
---|
338 | |
---|
339 | ///Sets the width of the border around the picture |
---|
340 | /// |
---|
341 | GraphToEps<T> &border(double x, double y) { |
---|
342 | _xBorder=x;_yBorder=y;return *this; |
---|
343 | } |
---|
344 | ///Sets whether to draw arrows |
---|
345 | |
---|
346 | ///Sets whether to draw arrows |
---|
347 | /// |
---|
348 | GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;} |
---|
349 | ///Sets the length of the arrowheads |
---|
350 | |
---|
351 | ///Sets the length of the arrowheads |
---|
352 | /// |
---|
353 | GraphToEps<T> &arrowLength(double d) {_arrowLength*=d;return *this;} |
---|
354 | ///Sets the width of the arrowheads |
---|
355 | |
---|
356 | ///Sets the width of the arrowheads |
---|
357 | /// |
---|
358 | GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;} |
---|
359 | |
---|
360 | ///Enables parallel edges |
---|
361 | |
---|
362 | ///Enables parallel edges |
---|
363 | ///\todo Partially implemented |
---|
364 | GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;} |
---|
365 | |
---|
366 | ///Sets the distance |
---|
367 | |
---|
368 | ///Sets the distance |
---|
369 | /// |
---|
370 | GraphToEps<T> &parEdgeDist(double d) {_parEdgeDist*=d;return *this;} |
---|
371 | |
---|
372 | ///Hides the edges |
---|
373 | |
---|
374 | ///Hides the edges |
---|
375 | /// |
---|
376 | GraphToEps<T> &hideEdges(bool b=true) {_showEdges=!b;return *this;} |
---|
377 | ///Hides the nodes |
---|
378 | |
---|
379 | ///Hides the nodes |
---|
380 | /// |
---|
381 | GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;} |
---|
382 | |
---|
383 | ///Sets the size of the node texts |
---|
384 | |
---|
385 | ///Sets the size of the node texts |
---|
386 | /// |
---|
387 | GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;} |
---|
388 | ///Gives a preamble block for node Postscript block. |
---|
389 | |
---|
390 | ///Gives a preamble block for node Postscript block. |
---|
391 | /// |
---|
392 | ///\sa nodePsTexts() |
---|
393 | GraphToEps<T> & nodePsTextsPreamble(const char *str) { |
---|
394 | _nodePsTextsPreamble=s ;return *this; |
---|
395 | } |
---|
396 | ///Sets whether the the graph is undirected |
---|
397 | |
---|
398 | ///Sets whether the the graph is undirected |
---|
399 | /// |
---|
400 | GraphToEps<T> &undir(bool b=true) {_undir=b;return *this;} |
---|
401 | ///Sets whether the the graph is directed |
---|
402 | |
---|
403 | ///Sets whether the the graph is directed. |
---|
404 | ///Use it to show the undirected edges as a pair of directed ones. |
---|
405 | GraphToEps<T> &bidir(bool b=true) {_undir=!b;return *this;} |
---|
406 | |
---|
407 | protected: |
---|
408 | bool isInsideNode(xy<double> p, double r,int t) |
---|
409 | { |
---|
410 | switch(t) { |
---|
411 | case CIRCLE: |
---|
412 | return p.normSquare()<=r*r; |
---|
413 | case SQUARE: |
---|
414 | return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r; |
---|
415 | } |
---|
416 | return false; |
---|
417 | } |
---|
418 | |
---|
419 | public: |
---|
420 | ~GraphToEps() |
---|
421 | { |
---|
422 | if(dontPrint) return; |
---|
423 | |
---|
424 | os << "%!PS-Adobe-2.0 EPSF-2.0\n"; |
---|
425 | //\todo: Chech whether the graph is empty. |
---|
426 | BoundingBox<double> bb; |
---|
427 | for(NodeIt n(g);n!=INVALID;++n) { |
---|
428 | double ns=_nodeSizes[n]*_nodeScale; |
---|
429 | xy<double> p(ns,ns); |
---|
430 | bb+=p+_coords[n]; |
---|
431 | bb+=-p+_coords[n]; |
---|
432 | } |
---|
433 | os << "%%BoundingBox: " |
---|
434 | << bb.left()* _scale-_xBorder << ' ' |
---|
435 | << bb.bottom()*_scale-_yBorder << ' ' |
---|
436 | << bb.right()* _scale+_xBorder << ' ' |
---|
437 | << bb.top()* _scale+_yBorder << '\n'; |
---|
438 | //x1 y1 x2 y2 x3 y3 cr cg cb w |
---|
439 | os << "/lb { setlinewidth setrgbcolor newpath moveto\n" |
---|
440 | << " 4 2 roll 1 index 1 index curveto stroke } bind def\n"; |
---|
441 | os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n"; |
---|
442 | //x y r |
---|
443 | os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n"; |
---|
444 | //x y r |
---|
445 | os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n" |
---|
446 | << " 2 index 1 index sub 2 index 2 index add lineto\n" |
---|
447 | << " 2 index 1 index sub 2 index 2 index sub lineto\n" |
---|
448 | << " 2 index 1 index add 2 index 2 index sub lineto\n" |
---|
449 | << " closepath pop pop pop} bind def\n"; |
---|
450 | // x y r cr cg cb |
---|
451 | os << "/nc { setrgbcolor 2 index 2 index 2 index c fill\n" |
---|
452 | << " 0 0 0 setrgbcolor dup " |
---|
453 | << _nodeBorderQuotient << " mul setlinewidth " |
---|
454 | << 1+_nodeBorderQuotient/2 << " div c stroke\n" |
---|
455 | << " } bind def\n"; |
---|
456 | os << "/nsq { setrgbcolor 2 index 2 index 2 index sq fill\n" |
---|
457 | << " 0 0 0 setrgbcolor dup " |
---|
458 | << _nodeBorderQuotient << " mul setlinewidth " |
---|
459 | << 1+_nodeBorderQuotient/2 << " div sq stroke\n" |
---|
460 | << " } bind def\n"; |
---|
461 | os << "/arrl " << _arrowLength << " def\n"; |
---|
462 | os << "/arrw " << _arrowWidth << " def\n"; |
---|
463 | // l dx_norm dy_norm |
---|
464 | os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n"; |
---|
465 | //len w dx_norm dy_norm x1 y1 cr cg cb |
---|
466 | os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n" |
---|
467 | << " /w exch def /len exch def\n" |
---|
468 | // << " 0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke" |
---|
469 | << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n" |
---|
470 | << " len w sub arrl sub dx dy lrl\n" |
---|
471 | << " arrw dy dx neg lrl\n" |
---|
472 | << " dx arrl w add mul dy w 2 div arrw add mul sub\n" |
---|
473 | << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n" |
---|
474 | << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n" |
---|
475 | << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n" |
---|
476 | << " arrw dy dx neg lrl\n" |
---|
477 | << " len w sub arrl sub neg dx dy lrl\n" |
---|
478 | << " closepath fill } bind def\n"; |
---|
479 | os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n" |
---|
480 | << " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n"; |
---|
481 | |
---|
482 | os << "\ngsave\n"; |
---|
483 | if(_scale!=1.0) os << _scale << " dup scale\n"; |
---|
484 | |
---|
485 | if(_showEdges) { |
---|
486 | os << "%Edges:\ngsave\n"; |
---|
487 | if(_enableParallel) { |
---|
488 | std::vector<Edge> el; |
---|
489 | for(EdgeIt e(g);e!=INVALID;++e) |
---|
490 | if(!_undir||g.source(e)<g.target(e)) el.push_back(e); |
---|
491 | sort(el.begin(),el.end(),edgeLess(g)); |
---|
492 | |
---|
493 | typename std::vector<Edge>::iterator j; |
---|
494 | for(typename std::vector<Edge>::iterator i=el.begin();i!=el.end();i=j) { |
---|
495 | for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ; |
---|
496 | |
---|
497 | double sw=0; |
---|
498 | for(typename std::vector<Edge>::iterator e=i;e!=j;++e) |
---|
499 | sw+=_edgeWidths[*e]*_edgeWidthScale+_parEdgeDist; |
---|
500 | sw-=_parEdgeDist; |
---|
501 | sw/=-2.0; |
---|
502 | xy<double> dvec(_coords[g.target(*i)]-_coords[g.source(*i)]); |
---|
503 | double l=sqrt(dvec.normSquare()); |
---|
504 | xy<double> d(dvec/l); |
---|
505 | xy<double> m; |
---|
506 | // m=xy<double>(_coords[g.target(*i)]+_coords[g.source(*i)])/2.0; |
---|
507 | |
---|
508 | // m=xy<double>(_coords[g.source(*i)])+ |
---|
509 | // dvec*(double(_nodeSizes[g.source(*i)])/ |
---|
510 | // (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)])); |
---|
511 | |
---|
512 | m=xy<double>(_coords[g.source(*i)])+ |
---|
513 | d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0; |
---|
514 | |
---|
515 | for(typename std::vector<Edge>::iterator e=i;e!=j;++e) { |
---|
516 | sw+=_edgeWidths[*e]*_edgeWidthScale/2.0; |
---|
517 | xy<double> mm=m+rot(d)*sw/.75; |
---|
518 | if(_drawArrows) { |
---|
519 | int node_shape; |
---|
520 | const int INERPOL_PREC=20; |
---|
521 | xy<double> s=_coords[g.source(*e)]; |
---|
522 | xy<double> t=_coords[g.target(*e)]; |
---|
523 | double rn=_nodeSizes[g.target(*e)]*_nodeScale; |
---|
524 | node_shape=_nodeShapes[g.target(*e)]; |
---|
525 | Bezier3 bez(s,mm,mm,t); |
---|
526 | double t1=0,t2=1; |
---|
527 | for(int i=0;i<INERPOL_PREC;++i) |
---|
528 | if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2; |
---|
529 | else t1=(t1+t2)/2; |
---|
530 | xy<double> apoint=bez((t1+t2)/2); |
---|
531 | rn = _arrowLength+_edgeWidths[*e]*_edgeWidthScale; |
---|
532 | rn*=rn; |
---|
533 | t2=(t1+t2)/2;t1=0; |
---|
534 | for(int i=0;i<INERPOL_PREC;++i) |
---|
535 | if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2; |
---|
536 | else t2=(t1+t2)/2; |
---|
537 | xy<double> linend=bez((t1+t2)/2); |
---|
538 | bez=bez.before((t1+t2)/2); |
---|
539 | // rn=_nodeSizes[g.source(*e)]*_nodeScale; |
---|
540 | // node_shape=_nodeShapes[g.source(*e)]; |
---|
541 | // t1=0;t2=1; |
---|
542 | // for(int i=0;i<INERPOL_PREC;++i) |
---|
543 | // if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t1=(t1+t2)/2; |
---|
544 | // else t2=(t1+t2)/2; |
---|
545 | // bez=bez.after((t1+t2)/2); |
---|
546 | os << _edgeWidths[*e]*_edgeWidthScale << " setlinewidth " |
---|
547 | << _edgeColors[*e].getR() << ' ' |
---|
548 | << _edgeColors[*e].getG() << ' ' |
---|
549 | << _edgeColors[*e].getB() << " setrgbcolor newpath\n" |
---|
550 | << bez.p1.x << ' ' << bez.p1.y << " moveto\n" |
---|
551 | << bez.p2.x << ' ' << bez.p2.y << ' ' |
---|
552 | << bez.p3.x << ' ' << bez.p3.y << ' ' |
---|
553 | << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n"; |
---|
554 | xy<double> dd(rot(linend-apoint)); |
---|
555 | dd*=(_edgeWidths[*e]*_edgeWidthScale+_arrowWidth)/ |
---|
556 | sqrt(dd.normSquare()); |
---|
557 | os << "newpath " << psOut(apoint) << " moveto " |
---|
558 | << psOut(linend+dd) << " lineto " |
---|
559 | << psOut(linend-dd) << " lineto closepath fill\n"; |
---|
560 | } |
---|
561 | else { |
---|
562 | os << _coords[g.source(*e)].x << ' ' |
---|
563 | << _coords[g.source(*e)].y << ' ' |
---|
564 | << mm.x << ' ' << mm.y << ' ' |
---|
565 | << _coords[g.target(*e)].x << ' ' |
---|
566 | << _coords[g.target(*e)].y << ' ' |
---|
567 | << _edgeColors[*e].getR() << ' ' |
---|
568 | << _edgeColors[*e].getG() << ' ' |
---|
569 | << _edgeColors[*e].getB() << ' ' |
---|
570 | << _edgeWidths[*e]*_edgeWidthScale << " lb\n"; |
---|
571 | } |
---|
572 | sw+=_edgeWidths[*e]*_edgeWidthScale/2.0+_parEdgeDist; |
---|
573 | } |
---|
574 | } |
---|
575 | } |
---|
576 | else for(EdgeIt e(g);e!=INVALID;++e) |
---|
577 | if(!_undir||g.source(e)<g.target(e)) |
---|
578 | if(_drawArrows) { |
---|
579 | xy<double> d(_coords[g.target(e)]-_coords[g.source(e)]); |
---|
580 | double l=sqrt(d.normSquare()); |
---|
581 | d/=l; |
---|
582 | xy<double> x1(d*_nodeScale*_nodeSizes[g.source(e)]+ |
---|
583 | _coords[g.source(e)]); |
---|
584 | os << l-(_nodeSizes[g.source(e)]+ |
---|
585 | _nodeSizes[g.target(e)])*_nodeScale << ' ' |
---|
586 | << _edgeWidths[e]*_edgeWidthScale << ' ' |
---|
587 | << d.x << ' ' << d.y << ' ' |
---|
588 | << x1.x << ' ' << x1.y << ' ' |
---|
589 | << _edgeColors[e].getR() << ' ' |
---|
590 | << _edgeColors[e].getG() << ' ' |
---|
591 | << _edgeColors[e].getB() << " arr\n"; |
---|
592 | } |
---|
593 | else os << _coords[g.source(e)].x << ' ' |
---|
594 | << _coords[g.source(e)].y << ' ' |
---|
595 | << _coords[g.target(e)].x << ' ' |
---|
596 | << _coords[g.target(e)].y << ' ' |
---|
597 | << _edgeColors[e].getR() << ' ' |
---|
598 | << _edgeColors[e].getG() << ' ' |
---|
599 | << _edgeColors[e].getB() << ' ' |
---|
600 | << _edgeWidths[e]*_edgeWidthScale << " l\n"; |
---|
601 | os << "grestore\n"; |
---|
602 | } |
---|
603 | if(_showNodes) { |
---|
604 | os << "%Nodes:\ngsave\n"; |
---|
605 | for(NodeIt n(g);n!=INVALID;++n) { |
---|
606 | os << _coords[n].x << ' ' << _coords[n].y << ' ' |
---|
607 | << _nodeSizes[n]*_nodeScale << ' ' |
---|
608 | << _nodeColors[n].getR() << ' ' |
---|
609 | << _nodeColors[n].getG() << ' ' |
---|
610 | << _nodeColors[n].getB() << ' '; |
---|
611 | switch(_nodeShapes[n]) { |
---|
612 | case CIRCLE: |
---|
613 | os<< "nc";break; |
---|
614 | case SQUARE: |
---|
615 | os<< "nsq";break; |
---|
616 | } |
---|
617 | os<<'\n'; |
---|
618 | } |
---|
619 | os << "grestore\n"; |
---|
620 | } |
---|
621 | if(_showNodeText) { |
---|
622 | os << "%Node texts:\ngsave\n"; |
---|
623 | os << "/fosi " << _nodeTextSize << " def\n"; |
---|
624 | os << "(Helvetica) findfont fosi scalefont setfont\n"; |
---|
625 | os << "0 0 0 setrgbcolor\n"; |
---|
626 | for(NodeIt n(g);n!=INVALID;++n) |
---|
627 | os << _coords[n].x << ' ' << _coords[n].y |
---|
628 | << " (" << _nodeTexts[n] << ") cshow\n"; |
---|
629 | os << "grestore\n"; |
---|
630 | } |
---|
631 | if(_showNodePsText) { |
---|
632 | os << "%Node PS blocks:\ngsave\n"; |
---|
633 | for(NodeIt n(g);n!=INVALID;++n) |
---|
634 | os << _coords[n].x << ' ' << _coords[n].y |
---|
635 | << " moveto\n" << _nodePsTexts[n] << "\n"; |
---|
636 | os << "grestore\n"; |
---|
637 | } |
---|
638 | |
---|
639 | os << "grestore\n"; |
---|
640 | |
---|
641 | //CleanUp: |
---|
642 | if(_pleaseRemoveOsStream) {delete &os;} |
---|
643 | } |
---|
644 | }; |
---|
645 | |
---|
646 | |
---|
647 | ///Generates an EPS file from a graph |
---|
648 | |
---|
649 | ///\ingroup misc |
---|
650 | ///Generates an EPS file from a graph. |
---|
651 | ///\param g is a reference to the graph to be printed |
---|
652 | ///\param os is a reference to the output stream. |
---|
653 | ///By default it is <tt>std::cout</tt> |
---|
654 | /// |
---|
655 | ///This function also has a lot of \ref named-templ-param "named parameters", |
---|
656 | ///they are declared as the members of class \ref GraphToEps. The following |
---|
657 | ///example shows how to use these parameters. |
---|
658 | ///\code |
---|
659 | /// graphToEps(g).scale(10).coords(coords) |
---|
660 | /// .nodeScale(2).nodeSizes(sizes) |
---|
661 | /// .edgeWidthScale(.4); |
---|
662 | ///\endcode |
---|
663 | ///\sa GraphToEps |
---|
664 | ///\sa graphToEps(G &g, char *file_name) |
---|
665 | template<class G> |
---|
666 | GraphToEps<DefaultGraphToEpsTraits<G> > |
---|
667 | graphToEps(G &g, std::ostream& os=std::cout) |
---|
668 | { |
---|
669 | return |
---|
670 | GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os)); |
---|
671 | } |
---|
672 | |
---|
673 | ///Generates an EPS file from a graph |
---|
674 | |
---|
675 | //\ingroup misc |
---|
676 | ///This function does the same as |
---|
677 | ///\ref graphToEps(G &g,std::ostream& os) |
---|
678 | ///but it writes its output into the file \c file_name |
---|
679 | ///instead of a stream. |
---|
680 | ///\sa graphToEps(G &g, std::ostream& os) |
---|
681 | template<class G> |
---|
682 | GraphToEps<DefaultGraphToEpsTraits<G> > |
---|
683 | graphToEps(G &g,char *file_name) |
---|
684 | { |
---|
685 | return GraphToEps<DefaultGraphToEpsTraits<G> > |
---|
686 | (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true)); |
---|
687 | } |
---|
688 | |
---|
689 | } //END OF NAMESPACE LEMON |
---|
690 | |
---|
691 | #endif // LEMON_GRAPH_TO_EPS_H |
---|