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