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