1 | /* -*- C++ -*- |
---|
2 | * |
---|
3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
4 | * |
---|
5 | * Copyright (C) 2003-2008 |
---|
6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
8 | * |
---|
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. |
---|
12 | * |
---|
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 |
---|
15 | * purpose. |
---|
16 | * |
---|
17 | */ |
---|
18 | |
---|
19 | #ifndef LEMON_GRAPH_TO_EPS_H |
---|
20 | #define LEMON_GRAPH_TO_EPS_H |
---|
21 | |
---|
22 | #include <sys/time.h> |
---|
23 | |
---|
24 | #ifdef WIN32 |
---|
25 | #include <lemon/bits/mingw32_time.h> |
---|
26 | #endif |
---|
27 | |
---|
28 | #include<iostream> |
---|
29 | #include<fstream> |
---|
30 | #include<sstream> |
---|
31 | #include<algorithm> |
---|
32 | #include<vector> |
---|
33 | |
---|
34 | #include<ctime> |
---|
35 | |
---|
36 | #include<lemon/math.h> |
---|
37 | #include<lemon/bits/invalid.h> |
---|
38 | #include<lemon/dim2.h> |
---|
39 | #include<lemon/maps.h> |
---|
40 | #include<lemon/color.h> |
---|
41 | #include<lemon/bits/bezier.h> |
---|
42 | |
---|
43 | |
---|
44 | ///\ingroup eps_io |
---|
45 | ///\file |
---|
46 | ///\brief Simple graph drawer |
---|
47 | /// |
---|
48 | ///\author Alpar Juttner |
---|
49 | |
---|
50 | namespace lemon { |
---|
51 | |
---|
52 | namespace _graph_to_eps_bits { |
---|
53 | template<class MT> |
---|
54 | class _NegY { |
---|
55 | public: |
---|
56 | typedef typename MT::Key Key; |
---|
57 | typedef typename MT::Value Value; |
---|
58 | const MT ↦ |
---|
59 | int yscale; |
---|
60 | _NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {} |
---|
61 | Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);} |
---|
62 | }; |
---|
63 | } |
---|
64 | |
---|
65 | ///Default traits class of \ref GraphToEps |
---|
66 | |
---|
67 | ///Default traits class of \ref GraphToEps |
---|
68 | /// |
---|
69 | ///\c G is the type of the underlying graph. |
---|
70 | template<class G> |
---|
71 | struct DefaultGraphToEpsTraits |
---|
72 | { |
---|
73 | typedef G Graph; |
---|
74 | typedef typename Graph::Node Node; |
---|
75 | typedef typename Graph::NodeIt NodeIt; |
---|
76 | typedef typename Graph::Arc Arc; |
---|
77 | typedef typename Graph::ArcIt ArcIt; |
---|
78 | typedef typename Graph::InArcIt InArcIt; |
---|
79 | typedef typename Graph::OutArcIt OutArcIt; |
---|
80 | |
---|
81 | |
---|
82 | const Graph &g; |
---|
83 | |
---|
84 | std::ostream& os; |
---|
85 | |
---|
86 | typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType; |
---|
87 | CoordsMapType _coords; |
---|
88 | ConstMap<typename Graph::Node,double > _nodeSizes; |
---|
89 | ConstMap<typename Graph::Node,int > _nodeShapes; |
---|
90 | |
---|
91 | ConstMap<typename Graph::Node,Color > _nodeColors; |
---|
92 | ConstMap<typename Graph::Arc,Color > _arcColors; |
---|
93 | |
---|
94 | ConstMap<typename Graph::Arc,double > _arcWidths; |
---|
95 | |
---|
96 | double _arcWidthScale; |
---|
97 | |
---|
98 | double _nodeScale; |
---|
99 | double _xBorder, _yBorder; |
---|
100 | double _scale; |
---|
101 | double _nodeBorderQuotient; |
---|
102 | |
---|
103 | bool _drawArrows; |
---|
104 | double _arrowLength, _arrowWidth; |
---|
105 | |
---|
106 | bool _showNodes, _showArcs; |
---|
107 | |
---|
108 | bool _enableParallel; |
---|
109 | double _parArcDist; |
---|
110 | |
---|
111 | bool _showNodeText; |
---|
112 | ConstMap<typename Graph::Node,bool > _nodeTexts; |
---|
113 | double _nodeTextSize; |
---|
114 | |
---|
115 | bool _showNodePsText; |
---|
116 | ConstMap<typename Graph::Node,bool > _nodePsTexts; |
---|
117 | char *_nodePsTextsPreamble; |
---|
118 | |
---|
119 | bool _undirected; |
---|
120 | |
---|
121 | bool _pleaseRemoveOsStream; |
---|
122 | |
---|
123 | bool _scaleToA4; |
---|
124 | |
---|
125 | std::string _title; |
---|
126 | std::string _copyright; |
---|
127 | |
---|
128 | enum NodeTextColorType |
---|
129 | { DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType; |
---|
130 | ConstMap<typename Graph::Node,Color > _nodeTextColors; |
---|
131 | |
---|
132 | bool _autoNodeScale; |
---|
133 | bool _autoArcWidthScale; |
---|
134 | |
---|
135 | bool _absoluteNodeSizes; |
---|
136 | bool _absoluteArcWidths; |
---|
137 | |
---|
138 | bool _negY; |
---|
139 | |
---|
140 | bool _preScale; |
---|
141 | ///Constructor |
---|
142 | |
---|
143 | ///Constructor |
---|
144 | ///\param _g is a reference to the graph to be printed |
---|
145 | ///\param _os is a reference to the output stream. |
---|
146 | ///\param _os is a reference to the output stream. |
---|
147 | ///\param _pros If it is \c true, then the \c ostream referenced by \c _os |
---|
148 | ///will be explicitly deallocated by the destructor. |
---|
149 | ///By default it is <tt>std::cout</tt> |
---|
150 | DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout, |
---|
151 | bool _pros=false) : |
---|
152 | g(_g), os(_os), |
---|
153 | _coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0), |
---|
154 | _nodeColors(WHITE), _arcColors(BLACK), |
---|
155 | _arcWidths(1.0), _arcWidthScale(0.003), |
---|
156 | _nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0), |
---|
157 | _nodeBorderQuotient(.1), |
---|
158 | _drawArrows(false), _arrowLength(1), _arrowWidth(0.3), |
---|
159 | _showNodes(true), _showArcs(true), |
---|
160 | _enableParallel(false), _parArcDist(1), |
---|
161 | _showNodeText(false), _nodeTexts(false), _nodeTextSize(1), |
---|
162 | _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0), |
---|
163 | _undirected(lemon::UndirectedTagIndicator<G>::value), |
---|
164 | _pleaseRemoveOsStream(_pros), _scaleToA4(false), |
---|
165 | _nodeTextColorType(SAME_COL), _nodeTextColors(BLACK), |
---|
166 | _autoNodeScale(false), |
---|
167 | _autoArcWidthScale(false), |
---|
168 | _absoluteNodeSizes(false), |
---|
169 | _absoluteArcWidths(false), |
---|
170 | _negY(false), |
---|
171 | _preScale(true) |
---|
172 | {} |
---|
173 | }; |
---|
174 | |
---|
175 | ///Helper class to implement the named parameters of \ref graphToEps() |
---|
176 | |
---|
177 | ///Helper class to implement the named parameters of \ref graphToEps() |
---|
178 | ///\todo Is 'helper class' a good name for this? |
---|
179 | /// |
---|
180 | ///\todo Follow PostScript's DSC. |
---|
181 | /// Use own dictionary. |
---|
182 | ///\todo Useful new features. |
---|
183 | /// - Linestyles: dotted, dashed etc. |
---|
184 | /// - A second color and percent value for the lines. |
---|
185 | template<class T> class GraphToEps : public T |
---|
186 | { |
---|
187 | // Can't believe it is required by the C++ standard |
---|
188 | using T::g; |
---|
189 | using T::os; |
---|
190 | |
---|
191 | using T::_coords; |
---|
192 | using T::_nodeSizes; |
---|
193 | using T::_nodeShapes; |
---|
194 | using T::_nodeColors; |
---|
195 | using T::_arcColors; |
---|
196 | using T::_arcWidths; |
---|
197 | |
---|
198 | using T::_arcWidthScale; |
---|
199 | using T::_nodeScale; |
---|
200 | using T::_xBorder; |
---|
201 | using T::_yBorder; |
---|
202 | using T::_scale; |
---|
203 | using T::_nodeBorderQuotient; |
---|
204 | |
---|
205 | using T::_drawArrows; |
---|
206 | using T::_arrowLength; |
---|
207 | using T::_arrowWidth; |
---|
208 | |
---|
209 | using T::_showNodes; |
---|
210 | using T::_showArcs; |
---|
211 | |
---|
212 | using T::_enableParallel; |
---|
213 | using T::_parArcDist; |
---|
214 | |
---|
215 | using T::_showNodeText; |
---|
216 | using T::_nodeTexts; |
---|
217 | using T::_nodeTextSize; |
---|
218 | |
---|
219 | using T::_showNodePsText; |
---|
220 | using T::_nodePsTexts; |
---|
221 | using T::_nodePsTextsPreamble; |
---|
222 | |
---|
223 | using T::_undirected; |
---|
224 | |
---|
225 | using T::_pleaseRemoveOsStream; |
---|
226 | |
---|
227 | using T::_scaleToA4; |
---|
228 | |
---|
229 | using T::_title; |
---|
230 | using T::_copyright; |
---|
231 | |
---|
232 | using T::NodeTextColorType; |
---|
233 | using T::CUST_COL; |
---|
234 | using T::DIST_COL; |
---|
235 | using T::DIST_BW; |
---|
236 | using T::_nodeTextColorType; |
---|
237 | using T::_nodeTextColors; |
---|
238 | |
---|
239 | using T::_autoNodeScale; |
---|
240 | using T::_autoArcWidthScale; |
---|
241 | |
---|
242 | using T::_absoluteNodeSizes; |
---|
243 | using T::_absoluteArcWidths; |
---|
244 | |
---|
245 | |
---|
246 | using T::_negY; |
---|
247 | using T::_preScale; |
---|
248 | |
---|
249 | // dradnats ++C eht yb deriuqer si ti eveileb t'naC |
---|
250 | |
---|
251 | typedef typename T::Graph Graph; |
---|
252 | typedef typename Graph::Node Node; |
---|
253 | typedef typename Graph::NodeIt NodeIt; |
---|
254 | typedef typename Graph::Arc Arc; |
---|
255 | typedef typename Graph::ArcIt ArcIt; |
---|
256 | typedef typename Graph::InArcIt InArcIt; |
---|
257 | typedef typename Graph::OutArcIt OutArcIt; |
---|
258 | |
---|
259 | static const int INTERPOL_PREC; |
---|
260 | static const double A4HEIGHT; |
---|
261 | static const double A4WIDTH; |
---|
262 | static const double A4BORDER; |
---|
263 | |
---|
264 | bool dontPrint; |
---|
265 | |
---|
266 | public: |
---|
267 | ///Node shapes |
---|
268 | |
---|
269 | ///Node shapes |
---|
270 | /// |
---|
271 | enum NodeShapes { |
---|
272 | /// = 0 |
---|
273 | ///\image html nodeshape_0.png |
---|
274 | ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm |
---|
275 | CIRCLE=0, |
---|
276 | /// = 1 |
---|
277 | ///\image html nodeshape_1.png |
---|
278 | ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm |
---|
279 | /// |
---|
280 | SQUARE=1, |
---|
281 | /// = 2 |
---|
282 | ///\image html nodeshape_2.png |
---|
283 | ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm |
---|
284 | /// |
---|
285 | DIAMOND=2, |
---|
286 | /// = 3 |
---|
287 | ///\image html nodeshape_3.png |
---|
288 | ///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm |
---|
289 | /// |
---|
290 | MALE=3, |
---|
291 | /// = 4 |
---|
292 | ///\image html nodeshape_4.png |
---|
293 | ///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm |
---|
294 | /// |
---|
295 | FEMALE=4 |
---|
296 | }; |
---|
297 | |
---|
298 | private: |
---|
299 | class arcLess { |
---|
300 | const Graph &g; |
---|
301 | public: |
---|
302 | arcLess(const Graph &_g) : g(_g) {} |
---|
303 | bool operator()(Arc a,Arc b) const |
---|
304 | { |
---|
305 | Node ai=std::min(g.source(a),g.target(a)); |
---|
306 | Node aa=std::max(g.source(a),g.target(a)); |
---|
307 | Node bi=std::min(g.source(b),g.target(b)); |
---|
308 | Node ba=std::max(g.source(b),g.target(b)); |
---|
309 | return ai<bi || |
---|
310 | (ai==bi && (aa < ba || |
---|
311 | (aa==ba && ai==g.source(a) && bi==g.target(b)))); |
---|
312 | } |
---|
313 | }; |
---|
314 | bool isParallel(Arc e,Arc f) const |
---|
315 | { |
---|
316 | return (g.source(e)==g.source(f)&& |
---|
317 | g.target(e)==g.target(f)) || |
---|
318 | (g.source(e)==g.target(f)&& |
---|
319 | g.target(e)==g.source(f)); |
---|
320 | } |
---|
321 | template<class TT> |
---|
322 | static std::string psOut(const dim2::Point<TT> &p) |
---|
323 | { |
---|
324 | std::ostringstream os; |
---|
325 | os << p.x << ' ' << p.y; |
---|
326 | return os.str(); |
---|
327 | } |
---|
328 | static std::string psOut(const Color &c) |
---|
329 | { |
---|
330 | std::ostringstream os; |
---|
331 | os << c.red() << ' ' << c.green() << ' ' << c.blue(); |
---|
332 | return os.str(); |
---|
333 | } |
---|
334 | |
---|
335 | public: |
---|
336 | GraphToEps(const T &t) : T(t), dontPrint(false) {}; |
---|
337 | |
---|
338 | template<class X> struct CoordsTraits : public T { |
---|
339 | typedef X CoordsMapType; |
---|
340 | const X &_coords; |
---|
341 | CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {} |
---|
342 | }; |
---|
343 | ///Sets the map of the node coordinates |
---|
344 | |
---|
345 | ///Sets the map of the node coordinates. |
---|
346 | ///\param x must be a node map with dim2::Point<double> or |
---|
347 | ///\ref dim2::Point "dim2::Point<int>" values. |
---|
348 | template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) { |
---|
349 | dontPrint=true; |
---|
350 | return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x)); |
---|
351 | } |
---|
352 | template<class X> struct NodeSizesTraits : public T { |
---|
353 | const X &_nodeSizes; |
---|
354 | NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {} |
---|
355 | }; |
---|
356 | ///Sets the map of the node sizes |
---|
357 | |
---|
358 | ///Sets the map of the node sizes |
---|
359 | ///\param x must be a node map with \c double (or convertible) values. |
---|
360 | template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x) |
---|
361 | { |
---|
362 | dontPrint=true; |
---|
363 | return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x)); |
---|
364 | } |
---|
365 | template<class X> struct NodeShapesTraits : public T { |
---|
366 | const X &_nodeShapes; |
---|
367 | NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {} |
---|
368 | }; |
---|
369 | ///Sets the map of the node shapes |
---|
370 | |
---|
371 | ///Sets the map of the node shapes. |
---|
372 | ///The availabe shape values |
---|
373 | ///can be found in \ref NodeShapes "enum NodeShapes". |
---|
374 | ///\param x must be a node map with \c int (or convertible) values. |
---|
375 | ///\sa NodeShapes |
---|
376 | template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x) |
---|
377 | { |
---|
378 | dontPrint=true; |
---|
379 | return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x)); |
---|
380 | } |
---|
381 | template<class X> struct NodeTextsTraits : public T { |
---|
382 | const X &_nodeTexts; |
---|
383 | NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {} |
---|
384 | }; |
---|
385 | ///Sets the text printed on the nodes |
---|
386 | |
---|
387 | ///Sets the text printed on the nodes |
---|
388 | ///\param x must be a node map with type that can be pushed to a standard |
---|
389 | ///ostream. |
---|
390 | template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x) |
---|
391 | { |
---|
392 | dontPrint=true; |
---|
393 | _showNodeText=true; |
---|
394 | return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x)); |
---|
395 | } |
---|
396 | template<class X> struct NodePsTextsTraits : public T { |
---|
397 | const X &_nodePsTexts; |
---|
398 | NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {} |
---|
399 | }; |
---|
400 | ///Inserts a PostScript block to the nodes |
---|
401 | |
---|
402 | ///With this command it is possible to insert a verbatim PostScript |
---|
403 | ///block to the nodes. |
---|
404 | ///The PS current point will be moved to the centre of the node before |
---|
405 | ///the PostScript block inserted. |
---|
406 | /// |
---|
407 | ///Before and after the block a newline character is inserted so you |
---|
408 | ///don't have to bother with the separators. |
---|
409 | /// |
---|
410 | ///\param x must be a node map with type that can be pushed to a standard |
---|
411 | ///ostream. |
---|
412 | /// |
---|
413 | ///\sa nodePsTextsPreamble() |
---|
414 | ///\todo Offer the choise not to move to the centre but pass the coordinates |
---|
415 | ///to the Postscript block inserted. |
---|
416 | template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x) |
---|
417 | { |
---|
418 | dontPrint=true; |
---|
419 | _showNodePsText=true; |
---|
420 | return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x)); |
---|
421 | } |
---|
422 | template<class X> struct ArcWidthsTraits : public T { |
---|
423 | const X &_arcWidths; |
---|
424 | ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {} |
---|
425 | }; |
---|
426 | ///Sets the map of the arc widths |
---|
427 | |
---|
428 | ///Sets the map of the arc widths |
---|
429 | ///\param x must be a arc map with \c double (or convertible) values. |
---|
430 | template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x) |
---|
431 | { |
---|
432 | dontPrint=true; |
---|
433 | return GraphToEps<ArcWidthsTraits<X> >(ArcWidthsTraits<X>(*this,x)); |
---|
434 | } |
---|
435 | |
---|
436 | template<class X> struct NodeColorsTraits : public T { |
---|
437 | const X &_nodeColors; |
---|
438 | NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {} |
---|
439 | }; |
---|
440 | ///Sets the map of the node colors |
---|
441 | |
---|
442 | ///Sets the map of the node colors |
---|
443 | ///\param x must be a node map with \ref Color values. |
---|
444 | /// |
---|
445 | ///\sa Palette |
---|
446 | template<class X> GraphToEps<NodeColorsTraits<X> > |
---|
447 | nodeColors(const X &x) |
---|
448 | { |
---|
449 | dontPrint=true; |
---|
450 | return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x)); |
---|
451 | } |
---|
452 | template<class X> struct NodeTextColorsTraits : public T { |
---|
453 | const X &_nodeTextColors; |
---|
454 | NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {} |
---|
455 | }; |
---|
456 | ///Sets the map of the node text colors |
---|
457 | |
---|
458 | ///Sets the map of the node text colors |
---|
459 | ///\param x must be a node map with \ref Color values. |
---|
460 | /// |
---|
461 | ///\sa Palette |
---|
462 | template<class X> GraphToEps<NodeTextColorsTraits<X> > |
---|
463 | nodeTextColors(const X &x) |
---|
464 | { |
---|
465 | dontPrint=true; |
---|
466 | _nodeTextColorType=CUST_COL; |
---|
467 | return GraphToEps<NodeTextColorsTraits<X> > |
---|
468 | (NodeTextColorsTraits<X>(*this,x)); |
---|
469 | } |
---|
470 | template<class X> struct ArcColorsTraits : public T { |
---|
471 | const X &_arcColors; |
---|
472 | ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {} |
---|
473 | }; |
---|
474 | ///Sets the map of the arc colors |
---|
475 | |
---|
476 | ///Sets the map of the arc colors |
---|
477 | ///\param x must be a arc map with \ref Color values. |
---|
478 | /// |
---|
479 | ///\sa Palette |
---|
480 | template<class X> GraphToEps<ArcColorsTraits<X> > |
---|
481 | arcColors(const X &x) |
---|
482 | { |
---|
483 | dontPrint=true; |
---|
484 | return GraphToEps<ArcColorsTraits<X> >(ArcColorsTraits<X>(*this,x)); |
---|
485 | } |
---|
486 | ///Sets a global scale factor for node sizes |
---|
487 | |
---|
488 | ///Sets a global scale factor for node sizes. |
---|
489 | /// |
---|
490 | /// If nodeSizes() is not given, this function simply sets the node |
---|
491 | /// sizes to \c d. If nodeSizes() is given, but |
---|
492 | /// autoNodeScale() is not, then the node size given by |
---|
493 | /// nodeSizes() will be multiplied by the value \c d. |
---|
494 | /// If both nodeSizes() and autoNodeScale() are used, then the |
---|
495 | /// node sizes will be scaled in such a way that the greatest size will be |
---|
496 | /// equal to \c d. |
---|
497 | /// \sa nodeSizes() |
---|
498 | /// \sa autoNodeScale() |
---|
499 | GraphToEps<T> &nodeScale(double d=.01) {_nodeScale=d;return *this;} |
---|
500 | ///Turns on/off the automatic node width scaling. |
---|
501 | |
---|
502 | ///Turns on/off the automatic node width scaling. |
---|
503 | /// |
---|
504 | ///\sa nodeScale() |
---|
505 | /// |
---|
506 | GraphToEps<T> &autoNodeScale(bool b=true) { |
---|
507 | _autoNodeScale=b;return *this; |
---|
508 | } |
---|
509 | |
---|
510 | ///Turns on/off the absolutematic node width scaling. |
---|
511 | |
---|
512 | ///Turns on/off the absolutematic node width scaling. |
---|
513 | /// |
---|
514 | ///\sa nodeScale() |
---|
515 | /// |
---|
516 | GraphToEps<T> &absoluteNodeSizes(bool b=true) { |
---|
517 | _absoluteNodeSizes=b;return *this; |
---|
518 | } |
---|
519 | |
---|
520 | ///Negates the Y coordinates. |
---|
521 | |
---|
522 | ///Negates the Y coordinates. |
---|
523 | /// |
---|
524 | ///\todo More docs. |
---|
525 | /// |
---|
526 | GraphToEps<T> &negateY(bool b=true) { |
---|
527 | _negY=b;return *this; |
---|
528 | } |
---|
529 | |
---|
530 | ///Turn on/off prescaling |
---|
531 | |
---|
532 | ///By default graphToEps() rescales the whole image in order to avoid |
---|
533 | ///very big or very small bounding boxes. |
---|
534 | /// |
---|
535 | ///This (p)rescaling can be turned off with this function. |
---|
536 | /// |
---|
537 | GraphToEps<T> &preScale(bool b=true) { |
---|
538 | _preScale=b;return *this; |
---|
539 | } |
---|
540 | |
---|
541 | ///Sets a global scale factor for arc widths |
---|
542 | |
---|
543 | /// Sets a global scale factor for arc widths. |
---|
544 | /// |
---|
545 | /// If arcWidths() is not given, this function simply sets the arc |
---|
546 | /// widths to \c d. If arcWidths() is given, but |
---|
547 | /// autoArcWidthScale() is not, then the arc withs given by |
---|
548 | /// arcWidths() will be multiplied by the value \c d. |
---|
549 | /// If both arcWidths() and autoArcWidthScale() are used, then the |
---|
550 | /// arc withs will be scaled in such a way that the greatest width will be |
---|
551 | /// equal to \c d. |
---|
552 | GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;} |
---|
553 | ///Turns on/off the automatic arc width scaling. |
---|
554 | |
---|
555 | ///Turns on/off the automatic arc width scaling. |
---|
556 | /// |
---|
557 | ///\sa arcWidthScale() |
---|
558 | /// |
---|
559 | GraphToEps<T> &autoArcWidthScale(bool b=true) { |
---|
560 | _autoArcWidthScale=b;return *this; |
---|
561 | } |
---|
562 | ///Turns on/off the absolutematic arc width scaling. |
---|
563 | |
---|
564 | ///Turns on/off the absolutematic arc width scaling. |
---|
565 | /// |
---|
566 | ///\sa arcWidthScale() |
---|
567 | /// |
---|
568 | GraphToEps<T> &absoluteArcWidths(bool b=true) { |
---|
569 | _absoluteArcWidths=b;return *this; |
---|
570 | } |
---|
571 | ///Sets a global scale factor for the whole picture |
---|
572 | |
---|
573 | ///Sets a global scale factor for the whole picture |
---|
574 | /// |
---|
575 | |
---|
576 | GraphToEps<T> &scale(double d) {_scale=d;return *this;} |
---|
577 | ///Sets the width of the border around the picture |
---|
578 | |
---|
579 | ///Sets the width of the border around the picture |
---|
580 | /// |
---|
581 | GraphToEps<T> &border(double b) {_xBorder=_yBorder=b;return *this;} |
---|
582 | ///Sets the width of the border around the picture |
---|
583 | |
---|
584 | ///Sets the width of the border around the picture |
---|
585 | /// |
---|
586 | GraphToEps<T> &border(double x, double y) { |
---|
587 | _xBorder=x;_yBorder=y;return *this; |
---|
588 | } |
---|
589 | ///Sets whether to draw arrows |
---|
590 | |
---|
591 | ///Sets whether to draw arrows |
---|
592 | /// |
---|
593 | GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;} |
---|
594 | ///Sets the length of the arrowheads |
---|
595 | |
---|
596 | ///Sets the length of the arrowheads |
---|
597 | /// |
---|
598 | GraphToEps<T> &arrowLength(double d) {_arrowLength*=d;return *this;} |
---|
599 | ///Sets the width of the arrowheads |
---|
600 | |
---|
601 | ///Sets the width of the arrowheads |
---|
602 | /// |
---|
603 | GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;} |
---|
604 | |
---|
605 | ///Scales the drawing to fit to A4 page |
---|
606 | |
---|
607 | ///Scales the drawing to fit to A4 page |
---|
608 | /// |
---|
609 | GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;} |
---|
610 | |
---|
611 | ///Enables parallel arcs |
---|
612 | |
---|
613 | ///Enables parallel arcs |
---|
614 | GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;} |
---|
615 | |
---|
616 | ///Sets the distance |
---|
617 | |
---|
618 | ///Sets the distance |
---|
619 | /// |
---|
620 | GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;} |
---|
621 | |
---|
622 | ///Hides the arcs |
---|
623 | |
---|
624 | ///Hides the arcs |
---|
625 | /// |
---|
626 | GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;} |
---|
627 | ///Hides the nodes |
---|
628 | |
---|
629 | ///Hides the nodes |
---|
630 | /// |
---|
631 | GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;} |
---|
632 | |
---|
633 | ///Sets the size of the node texts |
---|
634 | |
---|
635 | ///Sets the size of the node texts |
---|
636 | /// |
---|
637 | GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;} |
---|
638 | |
---|
639 | ///Sets the color of the node texts to be different from the node color |
---|
640 | |
---|
641 | ///Sets the color of the node texts to be as different from the node color |
---|
642 | ///as it is possible |
---|
643 | /// |
---|
644 | GraphToEps<T> &distantColorNodeTexts() |
---|
645 | {_nodeTextColorType=DIST_COL;return *this;} |
---|
646 | ///Sets the color of the node texts to be black or white and always visible. |
---|
647 | |
---|
648 | ///Sets the color of the node texts to be black or white according to |
---|
649 | ///which is more |
---|
650 | ///different from the node color |
---|
651 | /// |
---|
652 | GraphToEps<T> &distantBWNodeTexts() |
---|
653 | {_nodeTextColorType=DIST_BW;return *this;} |
---|
654 | |
---|
655 | ///Gives a preamble block for node Postscript block. |
---|
656 | |
---|
657 | ///Gives a preamble block for node Postscript block. |
---|
658 | /// |
---|
659 | ///\sa nodePsTexts() |
---|
660 | GraphToEps<T> & nodePsTextsPreamble(const char *str) { |
---|
661 | _nodePsTextsPreamble=str ;return *this; |
---|
662 | } |
---|
663 | ///Sets whether the the graph is undirected |
---|
664 | |
---|
665 | ///Sets whether the the graph is undirected |
---|
666 | /// |
---|
667 | GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;} |
---|
668 | |
---|
669 | ///Sets whether the the graph is directed |
---|
670 | |
---|
671 | ///Sets whether the the graph is directed. |
---|
672 | ///Use it to show the edges as a pair of directed ones. |
---|
673 | GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;} |
---|
674 | |
---|
675 | ///Sets the title. |
---|
676 | |
---|
677 | ///Sets the title of the generated image, |
---|
678 | ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of |
---|
679 | ///the EPS file. |
---|
680 | GraphToEps<T> &title(const std::string &t) {_title=t;return *this;} |
---|
681 | ///Sets the copyright statement. |
---|
682 | |
---|
683 | ///Sets the copyright statement of the generated image, |
---|
684 | ///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of |
---|
685 | ///the EPS file. |
---|
686 | ///\todo Multiline copyright notice could be supported. |
---|
687 | GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;} |
---|
688 | |
---|
689 | protected: |
---|
690 | bool isInsideNode(dim2::Point<double> p, double r,int t) |
---|
691 | { |
---|
692 | switch(t) { |
---|
693 | case CIRCLE: |
---|
694 | case MALE: |
---|
695 | case FEMALE: |
---|
696 | return p.normSquare()<=r*r; |
---|
697 | case SQUARE: |
---|
698 | return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r; |
---|
699 | case DIAMOND: |
---|
700 | return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r; |
---|
701 | } |
---|
702 | return false; |
---|
703 | } |
---|
704 | |
---|
705 | public: |
---|
706 | ~GraphToEps() { } |
---|
707 | |
---|
708 | ///Draws the graph. |
---|
709 | |
---|
710 | ///Like other functions using |
---|
711 | ///\ref named-templ-func-param "named template parameters", |
---|
712 | ///this function calles the algorithm itself, i.e. in this case |
---|
713 | ///it draws the graph. |
---|
714 | void run() { |
---|
715 | ///\todo better 'epsilon' would be nice here. |
---|
716 | const double EPSILON=1e-9; |
---|
717 | if(dontPrint) return; |
---|
718 | |
---|
719 | _graph_to_eps_bits::_NegY<typename T::CoordsMapType> |
---|
720 | mycoords(_coords,_negY); |
---|
721 | |
---|
722 | os << "%!PS-Adobe-2.0 EPSF-2.0\n"; |
---|
723 | if(_title.size()>0) os << "%%Title: " << _title << '\n'; |
---|
724 | if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n'; |
---|
725 | // << "%%Copyright: XXXX\n" |
---|
726 | os << "%%Creator: LEMON, graphToEps()\n"; |
---|
727 | |
---|
728 | { |
---|
729 | char cbuf[50]; |
---|
730 | timeval tv; |
---|
731 | gettimeofday(&tv, 0); |
---|
732 | ctime_r(&tv.tv_sec,cbuf); |
---|
733 | os << "%%CreationDate: " << cbuf; |
---|
734 | } |
---|
735 | |
---|
736 | if (_autoArcWidthScale) { |
---|
737 | double max_w=0; |
---|
738 | for(ArcIt e(g);e!=INVALID;++e) |
---|
739 | max_w=std::max(double(_arcWidths[e]),max_w); |
---|
740 | ///\todo better 'epsilon' would be nice here. |
---|
741 | if(max_w>EPSILON) { |
---|
742 | _arcWidthScale/=max_w; |
---|
743 | } |
---|
744 | } |
---|
745 | |
---|
746 | if (_autoNodeScale) { |
---|
747 | double max_s=0; |
---|
748 | for(NodeIt n(g);n!=INVALID;++n) |
---|
749 | max_s=std::max(double(_nodeSizes[n]),max_s); |
---|
750 | ///\todo better 'epsilon' would be nice here. |
---|
751 | if(max_s>EPSILON) { |
---|
752 | _nodeScale/=max_s; |
---|
753 | } |
---|
754 | } |
---|
755 | |
---|
756 | double diag_len = 1; |
---|
757 | if(!(_absoluteNodeSizes&&_absoluteArcWidths)) { |
---|
758 | dim2::BoundingBox<double> bb; |
---|
759 | for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]); |
---|
760 | if (bb.empty()) { |
---|
761 | bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0)); |
---|
762 | } |
---|
763 | diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare()); |
---|
764 | if(diag_len<EPSILON) diag_len = 1; |
---|
765 | if(!_absoluteNodeSizes) _nodeScale*=diag_len; |
---|
766 | if(!_absoluteArcWidths) _arcWidthScale*=diag_len; |
---|
767 | } |
---|
768 | |
---|
769 | dim2::BoundingBox<double> bb; |
---|
770 | for(NodeIt n(g);n!=INVALID;++n) { |
---|
771 | double ns=_nodeSizes[n]*_nodeScale; |
---|
772 | dim2::Point<double> p(ns,ns); |
---|
773 | switch(_nodeShapes[n]) { |
---|
774 | case CIRCLE: |
---|
775 | case SQUARE: |
---|
776 | case DIAMOND: |
---|
777 | bb.add(p+mycoords[n]); |
---|
778 | bb.add(-p+mycoords[n]); |
---|
779 | break; |
---|
780 | case MALE: |
---|
781 | bb.add(-p+mycoords[n]); |
---|
782 | bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]); |
---|
783 | break; |
---|
784 | case FEMALE: |
---|
785 | bb.add(p+mycoords[n]); |
---|
786 | bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]); |
---|
787 | break; |
---|
788 | } |
---|
789 | } |
---|
790 | if (bb.empty()) { |
---|
791 | bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0)); |
---|
792 | } |
---|
793 | |
---|
794 | if(_scaleToA4) |
---|
795 | os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n"; |
---|
796 | else { |
---|
797 | if(_preScale) { |
---|
798 | //Rescale so that BoundingBox won't be neither to big nor too small. |
---|
799 | while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10; |
---|
800 | while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10; |
---|
801 | } |
---|
802 | |
---|
803 | os << "%%BoundingBox: " |
---|
804 | << int(floor(bb.left() * _scale - _xBorder)) << ' ' |
---|
805 | << int(floor(bb.bottom() * _scale - _yBorder)) << ' ' |
---|
806 | << int(ceil(bb.right() * _scale + _xBorder)) << ' ' |
---|
807 | << int(ceil(bb.top() * _scale + _yBorder)) << '\n'; |
---|
808 | } |
---|
809 | |
---|
810 | os << "%%EndComments\n"; |
---|
811 | |
---|
812 | //x1 y1 x2 y2 x3 y3 cr cg cb w |
---|
813 | os << "/lb { setlinewidth setrgbcolor newpath moveto\n" |
---|
814 | << " 4 2 roll 1 index 1 index curveto stroke } bind def\n"; |
---|
815 | os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n"; |
---|
816 | //x y r |
---|
817 | os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n"; |
---|
818 | //x y r |
---|
819 | os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n" |
---|
820 | << " 2 index 1 index sub 2 index 2 index add lineto\n" |
---|
821 | << " 2 index 1 index sub 2 index 2 index sub lineto\n" |
---|
822 | << " 2 index 1 index add 2 index 2 index sub lineto\n" |
---|
823 | << " closepath pop pop pop} bind def\n"; |
---|
824 | //x y r |
---|
825 | os << "/di { newpath 2 index 1 index add 2 index moveto\n" |
---|
826 | << " 2 index 2 index 2 index add lineto\n" |
---|
827 | << " 2 index 1 index sub 2 index lineto\n" |
---|
828 | << " 2 index 2 index 2 index sub lineto\n" |
---|
829 | << " closepath pop pop pop} bind def\n"; |
---|
830 | // x y r cr cg cb |
---|
831 | os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n" |
---|
832 | << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
---|
833 | << " } bind def\n"; |
---|
834 | os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n" |
---|
835 | << " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n" |
---|
836 | << " } bind def\n"; |
---|
837 | os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n" |
---|
838 | << " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n" |
---|
839 | << " } bind def\n"; |
---|
840 | os << "/nfemale { 0 0 0 setrgbcolor 3 index " |
---|
841 | << _nodeBorderQuotient/(1+_nodeBorderQuotient) |
---|
842 | << " 1.5 mul mul setlinewidth\n" |
---|
843 | << " newpath 5 index 5 index moveto " |
---|
844 | << "5 index 5 index 5 index 3.01 mul sub\n" |
---|
845 | << " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub moveto\n" |
---|
846 | << " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto stroke\n" |
---|
847 | << " 5 index 5 index 5 index c fill\n" |
---|
848 | << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
---|
849 | << " } bind def\n"; |
---|
850 | os << "/nmale {\n" |
---|
851 | << " 0 0 0 setrgbcolor 3 index " |
---|
852 | << _nodeBorderQuotient/(1+_nodeBorderQuotient) |
---|
853 | <<" 1.5 mul mul setlinewidth\n" |
---|
854 | << " newpath 5 index 5 index moveto\n" |
---|
855 | << " 5 index 4 index 1 mul 1.5 mul add\n" |
---|
856 | << " 5 index 5 index 3 sqrt 1.5 mul mul add\n" |
---|
857 | << " 1 index 1 index lineto\n" |
---|
858 | << " 1 index 1 index 7 index sub moveto\n" |
---|
859 | << " 1 index 1 index lineto\n" |
---|
860 | << " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub lineto\n" |
---|
861 | << " stroke\n" |
---|
862 | << " 5 index 5 index 5 index c fill\n" |
---|
863 | << " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
---|
864 | << " } bind def\n"; |
---|
865 | |
---|
866 | |
---|
867 | os << "/arrl " << _arrowLength << " def\n"; |
---|
868 | os << "/arrw " << _arrowWidth << " def\n"; |
---|
869 | // l dx_norm dy_norm |
---|
870 | os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n"; |
---|
871 | //len w dx_norm dy_norm x1 y1 cr cg cb |
---|
872 | os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n" |
---|
873 | << " /w exch def /len exch def\n" |
---|
874 | // << " 0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke" |
---|
875 | << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n" |
---|
876 | << " len w sub arrl sub dx dy lrl\n" |
---|
877 | << " arrw dy dx neg lrl\n" |
---|
878 | << " dx arrl w add mul dy w 2 div arrw add mul sub\n" |
---|
879 | << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n" |
---|
880 | << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n" |
---|
881 | << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n" |
---|
882 | << " arrw dy dx neg lrl\n" |
---|
883 | << " len w sub arrl sub neg dx dy lrl\n" |
---|
884 | << " closepath fill } bind def\n"; |
---|
885 | os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n" |
---|
886 | << " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n"; |
---|
887 | |
---|
888 | os << "\ngsave\n"; |
---|
889 | if(_scaleToA4) |
---|
890 | if(bb.height()>bb.width()) { |
---|
891 | double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(), |
---|
892 | (A4WIDTH-2*A4BORDER)/bb.width()); |
---|
893 | os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' ' |
---|
894 | << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER |
---|
895 | << " translate\n" |
---|
896 | << sc << " dup scale\n" |
---|
897 | << -bb.left() << ' ' << -bb.bottom() << " translate\n"; |
---|
898 | } |
---|
899 | else { |
---|
900 | //\todo Verify centering |
---|
901 | double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(), |
---|
902 | (A4WIDTH-2*A4BORDER)/bb.height()); |
---|
903 | os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' ' |
---|
904 | << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER |
---|
905 | << " translate\n" |
---|
906 | << sc << " dup scale\n90 rotate\n" |
---|
907 | << -bb.left() << ' ' << -bb.top() << " translate\n"; |
---|
908 | } |
---|
909 | else if(_scale!=1.0) os << _scale << " dup scale\n"; |
---|
910 | |
---|
911 | if(_showArcs) { |
---|
912 | os << "%Arcs:\ngsave\n"; |
---|
913 | if(_enableParallel) { |
---|
914 | std::vector<Arc> el; |
---|
915 | for(ArcIt e(g);e!=INVALID;++e) |
---|
916 | if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0 |
---|
917 | &&g.source(e)!=g.target(e)) |
---|
918 | el.push_back(e); |
---|
919 | std::sort(el.begin(),el.end(),arcLess(g)); |
---|
920 | |
---|
921 | typename std::vector<Arc>::iterator j; |
---|
922 | for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) { |
---|
923 | for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ; |
---|
924 | |
---|
925 | double sw=0; |
---|
926 | for(typename std::vector<Arc>::iterator e=i;e!=j;++e) |
---|
927 | sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist; |
---|
928 | sw-=_parArcDist; |
---|
929 | sw/=-2.0; |
---|
930 | dim2::Point<double> |
---|
931 | dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]); |
---|
932 | double l=std::sqrt(dvec.normSquare()); |
---|
933 | ///\todo better 'epsilon' would be nice here. |
---|
934 | dim2::Point<double> d(dvec/std::max(l,EPSILON)); |
---|
935 | dim2::Point<double> m; |
---|
936 | // m=dim2::Point<double>(mycoords[g.target(*i)]+mycoords[g.source(*i)])/2.0; |
---|
937 | |
---|
938 | // m=dim2::Point<double>(mycoords[g.source(*i)])+ |
---|
939 | // dvec*(double(_nodeSizes[g.source(*i)])/ |
---|
940 | // (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)])); |
---|
941 | |
---|
942 | m=dim2::Point<double>(mycoords[g.source(*i)])+ |
---|
943 | d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0; |
---|
944 | |
---|
945 | for(typename std::vector<Arc>::iterator e=i;e!=j;++e) { |
---|
946 | sw+=_arcWidths[*e]*_arcWidthScale/2.0; |
---|
947 | dim2::Point<double> mm=m+rot90(d)*sw/.75; |
---|
948 | if(_drawArrows) { |
---|
949 | int node_shape; |
---|
950 | dim2::Point<double> s=mycoords[g.source(*e)]; |
---|
951 | dim2::Point<double> t=mycoords[g.target(*e)]; |
---|
952 | double rn=_nodeSizes[g.target(*e)]*_nodeScale; |
---|
953 | node_shape=_nodeShapes[g.target(*e)]; |
---|
954 | dim2::Bezier3 bez(s,mm,mm,t); |
---|
955 | double t1=0,t2=1; |
---|
956 | for(int ii=0;ii<INTERPOL_PREC;++ii) |
---|
957 | if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2; |
---|
958 | else t1=(t1+t2)/2; |
---|
959 | dim2::Point<double> apoint=bez((t1+t2)/2); |
---|
960 | rn = _arrowLength+_arcWidths[*e]*_arcWidthScale; |
---|
961 | rn*=rn; |
---|
962 | t2=(t1+t2)/2;t1=0; |
---|
963 | for(int ii=0;ii<INTERPOL_PREC;++ii) |
---|
964 | if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2; |
---|
965 | else t2=(t1+t2)/2; |
---|
966 | dim2::Point<double> linend=bez((t1+t2)/2); |
---|
967 | bez=bez.before((t1+t2)/2); |
---|
968 | // rn=_nodeSizes[g.source(*e)]*_nodeScale; |
---|
969 | // node_shape=_nodeShapes[g.source(*e)]; |
---|
970 | // t1=0;t2=1; |
---|
971 | // for(int i=0;i<INTERPOL_PREC;++i) |
---|
972 | // if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t1=(t1+t2)/2; |
---|
973 | // else t2=(t1+t2)/2; |
---|
974 | // bez=bez.after((t1+t2)/2); |
---|
975 | os << _arcWidths[*e]*_arcWidthScale << " setlinewidth " |
---|
976 | << _arcColors[*e].red() << ' ' |
---|
977 | << _arcColors[*e].green() << ' ' |
---|
978 | << _arcColors[*e].blue() << " setrgbcolor newpath\n" |
---|
979 | << bez.p1.x << ' ' << bez.p1.y << " moveto\n" |
---|
980 | << bez.p2.x << ' ' << bez.p2.y << ' ' |
---|
981 | << bez.p3.x << ' ' << bez.p3.y << ' ' |
---|
982 | << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n"; |
---|
983 | dim2::Point<double> dd(rot90(linend-apoint)); |
---|
984 | dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/ |
---|
985 | std::sqrt(dd.normSquare()); |
---|
986 | os << "newpath " << psOut(apoint) << " moveto " |
---|
987 | << psOut(linend+dd) << " lineto " |
---|
988 | << psOut(linend-dd) << " lineto closepath fill\n"; |
---|
989 | } |
---|
990 | else { |
---|
991 | os << mycoords[g.source(*e)].x << ' ' |
---|
992 | << mycoords[g.source(*e)].y << ' ' |
---|
993 | << mm.x << ' ' << mm.y << ' ' |
---|
994 | << mycoords[g.target(*e)].x << ' ' |
---|
995 | << mycoords[g.target(*e)].y << ' ' |
---|
996 | << _arcColors[*e].red() << ' ' |
---|
997 | << _arcColors[*e].green() << ' ' |
---|
998 | << _arcColors[*e].blue() << ' ' |
---|
999 | << _arcWidths[*e]*_arcWidthScale << " lb\n"; |
---|
1000 | } |
---|
1001 | sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist; |
---|
1002 | } |
---|
1003 | } |
---|
1004 | } |
---|
1005 | else for(ArcIt e(g);e!=INVALID;++e) |
---|
1006 | if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0 |
---|
1007 | &&g.source(e)!=g.target(e)) |
---|
1008 | if(_drawArrows) { |
---|
1009 | dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]); |
---|
1010 | double rn=_nodeSizes[g.target(e)]*_nodeScale; |
---|
1011 | int node_shape=_nodeShapes[g.target(e)]; |
---|
1012 | double t1=0,t2=1; |
---|
1013 | for(int i=0;i<INTERPOL_PREC;++i) |
---|
1014 | if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2; |
---|
1015 | else t2=(t1+t2)/2; |
---|
1016 | double l=std::sqrt(d.normSquare()); |
---|
1017 | d/=l; |
---|
1018 | |
---|
1019 | os << l*(1-(t1+t2)/2) << ' ' |
---|
1020 | << _arcWidths[e]*_arcWidthScale << ' ' |
---|
1021 | << d.x << ' ' << d.y << ' ' |
---|
1022 | << mycoords[g.source(e)].x << ' ' |
---|
1023 | << mycoords[g.source(e)].y << ' ' |
---|
1024 | << _arcColors[e].red() << ' ' |
---|
1025 | << _arcColors[e].green() << ' ' |
---|
1026 | << _arcColors[e].blue() << " arr\n"; |
---|
1027 | } |
---|
1028 | else os << mycoords[g.source(e)].x << ' ' |
---|
1029 | << mycoords[g.source(e)].y << ' ' |
---|
1030 | << mycoords[g.target(e)].x << ' ' |
---|
1031 | << mycoords[g.target(e)].y << ' ' |
---|
1032 | << _arcColors[e].red() << ' ' |
---|
1033 | << _arcColors[e].green() << ' ' |
---|
1034 | << _arcColors[e].blue() << ' ' |
---|
1035 | << _arcWidths[e]*_arcWidthScale << " l\n"; |
---|
1036 | os << "grestore\n"; |
---|
1037 | } |
---|
1038 | if(_showNodes) { |
---|
1039 | os << "%Nodes:\ngsave\n"; |
---|
1040 | for(NodeIt n(g);n!=INVALID;++n) { |
---|
1041 | os << mycoords[n].x << ' ' << mycoords[n].y << ' ' |
---|
1042 | << _nodeSizes[n]*_nodeScale << ' ' |
---|
1043 | << _nodeColors[n].red() << ' ' |
---|
1044 | << _nodeColors[n].green() << ' ' |
---|
1045 | << _nodeColors[n].blue() << ' '; |
---|
1046 | switch(_nodeShapes[n]) { |
---|
1047 | case CIRCLE: |
---|
1048 | os<< "nc";break; |
---|
1049 | case SQUARE: |
---|
1050 | os<< "nsq";break; |
---|
1051 | case DIAMOND: |
---|
1052 | os<< "ndi";break; |
---|
1053 | case MALE: |
---|
1054 | os<< "nmale";break; |
---|
1055 | case FEMALE: |
---|
1056 | os<< "nfemale";break; |
---|
1057 | } |
---|
1058 | os<<'\n'; |
---|
1059 | } |
---|
1060 | os << "grestore\n"; |
---|
1061 | } |
---|
1062 | if(_showNodeText) { |
---|
1063 | os << "%Node texts:\ngsave\n"; |
---|
1064 | os << "/fosi " << _nodeTextSize << " def\n"; |
---|
1065 | os << "(Helvetica) findfont fosi scalefont setfont\n"; |
---|
1066 | for(NodeIt n(g);n!=INVALID;++n) { |
---|
1067 | switch(_nodeTextColorType) { |
---|
1068 | case DIST_COL: |
---|
1069 | os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n"; |
---|
1070 | break; |
---|
1071 | case DIST_BW: |
---|
1072 | os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n"; |
---|
1073 | break; |
---|
1074 | case CUST_COL: |
---|
1075 | os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n"; |
---|
1076 | break; |
---|
1077 | default: |
---|
1078 | os << "0 0 0 setrgbcolor\n"; |
---|
1079 | } |
---|
1080 | os << mycoords[n].x << ' ' << mycoords[n].y |
---|
1081 | << " (" << _nodeTexts[n] << ") cshow\n"; |
---|
1082 | } |
---|
1083 | os << "grestore\n"; |
---|
1084 | } |
---|
1085 | if(_showNodePsText) { |
---|
1086 | os << "%Node PS blocks:\ngsave\n"; |
---|
1087 | for(NodeIt n(g);n!=INVALID;++n) |
---|
1088 | os << mycoords[n].x << ' ' << mycoords[n].y |
---|
1089 | << " moveto\n" << _nodePsTexts[n] << "\n"; |
---|
1090 | os << "grestore\n"; |
---|
1091 | } |
---|
1092 | |
---|
1093 | os << "grestore\nshowpage\n"; |
---|
1094 | |
---|
1095 | //CleanUp: |
---|
1096 | if(_pleaseRemoveOsStream) {delete &os;} |
---|
1097 | } |
---|
1098 | |
---|
1099 | ///\name Aliases |
---|
1100 | ///These are just some aliases to other parameter setting functions. |
---|
1101 | |
---|
1102 | ///@{ |
---|
1103 | |
---|
1104 | ///An alias for arcWidths() |
---|
1105 | |
---|
1106 | ///An alias for arcWidths() |
---|
1107 | /// |
---|
1108 | template<class X> GraphToEps<ArcWidthsTraits<X> > edgeWidths(const X &x) |
---|
1109 | { |
---|
1110 | return arcWidths(x); |
---|
1111 | } |
---|
1112 | |
---|
1113 | ///An alias for arcColors() |
---|
1114 | |
---|
1115 | ///An alias for arcColors() |
---|
1116 | /// |
---|
1117 | template<class X> GraphToEps<ArcColorsTraits<X> > |
---|
1118 | edgeColors(const X &x) |
---|
1119 | { |
---|
1120 | return arcColors(x); |
---|
1121 | } |
---|
1122 | |
---|
1123 | ///An alias for arcWidthScale() |
---|
1124 | |
---|
1125 | ///An alias for arcWidthScale() |
---|
1126 | /// |
---|
1127 | GraphToEps<T> &edgeWidthScale(double d) {return arcWidthScale(d);} |
---|
1128 | |
---|
1129 | ///An alias for autoArcWidthScale() |
---|
1130 | |
---|
1131 | ///An alias for autoArcWidthScale() |
---|
1132 | /// |
---|
1133 | GraphToEps<T> &autoEdgeWidthScale(bool b=true) |
---|
1134 | { |
---|
1135 | return autoArcWidthScale(b); |
---|
1136 | } |
---|
1137 | |
---|
1138 | ///An alias for absoluteArcWidths() |
---|
1139 | |
---|
1140 | ///An alias for absoluteArcWidths() |
---|
1141 | /// |
---|
1142 | GraphToEps<T> &absoluteEdgeWidths(bool b=true) |
---|
1143 | { |
---|
1144 | return absoluteArcWidths(b); |
---|
1145 | } |
---|
1146 | |
---|
1147 | ///An alias for parArcDist() |
---|
1148 | |
---|
1149 | ///An alias for parArcDist() |
---|
1150 | /// |
---|
1151 | GraphToEps<T> &parEdgeDist(double d) {return parArcDist(d);} |
---|
1152 | |
---|
1153 | ///An alias for hideArcs() |
---|
1154 | |
---|
1155 | ///An alias for hideArcs() |
---|
1156 | /// |
---|
1157 | GraphToEps<T> &hideEdges(bool b=true) {return hideArcs(b);} |
---|
1158 | |
---|
1159 | ///@} |
---|
1160 | }; |
---|
1161 | |
---|
1162 | template<class T> |
---|
1163 | const int GraphToEps<T>::INTERPOL_PREC = 20; |
---|
1164 | template<class T> |
---|
1165 | const double GraphToEps<T>::A4HEIGHT = 841.8897637795276; |
---|
1166 | template<class T> |
---|
1167 | const double GraphToEps<T>::A4WIDTH = 595.275590551181; |
---|
1168 | template<class T> |
---|
1169 | const double GraphToEps<T>::A4BORDER = 15; |
---|
1170 | |
---|
1171 | |
---|
1172 | ///Generates an EPS file from a graph |
---|
1173 | |
---|
1174 | ///\ingroup eps_io |
---|
1175 | ///Generates an EPS file from a graph. |
---|
1176 | ///\param g is a reference to the graph to be printed |
---|
1177 | ///\param os is a reference to the output stream. |
---|
1178 | ///By default it is <tt>std::cout</tt> |
---|
1179 | /// |
---|
1180 | ///This function also has a lot of |
---|
1181 | ///\ref named-templ-func-param "named parameters", |
---|
1182 | ///they are declared as the members of class \ref GraphToEps. The following |
---|
1183 | ///example shows how to use these parameters. |
---|
1184 | ///\code |
---|
1185 | /// graphToEps(g,os).scale(10).coords(coords) |
---|
1186 | /// .nodeScale(2).nodeSizes(sizes) |
---|
1187 | /// .arcWidthScale(.4).run(); |
---|
1188 | ///\endcode |
---|
1189 | ///\warning Don't forget to put the \ref GraphToEps::run() "run()" |
---|
1190 | ///to the end of the parameter list. |
---|
1191 | ///\sa GraphToEps |
---|
1192 | ///\sa graphToEps(G &g, const char *file_name) |
---|
1193 | template<class G> |
---|
1194 | GraphToEps<DefaultGraphToEpsTraits<G> > |
---|
1195 | graphToEps(G &g, std::ostream& os=std::cout) |
---|
1196 | { |
---|
1197 | return |
---|
1198 | GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os)); |
---|
1199 | } |
---|
1200 | |
---|
1201 | ///Generates an EPS file from a graph |
---|
1202 | |
---|
1203 | ///\ingroup eps_io |
---|
1204 | ///This function does the same as |
---|
1205 | ///\ref graphToEps(G &g,std::ostream& os) |
---|
1206 | ///but it writes its output into the file \c file_name |
---|
1207 | ///instead of a stream. |
---|
1208 | ///\sa graphToEps(G &g, std::ostream& os) |
---|
1209 | template<class G> |
---|
1210 | GraphToEps<DefaultGraphToEpsTraits<G> > |
---|
1211 | graphToEps(G &g,const char *file_name) |
---|
1212 | { |
---|
1213 | return GraphToEps<DefaultGraphToEpsTraits<G> > |
---|
1214 | (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true)); |
---|
1215 | } |
---|
1216 | |
---|
1217 | ///Generates an EPS file from a graph |
---|
1218 | |
---|
1219 | ///\ingroup eps_io |
---|
1220 | ///This function does the same as |
---|
1221 | ///\ref graphToEps(G &g,std::ostream& os) |
---|
1222 | ///but it writes its output into the file \c file_name |
---|
1223 | ///instead of a stream. |
---|
1224 | ///\sa graphToEps(G &g, std::ostream& os) |
---|
1225 | template<class G> |
---|
1226 | GraphToEps<DefaultGraphToEpsTraits<G> > |
---|
1227 | graphToEps(G &g,const std::string& file_name) |
---|
1228 | { |
---|
1229 | return GraphToEps<DefaultGraphToEpsTraits<G> > |
---|
1230 | (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name.c_str()),true)); |
---|
1231 | } |
---|
1232 | |
---|
1233 | } //END OF NAMESPACE LEMON |
---|
1234 | |
---|
1235 | #endif // LEMON_GRAPH_TO_EPS_H |
---|