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