gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Improved and fixed dim2.h. Improved and fixed documentation. Bug fix in BoundingBox<>::operator&. Removed \author tags.
0 1 0
default
1 file changed with 68 insertions and 48 deletions:
↑ Collapse diff ↑
Show white space 24 line context
... ...
@@ -25,27 +25,24 @@
25 25
///\ingroup misc
26 26
///\file
27 27
///\brief A simple two dimensional vector and a bounding box implementation 
28 28
///
29 29
/// The class \ref lemon::dim2::Point "dim2::Point" implements
30 30
///a two dimensional vector with the usual
31 31
/// operations.
32 32
///
33 33
/// The class \ref lemon::dim2::BoundingBox "dim2::BoundingBox"
34 34
/// can be used to determine
35 35
/// the rectangular bounding box of a set of
36 36
/// \ref lemon::dim2::Point "dim2::Point"'s.
37
///
38
///\author Attila Bernath
39

	
40 37

	
41 38
namespace lemon {
42 39

	
43 40
  ///Tools for handling two dimensional coordinates
44 41

	
45 42
  ///This namespace is a storage of several
46 43
  ///tools for handling two dimensional coordinates
47 44
  namespace dim2 {
48 45

	
49 46
  /// \addtogroup misc
50 47
  /// @{
51 48

	
... ...
@@ -53,39 +50,39 @@
53 50

	
54 51
  /// A simple two dimensional vector (plainvector) implementation
55 52
  ///with the usual vector
56 53
  /// operators.
57 54
  ///
58 55
  template<typename T>
59 56
    class Point {
60 57

	
61 58
    public:
62 59

	
63 60
      typedef T Value;
64 61

	
65
      ///First co-ordinate
62
      ///First coordinate
66 63
      T x;
67
      ///Second co-ordinate
64
      ///Second coordinate
68 65
      T y;     
69 66
      
70 67
      ///Default constructor
71 68
      Point() {}
72 69

	
73 70
      ///Construct an instance from coordinates
74 71
      Point(T a, T b) : x(a), y(b) { }
75 72

	
76 73
      ///The dimension of the vector.
77 74

	
78
      ///This class give back always 2.
79
      ///
75
      ///The dimension of the vector.
76
      ///This function always returns 2. 
80 77
      int size() const { return 2; }
81 78

	
82 79
      ///Subscripting operator
83 80

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

	
88 85
      ///Const subscripting operator
89 86

	
90 87
      ///\c p[0] is \c p.x and \c p[1] is \c p.y
91 88
      ///
... ...
@@ -129,25 +126,25 @@
129 126
  
130 127
      ///Return the scalar product of two vectors
131 128
      T operator *(const Point<T>& u) const {
132 129
        return x*u.x+y*u.y;
133 130
      }
134 131
  
135 132
      ///Return the sum of two vectors
136 133
      Point<T> operator+(const Point<T> &u) const {
137 134
        Point<T> b=*this;
138 135
        return b+=u;
139 136
      }
140 137

	
141
      ///Return the neg of the vectors
138
      ///Return the negative of the vector
142 139
      Point<T> operator-() const {
143 140
        Point<T> b=*this;
144 141
        b.x=-b.x; b.y=-b.y;
145 142
        return b;
146 143
      }
147 144

	
148 145
      ///Return the difference of two vectors
149 146
      Point<T> operator-(const Point<T> &u) const {
150 147
        Point<T> b=*this;
151 148
        return b-=u;
152 149
      }
153 150

	
... ...
@@ -166,44 +163,44 @@
166 163
      ///Test equality
167 164
      bool operator==(const Point<T> &u) const {
168 165
        return (x==u.x) && (y==u.y);
169 166
      }
170 167

	
171 168
      ///Test inequality
172 169
      bool operator!=(Point u) const {
173 170
        return  (x!=u.x) || (y!=u.y);
174 171
      }
175 172

	
176 173
    };
177 174

	
178
  ///Return an Point 
175
  ///Return a Point 
179 176

	
180
  ///Return an Point
177
  ///Return a Point.
181 178
  ///\relates Point
182 179
  template <typename T>
183 180
  inline Point<T> makePoint(const T& x, const T& y) {
184 181
    return Point<T>(x, y);
185 182
  }
186 183

	
187 184
  ///Return a vector multiplied by a scalar
188 185

	
189
  ///Return a vector multiplied by a scalar
186
  ///Return a vector multiplied by a scalar.
190 187
  ///\relates Point
191 188
  template<typename T> Point<T> operator*(const T &u,const Point<T> &x) {
192 189
    return x*u;
193 190
  }
194 191

	
195 192
  ///Read a plainvector from a stream
196 193

	
197
  ///Read a plainvector from a stream
194
  ///Read a plainvector from a stream.
198 195
  ///\relates Point
199 196
  ///
200 197
  template<typename T>
201 198
  inline std::istream& operator>>(std::istream &is, Point<T> &z) {
202 199
    char c;
203 200
    if (is >> c) {
204 201
      if (c != '(') is.putback(c);
205 202
    } else {
206 203
      is.clear();
207 204
    }
208 205
    if (!(is >> z.x)) return is;
209 206
    if (is >> c) {
... ...
@@ -213,319 +210,342 @@
213 210
    }
214 211
    if (!(is >> z.y)) return is;
215 212
    if (is >> c) {
216 213
      if (c != ')') is.putback(c);
217 214
    } else {
218 215
      is.clear();
219 216
    }
220 217
    return is;
221 218
  }
222 219

	
223 220
  ///Write a plainvector to a stream
224 221

	
225
  ///Write a plainvector to a stream
222
  ///Write a plainvector to a stream.
226 223
  ///\relates Point
227 224
  ///
228 225
  template<typename T>
229 226
  inline std::ostream& operator<<(std::ostream &os, const Point<T>& z)
230 227
  {
231 228
    os << "(" << z.x << ", " << z.y << ")";
232 229
    return os;
233 230
  }
234 231

	
235 232
  ///Rotate by 90 degrees
236 233

	
237
  ///Returns its parameter rotated by 90 degrees in positive direction.
234
  ///Returns the parameter rotated by 90 degrees in positive direction.
238 235
  ///\relates Point
239 236
  ///
240 237
  template<typename T>
241 238
  inline Point<T> rot90(const Point<T> &z)
242 239
  {
243 240
    return Point<T>(-z.y,z.x);
244 241
  }
245 242

	
246 243
  ///Rotate by 180 degrees
247 244

	
248
  ///Returns its parameter rotated by 180 degrees.
245
  ///Returns the parameter rotated by 180 degrees.
249 246
  ///\relates Point
250 247
  ///
251 248
  template<typename T>
252 249
  inline Point<T> rot180(const Point<T> &z)
253 250
  {
254 251
    return Point<T>(-z.x,-z.y);
255 252
  }
256 253

	
257 254
  ///Rotate by 270 degrees
258 255

	
259
  ///Returns its parameter rotated by 90 degrees in negative direction.
256
  ///Returns the parameter rotated by 90 degrees in negative direction.
260 257
  ///\relates Point
261 258
  ///
262 259
  template<typename T>
263 260
  inline Point<T> rot270(const Point<T> &z)
264 261
  {
265 262
    return Point<T>(z.y,-z.x);
266 263
  }
267 264

	
268 265
  
269 266

	
270 267
  /// A class to calculate or store the bounding box of plainvectors.
271 268

	
272 269
  /// A class to calculate or store the bounding box of plainvectors.
273 270
  ///
274
  ///\author Attila Bernath
275 271
    template<typename T>
276 272
    class BoundingBox {
277 273
      Point<T> bottom_left, top_right;
278 274
      bool _empty;
279 275
    public:
280 276
      
281 277
      ///Default constructor: creates an empty bounding box
282 278
      BoundingBox() { _empty = true; }
283 279

	
284 280
      ///Construct an instance from one point
285 281
      BoundingBox(Point<T> a) { bottom_left=top_right=a; _empty = false; }
286 282
      
287 283
      ///Construct an instance from two points
288 284
      
289
      ///Construct an instance from two points
290
      ///\warning The coordinates of the bottom-left corner must be no more
291
      ///than those of the top-right one
285
      ///Construct an instance from two points.
286
      ///\param a The bottom left corner.
287
      ///\param b The top right corner.
288
      ///\warning The coordinates of the bottom left corner must be no more
289
      ///than those of the top right one.
292 290
      BoundingBox(Point<T> a,Point<T> b)
293 291
      {
294 292
	bottom_left=a;
295 293
	top_right=b;
296 294
	_empty = false;
297 295
      }
298 296
      
299 297
      ///Construct an instance from four numbers
300 298

	
301
      ///Construct an instance from four numbers
302
      ///\warning The coordinates of the bottom-left corner must be no more
303
      ///than those of the top-right one
299
      ///Construct an instance from four numbers.
300
      ///\param l The left side of the box.
301
      ///\param b The bottom of the box.
302
      ///\param r The right side of the box.
303
      ///\param t The top of the box.
304
      ///\warning The left side must be no more than the right side and
305
      ///bottom must be no more than the top. 
304 306
      BoundingBox(T l,T b,T r,T t)
305 307
      {
306 308
	bottom_left=Point<T>(l,b);
307 309
	top_right=Point<T>(r,t);
308 310
	_empty = false;
309 311
      }
310 312
      
311
      ///Were any points added?
313
      ///Return \c true if the bounding box is empty.
314
      
315
      ///Return \c true if the bounding box is empty (i.e. return \c false
316
      ///if at least one point was added to the box or the coordinates of
317
      ///the box were set).
318
      ///The coordinates of an empty bounding box are not defined. 
312 319
      bool empty() const {
313 320
        return _empty;
314 321
      }
315 322
      
316 323
      ///Make the BoundingBox empty
317 324
      void clear() {
318 325
        _empty=1;
319 326
      }
320 327

	
321 328
      ///Give back the bottom left corner
322 329

	
323 330
      ///Give back the bottom left corner.
324 331
      ///If the bounding box is empty, then the return value is not defined.
325 332
      Point<T> bottomLeft() const {
326 333
        return bottom_left;
327 334
      }
328 335

	
329 336
      ///Set the bottom left corner
330 337

	
331 338
      ///Set the bottom left corner.
332
      ///It should only bee used for non-empty box.
339
      ///It should only be used for non-empty box.
333 340
      void bottomLeft(Point<T> p) {
334 341
	bottom_left = p;
335 342
      }
336 343

	
337 344
      ///Give back the top right corner
338 345

	
339 346
      ///Give back the top right corner.
340 347
      ///If the bounding box is empty, then the return value is not defined.
341 348
      Point<T> topRight() const {
342 349
        return top_right;
343 350
      }
344 351

	
345 352
      ///Set the top right corner
346 353

	
347 354
      ///Set the top right corner.
348
      ///It should only bee used for non-empty box.
355
      ///It should only be used for non-empty box.
349 356
      void topRight(Point<T> p) {
350 357
	top_right = p;
351 358
      }
352 359

	
353 360
      ///Give back the bottom right corner
354 361

	
355 362
      ///Give back the bottom right corner.
356 363
      ///If the bounding box is empty, then the return value is not defined.
357 364
      Point<T> bottomRight() const {
358 365
        return Point<T>(top_right.x,bottom_left.y);
359 366
      }
360 367

	
361 368
      ///Set the bottom right corner
362 369

	
363 370
      ///Set the bottom right corner.
364
      ///It should only bee used for non-empty box.
371
      ///It should only be used for non-empty box.
365 372
      void bottomRight(Point<T> p) {
366 373
	top_right.x = p.x;
367 374
	bottom_left.y = p.y;
368 375
      }
369 376
 
370 377
      ///Give back the top left corner
371 378

	
372 379
      ///Give back the top left corner.
373 380
      ///If the bounding box is empty, then the return value is not defined.
374 381
      Point<T> topLeft() const {
375 382
        return Point<T>(bottom_left.x,top_right.y);
376 383
      }
377 384

	
378 385
      ///Set the top left corner
379 386

	
380 387
      ///Set the top left corner.
381
      ///It should only bee used for non-empty box.
388
      ///It should only be used for non-empty box.
382 389
      void topLeft(Point<T> p) {
383 390
	top_right.y = p.y;
384 391
	bottom_left.x = p.x;
385 392
      }
386 393

	
387 394
      ///Give back the bottom of the box
388 395

	
389 396
      ///Give back the bottom of the box.
390 397
      ///If the bounding box is empty, then the return value is not defined.
391 398
      T bottom() const {
392 399
        return bottom_left.y;
393 400
      }
394 401

	
395 402
      ///Set the bottom of the box
396 403

	
397 404
      ///Set the bottom of the box.
398
      ///It should only bee used for non-empty box.
405
      ///It should only be used for non-empty box.
399 406
      void bottom(T t) {
400 407
	bottom_left.y = t;
401 408
      }
402 409

	
403 410
      ///Give back the top of the box
404 411

	
405 412
      ///Give back the top of the box.
406 413
      ///If the bounding box is empty, then the return value is not defined.
407 414
      T top() const {
408 415
        return top_right.y;
409 416
      }
410 417

	
411 418
      ///Set the top of the box
412 419

	
413 420
      ///Set the top of the box.
414
      ///It should only bee used for non-empty box.
421
      ///It should only be used for non-empty box.
415 422
      void top(T t) {
416 423
	top_right.y = t;
417 424
      }
418 425

	
419 426
      ///Give back the left side of the box
420 427

	
421 428
      ///Give back the left side of the box.
422 429
      ///If the bounding box is empty, then the return value is not defined.
423 430
      T left() const {
424 431
        return bottom_left.x;
425 432
      }
426 433
 
427 434
      ///Set the left side of the box
428 435

	
429 436
      ///Set the left side of the box.
430
      ///It should only bee used for non-empty box
437
      ///It should only be used for non-empty box.
431 438
      void left(T t) {
432 439
	bottom_left.x = t;
433 440
      }
434 441

	
435 442
      /// Give back the right side of the box
436 443

	
437 444
      /// Give back the right side of the box.
438 445
      ///If the bounding box is empty, then the return value is not defined.
439 446
      T right() const {
440 447
        return top_right.x;
441 448
      }
442 449

	
443 450
      ///Set the right side of the box
444 451

	
445 452
      ///Set the right side of the box.
446
      ///It should only bee used for non-empty box
453
      ///It should only be used for non-empty box.
447 454
      void right(T t) {
448 455
	top_right.x = t;
449 456
      }
450 457

	
451 458
      ///Give back the height of the box
452 459

	
453 460
      ///Give back the height of the box.
454 461
      ///If the bounding box is empty, then the return value is not defined.
455 462
      T height() const {
456 463
        return top_right.y-bottom_left.y;
457 464
      }
458 465

	
459 466
      ///Give back the width of the box
460 467

	
461 468
      ///Give back the width of the box.
462 469
      ///If the bounding box is empty, then the return value is not defined.
463 470
      T width() const {
464 471
        return top_right.x-bottom_left.x;
465 472
      }
466 473

	
467 474
      ///Checks whether a point is inside a bounding box
468
      bool inside(const Point<T>& u){
475
      bool inside(const Point<T>& u) const {
469 476
        if (_empty)
470 477
          return false;
471 478
        else{
472 479
          return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
473 480
              (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
474 481
        }
475 482
      }
476 483
  
477 484
      ///Increments a bounding box with a point
485

	
486
      ///Increments a bounding box with a point.
487
      ///
478 488
      BoundingBox& add(const Point<T>& u){
479 489
        if (_empty){
480 490
          bottom_left=top_right=u;
481 491
          _empty = false;
482 492
        }
483 493
        else{
484 494
          if (bottom_left.x > u.x) bottom_left.x = u.x;
485 495
          if (bottom_left.y > u.y) bottom_left.y = u.y;
486 496
          if (top_right.x < u.x) top_right.x = u.x;
487 497
          if (top_right.y < u.y) top_right.y = u.y;
488 498
        }
489 499
        return *this;
490 500
      }
491 501
    
492
      ///Increments a bounding to contain another bounding box
502
      ///Increments a bounding box to contain another bounding box
503
      
504
      ///Increments a bounding box to contain another bounding box.
505
      ///
493 506
      BoundingBox& add(const BoundingBox &u){
494 507
        if ( !u.empty() ){
495 508
          this->add(u.bottomLeft());
496 509
	  this->add(u.topRight());
497 510
        }
498 511
        return *this;
499 512
      }
500 513
  
501 514
      ///Intersection of two bounding boxes
502
      BoundingBox operator &(const BoundingBox& u){
515

	
516
      ///Intersection of two bounding boxes.
517
      ///
518
      BoundingBox operator&(const BoundingBox& u) const {
503 519
        BoundingBox b;
520
        if (this->_empty || u._empty) {
521
	  b._empty = true;
522
	} else {
504 523
	b.bottom_left.x=std::max(this->bottom_left.x,u.bottom_left.x);
505 524
	b.bottom_left.y=std::max(this->bottom_left.y,u.bottom_left.y);
506 525
	b.top_right.x=std::min(this->top_right.x,u.top_right.x);
507 526
	b.top_right.y=std::min(this->top_right.y,u.top_right.y);
508
	b._empty = this->_empty || u._empty ||
509
	  b.bottom_left.x>top_right.x && b.bottom_left.y>top_right.y;
527
	  b._empty = b.bottom_left.x > b.top_right.x ||
528
	             b.bottom_left.y > b.top_right.y;
529
	} 
510 530
        return b;
511 531
      }
512 532

	
513 533
    };//class Boundingbox
514 534

	
515 535

	
516
  ///Map of x-coordinates of a dim2::Point<>-map
536
  ///Map of x-coordinates of a \ref Point "Point"-map
517 537

	
518 538
  ///\ingroup maps
519
  ///Map of x-coordinates of a dim2::Point<>-map
539
  ///Map of x-coordinates of a \ref Point "Point"-map.
520 540
  ///
521 541
  template<class M>
522 542
  class XMap 
523 543
  {
524 544
    M& _map;
525 545
  public:
526 546

	
527 547
    typedef typename M::Value::Value Value;
528 548
    typedef typename M::Key Key;
529 549
    ///\e
530 550
    XMap(M& map) : _map(map) {}
531 551
    Value operator[](Key k) const {return _map[k].x;}
... ...
@@ -561,56 +581,56 @@
561 581
    const M& _map;
562 582
  public:
563 583

	
564 584
    typedef typename M::Value::Value Value;
565 585
    typedef typename M::Key Key;
566 586
    ///\e
567 587
    ConstXMap(const M &map) : _map(map) {}
568 588
    Value operator[](Key k) const {return _map[k].x;}
569 589
  };
570 590
    
571 591
  ///Returns a \ref ConstXMap class
572 592

	
573
  ///This function just returns an \ref ConstXMap class.
593
  ///This function just returns a \ref ConstXMap class.
574 594
  ///
575 595
  ///\ingroup maps
576 596
  ///\relates ConstXMap
577 597
  template<class M> 
578 598
  inline ConstXMap<M> xMap(const M &m) 
579 599
  {
580 600
    return ConstXMap<M>(m);
581 601
  }
582 602

	
583
  ///Map of y-coordinates of a dim2::Point<>-map
603
  ///Map of y-coordinates of a \ref Point "Point"-map
584 604
    
585 605
  ///\ingroup maps
586
  ///Map of y-coordinates of a dim2::Point<>-map
606
  ///Map of y-coordinates of a \ref Point "Point"-map.
587 607
  ///
588 608
  template<class M>
589 609
  class YMap 
590 610
  {
591 611
    M& _map;
592 612
  public:
593 613

	
594 614
    typedef typename M::Value::Value Value;
595 615
    typedef typename M::Key Key;
596 616
    ///\e
597 617
    YMap(M& map) : _map(map) {}
598 618
    Value operator[](Key k) const {return _map[k].y;}
599 619
    void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
600 620
  };
601 621

	
602
  ///Returns an \ref YMap class
622
  ///Returns a \ref YMap class
603 623

	
604
  ///This function just returns an \ref YMap class.
624
  ///This function just returns a \ref YMap class.
605 625
  ///
606 626
  ///\ingroup maps
607 627
  ///\relates YMap
608 628
  template<class M> 
609 629
  inline YMap<M> yMap(M &m) 
610 630
  {
611 631
    return YMap<M>(m);
612 632
  }
613 633

	
614 634
  template<class M> 
615 635
  inline YMap<M> yMap(const M &m) 
616 636
  {
... ...
@@ -628,58 +648,58 @@
628 648
    const M& _map;
629 649
  public:
630 650

	
631 651
    typedef typename M::Value::Value Value;
632 652
    typedef typename M::Key Key;
633 653
    ///\e
634 654
    ConstYMap(const M &map) : _map(map) {}
635 655
    Value operator[](Key k) const {return _map[k].y;}
636 656
  };
637 657
    
638 658
  ///Returns a \ref ConstYMap class
639 659

	
640
  ///This function just returns an \ref ConstYMap class.
660
  ///This function just returns a \ref ConstYMap class.
641 661
  ///
642 662
  ///\ingroup maps
643 663
  ///\relates ConstYMap
644 664
  template<class M> 
645 665
  inline ConstYMap<M> yMap(const M &m) 
646 666
  {
647 667
    return ConstYMap<M>(m);
648 668
  }
649 669

	
650 670

	
651 671
    ///\brief Map of the \ref Point::normSquare() "normSquare()"
652
    ///of an \ref Point "Point"-map
672
    ///of a \ref Point "Point"-map
653 673
    ///
654 674
    ///Map of the \ref Point::normSquare() "normSquare()"
655
    ///of an \ref Point "Point"-map
675
    ///of a \ref Point "Point"-map.
656 676
    ///\ingroup maps
657 677
    ///
658 678
  template<class M>
659 679
  class NormSquareMap 
660 680
  {
661 681
    const M& _map;
662 682
  public:
663 683

	
664 684
    typedef typename M::Value::Value Value;
665 685
    typedef typename M::Key Key;
666 686
    ///\e
667 687
    NormSquareMap(const M &map) : _map(map) {}
668 688
    Value operator[](Key k) const {return _map[k].normSquare();}
669 689
  };
670 690
    
671 691
  ///Returns a \ref NormSquareMap class
672 692

	
673
  ///This function just returns an \ref NormSquareMap class.
693
  ///This function just returns a \ref NormSquareMap class.
674 694
  ///
675 695
  ///\ingroup maps
676 696
  ///\relates NormSquareMap
677 697
  template<class M> 
678 698
  inline NormSquareMap<M> normSquareMap(const M &m) 
679 699
  {
680 700
    return NormSquareMap<M>(m);
681 701
  }
682 702

	
683 703
  /// @}
684 704

	
685 705
  } //namespce dim2
0 comments (0 inline)