lemon/dim2.h
changeset 255 914c5432be97
parent 250 d0aae16df1bb
child 313 64f8f7cc6168
     1.1 --- a/lemon/dim2.h	Tue Sep 02 00:44:17 2008 +0200
     1.2 +++ b/lemon/dim2.h	Tue Sep 02 10:23:23 2008 +0100
     1.3 @@ -28,8 +28,7 @@
     1.4  /// The class \ref lemon::dim2::Point "dim2::Point" implements
     1.5  /// a two dimensional vector with the usual operations.
     1.6  ///
     1.7 -/// The class \ref lemon::dim2::BoundingBox "dim2::BoundingBox"
     1.8 -/// can be used to determine
     1.9 +/// The class \ref lemon::dim2::Box "dim2::Box" can be used to determine
    1.10  /// the rectangular bounding box of a set of
    1.11  /// \ref lemon::dim2::Point "dim2::Point"'s.
    1.12  
    1.13 @@ -44,7 +43,7 @@
    1.14    /// \addtogroup misc
    1.15    /// @{
    1.16  
    1.17 -  /// A simple two dimensional vector (plain vector) implementation
    1.18 +  /// Two dimensional vector (plain vector)
    1.19  
    1.20    /// A simple two dimensional vector (plain vector) implementation
    1.21    /// with the usual vector operations.
    1.22 @@ -221,7 +220,7 @@
    1.23    template<typename T>
    1.24    inline std::ostream& operator<<(std::ostream &os, const Point<T>& z)
    1.25    {
    1.26 -    os << "(" << z.x << ", " << z.y << ")";
    1.27 +    os << "(" << z.x << "," << z.y << ")";
    1.28      return os;
    1.29    }
    1.30  
    1.31 @@ -260,67 +259,67 @@
    1.32  
    1.33  
    1.34  
    1.35 -    /// A class to calculate or store the bounding box of plain vectors.
    1.36 +  /// Bounding box of plain vectors (\ref Point points).
    1.37  
    1.38 -    /// A class to calculate or store the bounding box of plain vectors.
    1.39 -    ///
    1.40 -    template<typename T>
    1.41 -    class BoundingBox {
    1.42 +  /// A class to calculate or store the bounding box of plain vectors
    1.43 +  /// (\ref Point points).
    1.44 +  template<typename T>
    1.45 +  class Box {
    1.46        Point<T> _bottom_left, _top_right;
    1.47        bool _empty;
    1.48      public:
    1.49  
    1.50 -      ///Default constructor: creates an empty bounding box
    1.51 -      BoundingBox() { _empty = true; }
    1.52 +      ///Default constructor: creates an empty box
    1.53 +      Box() { _empty = true; }
    1.54  
    1.55 -      ///Construct an instance from one point
    1.56 -      BoundingBox(Point<T> a) {
    1.57 +      ///Construct a box from one point
    1.58 +      Box(Point<T> a) {
    1.59          _bottom_left = _top_right = a;
    1.60          _empty = false;
    1.61        }
    1.62  
    1.63 -      ///Construct an instance from two points
    1.64 +      ///Construct a box from two points
    1.65  
    1.66 -      ///Construct an instance from two points.
    1.67 +      ///Construct a box from two points.
    1.68        ///\param a The bottom left corner.
    1.69        ///\param b The top right corner.
    1.70        ///\warning The coordinates of the bottom left corner must be no more
    1.71        ///than those of the top right one.
    1.72 -      BoundingBox(Point<T> a,Point<T> b)
    1.73 +      Box(Point<T> a,Point<T> b)
    1.74        {
    1.75          _bottom_left = a;
    1.76          _top_right = b;
    1.77          _empty = false;
    1.78        }
    1.79  
    1.80 -      ///Construct an instance from four numbers
    1.81 +      ///Construct a box from four numbers
    1.82  
    1.83 -      ///Construct an instance from four numbers.
    1.84 +      ///Construct a box from four numbers.
    1.85        ///\param l The left side of the box.
    1.86        ///\param b The bottom of the box.
    1.87        ///\param r The right side of the box.
    1.88        ///\param t The top of the box.
    1.89        ///\warning The left side must be no more than the right side and
    1.90        ///bottom must be no more than the top.
    1.91 -      BoundingBox(T l,T b,T r,T t)
    1.92 +      Box(T l,T b,T r,T t)
    1.93        {
    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 -      ///Return \c true if the bounding box is empty.
   1.100 +      ///Return \c true if the box is empty.
   1.101  
   1.102 -      ///Return \c true if the bounding box is empty (i.e. return \c false
   1.103 +      ///Return \c true if the box is empty (i.e. return \c false
   1.104        ///if at least one point was added to the box or the coordinates of
   1.105        ///the box were set).
   1.106        ///
   1.107 -      ///The coordinates of an empty bounding box are not defined.
   1.108 +      ///The coordinates of an empty box are not defined.
   1.109        bool empty() const {
   1.110          return _empty;
   1.111        }
   1.112  
   1.113 -      ///Make the BoundingBox empty
   1.114 +      ///Make the box empty
   1.115        void clear() {
   1.116          _empty = true;
   1.117        }
   1.118 @@ -328,7 +327,7 @@
   1.119        ///Give back the bottom left corner of the box
   1.120  
   1.121        ///Give back the bottom left corner of the box.
   1.122 -      ///If the bounding box is empty, then the return value is not defined.
   1.123 +      ///If the box is empty, then the return value is not defined.
   1.124        Point<T> bottomLeft() const {
   1.125          return _bottom_left;
   1.126        }
   1.127 @@ -344,7 +343,7 @@
   1.128        ///Give back the top right corner of the box
   1.129  
   1.130        ///Give back the top right corner of the box.
   1.131 -      ///If the bounding box is empty, then the return value is not defined.
   1.132 +      ///If the box is empty, then the return value is not defined.
   1.133        Point<T> topRight() const {
   1.134          return _top_right;
   1.135        }
   1.136 @@ -360,7 +359,7 @@
   1.137        ///Give back the bottom right corner of the box
   1.138  
   1.139        ///Give back the bottom right corner of the box.
   1.140 -      ///If the bounding box is empty, then the return value is not defined.
   1.141 +      ///If the box is empty, then the return value is not defined.
   1.142        Point<T> bottomRight() const {
   1.143          return Point<T>(_top_right.x,_bottom_left.y);
   1.144        }
   1.145 @@ -377,7 +376,7 @@
   1.146        ///Give back the top left corner of the box
   1.147  
   1.148        ///Give back the top left corner of the box.
   1.149 -      ///If the bounding box is empty, then the return value is not defined.
   1.150 +      ///If the box is empty, then the return value is not defined.
   1.151        Point<T> topLeft() const {
   1.152          return Point<T>(_bottom_left.x,_top_right.y);
   1.153        }
   1.154 @@ -394,7 +393,7 @@
   1.155        ///Give back the bottom of the box
   1.156  
   1.157        ///Give back the bottom of the box.
   1.158 -      ///If the bounding box is empty, then the return value is not defined.
   1.159 +      ///If the box is empty, then the return value is not defined.
   1.160        T bottom() const {
   1.161          return _bottom_left.y;
   1.162        }
   1.163 @@ -410,7 +409,7 @@
   1.164        ///Give back the top of the box
   1.165  
   1.166        ///Give back the top of the box.
   1.167 -      ///If the bounding box is empty, then the return value is not defined.
   1.168 +      ///If the box is empty, then the return value is not defined.
   1.169        T top() const {
   1.170          return _top_right.y;
   1.171        }
   1.172 @@ -426,7 +425,7 @@
   1.173        ///Give back the left side of the box
   1.174  
   1.175        ///Give back the left side of the box.
   1.176 -      ///If the bounding box is empty, then the return value is not defined.
   1.177 +      ///If the box is empty, then the return value is not defined.
   1.178        T left() const {
   1.179          return _bottom_left.x;
   1.180        }
   1.181 @@ -442,7 +441,7 @@
   1.182        /// Give back the right side of the box
   1.183  
   1.184        /// Give back the right side of the box.
   1.185 -      ///If the bounding box is empty, then the return value is not defined.
   1.186 +      ///If the box is empty, then the return value is not defined.
   1.187        T right() const {
   1.188          return _top_right.x;
   1.189        }
   1.190 @@ -458,7 +457,7 @@
   1.191        ///Give back the height of the box
   1.192  
   1.193        ///Give back the height of the box.
   1.194 -      ///If the bounding box is empty, then the return value is not defined.
   1.195 +      ///If the box is empty, then the return value is not defined.
   1.196        T height() const {
   1.197          return _top_right.y-_bottom_left.y;
   1.198        }
   1.199 @@ -466,12 +465,12 @@
   1.200        ///Give back the width of the box
   1.201  
   1.202        ///Give back the width of the box.
   1.203 -      ///If the bounding box is empty, then the return value is not defined.
   1.204 +      ///If the box is empty, then the return value is not defined.
   1.205        T width() const {
   1.206          return _top_right.x-_bottom_left.x;
   1.207        }
   1.208  
   1.209 -      ///Checks whether a point is inside a bounding box
   1.210 +      ///Checks whether a point is inside the box
   1.211        bool inside(const Point<T>& u) const {
   1.212          if (_empty)
   1.213            return false;
   1.214 @@ -481,11 +480,11 @@
   1.215          }
   1.216        }
   1.217  
   1.218 -      ///Increments a bounding box with a point
   1.219 +      ///Increments the box with a point
   1.220  
   1.221 -      ///Increments a bounding box with a point.
   1.222 +      ///Increments the box with a point.
   1.223        ///
   1.224 -      BoundingBox& add(const Point<T>& u){
   1.225 +      Box& add(const Point<T>& u){
   1.226          if (_empty) {
   1.227            _bottom_left = _top_right = u;
   1.228            _empty = false;
   1.229 @@ -499,11 +498,11 @@
   1.230          return *this;
   1.231        }
   1.232  
   1.233 -      ///Increments a bounding box to contain another bounding box
   1.234 +      ///Increments the box to contain another box
   1.235  
   1.236 -      ///Increments a bounding box to contain another bounding box.
   1.237 +      ///Increments the box to contain another box.
   1.238        ///
   1.239 -      BoundingBox& add(const BoundingBox &u){
   1.240 +      Box& add(const Box &u){
   1.241          if ( !u.empty() ){
   1.242            add(u._bottom_left);
   1.243            add(u._top_right);
   1.244 @@ -511,12 +510,12 @@
   1.245          return *this;
   1.246        }
   1.247  
   1.248 -      ///Intersection of two bounding boxes
   1.249 +      ///Intersection of two boxes
   1.250  
   1.251 -      ///Intersection of two bounding boxes.
   1.252 +      ///Intersection of two boxes.
   1.253        ///
   1.254 -      BoundingBox operator&(const BoundingBox& u) const {
   1.255 -        BoundingBox b;
   1.256 +      Box operator&(const Box& u) const {
   1.257 +        Box b;
   1.258          if (_empty || u._empty) {
   1.259            b._empty = true;
   1.260          } else {
   1.261 @@ -530,9 +529,50 @@
   1.262          return b;
   1.263        }
   1.264  
   1.265 -    };//class Boundingbox
   1.266 +  };//class Box
   1.267  
   1.268  
   1.269 +  ///Read a box from a stream
   1.270 +
   1.271 +  ///Read a box from a stream.
   1.272 +  ///\relates Box
   1.273 +  template<typename T>
   1.274 +  inline std::istream& operator>>(std::istream &is, Box<T>& b) {
   1.275 +    char c;
   1.276 +    Point<T> p;
   1.277 +    if (is >> c) {
   1.278 +      if (c != '(') is.putback(c);
   1.279 +    } else {
   1.280 +      is.clear();
   1.281 +    }
   1.282 +    if (!(is >> p)) return is;
   1.283 +    b.bottomLeft(p);
   1.284 +    if (is >> c) {
   1.285 +      if (c != ',') is.putback(c);
   1.286 +    } else {
   1.287 +      is.clear();
   1.288 +    }
   1.289 +    if (!(is >> p)) return is;
   1.290 +    b.topRight(p);
   1.291 +    if (is >> c) {
   1.292 +      if (c != ')') is.putback(c);
   1.293 +    } else {
   1.294 +      is.clear();
   1.295 +    }
   1.296 +    return is;
   1.297 +  }
   1.298 +
   1.299 +  ///Write a box to a stream
   1.300 +
   1.301 +  ///Write a box to a stream.
   1.302 +  ///\relates Box
   1.303 +  template<typename T>
   1.304 +  inline std::ostream& operator<<(std::ostream &os, const Box<T>& b)
   1.305 +  {
   1.306 +    os << "(" << b.bottomLeft() << "," << b.topRight() << ")";
   1.307 +    return os;
   1.308 +  }
   1.309 +
   1.310    ///Map of x-coordinates of a \ref Point "Point"-map
   1.311  
   1.312    ///\ingroup maps