[1073] | 1 | /* -*- C++ -*- |
---|
| 2 | * src/lemon/graph_to_eps.h - Part of LEMON, a generic C++ optimization library |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
| 6 | * |
---|
| 7 | * Permission to use, modify and distribute this software is granted |
---|
| 8 | * provided that this copyright notice appears in all copies. For |
---|
| 9 | * precise terms see the accompanying LICENSE file. |
---|
| 10 | * |
---|
| 11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 12 | * express or implied, and with no claim as to its suitability for any |
---|
| 13 | * purpose. |
---|
| 14 | * |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | #ifndef LEMON_GRAPH_TO_EPS_H |
---|
| 18 | #define LEMON_GRAPH_TO_EPS_H |
---|
| 19 | |
---|
| 20 | #include<iostream> |
---|
| 21 | #include<fstream> |
---|
| 22 | #include<sstream> |
---|
| 23 | #include<algorithm> |
---|
| 24 | #include<vector> |
---|
| 25 | |
---|
| 26 | #include<lemon/xy.h> |
---|
| 27 | #include<lemon/maps.h> |
---|
| 28 | #include<lemon/bezier.h> |
---|
| 29 | |
---|
| 30 | ///\ingroup misc |
---|
| 31 | ///\file |
---|
| 32 | ///\brief Simple graph drawer |
---|
| 33 | /// |
---|
| 34 | ///\author Alpar Juttner |
---|
| 35 | |
---|
| 36 | namespace lemon { |
---|
| 37 | |
---|
| 38 | ///Data structure representing RGB colors. |
---|
| 39 | |
---|
| 40 | ///Data structure representing RGB colors. |
---|
| 41 | ///\ingroup misc |
---|
| 42 | class Color |
---|
| 43 | { |
---|
| 44 | double _r,_g,_b; |
---|
| 45 | public: |
---|
| 46 | ///Default constructor |
---|
| 47 | Color() {} |
---|
| 48 | ///Constructor |
---|
| 49 | Color(double r,double g,double b) :_r(r),_g(g),_b(b) {}; |
---|
| 50 | ///Returns the red component |
---|
| 51 | double getR() {return _r;} |
---|
| 52 | ///Returns the green component |
---|
| 53 | double getG() {return _g;} |
---|
| 54 | ///Returns the blue component |
---|
| 55 | double getB() {return _b;} |
---|
| 56 | ///Set the color components |
---|
| 57 | void set(double r,double g,double b) { _r=r;_g=g;_b=b; }; |
---|
| 58 | }; |
---|
| 59 | |
---|
| 60 | ///Default traits class of \ref GraphToEps |
---|
| 61 | |
---|
| 62 | ///Default traits class of \ref GraphToEps |
---|
| 63 | /// |
---|
| 64 | ///\c G is the type of the underlying graph. |
---|
| 65 | template<class G> |
---|
| 66 | struct DefaultGraphToEpsTraits |
---|
| 67 | { |
---|
| 68 | typedef G Graph; |
---|
| 69 | typedef typename Graph::Node Node; |
---|
| 70 | typedef typename Graph::NodeIt NodeIt; |
---|
| 71 | typedef typename Graph::Edge Edge; |
---|
| 72 | typedef typename Graph::EdgeIt EdgeIt; |
---|
| 73 | typedef typename Graph::InEdgeIt InEdgeIt; |
---|
| 74 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | const Graph &g; |
---|
| 78 | |
---|
| 79 | std::ostream& os; |
---|
| 80 | |
---|
| 81 | ConstMap<typename Graph::Node,xy<double> > _coords; |
---|
| 82 | ConstMap<typename Graph::Node,double > _nodeSizes; |
---|
| 83 | |
---|
| 84 | ConstMap<typename Graph::Node,Color > _nodeColors; |
---|
| 85 | ConstMap<typename Graph::Edge,Color > _edgeColors; |
---|
| 86 | |
---|
| 87 | ConstMap<typename Graph::Edge,double > _edgeWidths; |
---|
| 88 | |
---|
| 89 | double _edgeWidthScale; |
---|
| 90 | |
---|
| 91 | double _nodeScale; |
---|
| 92 | double _xBorder, _yBorder; |
---|
| 93 | double _scale; |
---|
| 94 | double _nodeBorderQuotient; |
---|
| 95 | |
---|
| 96 | bool _drawArrows; |
---|
| 97 | double _arrowLength, _arrowWidth; |
---|
| 98 | |
---|
| 99 | bool _showNodes, _showEdges; |
---|
| 100 | |
---|
| 101 | bool _enableParallel; |
---|
| 102 | double _parEdgeDist; |
---|
| 103 | |
---|
| 104 | bool _showNodeText; |
---|
| 105 | ConstMap<typename Graph::Node,bool > _nodeTexts; |
---|
| 106 | double _nodeTextSize; |
---|
| 107 | |
---|
| 108 | bool _undir; |
---|
| 109 | bool _pleaseRemoveOsStream; |
---|
| 110 | ///Constructor |
---|
| 111 | |
---|
| 112 | ///Constructor |
---|
| 113 | ///\param _g is a reference to the graph to be printed |
---|
| 114 | ///\param _os is a reference to the output stream. |
---|
| 115 | ///\param _os is a reference to the output stream. |
---|
| 116 | ///\param _pros If it is \c true, then the \c ostream referenced by \c _os |
---|
| 117 | ///will be explicitly deallocated by the destructor. |
---|
| 118 | ///By default it is <tt>std::cout</tt> |
---|
| 119 | DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout, |
---|
| 120 | bool _pros=false) : |
---|
| 121 | g(_g), os(_os), |
---|
| 122 | _coords(xy<double>(1,1)), _nodeSizes(1.0), |
---|
| 123 | _nodeColors(Color(1,1,1)), _edgeColors(Color(0,0,0)), |
---|
| 124 | _edgeWidths(1), _edgeWidthScale(0.3), |
---|
| 125 | _nodeScale(1.0), _xBorder(10), _yBorder(10), _scale(1.0), |
---|
| 126 | _nodeBorderQuotient(.1), |
---|
| 127 | _drawArrows(false), _arrowLength(1), _arrowWidth(0.3), |
---|
| 128 | _showNodes(true), _showEdges(true), |
---|
| 129 | _enableParallel(false), _parEdgeDist(1), |
---|
| 130 | _showNodeText(false), _nodeTexts(false), _nodeTextSize(1), |
---|
| 131 | _undir(false), |
---|
| 132 | _pleaseRemoveOsStream(_pros) {} |
---|
| 133 | }; |
---|
| 134 | |
---|
| 135 | ///Helper class to implement the named parameters of \ref graphToEps() |
---|
| 136 | |
---|
| 137 | ///Helper class to implement the named parameters of \ref graphToEps() |
---|
| 138 | ///\todo Is 'helper class' a good name for this? |
---|
| 139 | /// |
---|
| 140 | template<class T> class GraphToEps : public T |
---|
| 141 | { |
---|
| 142 | typedef typename T::Graph Graph; |
---|
| 143 | typedef typename Graph::Node Node; |
---|
| 144 | typedef typename Graph::NodeIt NodeIt; |
---|
| 145 | typedef typename Graph::Edge Edge; |
---|
| 146 | typedef typename Graph::EdgeIt EdgeIt; |
---|
| 147 | typedef typename Graph::InEdgeIt InEdgeIt; |
---|
| 148 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
| 149 | |
---|
| 150 | bool dontPrint; |
---|
| 151 | |
---|
| 152 | class edgeLess { |
---|
| 153 | const Graph &g; |
---|
| 154 | public: |
---|
| 155 | edgeLess(const Graph &_g) : g(_g) {} |
---|
| 156 | bool operator()(Edge a,Edge b) const |
---|
| 157 | { |
---|
| 158 | Node ai=min(g.source(a),g.target(a)); |
---|
| 159 | Node aa=max(g.source(a),g.target(a)); |
---|
| 160 | Node bi=min(g.source(b),g.target(b)); |
---|
| 161 | Node ba=max(g.source(b),g.target(b)); |
---|
| 162 | return ai<bi || |
---|
| 163 | (ai==bi && (aa < ba || |
---|
| 164 | (aa==ba && ai==g.source(a) && bi==g.target(b)))); |
---|
| 165 | } |
---|
| 166 | }; |
---|
| 167 | bool isParallel(Edge e,Edge f) const |
---|
| 168 | { |
---|
| 169 | return (g.source(e)==g.source(f)&&g.target(e)==g.target(f))|| |
---|
| 170 | (g.source(e)==g.target(f)&&g.target(e)==g.source(f)); |
---|
| 171 | } |
---|
| 172 | static xy<double> rot(xy<double> v) |
---|
| 173 | { |
---|
| 174 | return xy<double>(v.y,-v.x); |
---|
| 175 | } |
---|
| 176 | template<class xy> |
---|
| 177 | static std::string psOut(const xy &p) |
---|
| 178 | { |
---|
| 179 | std::ostringstream os; |
---|
| 180 | os << p.x << ' ' << p.y; |
---|
| 181 | return os.str(); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | public: |
---|
| 185 | GraphToEps(const T &t) : T(t), dontPrint(false) {}; |
---|
| 186 | |
---|
| 187 | template<class X> struct CoordsTraits : public T { |
---|
| 188 | const X &_coords; |
---|
| 189 | CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {} |
---|
| 190 | }; |
---|
| 191 | ///Sets the map of the node coordinates |
---|
| 192 | |
---|
| 193 | ///Sets the map of the node coordinates. |
---|
| 194 | ///\param x must be a node map with xy<double> or xy<int> values. |
---|
| 195 | template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) { |
---|
| 196 | dontPrint=true; |
---|
| 197 | return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x)); |
---|
| 198 | } |
---|
| 199 | template<class X> struct NodeSizesTraits : public T { |
---|
| 200 | const X &_nodeSizes; |
---|
| 201 | NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {} |
---|
| 202 | }; |
---|
| 203 | ///Sets the map of the node sizes |
---|
| 204 | |
---|
| 205 | ///Sets the map of the node sizes |
---|
| 206 | ///\param x must be a node map with \c double (or convertible) values. |
---|
| 207 | template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x) |
---|
| 208 | { |
---|
| 209 | dontPrint=true; |
---|
| 210 | return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x)); |
---|
| 211 | } |
---|
| 212 | template<class X> struct NodeTextsTraits : public T { |
---|
| 213 | const X &_nodeTexts; |
---|
| 214 | NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {} |
---|
| 215 | }; |
---|
| 216 | ///Sets the text printed on the nodes |
---|
| 217 | |
---|
| 218 | ///Sets the text printed on the nodes |
---|
| 219 | ///\param x must be a node map with type that can be pushed to a standard |
---|
| 220 | ///ostream. |
---|
| 221 | template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x) |
---|
| 222 | { |
---|
| 223 | dontPrint=true; |
---|
| 224 | _showNodeText=true; |
---|
| 225 | return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x)); |
---|
| 226 | } |
---|
| 227 | template<class X> struct EdgeWidthsTraits : public T { |
---|
| 228 | const X &_edgeWidths; |
---|
| 229 | EdgeWidthsTraits(const T &t,const X &x) : T(t), _edgeWidths(x) {} |
---|
| 230 | }; |
---|
| 231 | ///Sets the map of the edge widths |
---|
| 232 | |
---|
| 233 | ///Sets the map of the edge widths |
---|
| 234 | ///\param x must be a edge map with \c double (or convertible) values. |
---|
| 235 | template<class X> GraphToEps<EdgeWidthsTraits<X> > edgeWidths(const X &x) |
---|
| 236 | { |
---|
| 237 | dontPrint=true; |
---|
| 238 | return GraphToEps<EdgeWidthsTraits<X> >(EdgeWidthsTraits<X>(*this,x)); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | template<class X> struct NodeColorsTraits : public T { |
---|
| 242 | const X &_nodeColors; |
---|
| 243 | NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {} |
---|
| 244 | }; |
---|
| 245 | ///Sets the map of the node colors |
---|
| 246 | |
---|
| 247 | ///Sets the map of the node colors |
---|
| 248 | ///\param x must be a node map with \ref Color values. |
---|
| 249 | template<class X> GraphToEps<NodeColorsTraits<X> > |
---|
| 250 | nodeColors(const X &x) |
---|
| 251 | { |
---|
| 252 | dontPrint=true; |
---|
| 253 | return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x)); |
---|
| 254 | } |
---|
| 255 | template<class X> struct EdgeColorsTraits : public T { |
---|
| 256 | const X &_edgeColors; |
---|
| 257 | EdgeColorsTraits(const T &t,const X &x) : T(t), _edgeColors(x) {} |
---|
| 258 | }; |
---|
| 259 | ///Sets the map of the edge colors |
---|
| 260 | |
---|
| 261 | ///Sets the map of the edge colors |
---|
| 262 | ///\param x must be a edge map with \ref Color values. |
---|
| 263 | template<class X> GraphToEps<EdgeColorsTraits<X> > |
---|
| 264 | edgeColors(const X &x) |
---|
| 265 | { |
---|
| 266 | dontPrint=true; |
---|
| 267 | return GraphToEps<EdgeColorsTraits<X> >(EdgeColorsTraits<X>(*this,x)); |
---|
| 268 | } |
---|
| 269 | ///Sets a global scale factor for node sizes |
---|
| 270 | |
---|
| 271 | ///Sets a global scale factor for node sizes |
---|
| 272 | /// |
---|
| 273 | GraphToEps<T> &nodeScale(double d) {_nodeScale=d;return *this;} |
---|
| 274 | ///Sets a global scale factor for edge widths |
---|
| 275 | |
---|
| 276 | ///Sets a global scale factor for edge widths |
---|
| 277 | /// |
---|
| 278 | GraphToEps<T> &edgeWidthScale(double d) {_edgeWidthScale=d;return *this;} |
---|
| 279 | ///Sets a global scale factor for the whole picture |
---|
| 280 | |
---|
| 281 | ///Sets a global scale factor for the whole picture |
---|
| 282 | /// |
---|
| 283 | GraphToEps<T> &scale(double d) {_scale=d;return *this;} |
---|
| 284 | ///Sets the width of the border around the picture |
---|
| 285 | |
---|
| 286 | ///Sets the width of the border around the picture |
---|
| 287 | /// |
---|
| 288 | GraphToEps<T> &border(double b) {_xBorder=_yBorder=b;return *this;} |
---|
| 289 | ///Sets the width of the border around the picture |
---|
| 290 | |
---|
| 291 | ///Sets the width of the border around the picture |
---|
| 292 | /// |
---|
| 293 | GraphToEps<T> &border(double x, double y) { |
---|
| 294 | _xBorder=x;_yBorder=y;return *this; |
---|
| 295 | } |
---|
| 296 | ///Sets whether to draw arrows |
---|
| 297 | |
---|
| 298 | ///Sets whether to draw arrows |
---|
| 299 | /// |
---|
| 300 | GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;} |
---|
| 301 | ///Sets the length of the arrowheads |
---|
| 302 | |
---|
| 303 | ///Sets the length of the arrowheads |
---|
| 304 | /// |
---|
| 305 | GraphToEps<T> &arrowLength(double d) {_arrowLength*=d;return *this;} |
---|
| 306 | ///Sets the width of the arrowheads |
---|
| 307 | |
---|
| 308 | ///Sets the width of the arrowheads |
---|
| 309 | /// |
---|
| 310 | GraphToEps<T> &arrowWidth(double d) {_arrowWidth*=d;return *this;} |
---|
| 311 | |
---|
| 312 | ///Enables parallel edges |
---|
| 313 | |
---|
| 314 | ///Enables parallel edges |
---|
| 315 | ///\todo Partially implemented |
---|
| 316 | GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;} |
---|
| 317 | |
---|
| 318 | ///Sets the distance |
---|
| 319 | |
---|
| 320 | ///Sets the distance |
---|
| 321 | /// |
---|
| 322 | GraphToEps<T> &parEdgeDist(double d) {_parEdgeDist*=d;return *this;} |
---|
| 323 | |
---|
| 324 | ///Hides the edges |
---|
| 325 | |
---|
| 326 | ///Hides the edges |
---|
| 327 | /// |
---|
| 328 | GraphToEps<T> &hideEdges(bool b=true) {_showEdges=!b;return *this;} |
---|
| 329 | ///Hides the nodes |
---|
| 330 | |
---|
| 331 | ///Hides the nodes |
---|
| 332 | /// |
---|
| 333 | GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;} |
---|
| 334 | |
---|
| 335 | ///Sets the size of the node texts |
---|
| 336 | |
---|
| 337 | ///Sets the size of the node texts |
---|
| 338 | /// |
---|
| 339 | GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;} |
---|
| 340 | ///Sets whether the the graph is undirected |
---|
| 341 | |
---|
| 342 | ///Sets whether the the graph is undirected |
---|
| 343 | /// |
---|
| 344 | GraphToEps<T> &undir(bool b=true) {_undir=b;return *this;} |
---|
| 345 | ///Sets whether the the graph is directed |
---|
| 346 | |
---|
| 347 | ///Sets whether the the graph is directed. |
---|
| 348 | ///Use it to show the undirected edges as a pair of directed ones. |
---|
| 349 | GraphToEps<T> &bidir(bool b=true) {_undir=!b;return *this;} |
---|
| 350 | |
---|
| 351 | ~GraphToEps() |
---|
| 352 | { |
---|
| 353 | if(dontPrint) return; |
---|
| 354 | |
---|
| 355 | os << "%!PS-Adobe-2.0 EPSF-2.0\n"; |
---|
| 356 | //\todo: Chech whether the graph is empty. |
---|
| 357 | BoundingBox<double> bb; |
---|
| 358 | for(NodeIt n(g);n!=INVALID;++n) { |
---|
| 359 | double ns=_nodeSizes[n]*_nodeScale; |
---|
| 360 | xy<double> p(ns,ns); |
---|
| 361 | bb+=p+_coords[n]; |
---|
| 362 | bb+=-p+_coords[n]; |
---|
| 363 | } |
---|
| 364 | os << "%%BoundingBox: " |
---|
| 365 | << bb.left()* _scale-_xBorder << ' ' |
---|
| 366 | << bb.bottom()*_scale-_yBorder << ' ' |
---|
| 367 | << bb.right()* _scale+_xBorder << ' ' |
---|
| 368 | << bb.top()* _scale+_yBorder << '\n'; |
---|
| 369 | //x1 y1 x2 y2 x3 y3 cr cg cb w |
---|
| 370 | os << "/lb { setlinewidth setrgbcolor newpath moveto\n" |
---|
| 371 | << " 4 2 roll 1 index 1 index curveto stroke } bind def\n"; |
---|
| 372 | os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n"; |
---|
| 373 | os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n"; |
---|
| 374 | // x y r cr cg cb |
---|
| 375 | os << "/n { setrgbcolor 2 index 2 index 2 index c fill\n" |
---|
| 376 | << " 0 0 0 setrgbcolor dup " |
---|
| 377 | << _nodeBorderQuotient << " mul setlinewidth " |
---|
| 378 | << 1+_nodeBorderQuotient/2 << " div c stroke\n" |
---|
| 379 | << " } bind def\n"; |
---|
| 380 | os << "/arrl " << _arrowLength << " def\n"; |
---|
| 381 | os << "/arrw " << _arrowWidth << " def\n"; |
---|
| 382 | // l dx_norm dy_norm |
---|
| 383 | os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n"; |
---|
| 384 | //len w dx_norm dy_norm x1 y1 cr cg cb |
---|
| 385 | os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def\n" |
---|
| 386 | << " /w exch def /len exch def\n" |
---|
| 387 | // << " 0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke" |
---|
| 388 | << " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n" |
---|
| 389 | << " len w sub arrl sub dx dy lrl\n" |
---|
| 390 | << " arrw dy dx neg lrl\n" |
---|
| 391 | << " dx arrl w add mul dy w 2 div arrw add mul sub\n" |
---|
| 392 | << " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n" |
---|
| 393 | << " dx arrl w add mul neg dy w 2 div arrw add mul sub\n" |
---|
| 394 | << " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n" |
---|
| 395 | << " arrw dy dx neg lrl\n" |
---|
| 396 | << " len w sub arrl sub neg dx dy lrl\n" |
---|
| 397 | << " closepath fill } bind def\n"; |
---|
| 398 | os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n" |
---|
| 399 | << " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n"; |
---|
| 400 | |
---|
| 401 | os << "\ngsave\n"; |
---|
| 402 | if(_scale!=1.0) os << _scale << " dup scale\n"; |
---|
| 403 | |
---|
| 404 | os << "%Edges:\ngsave\n"; |
---|
| 405 | |
---|
| 406 | if(_showEdges) |
---|
| 407 | if(_enableParallel) { |
---|
| 408 | std::vector<Edge> el; |
---|
| 409 | for(EdgeIt e(g);e!=INVALID;++e) |
---|
| 410 | if(!_undir||g.source(e)<g.target(e)) el.push_back(e); |
---|
| 411 | sort(el.begin(),el.end(),edgeLess(g)); |
---|
| 412 | |
---|
| 413 | typename std::vector<Edge>::iterator j; |
---|
| 414 | for(typename std::vector<Edge>::iterator i=el.begin();i!=el.end();i=j) { |
---|
| 415 | for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ; |
---|
| 416 | |
---|
| 417 | double sw=0; |
---|
| 418 | for(typename std::vector<Edge>::iterator e=i;e!=j;++e) |
---|
| 419 | sw+=_edgeWidths[*e]*_edgeWidthScale+_parEdgeDist; |
---|
| 420 | sw-=_parEdgeDist; |
---|
| 421 | sw/=-2.0; |
---|
| 422 | xy<double> d(_coords[g.target(*i)]-_coords[g.source(*i)]); |
---|
| 423 | double l=sqrt(d.normSquare()); |
---|
| 424 | d/=l; |
---|
| 425 | |
---|
| 426 | for(typename std::vector<Edge>::iterator e=i;e!=j;++e) { |
---|
| 427 | sw+=_edgeWidths[*e]*_edgeWidthScale/2.0; |
---|
| 428 | xy<double> m(_coords[g.target(*e)]+_coords[g.source(*e)]); |
---|
| 429 | m=m/2.0+rot(d)*sw/.75; |
---|
| 430 | if(_drawArrows) { |
---|
| 431 | const int INERPOL_PREC=20; |
---|
| 432 | xy<double> s=_coords[g.source(*e)]; |
---|
| 433 | xy<double> t=_coords[g.target(*e)]; |
---|
| 434 | double rn=_nodeSizes[g.target(*e)]*_nodeScale; |
---|
| 435 | rn*=rn; |
---|
| 436 | Bezier3 bez(s,m,m,t); |
---|
| 437 | double t1=0,t2=1; |
---|
| 438 | for(int i=0;i<INERPOL_PREC;++i) |
---|
| 439 | if((bez((t1+t2)/2)-t).normSquare()>rn) t1=(t1+t2)/2; |
---|
| 440 | else t2=(t1+t2)/2; |
---|
| 441 | xy<double> apoint=bez((t1+t2)/2); |
---|
| 442 | rn = _nodeSizes[g.target(*e)]*_nodeScale+_arrowLength+ |
---|
| 443 | _edgeWidths[*e]*_edgeWidthScale; |
---|
| 444 | rn*=rn; |
---|
| 445 | t1=0;t2=1; |
---|
| 446 | for(int i=0;i<INERPOL_PREC;++i) |
---|
| 447 | if((bez((t1+t2)/2)-t).normSquare()>rn) t1=(t1+t2)/2; |
---|
| 448 | else t2=(t1+t2)/2; |
---|
| 449 | xy<double> linend=bez((t1+t2)/2); |
---|
| 450 | bez=bez.before((t1+t2)/2); |
---|
| 451 | rn=_nodeSizes[g.source(*e)]*_nodeScale; rn*=rn; |
---|
| 452 | t1=0;t2=1; |
---|
| 453 | for(int i=0;i<INERPOL_PREC;++i) |
---|
| 454 | if((bez((t1+t2)/2)-s).normSquare()>rn) t2=(t1+t2)/2; |
---|
| 455 | else t1=(t1+t2)/2; |
---|
| 456 | bez=bez.after((t1+t2)/2); |
---|
| 457 | os << _edgeWidths[*e]*_edgeWidthScale << " setlinewidth " |
---|
| 458 | << _edgeColors[*e].getR() << ' ' |
---|
| 459 | << _edgeColors[*e].getG() << ' ' |
---|
| 460 | << _edgeColors[*e].getB() << " setrgbcolor newpath\n" |
---|
| 461 | << bez.p1.x << ' ' << bez.p1.y << " moveto\n" |
---|
| 462 | << bez.p2.x << ' ' << bez.p2.y << ' ' |
---|
| 463 | << bez.p3.x << ' ' << bez.p3.y << ' ' |
---|
| 464 | << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n"; |
---|
| 465 | xy<double> dd(rot(linend-apoint)); |
---|
| 466 | dd*=(_edgeWidths[*e]*_edgeWidthScale+_arrowWidth)/ |
---|
| 467 | sqrt(dd.normSquare()); |
---|
| 468 | os << "newpath " << psOut(apoint) << " moveto " |
---|
| 469 | << psOut(linend+dd) << " lineto " |
---|
| 470 | << psOut(linend-dd) << " lineto closepath fill\n"; |
---|
| 471 | } |
---|
| 472 | else { |
---|
| 473 | os << _coords[g.source(*e)].x << ' ' |
---|
| 474 | << _coords[g.source(*e)].y << ' ' |
---|
| 475 | << m.x << ' ' << m.y << ' ' |
---|
| 476 | << _coords[g.target(*e)].x << ' ' |
---|
| 477 | << _coords[g.target(*e)].y << ' ' |
---|
| 478 | << _edgeColors[*e].getR() << ' ' |
---|
| 479 | << _edgeColors[*e].getG() << ' ' |
---|
| 480 | << _edgeColors[*e].getB() << ' ' |
---|
| 481 | << _edgeWidths[*e]*_edgeWidthScale << " lb\n"; |
---|
| 482 | } |
---|
| 483 | sw+=_edgeWidths[*e]*_edgeWidthScale/2.0+_parEdgeDist; |
---|
| 484 | } |
---|
| 485 | } |
---|
| 486 | } |
---|
| 487 | else for(EdgeIt e(g);e!=INVALID;++e) |
---|
| 488 | if(!_undir||g.source(e)<g.target(e)) |
---|
| 489 | if(_drawArrows) { |
---|
| 490 | xy<double> d(_coords[g.target(e)]-_coords[g.source(e)]); |
---|
| 491 | double l=sqrt(d.normSquare()); |
---|
| 492 | d/=l; |
---|
| 493 | xy<double> x1(d*_nodeScale*_nodeSizes[g.source(e)]+ |
---|
| 494 | _coords[g.source(e)]); |
---|
| 495 | os << l-(_nodeSizes[g.source(e)]+ |
---|
| 496 | _nodeSizes[g.target(e)])*_nodeScale << ' ' |
---|
| 497 | << _edgeWidths[e]*_edgeWidthScale << ' ' |
---|
| 498 | << d.x << ' ' << d.y << ' ' |
---|
| 499 | << x1.x << ' ' << x1.y << ' ' |
---|
| 500 | << _edgeColors[e].getR() << ' ' |
---|
| 501 | << _edgeColors[e].getG() << ' ' |
---|
| 502 | << _edgeColors[e].getB() << " arr\n"; |
---|
| 503 | } |
---|
| 504 | else os << _coords[g.source(e)].x << ' ' |
---|
| 505 | << _coords[g.source(e)].y << ' ' |
---|
| 506 | << _coords[g.target(e)].x << ' ' |
---|
| 507 | << _coords[g.target(e)].y << ' ' |
---|
| 508 | << _edgeColors[e].getR() << ' ' |
---|
| 509 | << _edgeColors[e].getG() << ' ' |
---|
| 510 | << _edgeColors[e].getB() << ' ' |
---|
| 511 | << _edgeWidths[e]*_edgeWidthScale << " l\n"; |
---|
| 512 | os << "grestore\n%Nodes:\ngsave\n"; |
---|
| 513 | if(_showNodes) |
---|
| 514 | for(NodeIt n(g);n!=INVALID;++n) |
---|
| 515 | os << _coords[n].x << ' ' << _coords[n].y << ' ' |
---|
| 516 | << _nodeSizes[n]*_nodeScale << ' ' |
---|
| 517 | << _nodeColors[n].getR() << ' ' |
---|
| 518 | << _nodeColors[n].getG() << ' ' |
---|
| 519 | << _nodeColors[n].getB() << " n\n"; |
---|
| 520 | if(_showNodeText) { |
---|
| 521 | os << "grestore\n%Node texts:\ngsave\n"; |
---|
| 522 | os << "/fosi " << _nodeTextSize << " def\n"; |
---|
| 523 | os << "(Helvetica) findfont fosi scalefont setfont\n"; |
---|
| 524 | os << "0 0 0 setrgbcolor\n"; |
---|
| 525 | for(NodeIt n(g);n!=INVALID;++n) |
---|
| 526 | os << _coords[n].x << ' ' << _coords[n].y |
---|
| 527 | << " (" << _nodeTexts[n] << ") cshow\n"; |
---|
| 528 | } |
---|
| 529 | os << "grestore\ngrestore\n"; |
---|
| 530 | |
---|
| 531 | //CleanUp: |
---|
| 532 | if(_pleaseRemoveOsStream) {delete &os;} |
---|
| 533 | } |
---|
| 534 | }; |
---|
| 535 | |
---|
| 536 | |
---|
| 537 | ///Generates an EPS file from a graph |
---|
| 538 | |
---|
| 539 | ///\ingroup misc |
---|
| 540 | ///Generates an EPS file from a graph. |
---|
| 541 | ///\param g is a reference to the graph to be printed |
---|
| 542 | ///\param os is a reference to the output stream. |
---|
| 543 | ///By default it is <tt>std::cout</tt> |
---|
| 544 | /// |
---|
| 545 | ///This function also has a lot of \ref named-templ-param "named parameters", |
---|
| 546 | ///they are declared as the members of class \ref GraphToEps. The following |
---|
| 547 | ///example shows how to use these parameters. |
---|
| 548 | ///\code |
---|
| 549 | /// graphToEps(g).scale(10).coords(coords) |
---|
| 550 | /// .nodeScale(2).nodeSizes(sizes) |
---|
| 551 | /// .edgeWidthScale(.4); |
---|
| 552 | ///\endcode |
---|
| 553 | ///\sa GraphToEps |
---|
| 554 | ///\sa graphToEps(G &g, char *file_name) |
---|
| 555 | template<class G> |
---|
| 556 | GraphToEps<DefaultGraphToEpsTraits<G> > |
---|
| 557 | graphToEps(G &g, std::ostream& os=std::cout) |
---|
| 558 | { |
---|
| 559 | return |
---|
| 560 | GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os)); |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | ///Generates an EPS file from a graph |
---|
| 564 | |
---|
| 565 | //\ingroup misc |
---|
| 566 | ///This function does the same as |
---|
| 567 | ///\ref graphToEps(G &g,std::ostream& os) |
---|
| 568 | ///but it writes its output into the file \c file_name |
---|
| 569 | ///instead of a stream. |
---|
| 570 | ///\sa graphToEps(G &g, std::ostream& os) |
---|
| 571 | template<class G> |
---|
| 572 | GraphToEps<DefaultGraphToEpsTraits<G> > |
---|
| 573 | graphToEps(G &g,char *file_name) |
---|
| 574 | { |
---|
| 575 | return GraphToEps<DefaultGraphToEpsTraits<G> > |
---|
| 576 | (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true)); |
---|
| 577 | } |
---|
| 578 | |
---|
| 579 | //Generates an EPS file from a graph. |
---|
| 580 | //\param g is a reference to the graph to be printed |
---|
| 581 | //\param file_name is the output file_name. |
---|
| 582 | // |
---|
| 583 | //This function also has a lot of \ref named-templ-param "named parameters", |
---|
| 584 | //they are declared as the members of class \ref GraphToEps. The following |
---|
| 585 | //example shows how to use these parameters. |
---|
| 586 | //\code |
---|
| 587 | // graphToEps(g).scale(10).coords(coords) |
---|
| 588 | // .nodeScale(2).nodeSizes(sizes) |
---|
| 589 | // .edgeWidthScale(.4); |
---|
| 590 | //\endcode |
---|
| 591 | //\sa GraphToEps |
---|
| 592 | //\todo Avoid duplicated documentation |
---|
| 593 | //\bug Exception handling is missing? (Or we can just ignore it?) |
---|
| 594 | |
---|
| 595 | } //END OF NAMESPACE LEMON |
---|
| 596 | |
---|
| 597 | #endif // LEMON_GRAPH_TO_EPS_H |
---|