COIN-OR::LEMON - Graph Library

Ticket #126: dim2_bb_92f046dd7f6c.patch

File dim2_bb_92f046dd7f6c.patch, 10.8 KB (added by Peter Kovacs, 16 years ago)
  • lemon/dim2.h

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1217335276 -7200
    # Node ID 92f046dd7f6c38ba36b0bb656ee04dd8950544e7
    # Parent  b46d2787e9c26d9c434a65f48074ee2b80b3d991
    Improvements in dim2::BoundingBox (ticket #126)
    - Rename the private varibles to start with underscore.
    - Doc improvements.
    
    diff -r b46d2787e9c2 -r 92f046dd7f6c lemon/dim2.h
    a b  
    2020#define LEMON_DIM2_H
    2121
    2222#include <iostream>
    23 #include <lemon/core.h>
    2423
    2524///\ingroup misc
    2625///\file
     
    4544  /// \addtogroup misc
    4645  /// @{
    4746
    48   /// A simple two dimensional vector (plainvector) implementation
     47  /// A simple two dimensional vector (plain vector) implementation
    4948
    50   /// A simple two dimensional vector (plainvector) implementation
     49  /// A simple two dimensional vector (plain vector) implementation
    5150  /// with the usual vector operations.
    5251  template<typename T>
    5352    class Point {
     
    186185    return x*u;
    187186  }
    188187
    189   ///Read a plainvector from a stream
     188  ///Read a plain vector from a stream
    190189
    191   ///Read a plainvector from a stream.
     190  ///Read a plain vector from a stream.
    192191  ///\relates Point
    193192  ///
    194193  template<typename T>
     
    214213    return is;
    215214  }
    216215
    217   ///Write a plainvector to a stream
     216  ///Write a plain vector to a stream
    218217
    219   ///Write a plainvector to a stream.
     218  ///Write a plain vector to a stream.
    220219  ///\relates Point
    221220  ///
    222221  template<typename T>
     
    261260
    262261
    263262
    264   /// A class to calculate or store the bounding box of plainvectors.
     263    /// A class to calculate or store the bounding box of plain vectors.
    265264
    266   /// A class to calculate or store the bounding box of plainvectors.
    267   ///
     265    /// A class to calculate or store the bounding box of plain vectors.
     266    ///
    268267    template<typename T>
    269268    class BoundingBox {
    270       Point<T> bottom_left, top_right;
     269      Point<T> _bottom_left, _top_right;
    271270      bool _empty;
    272271    public:
    273272
     
    275274      BoundingBox() { _empty = true; }
    276275
    277276      ///Construct an instance from one point
    278       BoundingBox(Point<T> a) { bottom_left=top_right=a; _empty = false; }
     277      BoundingBox(Point<T> a) {
     278        _bottom_left = _top_right = a;
     279        _empty = false;
     280      }
    279281
    280282      ///Construct an instance from two points
    281283
     
    286288      ///than those of the top right one.
    287289      BoundingBox(Point<T> a,Point<T> b)
    288290      {
    289         bottom_left=a;
    290         top_right=b;
     291        _bottom_left = a;
     292        _top_right = b;
    291293        _empty = false;
    292294      }
    293295
     
    302304      ///bottom must be no more than the top.
    303305      BoundingBox(T l,T b,T r,T t)
    304306      {
    305         bottom_left=Point<T>(l,b);
    306         top_right=Point<T>(r,t);
     307        _bottom_left=Point<T>(l,b);
     308        _top_right=Point<T>(r,t);
    307309        _empty = false;
    308310      }
    309311
     
    320322
    321323      ///Make the BoundingBox empty
    322324      void clear() {
    323         _empty=1;
     325        _empty = true;
    324326      }
    325327
    326328      ///Give back the bottom left corner of the box
     
    328330      ///Give back the bottom left corner of the box.
    329331      ///If the bounding box is empty, then the return value is not defined.
    330332      Point<T> bottomLeft() const {
    331         return bottom_left;
     333        return _bottom_left;
    332334      }
    333335
    334336      ///Set the bottom left corner of the box
    335337
    336338      ///Set the bottom left corner of the box.
    337       ///It should only be used for non-empty box.
     339      ///\pre The box must not be empty.
    338340      void bottomLeft(Point<T> p) {
    339         bottom_left = p;
     341        _bottom_left = p;
    340342      }
    341343
    342344      ///Give back the top right corner of the box
     
    344346      ///Give back the top right corner of the box.
    345347      ///If the bounding box is empty, then the return value is not defined.
    346348      Point<T> topRight() const {
    347         return top_right;
     349        return _top_right;
    348350      }
    349351
    350352      ///Set the top right corner of the box
    351353
    352354      ///Set the top right corner of the box.
    353       ///It should only be used for non-empty box.
     355      ///\pre The box must not be empty.
    354356      void topRight(Point<T> p) {
    355         top_right = p;
     357        _top_right = p;
    356358      }
    357359
    358360      ///Give back the bottom right corner of the box
     
    360362      ///Give back the bottom right corner of the box.
    361363      ///If the bounding box is empty, then the return value is not defined.
    362364      Point<T> bottomRight() const {
    363         return Point<T>(top_right.x,bottom_left.y);
     365        return Point<T>(_top_right.x,_bottom_left.y);
    364366      }
    365367
    366368      ///Set the bottom right corner of the box
    367369
    368370      ///Set the bottom right corner of the box.
    369       ///It should only be used for non-empty box.
     371      ///\pre The box must not be empty.
    370372      void bottomRight(Point<T> p) {
    371         top_right.x = p.x;
    372         bottom_left.y = p.y;
     373        _top_right.x = p.x;
     374        _bottom_left.y = p.y;
    373375      }
    374376
    375377      ///Give back the top left corner of the box
     
    377379      ///Give back the top left corner of the box.
    378380      ///If the bounding box is empty, then the return value is not defined.
    379381      Point<T> topLeft() const {
    380         return Point<T>(bottom_left.x,top_right.y);
     382        return Point<T>(_bottom_left.x,_top_right.y);
    381383      }
    382384
    383385      ///Set the top left corner of the box
    384386
    385387      ///Set the top left corner of the box.
    386       ///It should only be used for non-empty box.
     388      ///\pre The box must not be empty.
    387389      void topLeft(Point<T> p) {
    388         top_right.y = p.y;
    389         bottom_left.x = p.x;
     390        _top_right.y = p.y;
     391        _bottom_left.x = p.x;
    390392      }
    391393
    392394      ///Give back the bottom of the box
     
    394396      ///Give back the bottom of the box.
    395397      ///If the bounding box is empty, then the return value is not defined.
    396398      T bottom() const {
    397         return bottom_left.y;
     399        return _bottom_left.y;
    398400      }
    399401
    400402      ///Set the bottom of the box
    401403
    402404      ///Set the bottom of the box.
    403       ///It should only be used for non-empty box.
     405      ///\pre The box must not be empty.
    404406      void bottom(T t) {
    405         bottom_left.y = t;
     407        _bottom_left.y = t;
    406408      }
    407409
    408410      ///Give back the top of the box
     
    410412      ///Give back the top of the box.
    411413      ///If the bounding box is empty, then the return value is not defined.
    412414      T top() const {
    413         return top_right.y;
     415        return _top_right.y;
    414416      }
    415417
    416418      ///Set the top of the box
    417419
    418420      ///Set the top of the box.
    419       ///It should only be used for non-empty box.
     421      ///\pre The box must not be empty.
    420422      void top(T t) {
    421         top_right.y = t;
     423        _top_right.y = t;
    422424      }
    423425
    424426      ///Give back the left side of the box
     
    426428      ///Give back the left side of the box.
    427429      ///If the bounding box is empty, then the return value is not defined.
    428430      T left() const {
    429         return bottom_left.x;
     431        return _bottom_left.x;
    430432      }
    431433
    432434      ///Set the left side of the box
    433435
    434436      ///Set the left side of the box.
    435       ///It should only be used for non-empty box.
     437      ///\pre The box must not be empty.
    436438      void left(T t) {
    437         bottom_left.x = t;
     439        _bottom_left.x = t;
    438440      }
    439441
    440442      /// Give back the right side of the box
     
    442444      /// Give back the right side of the box.
    443445      ///If the bounding box is empty, then the return value is not defined.
    444446      T right() const {
    445         return top_right.x;
     447        return _top_right.x;
    446448      }
    447449
    448450      ///Set the right side of the box
    449451
    450452      ///Set the right side of the box.
    451       ///It should only be used for non-empty box.
     453      ///\pre The box must not be empty.
    452454      void right(T t) {
    453         top_right.x = t;
     455        _top_right.x = t;
    454456      }
    455457
    456458      ///Give back the height of the box
     
    458460      ///Give back the height of the box.
    459461      ///If the bounding box is empty, then the return value is not defined.
    460462      T height() const {
    461         return top_right.y-bottom_left.y;
     463        return _top_right.y-_bottom_left.y;
    462464      }
    463465
    464466      ///Give back the width of the box
     
    466468      ///Give back the width of the box.
    467469      ///If the bounding box is empty, then the return value is not defined.
    468470      T width() const {
    469         return top_right.x-bottom_left.x;
     471        return _top_right.x-_bottom_left.x;
    470472      }
    471473
    472474      ///Checks whether a point is inside a bounding box
    473475      bool inside(const Point<T>& u) const {
    474476        if (_empty)
    475477          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 );
     478        else {
     479          return ( (u.x-_bottom_left.x)*(_top_right.x-u.x) >= 0 &&
     480                   (u.y-_bottom_left.y)*(_top_right.y-u.y) >= 0 );
    479481        }
    480482      }
    481483
     
    484486      ///Increments a bounding box with a point.
    485487      ///
    486488      BoundingBox& add(const Point<T>& u){
    487         if (_empty){
    488           bottom_left=top_right=u;
     489        if (_empty) {
     490          _bottom_left = _top_right = u;
    489491          _empty = false;
    490492        }
    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;
     493        else {
     494          if (_bottom_left.x > u.x) _bottom_left.x = u.x;
     495          if (_bottom_left.y > u.y) _bottom_left.y = u.y;
     496          if (_top_right.x < u.x) _top_right.x = u.x;
     497          if (_top_right.y < u.y) _top_right.y = u.y;
    496498        }
    497499        return *this;
    498500      }
     
    503505      ///
    504506      BoundingBox& add(const BoundingBox &u){
    505507        if ( !u.empty() ){
    506           this->add(u.bottomLeft());
    507           this->add(u.topRight());
     508          add(u._bottom_left);
     509          add(u._top_right);
    508510        }
    509511        return *this;
    510512      }
     
    515517      ///
    516518      BoundingBox operator&(const BoundingBox& u) const {
    517519        BoundingBox b;
    518         if (this->_empty || u._empty) {
     520        if (_empty || u._empty) {
    519521          b._empty = true;
    520522        } 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;
     523          b._bottom_left.x = std::max(_bottom_left.x, u._bottom_left.x);
     524          b._bottom_left.y = std::max(_bottom_left.y, u._bottom_left.y);
     525          b._top_right.x = std::min(_top_right.x, u._top_right.x);
     526          b._top_right.y = std::min(_top_right.y, u._top_right.y);
     527          b._empty = b._bottom_left.x > b._top_right.x ||
     528                     b._bottom_left.y > b._top_right.y;
    527529        }
    528530        return b;
    529531      }