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