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