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