COIN-OR::LEMON - Graph Library

Ticket #126: dim2_bb_0b0f802b14aa.patch

File dim2_bb_0b0f802b14aa.patch, 12.6 KB (added by Peter Kovacs, 12 years ago)
  • lemon/dim2.h

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1216811049 -7200
    # Node ID 0b0f802b14aa29a3c940ffaec1962cb24f2220ea
    # Parent  b6732e0d38c5c5e6b21123d8425a62591491e391
    Improvements in dim2::BoundingBox (ticket #126)
    - Rename the private varibles to start with underscore.
    - Use LEMON_ASSERT() in setting functions to check the size invariant.
    - Doc improvements.
    
    diff -r b6732e0d38c5 -r 0b0f802b14aa lemon/dim2.h
    a b  
    2020#define LEMON_DIM2_H
    2121
    2222#include <iostream>
    23 #include <lemon/core.h>
     23#include <lemon/assert.h>
    2424
    2525///\ingroup misc
    2626///\file
     
    4545  /// \addtogroup misc
    4646  /// @{
    4747
    48   /// A simple two dimensional vector (plainvector) implementation
     48  /// A simple two dimensional vector (plain vector) implementation
    4949
    50   /// A simple two dimensional vector (plainvector) implementation
     50  /// A simple two dimensional vector (plain vector) implementation
    5151  /// with the usual vector operations.
    5252  template<typename T>
    5353    class Point {
     
    186186    return x*u;
    187187  }
    188188
    189   ///Read a plainvector from a stream
     189  ///Read a plain vector from a stream
    190190
    191   ///Read a plainvector from a stream.
     191  ///Read a plain vector from a stream.
    192192  ///\relates Point
    193193  ///
    194194  template<typename T>
     
    214214    return is;
    215215  }
    216216
    217   ///Write a plainvector to a stream
     217  ///Write a plain vector to a stream
    218218
    219   ///Write a plainvector to a stream.
     219  ///Write a plain vector to a stream.
    220220  ///\relates Point
    221221  ///
    222222  template<typename T>
     
    261261
    262262
    263263
    264   /// A class to calculate or store the bounding box of plainvectors.
     264    /// A class to calculate or store the bounding box of plain vectors.
    265265
    266   /// A class to calculate or store the bounding box of plainvectors.
    267   ///
     266    /// A class to calculate or store the bounding box of plain vectors.
     267    ///
    268268    template<typename T>
    269269    class BoundingBox {
    270       Point<T> bottom_left, top_right;
     270      Point<T> _bottom_left, _top_right;
    271271      bool _empty;
    272272    public:
    273273
     
    275275      BoundingBox() { _empty = true; }
    276276
    277277      ///Construct an instance from one point
    278       BoundingBox(Point<T> a) { bottom_left=top_right=a; _empty = false; }
     278      BoundingBox(Point<T> a) {
     279        _bottom_left = _top_right = a;
     280        _empty = false;
     281      }
    279282
    280283      ///Construct an instance from two points
    281284
     
    286289      ///than those of the top right one.
    287290      BoundingBox(Point<T> a,Point<T> b)
    288291      {
    289         bottom_left=a;
    290         top_right=b;
     292        _bottom_left = a;
     293        _top_right = b;
    291294        _empty = false;
     295        LEMON_ASSERT(_bottom_left.x <= _top_right.x &&
     296                     _bottom_left.y <= _top_right.y,
     297                     "The size invariant is violated");
    292298      }
    293299
    294300      ///Construct an instance from four numbers
     
    302308      ///bottom must be no more than the top.
    303309      BoundingBox(T l,T b,T r,T t)
    304310      {
    305         bottom_left=Point<T>(l,b);
    306         top_right=Point<T>(r,t);
     311        _bottom_left=Point<T>(l,b);
     312        _top_right=Point<T>(r,t);
    307313        _empty = false;
     314        LEMON_ASSERT(_bottom_left.x <= _top_right.x &&
     315                     _bottom_left.y <= _top_right.y,
     316                     "The size invariant is violated");
    308317      }
    309318
    310319      ///Return \c true if the bounding box is empty.
     
    320329
    321330      ///Make the BoundingBox empty
    322331      void clear() {
    323         _empty=1;
     332        _empty = true;
    324333      }
    325334
    326335      ///Give back the bottom left corner of the box
     
    328337      ///Give back the bottom left corner of the box.
    329338      ///If the bounding box is empty, then the return value is not defined.
    330339      Point<T> bottomLeft() const {
    331         return bottom_left;
     340        return _bottom_left;
    332341      }
    333342
    334343      ///Set the bottom left corner of the box
    335344
    336345      ///Set the bottom left corner of the box.
    337       ///It should only be used for non-empty box.
     346      ///\warning It should only be used for non-empty box.
    338347      void bottomLeft(Point<T> p) {
    339         bottom_left = p;
     348        _bottom_left = p;
     349        LEMON_ASSERT(_bottom_left.x <= _top_right.x &&
     350                     _bottom_left.y <= _top_right.y,
     351                     "The size invariant is violated");
    340352      }
    341353
    342354      ///Give back the top right corner of the box
     
    344356      ///Give back the top right corner of the box.
    345357      ///If the bounding box is empty, then the return value is not defined.
    346358      Point<T> topRight() const {
    347         return top_right;
     359        return _top_right;
    348360      }
    349361
    350362      ///Set the top right corner of the box
    351363
    352364      ///Set the top right corner of the box.
    353       ///It should only be used for non-empty box.
     365      ///\warning It should only be used for non-empty box.
    354366      void topRight(Point<T> p) {
    355         top_right = p;
     367        _top_right = p;
     368        LEMON_ASSERT(_bottom_left.x <= _top_right.x &&
     369                     _bottom_left.y <= _top_right.y,
     370                     "The size invariant is violated");
    356371      }
    357372
    358373      ///Give back the bottom right corner of the box
     
    360375      ///Give back the bottom right corner of the box.
    361376      ///If the bounding box is empty, then the return value is not defined.
    362377      Point<T> bottomRight() const {
    363         return Point<T>(top_right.x,bottom_left.y);
     378        return Point<T>(_top_right.x,_bottom_left.y);
    364379      }
    365380
    366381      ///Set the bottom right corner of the box
    367382
    368383      ///Set the bottom right corner of the box.
    369       ///It should only be used for non-empty box.
     384      ///\warning It should only be used for non-empty box.
    370385      void bottomRight(Point<T> p) {
    371         top_right.x = p.x;
    372         bottom_left.y = p.y;
     386        _top_right.x = p.x;
     387        _bottom_left.y = p.y;
     388        LEMON_ASSERT(_bottom_left.x <= _top_right.x &&
     389                     _bottom_left.y <= _top_right.y,
     390                     "The size invariant is violated");
    373391      }
    374392
    375393      ///Give back the top left corner of the box
     
    377395      ///Give back the top left corner of the box.
    378396      ///If the bounding box is empty, then the return value is not defined.
    379397      Point<T> topLeft() const {
    380         return Point<T>(bottom_left.x,top_right.y);
     398        return Point<T>(_bottom_left.x,_top_right.y);
    381399      }
    382400
    383401      ///Set the top left corner of the box
    384402
    385403      ///Set the top left corner of the box.
    386       ///It should only be used for non-empty box.
     404      ///\warning It should only be used for non-empty box.
    387405      void topLeft(Point<T> p) {
    388         top_right.y = p.y;
    389         bottom_left.x = p.x;
     406        _top_right.y = p.y;
     407        _bottom_left.x = p.x;
     408        LEMON_ASSERT(_bottom_left.x <= _top_right.x &&
     409                     _bottom_left.y <= _top_right.y,
     410                     "The size invariant is violated");
    390411      }
    391412
    392413      ///Give back the bottom of the box
     
    394415      ///Give back the bottom of the box.
    395416      ///If the bounding box is empty, then the return value is not defined.
    396417      T bottom() const {
    397         return bottom_left.y;
     418        return _bottom_left.y;
    398419      }
    399420
    400421      ///Set the bottom of the box
    401422
    402423      ///Set the bottom of the box.
    403       ///It should only be used for non-empty box.
     424      ///\warning It should only be used for non-empty box.
    404425      void bottom(T t) {
    405         bottom_left.y = t;
     426        _bottom_left.y = t;
     427        LEMON_ASSERT(_bottom_left.y <= _top_right.y,
     428                     "The size invariant is violated");
    406429      }
    407430
    408431      ///Give back the top of the box
     
    410433      ///Give back the top of the box.
    411434      ///If the bounding box is empty, then the return value is not defined.
    412435      T top() const {
    413         return top_right.y;
     436        return _top_right.y;
    414437      }
    415438
    416439      ///Set the top of the box
    417440
    418441      ///Set the top of the box.
    419       ///It should only be used for non-empty box.
     442      ///\warning It should only be used for non-empty box.
    420443      void top(T t) {
    421         top_right.y = t;
     444        _top_right.y = t;
     445        LEMON_ASSERT(_bottom_left.y <= _top_right.y,
     446                     "The size invariant is violated");
    422447      }
    423448
    424449      ///Give back the left side of the box
     
    426451      ///Give back the left side of the box.
    427452      ///If the bounding box is empty, then the return value is not defined.
    428453      T left() const {
    429         return bottom_left.x;
     454        return _bottom_left.x;
    430455      }
    431456
    432457      ///Set the left side of the box
    433458
    434459      ///Set the left side of the box.
    435       ///It should only be used for non-empty box.
     460      ///\warning It should only be used for non-empty box.
    436461      void left(T t) {
    437         bottom_left.x = t;
     462        _bottom_left.x = t;
     463        LEMON_ASSERT(_bottom_left.x <= _top_right.x,
     464                     "The size invariant is violated");
    438465      }
    439466
    440467      /// Give back the right side of the box
     
    442469      /// Give back the right side of the box.
    443470      ///If the bounding box is empty, then the return value is not defined.
    444471      T right() const {
    445         return top_right.x;
     472        return _top_right.x;
    446473      }
    447474
    448475      ///Set the right side of the box
    449476
    450477      ///Set the right side of the box.
    451       ///It should only be used for non-empty box.
     478      ///\warning It should only be used for non-empty box.
    452479      void right(T t) {
    453         top_right.x = t;
     480        _top_right.x = t;
     481        LEMON_ASSERT(_bottom_left.x <= _top_right.x,
     482                     "The size invariant is violated");
    454483      }
    455484
    456485      ///Give back the height of the box
     
    458487      ///Give back the height of the box.
    459488      ///If the bounding box is empty, then the return value is not defined.
    460489      T height() const {
    461         return top_right.y-bottom_left.y;
     490        return _top_right.y-_bottom_left.y;
    462491      }
    463492
    464493      ///Give back the width of the box
     
    466495      ///Give back the width of the box.
    467496      ///If the bounding box is empty, then the return value is not defined.
    468497      T width() const {
    469         return top_right.x-bottom_left.x;
     498        return _top_right.x-_bottom_left.x;
    470499      }
    471500
    472501      ///Checks whether a point is inside a bounding box
    473502      bool inside(const Point<T>& u) const {
    474503        if (_empty)
    475504          return false;
    476         else{
    477           return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
    478               (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
     505        else {
     506          return ( (u.x-_bottom_left.x)*(_top_right.x-u.x) >= 0 &&
     507                   (u.y-_bottom_left.y)*(_top_right.y-u.y) >= 0 );
    479508        }
    480509      }
    481510
     
    484513      ///Increments a bounding box with a point.
    485514      ///
    486515      BoundingBox& add(const Point<T>& u){
    487         if (_empty){
    488           bottom_left=top_right=u;
     516        if (_empty) {
     517          _bottom_left = _top_right = u;
    489518          _empty = false;
    490519        }
    491         else{
    492           if (bottom_left.x > u.x) bottom_left.x = u.x;
    493           if (bottom_left.y > u.y) bottom_left.y = u.y;
    494           if (top_right.x < u.x) top_right.x = u.x;
    495           if (top_right.y < u.y) top_right.y = u.y;
     520        else {
     521          if (_bottom_left.x > u.x) _bottom_left.x = u.x;
     522          if (_bottom_left.y > u.y) _bottom_left.y = u.y;
     523          if (_top_right.x < u.x) _top_right.x = u.x;
     524          if (_top_right.y < u.y) _top_right.y = u.y;
    496525        }
    497526        return *this;
    498527      }
     
    503532      ///
    504533      BoundingBox& add(const BoundingBox &u){
    505534        if ( !u.empty() ){
    506           this->add(u.bottomLeft());
    507           this->add(u.topRight());
     535          add(u._bottom_left);
     536          add(u._top_right);
    508537        }
    509538        return *this;
    510539      }
     
    515544      ///
    516545      BoundingBox operator&(const BoundingBox& u) const {
    517546        BoundingBox b;
    518         if (this->_empty || u._empty) {
     547        if (_empty || u._empty) {
    519548          b._empty = true;
    520549        } else {
    521           b.bottom_left.x = std::max(this->bottom_left.x,u.bottom_left.x);
    522           b.bottom_left.y = std::max(this->bottom_left.y,u.bottom_left.y);
    523           b.top_right.x = std::min(this->top_right.x,u.top_right.x);
    524           b.top_right.y = std::min(this->top_right.y,u.top_right.y);
    525           b._empty = b.bottom_left.x > b.top_right.x ||
    526                      b.bottom_left.y > b.top_right.y;
     550          b._bottom_left.x = std::max(_bottom_left.x, u._bottom_left.x);
     551          b._bottom_left.y = std::max(_bottom_left.y, u._bottom_left.y);
     552          b._top_right.x = std::min(_top_right.x, u._top_right.x);
     553          b._top_right.y = std::min(_top_right.y, u._top_right.y);
     554          b._empty = b._bottom_left.x > b._top_right.x ||
     555                     b._bottom_left.y > b._top_right.y;
    527556        }
    528557        return b;
    529558      }