1 | /* -*- C++ -*- |
---|
2 | * |
---|
3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
4 | * |
---|
5 | * Copyright (C) 2003-2007 |
---|
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_EPS_H |
---|
20 | #define LEMON_EPS_H |
---|
21 | |
---|
22 | #include<string> |
---|
23 | #include<iostream> |
---|
24 | #include<fstream> |
---|
25 | #include<sstream> |
---|
26 | #include<lemon/color.h> |
---|
27 | #include<lemon/dim2.h> |
---|
28 | |
---|
29 | ///\ingroup eps_io |
---|
30 | ///\file |
---|
31 | ///\brief Simple tool to create \c .eps files |
---|
32 | /// |
---|
33 | ///\author Alpar Juttner |
---|
34 | |
---|
35 | namespace lemon { |
---|
36 | |
---|
37 | ///\ingroup eps_io |
---|
38 | ///\brief A simple tool to create \c .eps files |
---|
39 | /// |
---|
40 | ///A simple tool to create \c .eps files |
---|
41 | ///\author Alpar Juttner |
---|
42 | class EpsDrawer |
---|
43 | { |
---|
44 | void defMacros(); |
---|
45 | void init(int x1,int y1,int x2,int y2); |
---|
46 | void init(double x1,double y1,double x2,double y2); |
---|
47 | bool local_stream; |
---|
48 | public: |
---|
49 | |
---|
50 | std::ostream &out; |
---|
51 | |
---|
52 | ///Node shapes |
---|
53 | /// |
---|
54 | enum NodeShapes { |
---|
55 | /// = 0 |
---|
56 | ///\image html nodeshape_0.png |
---|
57 | ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm |
---|
58 | CIRCLE=0, |
---|
59 | /// = 1 |
---|
60 | ///\image html nodeshape_1.png |
---|
61 | ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm |
---|
62 | /// |
---|
63 | SQUARE=1, |
---|
64 | /// = 2 |
---|
65 | ///\image html nodeshape_2.png |
---|
66 | ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm |
---|
67 | /// |
---|
68 | DIAMOND=2, |
---|
69 | /// = 3 |
---|
70 | ///\image html nodeshape_3.png |
---|
71 | ///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm |
---|
72 | /// |
---|
73 | ///\warning Not implemented |
---|
74 | MALE=3, |
---|
75 | /// = 4 |
---|
76 | ///\image html nodeshape_4.png |
---|
77 | ///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm |
---|
78 | /// |
---|
79 | ///\warning Not implemented |
---|
80 | FEMALE=4 |
---|
81 | }; |
---|
82 | ///\e |
---|
83 | |
---|
84 | ///The generated file is put to \c os. |
---|
85 | /// |
---|
86 | /// \c x and \c y determine the upper |
---|
87 | ///right corner of the bounding box. The lower left corner is (0,0). |
---|
88 | EpsDrawer(std::ostream &os,int x,int y); |
---|
89 | ///\e |
---|
90 | |
---|
91 | ///The generated file is put to \c os. |
---|
92 | /// |
---|
93 | ///(x1,y1) and (x2,y2) |
---|
94 | /// determine the lower left and the upper right corners of |
---|
95 | ///the bounding box, respectively. |
---|
96 | EpsDrawer(std::ostream &os,int x1,int y1,int x2,int y2); |
---|
97 | ///\e |
---|
98 | |
---|
99 | ///The generated file is put to \c os. |
---|
100 | /// |
---|
101 | ///\c s determines the upper |
---|
102 | ///right corner of the bounding box. The lower left corner is (0,0). |
---|
103 | EpsDrawer(std::ostream &os,dim2::Point<double> s); |
---|
104 | ///\e |
---|
105 | |
---|
106 | ///The generated file is put to \c os. |
---|
107 | /// |
---|
108 | ///\c a and \c b |
---|
109 | /// determine the lower left and the upper right corners of |
---|
110 | ///the bounding box, respectively. |
---|
111 | EpsDrawer(std::ostream &os,dim2::Point<double> a, dim2::Point<double> b); |
---|
112 | ///\e |
---|
113 | |
---|
114 | ///The generated picture is put to the file \c name. |
---|
115 | /// |
---|
116 | ///\c x and \c y determine the upper |
---|
117 | ///right corner of the bounding box. The lower left corner is (0,0). |
---|
118 | EpsDrawer(const std::string &name,int x,int y); |
---|
119 | ///\e |
---|
120 | |
---|
121 | ///The generated picture is put to the file \c name. |
---|
122 | /// |
---|
123 | ///(x1,y1) and (x2,y2) |
---|
124 | /// determine the lower left and the upper right corners of |
---|
125 | ///the bounding box, respectively. |
---|
126 | EpsDrawer(const std::string &name,int x1,int y1,int x2,int y2); |
---|
127 | ///\e |
---|
128 | |
---|
129 | ///The generated picture is put to the file \c name. |
---|
130 | /// |
---|
131 | ///\c s determines the upper |
---|
132 | ///right corner of the bounding box. The lower left corner is (0,0). |
---|
133 | EpsDrawer(const std::string &name,dim2::Point<double> s); |
---|
134 | ///\e |
---|
135 | |
---|
136 | ///The generated picture is put to the file \c name. |
---|
137 | /// |
---|
138 | ///\c a and \c b |
---|
139 | /// determine the lower left and the upper right corners of |
---|
140 | ///the bounding box, respectively. |
---|
141 | EpsDrawer(const std::string &name,dim2::Point<double> a, dim2::Point<double> b); |
---|
142 | |
---|
143 | // template<class T> EpsDrawer(std::ostream &os,BoundingBox<T> b) |
---|
144 | // template<class T> EpsDrawer(std::ostream &os,BoundingBox<T> b); |
---|
145 | |
---|
146 | ~EpsDrawer(); |
---|
147 | |
---|
148 | ///Save the current graphic state. |
---|
149 | |
---|
150 | ///This function saves the current coordinate system, and the parameters |
---|
151 | ///set by \ref color(), \ref lineWidth() etc. |
---|
152 | ///The can be \ref restore "restore()"d later. |
---|
153 | /// |
---|
154 | ///The \ref save() - \ref restore() pairs can be nested. |
---|
155 | /// |
---|
156 | EpsDrawer &save(); |
---|
157 | ///Restore the saves graphic state. |
---|
158 | |
---|
159 | EpsDrawer &restore(); |
---|
160 | |
---|
161 | ///Draw a line |
---|
162 | EpsDrawer &line(double x1,double y1,double x2,double y2); |
---|
163 | ///Draw a line from the current point |
---|
164 | EpsDrawer &lineTo(double x1,double y1); |
---|
165 | ///Move the current point |
---|
166 | EpsDrawer &moveTo(double x1,double y1); |
---|
167 | ///Draw a circle |
---|
168 | EpsDrawer &circle(double x,double y, double r); |
---|
169 | |
---|
170 | ///Draw a line |
---|
171 | template<class T> EpsDrawer &line(dim2::Point<T> p1,dim2::Point<T> p2) |
---|
172 | { |
---|
173 | return line(p1.x,p1.y,p2.x,p2.y); |
---|
174 | } |
---|
175 | ///Draw a line from the current point |
---|
176 | template<class T> EpsDrawer &lineTo(dim2::Point<T> p) |
---|
177 | { |
---|
178 | return lineTo(p.x,p.y); |
---|
179 | } |
---|
180 | ///Move the current point |
---|
181 | template<class T> EpsDrawer &moveTo(dim2::Point<T> p) |
---|
182 | { |
---|
183 | return moveTo(p.x,p.y); |
---|
184 | } |
---|
185 | ///Draw a circle |
---|
186 | template<class T> EpsDrawer &circle(dim2::Point<T> p, double r) |
---|
187 | { |
---|
188 | return circle(p.x,p.y,r); |
---|
189 | } |
---|
190 | |
---|
191 | ///Set the font size |
---|
192 | EpsDrawer &fontSize(double si); |
---|
193 | ///Set the fint type |
---|
194 | EpsDrawer &font(std::string ); |
---|
195 | ///Sets whether text output is centerized of not |
---|
196 | |
---|
197 | ///Sets whether text output is centerized of not. |
---|
198 | /// |
---|
199 | ///\warning \ref save() doesn't save this setting. |
---|
200 | /// |
---|
201 | EpsDrawer ¢erMode(bool m); |
---|
202 | ///Turn to collect mode. |
---|
203 | |
---|
204 | ///If you call this function, then the drawing operations like \ref line(), |
---|
205 | ///\ref lineTo(), \ref moveTo() will not take place immediately, but istead |
---|
206 | ///they |
---|
207 | ///are collected. These operations form a \e path. |
---|
208 | ///Then you can \ref stroke(), \ref fill(), \ref eoFill(), \ref clip() or |
---|
209 | ///\ref eoClip() it. |
---|
210 | ///When drawing, you can also use \ref closePath() to - surprise - close the |
---|
211 | ///current path. |
---|
212 | /// |
---|
213 | ///This example draws a red filled diamond. |
---|
214 | ///\code |
---|
215 | /// EpsDraw ed("diamond.eps",-1,-1,1,1); |
---|
216 | /// ed.color(1,0,0,).collect().line(0,-1,1,0).lineTo(0,1) |
---|
217 | /// .lineTo(-1,0).closePath().fill(); |
---|
218 | ///\endcode |
---|
219 | EpsDrawer &collect(); |
---|
220 | ///Close the current drawing path |
---|
221 | EpsDrawer &closePath(); |
---|
222 | ///Stroke (draw) a path |
---|
223 | |
---|
224 | ///Stroke (draw) a path. |
---|
225 | ///\sa collect() |
---|
226 | /// |
---|
227 | EpsDrawer &stroke(); |
---|
228 | ///Fill a path |
---|
229 | |
---|
230 | ///Fill a path. |
---|
231 | ///\sa collect() |
---|
232 | /// |
---|
233 | EpsDrawer &fill(); |
---|
234 | ///Even-odd fill a path |
---|
235 | |
---|
236 | ///Even-odd fill a path. |
---|
237 | ///\sa collect() |
---|
238 | /// |
---|
239 | EpsDrawer &eoFill(); |
---|
240 | ///Set a clipping area. |
---|
241 | |
---|
242 | ///This function sets a clipping area. After that, the drawing operations |
---|
243 | ///will affect only this area. |
---|
244 | ///\sa collect() |
---|
245 | /// |
---|
246 | EpsDrawer &clip(); |
---|
247 | ///Set a clipping area using even-odd rule. |
---|
248 | |
---|
249 | ///This function sets a clipping area using even-odd rule. |
---|
250 | ///After that, the drawing operations |
---|
251 | ///will affect only this area. |
---|
252 | ///\sa collect() |
---|
253 | /// |
---|
254 | EpsDrawer &eoClip(); |
---|
255 | |
---|
256 | ///Set the line width. |
---|
257 | EpsDrawer &lineWidth(double w); |
---|
258 | ///Set the style of the line ends |
---|
259 | |
---|
260 | ///\param i It can be 0, 1 or 2 |
---|
261 | /// |
---|
262 | EpsDrawer &lineCap(int i); |
---|
263 | ///Set the style of the line joins |
---|
264 | |
---|
265 | ///\param i It can be 0, 1 or 2 |
---|
266 | /// |
---|
267 | EpsDrawer &lineJoin(int i); |
---|
268 | ///Set the cut length of near parallel joining lines. |
---|
269 | EpsDrawer &miterLimit(double w); |
---|
270 | |
---|
271 | ///Set the drawing color |
---|
272 | EpsDrawer &color(double r, double g, double b); |
---|
273 | ///Set the drawing color |
---|
274 | EpsDrawer &color(Color c) |
---|
275 | { |
---|
276 | return color(c.red(),c.green(),c.blue()); |
---|
277 | } |
---|
278 | |
---|
279 | ///Draw a node shape |
---|
280 | |
---|
281 | ///Draw a node shape. |
---|
282 | /// |
---|
283 | ///\param t The shape of the drawn object |
---|
284 | ///\param x The \c x coordinate of the node |
---|
285 | ///\param y The \c y coordinate of the node |
---|
286 | ///\param r The size (radius) of the node |
---|
287 | ///\param col Color of the node. The default color is white |
---|
288 | ///\param brd Color of the node border. The default color is black |
---|
289 | EpsDrawer &node(NodeShapes t, double x, double y, double r, |
---|
290 | Color col=WHITE, Color brd=BLACK); |
---|
291 | ///Draw a node shape |
---|
292 | |
---|
293 | ///Draw a node shape. |
---|
294 | /// |
---|
295 | ///\param t The shape of the drawn object |
---|
296 | ///\param pos Position of the node |
---|
297 | ///\param r The size (radius) of the node |
---|
298 | ///\param col Color of the node. The default color is white |
---|
299 | ///\param brd Color of the node border. The default color is black |
---|
300 | template<class T> |
---|
301 | EpsDrawer &node(NodeShapes t, dim2::Point<T> pos, double r, |
---|
302 | Color col=WHITE, Color brd=BLACK) |
---|
303 | { |
---|
304 | return node(t,pos.x,pos.y,r,col,brd); |
---|
305 | } |
---|
306 | |
---|
307 | ///Translate the coordinate system |
---|
308 | EpsDrawer &translate(double x,double y); |
---|
309 | ///Translate the coordinate system |
---|
310 | template<class T> EpsDrawer &translate(dim2::Point<T> p) |
---|
311 | { |
---|
312 | return translate(p.x,p.y); |
---|
313 | } |
---|
314 | ///Rotate the coordinate system |
---|
315 | EpsDrawer &rotate(double r); |
---|
316 | ///Scale the coordinate system |
---|
317 | EpsDrawer &scale(double sx, double sy); |
---|
318 | ///Scale the coordinate system |
---|
319 | EpsDrawer &scale(double s) { return scale(s,s); } |
---|
320 | ///Scale the coordinate system |
---|
321 | template<class T> EpsDrawer &scale(dim2::Point<T> p) |
---|
322 | { |
---|
323 | return scale(p.x,p.y); |
---|
324 | } |
---|
325 | |
---|
326 | ///\e |
---|
327 | EpsDrawer &flush(); |
---|
328 | ///Clear the image |
---|
329 | EpsDrawer &clear(); |
---|
330 | |
---|
331 | ///Print a text at the current point |
---|
332 | EpsDrawer &operator<<(const std::string &s); |
---|
333 | ///Print a text at the current point |
---|
334 | EpsDrawer &operator<<(const char *s); |
---|
335 | ///Print a number at the current point |
---|
336 | EpsDrawer &operator<<(int i); |
---|
337 | ///Print a number at the current point |
---|
338 | EpsDrawer &operator<<(double d); |
---|
339 | ///Print a coordinate at the current point |
---|
340 | template<class T> |
---|
341 | EpsDrawer &operator<<(dim2::Point<T> p) |
---|
342 | { |
---|
343 | out << "((" << p.x << ',' << p.y <<")) show\n"; |
---|
344 | return *this; |
---|
345 | } |
---|
346 | |
---|
347 | }; |
---|
348 | |
---|
349 | } |
---|
350 | |
---|
351 | #endif |
---|