gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Stream operators for Point and BoundingBox classes (ticket #126) - Add operator<< and operator>> for BoundingBox. - operator<< of Point gives space-less output.
0 1 0
default
1 file changed with 42 insertions and 1 deletions:
↑ Collapse diff ↑
Ignore white space 192 line context
... ...
@@ -128,193 +128,193 @@
128 128
      ///Return the sum of two vectors
129 129
      Point<T> operator+(const Point<T> &u) const {
130 130
        Point<T> b=*this;
131 131
        return b+=u;
132 132
      }
133 133

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

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

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

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

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

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

	
169 169
    };
170 170

	
171 171
  ///Return a Point
172 172

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

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

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

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

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

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

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

	
228 228
  ///Rotate by 90 degrees
229 229

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

	
239 239
  ///Rotate by 180 degrees
240 240

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

	
250 250
  ///Rotate by 270 degrees
251 251

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

	
261 261

	
262 262

	
263 263
    /// A class to calculate or store the bounding box of plain vectors.
264 264

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

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

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

	
282 282
      ///Construct an instance from two points
283 283

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

	
296 296
      ///Construct an instance from four numbers
297 297

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
533 533
    };//class Boundingbox
534 534

	
535 535

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

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

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

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

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

	
538 579
  ///\ingroup maps
539 580
  ///Map of x-coordinates of a \ref Point "Point"-map.
540 581
  ///
541 582
  template<class M>
542 583
  class XMap
543 584
  {
544 585
    M& _map;
545 586
  public:
546 587

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

	
555 596
  ///Returns an \ref XMap class
556 597

	
557 598
  ///This function just returns an \ref XMap class.
558 599
  ///
559 600
  ///\ingroup maps
560 601
  ///\relates XMap
561 602
  template<class M>
562 603
  inline XMap<M> xMap(M &m)
563 604
  {
564 605
    return XMap<M>(m);
565 606
  }
566 607

	
567 608
  template<class M>
568 609
  inline XMap<M> xMap(const M &m)
569 610
  {
570 611
    return XMap<M>(m);
571 612
  }
572 613

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

	
575 616
  ///\ingroup maps
576 617
  ///Constant (read only) version of \ref XMap
577 618
  ///
578 619
  template<class M>
579 620
  class ConstXMap
580 621
  {
581 622
    const M& _map;
582 623
  public:
583 624

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

	
591 632
  ///Returns a \ref ConstXMap class
592 633

	
593 634
  ///This function just returns a \ref ConstXMap class.
594 635
  ///
595 636
  ///\ingroup maps
596 637
  ///\relates ConstXMap
597 638
  template<class M>
598 639
  inline ConstXMap<M> xMap(const M &m)
599 640
  {
600 641
    return ConstXMap<M>(m);
601 642
  }
602 643

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

	
605 646
  ///\ingroup maps
606 647
  ///Map of y-coordinates of a \ref Point "Point"-map.
607 648
  ///
608 649
  template<class M>
609 650
  class YMap
610 651
  {
611 652
    M& _map;
612 653
  public:
613 654

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

	
622 663
  ///Returns a \ref YMap class
623 664

	
624 665
  ///This function just returns a \ref YMap class.
625 666
  ///
626 667
  ///\ingroup maps
627 668
  ///\relates YMap
628 669
  template<class M>
629 670
  inline YMap<M> yMap(M &m)
630 671
  {
631 672
    return YMap<M>(m);
0 comments (0 inline)