Improvements in dim2::BoundingBox (ticket #126)
authorPeter Kovacs <kpeter@inf.elte.hu>
Tue, 29 Jul 2008 14:41:16 +0200
changeset 24192f046dd7f6c
parent 235 b46d2787e9c2
child 242 dbe3fc9c875d
Improvements in dim2::BoundingBox (ticket #126)
- Rename the private varibles to start with underscore.
- Doc improvements.
lemon/dim2.h
     1.1 --- a/lemon/dim2.h	Mon Jul 28 11:22:50 2008 +0200
     1.2 +++ b/lemon/dim2.h	Tue Jul 29 14:41:16 2008 +0200
     1.3 @@ -20,7 +20,6 @@
     1.4  #define LEMON_DIM2_H
     1.5  
     1.6  #include <iostream>
     1.7 -#include <lemon/core.h>
     1.8  
     1.9  ///\ingroup misc
    1.10  ///\file
    1.11 @@ -45,9 +44,9 @@
    1.12    /// \addtogroup misc
    1.13    /// @{
    1.14  
    1.15 -  /// A simple two dimensional vector (plainvector) implementation
    1.16 +  /// A simple two dimensional vector (plain vector) implementation
    1.17  
    1.18 -  /// A simple two dimensional vector (plainvector) implementation
    1.19 +  /// A simple two dimensional vector (plain vector) implementation
    1.20    /// with the usual vector operations.
    1.21    template<typename T>
    1.22      class Point {
    1.23 @@ -186,9 +185,9 @@
    1.24      return x*u;
    1.25    }
    1.26  
    1.27 -  ///Read a plainvector from a stream
    1.28 +  ///Read a plain vector from a stream
    1.29  
    1.30 -  ///Read a plainvector from a stream.
    1.31 +  ///Read a plain vector from a stream.
    1.32    ///\relates Point
    1.33    ///
    1.34    template<typename T>
    1.35 @@ -214,9 +213,9 @@
    1.36      return is;
    1.37    }
    1.38  
    1.39 -  ///Write a plainvector to a stream
    1.40 +  ///Write a plain vector to a stream
    1.41  
    1.42 -  ///Write a plainvector to a stream.
    1.43 +  ///Write a plain vector to a stream.
    1.44    ///\relates Point
    1.45    ///
    1.46    template<typename T>
    1.47 @@ -261,13 +260,13 @@
    1.48  
    1.49  
    1.50  
    1.51 -  /// A class to calculate or store the bounding box of plainvectors.
    1.52 +    /// A class to calculate or store the bounding box of plain vectors.
    1.53  
    1.54 -  /// A class to calculate or store the bounding box of plainvectors.
    1.55 -  ///
    1.56 +    /// A class to calculate or store the bounding box of plain vectors.
    1.57 +    ///
    1.58      template<typename T>
    1.59      class BoundingBox {
    1.60 -      Point<T> bottom_left, top_right;
    1.61 +      Point<T> _bottom_left, _top_right;
    1.62        bool _empty;
    1.63      public:
    1.64  
    1.65 @@ -275,7 +274,10 @@
    1.66        BoundingBox() { _empty = true; }
    1.67  
    1.68        ///Construct an instance from one point
    1.69 -      BoundingBox(Point<T> a) { bottom_left=top_right=a; _empty = false; }
    1.70 +      BoundingBox(Point<T> a) {
    1.71 +        _bottom_left = _top_right = a;
    1.72 +        _empty = false;
    1.73 +      }
    1.74  
    1.75        ///Construct an instance from two points
    1.76  
    1.77 @@ -286,8 +288,8 @@
    1.78        ///than those of the top right one.
    1.79        BoundingBox(Point<T> a,Point<T> b)
    1.80        {
    1.81 -        bottom_left=a;
    1.82 -        top_right=b;
    1.83 +        _bottom_left = a;
    1.84 +        _top_right = b;
    1.85          _empty = false;
    1.86        }
    1.87  
    1.88 @@ -302,8 +304,8 @@
    1.89        ///bottom must be no more than the top.
    1.90        BoundingBox(T l,T b,T r,T t)
    1.91        {
    1.92 -        bottom_left=Point<T>(l,b);
    1.93 -        top_right=Point<T>(r,t);
    1.94 +        _bottom_left=Point<T>(l,b);
    1.95 +        _top_right=Point<T>(r,t);
    1.96          _empty = false;
    1.97        }
    1.98  
    1.99 @@ -320,7 +322,7 @@
   1.100  
   1.101        ///Make the BoundingBox empty
   1.102        void clear() {
   1.103 -        _empty=1;
   1.104 +        _empty = true;
   1.105        }
   1.106  
   1.107        ///Give back the bottom left corner of the box
   1.108 @@ -328,15 +330,15 @@
   1.109        ///Give back the bottom left corner of the box.
   1.110        ///If the bounding box is empty, then the return value is not defined.
   1.111        Point<T> bottomLeft() const {
   1.112 -        return bottom_left;
   1.113 +        return _bottom_left;
   1.114        }
   1.115  
   1.116        ///Set the bottom left corner of the box
   1.117  
   1.118        ///Set the bottom left corner of the box.
   1.119 -      ///It should only be used for non-empty box.
   1.120 +      ///\pre The box must not be empty.
   1.121        void bottomLeft(Point<T> p) {
   1.122 -        bottom_left = p;
   1.123 +        _bottom_left = p;
   1.124        }
   1.125  
   1.126        ///Give back the top right corner of the box
   1.127 @@ -344,15 +346,15 @@
   1.128        ///Give back the top right corner of the box.
   1.129        ///If the bounding box is empty, then the return value is not defined.
   1.130        Point<T> topRight() const {
   1.131 -        return top_right;
   1.132 +        return _top_right;
   1.133        }
   1.134  
   1.135        ///Set the top right corner of the box
   1.136  
   1.137        ///Set the top right corner of the box.
   1.138 -      ///It should only be used for non-empty box.
   1.139 +      ///\pre The box must not be empty.
   1.140        void topRight(Point<T> p) {
   1.141 -        top_right = p;
   1.142 +        _top_right = p;
   1.143        }
   1.144  
   1.145        ///Give back the bottom right corner of the box
   1.146 @@ -360,16 +362,16 @@
   1.147        ///Give back the bottom right corner of the box.
   1.148        ///If the bounding box is empty, then the return value is not defined.
   1.149        Point<T> bottomRight() const {
   1.150 -        return Point<T>(top_right.x,bottom_left.y);
   1.151 +        return Point<T>(_top_right.x,_bottom_left.y);
   1.152        }
   1.153  
   1.154        ///Set the bottom right corner of the box
   1.155  
   1.156        ///Set the bottom right corner of the box.
   1.157 -      ///It should only be used for non-empty box.
   1.158 +      ///\pre The box must not be empty.
   1.159        void bottomRight(Point<T> p) {
   1.160 -        top_right.x = p.x;
   1.161 -        bottom_left.y = p.y;
   1.162 +        _top_right.x = p.x;
   1.163 +        _bottom_left.y = p.y;
   1.164        }
   1.165  
   1.166        ///Give back the top left corner of the box
   1.167 @@ -377,16 +379,16 @@
   1.168        ///Give back the top left corner of the box.
   1.169        ///If the bounding box is empty, then the return value is not defined.
   1.170        Point<T> topLeft() const {
   1.171 -        return Point<T>(bottom_left.x,top_right.y);
   1.172 +        return Point<T>(_bottom_left.x,_top_right.y);
   1.173        }
   1.174  
   1.175        ///Set the top left corner of the box
   1.176  
   1.177        ///Set the top left corner of the box.
   1.178 -      ///It should only be used for non-empty box.
   1.179 +      ///\pre The box must not be empty.
   1.180        void topLeft(Point<T> p) {
   1.181 -        top_right.y = p.y;
   1.182 -        bottom_left.x = p.x;
   1.183 +        _top_right.y = p.y;
   1.184 +        _bottom_left.x = p.x;
   1.185        }
   1.186  
   1.187        ///Give back the bottom of the box
   1.188 @@ -394,15 +396,15 @@
   1.189        ///Give back the bottom of the box.
   1.190        ///If the bounding box is empty, then the return value is not defined.
   1.191        T bottom() const {
   1.192 -        return bottom_left.y;
   1.193 +        return _bottom_left.y;
   1.194        }
   1.195  
   1.196        ///Set the bottom of the box
   1.197  
   1.198        ///Set the bottom of the box.
   1.199 -      ///It should only be used for non-empty box.
   1.200 +      ///\pre The box must not be empty.
   1.201        void bottom(T t) {
   1.202 -        bottom_left.y = t;
   1.203 +        _bottom_left.y = t;
   1.204        }
   1.205  
   1.206        ///Give back the top of the box
   1.207 @@ -410,15 +412,15 @@
   1.208        ///Give back the top of the box.
   1.209        ///If the bounding box is empty, then the return value is not defined.
   1.210        T top() const {
   1.211 -        return top_right.y;
   1.212 +        return _top_right.y;
   1.213        }
   1.214  
   1.215        ///Set the top of the box
   1.216  
   1.217        ///Set the top of the box.
   1.218 -      ///It should only be used for non-empty box.
   1.219 +      ///\pre The box must not be empty.
   1.220        void top(T t) {
   1.221 -        top_right.y = t;
   1.222 +        _top_right.y = t;
   1.223        }
   1.224  
   1.225        ///Give back the left side of the box
   1.226 @@ -426,15 +428,15 @@
   1.227        ///Give back the left side of the box.
   1.228        ///If the bounding box is empty, then the return value is not defined.
   1.229        T left() const {
   1.230 -        return bottom_left.x;
   1.231 +        return _bottom_left.x;
   1.232        }
   1.233  
   1.234        ///Set the left side of the box
   1.235  
   1.236        ///Set the left side of the box.
   1.237 -      ///It should only be used for non-empty box.
   1.238 +      ///\pre The box must not be empty.
   1.239        void left(T t) {
   1.240 -        bottom_left.x = t;
   1.241 +        _bottom_left.x = t;
   1.242        }
   1.243  
   1.244        /// Give back the right side of the box
   1.245 @@ -442,15 +444,15 @@
   1.246        /// Give back the right side of the box.
   1.247        ///If the bounding box is empty, then the return value is not defined.
   1.248        T right() const {
   1.249 -        return top_right.x;
   1.250 +        return _top_right.x;
   1.251        }
   1.252  
   1.253        ///Set the right side of the box
   1.254  
   1.255        ///Set the right side of the box.
   1.256 -      ///It should only be used for non-empty box.
   1.257 +      ///\pre The box must not be empty.
   1.258        void right(T t) {
   1.259 -        top_right.x = t;
   1.260 +        _top_right.x = t;
   1.261        }
   1.262  
   1.263        ///Give back the height of the box
   1.264 @@ -458,7 +460,7 @@
   1.265        ///Give back the height of the box.
   1.266        ///If the bounding box is empty, then the return value is not defined.
   1.267        T height() const {
   1.268 -        return top_right.y-bottom_left.y;
   1.269 +        return _top_right.y-_bottom_left.y;
   1.270        }
   1.271  
   1.272        ///Give back the width of the box
   1.273 @@ -466,16 +468,16 @@
   1.274        ///Give back the width of the box.
   1.275        ///If the bounding box is empty, then the return value is not defined.
   1.276        T width() const {
   1.277 -        return top_right.x-bottom_left.x;
   1.278 +        return _top_right.x-_bottom_left.x;
   1.279        }
   1.280  
   1.281        ///Checks whether a point is inside a bounding box
   1.282        bool inside(const Point<T>& u) const {
   1.283          if (_empty)
   1.284            return false;
   1.285 -        else{
   1.286 -          return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
   1.287 -              (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
   1.288 +        else {
   1.289 +          return ( (u.x-_bottom_left.x)*(_top_right.x-u.x) >= 0 &&
   1.290 +                   (u.y-_bottom_left.y)*(_top_right.y-u.y) >= 0 );
   1.291          }
   1.292        }
   1.293  
   1.294 @@ -484,15 +486,15 @@
   1.295        ///Increments a bounding box with a point.
   1.296        ///
   1.297        BoundingBox& add(const Point<T>& u){
   1.298 -        if (_empty){
   1.299 -          bottom_left=top_right=u;
   1.300 +        if (_empty) {
   1.301 +          _bottom_left = _top_right = u;
   1.302            _empty = false;
   1.303          }
   1.304 -        else{
   1.305 -          if (bottom_left.x > u.x) bottom_left.x = u.x;
   1.306 -          if (bottom_left.y > u.y) bottom_left.y = u.y;
   1.307 -          if (top_right.x < u.x) top_right.x = u.x;
   1.308 -          if (top_right.y < u.y) top_right.y = u.y;
   1.309 +        else {
   1.310 +          if (_bottom_left.x > u.x) _bottom_left.x = u.x;
   1.311 +          if (_bottom_left.y > u.y) _bottom_left.y = u.y;
   1.312 +          if (_top_right.x < u.x) _top_right.x = u.x;
   1.313 +          if (_top_right.y < u.y) _top_right.y = u.y;
   1.314          }
   1.315          return *this;
   1.316        }
   1.317 @@ -503,8 +505,8 @@
   1.318        ///
   1.319        BoundingBox& add(const BoundingBox &u){
   1.320          if ( !u.empty() ){
   1.321 -          this->add(u.bottomLeft());
   1.322 -          this->add(u.topRight());
   1.323 +          add(u._bottom_left);
   1.324 +          add(u._top_right);
   1.325          }
   1.326          return *this;
   1.327        }
   1.328 @@ -515,15 +517,15 @@
   1.329        ///
   1.330        BoundingBox operator&(const BoundingBox& u) const {
   1.331          BoundingBox b;
   1.332 -        if (this->_empty || u._empty) {
   1.333 +        if (_empty || u._empty) {
   1.334            b._empty = true;
   1.335          } else {
   1.336 -          b.bottom_left.x = std::max(this->bottom_left.x,u.bottom_left.x);
   1.337 -          b.bottom_left.y = std::max(this->bottom_left.y,u.bottom_left.y);
   1.338 -          b.top_right.x = std::min(this->top_right.x,u.top_right.x);
   1.339 -          b.top_right.y = std::min(this->top_right.y,u.top_right.y);
   1.340 -          b._empty = b.bottom_left.x > b.top_right.x ||
   1.341 -                     b.bottom_left.y > b.top_right.y;
   1.342 +          b._bottom_left.x = std::max(_bottom_left.x, u._bottom_left.x);
   1.343 +          b._bottom_left.y = std::max(_bottom_left.y, u._bottom_left.y);
   1.344 +          b._top_right.x = std::min(_top_right.x, u._top_right.x);
   1.345 +          b._top_right.y = std::min(_top_right.y, u._top_right.y);
   1.346 +          b._empty = b._bottom_left.x > b._top_right.x ||
   1.347 +                     b._bottom_left.y > b._top_right.y;
   1.348          }
   1.349          return b;
   1.350        }