gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Rename BoundingBox to Box (ticket #126)
0 3 0
default
3 files changed with 71 insertions and 72 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_DIM2_H
20 20
#define LEMON_DIM2_H
21 21

	
22 22
#include <iostream>
23 23

	
24 24
///\ingroup misc
25 25
///\file
26 26
///\brief A simple two dimensional vector and a bounding box implementation
27 27
///
28 28
/// The class \ref lemon::dim2::Point "dim2::Point" implements
29 29
/// a two dimensional vector with the usual operations.
30 30
///
31
/// The class \ref lemon::dim2::BoundingBox "dim2::BoundingBox"
32
/// can be used to determine
31
/// The class \ref lemon::dim2::Box "dim2::Box" can be used to determine
33 32
/// the rectangular bounding box of a set of
34 33
/// \ref lemon::dim2::Point "dim2::Point"'s.
35 34

	
36 35
namespace lemon {
37 36

	
38 37
  ///Tools for handling two dimensional coordinates
39 38

	
40 39
  ///This namespace is a storage of several
41 40
  ///tools for handling two dimensional coordinates
42 41
  namespace dim2 {
43 42

	
44 43
  /// \addtogroup misc
45 44
  /// @{
46 45

	
47
  /// A simple two dimensional vector (plain vector) implementation
46
  /// Two dimensional vector (plain vector)
48 47

	
49 48
  /// A simple two dimensional vector (plain vector) implementation
50 49
  /// with the usual vector operations.
51 50
  template<typename T>
52 51
    class Point {
53 52

	
54 53
    public:
55 54

	
56 55
      typedef T Value;
57 56

	
58 57
      ///First coordinate
59 58
      T x;
60 59
      ///Second coordinate
61 60
      T y;
62 61

	
63 62
      ///Default constructor
64 63
      Point() {}
65 64

	
66 65
      ///Construct an instance from coordinates
67 66
      Point(T a, T b) : x(a), y(b) { }
68 67

	
69 68
      ///Returns the dimension of the vector (i.e. returns 2).
70 69

	
71 70
      ///The dimension of the vector.
72 71
      ///This function always returns 2.
73 72
      int size() const { return 2; }
74 73

	
75 74
      ///Subscripting operator
76 75

	
77 76
      ///\c p[0] is \c p.x and \c p[1] is \c p.y
78 77
      ///
79 78
      T& operator[](int idx) { return idx == 0 ? x : y; }
80 79

	
81 80
      ///Const subscripting operator
82 81

	
83 82
      ///\c p[0] is \c p.x and \c p[1] is \c p.y
84 83
      ///
85 84
      const T& operator[](int idx) const { return idx == 0 ? x : y; }
86 85

	
87 86
      ///Conversion constructor
88 87
      template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {}
89 88

	
90 89
      ///Give back the square of the norm of the vector
91 90
      T normSquare() const {
92 91
        return x*x+y*y;
93 92
      }
94 93

	
95 94
      ///Increment the left hand side by \c u
96 95
      Point<T>& operator +=(const Point<T>& u) {
97 96
        x += u.x;
98 97
        y += u.y;
99 98
        return *this;
100 99
      }
101 100

	
102 101
      ///Decrement the left hand side by \c u
103 102
      Point<T>& operator -=(const Point<T>& u) {
104 103
        x -= u.x;
105 104
        y -= u.y;
106 105
        return *this;
107 106
      }
108 107

	
109 108
      ///Multiply the left hand side with a scalar
110 109
      Point<T>& operator *=(const T &u) {
111 110
        x *= u;
112 111
        y *= u;
113 112
        return *this;
114 113
      }
115 114

	
116 115
      ///Divide the left hand side by a scalar
117 116
      Point<T>& operator /=(const T &u) {
118 117
        x /= u;
119 118
        y /= u;
120 119
        return *this;
121 120
      }
122 121

	
123 122
      ///Return the scalar product of two vectors
124 123
      T operator *(const Point<T>& u) const {
125 124
        return x*u.x+y*u.y;
126 125
      }
127 126

	
128 127
      ///Return the sum of two vectors
129 128
      Point<T> operator+(const Point<T> &u) const {
130 129
        Point<T> b=*this;
131 130
        return b+=u;
132 131
      }
133 132

	
134 133
      ///Return the negative of the vector
135 134
      Point<T> operator-() const {
136 135
        Point<T> b=*this;
137 136
        b.x=-b.x; b.y=-b.y;
138 137
        return b;
139 138
      }
140 139

	
141 140
      ///Return the difference of two vectors
142 141
      Point<T> operator-(const Point<T> &u) const {
143 142
        Point<T> b=*this;
144 143
        return b-=u;
145 144
      }
146 145

	
147 146
      ///Return a vector multiplied by a scalar
148 147
      Point<T> operator*(const T &u) const {
149 148
        Point<T> b=*this;
150 149
        return b*=u;
151 150
      }
152 151

	
153 152
      ///Return a vector divided by a scalar
154 153
      Point<T> operator/(const T &u) const {
155 154
        Point<T> b=*this;
156 155
        return b/=u;
157 156
      }
158 157

	
159 158
      ///Test equality
160 159
      bool operator==(const Point<T> &u) const {
161 160
        return (x==u.x) && (y==u.y);
162 161
      }
163 162

	
164 163
      ///Test inequality
165 164
      bool operator!=(Point u) const {
166 165
        return  (x!=u.x) || (y!=u.y);
167 166
      }
168 167

	
169 168
    };
170 169

	
171 170
  ///Return a Point
172 171

	
173 172
  ///Return a Point.
174 173
  ///\relates Point
175 174
  template <typename T>
176 175
  inline Point<T> makePoint(const T& x, const T& y) {
177 176
    return Point<T>(x, y);
178 177
  }
179 178

	
180 179
  ///Return a vector multiplied by a scalar
181 180

	
182 181
  ///Return a vector multiplied by a scalar.
183 182
  ///\relates Point
184 183
  template<typename T> Point<T> operator*(const T &u,const Point<T> &x) {
185 184
    return x*u;
186 185
  }
187 186

	
188 187
  ///Read a plain vector from a stream
189 188

	
190 189
  ///Read a plain vector from a stream.
191 190
  ///\relates Point
192 191
  ///
193 192
  template<typename T>
194 193
  inline std::istream& operator>>(std::istream &is, Point<T> &z) {
195 194
    char c;
196 195
    if (is >> c) {
197 196
      if (c != '(') is.putback(c);
198 197
    } else {
199 198
      is.clear();
200 199
    }
201 200
    if (!(is >> z.x)) return is;
202 201
    if (is >> c) {
203 202
      if (c != ',') is.putback(c);
204 203
    } else {
205 204
      is.clear();
206 205
    }
207 206
    if (!(is >> z.y)) return is;
208 207
    if (is >> c) {
209 208
      if (c != ')') is.putback(c);
210 209
    } else {
211 210
      is.clear();
212 211
    }
213 212
    return is;
214 213
  }
215 214

	
216 215
  ///Write a plain vector to a stream
217 216

	
218 217
  ///Write a plain vector to a stream.
219 218
  ///\relates Point
220 219
  ///
221 220
  template<typename T>
222 221
  inline std::ostream& operator<<(std::ostream &os, const Point<T>& z)
223 222
  {
224 223
    os << "(" << z.x << "," << z.y << ")";
225 224
    return os;
226 225
  }
227 226

	
228 227
  ///Rotate by 90 degrees
229 228

	
230 229
  ///Returns the parameter rotated by 90 degrees in positive direction.
231 230
  ///\relates Point
232 231
  ///
233 232
  template<typename T>
234 233
  inline Point<T> rot90(const Point<T> &z)
235 234
  {
236 235
    return Point<T>(-z.y,z.x);
237 236
  }
238 237

	
239 238
  ///Rotate by 180 degrees
240 239

	
241 240
  ///Returns the parameter rotated by 180 degrees.
242 241
  ///\relates Point
243 242
  ///
244 243
  template<typename T>
245 244
  inline Point<T> rot180(const Point<T> &z)
246 245
  {
247 246
    return Point<T>(-z.x,-z.y);
248 247
  }
249 248

	
250 249
  ///Rotate by 270 degrees
251 250

	
252 251
  ///Returns the parameter rotated by 90 degrees in negative direction.
253 252
  ///\relates Point
254 253
  ///
255 254
  template<typename T>
256 255
  inline Point<T> rot270(const Point<T> &z)
257 256
  {
258 257
    return Point<T>(z.y,-z.x);
259 258
  }
260 259

	
261 260

	
262 261

	
263
    /// A class to calculate or store the bounding box of plain vectors.
262
  /// Bounding box of plain vectors (\ref Point points).
264 263

	
265
    /// A class to calculate or store the bounding box of plain vectors.
266
    ///
267
    template<typename T>
268
    class BoundingBox {
264
  /// A class to calculate or store the bounding box of plain vectors
265
  /// (\ref Point points).
266
  template<typename T>
267
  class Box {
269 268
      Point<T> _bottom_left, _top_right;
270 269
      bool _empty;
271 270
    public:
272 271

	
273
      ///Default constructor: creates an empty bounding box
274
      BoundingBox() { _empty = true; }
272
      ///Default constructor: creates an empty box
273
      Box() { _empty = true; }
275 274

	
276
      ///Construct an instance from one point
277
      BoundingBox(Point<T> a) {
275
      ///Construct a box from one point
276
      Box(Point<T> a) {
278 277
        _bottom_left = _top_right = a;
279 278
        _empty = false;
280 279
      }
281 280

	
282
      ///Construct an instance from two points
281
      ///Construct a box from two points
283 282

	
284
      ///Construct an instance from two points.
283
      ///Construct a box from two points.
285 284
      ///\param a The bottom left corner.
286 285
      ///\param b The top right corner.
287 286
      ///\warning The coordinates of the bottom left corner must be no more
288 287
      ///than those of the top right one.
289
      BoundingBox(Point<T> a,Point<T> b)
288
      Box(Point<T> a,Point<T> b)
290 289
      {
291 290
        _bottom_left = a;
292 291
        _top_right = b;
293 292
        _empty = false;
294 293
      }
295 294

	
296
      ///Construct an instance from four numbers
295
      ///Construct a box from four numbers
297 296

	
298
      ///Construct an instance from four numbers.
297
      ///Construct a box from four numbers.
299 298
      ///\param l The left side of the box.
300 299
      ///\param b The bottom of the box.
301 300
      ///\param r The right side of the box.
302 301
      ///\param t The top of the box.
303 302
      ///\warning The left side must be no more than the right side and
304 303
      ///bottom must be no more than the top.
305
      BoundingBox(T l,T b,T r,T t)
304
      Box(T l,T b,T r,T t)
306 305
      {
307 306
        _bottom_left=Point<T>(l,b);
308 307
        _top_right=Point<T>(r,t);
309 308
        _empty = false;
310 309
      }
311 310

	
312
      ///Return \c true if the bounding box is empty.
311
      ///Return \c true if the box is empty.
313 312

	
314
      ///Return \c true if the bounding box is empty (i.e. return \c false
313
      ///Return \c true if the box is empty (i.e. return \c false
315 314
      ///if at least one point was added to the box or the coordinates of
316 315
      ///the box were set).
317 316
      ///
318
      ///The coordinates of an empty bounding box are not defined.
317
      ///The coordinates of an empty box are not defined.
319 318
      bool empty() const {
320 319
        return _empty;
321 320
      }
322 321

	
323
      ///Make the BoundingBox empty
322
      ///Make the box empty
324 323
      void clear() {
325 324
        _empty = true;
326 325
      }
327 326

	
328 327
      ///Give back the bottom left corner of the box
329 328

	
330 329
      ///Give back the bottom left corner of the box.
331
      ///If the bounding box is empty, then the return value is not defined.
330
      ///If the box is empty, then the return value is not defined.
332 331
      Point<T> bottomLeft() const {
333 332
        return _bottom_left;
334 333
      }
335 334

	
336 335
      ///Set the bottom left corner of the box
337 336

	
338 337
      ///Set the bottom left corner of the box.
339 338
      ///\pre The box must not be empty.
340 339
      void bottomLeft(Point<T> p) {
341 340
        _bottom_left = p;
342 341
      }
343 342

	
344 343
      ///Give back the top right corner of the box
345 344

	
346 345
      ///Give back the top right corner of the box.
347
      ///If the bounding box is empty, then the return value is not defined.
346
      ///If the box is empty, then the return value is not defined.
348 347
      Point<T> topRight() const {
349 348
        return _top_right;
350 349
      }
351 350

	
352 351
      ///Set the top right corner of the box
353 352

	
354 353
      ///Set the top right corner of the box.
355 354
      ///\pre The box must not be empty.
356 355
      void topRight(Point<T> p) {
357 356
        _top_right = p;
358 357
      }
359 358

	
360 359
      ///Give back the bottom right corner of the box
361 360

	
362 361
      ///Give back the bottom right corner of the box.
363
      ///If the bounding box is empty, then the return value is not defined.
362
      ///If the box is empty, then the return value is not defined.
364 363
      Point<T> bottomRight() const {
365 364
        return Point<T>(_top_right.x,_bottom_left.y);
366 365
      }
367 366

	
368 367
      ///Set the bottom right corner of the box
369 368

	
370 369
      ///Set the bottom right corner of the box.
371 370
      ///\pre The box must not be empty.
372 371
      void bottomRight(Point<T> p) {
373 372
        _top_right.x = p.x;
374 373
        _bottom_left.y = p.y;
375 374
      }
376 375

	
377 376
      ///Give back the top left corner of the box
378 377

	
379 378
      ///Give back the top left corner of the box.
380
      ///If the bounding box is empty, then the return value is not defined.
379
      ///If the box is empty, then the return value is not defined.
381 380
      Point<T> topLeft() const {
382 381
        return Point<T>(_bottom_left.x,_top_right.y);
383 382
      }
384 383

	
385 384
      ///Set the top left corner of the box
386 385

	
387 386
      ///Set the top left corner of the box.
388 387
      ///\pre The box must not be empty.
389 388
      void topLeft(Point<T> p) {
390 389
        _top_right.y = p.y;
391 390
        _bottom_left.x = p.x;
392 391
      }
393 392

	
394 393
      ///Give back the bottom of the box
395 394

	
396 395
      ///Give back the bottom of the box.
397
      ///If the bounding box is empty, then the return value is not defined.
396
      ///If the box is empty, then the return value is not defined.
398 397
      T bottom() const {
399 398
        return _bottom_left.y;
400 399
      }
401 400

	
402 401
      ///Set the bottom of the box
403 402

	
404 403
      ///Set the bottom of the box.
405 404
      ///\pre The box must not be empty.
406 405
      void bottom(T t) {
407 406
        _bottom_left.y = t;
408 407
      }
409 408

	
410 409
      ///Give back the top of the box
411 410

	
412 411
      ///Give back the top of the box.
413
      ///If the bounding box is empty, then the return value is not defined.
412
      ///If the box is empty, then the return value is not defined.
414 413
      T top() const {
415 414
        return _top_right.y;
416 415
      }
417 416

	
418 417
      ///Set the top of the box
419 418

	
420 419
      ///Set the top of the box.
421 420
      ///\pre The box must not be empty.
422 421
      void top(T t) {
423 422
        _top_right.y = t;
424 423
      }
425 424

	
426 425
      ///Give back the left side of the box
427 426

	
428 427
      ///Give back the left side of the box.
429
      ///If the bounding box is empty, then the return value is not defined.
428
      ///If the box is empty, then the return value is not defined.
430 429
      T left() const {
431 430
        return _bottom_left.x;
432 431
      }
433 432

	
434 433
      ///Set the left side of the box
435 434

	
436 435
      ///Set the left side of the box.
437 436
      ///\pre The box must not be empty.
438 437
      void left(T t) {
439 438
        _bottom_left.x = t;
440 439
      }
441 440

	
442 441
      /// Give back the right side of the box
443 442

	
444 443
      /// Give back the right side of the box.
445
      ///If the bounding box is empty, then the return value is not defined.
444
      ///If the box is empty, then the return value is not defined.
446 445
      T right() const {
447 446
        return _top_right.x;
448 447
      }
449 448

	
450 449
      ///Set the right side of the box
451 450

	
452 451
      ///Set the right side of the box.
453 452
      ///\pre The box must not be empty.
454 453
      void right(T t) {
455 454
        _top_right.x = t;
456 455
      }
457 456

	
458 457
      ///Give back the height of the box
459 458

	
460 459
      ///Give back the height of the box.
461
      ///If the bounding box is empty, then the return value is not defined.
460
      ///If the box is empty, then the return value is not defined.
462 461
      T height() const {
463 462
        return _top_right.y-_bottom_left.y;
464 463
      }
465 464

	
466 465
      ///Give back the width of the box
467 466

	
468 467
      ///Give back the width of the box.
469
      ///If the bounding box is empty, then the return value is not defined.
468
      ///If the box is empty, then the return value is not defined.
470 469
      T width() const {
471 470
        return _top_right.x-_bottom_left.x;
472 471
      }
473 472

	
474
      ///Checks whether a point is inside a bounding box
473
      ///Checks whether a point is inside the box
475 474
      bool inside(const Point<T>& u) const {
476 475
        if (_empty)
477 476
          return false;
478 477
        else {
479 478
          return ( (u.x-_bottom_left.x)*(_top_right.x-u.x) >= 0 &&
480 479
                   (u.y-_bottom_left.y)*(_top_right.y-u.y) >= 0 );
481 480
        }
482 481
      }
483 482

	
484
      ///Increments a bounding box with a point
483
      ///Increments the box with a point
485 484

	
486
      ///Increments a bounding box with a point.
485
      ///Increments the box with a point.
487 486
      ///
488
      BoundingBox& add(const Point<T>& u){
487
      Box& add(const Point<T>& u){
489 488
        if (_empty) {
490 489
          _bottom_left = _top_right = u;
491 490
          _empty = false;
492 491
        }
493 492
        else {
494 493
          if (_bottom_left.x > u.x) _bottom_left.x = u.x;
495 494
          if (_bottom_left.y > u.y) _bottom_left.y = u.y;
496 495
          if (_top_right.x < u.x) _top_right.x = u.x;
497 496
          if (_top_right.y < u.y) _top_right.y = u.y;
498 497
        }
499 498
        return *this;
500 499
      }
501 500

	
502
      ///Increments a bounding box to contain another bounding box
501
      ///Increments the box to contain another box
503 502

	
504
      ///Increments a bounding box to contain another bounding box.
503
      ///Increments the box to contain another box.
505 504
      ///
506
      BoundingBox& add(const BoundingBox &u){
505
      Box& add(const Box &u){
507 506
        if ( !u.empty() ){
508 507
          add(u._bottom_left);
509 508
          add(u._top_right);
510 509
        }
511 510
        return *this;
512 511
      }
513 512

	
514
      ///Intersection of two bounding boxes
513
      ///Intersection of two boxes
515 514

	
516
      ///Intersection of two bounding boxes.
515
      ///Intersection of two boxes.
517 516
      ///
518
      BoundingBox operator&(const BoundingBox& u) const {
519
        BoundingBox b;
517
      Box operator&(const Box& u) const {
518
        Box b;
520 519
        if (_empty || u._empty) {
521 520
          b._empty = true;
522 521
        } else {
523 522
          b._bottom_left.x = std::max(_bottom_left.x, u._bottom_left.x);
524 523
          b._bottom_left.y = std::max(_bottom_left.y, u._bottom_left.y);
525 524
          b._top_right.x = std::min(_top_right.x, u._top_right.x);
526 525
          b._top_right.y = std::min(_top_right.y, u._top_right.y);
527 526
          b._empty = b._bottom_left.x > b._top_right.x ||
528 527
                     b._bottom_left.y > b._top_right.y;
529 528
        }
530 529
        return b;
531 530
      }
532 531

	
533
    };//class Boundingbox
532
  };//class Box
534 533

	
535 534

	
536
  ///Read a bounding box from a stream
535
  ///Read a box from a stream
537 536

	
538
  ///Read a bounding box from a stream.
539
  ///\relates BoundingBox
537
  ///Read a box from a stream.
538
  ///\relates Box
540 539
  template<typename T>
541
  inline std::istream& operator>>(std::istream &is, BoundingBox<T>& b) {
540
  inline std::istream& operator>>(std::istream &is, Box<T>& b) {
542 541
    char c;
543 542
    Point<T> p;
544 543
    if (is >> c) {
545 544
      if (c != '(') is.putback(c);
546 545
    } else {
547 546
      is.clear();
548 547
    }
549 548
    if (!(is >> p)) return is;
550 549
    b.bottomLeft(p);
551 550
    if (is >> c) {
552 551
      if (c != ',') is.putback(c);
553 552
    } else {
554 553
      is.clear();
555 554
    }
556 555
    if (!(is >> p)) return is;
557 556
    b.topRight(p);
558 557
    if (is >> c) {
559 558
      if (c != ')') is.putback(c);
560 559
    } else {
561 560
      is.clear();
562 561
    }
563 562
    return is;
564 563
  }
565 564

	
566
  ///Write a bounding box to a stream
565
  ///Write a box to a stream
567 566

	
568
  ///Write a bounding box to a stream.
569
  ///\relates BoundingBox
567
  ///Write a box to a stream.
568
  ///\relates Box
570 569
  template<typename T>
571
  inline std::ostream& operator<<(std::ostream &os, const BoundingBox<T>& b)
570
  inline std::ostream& operator<<(std::ostream &os, const Box<T>& b)
572 571
  {
573 572
    os << "(" << b.bottomLeft() << "," << b.topRight() << ")";
574 573
    return os;
575 574
  }
576 575

	
577 576
  ///Map of x-coordinates of a \ref Point "Point"-map
578 577

	
579 578
  ///\ingroup maps
580 579
  ///Map of x-coordinates of a \ref Point "Point"-map.
581 580
  ///
582 581
  template<class M>
583 582
  class XMap
584 583
  {
585 584
    M& _map;
586 585
  public:
587 586

	
588 587
    typedef typename M::Value::Value Value;
589 588
    typedef typename M::Key Key;
590 589
    ///\e
591 590
    XMap(M& map) : _map(map) {}
592 591
    Value operator[](Key k) const {return _map[k].x;}
593 592
    void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
594 593
  };
595 594

	
596 595
  ///Returns an \ref XMap class
597 596

	
598 597
  ///This function just returns an \ref XMap class.
599 598
  ///
600 599
  ///\ingroup maps
601 600
  ///\relates XMap
602 601
  template<class M>
603 602
  inline XMap<M> xMap(M &m)
604 603
  {
605 604
    return XMap<M>(m);
606 605
  }
607 606

	
608 607
  template<class M>
609 608
  inline XMap<M> xMap(const M &m)
610 609
  {
611 610
    return XMap<M>(m);
612 611
  }
613 612

	
614 613
  ///Constant (read only) version of \ref XMap
615 614

	
616 615
  ///\ingroup maps
617 616
  ///Constant (read only) version of \ref XMap
618 617
  ///
619 618
  template<class M>
620 619
  class ConstXMap
621 620
  {
622 621
    const M& _map;
623 622
  public:
624 623

	
625 624
    typedef typename M::Value::Value Value;
626 625
    typedef typename M::Key Key;
627 626
    ///\e
628 627
    ConstXMap(const M &map) : _map(map) {}
629 628
    Value operator[](Key k) const {return _map[k].x;}
630 629
  };
631 630

	
632 631
  ///Returns a \ref ConstXMap class
633 632

	
634 633
  ///This function just returns a \ref ConstXMap class.
635 634
  ///
636 635
  ///\ingroup maps
637 636
  ///\relates ConstXMap
638 637
  template<class M>
639 638
  inline ConstXMap<M> xMap(const M &m)
640 639
  {
641 640
    return ConstXMap<M>(m);
642 641
  }
643 642

	
644 643
  ///Map of y-coordinates of a \ref Point "Point"-map
645 644

	
646 645
  ///\ingroup maps
647 646
  ///Map of y-coordinates of a \ref Point "Point"-map.
648 647
  ///
649 648
  template<class M>
650 649
  class YMap
651 650
  {
652 651
    M& _map;
653 652
  public:
654 653

	
655 654
    typedef typename M::Value::Value Value;
656 655
    typedef typename M::Key Key;
657 656
    ///\e
658 657
    YMap(M& map) : _map(map) {}
659 658
    Value operator[](Key k) const {return _map[k].y;}
660 659
    void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
661 660
  };
662 661

	
663 662
  ///Returns a \ref YMap class
664 663

	
665 664
  ///This function just returns a \ref YMap class.
666 665
  ///
667 666
  ///\ingroup maps
668 667
  ///\relates YMap
669 668
  template<class M>
670 669
  inline YMap<M> yMap(M &m)
671 670
  {
672 671
    return YMap<M>(m);
673 672
  }
674 673

	
675 674
  template<class M>
676 675
  inline YMap<M> yMap(const M &m)
677 676
  {
678 677
    return YMap<M>(m);
679 678
  }
680 679

	
681 680
  ///Constant (read only) version of \ref YMap
682 681

	
683 682
  ///\ingroup maps
684 683
  ///Constant (read only) version of \ref YMap
685 684
  ///
686 685
  template<class M>
687 686
  class ConstYMap
688 687
  {
689 688
    const M& _map;
690 689
  public:
691 690

	
692 691
    typedef typename M::Value::Value Value;
693 692
    typedef typename M::Key Key;
694 693
    ///\e
695 694
    ConstYMap(const M &map) : _map(map) {}
696 695
    Value operator[](Key k) const {return _map[k].y;}
697 696
  };
698 697

	
699 698
  ///Returns a \ref ConstYMap class
700 699

	
701 700
  ///This function just returns a \ref ConstYMap class.
702 701
  ///
703 702
  ///\ingroup maps
704 703
  ///\relates ConstYMap
705 704
  template<class M>
706 705
  inline ConstYMap<M> yMap(const M &m)
707 706
  {
708 707
    return ConstYMap<M>(m);
709 708
  }
710 709

	
711 710

	
712 711
  ///\brief Map of the \ref Point::normSquare() "normSquare()"
713 712
  ///of a \ref Point "Point"-map
714 713
  ///
715 714
  ///Map of the \ref Point::normSquare() "normSquare()"
716 715
  ///of a \ref Point "Point"-map.
717 716
  ///\ingroup maps
718 717
  template<class M>
719 718
  class NormSquareMap
720 719
  {
721 720
    const M& _map;
722 721
  public:
723 722

	
724 723
    typedef typename M::Value::Value Value;
725 724
    typedef typename M::Key Key;
726 725
    ///\e
727 726
    NormSquareMap(const M &map) : _map(map) {}
728 727
    Value operator[](Key k) const {return _map[k].normSquare();}
729 728
  };
730 729

	
731 730
  ///Returns a \ref NormSquareMap class
732 731

	
733 732
  ///This function just returns a \ref NormSquareMap class.
734 733
  ///
735 734
  ///\ingroup maps
736 735
  ///\relates NormSquareMap
737 736
  template<class M>
738 737
  inline NormSquareMap<M> normSquareMap(const M &m)
739 738
  {
740 739
    return NormSquareMap<M>(m);
741 740
  }
742 741

	
743 742
  /// @}
744 743

	
745 744
  } //namespce dim2
746 745

	
747 746
} //namespace lemon
748 747

	
749 748
#endif //LEMON_DIM2_H
Ignore white space 24576 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_GRAPH_TO_EPS_H
20 20
#define LEMON_GRAPH_TO_EPS_H
21 21

	
22 22
#include<iostream>
23 23
#include<fstream>
24 24
#include<sstream>
25 25
#include<algorithm>
26 26
#include<vector>
27 27

	
28 28
#ifndef WIN32
29 29
#include<sys/time.h>
30 30
#include<ctime>
31 31
#else
32 32
#define WIN32_LEAN_AND_MEAN
33 33
#define NOMINMAX
34 34
#include<windows.h>
35 35
#endif
36 36

	
37 37
#include<lemon/math.h>
38 38
#include<lemon/core.h>
39 39
#include<lemon/dim2.h>
40 40
#include<lemon/maps.h>
41 41
#include<lemon/color.h>
42 42
#include<lemon/bits/bezier.h>
43 43

	
44 44

	
45 45
///\ingroup eps_io
46 46
///\file
47 47
///\brief A well configurable tool for visualizing graphs
48 48

	
49 49
namespace lemon {
50 50

	
51 51
  namespace _graph_to_eps_bits {
52 52
    template<class MT>
53 53
    class _NegY {
54 54
    public:
55 55
      typedef typename MT::Key Key;
56 56
      typedef typename MT::Value Value;
57 57
      const MT &map;
58 58
      int yscale;
59 59
      _NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {}
60 60
      Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);}
61 61
    };
62 62
  }
63 63

	
64 64
///Default traits class of \ref GraphToEps
65 65

	
66 66
///Default traits class of \ref GraphToEps.
67 67
///
68 68
///\c G is the type of the underlying graph.
69 69
template<class G>
70 70
struct DefaultGraphToEpsTraits
71 71
{
72 72
  typedef G Graph;
73 73
  typedef typename Graph::Node Node;
74 74
  typedef typename Graph::NodeIt NodeIt;
75 75
  typedef typename Graph::Arc Arc;
76 76
  typedef typename Graph::ArcIt ArcIt;
77 77
  typedef typename Graph::InArcIt InArcIt;
78 78
  typedef typename Graph::OutArcIt OutArcIt;
79 79

	
80 80

	
81 81
  const Graph &g;
82 82

	
83 83
  std::ostream& os;
84 84

	
85 85
  typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType;
86 86
  CoordsMapType _coords;
87 87
  ConstMap<typename Graph::Node,double > _nodeSizes;
88 88
  ConstMap<typename Graph::Node,int > _nodeShapes;
89 89

	
90 90
  ConstMap<typename Graph::Node,Color > _nodeColors;
91 91
  ConstMap<typename Graph::Arc,Color > _arcColors;
92 92

	
93 93
  ConstMap<typename Graph::Arc,double > _arcWidths;
94 94

	
95 95
  double _arcWidthScale;
96 96

	
97 97
  double _nodeScale;
98 98
  double _xBorder, _yBorder;
99 99
  double _scale;
100 100
  double _nodeBorderQuotient;
101 101

	
102 102
  bool _drawArrows;
103 103
  double _arrowLength, _arrowWidth;
104 104

	
105 105
  bool _showNodes, _showArcs;
106 106

	
107 107
  bool _enableParallel;
108 108
  double _parArcDist;
109 109

	
110 110
  bool _showNodeText;
111 111
  ConstMap<typename Graph::Node,bool > _nodeTexts;
112 112
  double _nodeTextSize;
113 113

	
114 114
  bool _showNodePsText;
115 115
  ConstMap<typename Graph::Node,bool > _nodePsTexts;
116 116
  char *_nodePsTextsPreamble;
117 117

	
118 118
  bool _undirected;
119 119

	
120 120
  bool _pleaseRemoveOsStream;
121 121

	
122 122
  bool _scaleToA4;
123 123

	
124 124
  std::string _title;
125 125
  std::string _copyright;
126 126

	
127 127
  enum NodeTextColorType
128 128
    { DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType;
129 129
  ConstMap<typename Graph::Node,Color > _nodeTextColors;
130 130

	
131 131
  bool _autoNodeScale;
132 132
  bool _autoArcWidthScale;
133 133

	
134 134
  bool _absoluteNodeSizes;
135 135
  bool _absoluteArcWidths;
136 136

	
137 137
  bool _negY;
138 138

	
139 139
  bool _preScale;
140 140
  ///Constructor
141 141

	
142 142
  ///Constructor
143 143
  ///\param _g  Reference to the graph to be printed.
144 144
  ///\param _os Reference to the output stream.
145 145
  ///\param _os Reference to the output stream.
146 146
  ///By default it is <tt>std::cout</tt>.
147 147
  ///\param _pros If it is \c true, then the \c ostream referenced by \c _os
148 148
  ///will be explicitly deallocated by the destructor.
149 149
  DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout,
150 150
                          bool _pros=false) :
151 151
    g(_g), os(_os),
152 152
    _coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0),
153 153
    _nodeColors(WHITE), _arcColors(BLACK),
154 154
    _arcWidths(1.0), _arcWidthScale(0.003),
155 155
    _nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0),
156 156
    _nodeBorderQuotient(.1),
157 157
    _drawArrows(false), _arrowLength(1), _arrowWidth(0.3),
158 158
    _showNodes(true), _showArcs(true),
159 159
    _enableParallel(false), _parArcDist(1),
160 160
    _showNodeText(false), _nodeTexts(false), _nodeTextSize(1),
161 161
    _showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0),
162 162
    _undirected(lemon::UndirectedTagIndicator<G>::value),
163 163
    _pleaseRemoveOsStream(_pros), _scaleToA4(false),
164 164
    _nodeTextColorType(SAME_COL), _nodeTextColors(BLACK),
165 165
    _autoNodeScale(false),
166 166
    _autoArcWidthScale(false),
167 167
    _absoluteNodeSizes(false),
168 168
    _absoluteArcWidths(false),
169 169
    _negY(false),
170 170
    _preScale(true)
171 171
  {}
172 172
};
173 173

	
174 174
///Auxiliary class to implement the named parameters of \ref graphToEps()
175 175

	
176 176
///Auxiliary class to implement the named parameters of \ref graphToEps().
177 177
///
178 178
///For detailed examples see the \ref graph_to_eps_demo.cc demo file.
179 179
template<class T> class GraphToEps : public T
180 180
{
181 181
  // Can't believe it is required by the C++ standard
182 182
  using T::g;
183 183
  using T::os;
184 184

	
185 185
  using T::_coords;
186 186
  using T::_nodeSizes;
187 187
  using T::_nodeShapes;
188 188
  using T::_nodeColors;
189 189
  using T::_arcColors;
190 190
  using T::_arcWidths;
191 191

	
192 192
  using T::_arcWidthScale;
193 193
  using T::_nodeScale;
194 194
  using T::_xBorder;
195 195
  using T::_yBorder;
196 196
  using T::_scale;
197 197
  using T::_nodeBorderQuotient;
198 198

	
199 199
  using T::_drawArrows;
200 200
  using T::_arrowLength;
201 201
  using T::_arrowWidth;
202 202

	
203 203
  using T::_showNodes;
204 204
  using T::_showArcs;
205 205

	
206 206
  using T::_enableParallel;
207 207
  using T::_parArcDist;
208 208

	
209 209
  using T::_showNodeText;
210 210
  using T::_nodeTexts;
211 211
  using T::_nodeTextSize;
212 212

	
213 213
  using T::_showNodePsText;
214 214
  using T::_nodePsTexts;
215 215
  using T::_nodePsTextsPreamble;
216 216

	
217 217
  using T::_undirected;
218 218

	
219 219
  using T::_pleaseRemoveOsStream;
220 220

	
221 221
  using T::_scaleToA4;
222 222

	
223 223
  using T::_title;
224 224
  using T::_copyright;
225 225

	
226 226
  using T::NodeTextColorType;
227 227
  using T::CUST_COL;
228 228
  using T::DIST_COL;
229 229
  using T::DIST_BW;
230 230
  using T::_nodeTextColorType;
231 231
  using T::_nodeTextColors;
232 232

	
233 233
  using T::_autoNodeScale;
234 234
  using T::_autoArcWidthScale;
235 235

	
236 236
  using T::_absoluteNodeSizes;
237 237
  using T::_absoluteArcWidths;
238 238

	
239 239

	
240 240
  using T::_negY;
241 241
  using T::_preScale;
242 242

	
243 243
  // dradnats ++C eht yb deriuqer si ti eveileb t'naC
244 244

	
245 245
  typedef typename T::Graph Graph;
246 246
  typedef typename Graph::Node Node;
247 247
  typedef typename Graph::NodeIt NodeIt;
248 248
  typedef typename Graph::Arc Arc;
249 249
  typedef typename Graph::ArcIt ArcIt;
250 250
  typedef typename Graph::InArcIt InArcIt;
251 251
  typedef typename Graph::OutArcIt OutArcIt;
252 252

	
253 253
  static const int INTERPOL_PREC;
254 254
  static const double A4HEIGHT;
255 255
  static const double A4WIDTH;
256 256
  static const double A4BORDER;
257 257

	
258 258
  bool dontPrint;
259 259

	
260 260
public:
261 261
  ///Node shapes
262 262

	
263 263
  ///Node shapes.
264 264
  ///
265 265
  enum NodeShapes {
266 266
    /// = 0
267 267
    ///\image html nodeshape_0.png
268 268
    ///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm
269 269
    CIRCLE=0,
270 270
    /// = 1
271 271
    ///\image html nodeshape_1.png
272 272
    ///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm
273 273
    ///
274 274
    SQUARE=1,
275 275
    /// = 2
276 276
    ///\image html nodeshape_2.png
277 277
    ///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm
278 278
    ///
279 279
    DIAMOND=2,
280 280
    /// = 3
281 281
    ///\image html nodeshape_3.png
282 282
    ///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm
283 283
    ///
284 284
    MALE=3,
285 285
    /// = 4
286 286
    ///\image html nodeshape_4.png
287 287
    ///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm
288 288
    ///
289 289
    FEMALE=4
290 290
  };
291 291

	
292 292
private:
293 293
  class arcLess {
294 294
    const Graph &g;
295 295
  public:
296 296
    arcLess(const Graph &_g) : g(_g) {}
297 297
    bool operator()(Arc a,Arc b) const
298 298
    {
299 299
      Node ai=std::min(g.source(a),g.target(a));
300 300
      Node aa=std::max(g.source(a),g.target(a));
301 301
      Node bi=std::min(g.source(b),g.target(b));
302 302
      Node ba=std::max(g.source(b),g.target(b));
303 303
      return ai<bi ||
304 304
        (ai==bi && (aa < ba ||
305 305
                    (aa==ba && ai==g.source(a) && bi==g.target(b))));
306 306
    }
307 307
  };
308 308
  bool isParallel(Arc e,Arc f) const
309 309
  {
310 310
    return (g.source(e)==g.source(f)&&
311 311
            g.target(e)==g.target(f)) ||
312 312
      (g.source(e)==g.target(f)&&
313 313
       g.target(e)==g.source(f));
314 314
  }
315 315
  template<class TT>
316 316
  static std::string psOut(const dim2::Point<TT> &p)
317 317
    {
318 318
      std::ostringstream os;
319 319
      os << p.x << ' ' << p.y;
320 320
      return os.str();
321 321
    }
322 322
  static std::string psOut(const Color &c)
323 323
    {
324 324
      std::ostringstream os;
325 325
      os << c.red() << ' ' << c.green() << ' ' << c.blue();
326 326
      return os.str();
327 327
    }
328 328

	
329 329
public:
330 330
  GraphToEps(const T &t) : T(t), dontPrint(false) {};
331 331

	
332 332
  template<class X> struct CoordsTraits : public T {
333 333
  typedef X CoordsMapType;
334 334
    const X &_coords;
335 335
    CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {}
336 336
  };
337 337
  ///Sets the map of the node coordinates
338 338

	
339 339
  ///Sets the map of the node coordinates.
340 340
  ///\param x must be a node map with \ref dim2::Point "dim2::Point<double>" or
341 341
  ///\ref dim2::Point "dim2::Point<int>" values.
342 342
  template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) {
343 343
    dontPrint=true;
344 344
    return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x));
345 345
  }
346 346
  template<class X> struct NodeSizesTraits : public T {
347 347
    const X &_nodeSizes;
348 348
    NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {}
349 349
  };
350 350
  ///Sets the map of the node sizes
351 351

	
352 352
  ///Sets the map of the node sizes.
353 353
  ///\param x must be a node map with \c double (or convertible) values.
354 354
  template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x)
355 355
  {
356 356
    dontPrint=true;
357 357
    return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x));
358 358
  }
359 359
  template<class X> struct NodeShapesTraits : public T {
360 360
    const X &_nodeShapes;
361 361
    NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {}
362 362
  };
363 363
  ///Sets the map of the node shapes
364 364

	
365 365
  ///Sets the map of the node shapes.
366 366
  ///The available shape values
367 367
  ///can be found in \ref NodeShapes "enum NodeShapes".
368 368
  ///\param x must be a node map with \c int (or convertible) values.
369 369
  ///\sa NodeShapes
370 370
  template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x)
371 371
  {
372 372
    dontPrint=true;
373 373
    return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x));
374 374
  }
375 375
  template<class X> struct NodeTextsTraits : public T {
376 376
    const X &_nodeTexts;
377 377
    NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {}
378 378
  };
379 379
  ///Sets the text printed on the nodes
380 380

	
381 381
  ///Sets the text printed on the nodes.
382 382
  ///\param x must be a node map with type that can be pushed to a standard
383 383
  ///\c ostream.
384 384
  template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x)
385 385
  {
386 386
    dontPrint=true;
387 387
    _showNodeText=true;
388 388
    return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x));
389 389
  }
390 390
  template<class X> struct NodePsTextsTraits : public T {
391 391
    const X &_nodePsTexts;
392 392
    NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {}
393 393
  };
394 394
  ///Inserts a PostScript block to the nodes
395 395

	
396 396
  ///With this command it is possible to insert a verbatim PostScript
397 397
  ///block to the nodes.
398 398
  ///The PS current point will be moved to the center of the node before
399 399
  ///the PostScript block inserted.
400 400
  ///
401 401
  ///Before and after the block a newline character is inserted so you
402 402
  ///don't have to bother with the separators.
403 403
  ///
404 404
  ///\param x must be a node map with type that can be pushed to a standard
405 405
  ///\c ostream.
406 406
  ///
407 407
  ///\sa nodePsTextsPreamble()
408 408
  template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x)
409 409
  {
410 410
    dontPrint=true;
411 411
    _showNodePsText=true;
412 412
    return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x));
413 413
  }
414 414
  template<class X> struct ArcWidthsTraits : public T {
415 415
    const X &_arcWidths;
416 416
    ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {}
417 417
  };
418 418
  ///Sets the map of the arc widths
419 419

	
420 420
  ///Sets the map of the arc widths.
421 421
  ///\param x must be an arc map with \c double (or convertible) values.
422 422
  template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x)
423 423
  {
424 424
    dontPrint=true;
425 425
    return GraphToEps<ArcWidthsTraits<X> >(ArcWidthsTraits<X>(*this,x));
426 426
  }
427 427

	
428 428
  template<class X> struct NodeColorsTraits : public T {
429 429
    const X &_nodeColors;
430 430
    NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {}
431 431
  };
432 432
  ///Sets the map of the node colors
433 433

	
434 434
  ///Sets the map of the node colors.
435 435
  ///\param x must be a node map with \ref Color values.
436 436
  ///
437 437
  ///\sa Palette
438 438
  template<class X> GraphToEps<NodeColorsTraits<X> >
439 439
  nodeColors(const X &x)
440 440
  {
441 441
    dontPrint=true;
442 442
    return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x));
443 443
  }
444 444
  template<class X> struct NodeTextColorsTraits : public T {
445 445
    const X &_nodeTextColors;
446 446
    NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {}
447 447
  };
448 448
  ///Sets the map of the node text colors
449 449

	
450 450
  ///Sets the map of the node text colors.
451 451
  ///\param x must be a node map with \ref Color values.
452 452
  ///
453 453
  ///\sa Palette
454 454
  template<class X> GraphToEps<NodeTextColorsTraits<X> >
455 455
  nodeTextColors(const X &x)
456 456
  {
457 457
    dontPrint=true;
458 458
    _nodeTextColorType=CUST_COL;
459 459
    return GraphToEps<NodeTextColorsTraits<X> >
460 460
      (NodeTextColorsTraits<X>(*this,x));
461 461
  }
462 462
  template<class X> struct ArcColorsTraits : public T {
463 463
    const X &_arcColors;
464 464
    ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {}
465 465
  };
466 466
  ///Sets the map of the arc colors
467 467

	
468 468
  ///Sets the map of the arc colors.
469 469
  ///\param x must be an arc map with \ref Color values.
470 470
  ///
471 471
  ///\sa Palette
472 472
  template<class X> GraphToEps<ArcColorsTraits<X> >
473 473
  arcColors(const X &x)
474 474
  {
475 475
    dontPrint=true;
476 476
    return GraphToEps<ArcColorsTraits<X> >(ArcColorsTraits<X>(*this,x));
477 477
  }
478 478
  ///Sets a global scale factor for node sizes
479 479

	
480 480
  ///Sets a global scale factor for node sizes.
481 481
  ///
482 482
  /// If nodeSizes() is not given, this function simply sets the node
483 483
  /// sizes to \c d.  If nodeSizes() is given, but
484 484
  /// autoNodeScale() is not, then the node size given by
485 485
  /// nodeSizes() will be multiplied by the value \c d.
486 486
  /// If both nodeSizes() and autoNodeScale() are used, then the
487 487
  /// node sizes will be scaled in such a way that the greatest size will be
488 488
  /// equal to \c d.
489 489
  /// \sa nodeSizes()
490 490
  /// \sa autoNodeScale()
491 491
  GraphToEps<T> &nodeScale(double d=.01) {_nodeScale=d;return *this;}
492 492
  ///Turns on/off the automatic node size scaling.
493 493

	
494 494
  ///Turns on/off the automatic node size scaling.
495 495
  ///
496 496
  ///\sa nodeScale()
497 497
  ///
498 498
  GraphToEps<T> &autoNodeScale(bool b=true) {
499 499
    _autoNodeScale=b;return *this;
500 500
  }
501 501

	
502 502
  ///Turns on/off the absolutematic node size scaling.
503 503

	
504 504
  ///Turns on/off the absolutematic node size scaling.
505 505
  ///
506 506
  ///\sa nodeScale()
507 507
  ///
508 508
  GraphToEps<T> &absoluteNodeSizes(bool b=true) {
509 509
    _absoluteNodeSizes=b;return *this;
510 510
  }
511 511

	
512 512
  ///Negates the Y coordinates.
513 513
  GraphToEps<T> &negateY(bool b=true) {
514 514
    _negY=b;return *this;
515 515
  }
516 516

	
517 517
  ///Turn on/off pre-scaling
518 518

	
519 519
  ///By default graphToEps() rescales the whole image in order to avoid
520 520
  ///very big or very small bounding boxes.
521 521
  ///
522 522
  ///This (p)rescaling can be turned off with this function.
523 523
  ///
524 524
  GraphToEps<T> &preScale(bool b=true) {
525 525
    _preScale=b;return *this;
526 526
  }
527 527

	
528 528
  ///Sets a global scale factor for arc widths
529 529

	
530 530
  /// Sets a global scale factor for arc widths.
531 531
  ///
532 532
  /// If arcWidths() is not given, this function simply sets the arc
533 533
  /// widths to \c d.  If arcWidths() is given, but
534 534
  /// autoArcWidthScale() is not, then the arc withs given by
535 535
  /// arcWidths() will be multiplied by the value \c d.
536 536
  /// If both arcWidths() and autoArcWidthScale() are used, then the
537 537
  /// arc withs will be scaled in such a way that the greatest width will be
538 538
  /// equal to \c d.
539 539
  GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;}
540 540
  ///Turns on/off the automatic arc width scaling.
541 541

	
542 542
  ///Turns on/off the automatic arc width scaling.
543 543
  ///
544 544
  ///\sa arcWidthScale()
545 545
  ///
546 546
  GraphToEps<T> &autoArcWidthScale(bool b=true) {
547 547
    _autoArcWidthScale=b;return *this;
548 548
  }
549 549
  ///Turns on/off the absolutematic arc width scaling.
550 550

	
551 551
  ///Turns on/off the absolutematic arc width scaling.
552 552
  ///
553 553
  ///\sa arcWidthScale()
554 554
  ///
555 555
  GraphToEps<T> &absoluteArcWidths(bool b=true) {
556 556
    _absoluteArcWidths=b;return *this;
557 557
  }
558 558
  ///Sets a global scale factor for the whole picture
559 559
  GraphToEps<T> &scale(double d) {_scale=d;return *this;}
560 560
  ///Sets the width of the border around the picture
561 561
  GraphToEps<T> &border(double b=10) {_xBorder=_yBorder=b;return *this;}
562 562
  ///Sets the width of the border around the picture
563 563
  GraphToEps<T> &border(double x, double y) {
564 564
    _xBorder=x;_yBorder=y;return *this;
565 565
  }
566 566
  ///Sets whether to draw arrows
567 567
  GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
568 568
  ///Sets the length of the arrowheads
569 569
  GraphToEps<T> &arrowLength(double d=1.0) {_arrowLength*=d;return *this;}
570 570
  ///Sets the width of the arrowheads
571 571
  GraphToEps<T> &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;}
572 572

	
573 573
  ///Scales the drawing to fit to A4 page
574 574
  GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
575 575

	
576 576
  ///Enables parallel arcs
577 577
  GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
578 578

	
579 579
  ///Sets the distance between parallel arcs
580 580
  GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;}
581 581

	
582 582
  ///Hides the arcs
583 583
  GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;}
584 584
  ///Hides the nodes
585 585
  GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
586 586

	
587 587
  ///Sets the size of the node texts
588 588
  GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
589 589

	
590 590
  ///Sets the color of the node texts to be different from the node color
591 591

	
592 592
  ///Sets the color of the node texts to be as different from the node color
593 593
  ///as it is possible.
594 594
  GraphToEps<T> &distantColorNodeTexts()
595 595
  {_nodeTextColorType=DIST_COL;return *this;}
596 596
  ///Sets the color of the node texts to be black or white and always visible.
597 597

	
598 598
  ///Sets the color of the node texts to be black or white according to
599 599
  ///which is more different from the node color.
600 600
  GraphToEps<T> &distantBWNodeTexts()
601 601
  {_nodeTextColorType=DIST_BW;return *this;}
602 602

	
603 603
  ///Gives a preamble block for node Postscript block.
604 604

	
605 605
  ///Gives a preamble block for node Postscript block.
606 606
  ///
607 607
  ///\sa nodePsTexts()
608 608
  GraphToEps<T> & nodePsTextsPreamble(const char *str) {
609 609
    _nodePsTextsPreamble=str ;return *this;
610 610
  }
611 611
  ///Sets whether the graph is undirected
612 612

	
613 613
  ///Sets whether the graph is undirected.
614 614
  ///
615 615
  ///This setting is the default for undirected graphs.
616 616
  ///
617 617
  ///\sa directed()
618 618
   GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;}
619 619

	
620 620
  ///Sets whether the graph is directed
621 621

	
622 622
  ///Sets whether the graph is directed.
623 623
  ///Use it to show the edges as a pair of directed ones.
624 624
  ///
625 625
  ///This setting is the default for digraphs.
626 626
  ///
627 627
  ///\sa undirected()
628 628
  GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;}
629 629

	
630 630
  ///Sets the title.
631 631

	
632 632
  ///Sets the title of the generated image,
633 633
  ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of
634 634
  ///the EPS file.
635 635
  GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
636 636
  ///Sets the copyright statement.
637 637

	
638 638
  ///Sets the copyright statement of the generated image,
639 639
  ///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of
640 640
  ///the EPS file.
641 641
  GraphToEps<T> &copyright(const std::string &t) {_copyright=t;return *this;}
642 642

	
643 643
protected:
644 644
  bool isInsideNode(dim2::Point<double> p, double r,int t)
645 645
  {
646 646
    switch(t) {
647 647
    case CIRCLE:
648 648
    case MALE:
649 649
    case FEMALE:
650 650
      return p.normSquare()<=r*r;
651 651
    case SQUARE:
652 652
      return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
653 653
    case DIAMOND:
654 654
      return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
655 655
    }
656 656
    return false;
657 657
  }
658 658

	
659 659
public:
660 660
  ~GraphToEps() { }
661 661

	
662 662
  ///Draws the graph.
663 663

	
664 664
  ///Like other functions using
665 665
  ///\ref named-templ-func-param "named template parameters",
666 666
  ///this function calls the algorithm itself, i.e. in this case
667 667
  ///it draws the graph.
668 668
  void run() {
669 669
    //\todo better 'epsilon' would be nice here.
670 670
    const double EPSILON=1e-9;
671 671
    if(dontPrint) return;
672 672

	
673 673
    _graph_to_eps_bits::_NegY<typename T::CoordsMapType>
674 674
      mycoords(_coords,_negY);
675 675

	
676 676
    os << "%!PS-Adobe-2.0 EPSF-2.0\n";
677 677
    if(_title.size()>0) os << "%%Title: " << _title << '\n';
678 678
     if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
679 679
    os << "%%Creator: LEMON, graphToEps()\n";
680 680

	
681 681
    {
682 682
#ifndef WIN32
683 683
      timeval tv;
684 684
      gettimeofday(&tv, 0);
685 685

	
686 686
      char cbuf[26];
687 687
      ctime_r(&tv.tv_sec,cbuf);
688 688
      os << "%%CreationDate: " << cbuf;
689 689
#else
690 690
      SYSTEMTIME time;
691 691
      char buf1[11], buf2[9], buf3[5];
692 692

	
693 693
      GetSystemTime(&time);
694 694
      if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
695 695
                        "ddd MMM dd", buf1, 11) &&
696 696
          GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time,
697 697
                        "HH':'mm':'ss", buf2, 9) &&
698 698
          GetDateFormat(LOCALE_USER_DEFAULT, 0, &time,
699 699
                                "yyyy", buf3, 5)) {
700 700
        os << "%%CreationDate: " << buf1 << ' '
701 701
           << buf2 << ' ' << buf3 << std::endl;
702 702
      }
703 703
#endif
704 704
    }
705 705

	
706 706
    if (_autoArcWidthScale) {
707 707
      double max_w=0;
708 708
      for(ArcIt e(g);e!=INVALID;++e)
709 709
        max_w=std::max(double(_arcWidths[e]),max_w);
710 710
      //\todo better 'epsilon' would be nice here.
711 711
      if(max_w>EPSILON) {
712 712
        _arcWidthScale/=max_w;
713 713
      }
714 714
    }
715 715

	
716 716
    if (_autoNodeScale) {
717 717
      double max_s=0;
718 718
      for(NodeIt n(g);n!=INVALID;++n)
719 719
        max_s=std::max(double(_nodeSizes[n]),max_s);
720 720
      //\todo better 'epsilon' would be nice here.
721 721
      if(max_s>EPSILON) {
722 722
        _nodeScale/=max_s;
723 723
      }
724 724
    }
725 725

	
726 726
    double diag_len = 1;
727 727
    if(!(_absoluteNodeSizes&&_absoluteArcWidths)) {
728
      dim2::BoundingBox<double> bb;
728
      dim2::Box<double> bb;
729 729
      for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]);
730 730
      if (bb.empty()) {
731
        bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0));
731
        bb = dim2::Box<double>(dim2::Point<double>(0,0));
732 732
      }
733 733
      diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare());
734 734
      if(diag_len<EPSILON) diag_len = 1;
735 735
      if(!_absoluteNodeSizes) _nodeScale*=diag_len;
736 736
      if(!_absoluteArcWidths) _arcWidthScale*=diag_len;
737 737
    }
738 738

	
739
    dim2::BoundingBox<double> bb;
739
    dim2::Box<double> bb;
740 740
    for(NodeIt n(g);n!=INVALID;++n) {
741 741
      double ns=_nodeSizes[n]*_nodeScale;
742 742
      dim2::Point<double> p(ns,ns);
743 743
      switch(_nodeShapes[n]) {
744 744
      case CIRCLE:
745 745
      case SQUARE:
746 746
      case DIAMOND:
747 747
        bb.add(p+mycoords[n]);
748 748
        bb.add(-p+mycoords[n]);
749 749
        break;
750 750
      case MALE:
751 751
        bb.add(-p+mycoords[n]);
752 752
        bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]);
753 753
        break;
754 754
      case FEMALE:
755 755
        bb.add(p+mycoords[n]);
756 756
        bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]);
757 757
        break;
758 758
      }
759 759
    }
760 760
    if (bb.empty()) {
761
      bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0));
761
      bb = dim2::Box<double>(dim2::Point<double>(0,0));
762 762
    }
763 763

	
764 764
    if(_scaleToA4)
765 765
      os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
766 766
    else {
767 767
      if(_preScale) {
768 768
        //Rescale so that BoundingBox won't be neither to big nor too small.
769 769
        while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10;
770 770
        while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10;
771 771
      }
772 772

	
773 773
      os << "%%BoundingBox: "
774 774
         << int(floor(bb.left()   * _scale - _xBorder)) << ' '
775 775
         << int(floor(bb.bottom() * _scale - _yBorder)) << ' '
776 776
         << int(ceil(bb.right()  * _scale + _xBorder)) << ' '
777 777
         << int(ceil(bb.top()    * _scale + _yBorder)) << '\n';
778 778
    }
779 779

	
780 780
    os << "%%EndComments\n";
781 781

	
782 782
    //x1 y1 x2 y2 x3 y3 cr cg cb w
783 783
    os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
784 784
       << "      4 2 roll 1 index 1 index curveto stroke } bind def\n";
785 785
    os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke }"
786 786
       << " bind def\n";
787 787
    //x y r
788 788
    os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath }"
789 789
       << " bind def\n";
790 790
    //x y r
791 791
    os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
792 792
       << "      2 index 1 index sub 2 index 2 index add lineto\n"
793 793
       << "      2 index 1 index sub 2 index 2 index sub lineto\n"
794 794
       << "      2 index 1 index add 2 index 2 index sub lineto\n"
795 795
       << "      closepath pop pop pop} bind def\n";
796 796
    //x y r
797 797
    os << "/di { newpath 2 index 1 index add 2 index moveto\n"
798 798
       << "      2 index             2 index 2 index add lineto\n"
799 799
       << "      2 index 1 index sub 2 index             lineto\n"
800 800
       << "      2 index             2 index 2 index sub lineto\n"
801 801
       << "      closepath pop pop pop} bind def\n";
802 802
    // x y r cr cg cb
803 803
    os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
804 804
       << "     setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
805 805
       << "   } bind def\n";
806 806
    os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
807 807
       << "     setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
808 808
       << "   } bind def\n";
809 809
    os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
810 810
       << "     setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
811 811
       << "   } bind def\n";
812 812
    os << "/nfemale { 0 0 0 setrgbcolor 3 index "
813 813
       << _nodeBorderQuotient/(1+_nodeBorderQuotient)
814 814
       << " 1.5 mul mul setlinewidth\n"
815 815
       << "  newpath 5 index 5 index moveto "
816 816
       << "5 index 5 index 5 index 3.01 mul sub\n"
817 817
       << "  lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub"
818 818
       << " moveto\n"
819 819
       << "  5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto "
820 820
       << "stroke\n"
821 821
       << "  5 index 5 index 5 index c fill\n"
822 822
       << "  setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
823 823
       << "  } bind def\n";
824 824
    os << "/nmale {\n"
825 825
       << "  0 0 0 setrgbcolor 3 index "
826 826
       << _nodeBorderQuotient/(1+_nodeBorderQuotient)
827 827
       <<" 1.5 mul mul setlinewidth\n"
828 828
       << "  newpath 5 index 5 index moveto\n"
829 829
       << "  5 index 4 index 1 mul 1.5 mul add\n"
830 830
       << "  5 index 5 index 3 sqrt 1.5 mul mul add\n"
831 831
       << "  1 index 1 index lineto\n"
832 832
       << "  1 index 1 index 7 index sub moveto\n"
833 833
       << "  1 index 1 index lineto\n"
834 834
       << "  exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub"
835 835
       << " lineto\n"
836 836
       << "  stroke\n"
837 837
       << "  5 index 5 index 5 index c fill\n"
838 838
       << "  setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
839 839
       << "  } bind def\n";
840 840

	
841 841

	
842 842
    os << "/arrl " << _arrowLength << " def\n";
843 843
    os << "/arrw " << _arrowWidth << " def\n";
844 844
    // l dx_norm dy_norm
845 845
    os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
846 846
    //len w dx_norm dy_norm x1 y1 cr cg cb
847 847
    os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx "
848 848
       << "exch def\n"
849 849
       << "       /w exch def /len exch def\n"
850 850
      //<< "0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
851 851
       << "       newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
852 852
       << "       len w sub arrl sub dx dy lrl\n"
853 853
       << "       arrw dy dx neg lrl\n"
854 854
       << "       dx arrl w add mul dy w 2 div arrw add mul sub\n"
855 855
       << "       dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
856 856
       << "       dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
857 857
       << "       dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
858 858
       << "       arrw dy dx neg lrl\n"
859 859
       << "       len w sub arrl sub neg dx dy lrl\n"
860 860
       << "       closepath fill } bind def\n";
861 861
    os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
862 862
       << "         neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
863 863

	
864 864
    os << "\ngsave\n";
865 865
    if(_scaleToA4)
866 866
      if(bb.height()>bb.width()) {
867 867
        double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(),
868 868
                  (A4WIDTH-2*A4BORDER)/bb.width());
869 869
        os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
870 870
           << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER
871 871
           << " translate\n"
872 872
           << sc << " dup scale\n"
873 873
           << -bb.left() << ' ' << -bb.bottom() << " translate\n";
874 874
      }
875 875
      else {
876 876
        //\todo Verify centering
877 877
        double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(),
878 878
                  (A4WIDTH-2*A4BORDER)/bb.height());
879 879
        os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
880 880
           << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER
881 881
           << " translate\n"
882 882
           << sc << " dup scale\n90 rotate\n"
883 883
           << -bb.left() << ' ' << -bb.top() << " translate\n";
884 884
        }
885 885
    else if(_scale!=1.0) os << _scale << " dup scale\n";
886 886

	
887 887
    if(_showArcs) {
888 888
      os << "%Arcs:\ngsave\n";
889 889
      if(_enableParallel) {
890 890
        std::vector<Arc> el;
891 891
        for(ArcIt e(g);e!=INVALID;++e)
892 892
          if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
893 893
             &&g.source(e)!=g.target(e))
894 894
            el.push_back(e);
895 895
        std::sort(el.begin(),el.end(),arcLess(g));
896 896

	
897 897
        typename std::vector<Arc>::iterator j;
898 898
        for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) {
899 899
          for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
900 900

	
901 901
          double sw=0;
902 902
          for(typename std::vector<Arc>::iterator e=i;e!=j;++e)
903 903
            sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist;
904 904
          sw-=_parArcDist;
905 905
          sw/=-2.0;
906 906
          dim2::Point<double>
907 907
            dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]);
908 908
          double l=std::sqrt(dvec.normSquare());
909 909
          //\todo better 'epsilon' would be nice here.
910 910
          dim2::Point<double> d(dvec/std::max(l,EPSILON));
911 911
          dim2::Point<double> m;
912 912
//           m=dim2::Point<double>(mycoords[g.target(*i)]+
913 913
//                                 mycoords[g.source(*i)])/2.0;
914 914

	
915 915
//            m=dim2::Point<double>(mycoords[g.source(*i)])+
916 916
//             dvec*(double(_nodeSizes[g.source(*i)])/
917 917
//                (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)]));
918 918

	
919 919
          m=dim2::Point<double>(mycoords[g.source(*i)])+
920 920
            d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0;
921 921

	
922 922
          for(typename std::vector<Arc>::iterator e=i;e!=j;++e) {
923 923
            sw+=_arcWidths[*e]*_arcWidthScale/2.0;
924 924
            dim2::Point<double> mm=m+rot90(d)*sw/.75;
925 925
            if(_drawArrows) {
926 926
              int node_shape;
927 927
              dim2::Point<double> s=mycoords[g.source(*e)];
928 928
              dim2::Point<double> t=mycoords[g.target(*e)];
929 929
              double rn=_nodeSizes[g.target(*e)]*_nodeScale;
930 930
              node_shape=_nodeShapes[g.target(*e)];
931 931
              dim2::Bezier3 bez(s,mm,mm,t);
932 932
              double t1=0,t2=1;
933 933
              for(int ii=0;ii<INTERPOL_PREC;++ii)
934 934
                if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2;
935 935
                else t1=(t1+t2)/2;
936 936
              dim2::Point<double> apoint=bez((t1+t2)/2);
937 937
              rn = _arrowLength+_arcWidths[*e]*_arcWidthScale;
938 938
              rn*=rn;
939 939
              t2=(t1+t2)/2;t1=0;
940 940
              for(int ii=0;ii<INTERPOL_PREC;++ii)
941 941
                if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2;
942 942
                else t2=(t1+t2)/2;
943 943
              dim2::Point<double> linend=bez((t1+t2)/2);
944 944
              bez=bez.before((t1+t2)/2);
945 945
//               rn=_nodeSizes[g.source(*e)]*_nodeScale;
946 946
//               node_shape=_nodeShapes[g.source(*e)];
947 947
//               t1=0;t2=1;
948 948
//               for(int i=0;i<INTERPOL_PREC;++i)
949 949
//                 if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape))
950 950
//                   t1=(t1+t2)/2;
951 951
//                 else t2=(t1+t2)/2;
952 952
//               bez=bez.after((t1+t2)/2);
953 953
              os << _arcWidths[*e]*_arcWidthScale << " setlinewidth "
954 954
                 << _arcColors[*e].red() << ' '
955 955
                 << _arcColors[*e].green() << ' '
956 956
                 << _arcColors[*e].blue() << " setrgbcolor newpath\n"
957 957
                 << bez.p1.x << ' ' <<  bez.p1.y << " moveto\n"
958 958
                 << bez.p2.x << ' ' << bez.p2.y << ' '
959 959
                 << bez.p3.x << ' ' << bez.p3.y << ' '
960 960
                 << bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n";
961 961
              dim2::Point<double> dd(rot90(linend-apoint));
962 962
              dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/
963 963
                std::sqrt(dd.normSquare());
964 964
              os << "newpath " << psOut(apoint) << " moveto "
965 965
                 << psOut(linend+dd) << " lineto "
966 966
                 << psOut(linend-dd) << " lineto closepath fill\n";
967 967
            }
968 968
            else {
969 969
              os << mycoords[g.source(*e)].x << ' '
970 970
                 << mycoords[g.source(*e)].y << ' '
971 971
                 << mm.x << ' ' << mm.y << ' '
972 972
                 << mycoords[g.target(*e)].x << ' '
973 973
                 << mycoords[g.target(*e)].y << ' '
974 974
                 << _arcColors[*e].red() << ' '
975 975
                 << _arcColors[*e].green() << ' '
976 976
                 << _arcColors[*e].blue() << ' '
977 977
                 << _arcWidths[*e]*_arcWidthScale << " lb\n";
978 978
            }
979 979
            sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist;
980 980
          }
981 981
        }
982 982
      }
983 983
      else for(ArcIt e(g);e!=INVALID;++e)
984 984
        if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
985 985
           &&g.source(e)!=g.target(e)) {
986 986
          if(_drawArrows) {
987 987
            dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]);
988 988
            double rn=_nodeSizes[g.target(e)]*_nodeScale;
989 989
            int node_shape=_nodeShapes[g.target(e)];
990 990
            double t1=0,t2=1;
991 991
            for(int i=0;i<INTERPOL_PREC;++i)
992 992
              if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2;
993 993
              else t2=(t1+t2)/2;
994 994
            double l=std::sqrt(d.normSquare());
995 995
            d/=l;
996 996

	
997 997
            os << l*(1-(t1+t2)/2) << ' '
998 998
               << _arcWidths[e]*_arcWidthScale << ' '
999 999
               << d.x << ' ' << d.y << ' '
1000 1000
               << mycoords[g.source(e)].x << ' '
1001 1001
               << mycoords[g.source(e)].y << ' '
1002 1002
               << _arcColors[e].red() << ' '
1003 1003
               << _arcColors[e].green() << ' '
1004 1004
               << _arcColors[e].blue() << " arr\n";
1005 1005
          }
1006 1006
          else os << mycoords[g.source(e)].x << ' '
1007 1007
                  << mycoords[g.source(e)].y << ' '
1008 1008
                  << mycoords[g.target(e)].x << ' '
1009 1009
                  << mycoords[g.target(e)].y << ' '
1010 1010
                  << _arcColors[e].red() << ' '
1011 1011
                  << _arcColors[e].green() << ' '
1012 1012
                  << _arcColors[e].blue() << ' '
1013 1013
                  << _arcWidths[e]*_arcWidthScale << " l\n";
1014 1014
        }
1015 1015
      os << "grestore\n";
1016 1016
    }
1017 1017
    if(_showNodes) {
1018 1018
      os << "%Nodes:\ngsave\n";
1019 1019
      for(NodeIt n(g);n!=INVALID;++n) {
1020 1020
        os << mycoords[n].x << ' ' << mycoords[n].y << ' '
1021 1021
           << _nodeSizes[n]*_nodeScale << ' '
1022 1022
           << _nodeColors[n].red() << ' '
1023 1023
           << _nodeColors[n].green() << ' '
1024 1024
           << _nodeColors[n].blue() << ' ';
1025 1025
        switch(_nodeShapes[n]) {
1026 1026
        case CIRCLE:
1027 1027
          os<< "nc";break;
1028 1028
        case SQUARE:
1029 1029
          os<< "nsq";break;
1030 1030
        case DIAMOND:
1031 1031
          os<< "ndi";break;
1032 1032
        case MALE:
1033 1033
          os<< "nmale";break;
1034 1034
        case FEMALE:
1035 1035
          os<< "nfemale";break;
1036 1036
        }
1037 1037
        os<<'\n';
1038 1038
      }
1039 1039
      os << "grestore\n";
1040 1040
    }
1041 1041
    if(_showNodeText) {
1042 1042
      os << "%Node texts:\ngsave\n";
1043 1043
      os << "/fosi " << _nodeTextSize << " def\n";
1044 1044
      os << "(Helvetica) findfont fosi scalefont setfont\n";
1045 1045
      for(NodeIt n(g);n!=INVALID;++n) {
1046 1046
        switch(_nodeTextColorType) {
1047 1047
        case DIST_COL:
1048 1048
          os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n";
1049 1049
          break;
1050 1050
        case DIST_BW:
1051 1051
          os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n";
1052 1052
          break;
1053 1053
        case CUST_COL:
1054 1054
          os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n";
1055 1055
          break;
1056 1056
        default:
1057 1057
          os << "0 0 0 setrgbcolor\n";
1058 1058
        }
1059 1059
        os << mycoords[n].x << ' ' << mycoords[n].y
1060 1060
           << " (" << _nodeTexts[n] << ") cshow\n";
1061 1061
      }
1062 1062
      os << "grestore\n";
1063 1063
    }
1064 1064
    if(_showNodePsText) {
1065 1065
      os << "%Node PS blocks:\ngsave\n";
1066 1066
      for(NodeIt n(g);n!=INVALID;++n)
1067 1067
        os << mycoords[n].x << ' ' << mycoords[n].y
1068 1068
           << " moveto\n" << _nodePsTexts[n] << "\n";
1069 1069
      os << "grestore\n";
1070 1070
    }
1071 1071

	
1072 1072
    os << "grestore\nshowpage\n";
1073 1073

	
1074 1074
    //CleanUp:
1075 1075
    if(_pleaseRemoveOsStream) {delete &os;}
1076 1076
  }
1077 1077

	
1078 1078
  ///\name Aliases
1079 1079
  ///These are just some aliases to other parameter setting functions.
1080 1080

	
1081 1081
  ///@{
1082 1082

	
1083 1083
  ///An alias for arcWidths()
1084 1084
  template<class X> GraphToEps<ArcWidthsTraits<X> > edgeWidths(const X &x)
1085 1085
  {
1086 1086
    return arcWidths(x);
1087 1087
  }
1088 1088

	
1089 1089
  ///An alias for arcColors()
1090 1090
  template<class X> GraphToEps<ArcColorsTraits<X> >
1091 1091
  edgeColors(const X &x)
1092 1092
  {
1093 1093
    return arcColors(x);
1094 1094
  }
1095 1095

	
1096 1096
  ///An alias for arcWidthScale()
1097 1097
  GraphToEps<T> &edgeWidthScale(double d) {return arcWidthScale(d);}
1098 1098

	
1099 1099
  ///An alias for autoArcWidthScale()
1100 1100
  GraphToEps<T> &autoEdgeWidthScale(bool b=true)
1101 1101
  {
1102 1102
    return autoArcWidthScale(b);
1103 1103
  }
1104 1104

	
1105 1105
  ///An alias for absoluteArcWidths()
1106 1106
  GraphToEps<T> &absoluteEdgeWidths(bool b=true)
1107 1107
  {
1108 1108
    return absoluteArcWidths(b);
1109 1109
  }
1110 1110

	
1111 1111
  ///An alias for parArcDist()
1112 1112
  GraphToEps<T> &parEdgeDist(double d) {return parArcDist(d);}
1113 1113

	
1114 1114
  ///An alias for hideArcs()
1115 1115
  GraphToEps<T> &hideEdges(bool b=true) {return hideArcs(b);}
1116 1116

	
1117 1117
  ///@}
1118 1118
};
1119 1119

	
1120 1120
template<class T>
1121 1121
const int GraphToEps<T>::INTERPOL_PREC = 20;
1122 1122
template<class T>
1123 1123
const double GraphToEps<T>::A4HEIGHT = 841.8897637795276;
1124 1124
template<class T>
1125 1125
const double GraphToEps<T>::A4WIDTH  = 595.275590551181;
1126 1126
template<class T>
1127 1127
const double GraphToEps<T>::A4BORDER = 15;
1128 1128

	
1129 1129

	
1130 1130
///Generates an EPS file from a graph
1131 1131

	
1132 1132
///\ingroup eps_io
1133 1133
///Generates an EPS file from a graph.
1134 1134
///\param g Reference to the graph to be printed.
1135 1135
///\param os Reference to the output stream.
1136 1136
///By default it is <tt>std::cout</tt>.
1137 1137
///
1138 1138
///This function also has a lot of
1139 1139
///\ref named-templ-func-param "named parameters",
1140 1140
///they are declared as the members of class \ref GraphToEps. The following
1141 1141
///example shows how to use these parameters.
1142 1142
///\code
1143 1143
/// graphToEps(g,os).scale(10).coords(coords)
1144 1144
///              .nodeScale(2).nodeSizes(sizes)
1145 1145
///              .arcWidthScale(.4).run();
1146 1146
///\endcode
1147 1147
///
1148 1148
///For more detailed examples see the \ref graph_to_eps_demo.cc demo file.
1149 1149
///
1150 1150
///\warning Don't forget to put the \ref GraphToEps::run() "run()"
1151 1151
///to the end of the parameter list.
1152 1152
///\sa GraphToEps
1153 1153
///\sa graphToEps(G &g, const char *file_name)
1154 1154
template<class G>
1155 1155
GraphToEps<DefaultGraphToEpsTraits<G> >
1156 1156
graphToEps(G &g, std::ostream& os=std::cout)
1157 1157
{
1158 1158
  return
1159 1159
    GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os));
1160 1160
}
1161 1161

	
1162 1162
///Generates an EPS file from a graph
1163 1163

	
1164 1164
///\ingroup eps_io
1165 1165
///This function does the same as
1166 1166
///\ref graphToEps(G &g,std::ostream& os)
1167 1167
///but it writes its output into the file \c file_name
1168 1168
///instead of a stream.
1169 1169
///\sa graphToEps(G &g, std::ostream& os)
1170 1170
template<class G>
1171 1171
GraphToEps<DefaultGraphToEpsTraits<G> >
1172 1172
graphToEps(G &g,const char *file_name)
1173 1173
{
1174 1174
  return GraphToEps<DefaultGraphToEpsTraits<G> >
1175 1175
    (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true));
1176 1176
}
1177 1177

	
1178 1178
///Generates an EPS file from a graph
1179 1179

	
1180 1180
///\ingroup eps_io
1181 1181
///This function does the same as
1182 1182
///\ref graphToEps(G &g,std::ostream& os)
1183 1183
///but it writes its output into the file \c file_name
1184 1184
///instead of a stream.
1185 1185
///\sa graphToEps(G &g, std::ostream& os)
1186 1186
template<class G>
1187 1187
GraphToEps<DefaultGraphToEpsTraits<G> >
1188 1188
graphToEps(G &g,const std::string& file_name)
1189 1189
{
1190 1190
  return GraphToEps<DefaultGraphToEpsTraits<G> >
1191 1191
    (DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name.c_str()),true));
1192 1192
}
1193 1193

	
1194 1194
} //END OF NAMESPACE LEMON
1195 1195

	
1196 1196
#endif // LEMON_GRAPH_TO_EPS_H
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <lemon/dim2.h>
20 20
#include <iostream>
21 21
#include "test_tools.h"
22 22

	
23 23
using namespace std;
24 24
using namespace lemon;
25 25

	
26 26
int main()
27 27
{
28 28
  typedef dim2::Point<int> Point;
29 29

	
30 30
  Point p;
31 31
  check(p.size()==2, "Wrong dim2::Point initialization.");
32 32

	
33 33
  Point a(1,2);
34 34
  Point b(3,4);
35 35
  check(a[0]==1 && a[1]==2, "Wrong dim2::Point initialization.");
36 36

	
37 37
  p = a+b;
38 38
  check(p.x==4 && p.y==6, "Wrong dim2::Point addition.");
39 39

	
40 40
  p = a-b;
41 41
  check(p.x==-2 && p.y==-2, "Wrong dim2::Point subtraction.");
42 42

	
43 43
  check(a.normSquare()==5,"Wrong dim2::Point norm calculation.");
44 44
  check(a*b==11, "Wrong dim2::Point scalar product.");
45 45

	
46 46
  int l=2;
47 47
  p = a*l;
48 48
  check(p.x==2 && p.y==4, "Wrong dim2::Point multiplication by a scalar.");
49 49

	
50 50
  p = b/l;
51 51
  check(p.x==1 && p.y==2, "Wrong dim2::Point division by a scalar.");
52 52

	
53
  typedef dim2::BoundingBox<int> BB;
54
  BB box1;
55
  check(box1.empty(), "Wrong empty() in dim2::BoundingBox.");
53
  typedef dim2::Box<int> Box;
54
  Box box1;
55
  check(box1.empty(), "Wrong empty() in dim2::Box.");
56 56

	
57 57
  box1.add(a);
58
  check(!box1.empty(), "Wrong empty() in dim2::BoundingBox.");
58
  check(!box1.empty(), "Wrong empty() in dim2::Box.");
59 59
  box1.add(b);
60 60

	
61 61
  check(box1.left()==1 && box1.bottom()==2 &&
62 62
        box1.right()==3 && box1.top()==4,
63
        "Wrong addition of points to dim2::BoundingBox.");
63
        "Wrong addition of points to dim2::Box.");
64 64

	
65
  check(box1.inside(Point(2,3)), "Wrong inside() in dim2::BoundingBox.");
66
  check(box1.inside(Point(1,3)), "Wrong inside() in dim2::BoundingBox.");
67
  check(!box1.inside(Point(0,3)), "Wrong inside() in dim2::BoundingBox.");
65
  check(box1.inside(Point(2,3)), "Wrong inside() in dim2::Box.");
66
  check(box1.inside(Point(1,3)), "Wrong inside() in dim2::Box.");
67
  check(!box1.inside(Point(0,3)), "Wrong inside() in dim2::Box.");
68 68

	
69
  BB box2(Point(2,2));
70
  check(!box2.empty(), "Wrong empty() in dim2::BoundingBox.");
71
  
69
  Box box2(Point(2,2));
70
  check(!box2.empty(), "Wrong empty() in dim2::Box.");
71

	
72 72
  box2.bottomLeft(Point(2,0));
73 73
  box2.topRight(Point(5,3));
74
  BB box3 = box1 & box2;
74
  Box box3 = box1 & box2;
75 75
  check(!box3.empty() &&
76
        box3.left()==2 && box3.bottom()==2 && 
76
        box3.left()==2 && box3.bottom()==2 &&
77 77
        box3.right()==3 && box3.top()==3,
78
        "Wrong intersection of two dim2::BoundingBox objects.");
79
  
78
        "Wrong intersection of two dim2::Box objects.");
79

	
80 80
  box1.add(box2);
81 81
  check(!box1.empty() &&
82 82
        box1.left()==1 && box1.bottom()==0 &&
83 83
        box1.right()==5 && box1.top()==4,
84
        "Wrong addition of two dim2::BoundingBox objects.");
84
        "Wrong addition of two dim2::Box objects.");
85 85

	
86 86
  return 0;
87 87
}
0 comments (0 inline)