COIN-OR::LEMON - Graph Library

Ticket #126: dim2_bounding_box_2ccb860dfdcb.patch

File dim2_bounding_box_2ccb860dfdcb.patch, 10.9 KB (added by Peter Kovacs, 12 years ago)

Some cleanup in the implementation of dim2::BoundingBox?

  • lemon/dim2.h

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1216060940 -7200
    # Node ID 2ccb860dfdcb5152b4006558647e2e63b454bb0b
    # Parent  81cfc04531e8fcf0a6e72f22ff2871fcb827c416
    Some cleanup in the implementation of dim2::BoundingBox
    - Rename the private varibles to start with underscore.
    - empty() function works correctly in all cases.
      A bounding box is empty if _empty is true and _bottom_left <= _top_right.
    
    diff -r 81cfc04531e8 -r 2ccb860dfdcb lemon/dim2.h
    a b  
    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
    282285      ///Construct an instance from two points.
    283286      ///\param a The bottom left corner.
    284287      ///\param b The top right corner.
    285       ///\warning The coordinates of the bottom left corner must be no more
     288      ///\warning The coordinates of the bottom left corner should be no more
    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;
    292295      }
    293296
     
    298301      ///\param b The bottom of the box.
    299302      ///\param r The right side of the box.
    300303      ///\param t The top of the box.
    301       ///\warning The left side must be no more than the right side and
    302       ///bottom must be no more than the top.
     304      ///\warning The left side should be no more than the right side and
     305      ///bottom should be no more than the top.
    303306      BoundingBox(T l,T b,T r,T t)
    304307      {
    305         bottom_left=Point<T>(l,b);
    306         top_right=Point<T>(r,t);
     308        _bottom_left=Point<T>(l,b);
     309        _top_right=Point<T>(r,t);
    307310        _empty = false;
    308311      }
    309312
     
    311314
    312315      ///Return \c true if the bounding box is empty (i.e. return \c false
    313316      ///if at least one point was added to the box or the coordinates of
    314       ///the box were set).
     317      ///the box were set correctly).
    315318      ///
    316319      ///The coordinates of an empty bounding box are not defined.
    317320      bool empty() const {
    318         return _empty;
     321        return _empty || _bottom_left.x > _top_right.x
     322                      || _bottom_left.y > _top_right.y;
    319323      }
    320324
    321325      ///Make the BoundingBox empty
    322326      void clear() {
    323         _empty=1;
     327        _empty = true;
    324328      }
    325329
    326330      ///Give back the bottom left corner of the box
     
    328332      ///Give back the bottom left corner of the box.
    329333      ///If the bounding box is empty, then the return value is not defined.
    330334      Point<T> bottomLeft() const {
    331         return bottom_left;
     335        return _bottom_left;
    332336      }
    333337
    334338      ///Set the bottom left corner of the box
     
    336340      ///Set the bottom left corner of the box.
    337341      ///It should only be used for non-empty box.
    338342      void bottomLeft(Point<T> p) {
    339         bottom_left = p;
     343        _bottom_left = p;
     344        _empty = false;
    340345      }
    341346
    342347      ///Give back the top right corner of the box
     
    344349      ///Give back the top right corner of the box.
    345350      ///If the bounding box is empty, then the return value is not defined.
    346351      Point<T> topRight() const {
    347         return top_right;
     352        return _top_right;
    348353      }
    349354
    350355      ///Set the top right corner of the box
     
    352357      ///Set the top right corner of the box.
    353358      ///It should only be used for non-empty box.
    354359      void topRight(Point<T> p) {
    355         top_right = p;
     360        _top_right = p;
     361        _empty = false;
    356362      }
    357363
    358364      ///Give back the bottom right corner of the box
     
    360366      ///Give back the bottom right corner of the box.
    361367      ///If the bounding box is empty, then the return value is not defined.
    362368      Point<T> bottomRight() const {
    363         return Point<T>(top_right.x,bottom_left.y);
     369        return Point<T>(_top_right.x,_bottom_left.y);
    364370      }
    365371
    366372      ///Set the bottom right corner of the box
     
    368374      ///Set the bottom right corner of the box.
    369375      ///It should only be used for non-empty box.
    370376      void bottomRight(Point<T> p) {
    371         top_right.x = p.x;
    372         bottom_left.y = p.y;
     377        _top_right.x = p.x;
     378        _bottom_left.y = p.y;
     379        _empty = false;
    373380      }
    374381
    375382      ///Give back the top left corner of the box
     
    377384      ///Give back the top left corner of the box.
    378385      ///If the bounding box is empty, then the return value is not defined.
    379386      Point<T> topLeft() const {
    380         return Point<T>(bottom_left.x,top_right.y);
     387        return Point<T>(_bottom_left.x,_top_right.y);
    381388      }
    382389
    383390      ///Set the top left corner of the box
     
    385392      ///Set the top left corner of the box.
    386393      ///It should only be used for non-empty box.
    387394      void topLeft(Point<T> p) {
    388         top_right.y = p.y;
    389         bottom_left.x = p.x;
     395        _top_right.y = p.y;
     396        _bottom_left.x = p.x;
     397        _empty = false;
    390398      }
    391399
    392400      ///Give back the bottom of the box
     
    394402      ///Give back the bottom of the box.
    395403      ///If the bounding box is empty, then the return value is not defined.
    396404      T bottom() const {
    397         return bottom_left.y;
     405        return _bottom_left.y;
    398406      }
    399407
    400408      ///Set the bottom of the box
     
    402410      ///Set the bottom of the box.
    403411      ///It should only be used for non-empty box.
    404412      void bottom(T t) {
    405         bottom_left.y = t;
     413        _bottom_left.y = t;
     414        _empty = false;
    406415      }
    407416
    408417      ///Give back the top of the box
     
    410419      ///Give back the top of the box.
    411420      ///If the bounding box is empty, then the return value is not defined.
    412421      T top() const {
    413         return top_right.y;
     422        return _top_right.y;
    414423      }
    415424
    416425      ///Set the top of the box
     
    418427      ///Set the top of the box.
    419428      ///It should only be used for non-empty box.
    420429      void top(T t) {
    421         top_right.y = t;
     430        _top_right.y = t;
     431        _empty = false;
    422432      }
    423433
    424434      ///Give back the left side of the box
     
    426436      ///Give back the left side of the box.
    427437      ///If the bounding box is empty, then the return value is not defined.
    428438      T left() const {
    429         return bottom_left.x;
     439        return _bottom_left.x;
    430440      }
    431441
    432442      ///Set the left side of the box
     
    434444      ///Set the left side of the box.
    435445      ///It should only be used for non-empty box.
    436446      void left(T t) {
    437         bottom_left.x = t;
     447        _bottom_left.x = t;
     448        _empty = false;
    438449      }
    439450
    440451      /// Give back the right side of the box
     
    442453      /// Give back the right side of the box.
    443454      ///If the bounding box is empty, then the return value is not defined.
    444455      T right() const {
    445         return top_right.x;
     456        return _top_right.x;
    446457      }
    447458
    448459      ///Set the right side of the box
     
    450461      ///Set the right side of the box.
    451462      ///It should only be used for non-empty box.
    452463      void right(T t) {
    453         top_right.x = t;
     464        _top_right.x = t;
     465        _empty = false;
    454466      }
    455467
    456468      ///Give back the height of the box
     
    458470      ///Give back the height of the box.
    459471      ///If the bounding box is empty, then the return value is not defined.
    460472      T height() const {
    461         return top_right.y-bottom_left.y;
     473        return _top_right.y-_bottom_left.y;
    462474      }
    463475
    464476      ///Give back the width of the box
     
    466478      ///Give back the width of the box.
    467479      ///If the bounding box is empty, then the return value is not defined.
    468480      T width() const {
    469         return top_right.x-bottom_left.x;
     481        return _top_right.x-_bottom_left.x;
    470482      }
    471483
    472484      ///Checks whether a point is inside a bounding box
    473485      bool inside(const Point<T>& u) const {
    474         if (_empty)
     486        if ( empty() )
    475487          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 );
     488        else {
     489          return ( (u.x-_bottom_left.x)*(_top_right.x-u.x) >= 0 &&
     490                   (u.y-_bottom_left.y)*(_top_right.y-u.y) >= 0 );
    479491        }
    480492      }
    481493
     
    484496      ///Increments a bounding box with a point.
    485497      ///
    486498      BoundingBox& add(const Point<T>& u){
    487         if (_empty){
    488           bottom_left=top_right=u;
     499        if ( empty() ) {
     500          _bottom_left = _top_right = u;
    489501          _empty = false;
    490502        }
    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;
     503        else {
     504          if (_bottom_left.x > u.x) _bottom_left.x = u.x;
     505          if (_bottom_left.y > u.y) _bottom_left.y = u.y;
     506          if (_top_right.x < u.x) _top_right.x = u.x;
     507          if (_top_right.y < u.y) _top_right.y = u.y;
    496508        }
    497509        return *this;
    498510      }
     
    503515      ///
    504516      BoundingBox& add(const BoundingBox &u){
    505517        if ( !u.empty() ){
    506           this->add(u.bottomLeft());
    507           this->add(u.topRight());
     518          add(u._bottom_left);
     519          add(u._top_right);
    508520        }
    509521        return *this;
    510522      }
     
    515527      ///
    516528      BoundingBox operator&(const BoundingBox& u) const {
    517529        BoundingBox b;
    518         if (this->_empty || u._empty) {
     530        if ( empty() || u.empty() ) {
    519531          b._empty = true;
    520532        } 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;
     533          b._empty = false;
     534          b._bottom_left.x = std::max(_bottom_left.x, u._bottom_left.x);
     535          b._bottom_left.y = std::max(_bottom_left.y, u._bottom_left.y);
     536          b._top_right.x = std::min(_top_right.x, u._top_right.x);
     537          b._top_right.y = std::min(_top_right.y, u._top_right.y);
    527538        }
    528539        return b;
    529540      }