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